Skip to content

Commit 55d740e

Browse files
committed
TLSSocketWrapper: add method to retrieve certificates from filesystem
1 parent 22d926f commit 55d740e

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

connectivity/netsocket/include/netsocket/TLSSocketWrapper.h

+12
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,18 @@ class TLSSocketWrapper : public Socket {
116116
*/
117117
nsapi_error_t set_root_ca_cert(const char *root_ca_pem);
118118

119+
/** Sets the certification of Root CA.
120+
*
121+
* @note Must be called before calling connect()
122+
*
123+
* @param root_ca Path containing Root CA Certificate files in any Mbed TLS-supported format.
124+
* @retval NSAPI_ERROR_OK on success.
125+
* @retval NSAPI_ERROR_NO_MEMORY in case there is not enough memory to allocate certificate.
126+
* @retval NSAPI_ERROR_PARAMETER in case the provided root_ca parameter failed parsing.
127+
*
128+
*/
129+
nsapi_error_t set_root_ca_cert_path(const void *root_ca);
130+
119131
/** Sets client certificate, and client private key.
120132
*
121133
* @param client_cert Client certification in PEM or DER format.

connectivity/netsocket/source/TLSSocketWrapper.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,35 @@ nsapi_error_t TLSSocketWrapper::set_root_ca_cert(const char *root_ca_pem)
121121
return set_root_ca_cert(root_ca_pem, strlen(root_ca_pem) + 1);
122122
}
123123

124+
nsapi_error_t TLSSocketWrapper::set_root_ca_cert_path(const void *root_ca)
125+
{
126+
#if !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO)
127+
return NSAPI_ERROR_UNSUPPORTED;
128+
#else
129+
mbedtls_x509_crt *crt;
130+
131+
crt = new (std::nothrow) mbedtls_x509_crt;
132+
if (!crt) {
133+
return NSAPI_ERROR_NO_MEMORY;
134+
}
135+
136+
mbedtls_x509_crt_init(crt);
137+
138+
/* Parse CA certification */
139+
int ret = mbedtls_x509_crt_parse_path(crt, static_cast<const char *>(root_ca));
140+
if (ret < 0) {
141+
print_mbedtls_error("mbedtls_x509_crt_parse", ret);
142+
mbedtls_x509_crt_free(crt);
143+
delete crt;
144+
return NSAPI_ERROR_PARAMETER;
145+
}
146+
set_ca_chain(crt);
147+
_cacert_allocated = true;
148+
return NSAPI_ERROR_OK;
149+
#endif
150+
}
151+
152+
124153
nsapi_error_t TLSSocketWrapper::set_client_cert_key(const char *client_cert_pem, const char *client_private_key_pem)
125154
{
126155
return set_client_cert_key(client_cert_pem, strlen(client_cert_pem) + 1, client_private_key_pem, strlen(client_private_key_pem) + 1);

0 commit comments

Comments
 (0)