Skip to content

Commit 9af8716

Browse files
Merge pull request #8087 from ColtonWilley/x509_store_rewrite
Initial rewrite of X509 STORE to replicate openssl behavior
2 parents e7e2053 + cab20fb commit 9af8716

File tree

9 files changed

+1072
-170
lines changed

9 files changed

+1072
-170
lines changed

src/ssl.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5535,13 +5535,15 @@ int AddCA(WOLFSSL_CERT_MANAGER* cm, DerBuffer** pDer, int type, int verify)
55355535
}
55365536
}
55375537

5538-
if (ret == 0 && cert->isCA == 0 && type != WOLFSSL_USER_CA) {
5538+
if (ret == 0 && cert->isCA == 0 && type != WOLFSSL_USER_CA &&
5539+
type != WOLFSSL_TEMP_CA) {
55395540
WOLFSSL_MSG("\tCan't add as CA if not actually one");
55405541
ret = NOT_CA_ERROR;
55415542
}
55425543
#ifndef ALLOW_INVALID_CERTSIGN
55435544
else if (ret == 0 && cert->isCA == 1 && type != WOLFSSL_USER_CA &&
5544-
!cert->selfSigned && (cert->extKeyUsage & KEYUSE_KEY_CERT_SIGN) == 0) {
5545+
type != WOLFSSL_TEMP_CA && !cert->selfSigned &&
5546+
(cert->extKeyUsage & KEYUSE_KEY_CERT_SIGN) == 0) {
55455547
/* Intermediate CA certs are required to have the keyCertSign
55465548
* extension set. User loaded root certs are not. */
55475549
WOLFSSL_MSG("\tDoesn't have key usage certificate signing");

src/ssl_certman.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,12 @@ int wolfSSL_CertManagerUnloadCAs(WOLFSSL_CERT_MANAGER* cm)
455455
return ret;
456456
}
457457

458-
int wolfSSL_CertManagerUnloadIntermediateCerts(WOLFSSL_CERT_MANAGER* cm)
458+
static int wolfSSL_CertManagerUnloadIntermediateCertsEx(
459+
WOLFSSL_CERT_MANAGER* cm, byte type)
459460
{
460461
int ret = WOLFSSL_SUCCESS;
461462

462-
WOLFSSL_ENTER("wolfSSL_CertManagerUnloadIntermediateCerts");
463+
WOLFSSL_ENTER("wolfSSL_CertManagerUnloadIntermediateCertsEx");
463464

464465
/* Validate parameter. */
465466
if (cm == NULL) {
@@ -471,7 +472,7 @@ int wolfSSL_CertManagerUnloadIntermediateCerts(WOLFSSL_CERT_MANAGER* cm)
471472
}
472473
if (ret == WOLFSSL_SUCCESS) {
473474
/* Dispose of CA table. */
474-
FreeSignerTableType(cm->caTable, CA_TABLE_SIZE, WOLFSSL_CHAIN_CA,
475+
FreeSignerTableType(cm->caTable, CA_TABLE_SIZE, type,
475476
cm->heap);
476477

477478
/* Unlock CA table. */
@@ -481,6 +482,22 @@ int wolfSSL_CertManagerUnloadIntermediateCerts(WOLFSSL_CERT_MANAGER* cm)
481482
return ret;
482483
}
483484

485+
#if defined(OPENSSL_EXTRA)
486+
static int wolfSSL_CertManagerUnloadTempIntermediateCerts(
487+
WOLFSSL_CERT_MANAGER* cm)
488+
{
489+
WOLFSSL_ENTER("wolfSSL_CertManagerUnloadTempIntermediateCerts");
490+
return wolfSSL_CertManagerUnloadIntermediateCertsEx(cm, WOLFSSL_TEMP_CA);
491+
}
492+
#endif
493+
494+
int wolfSSL_CertManagerUnloadIntermediateCerts(
495+
WOLFSSL_CERT_MANAGER* cm)
496+
{
497+
WOLFSSL_ENTER("wolfSSL_CertManagerUnloadIntermediateCerts");
498+
return wolfSSL_CertManagerUnloadIntermediateCertsEx(cm, WOLFSSL_CHAIN_CA);
499+
}
500+
484501
#ifdef WOLFSSL_TRUST_PEER_CERT
485502
/* Unload the trusted peers table.
486503
*

src/x509.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5559,7 +5559,7 @@ WOLFSSL_EVP_PKEY* wolfSSL_X509_get_pubkey(WOLFSSL_X509* x509)
55595559
* size of this subset and its memory usage */
55605560
#endif /* OPENSSL_EXTRA_X509_SMALL || KEEP_PEER_CERT || SESSION_CERTS */
55615561

5562-
#if defined(OPENSSL_ALL)
5562+
#if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
55635563
/*
55645564
* Converts a and b to DER and then does an XMEMCMP to check if they match.
55655565
* Returns 0 when certificates match and WOLFSSL_FATAL_ERROR when they don't.
@@ -7536,7 +7536,6 @@ int wolfSSL_X509_LOOKUP_load_file(WOLFSSL_X509_LOOKUP* lookup,
75367536
byte* pem = NULL;
75377537
byte* curr = NULL;
75387538
byte* prev = NULL;
7539-
WOLFSSL_X509* x509;
75407539
const char* header = NULL;
75417540
const char* footer = NULL;
75427541

@@ -7597,12 +7596,8 @@ int wolfSSL_X509_LOOKUP_load_file(WOLFSSL_X509_LOOKUP* lookup,
75977596
}
75987597
else if (wc_PemGetHeaderFooter(CERT_TYPE, &header, &footer) == 0 &&
75997598
XSTRNSTR((char*)curr, header, (unsigned int)sz) != NULL) {
7600-
x509 = wolfSSL_X509_load_certificate_buffer(curr, (int)sz,
7601-
WOLFSSL_FILETYPE_PEM);
7602-
if (x509 == NULL)
7603-
goto end;
7604-
ret = wolfSSL_X509_STORE_add_cert(lookup->store, x509);
7605-
wolfSSL_X509_free(x509);
7599+
ret = X509StoreLoadCertBuffer(lookup->store, curr,
7600+
(word32)sz, WOLFSSL_FILETYPE_PEM);
76067601
if (ret != WOLFSSL_SUCCESS)
76077602
goto end;
76087603
curr = (byte*)XSTRNSTR((char*)curr, footer, (unsigned int)sz);
@@ -14210,6 +14205,9 @@ int wolfSSL_X509_NAME_digest(const WOLFSSL_X509_NAME *name,
1421014205

1421114206
#if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
1421214207
defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
14208+
#if defined(OPENSSL_EXTRA) && \
14209+
((defined(SESSION_CERTS) && !defined(WOLFSSL_QT)) || \
14210+
defined(WOLFSSL_SIGNER_DER_CERT))
1421314211

1421414212
/**
1421514213
* Find the issuing cert of the input cert. On a self-signed cert this
@@ -14280,6 +14278,8 @@ static int x509GetIssuerFromCM(WOLFSSL_X509 **issuer, WOLFSSL_CERT_MANAGER* cm,
1428014278

1428114279
return WOLFSSL_SUCCESS;
1428214280
}
14281+
#endif /* if defined(OPENSSL_EXTRA) && (defined(SESSION_CERTS) || \
14282+
defined(WOLFSSL_SIGNER_DER_CERT)) */
1428314283

1428414284
void wolfSSL_X509_email_free(WOLF_STACK_OF(WOLFSSL_STRING) *sk)
1428514285
{

0 commit comments

Comments
 (0)