@@ -5,7 +5,7 @@ namespace httpsserver {
55
66HTTPSConnection::HTTPSConnection (ResourceResolver * resResolver):
77 HTTPConnection (resResolver) {
8- _ssl = NULL ;
8+ _ssl = esp_tls_init () ;
99}
1010
1111HTTPSConnection::~HTTPSConnection () {
@@ -22,36 +22,30 @@ bool HTTPSConnection::isSecure() {
2222 *
2323 * The call WILL BLOCK if accept(serverSocketID) blocks. So use select() to check for that in advance.
2424 */
25- int HTTPSConnection::initialize (int serverSocketID, SSL_CTX * sslCtx , HTTPHeaders *defaultHeaders) {
25+ int HTTPSConnection::initialize (int serverSocketID, esp_tls_cfg_server_t * cfgSrv , HTTPHeaders *defaultHeaders) {
2626 if (_connectionState == STATE_UNDEFINED) {
2727 // Let the base class connect the plain tcp socket
2828 int resSocket = HTTPConnection::initialize (serverSocketID, defaultHeaders);
29-
29+ HTTPS_LOGI ( " Cert len:%d, apn:%s \n " ,cfgSrv-> servercert_bytes ,cfgSrv-> alpn_protos [ 0 ]);
3030 // Build up SSL Connection context if the socket has been created successfully
3131 if (resSocket >= 0 ) {
32-
33- _ssl = SSL_new (sslCtx);
34-
35- if (_ssl) {
32+ int res= esp_tls_server_session_create (cfgSrv,resSocket,_ssl);
33+ if ( 0 ==res) {
34+ esp_tls_cfg_server_session_tickets_init (cfgSrv);
35+ _cfg = cfgSrv;
3636 // Bind SSL to the socket
37- int success = SSL_set_fd (_ssl, resSocket);
38- if (success) {
39-
40- // Perform the handshake
41- success = SSL_accept (_ssl);
42- if (success) {
43- return resSocket;
44- } else {
45- HTTPS_LOGE (" SSL_accept failed. Aborting handshake. FID=%d" , resSocket);
46- }
47- } else {
48- HTTPS_LOGE (" SSL_set_fd failed. Aborting handshake. FID=%d" , resSocket);
37+ if (ESP_OK == esp_tls_get_conn_sockfd (_ssl,&resSocket)) {
38+ return resSocket;
39+ }
40+ else {
41+ HTTPS_LOGE (" SSL_accept failed. Aborting handshake. FID=%d" , resSocket);
4942 }
50- } else {
51- HTTPS_LOGE (" SSL_new failed. Aborting handshake. FID=%d" , resSocket);
43+ }
44+ else {
45+ HTTPS_LOGE (" SSL_new failed. Aborting handshake. Error=%d" , res);
5246 }
53-
54- } else {
47+ }
48+ else {
5549 HTTPS_LOGE (" Could not accept() new connection. FID=%d" , resSocket);
5650 }
5751
@@ -66,9 +60,7 @@ int HTTPSConnection::initialize(int serverSocketID, SSL_CTX * sslCtx, HTTPHeader
6660 return -1 ;
6761}
6862
69-
7063void HTTPSConnection::closeConnection () {
71-
7264 // FIXME: Copy from HTTPConnection, could be done better probably
7365 if (_connectionState != STATE_ERROR && _connectionState != STATE_CLOSED) {
7466
@@ -81,47 +73,37 @@ void HTTPSConnection::closeConnection() {
8173 // correctly
8274 _connectionState = STATE_CLOSING;
8375 }
84-
8576 // Try to tear down SSL while we are in the _shutdownTS timeout period or if an error occurred
8677 if (_ssl) {
87- if (_connectionState == STATE_ERROR || SSL_shutdown (_ssl) == 0 ) {
88- // SSL_shutdown will return 1 as soon as the client answered with close notify
89- // This means we are safe to close the socket
90- SSL_free (_ssl);
91- _ssl = NULL ;
92- } else if (_shutdownTS + HTTPS_SHUTDOWN_TIMEOUT < millis ()) {
93- // The timeout has been hit, we force SSL shutdown now by freeing the context
94- SSL_free (_ssl);
95- _ssl = NULL ;
96- HTTPS_LOGW (" SSL_shutdown did not receive close notification from the client" );
97- _connectionState = STATE_ERROR;
98- }
78+ esp_tls_cfg_server_session_tickets_free (_cfg);
79+ esp_tls_server_session_delete (_ssl);
80+ _ssl = NULL ;
81+ _connectionState = STATE_ERROR;
9982 }
100-
10183 // If SSL has been brought down, close the socket
10284 if (!_ssl) {
10385 HTTPConnection::closeConnection ();
10486 }
10587}
10688
10789size_t HTTPSConnection::writeBuffer (byte* buffer, size_t length) {
108- return SSL_write (_ssl, buffer, length);
90+ return esp_tls_conn_write (_ssl,buffer,length);
10991}
11092
11193size_t HTTPSConnection::readBytesToBuffer (byte* buffer, size_t length) {
112- int ret = SSL_read (_ssl, buffer, length);
94+ int ret = esp_tls_conn_read (_ssl, buffer, length);
11395 if (ret < 0 ) {
11496 HTTPS_LOGD (" SSL_read error: %d" , SSL_get_error (_ssl, ret));
11597 }
11698 return ret;
11799}
118100
119101size_t HTTPSConnection::pendingByteCount () {
120- return SSL_pending (_ssl);
102+ return esp_tls_get_bytes_avail (_ssl);
121103}
122104
123105bool HTTPSConnection::canReadData () {
124- return HTTPConnection::canReadData () || (SSL_pending (_ssl) > 0 );
106+ return HTTPConnection::canReadData () || (esp_tls_get_bytes_avail (_ssl) > 0 );
125107}
126108
127109} /* namespace httpsserver */
0 commit comments