r19604: This is a massive commit, and I appologise in advance for it's size.
[tprouty/samba.git] / source / heimdal / lib / asn1 / der_get.c
1 /*
2  * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright 
14  *    notice, this list of conditions and the following disclaimer in the 
15  *    documentation and/or other materials provided with the distribution. 
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors 
18  *    may be used to endorse or promote products derived from this software 
19  *    without specific prior written permission. 
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
31  * SUCH DAMAGE. 
32  */
33
34 #include "der_locl.h"
35
36 RCSID("$Id: der_get.c,v 1.50 2006/10/19 16:27:44 lha Exp $");
37
38 #include <version.h>
39
40 /* 
41  * All decoding functions take a pointer `p' to first position in
42  * which to read, from the left, `len' which means the maximum number
43  * of characters we are able to read, `ret' were the value will be
44  * returned and `size' where the number of used bytes is stored.
45  * Either 0 or an error code is returned.
46  */
47
48 int
49 der_get_unsigned (const unsigned char *p, size_t len,
50                   unsigned *ret, size_t *size)
51 {
52     unsigned val = 0;
53     size_t oldlen = len;
54
55     if (len == sizeof(unsigned) + 1 && p[0] == 0)
56         ;
57     else if (len > sizeof(unsigned))
58         return ASN1_OVERRUN;
59
60     while (len--)
61         val = val * 256 + *p++;
62     *ret = val;
63     if(size) *size = oldlen;
64     return 0;
65 }
66
67 int
68 der_get_integer (const unsigned char *p, size_t len,
69                  int *ret, size_t *size)
70 {
71     int val = 0;
72     size_t oldlen = len;
73
74     if (len > sizeof(int))
75         return ASN1_OVERRUN;
76
77     if (len > 0) {
78         val = (signed char)*p++;
79         while (--len)
80             val = val * 256 + *p++;
81     }
82     *ret = val;
83     if(size) *size = oldlen;
84     return 0;
85 }
86
87 int
88 der_get_length (const unsigned char *p, size_t len,
89                 size_t *val, size_t *size)
90 {
91     size_t v;
92
93     if (len <= 0)
94         return ASN1_OVERRUN;
95     --len;
96     v = *p++;
97     if (v < 128) {
98         *val = v;
99         if(size) *size = 1;
100     } else {
101         int e;
102         size_t l;
103         unsigned tmp;
104
105         if(v == 0x80){
106             *val = ASN1_INDEFINITE;
107             if(size) *size = 1;
108             return 0;
109         }
110         v &= 0x7F;
111         if (len < v)
112             return ASN1_OVERRUN;
113         e = der_get_unsigned (p, v, &tmp, &l);
114         if(e) return e;
115         *val = tmp;
116         if(size) *size = l + 1;
117     }
118     return 0;
119 }
120
121 int
122 der_get_boolean(const unsigned char *p, size_t len, int *data, size_t *size)
123 {
124     if(len < 1)
125         return ASN1_OVERRUN;
126     if(*p != 0)
127         *data = 1;
128     else
129         *data = 0;
130     *size = 1;
131     return 0;
132 }
133
134 int
135 der_get_general_string (const unsigned char *p, size_t len, 
136                         heim_general_string *str, size_t *size)
137 {
138     char *s;
139
140     if (len > len + 1)
141         return ASN1_BAD_LENGTH;
142
143     s = malloc (len + 1);
144     if (s == NULL)
145         return ENOMEM;
146     memcpy (s, p, len);
147     s[len] = '\0';
148     *str = s;
149     if(size) *size = len;
150     return 0;
151 }
152
153 int
154 der_get_utf8string (const unsigned char *p, size_t len, 
155                     heim_utf8_string *str, size_t *size)
156 {
157     return der_get_general_string(p, len, str, size);
158 }
159
160 int
161 der_get_printable_string (const unsigned char *p, size_t len, 
162                           heim_printable_string *str, size_t *size)
163 {
164     return der_get_general_string(p, len, str, size);
165 }
166
167 int
168 der_get_ia5_string (const unsigned char *p, size_t len, 
169                     heim_ia5_string *str, size_t *size)
170 {
171     return der_get_general_string(p, len, str, size);
172 }
173
174 int
175 der_get_bmp_string (const unsigned char *p, size_t len, 
176                     heim_bmp_string *data, size_t *size)
177 {
178     size_t i;
179
180     if (len & 1)
181         return ASN1_BAD_FORMAT;
182     data->length = len / 2;
183     data->data = malloc(data->length * sizeof(data->data[0]));
184     if (data->data == NULL && data->length != 0)
185         return ENOMEM;
186
187     for (i = 0; i < data->length; i++) {
188         data->data[i] = (p[0] << 8) | p[1];
189         p += 2;
190     }
191     if (size) *size = len;
192
193     return 0;
194 }
195
196 int
197 der_get_universal_string (const unsigned char *p, size_t len, 
198                           heim_universal_string *data, size_t *size)
199 {
200     size_t i;
201
202     if (len & 3)
203         return ASN1_BAD_FORMAT;
204     data->length = len / 4;
205     data->data = malloc(data->length * sizeof(data->data[0]));
206     if (data->data == NULL && data->length != 0)
207         return ENOMEM;
208
209     for (i = 0; i < data->length; i++) {
210         data->data[i] = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
211         p += 4;
212     }
213     if (size) *size = len;
214     return 0;
215 }
216
217 int
218 der_get_octet_string (const unsigned char *p, size_t len, 
219                       heim_octet_string *data, size_t *size)
220 {
221     data->length = len;
222     data->data = malloc(len);
223     if (data->data == NULL && data->length != 0)
224         return ENOMEM;
225     memcpy (data->data, p, len);
226     if(size) *size = len;
227     return 0;
228 }
229
230 int
231 der_get_heim_integer (const unsigned char *p, size_t len, 
232                       heim_integer *data, size_t *size)
233 {
234     data->length = 0;
235     data->negative = 0;
236     data->data = NULL;
237
238     if (len == 0) {
239         if (size)
240             *size = 0;
241         return 0;
242     }
243     if (p[0] & 0x80) {
244         unsigned char *q;
245         int carry = 1;
246         data->negative = 1;
247
248         data->length = len;
249
250         if (p[0] == 0xff) {
251             p++;
252             data->length--;
253         }
254         data->data = malloc(data->length);
255         if (data->data == NULL) {
256             data->length = 0;
257             if (size)
258                 *size = 0;
259             return ENOMEM;
260         }
261         q = &((unsigned char*)data->data)[data->length - 1];
262         p += data->length - 1;
263         while (q >= (unsigned char*)data->data) {
264             *q = *p ^ 0xff;
265             if (carry)
266                 carry = !++*q;
267             p--;
268             q--;
269         }
270     } else {
271         data->negative = 0;
272         data->length = len;
273
274         if (p[0] == 0) {
275             p++;
276             data->length--;
277         }
278         data->data = malloc(data->length);
279         if (data->data == NULL && data->length != 0) {
280             data->length = 0;
281             if (size)
282                 *size = 0;
283             return ENOMEM;
284         }
285         memcpy(data->data, p, data->length);
286     }
287     if (size)
288         *size = len;
289     return 0;
290 }
291
292 static int
293 generalizedtime2time (const char *s, time_t *t)
294 {
295     struct tm tm;
296
297     memset(&tm, 0, sizeof(tm));
298     if (sscanf (s, "%04d%02d%02d%02d%02d%02dZ",
299                 &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
300                 &tm.tm_min, &tm.tm_sec) != 6) {
301         if (sscanf (s, "%02d%02d%02d%02d%02d%02dZ",
302                     &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
303                     &tm.tm_min, &tm.tm_sec) != 6)
304             return ASN1_BAD_TIMEFORMAT;
305         if (tm.tm_year < 50)
306             tm.tm_year += 2000;
307         else
308             tm.tm_year += 1900;
309     }
310     tm.tm_year -= 1900;
311     tm.tm_mon -= 1;
312     *t = _der_timegm (&tm);
313     return 0;
314 }
315 #undef timegm
316
317 static int
318 der_get_time (const unsigned char *p, size_t len, 
319               time_t *data, size_t *size)
320 {
321     heim_octet_string k;
322     char *times;
323     size_t ret = 0;
324     size_t l;
325     int e;
326
327     e = der_get_octet_string (p, len, &k, &l);
328     if (e) return e;
329     p += l;
330     len -= l;
331     ret += l;
332     times = realloc(k.data, k.length + 1);
333     if (times == NULL){
334         free(k.data);
335         return ENOMEM;
336     }
337     times[k.length] = 0;
338     e = generalizedtime2time(times, data);
339     free (times);
340     if(size) *size = ret;
341     return e;
342 }
343
344 int
345 der_get_generalized_time (const unsigned char *p, size_t len, 
346                           time_t *data, size_t *size)
347 {
348     return der_get_time(p, len, data, size);
349 }
350
351 int
352 der_get_utctime (const unsigned char *p, size_t len, 
353                           time_t *data, size_t *size)
354 {
355     return der_get_time(p, len, data, size);
356 }
357
358 int
359 der_get_oid (const unsigned char *p, size_t len,
360              heim_oid *data, size_t *size)
361 {
362     int n;
363     size_t oldlen = len;
364
365     if (len < 1)
366         return ASN1_OVERRUN;
367
368     if (len > len + 1)
369         return ASN1_BAD_LENGTH;
370
371     data->components = malloc((len + 1) * sizeof(*data->components));
372     if (data->components == NULL)
373         return ENOMEM;
374     data->components[0] = (*p) / 40;
375     data->components[1] = (*p) % 40;
376     --len;
377     ++p;
378     for (n = 2; len > 0; ++n) {
379         unsigned u = 0, u1;
380         
381         do {
382             --len;
383             u1 = u * 128 + (*p++ % 128);
384             /* check that we don't overflow the element */
385             if (u1 < u) {
386                 der_free_oid(data);
387                 return ASN1_OVERRUN;
388             }
389             u = u1;
390         } while (len > 0 && p[-1] & 0x80);
391         data->components[n] = u;
392     }
393     if (n > 2 && p[-1] & 0x80) {
394         der_free_oid (data);
395         return ASN1_OVERRUN;
396     }
397     data->length = n;
398     if (size)
399         *size = oldlen;
400     return 0;
401 }
402
403 int
404 der_get_tag (const unsigned char *p, size_t len,
405              Der_class *class, Der_type *type,
406              unsigned int *tag, size_t *size)
407 {
408     size_t ret = 0;
409     if (len < 1)
410         return ASN1_OVERRUN;
411     *class = (Der_class)(((*p) >> 6) & 0x03);
412     *type = (Der_type)(((*p) >> 5) & 0x01);
413     *tag = (*p) & 0x1f;
414     p++; len--; ret++;
415     if(*tag == 0x1f) {
416         unsigned int continuation;
417         unsigned int tag1;
418         *tag = 0;
419         do {
420             if(len < 1)
421                 return ASN1_OVERRUN;
422             continuation = *p & 128;
423             tag1 = *tag * 128 + (*p % 128);
424             /* check that we don't overflow the tag */
425             if (tag1 < *tag)
426                 return ASN1_OVERFLOW;
427             *tag = tag1;
428             p++; len--; ret++;
429         } while(continuation);
430     }
431     if(size) *size = ret;
432     return 0;
433 }
434
435 int
436 der_match_tag (const unsigned char *p, size_t len,
437                Der_class class, Der_type type,
438                unsigned int tag, size_t *size)
439 {
440     size_t l;
441     Der_class thisclass;
442     Der_type thistype;
443     unsigned int thistag;
444     int e;
445
446     e = der_get_tag (p, len, &thisclass, &thistype, &thistag, &l);
447     if (e) return e;
448     if (class != thisclass || type != thistype)
449         return ASN1_BAD_ID;
450     if(tag > thistag)
451         return ASN1_MISPLACED_FIELD;
452     if(tag < thistag)
453         return ASN1_MISSING_FIELD;
454     if(size) *size = l;
455     return 0;
456 }
457
458 int
459 der_match_tag_and_length (const unsigned char *p, size_t len,
460                           Der_class class, Der_type type, unsigned int tag,
461                           size_t *length_ret, size_t *size)
462 {
463     size_t l, ret = 0;
464     int e;
465
466     e = der_match_tag (p, len, class, type, tag, &l);
467     if (e) return e;
468     p += l;
469     len -= l;
470     ret += l;
471     e = der_get_length (p, len, length_ret, &l);
472     if (e) return e;
473     p += l;
474     len -= l;
475     ret += l;
476     if(size) *size = ret;
477     return 0;
478 }
479
480 /* 
481  * Old versions of DCE was based on a very early beta of the MIT code,
482  * which used MAVROS for ASN.1 encoding. MAVROS had the interesting
483  * feature that it encoded data in the forward direction, which has
484  * it's problems, since you have no idea how long the data will be
485  * until after you're done. MAVROS solved this by reserving one byte
486  * for length, and later, if the actual length was longer, it reverted
487  * to indefinite, BER style, lengths. The version of MAVROS used by
488  * the DCE people could apparently generate correct X.509 DER encodings, and
489  * did this by making space for the length after encoding, but
490  * unfortunately this feature wasn't used with Kerberos. 
491  */
492
493 int
494 _heim_fix_dce(size_t reallen, size_t *len)
495 {
496     if(reallen == ASN1_INDEFINITE)
497         return 1;
498     if(*len < reallen)
499         return -1;
500     *len = reallen;
501     return 0;
502 }
503
504 int
505 der_get_bit_string (const unsigned char *p, size_t len, 
506                     heim_bit_string *data, size_t *size)
507 {
508     if (len < 1)
509         return ASN1_OVERRUN;
510     if (p[0] > 7)
511         return ASN1_BAD_FORMAT;
512     if (len - 1 == 0 && p[0] != 0)
513         return ASN1_BAD_FORMAT;
514     /* check if any of the three upper bits are set
515      * any of them will cause a interger overrun */
516     if ((len - 1) >> (sizeof(len) * 8 - 3))
517         return ASN1_OVERRUN;
518     data->length = (len - 1) * 8;
519     data->data = malloc(len - 1);
520     if (data->data == NULL && (len - 1) != 0)
521         return ENOMEM;
522     memcpy (data->data, p + 1, len - 1);
523     data->length -= p[0];
524     if(size) *size = len;
525     return 0;
526 }