Skip to content

Commit f83ebb2

Browse files
committed
In wolfSSL_CTX_set_cert_store, send certificates into the CertMgr
1 parent 5e8d018 commit f83ebb2

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

src/ssl.c

+15
Original file line numberDiff line numberDiff line change
@@ -12904,6 +12904,7 @@ int wolfSSL_set_compression(WOLFSSL* ssl)
1290412904

1290512905
void wolfSSL_CTX_set_cert_store(WOLFSSL_CTX* ctx, WOLFSSL_X509_STORE* str)
1290612906
{
12907+
WOLFSSL_X509 *x = NULL;
1290712908
WOLFSSL_ENTER("wolfSSL_CTX_set_cert_store");
1290812909
if (ctx == NULL || str == NULL || ctx->cm == str->cm) {
1290912910
return;
@@ -12920,6 +12921,20 @@ int wolfSSL_set_compression(WOLFSSL* ssl)
1292012921
ctx->cm = str->cm;
1292112922
ctx->x509_store.cm = str->cm;
1292212923

12924+
/* wolfSSL_CTX_set_cert_store() (this function) associates str with the
12925+
* wolfSSL_CTX. It is clear that this is a TLS use case which means we
12926+
* should move all the certs, if any, into the CertMgr and set
12927+
* str->certs to NULL as that will allow the certs to be properly
12928+
* processed. */
12929+
if (str->certs != NULL) {
12930+
while (wolfSSL_sk_X509_num(str->certs) > 0) {
12931+
x = wolfSSL_sk_X509_pop(str->certs);
12932+
X509StoreAddCa(str, x, WOLFSSL_USER_CA);
12933+
}
12934+
wolfSSL_sk_X509_pop_free(str->certs, NULL);
12935+
str->certs = NULL;
12936+
}
12937+
1292312938
/* free existing store if it exists */
1292412939
wolfSSL_X509_STORE_free(ctx->x509_store_pt);
1292512940
ctx->x509_store.cache = str->cache;

src/x509_str.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
#ifdef OPENSSL_EXTRA
3535
static int X509StoreGetIssuerEx(WOLFSSL_X509 **issuer,
3636
WOLFSSL_STACK *certs, WOLFSSL_X509 *x);
37-
static int X509StoreAddCa(WOLFSSL_X509_STORE* store,
38-
WOLFSSL_X509* x509, int type);
3937
#endif
4038

4139
/* Based on OpenSSL default max depth */
@@ -1361,8 +1359,7 @@ WOLFSSL_X509_LOOKUP* wolfSSL_X509_STORE_add_lookup(WOLFSSL_X509_STORE* store,
13611359
return &store->lookup;
13621360
}
13631361

1364-
static int X509StoreAddCa(WOLFSSL_X509_STORE* store,
1365-
WOLFSSL_X509* x509, int type)
1362+
int X509StoreAddCa(WOLFSSL_X509_STORE* store, WOLFSSL_X509* x509, int type)
13661363
{
13671364
int result = WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR);
13681365
DerBuffer* derCert = NULL;

tests/api.c

+41
Original file line numberDiff line numberDiff line change
@@ -28382,6 +28382,46 @@ static int test_wolfSSL_CTX_set_srp_password(void)
2838228382
return EXPECT_RESULT();
2838328383
}
2838428384

28385+
static int test_wolfSSL_CTX_set_cert_store_null_certs(void)
28386+
{
28387+
EXPECT_DECLS;
28388+
#if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_TLS)
28389+
X509_STORE *store = NULL;
28390+
WOLFSSL_CTX *ctx = NULL;
28391+
WOLFSSL_METHOD *method = NULL;
28392+
X509 *cert = NULL;
28393+
const char caCert[] = "./certs/ca-cert.pem";
28394+
28395+
/* Create a new X509_STORE */
28396+
ExpectNotNull(store = X509_STORE_new());
28397+
28398+
/* Load a certificate */
28399+
ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(caCert,
28400+
SSL_FILETYPE_PEM));
28401+
28402+
/* Add the certificate to the store */
28403+
ExpectIntEQ(X509_STORE_add_cert(store, cert), SSL_SUCCESS);
28404+
ExpectNotNull(store->certs);
28405+
28406+
/* Create a new SSL_CTX */
28407+
ExpectNotNull(method = wolfSSLv23_server_method());
28408+
ExpectNotNull(ctx = wolfSSL_CTX_new(method));
28409+
28410+
/* Set the store in the SSL_CTX */
28411+
wolfSSL_CTX_set_cert_store(ctx, store);
28412+
28413+
/* Verify that the certs member of the store is null */
28414+
ExpectNull(store->certs);
28415+
28416+
/* Clean up */
28417+
wolfSSL_CTX_free(ctx);
28418+
X509_free(cert);
28419+
28420+
#endif
28421+
return EXPECT_RESULT();
28422+
}
28423+
28424+
2838528425
static int test_wolfSSL_X509_STORE(void)
2838628426
{
2838728427
EXPECT_DECLS;
@@ -67086,6 +67126,7 @@ TEST_CASE testCases[] = {
6708667126
TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
6708767127
TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
6708867128
TEST_DECL(test_wolfSSL_X509_STORE),
67129+
TEST_DECL(test_wolfSSL_CTX_set_cert_store_null_certs),
6708967130
TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
6709067131
TEST_DECL(test_X509_STORE_get0_objects),
6709167132
TEST_DECL(test_wolfSSL_X509_load_crl_file),

wolfssl/internal.h

+5
Original file line numberDiff line numberDiff line change
@@ -2780,6 +2780,11 @@ WOLFSSL_LOCAL int X509StoreLoadCertBuffer(WOLFSSL_X509_STORE *str,
27802780
byte *buf, word32 bufLen, int type);
27812781
#endif /* !defined NO_CERTS */
27822782

2783+
#ifdef OPENSSL_EXTRA
2784+
WOLFSSL_LOCAL int X509StoreAddCa(WOLFSSL_X509_STORE* store,
2785+
WOLFSSL_X509* x509, int type);
2786+
#endif
2787+
27832788
/* wolfSSL Sock Addr */
27842789
struct WOLFSSL_SOCKADDR {
27852790
unsigned int sz; /* sockaddr size */

0 commit comments

Comments
 (0)