Skip to content

Commit 93e3327

Browse files
committed
netio.c: Read certs using readFile before passing with mbedtls APIs
1 parent b9c22be commit 93e3327

File tree

1 file changed

+96
-29
lines changed
  • esp_port/components/api_call

1 file changed

+96
-29
lines changed

esp_port/components/api_call/netio.c

Lines changed: 96 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222
#include <sys/time.h>
2323
#include <sys/socket.h>
2424

25-
/* Thirdparty headers */
26-
//#include "azure_c_shared_utility/xlogging.h"
2725
#include "mbedtls/ctr_drbg.h"
2826
#include "mbedtls/entropy.h"
27+
#include "mbedtls/error.h"
2928
#if MBEDTLS_VERSION_NUMBER < 0x03000000
3029
#include "mbedtls/net.h"
3130
#endif
@@ -36,6 +35,7 @@
3635
#include "platform_utils.h"
3736
/* Internal headers */
3837
#include "netio.h"
38+
#include "fileio.h"
3939

4040
/******************************************************************************
4141
* DEFINITIONS
@@ -95,6 +95,71 @@ static mbedtls_ctr_drbg_context ctr_drbg;
9595
static mbedtls_entropy_context entropy;
9696
#endif
9797

98+
static int prvReadAndParseCertificate(mbedtls_x509_crt* pCert, const char* pcPath)
99+
{
100+
STATUS retStatus = STATUS_SUCCESS;
101+
UINT64 cert_len = 0;
102+
PBYTE cert_buf = NULL;
103+
CHAR errBuf[128];
104+
105+
if (readFile(pcPath, FALSE, NULL, &cert_len) != STATUS_SUCCESS) {
106+
DLOGE("Failed to get cert file size");
107+
retStatus = STATUS_NULL_ARG;
108+
goto CleanUp;
109+
}
110+
111+
cert_buf = (PBYTE) MEMCALLOC(1, cert_len + 1);
112+
CHK(cert_buf != NULL, STATUS_NOT_ENOUGH_MEMORY);
113+
CHK_STATUS(readFile(pcPath, FALSE, cert_buf, &cert_len));
114+
int ret = mbedtls_x509_crt_parse(pCert, cert_buf, (size_t) (cert_len + 1));
115+
if (ret != 0) {
116+
mbedtls_strerror(ret, errBuf, SIZEOF(errBuf));
117+
DLOGE("mbedtls_x509_crt_parse failed: %s", errBuf);
118+
}
119+
CHK(ret == 0, STATUS_INVALID_CA_CERT_PATH);
120+
121+
CleanUp:
122+
CHK_LOG_ERR(retStatus);
123+
if (cert_buf != NULL) {
124+
MEMFREE(cert_buf);
125+
}
126+
return retStatus;
127+
}
128+
129+
static int prvReadAndParsePrivateKey(mbedtls_pk_context* pPrivKey, const char* pcPath)
130+
{
131+
STATUS retStatus = STATUS_SUCCESS;
132+
UINT64 key_len = 0;
133+
PBYTE key_buf = NULL;
134+
CHAR errBuf[128];
135+
136+
if (readFile(pcPath, FALSE, NULL, &key_len) != STATUS_SUCCESS) {
137+
DLOGE("Failed to get private key file size");
138+
retStatus = STATUS_NULL_ARG;
139+
goto CleanUp;
140+
}
141+
142+
key_buf = (PBYTE) MEMALLOC(key_len + 1);
143+
CHK(key_buf != NULL, STATUS_NOT_ENOUGH_MEMORY);
144+
CHK_STATUS(readFile(pcPath, FALSE, key_buf, &key_len));
145+
#if MBEDTLS_VERSION_NUMBER < 0x03000000
146+
int ret = mbedtls_pk_parse_key(pPrivKey, key_buf, key_len + 1, NULL, 0);
147+
#else
148+
int ret = mbedtls_pk_parse_key(pPrivKey, key_buf, key_len + 1, NULL, 0, &mbedtls_ctr_drbg_random, &ctr_drbg);
149+
#endif
150+
if (ret != 0) {
151+
mbedtls_strerror(ret, errBuf, SIZEOF(errBuf));
152+
DLOGE("mbedtls_pk_parse_key failed: %s", errBuf);
153+
}
154+
CHK(ret == 0, STATUS_FILE_CREDENTIAL_PROVIDER_OPEN_FILE_FAILED);
155+
156+
CleanUp:
157+
if (key_buf != NULL) {
158+
MEMFREE(key_buf);
159+
}
160+
return retStatus;
161+
}
162+
98163
static int prvInitConfig(NetIo_t* pxNet, const char* pcRootCA, const char* pcCert, const char* pcPrivKey, bool bFilePath)
99164
{
100165
int xRes = STATUS_SUCCESS;
@@ -121,32 +186,36 @@ static int prvInitConfig(NetIo_t* pxNet, const char* pcRootCA, const char* pcCer
121186
NetIo_setSendTimeout(pxNet, pxNet->uSendTimeoutMs);
122187

123188
if (pcRootCA != NULL && pcCert != NULL && pcPrivKey != NULL) {
124-
if (bFilePath == false &&
125-
(mbedtls_x509_crt_parse(pxNet->pRootCA, (void*) pcRootCA, strlen(pcRootCA) + 1) != 0 ||
126-
mbedtls_x509_crt_parse(pxNet->pCert, (void*) pcCert, strlen(pcCert) + 1) != 0 ||
127-
#if MBEDTLS_VERSION_NUMBER < 0x03000000
128-
mbedtls_pk_parse_key(pxNet->pPrivKey, (void*) pcPrivKey, strlen(pcPrivKey) + 1, NULL, 0) != 0)) {
129-
#else
130-
mbedtls_pk_parse_key(pxNet->pPrivKey, (void*) pcPrivKey, strlen(pcPrivKey) + 1, NULL, 0, &mbedtls_ctr_drbg_random, &ctr_drbg) != 0)) {
131-
#endif
132-
DLOGE("Failed to parse x509");
133-
xRes = STATUS_NULL_ARG;
134-
} else if (mbedtls_x509_crt_parse_file(pxNet->pRootCA, (void*) pcRootCA) != 0 ||
135-
mbedtls_x509_crt_parse_file(pxNet->pCert, (void*) pcCert) != 0 ||
189+
if (bFilePath) {
190+
// Read and parse certificates from files
191+
if (prvReadAndParseCertificate(pxNet->pRootCA, pcRootCA) != STATUS_SUCCESS ||
192+
prvReadAndParseCertificate(pxNet->pCert, pcCert) != STATUS_SUCCESS ||
193+
prvReadAndParsePrivateKey(pxNet->pPrivKey, pcPrivKey) != STATUS_SUCCESS) {
194+
xRes = STATUS_NULL_ARG;
195+
goto CleanUp;
196+
}
197+
} else {
198+
// Parse certificates from memory buffers
199+
if (mbedtls_x509_crt_parse(pxNet->pRootCA, (void*) pcRootCA, strlen(pcRootCA) + 1) != 0 ||
200+
mbedtls_x509_crt_parse(pxNet->pCert, (void*) pcCert, strlen(pcCert) + 1) != 0 ||
136201
#if MBEDTLS_VERSION_NUMBER < 0x03000000
137-
mbedtls_pk_parse_keyfile(pxNet->pPrivKey, (void*) pcPrivKey, NULL) != 0) {
202+
mbedtls_pk_parse_key(pxNet->pPrivKey, (void*) pcPrivKey, strlen(pcPrivKey) + 1, NULL, 0) != 0) {
138203
#else
139-
mbedtls_pk_parse_keyfile(pxNet->pPrivKey, (void*) pcPrivKey, NULL, &mbedtls_ctr_drbg_random, &ctr_drbg) != 0) {
204+
mbedtls_pk_parse_key(pxNet->pPrivKey, (void*) pcPrivKey, strlen(pcPrivKey) + 1, NULL, 0, &mbedtls_ctr_drbg_random, &ctr_drbg) != 0) {
140205
#endif
141-
} else {
142-
mbedtls_ssl_conf_authmode(&(pxNet->xConf), MBEDTLS_SSL_VERIFY_REQUIRED);
143-
mbedtls_ssl_conf_ca_chain(&(pxNet->xConf), pxNet->pRootCA, NULL);
144-
145-
if (mbedtls_ssl_conf_own_cert(&(pxNet->xConf), pxNet->pCert, pxNet->pPrivKey) != 0) {
146-
DLOGE("Failed to conf own cert");
206+
DLOGE("Failed to parse x509");
147207
xRes = STATUS_NULL_ARG;
208+
goto CleanUp;
148209
}
149210
}
211+
212+
mbedtls_ssl_conf_authmode(&(pxNet->xConf), MBEDTLS_SSL_VERIFY_REQUIRED);
213+
mbedtls_ssl_conf_ca_chain(&(pxNet->xConf), pxNet->pRootCA, NULL);
214+
215+
if (mbedtls_ssl_conf_own_cert(&(pxNet->xConf), pxNet->pCert, pxNet->pPrivKey) != 0) {
216+
DLOGE("Failed to conf own cert");
217+
xRes = STATUS_NULL_ARG;
218+
}
150219
} else {
151220
mbedtls_ssl_conf_authmode(&(pxNet->xConf), MBEDTLS_SSL_VERIFY_OPTIONAL);
152221
}
@@ -160,6 +229,7 @@ static int prvInitConfig(NetIo_t* pxNet, const char* pcRootCA, const char* pcCer
160229
}
161230
}
162231

232+
CleanUp:
163233
return xRes;
164234
}
165235

@@ -257,9 +327,9 @@ void NetIo_terminate(NetIoHandle xNetIoHandle)
257327
int NetIo_connect(NetIoHandle xNetIoHandle, const char* pcHost, const char* pcPort)
258328
{
259329
DLOGD("Reached here: %s %d", __func__, __LINE__);
260-
if (heap_caps_check_integrity_all(true) == false) {
261-
DLOGE("Heap integrity check failed, line %d", __LINE__);
262-
}
330+
// if (heap_caps_check_integrity_all(true) == false) {
331+
// DLOGE("Heap integrity check failed, line %d", __LINE__);
332+
// }
263333

264334
return prvConnect(xNetIoHandle, pcHost, pcPort, NULL, NULL, NULL, false);
265335
}
@@ -336,16 +406,13 @@ int NetIo_recv(NetIoHandle xNetIoHandle, unsigned char* pBuffer, size_t uBufferS
336406

337407
bool NetIo_isDataAvailable(NetIoHandle xNetIoHandle)
338408
{
339-
int xRes = STATUS_SUCCESS;
340409
NetIo_t* pxNet = (NetIo_t*) xNetIoHandle;
341410
bool bDataAvailable = false;
342411
struct timeval tv = {0};
343412
fd_set read_fds = {0};
344413
int fd = 0;
345414

346-
if (pxNet == NULL) {
347-
xRes = STATUS_NULL_ARG;
348-
} else {
415+
if (pxNet) {
349416
fd = pxNet->xFd.fd;
350417
if (fd >= 0) {
351418
FD_ZERO(&read_fds);

0 commit comments

Comments
 (0)