Skip to content

Commit cf27000

Browse files
Qingling-Wujukkar
authored andcommitted
[noup] zephyr: crypto: fix coverity issue when getting public/private key
Fix INTEGER_OVERFLOW coverity issue. crypto_ec_key_get_subject_public_key/crypto_ec_key_get_ecprivate_key: tainted_data_return: Called function mbedtls_asn1_write_len, and a possible return value is known to be less than zero. overflow: The expression len is considered to have possibly overflowed. Check return value of mbedtls_asn1_write_len and mbedtls_asn1_write_tag, if less than zero, return NULL. Signed-off-by: Qingling Wu <qingling.wu@nxp.com>
1 parent 9d91a1d commit cf27000

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/crypto/crypto_mbedtls_alt.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2645,6 +2645,7 @@ struct wpabuf *crypto_ec_key_get_subject_public_key(struct crypto_ec_key *key)
26452645
/* algorithm AlgorithmIdentifier */
26462646
unsigned char *a = p;
26472647
size_t alen;
2648+
int ret;
26482649
mbedtls_asn1_get_tag(&p, end, &alen, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
26492650
p += alen;
26502651
alen = (size_t)(p - a);
@@ -2658,8 +2659,16 @@ struct wpabuf *crypto_ec_key_get_subject_public_key(struct crypto_ec_key *key)
26582659
os_memmove(p - alen, a, alen);
26592660
len += alen;
26602661
p -= alen;
2661-
len += mbedtls_asn1_write_len(&p, buf, (size_t)len);
2662-
len += mbedtls_asn1_write_tag(&p, buf, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
2662+
if ((ret = mbedtls_asn1_write_len(&p, buf, (size_t)len)) < 0)
2663+
{
2664+
return NULL;
2665+
}
2666+
len += ret;
2667+
if ((ret = mbedtls_asn1_write_tag(&p, buf, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) < 0)
2668+
{
2669+
return NULL;
2670+
}
2671+
len += ret;
26632672
}
26642673
#endif
26652674
return wpabuf_alloc_copy(p, (size_t)len);
@@ -2690,6 +2699,7 @@ struct wpabuf *crypto_ec_key_get_ecprivate_key(struct crypto_ec_key *key, bool i
26902699
unsigned char *p = priv + sizeof(priv) - privlen;
26912700
unsigned char *end = priv + sizeof(priv);
26922701
size_t len;
2702+
int ret;
26932703
/* ECPrivateKey SEQUENCE */
26942704
mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
26952705
/* version INTEGER */
@@ -2706,8 +2716,16 @@ struct wpabuf *crypto_ec_key_get_ecprivate_key(struct crypto_ec_key *key, bool i
27062716
/* write new SEQUENCE header (we know that it fits in priv[]) */
27072717
len = (size_t)(p - v);
27082718
p = v;
2709-
len += mbedtls_asn1_write_len(&p, priv, len);
2710-
len += mbedtls_asn1_write_tag(&p, priv, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
2719+
if ((ret = mbedtls_asn1_write_len(&p, priv, len)) < 0)
2720+
{
2721+
return NULL;
2722+
}
2723+
len += ret;
2724+
if ((ret = mbedtls_asn1_write_tag(&p, priv, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) < 0)
2725+
{
2726+
return NULL;
2727+
}
2728+
len += ret;
27112729
wbuf = wpabuf_alloc_copy(p, len);
27122730
}
27132731

0 commit comments

Comments
 (0)