Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/crl.c
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ int BufferLoadCRL(WOLFSSL_CRL* crl, const byte* buff, long sz, int type,

WOLFSSL_ENTER("BufferLoadCRL");

if (crl == NULL || buff == NULL || sz == 0)
if (crl == NULL || buff == NULL || sz <= 0)
return BAD_FUNC_ARG;

if (type == WOLFSSL_FILETYPE_PEM) {
Expand Down Expand Up @@ -1175,7 +1175,7 @@ int GetCRLInfo(WOLFSSL_CRL* crl, CrlInfo* info, const byte* buff,

WOLFSSL_ENTER("GetCRLInfo");

if (crl == NULL || info == NULL || buff == NULL || sz == 0)
if (crl == NULL || info == NULL || buff == NULL || sz <= 0)
return BAD_FUNC_ARG;

if (type == WOLFSSL_FILETYPE_PEM) {
Expand Down
4 changes: 4 additions & 0 deletions src/ocsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,10 @@ OcspResponse* wolfSSL_d2i_OCSP_RESPONSE(OcspResponse** response,

if (data == NULL)
return NULL;
if (*data == NULL)
return NULL;
if (len <= 0)
return NULL;

if (response != NULL)
resp = *response;
Expand Down
7 changes: 6 additions & 1 deletion src/pk_ec.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,8 @@ static WOLFSSL_EC_GROUP* wolfssl_ec_group_d2i(WOLFSSL_EC_GROUP** group,

if (in_pp == NULL || *in_pp == NULL)
return NULL;
if (inSz <= 0)
return NULL;

in = *in_pp;

Expand Down Expand Up @@ -4998,7 +5000,10 @@ WOLFSSL_ECDSA_SIG* wolfSSL_d2i_ECDSA_SIG(WOLFSSL_ECDSA_SIG** sig,
WOLFSSL_ECDSA_SIG *s = NULL;

/* Validate parameter. */
if (pp == NULL) {
if (pp == NULL || *pp == NULL) {
err = 1;
}
if ((!err) && (len <= 0)) {
err = 1;
}
if (!err) {
Expand Down
8 changes: 8 additions & 0 deletions src/pk_rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,10 @@ WOLFSSL_RSA *wolfSSL_d2i_RSAPublicKey(WOLFSSL_RSA **out,
WOLFSSL_ERROR_MSG("Bad argument");
err = 1;
}
if ((!err) && (derSz <= 0)) {
WOLFSSL_ERROR_MSG("Bad argument");
err = 1;
}
/* Create a new RSA key to return. */
if ((!err) && ((rsa = wolfSSL_RSA_new()) == NULL)) {
WOLFSSL_ERROR_MSG("RSA_new failed");
Expand Down Expand Up @@ -503,6 +507,10 @@ WOLFSSL_RSA *wolfSSL_d2i_RSAPrivateKey(WOLFSSL_RSA **out,
WOLFSSL_ERROR_MSG("Bad argument");
err = 1;
}
if ((!err) && (derSz <= 0)) {
WOLFSSL_ERROR_MSG("Bad argument");
err = 1;
}
/* Create a new RSA key to return. */
if ((!err) && ((rsa = wolfSSL_RSA_new()) == NULL)) {
WOLFSSL_ERROR_MSG("RSA_new failed");
Expand Down
13 changes: 9 additions & 4 deletions src/ssl_asn1.c
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ WOLFSSL_ASN1_BIT_STRING* wolfSSL_d2i_ASN1_BIT_STRING(

WOLFSSL_ENTER("wolfSSL_d2i_ASN1_BIT_STRING");

if (src == NULL || *src == NULL || len == 0)
if (src == NULL || *src == NULL || len <= 0)
return NULL;

if (GetASNTag(*src, &idx, &tag, (word32)len) < 0)
Expand Down Expand Up @@ -2984,7 +2984,7 @@ static WOLFSSL_ASN1_STRING* d2i_ASN1_STRING(WOLFSSL_ASN1_STRING** out,

WOLFSSL_ENTER("d2i_ASN1_STRING");

if (src == NULL || *src == NULL || len == 0)
if (src == NULL || *src == NULL || len <= 0)
return NULL;

if (GetASNTag(*src, &idx, &tag, (word32)len) < 0)
Expand Down Expand Up @@ -3159,16 +3159,21 @@ int wolfSSL_ASN1_STRING_set(WOLFSSL_ASN1_STRING* asn1, const void* data, int sz)
}

if (ret == 1) {
/* Cast to size_t BEFORE adding 1 to prevent signed overflow
* when sz == INT_MAX. By this point sz >= 0 (negative sz is
* handled above as OpenSSL -1/strlen compat). */
size_t allocSz = (size_t)sz + 1;

/* Dispose of any existing dynamic data. */
if (asn1->isDynamic) {
XFREE(asn1->data, NULL, DYNAMIC_TYPE_OPENSSL);
asn1->data = NULL;
}

/* Check string will fit - including NUL. */
if (sz + 1 > CTC_NAME_SIZE) {
if (allocSz > CTC_NAME_SIZE) {
/* Allocate new buffer. */
asn1->data = (char*)XMALLOC((size_t)(sz + 1), NULL,
asn1->data = (char*)XMALLOC(allocSz, NULL,
DYNAMIC_TYPE_OPENSSL);
if (asn1->data == NULL) {
ret = 0;
Expand Down
4 changes: 4 additions & 0 deletions src/ssl_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -2423,6 +2423,10 @@ int ProcessBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff, long sz,
if ((ret == 0) && (type == CHAIN_CERT_TYPE)) {
ret = BAD_FUNC_ARG;
}
/* Reject negative size - would wrap to huge word32. */
if ((ret == 0) && (sz < 0)) {
ret = BAD_FUNC_ARG;
}

#ifdef WOLFSSL_SMALL_STACK
if (ret == 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/x509.c
Original file line number Diff line number Diff line change
Expand Up @@ -4147,7 +4147,7 @@ static WOLFSSL_X509* d2i_X509orX509REQ(WOLFSSL_X509** x509,

WOLFSSL_ENTER("wolfSSL_X509_d2i");

if (in != NULL && len != 0
if (in != NULL && len > 0
#ifndef WOLFSSL_CERT_REQ
&& req == 0
#else
Expand Down Expand Up @@ -11291,7 +11291,7 @@ WOLFSSL_X509_ALGOR* wolfSSL_d2i_X509_ALGOR(WOLFSSL_X509_ALGOR** out,

WOLFSSL_ENTER("wolfSSL_d2i_X509_ALGOR");

if (src == NULL || *src == NULL || len == 0)
if (src == NULL || *src == NULL || len <= 0)
return NULL;

if (GetAlgoId(*src, &idx, &oid, oidIgnoreType, (word32)len) != 0)
Expand Down
43 changes: 43 additions & 0 deletions tests/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -2516,6 +2516,28 @@ static int test_wolfSSL_CTX_use_certificate_buffer(void)

} /* END test_wolfSSL_CTX_use_certificate_buffer */

static int test_ProcessBuffer_negative_size(void)
{
EXPECT_DECLS;
#if !defined(NO_CERTS) && !defined(NO_TLS) && !defined(NO_WOLFSSL_SERVER) && \
defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
WOLFSSL_CTX* ctx = NULL;

ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));

ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx,
server_cert_der_2048, -1, WOLFSSL_FILETYPE_ASN1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));

ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx,
server_cert_der_2048, sizeof_server_cert_der_2048,
WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);

wolfSSL_CTX_free(ctx);
#endif
return EXPECT_RESULT();
}

static int test_wolfSSL_use_certificate_buffer(void)
{
EXPECT_DECLS;
Expand Down Expand Up @@ -12159,6 +12181,12 @@ static int test_wc_PemToDer(void)

XMEMSET(&info, 0, sizeof(info));

{
const byte dummy = 'X';
ExpectIntEQ(wc_PemToDer(&dummy, -1, CERT_TYPE, &pDer, NULL,
&info, &eccKey), WC_NO_ERR_TRACE(BAD_FUNC_ARG));
}

ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
ExpectIntEQ(ret = wc_PemToDer(cert_buf, (long int)cert_sz, CERT_TYPE, &pDer, NULL,
&info, &eccKey), 0);
Expand Down Expand Up @@ -12332,6 +12360,10 @@ static int test_wc_KeyPemToDer(void)
ExpectIntEQ(wc_KeyPemToDer(cert_buf, 0, (byte*)&cert_der, cert_sz, ""),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));

/* Bad arg: NULL der buffer with negative pemSz (NULL-deref guard). */
ExpectIntEQ(wc_KeyPemToDer(cert_buf, -1, NULL, 0, ""),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));

/* Test normal operation */
cert_dersz = cert_sz; /* DER will be smaller than PEM */
ExpectNotNull(cert_der = (byte*)malloc((size_t)cert_dersz));
Expand Down Expand Up @@ -23478,6 +23510,13 @@ static int test_wc_SetIssueBuffer(void)

ExpectIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));

/* Negative-size rejection: pin both wc_SetIssuerBuffer and
* wc_SetSubjectBuffer (representatives for the seven wc_Set* siblings). */
ExpectIntEQ(wc_SetIssuerBuffer(&forgedCert, peerCertBuf, -1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
ExpectIntEQ(wc_SetSubjectBuffer(&forgedCert, peerCertBuf, -1),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));

wolfSSL_FreeX509(x509);
#endif
return EXPECT_RESULT();
Expand Down Expand Up @@ -27379,6 +27418,9 @@ static int test_wolfSSL_CTX_LoadCRL_largeCRLnum(void)
WOLFSSL_SUCCESS);
AssertIntEQ(XMEMCMP(
crlInfo.crlNumber, exp_crlnum, XSTRLEN(exp_crlnum)), 0);
ExpectIntEQ(wolfSSL_CertManagerGetCRLInfo(
cm, &crlInfo, crlLrgCrlNumBuff, -1, WOLFSSL_FILETYPE_PEM),
WC_NO_ERR_TRACE(BAD_FUNC_ARG));
/* Expect to fail loading CRL because of >21 octets CRL number */
ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_lrgcrlnum2,
WOLFSSL_FILETYPE_PEM),
Expand Down Expand Up @@ -40624,6 +40666,7 @@ TEST_CASE testCases[] = {
TEST_DECL(test_wolfSSL_CTX_use_certificate),
TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
TEST_DECL(test_ProcessBuffer_negative_size),
TEST_DECL(test_wolfSSL_use_certificate_buffer),
TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
TEST_DECL(test_wolfSSL_CTX_use_RSAPrivateKey_file),
Expand Down
5 changes: 5 additions & 0 deletions tests/api/test_ossl_ec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,11 @@ int test_wolfSSL_ECDSA_SIG(void)
sig = NULL;

ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
/* Reject non-positive length and *pp == NULL (PR #10207). */
cp = sigData;
ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, -1));
cp = NULL;
ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData)));
cp = sigDataBad;
ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
cp = sigData;
Expand Down
33 changes: 31 additions & 2 deletions wolfcrypt/src/asn.c
Original file line number Diff line number Diff line change
Expand Up @@ -24671,10 +24671,10 @@ int PemToDer(const unsigned char* buff, long longSz, int type,
const char* headerEnd = NULL;
const char* footerEnd = NULL;
const char* consumedEnd = NULL;
const char* bufferEnd = (const char*)(buff + longSz);
const char* bufferEnd = NULL;
long neededSz;
int ret = 0;
word32 sz = (word32)longSz;
word32 sz = 0;
int encrypted_key = 0;
DerBuffer* der;
word32 algId = 0;
Expand All @@ -24695,6 +24695,14 @@ int PemToDer(const unsigned char* buff, long longSz, int type,

WOLFSSL_ENTER("PemToDer");

/* Reject negative size - would wrap word32 and corrupt pointer arithmetic. */
if (longSz < 0) {
return BAD_FUNC_ARG;
}

bufferEnd = (const char*)(buff + longSz);
sz = (word32)longSz;

/* get PEM header and footer based on type */
ret = wc_PemGetHeaderFooter(type, &header, &footer);
if (ret != 0)
Expand Down Expand Up @@ -29958,6 +29966,9 @@ int wc_SetAuthKeyIdFromCert(Cert *cert, const byte *der, int derSz)
if (cert == NULL) {
ret = BAD_FUNC_ARG;
}
else if (derSz < 0) {
ret = BAD_FUNC_ARG;
}
else {
/* Check if decodedCert is cached */
if (cert->der != der) {
Expand Down Expand Up @@ -30462,6 +30473,9 @@ int wc_SetIssuerBuffer(Cert* cert, const byte* der, int derSz)
if (cert == NULL) {
ret = BAD_FUNC_ARG;
}
else if (derSz < 0) {
ret = BAD_FUNC_ARG;
}
else {
cert->selfSigned = 0;

Expand Down Expand Up @@ -30491,6 +30505,9 @@ int wc_SetSubjectBuffer(Cert* cert, const byte* der, int derSz)
if (cert == NULL) {
ret = BAD_FUNC_ARG;
}
else if (derSz < 0) {
ret = BAD_FUNC_ARG;
}
else {
/* Check if decodedCert is cached */
if (cert->der != der) {
Expand Down Expand Up @@ -30518,6 +30535,9 @@ int wc_SetSubjectRaw(Cert* cert, const byte* der, int derSz)
if (cert == NULL) {
ret = BAD_FUNC_ARG;
}
else if (derSz < 0) {
ret = BAD_FUNC_ARG;
}
else {
/* Check if decodedCert is cached */
if (cert->der != der) {
Expand Down Expand Up @@ -30552,6 +30572,9 @@ int wc_SetIssuerRaw(Cert* cert, const byte* der, int derSz)
if (cert == NULL) {
ret = BAD_FUNC_ARG;
}
else if (derSz < 0) {
ret = BAD_FUNC_ARG;
}
else {
/* Check if decodedCert is cached */
if (cert->der != der) {
Expand Down Expand Up @@ -30589,6 +30612,9 @@ int wc_SetAltNamesBuffer(Cert* cert, const byte* der, int derSz)
if (cert == NULL) {
ret = BAD_FUNC_ARG;
}
else if (derSz < 0) {
ret = BAD_FUNC_ARG;
}
else {
/* Check if decodedCert is cached */
if (cert->der != der) {
Expand Down Expand Up @@ -30616,6 +30642,9 @@ int wc_SetDatesBuffer(Cert* cert, const byte* der, int derSz)
if (cert == NULL) {
ret = BAD_FUNC_ARG;
}
else if (derSz < 0) {
ret = BAD_FUNC_ARG;
}
else {
/* Check if decodedCert is cached */
if (cert->der != der) {
Expand Down
12 changes: 9 additions & 3 deletions wolfcrypt/src/evp_pk.c
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,7 @@ static WOLFSSL_EVP_PKEY* d2i_evp_pkey(int type, WOLFSSL_EVP_PKEY** out,
(void)opt;

/* Validate parameters. */
if (in == NULL || inSz < 0) {
if (in == NULL || *in == NULL || inSz <= 0) {
WOLFSSL_MSG("Bad argument");
return NULL;
}
Expand Down Expand Up @@ -1464,12 +1464,18 @@ WOLFSSL_EVP_PKEY* wolfSSL_d2i_AutoPrivateKey(WOLFSSL_EVP_PKEY** pkey,
{
int ret;
WOLFSSL_EVP_PKEY* key = NULL;
const byte* der = *pp;
const byte* der;
word32 idx = 0;
int len = 0;
int cnt = 0;
word32 algId;
word32 keyLen = (word32)length;
word32 keyLen;

if (pp == NULL || *pp == NULL || length <= 0)
return NULL;

der = *pp;
keyLen = (word32)length;

/* Take off PKCS#8 wrapper if found. */
if ((len = ToTraditionalInline_ex(der, &idx, keyLen, &algId)) >= 0) {
Expand Down
Loading