From 6a7104a324ce67c5585c9a75ff9e48fa15408fa5 Mon Sep 17 00:00:00 2001 From: Demi Marie Obenour Date: Thu, 19 Jan 2023 09:46:55 -0500 Subject: [PATCH] refactor(auth): remove code duplication The unique IDs are handled identically, so just use a for loop to get both of them. Change-Id: I44baaa4747ca7f314d364a79dfcbce97315f5a92 Signed-off-by: Demi Marie Obenour --- drivers/auth/mbedtls/mbedtls_x509_parser.c | 34 +++++++++------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/drivers/auth/mbedtls/mbedtls_x509_parser.c b/drivers/auth/mbedtls/mbedtls_x509_parser.c index bbabd9b99..41024aa70 100644 --- a/drivers/auth/mbedtls/mbedtls_x509_parser.c +++ b/drivers/auth/mbedtls/mbedtls_x509_parser.c @@ -288,30 +288,24 @@ static int cert_parse(void *img, unsigned int img_len) /* * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL, - */ - ret = mbedtls_asn1_get_tag(&p, end, &len, - MBEDTLS_ASN1_CONTEXT_SPECIFIC | - MBEDTLS_ASN1_CONSTRUCTED | 1); - if (ret != 0) { - if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { - return IMG_PARSER_ERR_FORMAT; - } - } else { - p += len; - } - - /* * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL, + * -- technically these contain BIT STRINGs but that is not worth + * -- validating */ - ret = mbedtls_asn1_get_tag(&p, end, &len, - MBEDTLS_ASN1_CONTEXT_SPECIFIC | - MBEDTLS_ASN1_CONSTRUCTED | 2); - if (ret != 0) { + for (int i = 1; i < 3; i++) { + ret = mbedtls_asn1_get_tag(&p, end, &len, + MBEDTLS_ASN1_CONTEXT_SPECIFIC | + MBEDTLS_ASN1_CONSTRUCTED | i); + /* + * Unique IDs are obsolete, so MBEDTLS_ERR_ASN1_UNEXPECTED_TAG + * is the common case. + */ if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) { - return IMG_PARSER_ERR_FORMAT; + if (ret != 0) { + return IMG_PARSER_ERR_FORMAT; + } + p += len; } - } else { - p += len; } /* -- 2.39.5