@@ -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,35 +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 * 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);
2929
30+ HTTPS_LOGI (" Cert len:%d, apn:%s\n " , cfgSrv->servercert_bytes , cfgSrv->alpn_protos [0 ]);
31+
3032 // Build up SSL Connection context if the socket has been created successfully
3133 if (resSocket >= 0 ) {
3234
33- _ssl = SSL_new (sslCtx );
35+ int res = esp_tls_server_session_create (cfgSrv, resSocket, _ssl );
3436
35- if (_ssl) {
36- // Bind SSL to the socket
37- int success = SSL_set_fd (_ssl, resSocket);
38- if (success) {
37+ if (0 == res) {
38+ esp_tls_cfg_server_session_tickets_init (cfgSrv);
39+ _cfg = cfgSrv;
3940
40- // Perform the handshake
41- success = SSL_accept (_ssl);
42- if (success) {
41+ if (ESP_OK == esp_tls_get_conn_sockfd (_ssl, &resSocket)){
4342 return resSocket;
4443 } else {
4544 HTTPS_LOGE (" SSL_accept failed. Aborting handshake. FID=%d" , resSocket);
4645 }
47- } else {
48- HTTPS_LOGE (" SSL_set_fd failed. Aborting handshake. FID=%d" , resSocket);
49- }
5046 } else {
51- HTTPS_LOGE (" SSL_new failed. Aborting handshake. FID =%d" , resSocket );
47+ HTTPS_LOGE (" SSL_new failed. Aborting handshake. Error =%d" , res );
5248 }
53-
5449 } else {
5550 HTTPS_LOGE (" Could not accept() new connection. FID=%d" , resSocket);
5651 }
@@ -84,18 +79,10 @@ void HTTPSConnection::closeConnection() {
8479
8580 // Try to tear down SSL while we are in the _shutdownTS timeout period or if an error occurred
8681 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- }
82+ esp_tls_cfg_server_session_tickets_free (_cfg);
83+ esp_tls_server_session_delete (_ssl);
84+ _ssl = NULL ;
85+ _connectionState = STATE_ERROR;
9986 }
10087
10188 // If SSL has been brought down, close the socket
@@ -105,19 +92,19 @@ void HTTPSConnection::closeConnection() {
10592}
10693
10794size_t HTTPSConnection::writeBuffer (byte* buffer, size_t length) {
108- return SSL_write (_ssl, buffer, length);
95+ return esp_tls_conn_write (_ssl, buffer, length);
10996}
11097
11198size_t HTTPSConnection::readBytesToBuffer (byte* buffer, size_t length) {
112- return SSL_read (_ssl, buffer, length);
99+ return esp_tls_conn_read (_ssl, buffer, length);
113100}
114101
115102size_t HTTPSConnection::pendingByteCount () {
116- return SSL_pending (_ssl);
103+ return esp_tls_get_bytes_avail (_ssl);
117104}
118105
119106bool HTTPSConnection::canReadData () {
120- return HTTPConnection::canReadData () || (SSL_pending (_ssl) > 0 );
107+ return HTTPConnection::canReadData () || (esp_tls_get_bytes_avail (_ssl) > 0 );
121108}
122109
123110} /* namespace httpsserver */
0 commit comments