Michael Ansolis / TLS_cyassl

Dependencies:   cyassl-lib

Fork of TLS_cyassl by Francois Berder

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TLSConnection.cpp Source File

TLSConnection.cpp

00001 #include "TLSConnection.h"
00002 #include <stdlib.h>
00003 #include <stdio.h>
00004 #include "cert.h"
00005 #include <string.h>
00006 
00007 static int receiveFunc(CYASSL* ssl, char *buf, int sz, void *ctx)
00008 {    
00009     int fd = *(int*)ctx;
00010     fd_set rfds;
00011     FD_ZERO(&rfds);
00012     FD_SET(fd, &rfds);
00013     
00014     if (lwip_select(FD_SETSIZE, &rfds, NULL, NULL, NULL) < 0)
00015         return -1;
00016             
00017     return lwip_recv(fd, buf, sz, 0);
00018 }
00019 
00020 static int sendFunc(CYASSL* ssl, char *buf, int sz, void *ctx)
00021 {
00022     int fd = *(int*)ctx;
00023     fd_set wfds;
00024     FD_ZERO(&wfds);
00025     FD_SET(fd, &wfds);
00026     
00027     if (lwip_select(FD_SETSIZE, NULL, &wfds, NULL, NULL) < 0)
00028         return -1;
00029             
00030     return lwip_send(fd, buf, sz, 0);    
00031 }
00032 
00033 const static int HTTPS_PORT = 443;
00034 
00035 TLSConnection::TLSConnection():
00036     Socket(),
00037     Endpoint(),
00038     _is_connected(false),
00039     _ssl_ctx(),
00040     _ssl()
00041 {
00042 }
00043 
00044 
00045 bool TLSConnection::connect(const char *host)
00046 {
00047     if (init_socket(SOCK_STREAM) < 0)
00048         return false;
00049 
00050     if (set_address(host, HTTPS_PORT) != 0)
00051         return false;
00052 
00053     if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) {
00054         close();
00055         return false;
00056     }
00057 
00058     CyaSSL_Init();
00059     
00060     CYASSL_METHOD* method = CyaTLSv1_2_client_method();
00061     if(method == NULL)
00062     {
00063         return false;
00064     }
00065 
00066     _ssl_ctx = CyaSSL_CTX_new(method);
00067     if(_ssl_ctx == NULL)
00068     {
00069         return false;
00070     }
00071     CyaSSL_CTX_set_verify(_ssl_ctx, SSL_VERIFY_NONE, 0); 
00072     CyaSSL_SetIOSend(_ssl_ctx, &sendFunc);
00073     CyaSSL_SetIORecv(_ssl_ctx, &receiveFunc);
00074     CyaSSL_CTX_load_verify_buffer(_ssl_ctx,(unsigned char*)root_cert, root_cert_len,SSL_FILETYPE_ASN1);
00075 
00076     _ssl = CyaSSL_new(_ssl_ctx);
00077     if(_ssl == NULL) 
00078     {
00079         return false;
00080     }
00081     CyaSSL_set_fd(_ssl, _sock_fd);
00082 
00083     int result = CyaSSL_connect(_ssl);
00084     if(result!=SSL_SUCCESS) 
00085     {
00086         printf("error=%d\n", result);
00087         return false;
00088     }  
00089 
00090     _is_connected = true;
00091 
00092     return true;
00093 }
00094 
00095 bool TLSConnection::is_connected(void)
00096 {
00097     return _is_connected;
00098 }
00099 
00100 int TLSConnection::send_all(char *data, int length)
00101 {
00102     if(!_is_connected)
00103         return 0;
00104         
00105     return CyaSSL_write(_ssl, data, length);
00106 }
00107 
00108 int TLSConnection::receive(char *data, int length)
00109 {
00110     if(!_is_connected)
00111         return 0;
00112         
00113     return CyaSSL_read(_ssl, data, length);
00114 }
00115 
00116 bool TLSConnection::close(bool shutdown)
00117 {
00118     if(!_is_connected)
00119         return true;
00120 
00121     _is_connected = false;
00122 
00123     CyaSSL_free(_ssl);
00124     CyaSSL_CTX_free(_ssl_ctx);
00125     CyaSSL_Cleanup();
00126 
00127     return Socket::close(shutdown) == 0;
00128 }
00129