Skip to content

Commit 14b70e9

Browse files
committed
Allow setting client side TLS key and certificate
1 parent 6554377 commit 14b70e9

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

libraries/ESP8266WiFi/examples/HTTPSRequest/HTTPSRequest.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This example demonstrates how to use
55
* WiFiClientSecure class to access HTTPS API.
66
* We fetch and display the status of
7-
* esp8266/Arduino project continous integration
7+
* esp8266/Arduino project continuous integration
88
* build.
99
*
1010
* Created by Ivan Grokhotkov, 2015.
@@ -54,7 +54,7 @@ void setup() {
5454
Serial.println("certificate doesn't match");
5555
}
5656

57-
String url = "/repos/esp8266/Arduino/commits/esp8266/status";
57+
String url = "/repos/esp8266/Arduino/commits/master/status";
5858
Serial.print("requesting URL: ");
5959
Serial.println(url);
6060

libraries/ESP8266WiFi/src/WiFiClientSecure.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ extern "C"
5050
#define SSL_DEBUG_OPTS 0
5151
#endif
5252

53+
uint8_t* default_private_key = 0;
54+
uint32_t default_private_key_len = 0;
55+
static bool default_private_key_dynamic = false;
56+
//
57+
uint8_t* default_certificate = 0;
58+
uint32_t default_certificate_len = 0;
59+
static bool default_certificate_dynamic = false;
60+
61+
static void clear_private_key();
62+
static void clear_certificate();
63+
5364

5465
class SSLContext {
5566
public:
@@ -70,6 +81,9 @@ class SSLContext {
7081
if (_ssl_ctx_refcnt == 0) {
7182
ssl_ctx_free(_ssl_ctx);
7283
}
84+
85+
clear_private_key();
86+
clear_certificate();
7387
}
7488

7589
void ref() {
@@ -337,6 +351,66 @@ bool WiFiClientSecure::verify(const char* fp, const char* url) {
337351
return true;
338352
}
339353

354+
void WiFiClientSecure::setCertificate(const uint8_t* cert_data, size_t size) {
355+
clear_certificate();
356+
default_certificate = (uint8_t*) cert_data;
357+
default_certificate_len = size;
358+
}
359+
360+
void WiFiClientSecure::setPrivateKey(const uint8_t* pk, size_t size) {
361+
clear_private_key();
362+
default_private_key = (uint8_t*) pk;
363+
default_private_key_len = size;
364+
}
365+
366+
bool WiFiClientSecure::loadCertificate(Stream& stream, size_t size) {
367+
clear_certificate();
368+
default_certificate = new uint8_t[size];
369+
if (!default_certificate) {
370+
return false;
371+
}
372+
if (stream.readBytes(default_certificate, size) != size) {
373+
delete[] default_certificate;
374+
return false;
375+
}
376+
default_certificate_dynamic = true;
377+
default_certificate_len = size;
378+
return true;
379+
}
380+
381+
bool WiFiClientSecure::loadPrivateKey(Stream& stream, size_t size) {
382+
clear_private_key();
383+
default_private_key = new uint8_t[size];
384+
if (!default_private_key) {
385+
return false;
386+
}
387+
if (stream.readBytes(default_private_key, size) != size) {
388+
delete[] default_private_key;
389+
return false;
390+
}
391+
default_private_key_dynamic = true;
392+
default_private_key_len = size;
393+
return true;
394+
}
395+
396+
static void clear_private_key() {
397+
if (default_private_key && default_private_key_dynamic) {
398+
delete[] default_private_key;
399+
default_private_key_dynamic = false;
400+
}
401+
default_private_key = 0;
402+
default_private_key_len = 0;
403+
}
404+
405+
static void clear_certificate() {
406+
if (default_certificate && default_certificate_dynamic) {
407+
delete[] default_certificate;
408+
default_certificate_dynamic = false;
409+
}
410+
default_certificate = 0;
411+
default_certificate_len = 0;
412+
}
413+
340414
extern "C" int ax_port_read(int fd, uint8_t* buffer, size_t count) {
341415
ClientContext* _client = reinterpret_cast<ClientContext*>(fd);
342416
if (_client->state() != ESTABLISHED && !_client->getSize()) {

libraries/ESP8266WiFi/src/WiFiClientSecure.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,22 @@ class WiFiClientSecure : public WiFiClient {
4848
int peek() override;
4949
void stop() override;
5050

51+
void setCertificate(const uint8_t* cert_data, size_t size);
52+
void setPrivateKey(const uint8_t* pk, size_t size);
53+
54+
bool loadCertificate(Stream& stream, size_t size);
55+
bool loadPrivateKey(Stream& stream, size_t size);
56+
57+
template<typename TFile>
58+
bool loadCertificate(TFile& file) {
59+
return loadCertificate(file, file.size());
60+
}
61+
62+
template<typename TFile>
63+
bool loadPrivateKey(TFile& file) {
64+
return loadPrivateKey(file, file.size());
65+
}
66+
5167
protected:
5268
int _connectSSL();
5369

tools/sdk/lib/libaxtls.a

-1.61 KB
Binary file not shown.

0 commit comments

Comments
 (0)