Skip to content

Commit c90fb03

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

File tree

4 files changed

+63
-4
lines changed

4 files changed

+63
-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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28412,6 +28412,47 @@ 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+
!defined(NO_WOLFSSL_SERVER)
28420+
X509_STORE *store = NULL;
28421+
WOLFSSL_CTX *ctx = NULL;
28422+
WOLFSSL_METHOD *method = NULL;
28423+
X509 *cert = NULL;
28424+
const char caCert[] = "./certs/ca-cert.pem";
28425+
28426+
/* Create a new X509_STORE */
28427+
ExpectNotNull(store = X509_STORE_new());
28428+
28429+
/* Load a certificate */
28430+
ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(caCert,
28431+
SSL_FILETYPE_PEM));
28432+
28433+
/* Add the certificate to the store */
28434+
ExpectIntEQ(X509_STORE_add_cert(store, cert), SSL_SUCCESS);
28435+
ExpectNotNull(store->certs);
28436+
28437+
/* Create a new SSL_CTX */
28438+
ExpectNotNull(method = wolfSSLv23_server_method());
28439+
ExpectNotNull(ctx = wolfSSL_CTX_new(method));
28440+
28441+
/* Set the store in the SSL_CTX */
28442+
wolfSSL_CTX_set_cert_store(ctx, store);
28443+
28444+
/* Verify that the certs member of the store is null */
28445+
ExpectNull(store->certs);
28446+
28447+
/* Clean up */
28448+
wolfSSL_CTX_free(ctx);
28449+
X509_free(cert);
28450+
28451+
#endif
28452+
return EXPECT_RESULT();
28453+
}
28454+
28455+
2841528456
static int test_wolfSSL_X509_STORE(void)
2841628457
{
2841728458
EXPECT_DECLS;
@@ -67156,6 +67197,7 @@ TEST_CASE testCases[] = {
6715667197
TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
6715767198
TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
6715867199
TEST_DECL(test_wolfSSL_X509_STORE),
67200+
TEST_DECL(test_wolfSSL_CTX_set_cert_store_null_certs),
6715967201
TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
6716067202
TEST_DECL(test_X509_STORE_get0_objects),
6716167203
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)