Skip to content

Commit e2c2b51

Browse files
committed
lws/mbedtls-openssl: Supplement x509 method structure implementation
Signed-off-by: makejian <makejian@xiaomi.com>
1 parent c214b37 commit e2c2b51

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

lib/tls/mbedtls/wrapper/include/internal/ssl_types.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,10 @@ struct x509_method_st {
297297

298298
int (*x509_load)(X509 *x, const unsigned char *buf, int len);
299299

300+
int (*x509_load_file)(X509 *x, const char *file);
301+
302+
int (*x509_load_path)(X509 *x, const char *path);
303+
300304
int (*x509_show_info)(X509 *x);
301305
};
302306

lib/tls/mbedtls/wrapper/platform/ssl_pm.c

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

743756
int 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

761787
int pkey_pm_new(EVP_PKEY *pk, EVP_PKEY *m_pkey, void *rngctx)

0 commit comments

Comments
 (0)