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));
35 wpa_printf(MSG_DEBUG, "ASN.1: No room for Identifier");
38 hdr->identifier = *pos++;
39 hdr->class = hdr->identifier >> 6;
40 hdr->constructed = !!(hdr->identifier & (1 << 5));
42 if ((hdr->identifier & 0x1f) == 0x1f) {
46 wpa_printf(MSG_DEBUG, "ASN.1: Identifier "
51 wpa_printf(MSG_MSGDUMP, "ASN.1: Extended tag data: "
53 hdr->tag = (hdr->tag << 7) | (tmp & 0x7f);
56 hdr->tag = hdr->identifier & 0x1f;
59 wpa_printf(MSG_DEBUG, "ASN.1: No room for Length");
65 wpa_printf(MSG_DEBUG, "ASN.1: Reserved length "
69 tmp &= 0x7f; /* number of subsequent octets */
72 wpa_printf(MSG_DEBUG, "ASN.1: Too long length field");
77 wpa_printf(MSG_DEBUG, "ASN.1: Length "
81 hdr->length = (hdr->length << 8) | *pos++;
84 /* Short form - length 0..127 in one octet */
88 if (end < pos || hdr->length > (unsigned int) (end - pos)) {
89 wpa_printf(MSG_DEBUG, "ASN.1: Contents underflow");
98 int asn1_parse_oid(const u8 *buf, size_t len, struct asn1_oid *oid)
104 os_memset(oid, 0, sizeof(*oid));
116 val = (val << 7) | (tmp & 0x7f);
117 } while (tmp & 0x80);
119 if (oid->len >= ASN1_MAX_OID_LEN) {
120 wpa_printf(MSG_DEBUG, "ASN.1: Too long OID value");
125 * The first octet encodes the first two object
126 * identifier components in (X*40) + Y formula.
129 oid->oid[0] = val / 40;
132 oid->oid[1] = val - oid->oid[0] * 40;
135 oid->oid[oid->len++] = val;
142 int asn1_get_oid(const u8 *buf, size_t len, struct asn1_oid *oid,
147 if (asn1_get_next(buf, len, &hdr) < 0 || hdr.length == 0)
150 if (hdr.class != ASN1_CLASS_UNIVERSAL || hdr.tag != ASN1_TAG_OID) {
151 wpa_printf(MSG_DEBUG, "ASN.1: Expected OID - found class %d "
152 "tag 0x%x", hdr.class, hdr.tag);
156 *next = hdr.payload + hdr.length;
158 return asn1_parse_oid(hdr.payload, hdr.length, oid);
162 void asn1_oid_to_str(const struct asn1_oid *oid, char *buf, size_t len)
173 for (i = 0; i < oid->len; i++) {
174 ret = os_snprintf(pos, buf + len - pos,
176 i == 0 ? "" : ".", oid->oid[i]);
177 if (os_snprintf_error(buf + len - pos, ret))
185 static u8 rotate_bits(u8 octet)
191 for (i = 0; i < 8; i++) {
202 unsigned long asn1_bit_string_to_long(const u8 *buf, size_t len)
204 unsigned long val = 0;
207 /* BER requires that unused bits are zero, so we can ignore the number
212 val |= rotate_bits(*pos++);
214 val |= ((unsigned long) rotate_bits(*pos++)) << 8;
216 val |= ((unsigned long) rotate_bits(*pos++)) << 16;
218 val |= ((unsigned long) rotate_bits(*pos++)) << 24;
220 wpa_printf(MSG_DEBUG, "X509: %s - some bits ignored "
221 "(BIT STRING length %lu)",
222 __func__, (unsigned long) len);
228 int asn1_oid_equal(const struct asn1_oid *a, const struct asn1_oid *b)
232 if (a->len != b->len)
235 for (i = 0; i < a->len; i++) {
236 if (a->oid[i] != b->oid[i])