From ddd9f6757e61ca9abe682eeb8d6dc3b16ef1044c Mon Sep 17 00:00:00 2001 From: Demi Marie Obenour Date: Thu, 19 Jan 2023 09:50:16 -0500 Subject: [PATCH] refactor(auth): clean up certificate length checks The previous code was correct but unnecessarily verbose. Change-Id: Ia19c667811a7c3b6957a0274d36076b0b16e36b7 Signed-off-by: Demi Marie Obenour --- drivers/auth/mbedtls/mbedtls_x509_parser.c | 24 ++++++---------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/drivers/auth/mbedtls/mbedtls_x509_parser.c b/drivers/auth/mbedtls/mbedtls_x509_parser.c index 41024aa70..b538c782b 100644 --- a/drivers/auth/mbedtls/mbedtls_x509_parser.c +++ b/drivers/auth/mbedtls/mbedtls_x509_parser.c @@ -161,7 +161,8 @@ static int cert_parse(void *img, unsigned int img_len) p = (unsigned char *)img; len = img_len; - end = p + len; + crt_end = p + len; + end = crt_end; /* * Certificate ::= SEQUENCE { @@ -171,15 +172,10 @@ static int cert_parse(void *img, unsigned int img_len) */ ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE); - if (ret != 0) { + if ((ret != 0) || ((p + len) != end)) { return IMG_PARSER_ERR_FORMAT; } - if (len != (size_t)(end - p)) { - return IMG_PARSER_ERR_FORMAT; - } - crt_end = p + len; - /* * TBSCertificate ::= SEQUENCE { */ @@ -220,9 +216,6 @@ static int cert_parse(void *img, unsigned int img_len) if (ret != 0) { return IMG_PARSER_ERR_FORMAT; } - if ((end - p) < 1) { - return IMG_PARSER_ERR_FORMAT; - } sig_alg1.len = (p + len) - sig_alg1.p; p += len; @@ -408,19 +401,14 @@ static int cert_parse(void *img, unsigned int img_len) /* * signatureValue BIT STRING + * } -- must consume all bytes */ signature.p = p; ret = mbedtls_asn1_get_bitstring_null(&p, end, &len); - if (ret != 0) { - return IMG_PARSER_ERR_FORMAT; - } - signature.len = (p + len) - signature.p; - p += len; - - /* Check certificate length */ - if (p != end) { + if ((ret != 0) || ((p + len) != end)) { return IMG_PARSER_ERR_FORMAT; } + signature.len = end - signature.p; return IMG_PARSER_OK; } -- 2.39.5