forked from meshtastic/esp32_https_server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPSServer.cpp
More file actions
94 lines (74 loc) · 2.39 KB
/
HTTPSServer.cpp
File metadata and controls
94 lines (74 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "HTTPSServer.hpp"
namespace httpsserver {
HTTPSServer::HTTPSServer(SSLCert * cert, const uint16_t port, const uint8_t maxConnections, const in_addr_t bindAddress):
HTTPServer(port, maxConnections, bindAddress),
_cert(cert) {
// Configure runtime data
mbedtls_ssl_config_init(&_ssl_config);
mbedtls_x509_crt_init(&_ssl_cert);
mbedtls_pk_init(&_ssl_pk);
mbedtls_entropy_init(&_ssl_entropy);
mbedtls_ctr_drbg_init(&_ssl_ctr_drbg);
mbedtls_ctr_drbg_seed(&_ssl_ctr_drbg, mbedtls_entropy_func, &_ssl_entropy, nullptr, 0);
mbedtls_ssl_conf_rng(&_ssl_config, mbedtls_ctr_drbg_random, &_ssl_ctr_drbg);
}
HTTPSServer::~HTTPSServer() {
mbedtls_ctr_drbg_init(&_ssl_ctr_drbg);
mbedtls_entropy_init(&_ssl_entropy);
mbedtls_pk_free(&_ssl_pk);
mbedtls_x509_crt_free(&_ssl_cert);
mbedtls_ssl_config_free(&_ssl_config);
}
/**
* This method starts the server and begins to listen on the port
*/
uint8_t HTTPSServer::setupSocket() {
if (isRunning()) {
return 1;
}
int res = mbedtls_ssl_config_defaults(&_ssl_config,
MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
if (res != 0) {
Serial.println("Setting SSL default config failed");
return 0;
}
if (setupCert() != 0) {
Serial.println("setupCert failed");
return 0;
}
if (!HTTPServer::setupSocket()) {
Serial.println("setupSockets failed");
return 0;
}
return 1;
}
void HTTPSServer::teardownSocket() {
HTTPServer::teardownSocket();
}
int HTTPSServer::createConnection(int idx) {
HTTPSConnection * newConnection = new HTTPSConnection(this);
_connections[idx] = newConnection;
return newConnection->initialize(_socket, &_ssl_config, &_defaultHeaders);
}
/**
* This method configures the certificate and private key for the given
* ssl context
*/
int HTTPSServer::setupCert() {
int res = 0;
res = mbedtls_x509_crt_parse(&_ssl_cert, _cert->getCertData(), _cert->getCertLength());
if (res != 0) {
return res;
}
#if ESP_IDF_VERSION_MAJOR > 4
res = mbedtls_pk_parse_key(&_ssl_pk, _cert->getPKData(), _cert->getPKLength(), nullptr, 0,
mbedtls_ctr_drbg_random, &_ssl_ctr_drbg);
#else
res = mbedtls_pk_parse_key(&_ssl_pk, _cert->getPKData(), _cert->getPKLength(), nullptr, 0);
#endif
if (res != 0) {
return res;
}
return mbedtls_ssl_conf_own_cert(&_ssl_config, &_ssl_cert, &_ssl_pk);
}
} /* namespace httpsserver */