Skip to content

Commit b159a2a

Browse files
committed
Fix SKID buffer overflow in CSR path and add missing NULL checks
Fix heap buffer overflow write in ReqCertFromX509() when subjKeyIdSz exceeds CTC_MAX_SKID_SIZE. The existing CertFromX509() path already had this bound check but the CSR path did not, so wolfSSL_X509_set_subject_key_id() with a long SKID followed by X509_REQ_sign() could overflow cert->skid. Also fix NULL dereference in wc_SRTP_KDF / wc_SRTCP_KDF when idx is NULL but kdrIdx >= 0. The idx pointer is dereferenced unconditionally in wc_srtp_kdf_first_block() but was not validated at the entry points. Additional minor hardening in wolfSSL_X509_get_der() and wolfSSL_i2d_X509() to guard against derCert->length truncation when cast to int and to reject zero/negative derSz before use.
1 parent c36beba commit b159a2a

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/x509.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4404,6 +4404,10 @@ const byte* wolfSSL_X509_get_der(WOLFSSL_X509* x509, int* outSz)
44044404
if (x509 == NULL || x509->derCert == NULL || outSz == NULL)
44054405
return NULL;
44064406

4407+
if (x509->derCert->length > (word32)INT_MAX) {
4408+
return NULL;
4409+
}
4410+
44074411
*outSz = (int)x509->derCert->length;
44084412
return x509->derCert->buffer;
44094413
}
@@ -8674,7 +8678,7 @@ int wolfSSL_i2d_X509(WOLFSSL_X509* x509, unsigned char** out)
86748678
}
86758679

86768680
der = wolfSSL_X509_get_der(x509, &derSz);
8677-
if (der == NULL) {
8681+
if (der == NULL || derSz <= 0) {
86788682
WOLFSSL_LEAVE("wolfSSL_i2d_X509", MEMORY_E);
86798683
return MEMORY_E;
86808684
}
@@ -11711,10 +11715,15 @@ WOLF_STACK_OF(WOLFSSL_X509_OBJECT)* wolfSSL_sk_X509_OBJECT_deep_copy(
1171111715
cert->isCA = req->isCa;
1171211716
cert->basicConstSet = req->basicConstSet;
1171311717
#ifdef WOLFSSL_CERT_EXT
11714-
if (req->subjKeyIdSz != 0) {
11718+
if (req->subjKeyIdSz != 0 &&
11719+
req->subjKeyIdSz <= CTC_MAX_SKID_SIZE) {
1171511720
XMEMCPY(cert->skid, req->subjKeyId, req->subjKeyIdSz);
1171611721
cert->skidSz = (int)req->subjKeyIdSz;
1171711722
}
11723+
else if (req->subjKeyIdSz > CTC_MAX_SKID_SIZE) {
11724+
WOLFSSL_MSG("SKID too large for cert buffer");
11725+
return WOLFSSL_FAILURE;
11726+
}
1171811727
if (req->keyUsageSet)
1171911728
cert->keyUsage = req->keyUsage;
1172011729

wolfcrypt/src/kdf.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,8 @@ int wc_SRTP_KDF(const byte* key, word32 keySz, const byte* salt, word32 saltSz,
10091009

10101010
/* Validate parameters. */
10111011
if ((key == NULL) || (keySz > AES_256_KEY_SIZE) || (salt == NULL) ||
1012-
(saltSz > WC_SRTP_MAX_SALT) || (kdrIdx < -1) || (kdrIdx > 24)) {
1012+
(saltSz > WC_SRTP_MAX_SALT) || (kdrIdx < -1) || (kdrIdx > 24) ||
1013+
(idx == NULL && kdrIdx >= 0)) {
10131014
ret = BAD_FUNC_ARG;
10141015
}
10151016

@@ -1103,7 +1104,8 @@ int wc_SRTCP_KDF_ex(const byte* key, word32 keySz, const byte* salt, word32 salt
11031104

11041105
/* Validate parameters. */
11051106
if ((key == NULL) || (keySz > AES_256_KEY_SIZE) || (salt == NULL) ||
1106-
(saltSz > WC_SRTP_MAX_SALT) || (kdrIdx < -1) || (kdrIdx > 24)) {
1107+
(saltSz > WC_SRTP_MAX_SALT) || (kdrIdx < -1) || (kdrIdx > 24) ||
1108+
(idx == NULL && kdrIdx >= 0)) {
11071109
ret = BAD_FUNC_ARG;
11081110
}
11091111

@@ -1194,7 +1196,7 @@ int wc_SRTP_KDF_label(const byte* key, word32 keySz, const byte* salt,
11941196
/* Validate parameters. */
11951197
if ((key == NULL) || (keySz > AES_256_KEY_SIZE) || (salt == NULL) ||
11961198
(saltSz > WC_SRTP_MAX_SALT) || (kdrIdx < -1) || (kdrIdx > 24) ||
1197-
(outKey == NULL)) {
1199+
(outKey == NULL) || (idx == NULL && kdrIdx >= 0)) {
11981200
ret = BAD_FUNC_ARG;
11991201
}
12001202

@@ -1267,7 +1269,7 @@ int wc_SRTCP_KDF_label(const byte* key, word32 keySz, const byte* salt,
12671269
/* Validate parameters. */
12681270
if ((key == NULL) || (keySz > AES_256_KEY_SIZE) || (salt == NULL) ||
12691271
(saltSz > WC_SRTP_MAX_SALT) || (kdrIdx < -1) || (kdrIdx > 24) ||
1270-
(outKey == NULL)) {
1272+
(outKey == NULL) || (idx == NULL && kdrIdx >= 0)) {
12711273
ret = BAD_FUNC_ARG;
12721274
}
12731275

0 commit comments

Comments
 (0)