Skip to content

Commit ae1da8a

Browse files
committed
Add missing NULL checks in public API functions
Add NULL and bounds validation to public API entry points that were missing basic argument checks. Fixes span ALPN, session cache, X509, SRP, PrivateKey ID/Label, and OBJ_obj2txt.
1 parent c36beba commit ae1da8a

5 files changed

Lines changed: 48 additions & 11 deletions

File tree

src/ssl.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14545,7 +14545,10 @@ void* wolfSSL_GetHKDFExtractCtx(WOLFSSL* ssl)
1454514545
else if (a->type == WOLFSSL_GEN_DNS || a->type == WOLFSSL_GEN_EMAIL ||
1454614546
a->type == WOLFSSL_GEN_URI) {
1454714547
bufSz = (int)XSTRLEN((const char*)a->obj);
14548-
XMEMCPY(buf, a->obj, min((word32)bufSz, (word32)bufLen));
14548+
if (bufSz >= bufLen) {
14549+
bufSz = bufLen - 1;
14550+
}
14551+
XMEMCPY(buf, a->obj, (size_t)bufSz);
1454914552
}
1455014553
else if ((bufSz = wolfssl_obj2txt_numeric(buf, bufLen, a)) > 0) {
1455114554
if ((desc = oid_translate_num_to_str(buf))) {
@@ -17498,7 +17501,7 @@ int wolfSSL_CTX_set_alpn_protos(WOLFSSL_CTX *ctx, const unsigned char *p,
1749817501
unsigned int p_len)
1749917502
{
1750017503
WOLFSSL_ENTER("wolfSSL_CTX_set_alpn_protos");
17501-
if (ctx == NULL)
17504+
if (ctx == NULL || p == NULL)
1750217505
return BAD_FUNC_ARG;
1750317506
if (ctx->alpn_cli_protos != NULL) {
1750417507
XFREE((void*)ctx->alpn_cli_protos, ctx->heap, DYNAMIC_TYPE_OPENSSL);
@@ -17552,7 +17555,7 @@ int wolfSSL_set_alpn_protos(WOLFSSL* ssl,
1755217555

1755317556
WOLFSSL_ENTER("wolfSSL_set_alpn_protos");
1755417557

17555-
if (ssl == NULL || p_len <= 1) {
17558+
if (ssl == NULL || p_len <= 1 || p == NULL) {
1755617559
#if defined(WOLFSSL_ERROR_CODE_OPENSSL)
1755717560
/* 0 on success in OpenSSL, non-0 on failure in OpenSSL
1755817561
* the function reverses the return value convention.

src/ssl_load.c

Lines changed: 24 additions & 4 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

@@ -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(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

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
@@ -3277,8 +3277,8 @@ WOLFSSL_X509_EXTENSION* wolfSSL_X509V3_EXT_nconf(WOLFSSL_CONF *conf,
32773277

32783278
WOLFSSL_ENTER("wolfSSL_X509V3_EXT_nconf");
32793279

3280-
if (value == NULL) {
3281-
WOLFSSL_MSG("value NULL parameter");
3280+
if (value == NULL || sName == NULL) {
3281+
WOLFSSL_MSG("NULL parameter");
32823282
return NULL;
32833283
}
32843284

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)