]> git.baikalelectronics.ru Git - arm-tf.git/commitdiff
refactor(auth): clean up certificate length checks
authorDemi Marie Obenour <demiobenour@gmail.com>
Thu, 19 Jan 2023 14:50:16 +0000 (09:50 -0500)
committerDemi Marie Obenour <demiobenour@gmail.com>
Mon, 27 Feb 2023 19:08:28 +0000 (14:08 -0500)
The previous code was correct but unnecessarily verbose.

Change-Id: Ia19c667811a7c3b6957a0274d36076b0b16e36b7
Signed-off-by: Demi Marie Obenour <demiobenour@gmail.com>
drivers/auth/mbedtls/mbedtls_x509_parser.c

index 41024aa70d861d7e5416998814c995456a8f45ca..b538c782b1992384c4c840f2f53178adacb54feb 100644 (file)
@@ -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;
 }