Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a comment specifying that these functions has to be called before connect

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
5 changes: 4 additions & 1 deletion libraries/SocketWrapper/src/MbedSSLClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
arduino::MbedSSLClient::MbedSSLClient()
: _ca_cert_custom(nullptr),
_hostname(nullptr),
_disableSNI(false) {
_clientCert(nullptr),
_privateKey(nullptr),
_disableSNI(false),
_appendCA(true) {

onBeforeConnect(mbed::callback(this, &MbedSSLClient::setRootCA));
};
33 changes: 29 additions & 4 deletions libraries/SocketWrapper/src/MbedSSLClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,46 @@ class MbedSSLClient : public arduino::MbedClient {

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

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

private:
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 && _ca_cert_custom) {
return ((TLSSocket*)sock)->set_root_ca_cert(_ca_cert_custom);
}

#if defined(MBEDTLS_FS_IO)
mbed::BlockDevice* root = mbed::BlockDevice::get_default_instance();
err = root->init();
Expand All @@ -82,10 +111,6 @@ 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);
}
Expand Down