22
22
#include <sys/time.h>
23
23
#include <sys/socket.h>
24
24
25
- /* Thirdparty headers */
26
- //#include "azure_c_shared_utility/xlogging.h"
27
25
#include "mbedtls/ctr_drbg.h"
28
26
#include "mbedtls/entropy.h"
27
+ #include "mbedtls/error.h"
29
28
#if MBEDTLS_VERSION_NUMBER < 0x03000000
30
29
#include "mbedtls/net.h"
31
30
#endif
36
35
#include "platform_utils.h"
37
36
/* Internal headers */
38
37
#include "netio.h"
38
+ #include "fileio.h"
39
39
40
40
/******************************************************************************
41
41
* DEFINITIONS
@@ -95,6 +95,71 @@ static mbedtls_ctr_drbg_context ctr_drbg;
95
95
static mbedtls_entropy_context entropy ;
96
96
#endif
97
97
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
+
98
163
static int prvInitConfig (NetIo_t * pxNet , const char * pcRootCA , const char * pcCert , const char * pcPrivKey , bool bFilePath )
99
164
{
100
165
int xRes = STATUS_SUCCESS ;
@@ -121,32 +186,36 @@ static int prvInitConfig(NetIo_t* pxNet, const char* pcRootCA, const char* pcCer
121
186
NetIo_setSendTimeout (pxNet , pxNet -> uSendTimeoutMs );
122
187
123
188
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 ||
136
201
#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 ) {
138
203
#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 ) {
140
205
#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" );
147
207
xRes = STATUS_NULL_ARG ;
208
+ goto CleanUp ;
148
209
}
149
210
}
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
+ }
150
219
} else {
151
220
mbedtls_ssl_conf_authmode (& (pxNet -> xConf ), MBEDTLS_SSL_VERIFY_OPTIONAL );
152
221
}
@@ -160,6 +229,7 @@ static int prvInitConfig(NetIo_t* pxNet, const char* pcRootCA, const char* pcCer
160
229
}
161
230
}
162
231
232
+ CleanUp :
163
233
return xRes ;
164
234
}
165
235
@@ -257,9 +327,9 @@ void NetIo_terminate(NetIoHandle xNetIoHandle)
257
327
int NetIo_connect (NetIoHandle xNetIoHandle , const char * pcHost , const char * pcPort )
258
328
{
259
329
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
+ // }
263
333
264
334
return prvConnect (xNetIoHandle , pcHost , pcPort , NULL , NULL , NULL , false);
265
335
}
@@ -336,16 +406,13 @@ int NetIo_recv(NetIoHandle xNetIoHandle, unsigned char* pBuffer, size_t uBufferS
336
406
337
407
bool NetIo_isDataAvailable (NetIoHandle xNetIoHandle )
338
408
{
339
- int xRes = STATUS_SUCCESS ;
340
409
NetIo_t * pxNet = (NetIo_t * ) xNetIoHandle ;
341
410
bool bDataAvailable = false;
342
411
struct timeval tv = {0 };
343
412
fd_set read_fds = {0 };
344
413
int fd = 0 ;
345
414
346
- if (pxNet == NULL ) {
347
- xRes = STATUS_NULL_ARG ;
348
- } else {
415
+ if (pxNet ) {
349
416
fd = pxNet -> xFd .fd ;
350
417
if (fd >= 0 ) {
351
418
FD_ZERO (& read_fds );
0 commit comments