@@ -5,7 +5,7 @@ namespace httpsserver {
5
5
6
6
HTTPSConnection::HTTPSConnection (ResourceResolver * resResolver):
7
7
HTTPConnection (resResolver) {
8
- _ssl = NULL ;
8
+ _ssl = esp_tls_init () ;
9
9
}
10
10
11
11
HTTPSConnection::~HTTPSConnection () {
@@ -22,35 +22,30 @@ bool HTTPSConnection::isSecure() {
22
22
*
23
23
* The call WILL BLOCK if accept(serverSocketID) blocks. So use select() to check for that in advance.
24
24
*/
25
- int HTTPSConnection::initialize (int serverSocketID, SSL_CTX * sslCtx , HTTPHeaders *defaultHeaders) {
25
+ int HTTPSConnection::initialize (int serverSocketID, esp_tls_cfg_server * cfgSrv , HTTPHeaders *defaultHeaders) {
26
26
if (_connectionState == STATE_UNDEFINED) {
27
27
// Let the base class connect the plain tcp socket
28
28
int resSocket = HTTPConnection::initialize (serverSocketID, defaultHeaders);
29
29
30
+ HTTPS_LOGI (" Cert len:%d, apn:%s\n " , cfgSrv->servercert_bytes , cfgSrv->alpn_protos [0 ]);
31
+
30
32
// Build up SSL Connection context if the socket has been created successfully
31
33
if (resSocket >= 0 ) {
32
34
33
- _ssl = SSL_new (sslCtx );
35
+ int res = esp_tls_server_session_create (cfgSrv, resSocket, _ssl );
34
36
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;
39
40
40
- // Perform the handshake
41
- success = SSL_accept (_ssl);
42
- if (success) {
41
+ if (ESP_OK == esp_tls_get_conn_sockfd (_ssl, &resSocket)){
43
42
return resSocket;
44
43
} else {
45
44
HTTPS_LOGE (" SSL_accept failed. Aborting handshake. FID=%d" , resSocket);
46
45
}
47
- } else {
48
- HTTPS_LOGE (" SSL_set_fd failed. Aborting handshake. FID=%d" , resSocket);
49
- }
50
46
} else {
51
- HTTPS_LOGE (" SSL_new failed. Aborting handshake. FID =%d" , resSocket );
47
+ HTTPS_LOGE (" SSL_new failed. Aborting handshake. Error =%d" , res );
52
48
}
53
-
54
49
} else {
55
50
HTTPS_LOGE (" Could not accept() new connection. FID=%d" , resSocket);
56
51
}
@@ -84,18 +79,10 @@ void HTTPSConnection::closeConnection() {
84
79
85
80
// Try to tear down SSL while we are in the _shutdownTS timeout period or if an error occurred
86
81
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;
99
86
}
100
87
101
88
// If SSL has been brought down, close the socket
@@ -105,19 +92,19 @@ void HTTPSConnection::closeConnection() {
105
92
}
106
93
107
94
size_t HTTPSConnection::writeBuffer (byte* buffer, size_t length) {
108
- return SSL_write (_ssl, buffer, length);
95
+ return esp_tls_conn_write (_ssl, buffer, length);
109
96
}
110
97
111
98
size_t HTTPSConnection::readBytesToBuffer (byte* buffer, size_t length) {
112
- return SSL_read (_ssl, buffer, length);
99
+ return esp_tls_conn_read (_ssl, buffer, length);
113
100
}
114
101
115
102
size_t HTTPSConnection::pendingByteCount () {
116
- return SSL_pending (_ssl);
103
+ return esp_tls_get_bytes_avail (_ssl);
117
104
}
118
105
119
106
bool HTTPSConnection::canReadData () {
120
- return HTTPConnection::canReadData () || (SSL_pending (_ssl) > 0 );
107
+ return HTTPConnection::canReadData () || (esp_tls_get_bytes_avail (_ssl) > 0 );
121
108
}
122
109
123
110
} /* namespace httpsserver */
0 commit comments