Skip to content

Commit 8580ac0

Browse files
committed
Add Overflow check to DecodeAltNames input buffer access
1 parent 64ef6ae commit 8580ac0

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

wolfcrypt/src/asn.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17338,6 +17338,7 @@ static int DecodeAltNames(const byte* input, int sz, DecodedCert* cert)
1733817338
#ifndef WOLFSSL_ASN_TEMPLATE
1733917339
word32 idx = 0;
1734017340
int length = 0;
17341+
byte current_byte;
1734117342

1734217343
WOLFSSL_ENTER("DecodeAltNames");
1734317344

@@ -17362,13 +17363,20 @@ static int DecodeAltNames(const byte* input, int sz, DecodedCert* cert)
1736217363
cert->weOwnAltNames = 1;
1736317364

1736417365
while (length > 0) {
17365-
byte b = input[idx++];
17366+
17367+
/* Verify idx can't overflow input buffer */
17368+
if (idx >= (word32)sz) {
17369+
WOLFSSL_MSG("\tBad Index");
17370+
return BUFFER_E;
17371+
}
17372+
17373+
current_byte = input[idx++];
1736617374

1736717375
length--;
1736817376

1736917377
/* Save DNS Type names in the altNames list. */
1737017378
/* Save Other Type names in the cert's OidMap */
17371-
if (b == (ASN_CONTEXT_SPECIFIC | ASN_DNS_TYPE)) {
17379+
if (current_byte == (ASN_CONTEXT_SPECIFIC | ASN_DNS_TYPE)) {
1737217380
DNS_entry* dnsEntry;
1737317381
int strLen;
1737417382
word32 lenStartIdx = idx;
@@ -17403,7 +17411,7 @@ static int DecodeAltNames(const byte* input, int sz, DecodedCert* cert)
1740317411
idx += strLen;
1740417412
}
1740517413
#ifndef IGNORE_NAME_CONSTRAINTS
17406-
else if (b == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | ASN_DIR_TYPE)) {
17414+
else if (current_byte == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | ASN_DIR_TYPE)) {
1740717415
DNS_entry* dirEntry;
1740817416
int strLen;
1740917417
word32 lenStartIdx = idx;
@@ -17442,7 +17450,7 @@ static int DecodeAltNames(const byte* input, int sz, DecodedCert* cert)
1744217450
length -= strLen;
1744317451
idx += strLen;
1744417452
}
17445-
else if (b == (ASN_CONTEXT_SPECIFIC | ASN_RFC822_TYPE)) {
17453+
else if (current_byte == (ASN_CONTEXT_SPECIFIC | ASN_RFC822_TYPE)) {
1744617454
DNS_entry* emailEntry;
1744717455
int strLen;
1744817456
word32 lenStartIdx = idx;
@@ -17477,7 +17485,7 @@ static int DecodeAltNames(const byte* input, int sz, DecodedCert* cert)
1747717485
length -= strLen;
1747817486
idx += strLen;
1747917487
}
17480-
else if (b == (ASN_CONTEXT_SPECIFIC | ASN_URI_TYPE)) {
17488+
else if (current_byte == (ASN_CONTEXT_SPECIFIC | ASN_URI_TYPE)) {
1748117489
DNS_entry* uriEntry;
1748217490
int strLen;
1748317491
word32 lenStartIdx = idx;
@@ -17548,7 +17556,7 @@ static int DecodeAltNames(const byte* input, int sz, DecodedCert* cert)
1754817556
idx += strLen;
1754917557
}
1755017558
#if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
17551-
else if (b == (ASN_CONTEXT_SPECIFIC | ASN_IP_TYPE)) {
17559+
else if (current_byte == (ASN_CONTEXT_SPECIFIC | ASN_IP_TYPE)) {
1755217560
DNS_entry* ipAddr;
1755317561
int strLen;
1755417562
word32 lenStartIdx = idx;
@@ -17597,7 +17605,7 @@ static int DecodeAltNames(const byte* input, int sz, DecodedCert* cert)
1759717605
}
1759817606
#endif /* WOLFSSL_QT || OPENSSL_ALL */
1759917607
#endif /* IGNORE_NAME_CONSTRAINTS */
17600-
else if (b == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | ASN_OTHER_TYPE))
17608+
else if (current_byte == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | ASN_OTHER_TYPE))
1760117609
{
1760217610
int strLen;
1760317611
word32 lenStartIdx = idx;

0 commit comments

Comments
 (0)