Skip to content

Commit 4afc790

Browse files
committed
merging pull request fhessel#169
1 parent de1876c commit 4afc790

9 files changed

+54
-96
lines changed

Diff for: src/ConnectionContext.hpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
#include <IPAddress.h>
66

77
// Required for SSL
8-
#include "openssl/ssl.h"
9-
#undef read
8+
#include <esp_tls.h>
109

1110
namespace httpsserver {
1211

Diff for: src/HTTPConnection.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ void handleWebsocketHandshake(HTTPRequest * req, HTTPResponse * res) {
664664
std::string websocketKeyResponseHash(std::string const &key) {
665665
std::string newKey = key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
666666
uint8_t shaData[HTTPS_SHA1_LENGTH];
667-
esp_sha(SHA1, (uint8_t*)newKey.data(), newKey.length(), shaData);
667+
mbedtls_sha1_ret(SHA1, (uint8_t*)newKey.data(), newKey.length(), shaData);
668668

669669
// Get output size required for base64 representation
670670
size_t b64BufferSize = 0;

Diff for: src/HTTPConnection.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include <string>
88
#include <mbedtls/base64.h>
9-
#include <hwcrypto/sha.h>
9+
#include <mbedtls/sha1.h>
1010
#include <functional>
1111

1212
// Required for sockets

Diff for: src/HTTPResponse.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#undef write
1010
#include <vector>
1111

12-
#include <openssl/ssl.h>
12+
#include <esp_tls.h>
1313

1414
#include "util.hpp"
1515

Diff for: src/HTTPSConnection.cpp

+18-31
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace httpsserver {
55

66
HTTPSConnection::HTTPSConnection(ResourceResolver * resResolver):
77
HTTPConnection(resResolver) {
8-
_ssl = NULL;
8+
_ssl = esp_tls_init();
99
}
1010

1111
HTTPSConnection::~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

10794
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);
10996
}
11097

11198
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);
113100
}
114101

115102
size_t HTTPSConnection::pendingByteCount() {
116-
return SSL_pending(_ssl);
103+
return esp_tls_get_bytes_avail(_ssl);
117104
}
118105

119106
bool 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 */

Diff for: src/HTTPSConnection.hpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#include <string>
77

88
// Required for SSL
9-
#include "openssl/ssl.h"
10-
#undef read
9+
#include <esp_tls.h>
10+
1111

1212
// Required for sockets
1313
#include "lwip/netdb.h"
@@ -34,7 +34,7 @@ class HTTPSConnection : public HTTPConnection {
3434
HTTPSConnection(ResourceResolver * resResolver);
3535
virtual ~HTTPSConnection();
3636

37-
virtual int initialize(int serverSocketID, SSL_CTX * sslCtx, HTTPHeaders *defaultHeaders);
37+
virtual int initialize(int serverSocketID, esp_tls_cfg_server_t * cfgSrv, HTTPHeaders *defaultHeaders);
3838
virtual void closeConnection();
3939
virtual bool isSecure();
4040

@@ -49,7 +49,8 @@ class HTTPSConnection : public HTTPConnection {
4949

5050
private:
5151
// SSL context for this connection
52-
SSL * _ssl;
52+
esp_tls_t * _ssl;
53+
esp_tls_cfg_server_t * _cfg;
5354

5455
};
5556

Diff for: src/HTTPSServer.cpp

+21-51
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,41 @@
22

33
namespace httpsserver {
44

5+
constexpr const char * alpn_protos[] = {"http/1.1", NULL};
56

67
HTTPSServer::HTTPSServer(SSLCert * cert, const uint16_t port, const uint8_t maxConnections, const in_addr_t bindAddress):
78
HTTPServer(port, maxConnections, bindAddress),
89
_cert(cert) {
910

1011
// Configure runtime data
11-
_sslctx = NULL;
12+
_cfg = new esp_tls_cfg_server();
13+
_cfg->alpn_protos = (const char **)alpn_protos;
14+
_cfg->cacert_buf = NULL;
15+
_cfg->cacert_bytes = 0;
16+
_cfg->servercert_buf =cert->getCertData();
17+
_cfg->servercert_bytes = cert->getCertLength();
18+
_cfg->serverkey_buf= cert->getPKData();
19+
_cfg->serverkey_bytes= cert->getPKLength();
1220
}
1321

1422
HTTPSServer::~HTTPSServer() {
15-
23+
free(_cfg);
1624
}
1725

1826
/**
1927
* This method starts the server and begins to listen on the port
2028
*/
2129
uint8_t HTTPSServer::setupSocket() {
2230
if (!isRunning()) {
23-
if (!setupSSLCTX()) {
24-
Serial.println("setupSSLCTX failed");
25-
return 0;
26-
}
27-
28-
if (!setupCert()) {
29-
Serial.println("setupCert failed");
30-
SSL_CTX_free(_sslctx);
31-
_sslctx = NULL;
32-
return 0;
33-
}
31+
_cfg->servercert_buf= _cert->getCertData();
32+
_cfg->servercert_bytes = _cert->getCertLength();
33+
_cfg->serverkey_buf= _cert->getPKData();
34+
_cfg->serverkey_bytes= _cert->getPKLength();
3435

3536
if (HTTPServer::setupSocket()) {
3637
return 1;
3738
} else {
3839
Serial.println("setupSockets failed");
39-
SSL_CTX_free(_sslctx);
40-
_sslctx = NULL;
4140
return 0;
4241
}
4342
} else {
@@ -48,31 +47,13 @@ uint8_t HTTPSServer::setupSocket() {
4847
void HTTPSServer::teardownSocket() {
4948

5049
HTTPServer::teardownSocket();
51-
52-
// Tear down the SSL context
53-
SSL_CTX_free(_sslctx);
54-
_sslctx = NULL;
5550
}
5651

5752
int HTTPSServer::createConnection(int idx) {
5853
HTTPSConnection * newConnection = new HTTPSConnection(this);
5954
_connections[idx] = newConnection;
60-
return newConnection->initialize(_socket, _sslctx, &_defaultHeaders);
61-
}
62-
63-
/**
64-
* This method configures the ssl context that is used for the server
65-
*/
66-
uint8_t HTTPSServer::setupSSLCTX() {
67-
_sslctx = SSL_CTX_new(TLSv1_2_server_method());
68-
if (_sslctx) {
69-
// Set SSL Timeout to 5 minutes
70-
SSL_CTX_set_timeout(_sslctx, 300);
71-
return 1;
72-
} else {
73-
_sslctx = NULL;
74-
return 0;
75-
}
55+
56+
return newConnection->initialize(_socket, _cfg, &_defaultHeaders);
7657
}
7758

7859
/**
@@ -81,22 +62,11 @@ uint8_t HTTPSServer::setupSSLCTX() {
8162
*/
8263
uint8_t HTTPSServer::setupCert() {
8364
// Configure the certificate first
84-
uint8_t ret = SSL_CTX_use_certificate_ASN1(
85-
_sslctx,
86-
_cert->getCertLength(),
87-
_cert->getCertData()
88-
);
89-
90-
// Then set the private key accordingly
91-
if (ret) {
92-
ret = SSL_CTX_use_RSAPrivateKey_ASN1(
93-
_sslctx,
94-
_cert->getPKData(),
95-
_cert->getPKLength()
96-
);
97-
}
98-
99-
return ret;
65+
_cfg->servercert_buf= _cert->getCertData();
66+
_cfg->servercert_bytes = _cert->getCertLength();
67+
_cfg->serverkey_buf= _cert->getPKData();
68+
_cfg->serverkey_bytes= _cert->getPKLength();
69+
return 1;
10070
}
10171

10272
} /* namespace httpsserver */

Diff for: src/HTTPSServer.hpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
#include <Arduino.h>
99

1010
// Required for SSL
11-
#include "openssl/ssl.h"
12-
#undef read
11+
#include <esp_tls.h>
1312

1413
// Internal includes
1514
#include "HTTPServer.hpp"
@@ -32,19 +31,21 @@ class HTTPSServer : public HTTPServer {
3231
HTTPSServer(SSLCert * cert, const uint16_t portHTTPS = 443, const uint8_t maxConnections = 4, const in_addr_t bindAddress = 0);
3332
virtual ~HTTPSServer();
3433

34+
virtual esp_tls_cfg_server_t *getConfig() { return _cfg;}
35+
3536
private:
3637
// Static configuration. Port, keys, etc. ====================
3738
// Certificate that should be used (includes private key)
3839
SSLCert * _cert;
3940

4041
//// Runtime data ============================================
41-
SSL_CTX * _sslctx;
42+
esp_tls_cfg_server_t * _cfg;
43+
4244
// Status of the server: Are we running, or not?
4345

4446
// Setup functions
4547
virtual uint8_t setupSocket();
4648
virtual void teardownSocket();
47-
uint8_t setupSSLCTX();
4849
uint8_t setupCert();
4950

5051
// Helper functions

Diff for: src/WebsocketHandler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static void dumpFrame(WebsocketFrame frame) {
1717
case WebsocketHandler::OPCODE_TEXT: opcode = std::string("TEXT"); break;
1818
}
1919
ESP_LOGI(
20-
TAG,
20+
"",
2121
"Fin: %d, OpCode: %d (%s), Mask: %d, Len: %d",
2222
(int)frame.fin,
2323
(int)frame.opCode,

0 commit comments

Comments
 (0)