Skip to content

Commit adc18d7

Browse files
committed
In wolfSSL_CTX_set_cert_store, send certificates into the CertMgr
1 parent 813e36a commit adc18d7

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

src/ssl.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12930,6 +12930,7 @@ int wolfSSL_set_compression(WOLFSSL* ssl)
1293012930

1293112931
void wolfSSL_CTX_set_cert_store(WOLFSSL_CTX* ctx, WOLFSSL_X509_STORE* str)
1293212932
{
12933+
WOLFSSL_X509 *x = NULL;
1293312934
WOLFSSL_ENTER("wolfSSL_CTX_set_cert_store");
1293412935
if (ctx == NULL || str == NULL || ctx->cm == str->cm) {
1293512936
return;
@@ -12946,6 +12947,20 @@ int wolfSSL_set_compression(WOLFSSL* ssl)
1294612947
ctx->cm = str->cm;
1294712948
ctx->x509_store.cm = str->cm;
1294812949

12950+
/* wolfSSL_CTX_set_cert_store() (this function) associates str with the
12951+
* wolfSSL_CTX. It is clear that this is a TLS use case which means we
12952+
* should move all the certs, if any, into the CertMgr and set
12953+
* str->certs to NULL as that will allow the certs to be properly
12954+
* processed. */
12955+
if (str->certs != NULL) {
12956+
while (wolfSSL_sk_X509_num(str->certs) > 0) {
12957+
x = wolfSSL_sk_X509_pop(str->certs);
12958+
X509StoreAddCa(str, x, WOLFSSL_USER_CA);
12959+
}
12960+
wolfSSL_sk_X509_pop_free(str->certs, NULL);
12961+
str->certs = NULL;
12962+
}
12963+
1294912964
/* free existing store if it exists */
1295012965
wolfSSL_X509_STORE_free(ctx->x509_store_pt);
1295112966
ctx->x509_store.cache = str->cache;

src/x509_str.c

Lines changed: 1 addition & 4 deletions
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 */
@@ -1367,8 +1365,7 @@ WOLFSSL_X509_LOOKUP* wolfSSL_X509_STORE_add_lookup(WOLFSSL_X509_STORE* store,
13671365
return &store->lookup;
13681366
}
13691367

1370-
static int X509StoreAddCa(WOLFSSL_X509_STORE* store,
1371-
WOLFSSL_X509* x509, int type)
1368+
int X509StoreAddCa(WOLFSSL_X509_STORE* store, WOLFSSL_X509* x509, int type)
13721369
{
13731370
int result = WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR);
13741371
DerBuffer* derCert = NULL;

tests/api.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28412,6 +28412,46 @@ static int test_wolfSSL_CTX_set_srp_password(void)
2841228412
return EXPECT_RESULT();
2841328413
}
2841428414

28415+
static int test_wolfSSL_CTX_set_cert_store_null_certs(void)
28416+
{
28417+
EXPECT_DECLS;
28418+
#if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_TLS)
28419+
X509_STORE *store = NULL;
28420+
WOLFSSL_CTX *ctx = NULL;
28421+
WOLFSSL_METHOD *method = NULL;
28422+
X509 *cert = NULL;
28423+
const char caCert[] = "./certs/ca-cert.pem";
28424+
28425+
/* Create a new X509_STORE */
28426+
ExpectNotNull(store = X509_STORE_new());
28427+
28428+
/* Load a certificate */
28429+
ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(caCert,
28430+
SSL_FILETYPE_PEM));
28431+
28432+
/* Add the certificate to the store */
28433+
ExpectIntEQ(X509_STORE_add_cert(store, cert), SSL_SUCCESS);
28434+
ExpectNotNull(store->certs);
28435+
28436+
/* Create a new SSL_CTX */
28437+
ExpectNotNull(method = wolfSSLv23_server_method());
28438+
ExpectNotNull(ctx = wolfSSL_CTX_new(method));
28439+
28440+
/* Set the store in the SSL_CTX */
28441+
wolfSSL_CTX_set_cert_store(ctx, store);
28442+
28443+
/* Verify that the certs member of the store is null */
28444+
ExpectNull(store->certs);
28445+
28446+
/* Clean up */
28447+
wolfSSL_CTX_free(ctx);
28448+
X509_free(cert);
28449+
28450+
#endif
28451+
return EXPECT_RESULT();
28452+
}
28453+
28454+
2841528455
static int test_wolfSSL_X509_STORE(void)
2841628456
{
2841728457
EXPECT_DECLS;
@@ -67156,6 +67196,7 @@ TEST_CASE testCases[] = {
6715667196
TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
6715767197
TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
6715867198
TEST_DECL(test_wolfSSL_X509_STORE),
67199+
TEST_DECL(test_wolfSSL_CTX_set_cert_store_null_certs),
6715967200
TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
6716067201
TEST_DECL(test_X509_STORE_get0_objects),
6716167202
TEST_DECL(test_wolfSSL_X509_load_crl_file),

wolfssl/internal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2781,6 +2781,11 @@ WOLFSSL_LOCAL int X509StoreLoadCertBuffer(WOLFSSL_X509_STORE *str,
27812781
byte *buf, word32 bufLen, int type);
27822782
#endif /* !defined NO_CERTS */
27832783

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

0 commit comments

Comments
 (0)