X.509: Add parsing of alternative name to internal TLS implementation
[wpasupplicant] / src / tls / asn1.c
1 /*
2  * ASN.1 DER parsing
3  * Copyright (c) 2006, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18
19 #ifdef CONFIG_INTERNAL_X509
20
21 #include "asn1.h"
22
23 int asn1_get_next(const u8 *buf, size_t len, struct asn1_hdr *hdr)
24 {
25         const u8 *pos, *end;
26         u8 tmp;
27
28         os_memset(hdr, 0, sizeof(*hdr));
29         pos = buf;
30         end = buf + len;
31
32         hdr->identifier = *pos++;
33         hdr->class = hdr->identifier >> 6;
34         hdr->constructed = !!(hdr->identifier & (1 << 5));
35
36         if ((hdr->identifier & 0x1f) == 0x1f) {
37                 hdr->tag = 0;
38                 do {
39                         if (pos >= end) {
40                                 wpa_printf(MSG_DEBUG, "ASN.1: Identifier "
41                                            "underflow");
42                                 return -1;
43                         }
44                         tmp = *pos++;
45                         wpa_printf(MSG_MSGDUMP, "ASN.1: Extended tag data: "
46                                    "0x%02x", tmp);
47                         hdr->tag = (hdr->tag << 7) | (tmp & 0x7f);
48                 } while (tmp & 0x80);
49         } else
50                 hdr->tag = hdr->identifier & 0x1f;
51
52         tmp = *pos++;
53         if (tmp & 0x80) {
54                 if (tmp == 0xff) {
55                         wpa_printf(MSG_DEBUG, "ASN.1: Reserved length "
56                                    "value 0xff used");
57                         return -1;
58                 }
59                 tmp &= 0x7f; /* number of subsequent octets */
60                 hdr->length = 0;
61                 if (tmp > 4) {
62                         wpa_printf(MSG_DEBUG, "ASN.1: Too long length field");
63                         return -1;
64                 }
65                 while (tmp--) {
66                         if (pos >= end) {
67                                 wpa_printf(MSG_DEBUG, "ASN.1: Length "
68                                            "underflow");
69                                 return -1;
70                         }
71                         hdr->length = (hdr->length << 8) | *pos++;
72                 }
73         } else {
74                 /* Short form - length 0..127 in one octet */
75                 hdr->length = tmp;
76         }
77
78         if (end < pos || hdr->length > (unsigned int) (end - pos)) {
79                 wpa_printf(MSG_DEBUG, "ASN.1: Contents underflow");
80                 return -1;
81         }
82
83         hdr->payload = pos;
84         return 0;
85 }
86
87
88 int asn1_parse_oid(const u8 *buf, size_t len, struct asn1_oid *oid)
89 {
90         const u8 *pos, *end;
91         unsigned long val;
92         u8 tmp;
93
94         os_memset(oid, 0, sizeof(*oid));
95
96         pos = buf;
97         end = buf + len;
98
99         while (pos < end) {
100                 val = 0;
101
102                 do {
103                         if (pos >= end)
104                                 return -1;
105                         tmp = *pos++;
106                         val = (val << 7) | (tmp & 0x7f);
107                 } while (tmp & 0x80);
108
109                 if (oid->len >= ASN1_MAX_OID_LEN) {
110                         wpa_printf(MSG_DEBUG, "ASN.1: Too long OID value");
111                         return -1;
112                 }
113                 if (oid->len == 0) {
114                         /*
115                          * The first octet encodes the first two object
116                          * identifier components in (X*40) + Y formula.
117                          * X = 0..2.
118                          */
119                         oid->oid[0] = val / 40;
120                         if (oid->oid[0] > 2)
121                                 oid->oid[0] = 2;
122                         oid->oid[1] = val - oid->oid[0] * 40;
123                         oid->len = 2;
124                 } else
125                         oid->oid[oid->len++] = val;
126         }
127
128         return 0;
129 }
130
131
132 int asn1_get_oid(const u8 *buf, size_t len, struct asn1_oid *oid,
133                  const u8 **next)
134 {
135         struct asn1_hdr hdr;
136
137         if (asn1_get_next(buf, len, &hdr) < 0 || hdr.length == 0)
138                 return -1;
139
140         if (hdr.class != ASN1_CLASS_UNIVERSAL || hdr.tag != ASN1_TAG_OID) {
141                 wpa_printf(MSG_DEBUG, "ASN.1: Expected OID - found class %d "
142                            "tag 0x%x", hdr.class, hdr.tag);
143                 return -1;
144         }
145
146         *next = hdr.payload + hdr.length;
147
148         return asn1_parse_oid(hdr.payload, hdr.length, oid);
149 }
150
151
152 void asn1_oid_to_str(struct asn1_oid *oid, char *buf, size_t len)
153 {
154         char *pos = buf;
155         size_t i;
156         int ret;
157
158         if (len == 0)
159                 return;
160
161         buf[0] = '\0';
162
163         for (i = 0; i < oid->len; i++) {
164                 ret = os_snprintf(pos, buf + len - pos,
165                                   "%s%lu",
166                                   i == 0 ? "" : ".", oid->oid[i]);
167                 if (ret < 0 || ret >= buf + len - pos)
168                         break;
169                 pos += ret;
170         }
171         buf[len - 1] = '\0';
172 }
173
174
175 static u8 rotate_bits(u8 octet)
176 {
177         int i;
178         u8 res;
179
180         res = 0;
181         for (i = 0; i < 8; i++) {
182                 res <<= 1;
183                 if (octet & 1)
184                         res |= 1;
185                 octet >>= 1;
186         }
187
188         return res;
189 }
190
191
192 unsigned long asn1_bit_string_to_long(const u8 *buf, size_t len)
193 {
194         unsigned long val = 0;
195         const u8 *pos = buf;
196
197         /* BER requires that unused bits are zero, so we can ignore the number
198          * of unused bits */
199         pos++;
200
201         if (len >= 2)
202                 val |= rotate_bits(*pos++);
203         if (len >= 3)
204                 val |= ((unsigned long) rotate_bits(*pos++)) << 8;
205         if (len >= 4)
206                 val |= ((unsigned long) rotate_bits(*pos++)) << 16;
207         if (len >= 5)
208                 val |= ((unsigned long) rotate_bits(*pos++)) << 24;
209         if (len >= 6)
210                 wpa_printf(MSG_DEBUG, "X509: %s - some bits ignored "
211                            "(BIT STRING length %lu)",
212                            __func__, (unsigned long) len);
213
214         return val;
215 }
216
217 #endif /* CONFIG_INTERNAL_X509 */