@@ -32,7 +32,6 @@ bool ssl_generic_init_internal(
3232 int endpoint = 0 ;
3333 int ret = 0 ;
3434
35- mbedtls_x509_crt *ownCertificate = NULL ;
3635 HAL_Configuration_X509CaRootBundle *certStore = NULL ;
3736 HAL_Configuration_X509DeviceCertificate *deviceCert = NULL ;
3837
@@ -64,6 +63,7 @@ bool ssl_generic_init_internal(
6463 memset (context, 0 , sizeof (mbedTLS_NFContext));
6564
6665 // allocate memory for net context
66+ // this needs to be freed in ssl_exit_context_internal
6767 context->server_fd = (mbedtls_net_context *)platform_malloc (sizeof (mbedtls_net_context));
6868 if (context->server_fd == NULL )
6969 {
@@ -130,12 +130,12 @@ bool ssl_generic_init_internal(
130130
131131 // create and init X509 CRT
132132 // this needs to be freed in ssl_exit_context_internal
133- context->x509_crt = (mbedtls_x509_crt *)platform_malloc (sizeof (mbedtls_x509_crt));
134- if (context->x509_crt == NULL )
133+ context->ca_cert = (mbedtls_x509_crt *)platform_malloc (sizeof (mbedtls_x509_crt));
134+ if (context->ca_cert == NULL )
135135 {
136136 goto error;
137137 }
138- mbedtls_x509_crt_init (context->x509_crt );
138+ mbedtls_x509_crt_init (context->ca_cert );
139139
140140 // TODO: review if we can add some instance-unique data to the custom argument below
141141 if (mbedtls_ctr_drbg_seed (context->ctr_drbg , mbedtls_entropy_func, context->entropy , NULL , 0 ) != 0 )
@@ -207,11 +207,12 @@ bool ssl_generic_init_internal(
207207 // when the format is a string it has to include the terminator otherwise the parse will fail //
208208 // ///////////////////////////////////////////////////////////////////////////////////////////////
209209 mbedtls_x509_crt_parse (
210- context->x509_crt ,
210+ context->ca_cert ,
211211 (const unsigned char *)certStore->Certificate ,
212212 certStore->CertificateSize );
213213
214214 platform_free (certStore);
215+ certStore = NULL ;
215216 }
216217 }
217218
@@ -260,21 +261,22 @@ bool ssl_generic_init_internal(
260261 }
261262
262263 // parse certificate
263- ownCertificate = (mbedtls_x509_crt *)platform_malloc (sizeof (mbedtls_x509_crt));
264- if (ownCertificate == NULL )
264+ // this needs to be freed in ssl_exit_context_internal
265+ context->own_cert = (mbedtls_x509_crt *)platform_malloc (sizeof (mbedtls_x509_crt));
266+ if (context->own_cert == NULL )
265267 {
266268 goto error;
267269 }
268270
269- mbedtls_x509_crt_init (ownCertificate );
271+ mbedtls_x509_crt_init (context-> own_cert );
270272
271- if (mbedtls_x509_crt_parse (ownCertificate , (const unsigned char *)certificate, certLength))
273+ if (mbedtls_x509_crt_parse (context-> own_cert , (const unsigned char *)certificate, certLength))
272274 {
273275 // failed parsing own certificate failed
274276 goto error;
275277 }
276278
277- if (mbedtls_ssl_conf_own_cert (context->conf , ownCertificate , context->pk ))
279+ if (mbedtls_ssl_conf_own_cert (context->conf , context-> own_cert , context->pk ))
278280 {
279281 // configuring own certificate failed
280282 goto error;
@@ -284,6 +286,7 @@ bool ssl_generic_init_internal(
284286 if (deviceCert)
285287 {
286288 platform_free (deviceCert);
289+ deviceCert = NULL ;
287290 }
288291 }
289292 else
@@ -294,7 +297,7 @@ bool ssl_generic_init_internal(
294297 context->pk = NULL ;
295298 }
296299
297- mbedtls_ssl_conf_ca_chain (context->conf , context->x509_crt , NULL );
300+ mbedtls_ssl_conf_ca_chain (context->conf , context->ca_cert , NULL );
298301
299302 psa_crypto_init ();
300303
@@ -343,7 +346,8 @@ bool ssl_generic_init_internal(
343346
344347 mbedtls_ctr_drbg_free (context->ctr_drbg );
345348 mbedtls_entropy_free (context->entropy );
346- mbedtls_x509_crt_free (context->x509_crt );
349+ mbedtls_x509_crt_free (context->ca_cert );
350+ mbedtls_x509_crt_free (context->own_cert );
347351 mbedtls_ssl_config_free (context->conf );
348352 mbedtls_ssl_free (context->ssl );
349353
@@ -373,21 +377,21 @@ bool ssl_generic_init_internal(
373377 platform_free (context->server_fd );
374378 }
375379
376- if (context->x509_crt )
380+ if (context->ca_cert )
377381 {
378- platform_free (context->x509_crt );
382+ platform_free (context->ca_cert );
379383 }
380384
381- if (context->pk )
385+ if (context->own_cert )
382386 {
383- platform_free (context->pk );
387+ platform_free (context->own_cert );
384388 }
385389
386- if (ownCertificate )
390+ if (context-> pk )
387391 {
388- mbedtls_x509_crt_free (ownCertificate);
389- platform_free (ownCertificate);
392+ platform_free (context->pk );
390393 }
394+
391395 if (context)
392396 {
393397 platform_free (context);
0 commit comments