Skip to content

Commit 77ca1ca

Browse files
committed
lws/mbedtls: export Export SSL_CTX_load_verify series interfaces
Support interfaces SSL_CTX_load_verify_file/SSL_CTX_load_verify_dir/SSL_CTX_load_verify_locations to align with openssl-client behavior Signed-off-by: makejian <makejian@xiaomi.com>
1 parent 426124d commit 77ca1ca

File tree

7 files changed

+168
-1
lines changed

7 files changed

+168
-1
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,16 @@
7272
new, \
7373
free, \
7474
load, \
75+
load_file, \
76+
load_path, \
7577
show_info) \
7678
const X509_METHOD* func_name(void) { \
7779
static const X509_METHOD func_name##_data LOCAL_ATRR = { \
7880
new, \
7981
free, \
8082
load, \
83+
load_file, \
84+
load_path, \
8185
show_info \
8286
}; \
8387
return &func_name##_data; \

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/include/openssl/ssl.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,44 @@ const char *SSL_get_psk_identity_hint(SSL *ssl);
18221822
*/
18231823
const char *SSL_get_psk_identity(SSL *ssl);
18241824

1825+
/**
1826+
* @brief Load a file containing CA certificates for verification into the SSL context
1827+
*
1828+
* @param ctx - SSL context pointer
1829+
* @param CAfile - Path to the file containing CA certificates.
1830+
*
1831+
* @return result
1832+
* 1 : OK
1833+
* 0 : failed
1834+
*/
1835+
int SSL_CTX_load_verify_file(SSL_CTX *ctx, const char *CAfile);
1836+
1837+
/**
1838+
* @brief Load a directory containing CA certificates for verification into the SSL context
1839+
*
1840+
* @param ctx - SSL context pointer
1841+
* @param CApath - Path to the directory containing CA certificates.
1842+
*
1843+
* @return result
1844+
* 1 : OK
1845+
* 0 : failed
1846+
*/
1847+
int SSL_CTX_load_verify_dir(SSL_CTX *ctx, const char *CApath);
1848+
1849+
/**
1850+
* @brief Load CA certificates from file and/or directory for verification
1851+
*
1852+
* @param ctx - SSL context pointer
1853+
* @param CAfile - Path to the file containing CA certificates.
1854+
* @param CApath - Path to the directory containing CA certificates.
1855+
*
1856+
* @return result
1857+
* 1 : OK
1858+
* 0 : failed
1859+
*/
1860+
int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
1861+
const char *CApath);
1862+
18251863
#ifdef __cplusplus
18261864
}
18271865
#endif

lib/tls/mbedtls/wrapper/include/platform/ssl_pm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ int x509_pm_show_info(X509 *x);
4747
int x509_pm_new(X509 *x, X509 *m_x);
4848
void x509_pm_free(X509 *x);
4949
int x509_pm_load(X509 *x, const unsigned char *buffer, int len);
50+
int x509_pm_load_file(X509 *x, const char *path);
51+
int x509_pm_load_path(X509 *x, const char *path);
5052

5153
int pkey_pm_new(EVP_PKEY *pk, EVP_PKEY *m_pk, void *rngctx);
5254
void pkey_pm_free(EVP_PKEY *pk);

lib/tls/mbedtls/wrapper/library/ssl_lib.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,3 +1257,59 @@ void SSL_set_alpn_select_cb(SSL *ssl, void *arg)
12571257

12581258
_ssl_set_alpn_list(ssl);
12591259
}
1260+
1261+
int SSL_CTX_load_verify_file(SSL_CTX *ctx, const char *CAfile)
1262+
{
1263+
X509 *x;
1264+
int ret;
1265+
1266+
SSL_ASSERT1(ctx);
1267+
SSL_ASSERT1(CAfile);
1268+
1269+
x = X509_new();
1270+
ret = X509_METHOD_CALL(load_file, x, CAfile);
1271+
if (ret) {
1272+
X509_free(x);
1273+
return 0;
1274+
}
1275+
1276+
SSL_CTX_add_client_CA(ctx, x);
1277+
return 1;
1278+
}
1279+
1280+
int SSL_CTX_load_verify_dir(SSL_CTX *ctx, const char *CApath)
1281+
{
1282+
X509 *x;
1283+
int ret;
1284+
1285+
SSL_ASSERT1(ctx);
1286+
SSL_ASSERT1(CApath);
1287+
1288+
x = X509_new();
1289+
ret = X509_METHOD_CALL(load_path, x, CApath);
1290+
if (ret) {
1291+
X509_free(x);
1292+
return 0;
1293+
}
1294+
1295+
SSL_CTX_add_client_CA(ctx, x);
1296+
return 1;
1297+
}
1298+
1299+
int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
1300+
const char *CApath)
1301+
{
1302+
if (CAfile == NULL && CApath == NULL) {
1303+
return 0;
1304+
}
1305+
1306+
if (CAfile != NULL && !SSL_CTX_load_verify_file(ctx, CAfile)) {
1307+
return 0;
1308+
}
1309+
1310+
if (CApath != NULL && !SSL_CTX_load_verify_dir(ctx, CApath)) {
1311+
return 0;
1312+
}
1313+
1314+
return 1;
1315+
}

lib/tls/mbedtls/wrapper/library/ssl_methods.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ IMPLEMENT_SSL_METHOD(SSL3_VERSION, -1, TLS_method_func, SSLv3_method);
7979
*/
8080
IMPLEMENT_X509_METHOD(X509_method,
8181
x509_pm_new, x509_pm_free,
82-
x509_pm_load, x509_pm_show_info);
82+
x509_pm_load, x509_pm_load_file,
83+
x509_pm_load_path, x509_pm_show_info);
8384

8485
/**
8586
* @brief get private key object method

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,68 @@ int x509_pm_load(X509 *x, const unsigned char *buffer, int len)
722722
return -1;
723723
}
724724

725+
int x509_pm_load_file(X509 *x, const char *path)
726+
{
727+
int ret;
728+
struct x509_pm *x509_pm = (struct x509_pm *)x->x509_pm;
729+
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);
740+
if (ret) {
741+
SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL,
742+
"mbedtls_x509_crt_parse_file return -0x%x", -ret);
743+
goto failed;
744+
}
745+
746+
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;
754+
}
755+
756+
int x509_pm_load_path(X509 *x, const char *path)
757+
{
758+
int ret;
759+
struct x509_pm *x509_pm = (struct x509_pm *)x->x509_pm;
760+
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);
771+
if (ret) {
772+
SSL_DEBUG(SSL_PLATFORM_ERROR_LEVEL,
773+
"mbedtls_x509_crt_parse_path return -0x%x", -ret);
774+
goto failed;
775+
}
776+
777+
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;
785+
}
786+
725787
int pkey_pm_new(EVP_PKEY *pk, EVP_PKEY *m_pkey, void *rngctx)
726788
{
727789
struct pkey_pm *pkey_pm;

0 commit comments

Comments
 (0)