4
4
#define LOG_CLASS "TLS_mbedtls"
5
5
#include "../Include_i.h"
6
6
7
+ // Read and parse CA certificate
8
+ PRIVATE_API STATUS readAndParseCACertificate (PTlsSession pTlsSession )
9
+ {
10
+ ENTERS ();
11
+ STATUS retStatus = STATUS_SUCCESS ;
12
+ UINT64 cert_len = 0 ;
13
+ PBYTE cert_buf = NULL ;
14
+ CHAR errBuf [128 ];
15
+
16
+ CHK (pTlsSession != NULL , STATUS_NULL_ARG );
17
+
18
+ CHK_STATUS (readFile (KVS_CA_CERT_PATH , FALSE, NULL , & cert_len ));
19
+ CHK (cert_len > 0 , STATUS_INVALID_CERT_PATH_LENGTH );
20
+ cert_buf = (PBYTE ) MEMCALLOC (1 , cert_len + 1 );
21
+ CHK (cert_buf != NULL , STATUS_NOT_ENOUGH_MEMORY );
22
+ CHK_STATUS (readFile (KVS_CA_CERT_PATH , FALSE, cert_buf , & cert_len ));
23
+ int ret = mbedtls_x509_crt_parse (& pTlsSession -> cacert , cert_buf , (SIZE_T ) (cert_len + 1 ));
24
+ if (ret != 0 ) {
25
+ mbedtls_strerror (ret , errBuf , SIZEOF (errBuf ));
26
+ DLOGE ("mbedtls_x509_crt_parse failed: %s" , errBuf );
27
+ }
28
+ CHK (ret == 0 , STATUS_INVALID_CA_CERT_PATH );
29
+
30
+ CleanUp :
31
+ CHK_LOG_ERR (retStatus );
32
+ SAFE_MEMFREE (cert_buf );
33
+
34
+ LEAVES ();
35
+ return retStatus ;
36
+ }
37
+
7
38
STATUS createTlsSession (PTlsSessionCallbacks pCallbacks , PTlsSession * ppTlsSession )
8
39
{
9
40
ENTERS ();
@@ -26,9 +57,11 @@ STATUS createTlsSession(PTlsSessionCallbacks pCallbacks, PTlsSession* ppTlsSessi
26
57
mbedtls_ssl_config_init (& pTlsSession -> sslCtxConfig );
27
58
mbedtls_ssl_init (& pTlsSession -> sslCtx );
28
59
CHK (mbedtls_ctr_drbg_seed (& pTlsSession -> ctrDrbg , mbedtls_entropy_func , & pTlsSession -> entropy , NULL , 0 ) == 0 , STATUS_CREATE_SSL_FAILED );
29
- CHK (mbedtls_x509_crt_parse_file (& pTlsSession -> cacert , KVS_CA_CERT_PATH ) == 0 , STATUS_INVALID_CA_CERT_PATH );
60
+
61
+ CHK_STATUS (readAndParseCACertificate (pTlsSession ));
30
62
31
63
CleanUp :
64
+
32
65
if (STATUS_FAILED (retStatus ) && pTlsSession != NULL ) {
33
66
freeTlsSession (& pTlsSession );
34
67
}
0 commit comments