Skip to content

Commit 2192140

Browse files
authored
Merge pull request #10216 from ColtonWilley/add-null-checks-public-api
Add missing NULL checks in public API functions
2 parents 734a711 + c3e5f19 commit 2192140

6 files changed

Lines changed: 55 additions & 14 deletions

File tree

src/ssl.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14575,8 +14575,14 @@ void* wolfSSL_GetHKDFExtractCtx(WOLFSSL* ssl)
1457514575
}
1457614576
else if (a->type == WOLFSSL_GEN_DNS || a->type == WOLFSSL_GEN_EMAIL ||
1457714577
a->type == WOLFSSL_GEN_URI) {
14578-
bufSz = (int)XSTRLEN((const char*)a->obj);
14579-
XMEMCPY(buf, a->obj, min((word32)bufSz, (word32)bufLen));
14578+
size_t objLen = XSTRLEN((const char*)a->obj);
14579+
if (objLen >= (size_t)bufLen) {
14580+
bufSz = bufLen - 1;
14581+
}
14582+
else {
14583+
bufSz = (int)objLen;
14584+
}
14585+
XMEMCPY(buf, a->obj, (size_t)bufSz);
1458014586
}
1458114587
else if ((bufSz = wolfssl_obj2txt_numeric(buf, bufLen, a)) > 0) {
1458214588
if ((desc = oid_translate_num_to_str(buf))) {
@@ -17529,7 +17535,7 @@ int wolfSSL_CTX_set_alpn_protos(WOLFSSL_CTX *ctx, const unsigned char *p,
1752917535
unsigned int p_len)
1753017536
{
1753117537
WOLFSSL_ENTER("wolfSSL_CTX_set_alpn_protos");
17532-
if (ctx == NULL)
17538+
if (ctx == NULL || p == NULL)
1753317539
return BAD_FUNC_ARG;
1753417540
if (ctx->alpn_cli_protos != NULL) {
1753517541
XFREE((void*)ctx->alpn_cli_protos, ctx->heap, DYNAMIC_TYPE_OPENSSL);
@@ -17583,7 +17589,7 @@ int wolfSSL_set_alpn_protos(WOLFSSL* ssl,
1758317589

1758417590
WOLFSSL_ENTER("wolfSSL_set_alpn_protos");
1758517591

17586-
if (ssl == NULL || p_len <= 1) {
17592+
if (ssl == NULL || p_len <= 1 || p == NULL) {
1758717593
#if defined(WOLFSSL_ERROR_CODE_OPENSSL)
1758817594
/* 0 on success in OpenSSL, non-0 on failure in OpenSSL
1758917595
* the function reverses the return value convention.

src/ssl_load.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4159,6 +4159,10 @@ int wolfSSL_CTX_use_PrivateKey_Id(WOLFSSL_CTX* ctx, const unsigned char* id,
41594159

41604160
WOLFSSL_ENTER("wolfSSL_CTX_use_PrivateKey_Id");
41614161

4162+
if (ctx == NULL || id == NULL || sz < 0) {
4163+
return 0;
4164+
}
4165+
41624166
/* Dispose of old private key and allocate and copy in id. */
41634167
FreeDer(&ctx->privateKey);
41644168
if (AllocCopyDer(&ctx->privateKey, id, (word32)sz, PRIVATEKEY_TYPE,
@@ -4227,10 +4231,16 @@ int wolfSSL_CTX_use_PrivateKey_Label(WOLFSSL_CTX* ctx, const char* label,
42274231
int devId)
42284232
{
42294233
int ret = 1;
4230-
word32 sz = (word32)XSTRLEN(label) + 1;
4234+
word32 sz;
42314235

42324236
WOLFSSL_ENTER("wolfSSL_CTX_use_PrivateKey_Label");
42334237

4238+
if (ctx == NULL || label == NULL) {
4239+
return 0;
4240+
}
4241+
4242+
sz = (word32)XSTRLEN(label) + 1;
4243+
42344244
/* Dispose of old private key and allocate and copy in label. */
42354245
FreeDer(&ctx->privateKey);
42364246
if (AllocCopyDer(&ctx->privateKey, (const byte*)label, (word32)sz,
@@ -4268,7 +4278,7 @@ int wolfSSL_CTX_use_AltPrivateKey_Id(WOLFSSL_CTX* ctx, const unsigned char* id,
42684278

42694279
WOLFSSL_ENTER("wolfSSL_CTX_use_AltPrivateKey_Id");
42704280

4271-
if ((ctx == NULL) || (id == NULL)) {
4281+
if ((ctx == NULL) || (id == NULL) || (sz < 0)) {
42724282
ret = 0;
42734283
}
42744284

@@ -4280,7 +4290,7 @@ int wolfSSL_CTX_use_AltPrivateKey_Id(WOLFSSL_CTX* ctx, const unsigned char* id,
42804290
}
42814291
}
42824292
if (ret == 1) {
4283-
XMEMCPY(ctx->altPrivateKey->buffer, id, sz);
4293+
XMEMCPY(ctx->altPrivateKey->buffer, id, (word32)sz);
42844294
ctx->altPrivateKeyId = 1;
42854295
if (devId != INVALID_DEVID) {
42864296
ctx->altPrivateKeyDevId = devId;
@@ -4561,6 +4571,10 @@ int wolfSSL_use_PrivateKey_Id(WOLFSSL* ssl, const unsigned char* id,
45614571
{
45624572
int ret = 1;
45634573

4574+
if (ssl == NULL || id == NULL || sz < 0) {
4575+
return 0;
4576+
}
4577+
45644578
/* Dispose of old private key if owned and allocate and copy in id. */
45654579
if (ssl->buffers.weOwnKey) {
45664580
FreeDer(&ssl->buffers.key);
@@ -4629,7 +4643,13 @@ int wolfSSL_use_PrivateKey_Id_ex(WOLFSSL* ssl, const unsigned char* id,
46294643
int wolfSSL_use_PrivateKey_Label(WOLFSSL* ssl, const char* label, int devId)
46304644
{
46314645
int ret = 1;
4632-
word32 sz = (word32)XSTRLEN(label) + 1;
4646+
word32 sz;
4647+
4648+
if (ssl == NULL || label == NULL) {
4649+
return 0;
4650+
}
4651+
4652+
sz = (word32)XSTRLEN(label) + 1;
46334653

46344654
/* Dispose of old private key if owned and allocate and copy in label. */
46354655
if (ssl->buffers.weOwnKey) {
@@ -4672,7 +4692,7 @@ int wolfSSL_use_AltPrivateKey_Id(WOLFSSL* ssl, const unsigned char* id, long sz,
46724692
{
46734693
int ret = 1;
46744694

4675-
if ((ssl == NULL) || (id == NULL)) {
4695+
if ((ssl == NULL) || (id == NULL) || (sz < 0)) {
46764696
ret = 0;
46774697
}
46784698

@@ -4689,7 +4709,7 @@ int wolfSSL_use_AltPrivateKey_Id(WOLFSSL* ssl, const unsigned char* id, long sz,
46894709
}
46904710
}
46914711
if (ret == 1) {
4692-
XMEMCPY(ssl->buffers.altKey->buffer, id, sz);
4712+
XMEMCPY(ssl->buffers.altKey->buffer, id, (word32)sz);
46934713
ssl->buffers.weOwnAltKey = 1;
46944714
ssl->buffers.altKeyId = 1;
46954715
if (devId != INVALID_DEVID) {

src/ssl_sess.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,10 +430,16 @@ int wolfSSL_memsave_session_cache(void* mem, int sz)
430430
{
431431
int i;
432432
cache_header_t cache_header;
433-
SessionRow* row = (SessionRow*)((byte*)mem + sizeof(cache_header));
433+
SessionRow* row;
434434

435435
WOLFSSL_ENTER("wolfSSL_memsave_session_cache");
436436

437+
if (mem == NULL) {
438+
return BAD_FUNC_ARG;
439+
}
440+
441+
row = (SessionRow*)((byte*)mem + sizeof(cache_header));
442+
437443
if (sz < wolfSSL_get_session_cache_memsize()) {
438444
WOLFSSL_MSG("Memory buffer too small");
439445
return BUFFER_E;
@@ -520,10 +526,16 @@ int wolfSSL_memrestore_session_cache(const void* mem, int sz)
520526
{
521527
int i;
522528
cache_header_t cache_header;
523-
SessionRow* row = (SessionRow*)((byte*)mem + sizeof(cache_header));
529+
SessionRow* row;
524530

525531
WOLFSSL_ENTER("wolfSSL_memrestore_session_cache");
526532

533+
if (mem == NULL) {
534+
return BAD_FUNC_ARG;
535+
}
536+
537+
row = (SessionRow*)((byte*)mem + sizeof(cache_header));
538+
527539
if (sz < wolfSSL_get_session_cache_memsize()) {
528540
WOLFSSL_MSG("Memory buffer too small");
529541
return BUFFER_E;

src/x509.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3279,8 +3279,8 @@ WOLFSSL_X509_EXTENSION* wolfSSL_X509V3_EXT_nconf(WOLFSSL_CONF *conf,
32793279

32803280
WOLFSSL_ENTER("wolfSSL_X509V3_EXT_nconf");
32813281

3282-
if (value == NULL) {
3283-
WOLFSSL_MSG("value NULL parameter");
3282+
if (value == NULL || sName == NULL) {
3283+
WOLFSSL_MSG("NULL parameter");
32843284
return NULL;
32853285
}
32863286

tests/api/test_ossl_x509_ext.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,7 @@ int test_wolfSSL_X509V3_EXT_nconf(void)
10201020
ExpectNull(X509V3_EXT_nconf(NULL, NULL, ext_names[0], NULL));
10211021
ExpectNull(X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[0], NULL));
10221022
ExpectNull(X509V3_EXT_nconf(NULL, NULL, "", ext_values[0]));
1023+
ExpectNull(X509V3_EXT_nconf(NULL, NULL, NULL, ext_values[0]));
10231024
ExpectNull(X509V3_EXT_nconf_nid(NULL, NULL, 0, ext_values[0]));
10241025

10251026
/* conf and ctx ignored. */

wolfcrypt/src/srp.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,8 @@ int wc_SrpSetParams(Srp* srp, const byte* N, word32 nSz,
378378
if (srp->salt) {
379379
ForceZero(srp->salt, srp->saltSz);
380380
XFREE(srp->salt, srp->heap, DYNAMIC_TYPE_SRP);
381+
srp->salt = NULL;
382+
srp->saltSz = 0;
381383
}
382384

383385
srp->salt = (byte*)XMALLOC(saltSz, srp->heap, DYNAMIC_TYPE_SRP);

0 commit comments

Comments
 (0)