]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
ASN.1: Use the helper functions for recognizing tags and debug prints
authorJouni Malinen <j@w1.fi>
Sat, 13 Mar 2021 21:13:05 +0000 (23:13 +0200)
committerJouni Malinen <j@w1.fi>
Sun, 14 Mar 2021 09:37:58 +0000 (11:37 +0200)
Simplify the core ASN.1 parser implementation by using the helper
functions.

Signed-off-by: Jouni Malinen <j@w1.fi>
src/tls/asn1.c

index 970f680c1d018f0c2cda75e4281936a831527e1d..d4611edaf5a0aedb87cac9bc755fa083e5c6595a 100644 (file)
@@ -205,7 +205,11 @@ int asn1_get_next(const u8 *buf, size_t len, struct asn1_hdr *hdr)
 
        hdr->payload = pos;
 
-       return asn1_valid_der(hdr) ? 0 : -1;
+       if (!asn1_valid_der(hdr)) {
+               asn1_print_hdr(hdr, "ASN.1: Invalid DER encoding: ");
+               return -1;
+       }
+       return 0;
 }
 
 
@@ -272,12 +276,9 @@ int asn1_get_oid(const u8 *buf, size_t len, struct asn1_oid *oid,
 {
        struct asn1_hdr hdr;
 
-       if (asn1_get_next(buf, len, &hdr) < 0 || hdr.length == 0)
-               return -1;
-
-       if (hdr.class != ASN1_CLASS_UNIVERSAL || hdr.tag != ASN1_TAG_OID) {
-               wpa_printf(MSG_DEBUG, "ASN.1: Expected OID - found class %d "
-                          "tag 0x%x", hdr.class, hdr.tag);
+       if (asn1_get_next(buf, len, &hdr) < 0 || hdr.length == 0 ||
+           !asn1_is_oid(&hdr)) {
+               asn1_unexpected(&hdr, "ASN.1: Expected OID");
                return -1;
        }
 
@@ -376,13 +377,9 @@ int asn1_get_integer(const u8 *buf, size_t len, int *integer, const u8 **next)
        const u8 *pos;
        int value;
 
-       if (asn1_get_next(buf, len, &hdr) < 0 || hdr.length == 0)
-               return -1;
-
-       if (hdr.class != ASN1_CLASS_UNIVERSAL || hdr.tag != ASN1_TAG_INTEGER) {
-               wpa_printf(MSG_DEBUG,
-                          "ASN.1: Expected INTEGER - found class %d tag 0x%x",
-                          hdr.class, hdr.tag);
+       if (asn1_get_next(buf, len, &hdr) < 0 || hdr.length == 0 ||
+           !asn1_is_integer(&hdr)) {
+               asn1_unexpected(&hdr, "ASN.1: Expected INTEGER");
                return -1;
        }
 
@@ -409,12 +406,8 @@ int asn1_get_integer(const u8 *buf, size_t len, int *integer, const u8 **next)
 int asn1_get_sequence(const u8 *buf, size_t len, struct asn1_hdr *hdr,
                      const u8 **next)
 {
-       if (asn1_get_next(buf, len, hdr) < 0 ||
-           hdr->class != ASN1_CLASS_UNIVERSAL ||
-           hdr->tag != ASN1_TAG_SEQUENCE) {
-               wpa_printf(MSG_DEBUG,
-                          "ASN.1: Expected SEQUENCE - found class %d tag 0x%x",
-                          hdr->class, hdr->tag);
+       if (asn1_get_next(buf, len, hdr) < 0 || !asn1_is_sequence(hdr)) {
+               asn1_unexpected(hdr, "ASN.1: Expected SEQUENCE");
                return -1;
        }