Skip to content

Commit 37982e7

Browse files
Add X509StoreRemoveCa wrapper around RemoveCa
WOLFSSL_X509's calculated subject key hash is not guaranteed to match the cert's, ie. in the case that NO_SHA is defined. Use the same logic as AddCa, parsing the DER cert and using the decoded cert's subject key hash.
1 parent 6fa5886 commit 37982e7

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/ssl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6097,7 +6097,7 @@ int RemoveCA(WOLFSSL_CERT_MANAGER* cm, byte* hash, byte type)
60976097
{
60986098
Signer* current;
60996099
Signer* prev;
6100-
int ret = 0;
6100+
int ret = WC_NO_ERR_TRACE(WOLFSSL_FAILURE);
61016101
word32 row;
61026102

61036103
WOLFSSL_MSG("Removing a CA");
@@ -6131,7 +6131,7 @@ int RemoveCA(WOLFSSL_CERT_MANAGER* cm, byte* hash, byte type)
61316131
prev->next = current->next;
61326132
}
61336133
FreeSigner(current, cm->heap);
6134-
ret = 1;
6134+
ret = WOLFSSL_SUCCESS;
61356135
break;
61366136
}
61376137
prev = current;

src/x509_str.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ static int X509StorePopCert(WOLFSSL_STACK *certs_stack, WOLFSSL_STACK *dest_stac
3838
WOLFSSL_X509 *cert);
3939
static int X509StoreAddCa(WOLFSSL_X509_STORE* store,
4040
WOLFSSL_X509* x509, int type);
41+
static int X509StoreRemoveCa(WOLFSSL_X509_STORE* store,
42+
WOLFSSL_X509* x509, int type);
4143
#endif
4244

4345
/* Based on OpenSSL default max depth */
@@ -568,7 +570,9 @@ int wolfSSL_X509_verify_cert(WOLFSSL_X509_STORE_CTX* ctx)
568570
* cert with the same subject key which will work. Retry until all
569571
* possible candidate certs are exhausted. */
570572
WOLFSSL_MSG("X509_verify_cert current cert failed, retrying with other certs.");
571-
RemoveCA(ctx->store->cm, ctx->current_cert->subjKeyId, WOLFSSL_TEMP_CA);
573+
ret = X509StoreRemoveCa(ctx->store, ctx->current_cert, WOLFSSL_TEMP_CA);
574+
if (ret != WOLFSSL_SUCCESS)
575+
goto exit;
572576
X509StorePopCert(certs, failedCerts, ctx->current_cert);
573577
ctx->current_cert = wolfSSL_sk_X509_pop(ctx->chain);
574578
depth++;
@@ -1439,6 +1443,33 @@ static int X509StoreAddCa(WOLFSSL_X509_STORE* store,
14391443
return result;
14401444
}
14411445

1446+
static int X509StoreRemoveCa(WOLFSSL_X509_STORE* store,
1447+
WOLFSSL_X509* x509, int type) {
1448+
int result = WC_NO_ERR_TRACE(WOLFSSL_FATAL_ERROR);
1449+
DecodedCert* dCert = NULL;
1450+
1451+
if (store != NULL && x509 != NULL && x509->derCert != NULL) {
1452+
dCert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
1453+
DYNAMIC_TYPE_DCERT);
1454+
1455+
if (dCert == NULL) {
1456+
return result;
1457+
}
1458+
XMEMSET(dCert, 0, sizeof(DecodedCert));
1459+
wc_InitDecodedCert(dCert, x509->derCert->buffer, x509->derCert->length, NULL);
1460+
result = wc_ParseCert(dCert, CA_TYPE, NO_VERIFY, store->cm);
1461+
if (result)
1462+
return WOLFSSL_FATAL_ERROR;
1463+
1464+
result = RemoveCA(store->cm, dCert->extSubjKeyId, type);
1465+
}
1466+
1467+
if (dCert)
1468+
wc_FreeDecodedCert(dCert);
1469+
1470+
return result;
1471+
}
1472+
14421473

14431474
int wolfSSL_X509_STORE_add_cert(WOLFSSL_X509_STORE* store, WOLFSSL_X509* x509)
14441475
{

0 commit comments

Comments
 (0)