Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for client certificate and private key #992

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions libraries/SE05X/src/WiFiSSLSE050Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ arduino::MbedSSLSE050Client::MbedSSLSE050Client() {
void arduino::MbedSSLSE050Client::setEccSlot(int KeySlot, const byte cert[], int certLen) {

_keySlot = KeySlot;
_client_cert_len = certLen;
_client_cert = cert;
_certLen = certLen;
_cert = cert;
}

void WiFiSSLSE050Client::setEccSlot(int KeySlot, const byte cert[], int certLen) {
Expand Down
35 changes: 13 additions & 22 deletions libraries/SE05X/src/WiFiSSLSE050Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,28 @@ class MbedSSLSE050Client : public arduino::MbedSSLClient {
void setEccSlot(int KeySlot, const byte cert[], int certLen);

private:
const byte* _client_cert;
const char* _ca_cert;
int _client_cert_len;
const byte* _cert;
int _certLen;
int _keySlot;
sss_object_t _keyObject;

int setRootCAClientCertKey() {
if( NSAPI_ERROR_OK != ((TLSSocket*)sock)->set_root_ca_cert_path("/wlan/")) {
return 0;
int err = setRootCA();
if (err != NSAPI_ERROR_OK) {
return err;
}

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

if( NSAPI_ERROR_OK != ((TLSSocket*)sock)->append_root_ca_cert(_ca_cert_custom)) {
return 0;
if(((TLSSocket*)sock)->set_client_cert_key((void*)_cert,
(size_t)_certLen,
&_keyObject,
SE05X.getDeviceCtx()) != NSAPI_ERROR_OK) {
return NSAPI_ERROR_DEVICE_ERROR;
}

if(!SE05X.getObjectHandle(_keySlot, &_keyObject)) {
return 0;
}

if( NSAPI_ERROR_OK != ((TLSSocket*)sock)->set_client_cert_key((void*)_client_cert,
(size_t)_client_cert_len,
&_keyObject,
SE05X.getDeviceCtx())) {
return 0;
}

return 1;
return NSAPI_ERROR_OK;
}
};

Expand Down
21 changes: 21 additions & 0 deletions libraries/SocketWrapper/src/AClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,24 @@ void arduino::ASslClient::appendCustomCACert(const char* ca_cert) {
}
static_cast<MbedSSLClient*>(client.get())->appendCustomCACert(ca_cert);
}

void arduino::ASslClient::setCACert(const char* rootCA) {
if (!client) {
newMbedClient();
}
static_cast<MbedSSLClient*>(client.get())->setCACert(rootCA);
}

void arduino::ASslClient::setCertificate(const char* clientCert) {
if (!client) {
newMbedClient();
}
static_cast<MbedSSLClient*>(client.get())->setCertificate(clientCert);
}

void arduino::ASslClient::setPrivateKey(const char* privateKey) {
if (!client) {
newMbedClient();
}
static_cast<MbedSSLClient*>(client.get())->setPrivateKey(privateKey);
}
3 changes: 3 additions & 0 deletions libraries/SocketWrapper/src/AClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class ASslClient : public AClient {
void disableSNI(bool statusSNI);

void appendCustomCACert(const char* ca_cert);
void setCACert(const char* rootCA);
void setCertificate(const char* clientCert);
void setPrivateKey(const char* privateKey);

protected:
virtual void newMbedClient();
Expand Down
7 changes: 5 additions & 2 deletions libraries/SocketWrapper/src/MbedSSLClient.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include "MbedSSLClient.h"

arduino::MbedSSLClient::MbedSSLClient()
: _ca_cert_custom(nullptr),
: _rootCA(nullptr),
_hostname(nullptr),
_disableSNI(false) {
_clientCert(nullptr),
_privateKey(nullptr),
_disableSNI(false),
_appendCA(true) {

onBeforeConnect(mbed::callback(this, &MbedSSLClient::setRootCA));
};
47 changes: 36 additions & 11 deletions libraries/SocketWrapper/src/MbedSSLClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,48 @@ class MbedSSLClient : public arduino::MbedClient {
_disableSNI = statusSNI;
}

void appendCustomCACert(const char* ca_cert) {
_ca_cert_custom = ca_cert;
void appendCustomCACert(const char* rootCA) {
_rootCA = rootCA;
_appendCA = true;
}
void setCACert(const char* rootCA) {
_rootCA = rootCA;
_appendCA = false;
}
void setCertificate(const char* clientCert) {
_clientCert = clientCert;
}
void setPrivateKey(const char* privateKey) {
_privateKey = privateKey;
}

protected:
const char* _ca_cert_custom;
private:
const char* _rootCA;
const char* _hostname;
const char* _clientCert;
const char* _privateKey;
bool _disableSNI;
bool _appendCA;

private:
protected:
int setRootCA() {
int err = 0;

if(_hostname && !_disableSNI) {
((TLSSocket*)sock)->set_hostname(_hostname);
}

if(_clientCert && _privateKey) {
err = ((TLSSocket*)sock)->set_client_cert_key(_clientCert, _privateKey);
if( err != NSAPI_ERROR_OK) {
return err;
}
}

if(!_appendCA && _rootCA) {
return ((TLSSocket*)sock)->set_root_ca_cert(_rootCA);
}

#if defined(MBEDTLS_FS_IO)
mbed::BlockDevice* root = mbed::BlockDevice::get_default_instance();
err = root->init();
Expand All @@ -82,12 +111,8 @@ class MbedSSLClient : public arduino::MbedClient {
}
#endif

if(_hostname && !_disableSNI) {
((TLSSocket*)sock)->set_hostname(_hostname);
}

if(_ca_cert_custom != NULL) {
err = ((TLSSocket*)sock)->append_root_ca_cert(_ca_cert_custom);
if(_rootCA != NULL) {
err = ((TLSSocket*)sock)->append_root_ca_cert(_rootCA);
}
return err;
}
Expand Down