Skip to content

Commit 233d3e3

Browse files
Move BearSSLHelpers into BearSSL namespace (#5315)
BearSSLX509List, BearSSLSession, BearSSLPublicKey, and BearSSLPrivateKey were all in the global namespace and not in the BearSSL:: one, due to an oversight when they were originally created. Move them to the proper namespace with the following mapping: BearSSLX509List => BearSSL::X509List BearSSLSession => BearSSL::Session BearSSLPublicKey => BearSSL::PublicKey BearSSLPrivateKey => BearSSL::PrivateKey
1 parent a42c3c3 commit 233d3e3

19 files changed

+145
-134
lines changed

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

+9-11
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ There are many configuration options that require passing in a pointer to an obj
3434
BearSSL::WiFiClientSecure client;
3535
const char x509CA PROGMEM = ".......";
3636
void setup() {
37-
BearSSLX509List x509(x509CA);
37+
BearSSL::X509List x509(x509CA);
3838
client.setTrustAnchor(&x509);
3939
}
4040
void loop() {
@@ -73,7 +73,7 @@ TLS Sessions
7373

7474
TLS supports the notion of a session (completely independent and different from HTTP sessions) which allow clients to reconnect to a server without having to renegotiate encryption settings or validate X509 certificates. This can save significant time (3-4 seconds in the case of EC keys) and can help save power by allowing the ESP8266 to sleep for a long time, reconnect and transmit some samples using the SSL session, and then jump back to sleep quicker.
7575

76-
`BearSSLSession` is an opaque class. Use the `BearSSL::WiFiClientSecure.setSession(&BearSSLSession)` method to apply it before the first `BearSSL::WiFiClientSecure.connect()` and it will be updated with session parameters during the operation of the connection. After the connection has had `.close()` called on it, serialize the `BearSSLSession` object to stable storage (EEPROM, RTC RAM, etc.) and restore it before trying to reconnect. See the `BearSSL_Sessions` example for a detailed example.
76+
`BearSSL::Session` is an opaque class. Use the `BearSSL::WiFiClientSecure.setSession(&BearSSLSession)` method to apply it before the first `BearSSL::WiFiClientSecure.connect()` and it will be updated with session parameters during the operation of the connection. After the connection has had `.close()` called on it, serialize the `BearSSL::Session` object to stable storage (EEPROM, RTC RAM, etc.) and restore it before trying to reconnect. See the `BearSSL_Sessions` example for a detailed example.
7777

7878
`Sessions <#sessions-resuming-connections-fast>`__ contains additional information on the sessions API.
7979

@@ -82,15 +82,15 @@ X.509 Certificate(s)
8282

8383
X509 certificates are used to identify peers in TLS connections. Normally only the server identifies itself, but the client can also supply an X509 certificate if desired (this is often done in MQTT applications). The certificate contains many fields, but the most interesting in our applications are the name, the public key, and potentially a chain of signing that leads back to a trusted authority (like a global internet CA or a company-wide private certificate authority).
8484

85-
Any call that takes an X509 certificate can also take a list of X509 certificates, so there is no special `X509` class, simply `BearSSLX509List` (which may only contain a single certificate).
85+
Any call that takes an X509 certificate can also take a list of X509 certificates, so there is no special `X509` class, simply `BearSSL::X509List` (which may only contain a single certificate).
8686

8787
Generating a certificate to be used to validate using the constructor
8888

8989
.. code:: cpp
9090
91-
BearSSLX509List(const char *pemX509);
91+
BearSSL::X509List(const char *pemX509);
9292
...or...
93-
BearSSLX509List(const uint8_t *derCert, size_t derLen);
93+
BearSSL::X509List(const uint8_t *derCert, size_t derLen);
9494
9595
If you need to add additional certificates (unlikely in normal operation), the `::append()` operation can be used.
9696

@@ -100,7 +100,7 @@ Certificate Stores
100100

101101
The web browser you're using to read this document keeps a list of 100s of certification authorities (CAs) worldwide that it trusts to attest to the identity of websites.
102102

103-
In many cases your application will know the specific CA it needs to validate web or MQTT servers against (often just a single, self-signing CA private to your institution). Simply load your private CA in a `BearSSLX509List` and use that as your trust anchor.
103+
In many cases your application will know the specific CA it needs to validate web or MQTT servers against (often just a single, self-signing CA private to your institution). Simply load your private CA in a `BearSSL::X509List` and use that as your trust anchor.
104104

105105
However, there are cases where you will not know beforehand which CA you will need (i.e. a user enters a website through a keypad), and you need to keep the list of CAs just like your web browser. In those cases, you need to generate a certificate bundle on the PC while compiling your application, upload the `certs.ar` bundle to SPIFFS or SD when uploading your application binary, and pass it to a `BearSSL::CertStore()` in order to validate TLS peers.
106106

@@ -129,7 +129,7 @@ setInsecure()
129129

130130
Don't verify any X509 certificates. There is no guarantee that the server connected to is the one you think it is in this case, but this call will mimic the behavior of the deprecated axTLS code.
131131

132-
setKnownKey(const BearSSLPublicKey *pk)
132+
setKnownKey(const BearSSL::PublicKey *pk)
133133
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
134134
135135
Assume the server is using the specific public key. This does not verify the identity of the server or the X509 certificate it sends, it simply assumes that its public key is the one given. If the server updates its public key at a later point then connections will fail.
@@ -139,7 +139,7 @@ setFingerprint(const uint8_t fp[20]) / setFingerprint(const char *fpStr)
139139
140140
Verify the SHA1 fingerprint of the certificate returned matches this one. If the server certificate changes, it will fail. If an array of 20 bytes are sent in, it is assumed they are the binary SHA1 values. If a `char*` string is passed in, it is parsed as a series of human-readable hex values separated by spaces or colons (e.g. `setFingerprint("00:01:02:03:...:1f");`)
141141

142-
setTrustAnchors(BearSSLX509List *ta)
142+
setTrustAnchors(BearSSL::X509List *ta)
143143
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
144144
145145
Use the passed-in certificate(s) as a trust anchor, accepting remote certificates signed by any of these. If you have many trust anchors it may make sense to use a `BearSSL::CertStore` because it will only require RAM for a single trust anchor (while the `setTrustAnchors` call requires memory for all certificates in the list).
@@ -183,7 +183,7 @@ In certain applications where the TLS server does not support MFLN (not many do
183183
Sessions (Resuming connections fast)
184184
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
185185

186-
setSession(BearSSLSession &sess)
186+
setSession(BearSSL::Session &sess)
187187
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
188188

189189
If you are connecting to a server repeatedly in a fixed time period (usually 30 or 60 minutes, but normally configurable at the server), a TLS session can be used to cache crypto settings and speed up connections significantly.
@@ -212,5 +212,3 @@ setCiphersLessSecure()
212212
^^^^^^^^^^^^^^^^^^^^^^
213213

214214
Helper function which essentially limits BearSSL to ciphers that were supported by the deprecated axTLS. These may be less secure than the ones BearSSL would natively choose, but they may be helpful and faster if your server depended on specific axTLS crypto options.
215-
216-

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ This example command will generate a RSA 2048-bit key and certificate:
2323
2424
Again, it is up to the application author to generate this certificate and key and keep the private key safe and **private.**
2525

26-
setRSACert(const BearSSLX509List *chain, const BearSSLPrivateKey *sk)
26+
setRSACert(const BearSSL::X509List *chain, const BearSSL::PrivateKey *sk)
2727
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2828
2929
Sets a RSA certificate and key to be used by the server when connections are received. Needs to be called before `begin()`
3030

31-
setECCert(const BearSSLX509List *chain, unsigned cert_issuer_key_type, const BearSSLPrivateKey *sk)
31+
setECCert(const BearSSL::X509List *chain, unsigned cert_issuer_key_type, const BearSSL::PrivateKey *sk)
3232
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3333
3434
Sets an elliptic curve certificate and key for the server. Needs to be called before `begin()`.
@@ -38,7 +38,7 @@ Requiring Client Certificates
3838

3939
TLS servers can request the client to identify itself by transmitting a certificate during handshake. If the client cannot transmit the certificate, the connection will be dropped by the server.
4040

41-
setClientTrustAnchor(const BearSSLX509List *client_CA_ta)
41+
setClientTrustAnchor(const BearSSL::X509List *client_CA_ta)
4242
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4343
4444
Sets the trust anchor (normally a self-signing CA) that all received certificates will be verified against. Needs to be called before `begin()`.

Diff for: libraries/ESP8266HTTPUpdateServer/examples/SecureBearSSLUpdater/SecureBearSSLUpdater.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void setup()
101101

102102
MDNS.begin(host);
103103

104-
httpServer.setRSACert(new BearSSLX509List(serverCert), new BearSSLPrivateKey(serverKey));
104+
httpServer.setRSACert(new BearSSL::X509List(serverCert), new BearSSL::PrivateKey(serverKey));
105105
httpUpdater.setup(&httpServer, update_path, update_username, update_password);
106106
httpServer.begin();
107107

Diff for: libraries/ESP8266WebServer/examples/HelloServerBearSSL/HelloServerBearSSL.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ void setup(void){
123123
Serial.println("MDNS responder started");
124124
}
125125

126-
server.setRSACert(new BearSSLX509List(serverCert), new BearSSLPrivateKey(serverKey));
126+
server.setRSACert(new BearSSL::X509List(serverCert), new BearSSL::PrivateKey(serverKey));
127127

128128
server.on("/", handleRoot);
129129

Diff for: libraries/ESP8266WebServer/src/ESP8266WebServerSecureBearSSL.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ ESP8266WebServerSecure::ESP8266WebServerSecure(int port)
4646
{
4747
}
4848

49-
void ESP8266WebServerSecure::setRSACert(const BearSSLX509List *chain, const BearSSLPrivateKey *sk)
49+
void ESP8266WebServerSecure::setRSACert(const X509List *chain, const PrivateKey *sk)
5050
{
5151
_serverSecure.setRSACert(chain, sk);
5252
}
5353

54-
void ESP8266WebServerSecure::setECCert(const BearSSLX509List *chain, unsigned cert_issuer_key_type, const BearSSLPrivateKey *sk)
54+
void ESP8266WebServerSecure::setECCert(const X509List *chain, unsigned cert_issuer_key_type, const PrivateKey *sk)
5555
{
5656
_serverSecure.setECCert(chain, cert_issuer_key_type, sk);
5757
}
@@ -83,7 +83,7 @@ void ESP8266WebServerSecure::begin() {
8383

8484
void ESP8266WebServerSecure::handleClient() {
8585
if (_currentStatus == HC_NONE) {
86-
BearSSL::WiFiClientSecure client = _serverSecure.available();
86+
WiFiClientSecure client = _serverSecure.available();
8787
if (!client) {
8888
return;
8989
}
@@ -136,7 +136,7 @@ void ESP8266WebServerSecure::handleClient() {
136136
}
137137

138138
if (!keepCurrentClient) {
139-
_currentClientSecure = BearSSL::WiFiClientSecure();
139+
_currentClientSecure = WiFiClientSecure();
140140
_currentStatus = HC_NONE;
141141
_currentUpload.reset();
142142
}

Diff for: libraries/ESP8266WebServer/src/ESP8266WebServerSecureBearSSL.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class ESP8266WebServerSecure : public ESP8266WebServer
3737
virtual ~ESP8266WebServerSecure();
3838

3939
void setBufferSizes(int recv, int xmit);
40-
void setRSACert(const BearSSLX509List *chain, const BearSSLPrivateKey *sk);
41-
void setECCert(const BearSSLX509List *chain, unsigned cert_issuer_key_type, const BearSSLPrivateKey *sk);
40+
void setRSACert(const X509List *chain, const PrivateKey *sk);
41+
void setECCert(const X509List *chain, unsigned cert_issuer_key_type, const PrivateKey *sk);
4242

4343
WiFiClient client() override { return _currentClientSecure; }
4444

@@ -61,8 +61,8 @@ class ESP8266WebServerSecure : public ESP8266WebServer
6161
size_t _currentClientWrite_P (PGM_P bytes, size_t len) override { return _currentClientSecure.write_P(bytes, len); }
6262

6363
protected:
64-
BearSSL::WiFiServerSecure _serverSecure;
65-
BearSSL::WiFiClientSecure _currentClientSecure;
64+
WiFiServerSecure _serverSecure;
65+
WiFiClientSecure _currentClientSecure;
6666
};
6767

6868
};

Diff for: libraries/ESP8266WiFi/examples/BearSSL_Server/BearSSL_Server.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ void setup() {
121121
Serial.println(WiFi.localIP());
122122

123123
// Attach the server private cert/key combo
124-
BearSSLX509List *serverCertList = new BearSSLX509List(server_cert);
125-
BearSSLPrivateKey *serverPrivKey = new BearSSLPrivateKey(server_private_key);
124+
BearSSL::X509List *serverCertList = new BearSSL::X509List(server_cert);
125+
BearSSL::PrivateKey *serverPrivKey = new BearSSL::PrivateKey(server_private_key);
126126
server.setRSACert(serverCertList, serverPrivKey);
127127

128128
// Actually start accepting connections

Diff for: libraries/ESP8266WiFi/examples/BearSSL_ServerClientCert/BearSSL_ServerClientCert.ino

+3-3
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,12 @@ void setup() {
197197
setClock(); // Required for X.509 validation
198198

199199
// Attach the server private cert/key combo
200-
BearSSLX509List *serverCertList = new BearSSLX509List(server_cert);
201-
BearSSLPrivateKey *serverPrivKey = new BearSSLPrivateKey(server_private_key);
200+
BearSSL::X509List *serverCertList = new BearSSL::X509List(server_cert);
201+
BearSSL::PrivateKey *serverPrivKey = new BearSSL::PrivateKey(server_private_key);
202202
server.setRSACert(serverCertList, serverPrivKey);
203203

204204
// Require a certificate validated by the trusted CA
205-
BearSSLX509List *serverTrustedCA = new BearSSLX509List(ca_cert);
205+
BearSSL::X509List *serverTrustedCA = new BearSSL::X509List(ca_cert);
206206
server.setClientTrustAnchor(serverTrustedCA);
207207

208208
// Actually start accepting connections

Diff for: libraries/ESP8266WiFi/examples/BearSSL_Sessions/BearSSL_Sessions.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ vEsXCS+0yx5DaMkHJ8HSXPfqIbloEpw8nL+e/IBcm2PN7EeqJSdnoDfzAIJ9VNep
119119
)EOF";
120120
uint32_t start, finish;
121121
BearSSL::WiFiClientSecure client;
122-
BearSSLX509List cert(digicert);
122+
BearSSL::X509List cert(digicert);
123123

124124
Serial.printf("Connecting without sessions...");
125125
start = millis();
@@ -128,7 +128,7 @@ vEsXCS+0yx5DaMkHJ8HSXPfqIbloEpw8nL+e/IBcm2PN7EeqJSdnoDfzAIJ9VNep
128128
finish = millis();
129129
Serial.printf("Total time: %dms\n", finish - start);
130130

131-
BearSSLSession session;
131+
BearSSL::Session session;
132132
client.setSession(&session);
133133
Serial.printf("Connecting with an unitialized session...");
134134
start = millis();

Diff for: libraries/ESP8266WiFi/examples/BearSSL_Validation/BearSSL_Validation.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ wQIDAQAB
144144
-----END PUBLIC KEY-----
145145
)KEY";
146146
BearSSL::WiFiClientSecure client;
147-
BearSSLPublicKey key(pubkey);
147+
BearSSL::PublicKey key(pubkey);
148148
client.setKnownKey(&key);
149149
fetchURL(&client, host, port, path);
150150
}
@@ -186,7 +186,7 @@ BearSSL does verify the notValidBefore/After fields.
186186
)EOF");
187187

188188
BearSSL::WiFiClientSecure client;
189-
BearSSLX509List cert(digicert);
189+
BearSSL::X509List cert(digicert);
190190
client.setTrustAnchors(&cert);
191191
Serial.printf("Try validating without setting the time (should fail)\n");
192192
fetchURL(&client, host, port, path);

Diff for: libraries/ESP8266WiFi/keywords.txt

+6-3
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ WiFiServerSecure KEYWORD1
1919
WiFiUDP KEYWORD1
2020
WiFiClientSecure KEYWORD1
2121
ESP8266WiFiMulti KEYWORD1
22-
BearSSLX509List KEYWORD1
23-
BearSSLPrivateKey KEYWORD1
24-
BearSSLPublicKey KEYWORD1
22+
BearSSL KEYWORD1
23+
X509List KEYWORD1
24+
PrivateKey KEYWORD1
25+
PublicKey KEYWORD1
2526
CertStoreSPIFFSBearSSL KEYWORD1
2627
CertStoreSDBearSSL KEYWORD1
28+
Session KEYWORD1
29+
2730

2831
#######################################
2932
# Methods and Functions (KEYWORD2)

0 commit comments

Comments
 (0)