s4:heimdal: import lorikeet-heimdal-201001120029 (commit a5e675fed7c5db8a7370b77ed0bf...
[sfrench/samba-autobuild/.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         /* check for NUL in the middle of the string */
202         if (data->data[i] == 0 && i != (data->length - 1)) {
203             free(data->data);
204             data->data = NULL;
205             data->length = 0;
206             return ASN1_BAD_CHARACTER;
207         }
208     }
209     if (size) *size = len;
210
211     return 0;
212 }
213
214 int
215 der_get_universal_string (const unsigned char *p, size_t len,
216                           heim_universal_string *data, size_t *size)
217 {
218     size_t i;
219
220     if (len & 3)
221         return ASN1_BAD_FORMAT;
222     data->length = len / 4;
223     if (data->length > UINT_MAX/sizeof(data->data[0]))
224         return ERANGE;
225     data->data = malloc(data->length * sizeof(data->data[0]));
226     if (data->data == NULL && data->length != 0)
227         return ENOMEM;
228
229     for (i = 0; i < data->length; i++) {
230         data->data[i] = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
231         p += 4;
232         /* check for NUL in the middle of the string */
233         if (data->data[i] == 0 && i != (data->length - 1)) {
234             free(data->data);
235             data->data = NULL;
236             data->length = 0;
237             return ASN1_BAD_CHARACTER;
238         }
239     }
240     if (size) *size = len;
241     return 0;
242 }
243
244 int
245 der_get_visible_string (const unsigned char *p, size_t len,
246                         heim_visible_string *str, size_t *size)
247 {
248     return der_get_general_string(p, len, str, size);
249 }
250
251 int
252 der_get_octet_string (const unsigned char *p, size_t len,
253                       heim_octet_string *data, size_t *size)
254 {
255     data->length = len;
256     data->data = malloc(len);
257     if (data->data == NULL && data->length != 0)
258         return ENOMEM;
259     memcpy (data->data, p, len);
260     if(size) *size = len;
261     return 0;
262 }
263
264 int
265 der_get_octet_string_ber (const unsigned char *p, size_t len,
266                           heim_octet_string *data, size_t *size)
267 {
268     int e;
269     Der_type type;
270     Der_class class;
271     unsigned int tag, depth = 0;
272     size_t l, datalen, oldlen = len;
273
274     data->length = 0;
275     data->data = NULL;
276
277     while (len) {
278         e = der_get_tag (p, len, &class, &type, &tag, &l);
279         if (e) goto out;
280         if (class != ASN1_C_UNIV) {
281             e = ASN1_BAD_ID;
282             goto out;
283         }
284         if (type == PRIM && tag == UT_EndOfContent) {
285             if (depth == 0)
286                 break;
287             depth--;
288         }
289         if (tag != UT_OctetString) {
290             e = ASN1_BAD_ID;
291             goto out;
292         }
293
294         p += l;
295         len -= l;
296         e = der_get_length (p, len, &datalen, &l);
297         if (e) goto out;
298         p += l;
299         len -= l;
300
301         if (datalen > len)
302             return ASN1_OVERRUN;
303
304         if (type == PRIM) {
305             void *ptr;
306
307             ptr = realloc(data->data, data->length + datalen);
308             if (ptr == NULL) {
309                 e = ENOMEM;
310                 goto out;
311             }
312             data->data = ptr;
313             memcpy(((unsigned char *)data->data) + data->length, p, datalen);
314             data->length += datalen;
315         } else
316             depth++;
317
318         p += datalen;
319         len -= datalen;
320     }
321     if (depth != 0)
322         return ASN1_INDEF_OVERRUN;
323     if(size) *size = oldlen - len;
324     return 0;
325  out:
326     free(data->data);
327     data->data = NULL;
328     data->length = 0;
329     return e;
330 }
331
332
333 int
334 der_get_heim_integer (const unsigned char *p, size_t len,
335                       heim_integer *data, size_t *size)
336 {
337     data->length = 0;
338     data->negative = 0;
339     data->data = NULL;
340
341     if (len == 0) {
342         if (size)
343             *size = 0;
344         return 0;
345     }
346     if (p[0] & 0x80) {
347         unsigned char *q;
348         int carry = 1;
349         data->negative = 1;
350
351         data->length = len;
352
353         if (p[0] == 0xff) {
354             p++;
355             data->length--;
356         }
357         data->data = malloc(data->length);
358         if (data->data == NULL) {
359             data->length = 0;
360             if (size)
361                 *size = 0;
362             return ENOMEM;
363         }
364         q = &((unsigned char*)data->data)[data->length - 1];
365         p += data->length - 1;
366         while (q >= (unsigned char*)data->data) {
367             *q = *p ^ 0xff;
368             if (carry)
369                 carry = !++*q;
370             p--;
371             q--;
372         }
373     } else {
374         data->negative = 0;
375         data->length = len;
376
377         if (p[0] == 0) {
378             p++;
379             data->length--;
380         }
381         data->data = malloc(data->length);
382         if (data->data == NULL && data->length != 0) {
383             data->length = 0;
384             if (size)
385                 *size = 0;
386             return ENOMEM;
387         }
388         memcpy(data->data, p, data->length);
389     }
390     if (size)
391         *size = len;
392     return 0;
393 }
394
395 static int
396 generalizedtime2time (const char *s, time_t *t)
397 {
398     struct tm tm;
399
400     memset(&tm, 0, sizeof(tm));
401     if (sscanf (s, "%04d%02d%02d%02d%02d%02dZ",
402                 &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
403                 &tm.tm_min, &tm.tm_sec) != 6) {
404         if (sscanf (s, "%02d%02d%02d%02d%02d%02dZ",
405                     &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
406                     &tm.tm_min, &tm.tm_sec) != 6)
407             return ASN1_BAD_TIMEFORMAT;
408         if (tm.tm_year < 50)
409             tm.tm_year += 2000;
410         else
411             tm.tm_year += 1900;
412     }
413     tm.tm_year -= 1900;
414     tm.tm_mon -= 1;
415     *t = _der_timegm (&tm);
416     return 0;
417 }
418
419 static int
420 der_get_time (const unsigned char *p, size_t len,
421               time_t *data, size_t *size)
422 {
423     char *times;
424     int e;
425
426     if (len > len + 1 || len == 0)
427         return ASN1_BAD_LENGTH;
428
429     times = malloc(len + 1);
430     if (times == NULL)
431         return ENOMEM;
432     memcpy(times, p, len);
433     times[len] = '\0';
434     e = generalizedtime2time(times, data);
435     free (times);
436     if(size) *size = len;
437     return e;
438 }
439
440 int
441 der_get_generalized_time (const unsigned char *p, size_t len,
442                           time_t *data, size_t *size)
443 {
444     return der_get_time(p, len, data, size);
445 }
446
447 int
448 der_get_utctime (const unsigned char *p, size_t len,
449                           time_t *data, size_t *size)
450 {
451     return der_get_time(p, len, data, size);
452 }
453
454 int
455 der_get_oid (const unsigned char *p, size_t len,
456              heim_oid *data, size_t *size)
457 {
458     size_t n;
459     size_t oldlen = len;
460
461     if (len < 1)
462         return ASN1_OVERRUN;
463
464     if (len > len + 1)
465         return ASN1_BAD_LENGTH;
466
467     if (len + 1 > UINT_MAX/sizeof(data->components[0]))
468         return ERANGE;
469
470     data->components = malloc((len + 1) * sizeof(data->components[0]));
471     if (data->components == NULL)
472         return ENOMEM;
473     data->components[0] = (*p) / 40;
474     data->components[1] = (*p) % 40;
475     --len;
476     ++p;
477     for (n = 2; len > 0; ++n) {
478         unsigned u = 0, u1;
479
480         do {
481             --len;
482             u1 = u * 128 + (*p++ % 128);
483             /* check that we don't overflow the element */
484             if (u1 < u) {
485                 der_free_oid(data);
486                 return ASN1_OVERRUN;
487             }
488             u = u1;
489         } while (len > 0 && p[-1] & 0x80);
490         data->components[n] = u;
491     }
492     if (n > 2 && p[-1] & 0x80) {
493         der_free_oid (data);
494         return ASN1_OVERRUN;
495     }
496     data->length = n;
497     if (size)
498         *size = oldlen;
499     return 0;
500 }
501
502 int
503 der_get_tag (const unsigned char *p, size_t len,
504              Der_class *class, Der_type *type,
505              unsigned int *tag, size_t *size)
506 {
507     size_t ret = 0;
508     if (len < 1)
509         return ASN1_OVERRUN;
510     *class = (Der_class)(((*p) >> 6) & 0x03);
511     *type = (Der_type)(((*p) >> 5) & 0x01);
512     *tag = (*p) & 0x1f;
513     p++; len--; ret++;
514     if(*tag == 0x1f) {
515         unsigned int continuation;
516         unsigned int tag1;
517         *tag = 0;
518         do {
519             if(len < 1)
520                 return ASN1_OVERRUN;
521             continuation = *p & 128;
522             tag1 = *tag * 128 + (*p % 128);
523             /* check that we don't overflow the tag */
524             if (tag1 < *tag)
525                 return ASN1_OVERFLOW;
526             *tag = tag1;
527             p++; len--; ret++;
528         } while(continuation);
529     }
530     if(size) *size = ret;
531     return 0;
532 }
533
534 int
535 der_match_tag (const unsigned char *p, size_t len,
536                Der_class class, Der_type type,
537                unsigned int tag, size_t *size)
538 {
539     Der_type thistype;
540     int e;
541
542     e = der_match_tag2(p, len, class, &thistype, tag, size);
543     if (e) return e;
544     if (thistype != type) return ASN1_BAD_ID;
545     return 0;
546 }
547
548 int
549 der_match_tag2 (const unsigned char *p, size_t len,
550                 Der_class class, Der_type *type,
551                 unsigned int tag, size_t *size)
552 {
553     size_t l;
554     Der_class thisclass;
555     unsigned int thistag;
556     int e;
557
558     e = der_get_tag (p, len, &thisclass, type, &thistag, &l);
559     if (e) return e;
560     if (class != thisclass)
561         return ASN1_BAD_ID;
562     if(tag > thistag)
563         return ASN1_MISPLACED_FIELD;
564     if(tag < thistag)
565         return ASN1_MISSING_FIELD;
566     if(size) *size = l;
567     return 0;
568 }
569
570 int
571 der_match_tag_and_length (const unsigned char *p, size_t len,
572                           Der_class class, Der_type *type, unsigned int tag,
573                           size_t *length_ret, size_t *size)
574 {
575     size_t l, ret = 0;
576     int e;
577
578     e = der_match_tag2 (p, len, class, type, tag, &l);
579     if (e) return e;
580     p += l;
581     len -= l;
582     ret += l;
583     e = der_get_length (p, len, length_ret, &l);
584     if (e) return e;
585     if(size) *size = ret + l;
586     return 0;
587 }
588
589
590
591 /*
592  * Old versions of DCE was based on a very early beta of the MIT code,
593  * which used MAVROS for ASN.1 encoding. MAVROS had the interesting
594  * feature that it encoded data in the forward direction, which has
595  * it's problems, since you have no idea how long the data will be
596  * until after you're done. MAVROS solved this by reserving one byte
597  * for length, and later, if the actual length was longer, it reverted
598  * to indefinite, BER style, lengths. The version of MAVROS used by
599  * the DCE people could apparently generate correct X.509 DER encodings, and
600  * did this by making space for the length after encoding, but
601  * unfortunately this feature wasn't used with Kerberos.
602  */
603
604 int
605 _heim_fix_dce(size_t reallen, size_t *len)
606 {
607     if(reallen == ASN1_INDEFINITE)
608         return 1;
609     if(*len < reallen)
610         return -1;
611     *len = reallen;
612     return 0;
613 }
614
615 int
616 der_get_bit_string (const unsigned char *p, size_t len,
617                     heim_bit_string *data, size_t *size)
618 {
619     if (len < 1)
620         return ASN1_OVERRUN;
621     if (p[0] > 7)
622         return ASN1_BAD_FORMAT;
623     if (len - 1 == 0 && p[0] != 0)
624         return ASN1_BAD_FORMAT;
625     /* check if any of the three upper bits are set
626      * any of them will cause a interger overrun */
627     if ((len - 1) >> (sizeof(len) * 8 - 3))
628         return ASN1_OVERRUN;
629     data->length = (len - 1) * 8;
630     data->data = malloc(len - 1);
631     if (data->data == NULL && (len - 1) != 0)
632         return ENOMEM;
633     /* copy data is there is data to copy */
634     if (len - 1 != 0) {
635       memcpy (data->data, p + 1, len - 1);
636       data->length -= p[0];
637     }
638     if(size) *size = len;
639     return 0;
640 }