This is a fork of the mbed port of axTLS
Dependents: TLS_axTLS-Example HTTPSClientExample
TLSConnection.cpp
00001 #include "TLSConnection.h" 00002 #include <stdlib.h> 00003 #include <stdio.h> 00004 const static int HTTPS_PORT = 443; 00005 00006 TLSConnection::TLSConnection(): 00007 Socket(), 00008 Endpoint(), 00009 _is_connected(false), 00010 _ssl_ctx(), 00011 _ssl() 00012 { 00013 } 00014 00015 bool TLSConnection::connect(const char *host) 00016 { 00017 if (init_socket(SOCK_STREAM) < 0) 00018 return false; 00019 00020 if (set_address(host, HTTPS_PORT) != 0) 00021 return false; 00022 00023 if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) { 00024 close(); 00025 return false; 00026 } 00027 00028 if(ssl_ctx_new(&_ssl_ctx, 0, SSL_DEFAULT_CLNT_SESS) != &_ssl_ctx) 00029 return false; 00030 00031 _ssl.ssl_ctx = &_ssl_ctx; 00032 00033 if(ssl_client_new(&_ssl, _sock_fd, NULL, 0) == NULL) { 00034 close(); 00035 return false; 00036 } 00037 if(_ssl.hs_status != SSL_OK) { 00038 close(); 00039 return false; 00040 } 00041 00042 _is_connected = true; 00043 00044 return true; 00045 } 00046 00047 bool TLSConnection::is_connected(void) 00048 { 00049 return _is_connected; 00050 } 00051 00052 int TLSConnection::send_all(char *data, int length) 00053 { 00054 if ((_sock_fd < 0) || !_is_connected) 00055 return -1; 00056 00057 return ssl_write(&_ssl, (uint8_t*)data, length); 00058 } 00059 00060 int TLSConnection::receive(char *data, int length) 00061 { 00062 return ssl_read(&_ssl, (uint8_t*)data, length); 00063 } 00064 00065 bool TLSConnection::close(bool shutdown) 00066 { 00067 if(!_is_connected) 00068 return true; 00069 00070 _is_connected = false; 00071 ssl_ctx_free(_ssl.ssl_ctx); 00072 00073 return Socket::close(shutdown) == 0; 00074 } 00075
Generated on Wed Jul 13 2022 19:30:08 by 1.7.2