s4:heimdal: import lorikeet-heimdal-200906080040 (commit 904d0124b46eed7a8ad6e5b73e89...
[metze/samba/wip.git] / source4 / heimdal / lib / asn1 / der_get.c
1 /*
2  * Copyright (c) 1997 - 2007 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 /*
37  * All decoding functions take a pointer `p' to first position in
38  * which to read, from the left, `len' which means the maximum number
39  * of characters we are able to read, `ret' were the value will be
40  * returned and `size' where the number of used bytes is stored.
41  * Either 0 or an error code is returned.
42  */
43
44 int
45 der_get_unsigned (const unsigned char *p, size_t len,
46                   unsigned *ret, size_t *size)
47 {
48     unsigned val = 0;
49     size_t oldlen = len;
50
51     if (len == sizeof(unsigned) + 1 && p[0] == 0)
52         ;
53     else if (len > sizeof(unsigned))
54         return ASN1_OVERRUN;
55
56     while (len--)
57         val = val * 256 + *p++;
58     *ret = val;
59     if(size) *size = oldlen;
60     return 0;
61 }
62
63 int
64 der_get_integer (const unsigned char *p, size_t len,
65                  int *ret, size_t *size)
66 {
67     int val = 0;
68     size_t oldlen = len;
69
70     if (len > sizeof(int))
71         return ASN1_OVERRUN;
72
73     if (len > 0) {
74         val = (signed char)*p++;
75         while (--len)
76             val = val * 256 + *p++;
77     }
78     *ret = val;
79     if(size) *size = oldlen;
80     return 0;
81 }
82
83 int
84 der_get_length (const unsigned char *p, size_t len,
85                 size_t *val, size_t *size)
86 {
87     size_t v;
88
89     if (len <= 0)
90         return ASN1_OVERRUN;
91     --len;
92     v = *p++;
93     if (v < 128) {
94         *val = v;
95         if(size) *size = 1;
96     } else {
97         int e;
98         size_t l;
99         unsigned tmp;
100
101         if(v == 0x80){
102             *val = ASN1_INDEFINITE;
103             if(size) *size = 1;
104             return 0;
105         }
106         v &= 0x7F;
107         if (len < v)
108             return ASN1_OVERRUN;
109         e = der_get_unsigned (p, v, &tmp, &l);
110         if(e) return e;
111         *val = tmp;
112         if(size) *size = l + 1;
113     }
114     return 0;
115 }
116
117 int
118 der_get_boolean(const unsigned char *p, size_t len, int *data, size_t *size)
119 {
120     if(len < 1)
121         return ASN1_OVERRUN;
122     if(*p != 0)
123         *data = 1;
124     else
125         *data = 0;
126     *size = 1;
127     return 0;
128 }
129
130 int
131 der_get_general_string (const unsigned char *p, size_t len,
132                         heim_general_string *str, size_t *size)
133 {
134     const unsigned char *p1;
135     char *s;
136
137     p1 = memchr(p, 0, len);
138     if (p1 != NULL) {
139         /*
140          * Allow trailing NULs. We allow this since MIT Kerberos sends
141          * an strings in the NEED_PREAUTH case that includes a
142          * trailing NUL.
143          */
144         while (p1 - p < len && *p1 == '\0')
145             p1++;
146        if (p1 - p != len)
147             return ASN1_BAD_CHARACTER;
148     }
149     if (len > len + 1)
150         return ASN1_BAD_LENGTH;
151
152     s = malloc (len + 1);
153     if (s == NULL)
154         return ENOMEM;
155     memcpy (s, p, len);
156     s[len] = '\0';
157     *str = s;
158     if(size) *size = len;
159     return 0;
160 }
161
162 int
163 der_get_utf8string (const unsigned char *p, size_t len,
164                     heim_utf8_string *str, size_t *size)
165 {
166     return der_get_general_string(p, len, str, size);
167 }
168
169 int
170 der_get_printable_string (const unsigned char *p, size_t len,
171                           heim_printable_string *str, size_t *size)
172 {
173     return der_get_general_string(p, len, str, size);
174 }
175
176 int
177 der_get_ia5_string (const unsigned char *p, size_t len,
178                     heim_ia5_string *str, size_t *size)
179 {
180     return der_get_general_string(p, len, str, size);
181 }
182
183 int
184 der_get_bmp_string (const unsigned char *p, size_t len,
185                     heim_bmp_string *data, size_t *size)
186 {
187     size_t i;
188
189     if (len & 1)
190         return ASN1_BAD_FORMAT;
191     data->length = len / 2;
192     if (data->length > UINT_MAX/sizeof(data->data[0]))
193         return ERANGE;
194     data->data = malloc(data->length * sizeof(data->data[0]));
195     if (data->data == NULL && data->length != 0)
196         return ENOMEM;
197
198     for (i = 0; i < data->length; i++) {
199         data->data[i] = (p[0] << 8) | p[1];
200         p += 2;
201     }
202     if (size) *size = len;
203
204     return 0;
205 }
206
207 int
208 der_get_universal_string (const unsigned char *p, size_t len,
209                           heim_universal_string *data, size_t *size)
210 {
211     size_t i;
212
213     if (len & 3)
214         return ASN1_BAD_FORMAT;
215     data->length = len / 4;
216     if (data->length > UINT_MAX/sizeof(data->data[0]))
217         return ERANGE;
218     data->data = malloc(data->length * sizeof(data->data[0]));
219     if (data->data == NULL && data->length != 0)
220         return ENOMEM;
221
222     for (i = 0; i < data->length; i++) {
223         data->data[i] = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
224         p += 4;
225     }
226     if (size) *size = len;
227     return 0;
228 }
229
230 int
231 der_get_visible_string (const unsigned char *p, size_t len,
232                         heim_visible_string *str, size_t *size)
233 {
234     return der_get_general_string(p, len, str, size);
235 }
236
237 int
238 der_get_octet_string (const unsigned char *p, size_t len,
239                       heim_octet_string *data, size_t *size)
240 {
241     data->length = len;
242     data->data = malloc(len);
243     if (data->data == NULL && data->length != 0)
244         return ENOMEM;
245     memcpy (data->data, p, len);
246     if(size) *size = len;
247     return 0;
248 }
249
250 int
251 der_get_octet_string_ber (const unsigned char *p, size_t len,
252                           heim_octet_string *data, size_t *size)
253 {
254     int e;
255     Der_type type;
256     Der_class class;
257     unsigned int tag, depth = 0;
258     size_t l, datalen, oldlen = len;
259
260     data->length = 0;
261     data->data = NULL;
262
263     while (len) {
264         e = der_get_tag (p, len, &class, &type, &tag, &l);
265         if (e) goto out;
266         if (class != ASN1_C_UNIV) {
267             e = ASN1_BAD_ID;
268             goto out;
269         }
270         if (type == PRIM && tag == UT_EndOfContent) {
271             if (depth == 0)
272                 break;
273             depth--;
274         }
275         if (tag != UT_OctetString) {
276             e = ASN1_BAD_ID;
277             goto out;
278         }
279
280         p += l;
281         len -= l;
282         e = der_get_length (p, len, &datalen, &l);
283         if (e) goto out;
284         p += l;
285         len -= l;
286
287         if (datalen > len)
288             return ASN1_OVERRUN;
289
290         if (type == PRIM) {
291             void *ptr;
292
293             ptr = realloc(data->data, data->length + datalen);
294             if (ptr == NULL) {
295                 e = ENOMEM;
296                 goto out;
297             }
298             data->data = ptr;
299             memcpy(((unsigned char *)data->data) + data->length, p, datalen);
300             data->length += datalen;
301         } else
302             depth++;
303
304         p += datalen;
305         len -= datalen;
306     }
307     if (depth != 0)
308         return ASN1_INDEF_OVERRUN;
309     if(size) *size = oldlen - len;
310     return 0;
311  out:
312     free(data->data);
313     data->data = NULL;
314     data->length = 0;
315     return e;
316 }
317
318
319 int
320 der_get_heim_integer (const unsigned char *p, size_t len,
321                       heim_integer *data, size_t *size)
322 {
323     data->length = 0;
324     data->negative = 0;
325     data->data = NULL;
326
327     if (len == 0) {
328         if (size)
329             *size = 0;
330         return 0;
331     }
332     if (p[0] & 0x80) {
333         unsigned char *q;
334         int carry = 1;
335         data->negative = 1;
336
337         data->length = len;
338
339         if (p[0] == 0xff) {
340             p++;
341             data->length--;
342         }
343         data->data = malloc(data->length);
344         if (data->data == NULL) {
345             data->length = 0;
346             if (size)
347                 *size = 0;
348             return ENOMEM;
349         }
350         q = &((unsigned char*)data->data)[data->length - 1];
351         p += data->length - 1;
352         while (q >= (unsigned char*)data->data) {
353             *q = *p ^ 0xff;
354             if (carry)
355                 carry = !++*q;
356             p--;
357             q--;
358         }
359     } else {
360         data->negative = 0;
361         data->length = len;
362
363         if (p[0] == 0) {
364             p++;
365             data->length--;
366         }
367         data->data = malloc(data->length);
368         if (data->data == NULL && data->length != 0) {
369             data->length = 0;
370             if (size)
371                 *size = 0;
372             return ENOMEM;
373         }
374         memcpy(data->data, p, data->length);
375     }
376     if (size)
377         *size = len;
378     return 0;
379 }
380
381 static int
382 generalizedtime2time (const char *s, time_t *t)
383 {
384     struct tm tm;
385
386     memset(&tm, 0, sizeof(tm));
387     if (sscanf (s, "%04d%02d%02d%02d%02d%02dZ",
388                 &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
389                 &tm.tm_min, &tm.tm_sec) != 6) {
390         if (sscanf (s, "%02d%02d%02d%02d%02d%02dZ",
391                     &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
392                     &tm.tm_min, &tm.tm_sec) != 6)
393             return ASN1_BAD_TIMEFORMAT;
394         if (tm.tm_year < 50)
395             tm.tm_year += 2000;
396         else
397             tm.tm_year += 1900;
398     }
399     tm.tm_year -= 1900;
400     tm.tm_mon -= 1;
401     *t = _der_timegm (&tm);
402     return 0;
403 }
404
405 static int
406 der_get_time (const unsigned char *p, size_t len,
407               time_t *data, size_t *size)
408 {
409     char *times;
410     int e;
411
412     if (len > len + 1 || len == 0)
413         return ASN1_BAD_LENGTH;
414
415     times = malloc(len + 1);
416     if (times == NULL)
417         return ENOMEM;
418     memcpy(times, p, len);
419     times[len] = '\0';
420     e = generalizedtime2time(times, data);
421     free (times);
422     if(size) *size = len;
423     return e;
424 }
425
426 int
427 der_get_generalized_time (const unsigned char *p, size_t len,
428                           time_t *data, size_t *size)
429 {
430     return der_get_time(p, len, data, size);
431 }
432
433 int
434 der_get_utctime (const unsigned char *p, size_t len,
435                           time_t *data, size_t *size)
436 {
437     return der_get_time(p, len, data, size);
438 }
439
440 int
441 der_get_oid (const unsigned char *p, size_t len,
442              heim_oid *data, size_t *size)
443 {
444     size_t n;
445     size_t oldlen = len;
446
447     if (len < 1)
448         return ASN1_OVERRUN;
449
450     if (len > len + 1)
451         return ASN1_BAD_LENGTH;
452
453     if (len + 1 > UINT_MAX/sizeof(data->components[0]))
454         return ERANGE;
455
456     data->components = malloc((len + 1) * sizeof(data->components[0]));
457     if (data->components == NULL)
458         return ENOMEM;
459     data->components[0] = (*p) / 40;
460     data->components[1] = (*p) % 40;
461     --len;
462     ++p;
463     for (n = 2; len > 0; ++n) {
464         unsigned u = 0, u1;
465
466         do {
467             --len;
468             u1 = u * 128 + (*p++ % 128);
469             /* check that we don't overflow the element */
470             if (u1 < u) {
471                 der_free_oid(data);
472                 return ASN1_OVERRUN;
473             }
474             u = u1;
475         } while (len > 0 && p[-1] & 0x80);
476         data->components[n] = u;
477     }
478     if (n > 2 && p[-1] & 0x80) {
479         der_free_oid (data);
480         return ASN1_OVERRUN;
481     }
482     data->length = n;
483     if (size)
484         *size = oldlen;
485     return 0;
486 }
487
488 int
489 der_get_tag (const unsigned char *p, size_t len,
490              Der_class *class, Der_type *type,
491              unsigned int *tag, size_t *size)
492 {
493     size_t ret = 0;
494     if (len < 1)
495         return ASN1_OVERRUN;
496     *class = (Der_class)(((*p) >> 6) & 0x03);
497     *type = (Der_type)(((*p) >> 5) & 0x01);
498     *tag = (*p) & 0x1f;
499     p++; len--; ret++;
500     if(*tag == 0x1f) {
501         unsigned int continuation;
502         unsigned int tag1;
503         *tag = 0;
504         do {
505             if(len < 1)
506                 return ASN1_OVERRUN;
507             continuation = *p & 128;
508             tag1 = *tag * 128 + (*p % 128);
509             /* check that we don't overflow the tag */
510             if (tag1 < *tag)
511                 return ASN1_OVERFLOW;
512             *tag = tag1;
513             p++; len--; ret++;
514         } while(continuation);
515     }
516     if(size) *size = ret;
517     return 0;
518 }
519
520 int
521 der_match_tag (const unsigned char *p, size_t len,
522                Der_class class, Der_type type,
523                unsigned int tag, size_t *size)
524 {
525     Der_type thistype;
526     int e;
527
528     e = der_match_tag2(p, len, class, &thistype, tag, size);
529     if (e) return e;
530     if (thistype != type) return ASN1_BAD_ID;
531     return 0;
532 }
533
534 int
535 der_match_tag2 (const unsigned char *p, size_t len,
536                 Der_class class, Der_type *type,
537                 unsigned int tag, size_t *size)
538 {
539     size_t l;
540     Der_class thisclass;
541     unsigned int thistag;
542     int e;
543
544     e = der_get_tag (p, len, &thisclass, type, &thistag, &l);
545     if (e) return e;
546     if (class != thisclass)
547         return ASN1_BAD_ID;
548     if(tag > thistag)
549         return ASN1_MISPLACED_FIELD;
550     if(tag < thistag)
551         return ASN1_MISSING_FIELD;
552     if(size) *size = l;
553     return 0;
554 }
555
556 int
557 der_match_tag_and_length (const unsigned char *p, size_t len,
558                           Der_class class, Der_type *type, unsigned int tag,
559                           size_t *length_ret, size_t *size)
560 {
561     size_t l, ret = 0;
562     int e;
563
564     e = der_match_tag2 (p, len, class, type, tag, &l);
565     if (e) return e;
566     p += l;
567     len -= l;
568     ret += l;
569     e = der_get_length (p, len, length_ret, &l);
570     if (e) return e;
571     if(size) *size = ret + l;
572     return 0;
573 }
574
575
576
577 /*
578  * Old versions of DCE was based on a very early beta of the MIT code,
579  * which used MAVROS for ASN.1 encoding. MAVROS had the interesting
580  * feature that it encoded data in the forward direction, which has
581  * it's problems, since you have no idea how long the data will be
582  * until after you're done. MAVROS solved this by reserving one byte
583  * for length, and later, if the actual length was longer, it reverted
584  * to indefinite, BER style, lengths. The version of MAVROS used by
585  * the DCE people could apparently generate correct X.509 DER encodings, and
586  * did this by making space for the length after encoding, but
587  * unfortunately this feature wasn't used with Kerberos.
588  */
589
590 int
591 _heim_fix_dce(size_t reallen, size_t *len)
592 {
593     if(reallen == ASN1_INDEFINITE)
594         return 1;
595     if(*len < reallen)
596         return -1;
597     *len = reallen;
598     return 0;
599 }
600
601 int
602 der_get_bit_string (const unsigned char *p, size_t len,
603                     heim_bit_string *data, size_t *size)
604 {
605     if (len < 1)
606         return ASN1_OVERRUN;
607     if (p[0] > 7)
608         return ASN1_BAD_FORMAT;
609     if (len - 1 == 0 && p[0] != 0)
610         return ASN1_BAD_FORMAT;
611     /* check if any of the three upper bits are set
612      * any of them will cause a interger overrun */
613     if ((len - 1) >> (sizeof(len) * 8 - 3))
614         return ASN1_OVERRUN;
615     data->length = (len - 1) * 8;
616     data->data = malloc(len - 1);
617     if (data->data == NULL && (len - 1) != 0)
618         return ENOMEM;
619     /* copy data is there is data to copy */
620     if (len - 1 != 0) {
621       memcpy (data->data, p + 1, len - 1);
622       data->length -= p[0];
623     }
624     if(size) *size = len;
625     return 0;
626 }