Skip to content

Commit 70c8172

Browse files
Add setSSLVersion call to SSL object
Allow users to only allow specific TLS versions for connections with an additional call in their app, similar to the setCiphers call. Fixes #7918
1 parent e07542d commit 70c8172

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

Diff for: doc/esp8266wifi/bearssl-client-secure-class.rst

+11-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ BearSSL doesn't perform memory allocations at runtime, but it does require alloc
2020
. A per-application secondary stack
2121
. A per-connection TLS receive/transmit buffer plus overhead
2222

23-
The per-application secondary stack is approximately 5.6KB in size and is used for temporary variables during BearSSL processing. Only one stack is required, and it will be allocated whenever any `BearSSL::WiFiClientSecure` or `BearSSL::WiFiServerSecure` are instantiated. So, in the case of a global client or server, the memory will be allocated before `setup()` is called.
23+
The per-application secondary stack is approximately 6KB in size and is used for temporary variables during BearSSL processing. Only one stack is required, and it will be allocated whenever any `BearSSL::WiFiClientSecure` or `BearSSL::WiFiServerSecure` are instantiated. So, in the case of a global client or server, the memory will be allocated before `setup()` is called.
2424

2525
The per-connection buffers are approximately 22KB in size, but in certain circumstances it can be reduced dramatically by using MFLN or limiting message sizes. See the `MLFN section <#mfln-or-maximum-fragment-length-negotiation-saving-ram>`__ below for more information.
2626

@@ -219,3 +219,13 @@ setCiphersLessSecure()
219219
^^^^^^^^^^^^^^^^^^^^^^
220220

221221
Helper function which essentially limits BearSSL to less secure ciphers than it would natively choose, but they may be helpful and faster if your server depended on specific crypto options.
222+
223+
Limiting TLS(SSL) Versions
224+
~~~~~~~~~~~~~~~~~~~~~~~~~~
225+
226+
By default, BearSSL will connect with TLS 1.0, TLS 1.1, or TLS 1.2 protocols (depending on the request of the remote side). If you want to limit to a subset, use the following call:
227+
228+
setSSLVersion(uint32_t min, uint32_t max)
229+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
230+
231+
Valid values for min and max are `BR_TLS10`, `BR_TLS11`, `BR_TLS12`. Min and max may be set to the same value if only a single TLS version is desired.

Diff for: libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ void WiFiClientSecureCtx::_clear() {
9393
_session = nullptr;
9494
_cipher_list = nullptr;
9595
_cipher_cnt = 0;
96+
_tls_min = BR_TLS10;
97+
_tls_max = BR_TLS12;
9698
}
9799

98100
void WiFiClientSecureCtx::_clearAuthenticationSettings() {
@@ -989,6 +991,17 @@ bool WiFiClientSecureCtx::setCiphers(const std::vector<uint16_t>& list) {
989991
return setCiphers(&list[0], list.size());
990992
}
991993

994+
bool WiFiClientSecureCtx::setSSLVersion(uint32_t min, uint32_t max) {
995+
if ( ((min != BR_TLS10) && (min != BR_TLS11) && (min != BR_TLS12)) ||
996+
((max != BR_TLS10) && (max != BR_TLS11) && (max != BR_TLS12)) ||
997+
(max < min) ) {
998+
return false; // Invalid options
999+
}
1000+
_tls_min = min;
1001+
_tls_max = max;
1002+
return true;
1003+
}
1004+
9921005
// Installs the appropriate X509 cert validation method for a client connection
9931006
bool WiFiClientSecureCtx::_installClientX509Validator() {
9941007
if (_use_insecure || _use_fingerprint || _use_self_signed) {
@@ -1094,6 +1107,7 @@ bool WiFiClientSecureCtx::_connectSSL(const char* hostName) {
10941107
return false;
10951108
}
10961109
br_ssl_engine_set_buffers_bidi(_eng, _iobuf_in.get(), _iobuf_in_size, _iobuf_out.get(), _iobuf_out_size);
1110+
br_ssl_engine_set_versions(_eng, _tls_min, _tls_max);
10971111

10981112
// Apply any client certificates, if supplied.
10991113
if (_sk && _sk->isRSA()) {
@@ -1208,6 +1222,7 @@ bool WiFiClientSecureCtx::_connectSSLServerRSA(const X509List *chain,
12081222
sk ? sk->getRSA() : nullptr, BR_KEYTYPE_KEYX | BR_KEYTYPE_SIGN,
12091223
br_rsa_private_get_default(), br_rsa_pkcs1_sign_get_default());
12101224
br_ssl_engine_set_buffers_bidi(_eng, _iobuf_in.get(), _iobuf_in_size, _iobuf_out.get(), _iobuf_out_size);
1225+
br_ssl_engine_set_versions(_eng, _tls_min, _tls_max);
12111226
if (cache != nullptr)
12121227
br_ssl_server_set_cache(_sc_svr.get(), cache->getCache());
12131228
if (client_CA_ta && !_installServerX509Validator(client_CA_ta)) {
@@ -1254,6 +1269,7 @@ bool WiFiClientSecureCtx::_connectSSLServerEC(const X509List *chain,
12541269
sk ? sk->getEC() : nullptr, BR_KEYTYPE_KEYX | BR_KEYTYPE_SIGN,
12551270
cert_issuer_key_type, br_ssl_engine_get_ec(_eng), br_ecdsa_i15_sign_asn1);
12561271
br_ssl_engine_set_buffers_bidi(_eng, _iobuf_in.get(), _iobuf_in_size, _iobuf_out.get(), _iobuf_out_size);
1272+
br_ssl_engine_set_versions(_eng, _tls_min, _tls_max);
12571273
if (cache != nullptr)
12581274
br_ssl_server_set_cache(_sc_svr.get(), cache->getCache());
12591275
if (client_CA_ta && !_installServerX509Validator(client_CA_ta)) {

Diff for: libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.h

+8
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ class WiFiClientSecureCtx : public WiFiClient {
120120
bool setCiphers(const std::vector<uint16_t>& list);
121121
bool setCiphersLessSecure(); // Only use the limited set of RSA ciphers without EC
122122

123+
// Limit the TLS versions BearSSL will connect with. Default is
124+
// BR_TLS10...BR_TLS12
125+
bool setSSLVersion(uint32_t min = BR_TLS10, uint32_t max = BR_TLS12);
126+
123127
protected:
124128
bool _connectSSL(const char *hostName); // Do initial SSL handshake
125129

@@ -161,6 +165,10 @@ class WiFiClientSecureCtx : public WiFiClient {
161165
std::shared_ptr<uint16_t> _cipher_list;
162166
uint8_t _cipher_cnt;
163167

168+
// TLS ciphers allowed
169+
uint32_t _tls_min;
170+
uint32_t _tls_max;
171+
164172
unsigned char *_recvapp_buf;
165173
size_t _recvapp_len;
166174

0 commit comments

Comments
 (0)