Skip to content

Commit 53bbb4b

Browse files
authored
Merge pull request #992 from pennam/client-cert-key
Add support for client certificate and private key
2 parents c285265 + 8edb115 commit 53bbb4b

File tree

6 files changed

+85
-38
lines changed

6 files changed

+85
-38
lines changed

libraries/SE05X/src/WiFiSSLSE050Client.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ arduino::MbedSSLSE050Client::MbedSSLSE050Client() {
2626
void arduino::MbedSSLSE050Client::setEccSlot(int KeySlot, const byte cert[], int certLen) {
2727

2828
_keySlot = KeySlot;
29-
_client_cert_len = certLen;
30-
_client_cert = cert;
29+
_certLen = certLen;
30+
_cert = cert;
3131
}
3232

3333
void WiFiSSLSE050Client::setEccSlot(int KeySlot, const byte cert[], int certLen) {

libraries/SE05X/src/WiFiSSLSE050Client.h

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,37 +37,28 @@ class MbedSSLSE050Client : public arduino::MbedSSLClient {
3737
void setEccSlot(int KeySlot, const byte cert[], int certLen);
3838

3939
private:
40-
const byte* _client_cert;
41-
const char* _ca_cert;
42-
int _client_cert_len;
40+
const byte* _cert;
41+
int _certLen;
4342
int _keySlot;
4443
sss_object_t _keyObject;
4544

4645
int setRootCAClientCertKey() {
47-
if( NSAPI_ERROR_OK != ((TLSSocket*)sock)->set_root_ca_cert_path("/wlan/")) {
48-
return 0;
46+
int err = setRootCA();
47+
if (err != NSAPI_ERROR_OK) {
48+
return err;
4949
}
5050

51-
if(_hostname && !_disableSNI) {
52-
((TLSSocket*)sock)->set_hostname(_hostname);
51+
if(SE05X.getObjectHandle(_keySlot, &_keyObject) != NSAPI_ERROR_OK) {
52+
return NSAPI_ERROR_DEVICE_ERROR;
5353
}
5454

55-
if( NSAPI_ERROR_OK != ((TLSSocket*)sock)->append_root_ca_cert(_ca_cert_custom)) {
56-
return 0;
55+
if(((TLSSocket*)sock)->set_client_cert_key((void*)_cert,
56+
(size_t)_certLen,
57+
&_keyObject,
58+
SE05X.getDeviceCtx()) != NSAPI_ERROR_OK) {
59+
return NSAPI_ERROR_DEVICE_ERROR;
5760
}
58-
59-
if(!SE05X.getObjectHandle(_keySlot, &_keyObject)) {
60-
return 0;
61-
}
62-
63-
if( NSAPI_ERROR_OK != ((TLSSocket*)sock)->set_client_cert_key((void*)_client_cert,
64-
(size_t)_client_cert_len,
65-
&_keyObject,
66-
SE05X.getDeviceCtx())) {
67-
return 0;
68-
}
69-
70-
return 1;
61+
return NSAPI_ERROR_OK;
7162
}
7263
};
7364

libraries/SocketWrapper/src/AClient.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,24 @@ void arduino::ASslClient::appendCustomCACert(const char* ca_cert) {
143143
}
144144
static_cast<MbedSSLClient*>(client.get())->appendCustomCACert(ca_cert);
145145
}
146+
147+
void arduino::ASslClient::setCACert(const char* rootCA) {
148+
if (!client) {
149+
newMbedClient();
150+
}
151+
static_cast<MbedSSLClient*>(client.get())->setCACert(rootCA);
152+
}
153+
154+
void arduino::ASslClient::setCertificate(const char* clientCert) {
155+
if (!client) {
156+
newMbedClient();
157+
}
158+
static_cast<MbedSSLClient*>(client.get())->setCertificate(clientCert);
159+
}
160+
161+
void arduino::ASslClient::setPrivateKey(const char* privateKey) {
162+
if (!client) {
163+
newMbedClient();
164+
}
165+
static_cast<MbedSSLClient*>(client.get())->setPrivateKey(privateKey);
166+
}

libraries/SocketWrapper/src/AClient.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,16 @@ class ASslClient : public AClient {
7171

7272
ASslClient() {}
7373

74+
/* The disableSNI function needs to be called prior to connect */
7475
void disableSNI(bool statusSNI);
75-
76+
/* The appendCustomCACert function needs to be called prior to connect */
7677
void appendCustomCACert(const char* ca_cert);
78+
/* The setCACert function needs to be called prior to connect */
79+
void setCACert(const char* rootCA);
80+
/* The setCertificate function needs to be called prior to connect */
81+
void setCertificate(const char* clientCert);
82+
/* The setPrivateKey function needs to be called prior to connect */
83+
void setPrivateKey(const char* privateKey);
7784

7885
protected:
7986
virtual void newMbedClient();
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#include "MbedSSLClient.h"
22

33
arduino::MbedSSLClient::MbedSSLClient()
4-
: _ca_cert_custom(nullptr),
4+
: _rootCA(nullptr),
55
_hostname(nullptr),
6-
_disableSNI(false) {
6+
_clientCert(nullptr),
7+
_privateKey(nullptr),
8+
_disableSNI(false),
9+
_appendCA(true) {
710

811
onBeforeConnect(mbed::callback(this, &MbedSSLClient::setRootCA));
912
};

libraries/SocketWrapper/src/MbedSSLClient.h

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,48 @@ class MbedSSLClient : public arduino::MbedClient {
4848
_disableSNI = statusSNI;
4949
}
5050

51-
void appendCustomCACert(const char* ca_cert) {
52-
_ca_cert_custom = ca_cert;
51+
void appendCustomCACert(const char* rootCA) {
52+
_rootCA = rootCA;
53+
_appendCA = true;
54+
}
55+
void setCACert(const char* rootCA) {
56+
_rootCA = rootCA;
57+
_appendCA = false;
58+
}
59+
void setCertificate(const char* clientCert) {
60+
_clientCert = clientCert;
61+
}
62+
void setPrivateKey(const char* privateKey) {
63+
_privateKey = privateKey;
5364
}
5465

55-
protected:
56-
const char* _ca_cert_custom;
66+
private:
67+
const char* _rootCA;
5768
const char* _hostname;
69+
const char* _clientCert;
70+
const char* _privateKey;
5871
bool _disableSNI;
72+
bool _appendCA;
5973

60-
private:
74+
protected:
6175
int setRootCA() {
6276
int err = 0;
6377

78+
if(_hostname && !_disableSNI) {
79+
((TLSSocket*)sock)->set_hostname(_hostname);
80+
}
81+
82+
if(_clientCert && _privateKey) {
83+
err = ((TLSSocket*)sock)->set_client_cert_key(_clientCert, _privateKey);
84+
if( err != NSAPI_ERROR_OK) {
85+
return err;
86+
}
87+
}
88+
89+
if(!_appendCA && _rootCA) {
90+
return ((TLSSocket*)sock)->set_root_ca_cert(_rootCA);
91+
}
92+
6493
#if defined(MBEDTLS_FS_IO)
6594
mbed::BlockDevice* root = mbed::BlockDevice::get_default_instance();
6695
err = root->init();
@@ -82,12 +111,8 @@ class MbedSSLClient : public arduino::MbedClient {
82111
}
83112
#endif
84113

85-
if(_hostname && !_disableSNI) {
86-
((TLSSocket*)sock)->set_hostname(_hostname);
87-
}
88-
89-
if(_ca_cert_custom != NULL) {
90-
err = ((TLSSocket*)sock)->append_root_ca_cert(_ca_cert_custom);
114+
if(_rootCA != NULL) {
115+
err = ((TLSSocket*)sock)->append_root_ca_cert(_rootCA);
91116
}
92117
return err;
93118
}

0 commit comments

Comments
 (0)