Skip to content

Commit 68ebe0d

Browse files
[wifi ssl client] added the possibility of setting custom tls cert and private key in wifi clients
1 parent 34f296c commit 68ebe0d

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

libraries/WiFiS3/src/WiFiSSLClient.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ int WiFiSSLClient::connect(const char* host, uint16_t port) {
6262
if(!modem.passthrough((uint8_t *)_ecc_cert, _ecc_cert_len)) {
6363
return 0;
6464
}
65+
} else if(_client_cert != nullptr && _private_key != nullptr) { // TODO make sure if set certificate is called to not use the above code
66+
size_t size = strlen(_client_cert);
67+
modem.write_nowait(string(PROMPT(_SSLCLIENTSETCERT)),res, "%s%d,%d\r\n" , CMD_WRITE(_SSLCLIENTSETCERT), _sock, size);
68+
if(!modem.passthrough((uint8_t *)_client_cert, size)) {
69+
return 0;
70+
}
71+
72+
size = strlen(_private_key);
73+
modem.write_nowait(string(PROMPT(_SSLCLIENTSETPKEY)),res, "%s%d,%d\r\n" , CMD_WRITE(_SSLCLIENTSETPKEY), _sock, size);
74+
if(!modem.passthrough((uint8_t *)_private_key, size)) {
75+
return 0;
76+
}
6577
}
6678

6779
if (_connectionTimeout) {
@@ -88,6 +100,9 @@ void WiFiSSLClient::setEccSlot(int ecc508KeySlot, const byte cert[], int certLen
88100
_ecc_slot = ecc508KeySlot;
89101
_ecc_cert = cert;
90102
_ecc_cert_len = certLength;
103+
104+
_client_cert = nullptr;
105+
_private_key = nullptr;
91106
}
92107

93108
/* -------------------------------------------------------------------------- */
@@ -283,3 +298,21 @@ uint16_t WiFiSSLClient::remotePort(){
283298
}
284299
return rv;
285300
}
301+
302+
/* -------------------------------------------------------------------------- */
303+
void WiFiSSLClient::setCertificate(const char* clientCert){
304+
/* -------------------------------------------------------------------------- */
305+
_client_cert = clientCert;
306+
_ecc_slot = -1;
307+
_ecc_cert = nullptr;
308+
_ecc_cert_len = 0;
309+
}
310+
311+
/* -------------------------------------------------------------------------- */
312+
void WiFiSSLClient::setPrivateKey(const char* privateKey){
313+
/* -------------------------------------------------------------------------- */
314+
_private_key = privateKey;
315+
_ecc_slot = -1;
316+
_ecc_cert = nullptr;
317+
_ecc_cert_len = 0;
318+
}

libraries/WiFiS3/src/WiFiSSLClient.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ class WiFiSSLClient : public WiFiClient {
7575
* @brief Sets the ECC (Elliptic Curve Cryptography) key slot and
7676
* certificate for establishing secure SSL connections.
7777
*
78+
* Note that this function will disable custom certificates and private keys set with
79+
* setCertificate() and setPrivateKey()
80+
*
7881
* @param `int ecc508KeySlot` specifies the ECC key slot to be used for the SSL connection.
7982
* @param `const byte cert[]` is a pointer to the certificate data in the form of an array of bytes.
8083
* @param `int certLength` specifies the length of the certificate data array.
@@ -219,6 +222,30 @@ class WiFiSSLClient : public WiFiClient {
219222
*/
220223
virtual uint16_t remotePort();
221224

225+
/**
226+
* @brief Set the public certificate for this ssl client communication
227+
*
228+
* This function explicitly sets the certificate to use for this client in tls
229+
* communication. Note that if setEccSlot was used it will be disabled for this client.
230+
* This function should be called in conjunction with setPrivateKey()
231+
*
232+
* @param `clientCert` client certificate in PEM format
233+
*
234+
*/
235+
void setCertificate(const char* clientCert);
236+
237+
/**
238+
* @brief Set the private key for this ssl client communication
239+
*
240+
* This function explicitly sets the private key to use for this client in tls
241+
* communication. Note that if setEccSlot was used it will be disabled for this client.
242+
* This function should be called in conjunction with setCertificate()
243+
*
244+
* @param `privateKey` client private key in PEM format
245+
*
246+
*/
247+
void setPrivateKey(const char* privateKey);
248+
222249
/**
223250
* @brief Declares WiFiServer as a friend class.
224251
*
@@ -240,6 +267,8 @@ class WiFiSSLClient : public WiFiClient {
240267
int _read();
241268
void read_if_needed(size_t s);
242269
const char* _root_ca = nullptr;
270+
const char* _client_cert = nullptr;
271+
const char* _private_key = nullptr;
243272
int _ecc_slot = -1;
244273
const byte* _ecc_cert = nullptr;
245274
int _ecc_cert_len = 0;

0 commit comments

Comments
 (0)