Example CyaSSL SSL client connects to SSL server

Dependencies:   EthernetNetIf mbed CyaSSL

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetNetIf.h"
00003 #include "TCPSocket.h"
00004 #include "ssl.h"
00005 
00006 
00007 /* CyaSSL example client
00008 
00009 This example uses a static IP Address of 192.168.2.4
00010 Change that in the eth variable if yours is different
00011 
00012 If you're using dhcp uncomment the dhcp line and comment out the static ip
00013 
00014 This example assumes the SSL server is at 10.0.1.2 on port 11111
00015 Change those in the main loop if yours is differnt
00016 
00017 The example connects and then writes "secure hello from mbed" to server
00018 Any repsonse will written to stdout
00019 
00020 */
00021 
00022 //EthernetNetIf eth;   // dhcp
00023 
00024 EthernetNetIf eth(
00025   IpAddr(192,168,2,4), //IP Address
00026   IpAddr(255,255,255,0), //Network Mask
00027   IpAddr(192,168,2,1), //Gateway
00028   IpAddr(192,168,2,1)  //DNS
00029 );
00030 
00031 
00032 TCPSocket sock;
00033 SSL_CTX* ctx = 0;
00034 SSL*     ssl = 0;
00035 int      SSL_connected = 0;
00036 
00037 
00038 
00039 int recvf(char* buf, int sz, void* vp)
00040 {
00041     int got = sock.recv(buf, sz);
00042     
00043     if (got == 0)
00044         return -2;  // IO_ERR_WANT_READ;
00045     
00046     return got;
00047 }
00048 
00049 
00050 int sendf(char* buf, int sz, void* vp)
00051 {
00052 
00053     int sent = sock.send(buf, sz);
00054     
00055     if (sent == 0)
00056         return -2;  // IO_ERR_WANT_WRITE
00057         
00058     return sent;
00059 }
00060 
00061 
00062 void err_str(const char* from, SSL* ssl)
00063 {
00064     int  err;
00065     char str[80];
00066     
00067     printf("got error from %s\n", from);
00068     err = SSL_get_error(ssl, 0);
00069     if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE)
00070         printf("non-blocking IO couldn't complete, will continue when can\n");
00071     else
00072         printf("err = %d, %s\n", err, ERR_error_string(err, str));
00073 }
00074 
00075 
00076 void onTCPSocketEvent(TCPSocketEvent e)
00077 {
00078     int err;
00079     char buffer[1024];
00080     
00081     switch (e) {
00082         case TCPSOCKET_CONNECTED :
00083             printf("we connected\n");
00084 
00085             ctx = SSL_CTX_new(TLSv1_client_method());
00086             
00087             if (ctx == 0) {
00088                 printf("oops, bad SSL ctx\n");
00089                 break;
00090             }
00091                 
00092             SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
00093             
00094             CyaSSL_SetIORecv(ctx, recvf);
00095             CyaSSL_SetIOSend(ctx, sendf);
00096             
00097             
00098             ssl = SSL_new(ctx);
00099             
00100             if (ssl == 0) {
00101                 printf("oops, bad SSL ptr\n");
00102                 break;
00103             }
00104             
00105             CyaSSL_SetIOReadCtx(ssl, (void*)&sock);
00106             CyaSSL_SetIOWriteCtx(ssl, (void*)&sock);
00107             err = SSL_connect(ssl);
00108             
00109             if (err != SSL_SUCCESS) {
00110                 err_str("SSL connect", ssl);
00111             }             
00112             break;
00113             
00114         case TCPSOCKET_READABLE :
00115             printf("we're readable\n");
00116             
00117             if (SSL_connected == 0) {
00118                 err = SSL_connect(ssl);
00119                 if (err < 0) {
00120                     err_str("SSL connect", ssl);
00121                 }
00122                 else {
00123                     const char msg[] = "secure hello from mbed\n";
00124                     SSL_connected = 1;
00125                     printf("we did SSL connect!\n");
00126                     err = SSL_write(ssl, msg, sizeof(msg));
00127                     if (err < 0) {
00128                         err_str("SSL wirte", ssl);
00129                     }
00130                 }
00131             } else {
00132                 err = SSL_read(ssl, buffer, sizeof(buffer));
00133                 if (err < 0) {
00134                     err_str("SSL read", ssl);
00135                 }
00136                 else {
00137                     buffer[err] = 0;
00138                     printf("got message %s\n", buffer);
00139                 }
00140             }  
00141             break;
00142             
00143         case TCPSOCKET_WRITEABLE :
00144         
00145             break;
00146        
00147         default:
00148             printf("default, case e = %d\n", e);
00149             break;
00150     }
00151 }
00152 
00153 
00154 
00155 int main() {
00156    
00157     EthernetErr ethErr = eth.setup();
00158     if(ethErr)
00159     {
00160         printf("Error %d in setup.\n", ethErr);
00161         return -1;
00162     }
00163     printf("\r\nSetup OK\r\n");
00164 
00165     sock.setOnEvent(&onTCPSocketEvent);
00166     
00167     Host server(IpAddr(10,0,1,2), 11111);
00168     TCPSocketErr bindErr = sock.connect(server);
00169     
00170     printf("socket connect ret = %d\n", bindErr);
00171    
00172   
00173     while(1) {
00174        Net::poll();
00175     }
00176 }