@@ -727,35 +727,61 @@ int x509_pm_load_file(X509 *x, const char *path)
727727 int ret ;
728728 struct x509_pm * x509_pm = (struct x509_pm * )x -> x509_pm ;
729729
730- mbedtls_x509_crt_free (& x509_pm -> x509_crt );
731- mbedtls_x509_crt_init (& x509_pm -> x509_crt );
732- ret = mbedtls_x509_crt_parse_file (& x509_pm -> x509_crt , path );
730+ if (!x509_pm -> x509_crt ) {
731+ x509_pm -> x509_crt = ssl_mem_malloc (sizeof (mbedtls_x509_crt ) + 80 );
732+ if (!x509_pm -> x509_crt ) {
733+ SSL_DEBUG (SSL_PLATFORM_ERROR_LEVEL , "no enough memory > (x509_pm->x509_crt)" );
734+ goto no_mem ;
735+ }
736+ mbedtls_x509_crt_init (x509_pm -> x509_crt );
737+ }
738+
739+ ret = mbedtls_x509_crt_parse_file (x509_pm -> x509_crt , path );
733740 if (ret ) {
734741 SSL_DEBUG (SSL_PLATFORM_ERROR_LEVEL ,
735742 "mbedtls_x509_crt_parse_file return -0x%x" , - ret );
736- mbedtls_x509_crt_free (& x509_pm -> x509_crt );
737- return -1 ;
743+ goto failed ;
738744 }
739745
740746 return 0 ;
747+
748+ failed :
749+ mbedtls_x509_crt_free (x509_pm -> x509_crt );
750+ ssl_mem_free (x509_pm -> x509_crt );
751+ x509_pm -> x509_crt = NULL ;
752+ no_mem :
753+ return -1 ;
741754}
742755
743756int x509_pm_load_path (X509 * x , const char * path )
744757{
745758 int ret ;
746759 struct x509_pm * x509_pm = (struct x509_pm * )x -> x509_pm ;
747760
748- mbedtls_x509_crt_free (& x509_pm -> x509_crt );
749- mbedtls_x509_crt_init (& x509_pm -> x509_crt );
750- ret = mbedtls_x509_crt_parse_path (& x509_pm -> x509_crt , path );
761+ if (!x509_pm -> x509_crt ) {
762+ x509_pm -> x509_crt = ssl_mem_malloc (sizeof (mbedtls_x509_crt ) + 80 );
763+ if (!x509_pm -> x509_crt ) {
764+ SSL_DEBUG (SSL_PLATFORM_ERROR_LEVEL , "no enough memory > (x509_pm->x509_crt)" );
765+ goto no_mem ;
766+ }
767+ mbedtls_x509_crt_init (x509_pm -> x509_crt );
768+ }
769+
770+ ret = mbedtls_x509_crt_parse_path (x509_pm -> x509_crt , path );
751771 if (ret ) {
752772 SSL_DEBUG (SSL_PLATFORM_ERROR_LEVEL ,
753- "mbedtls_x509_crt_parse_file return -0x%x" , - ret );
754- mbedtls_x509_crt_free (& x509_pm -> x509_crt );
755- return -1 ;
773+ "mbedtls_x509_crt_parse_path return -0x%x" , - ret );
774+ goto failed ;
756775 }
757776
758777 return 0 ;
778+
779+ failed :
780+ mbedtls_x509_crt_free (x509_pm -> x509_crt );
781+ ssl_mem_free (x509_pm -> x509_crt );
782+ x509_pm -> x509_crt = NULL ;
783+ no_mem :
784+ return -1 ;
759785}
760786
761787int pkey_pm_new (EVP_PKEY * pk , EVP_PKEY * m_pkey , void * rngctx )
0 commit comments