]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
ASN.1: Add a helper for parsing INTEGER
authorJouni Malinen <jouni@codeaurora.org>
Thu, 12 Dec 2019 00:28:39 +0000 (02:28 +0200)
committerJouni Malinen <j@w1.fi>
Thu, 30 Jan 2020 10:00:21 +0000 (12:00 +0200)
Signed-off-by: Jouni Malinen <jouni@codeaurora.org>
src/tls/asn1.c
src/tls/asn1.h

index a08c2e1e3ed17d48e5a734c17bce074461741383..06bbd6f939c80280ee5ac59f565620a16a72842a 100644 (file)
@@ -270,3 +270,40 @@ int asn1_oid_equal(const struct asn1_oid *a, const struct asn1_oid *b)
 
        return 1;
 }
+
+
+int asn1_get_integer(const u8 *buf, size_t len, int *integer, const u8 **next)
+{
+       struct asn1_hdr hdr;
+       size_t left;
+       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);
+               return -1;
+       }
+
+       *next = hdr.payload + hdr.length;
+       pos = hdr.payload;
+       left = hdr.length;
+       if (left > sizeof(value)) {
+               wpa_printf(MSG_DEBUG, "ASN.1: Too large INTEGER (len %u)",
+                          hdr.length);
+               return -1;
+       }
+       value = 0;
+       while (left) {
+               value <<= 8;
+               value |= *pos++;
+               left--;
+       }
+
+       *integer = value;
+       return 0;
+}
index 6bd7df565dba44c231c1a6f74d8172107568e705..45b981260ca808c5c3af76f9e8c5c8c4b1d21f94 100644 (file)
@@ -65,6 +65,7 @@ int asn1_get_oid(const u8 *buf, size_t len, struct asn1_oid *oid,
 void asn1_oid_to_str(const struct asn1_oid *oid, char *buf, size_t len);
 unsigned long asn1_bit_string_to_long(const u8 *buf, size_t len);
 int asn1_oid_equal(const struct asn1_oid *a, const struct asn1_oid *b);
+int asn1_get_integer(const u8 *buf, size_t len, int *integer, const u8 **next);
 
 extern struct asn1_oid asn1_sha1_oid;
 extern struct asn1_oid asn1_sha256_oid;