3 * Copyright (c) 2006-2014, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
14 struct asn1_oid asn1_sha1_oid = {
15 .oid = { 1, 3, 14, 3, 2, 26 },
19 struct asn1_oid asn1_sha256_oid = {
20 .oid = { 2, 16, 840, 1, 101, 3, 4, 2, 1 },
25 int asn1_get_next(const u8 *buf, size_t len, struct asn1_hdr *hdr)
30 os_memset(hdr, 0, sizeof(*hdr));
34 hdr->identifier = *pos++;
35 hdr->class = hdr->identifier >> 6;
36 hdr->constructed = !!(hdr->identifier & (1 << 5));
38 if ((hdr->identifier & 0x1f) == 0x1f) {
42 wpa_printf(MSG_DEBUG, "ASN.1: Identifier "
47 wpa_printf(MSG_MSGDUMP, "ASN.1: Extended tag data: "
49 hdr->tag = (hdr->tag << 7) | (tmp & 0x7f);
52 hdr->tag = hdr->identifier & 0x1f;
57 wpa_printf(MSG_DEBUG, "ASN.1: Reserved length "
61 tmp &= 0x7f; /* number of subsequent octets */
64 wpa_printf(MSG_DEBUG, "ASN.1: Too long length field");
69 wpa_printf(MSG_DEBUG, "ASN.1: Length "
73 hdr->length = (hdr->length << 8) | *pos++;
76 /* Short form - length 0..127 in one octet */
80 if (end < pos || hdr->length > (unsigned int) (end - pos)) {
81 wpa_printf(MSG_DEBUG, "ASN.1: Contents underflow");
90 int asn1_parse_oid(const u8 *buf, size_t len, struct asn1_oid *oid)
96 os_memset(oid, 0, sizeof(*oid));
108 val = (val << 7) | (tmp & 0x7f);
109 } while (tmp & 0x80);
111 if (oid->len >= ASN1_MAX_OID_LEN) {
112 wpa_printf(MSG_DEBUG, "ASN.1: Too long OID value");
117 * The first octet encodes the first two object
118 * identifier components in (X*40) + Y formula.
121 oid->oid[0] = val / 40;
124 oid->oid[1] = val - oid->oid[0] * 40;
127 oid->oid[oid->len++] = val;
134 int asn1_get_oid(const u8 *buf, size_t len, struct asn1_oid *oid,
139 if (asn1_get_next(buf, len, &hdr) < 0 || hdr.length == 0)
142 if (hdr.class != ASN1_CLASS_UNIVERSAL || hdr.tag != ASN1_TAG_OID) {
143 wpa_printf(MSG_DEBUG, "ASN.1: Expected OID - found class %d "
144 "tag 0x%x", hdr.class, hdr.tag);
148 *next = hdr.payload + hdr.length;
150 return asn1_parse_oid(hdr.payload, hdr.length, oid);
154 void asn1_oid_to_str(const struct asn1_oid *oid, char *buf, size_t len)
165 for (i = 0; i < oid->len; i++) {
166 ret = os_snprintf(pos, buf + len - pos,
168 i == 0 ? "" : ".", oid->oid[i]);
169 if (os_snprintf_error(buf + len - pos, ret))
177 static u8 rotate_bits(u8 octet)
183 for (i = 0; i < 8; i++) {
194 unsigned long asn1_bit_string_to_long(const u8 *buf, size_t len)
196 unsigned long val = 0;
199 /* BER requires that unused bits are zero, so we can ignore the number
204 val |= rotate_bits(*pos++);
206 val |= ((unsigned long) rotate_bits(*pos++)) << 8;
208 val |= ((unsigned long) rotate_bits(*pos++)) << 16;
210 val |= ((unsigned long) rotate_bits(*pos++)) << 24;
212 wpa_printf(MSG_DEBUG, "X509: %s - some bits ignored "
213 "(BIT STRING length %lu)",
214 __func__, (unsigned long) len);
220 int asn1_oid_equal(const struct asn1_oid *a, const struct asn1_oid *b)
224 if (a->len != b->len)
227 for (i = 0; i < a->len; i++) {
228 if (a->oid[i] != b->oid[i])