-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ivan Kostoski
committed
Oct 10, 2019
1 parent
43b8c56
commit 7cb01b0
Showing
6 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#include "TLSTickets.hpp" | ||
#include "HTTPSServerConstants.hpp" | ||
|
||
#include "mbedtls/net_sockets.h" | ||
|
||
// Low level SSL implementation on ESP32 | ||
// Copied from esp-idf/components/openssl/platform/ssl_pm.c | ||
struct ssl_pm { | ||
mbedtls_net_context fd; | ||
mbedtls_net_context cl_fd; | ||
mbedtls_ssl_config conf; | ||
mbedtls_ctr_drbg_context ctr_drbg; | ||
mbedtls_ssl_context ssl; | ||
mbedtls_entropy_context entropy; | ||
}; | ||
|
||
namespace httpsserver { | ||
|
||
int TLSTickets::hardware_random(void * p_rng, unsigned char * output, size_t output_len) { | ||
esp_fill_random(output, output_len); | ||
return 0; | ||
} | ||
|
||
TLSTickets::TLSTickets(const char* tag, uint32_t lifetimeSeconds, bool useHWRNG) { | ||
_initOk = false; | ||
_useHWRNG = useHWRNG; | ||
|
||
// Setup TLS tickets context | ||
int ret = -1; | ||
if (_useHWRNG) { | ||
mbedtls_ssl_ticket_init(&_ticketCtx); | ||
ret = mbedtls_ssl_ticket_setup( | ||
&_ticketCtx, | ||
TLSTickets::hardware_random, | ||
NULL, | ||
MBEDTLS_CIPHER_AES_256_GCM, | ||
lifetimeSeconds | ||
); | ||
} else { | ||
mbedtls_entropy_init(&_entropy); | ||
mbedtls_ctr_drbg_init(&_ctr_drbg); | ||
mbedtls_ssl_ticket_init(&_ticketCtx); | ||
ret = mbedtls_ctr_drbg_seed( | ||
&_ctr_drbg, | ||
mbedtls_entropy_func, | ||
&_entropy, | ||
(unsigned char*)tag, | ||
strlen(tag) | ||
); | ||
if (ret == 0) { | ||
ret = mbedtls_ssl_ticket_setup( | ||
&_ticketCtx, | ||
mbedtls_ctr_drbg_random, | ||
&_ctr_drbg, | ||
MBEDTLS_CIPHER_AES_256_GCM, | ||
lifetimeSeconds | ||
); | ||
} | ||
} | ||
if (ret != 0) return; | ||
|
||
_initOk = true; | ||
HTTPS_LOGI("Using TLS session tickets"); | ||
} | ||
|
||
TLSTickets::~TLSTickets() { | ||
if (!_useHWRNG) { | ||
mbedtls_ctr_drbg_free(&_ctr_drbg); | ||
mbedtls_entropy_free(&_entropy); | ||
} | ||
mbedtls_ssl_ticket_free(&_ticketCtx); | ||
} | ||
|
||
bool TLSTickets::enable(SSL * ssl) { | ||
bool res = false; | ||
if (_initOk && ssl && ssl->ssl_pm) { | ||
// Get handle of low-level mbedtls structures for the session | ||
struct ssl_pm * ssl_pm = (struct ssl_pm *) ssl->ssl_pm; | ||
// Configure TLS ticket callbacks using default MbedTLS implementation | ||
mbedtls_ssl_conf_session_tickets_cb( | ||
&ssl_pm->conf, | ||
mbedtls_ssl_ticket_write, | ||
mbedtls_ssl_ticket_parse, | ||
&_ticketCtx | ||
); | ||
res = true; | ||
} | ||
return res; | ||
} | ||
|
||
} /* namespace httpsserver */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#ifndef SRC_TLSTICKETS_HPP_ | ||
#define SRC_TLSTICKETS_HPP_ | ||
|
||
#include <cstdint> | ||
#include "mbedtls/entropy.h" | ||
#include "mbedtls/ctr_drbg.h" | ||
#include "mbedtls/ssl_ticket.h" | ||
#include "openssl/ssl.h" | ||
|
||
namespace httpsserver { | ||
|
||
/** | ||
* Enables handling of RFC 5077 TLS session tickets | ||
*/ | ||
class TLSTickets { | ||
|
||
public: | ||
TLSTickets(const char* tag, uint32_t liftimeSecs, bool useHWRNG); | ||
~TLSTickets(); | ||
|
||
/** | ||
* Enables TLS ticket processing for SSL session | ||
*/ | ||
bool enable(SSL * ssl); | ||
|
||
protected: | ||
bool _initOk; | ||
bool _useHWRNG; | ||
|
||
/** | ||
* Holds TLS ticket keys | ||
*/ | ||
mbedtls_ssl_ticket_context _ticketCtx; | ||
|
||
/** | ||
* mbedTLS random number generator state | ||
*/ | ||
mbedtls_entropy_context _entropy; | ||
mbedtls_ctr_drbg_context _ctr_drbg; | ||
|
||
/** | ||
* MbedTLS Random Number Generator using ESP32's hardware RNG | ||
* | ||
* NOTE: Radio (WiFi/Bluetooth) MUST be running for hardware | ||
* entropy to be gathered. Otherwise this function is PRNG! | ||
* | ||
* See more details about esp_random(), here: | ||
* https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/system.html | ||
* | ||
*/ | ||
static int hardware_random(void * p_rng, unsigned char * output, size_t output_len); | ||
|
||
}; | ||
|
||
} /* namespace httpsserver */ | ||
|
||
#endif // SRC_TLSTICKETS_HPP_ |