2 * Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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
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.
45 der_get_unsigned (const unsigned char *p
, size_t len
,
46 unsigned *ret
, size_t *size
)
51 if (len
== sizeof(unsigned) + 1 && p
[0] == 0)
53 else if (len
> sizeof(unsigned))
57 val
= val
* 256 + *p
++;
59 if(size
) *size
= oldlen
;
64 der_get_integer (const unsigned char *p
, size_t len
,
65 int *ret
, size_t *size
)
70 if (len
> sizeof(int))
74 val
= (signed char)*p
++;
76 val
= val
* 256 + *p
++;
79 if(size
) *size
= oldlen
;
84 der_get_length (const unsigned char *p
, size_t len
,
85 size_t *val
, size_t *size
)
102 *val
= ASN1_INDEFINITE
;
109 e
= der_get_unsigned (p
, v
, &tmp
, &l
);
112 if(size
) *size
= l
+ 1;
118 der_get_boolean(const unsigned char *p
, size_t len
, int *data
, size_t *size
)
131 der_get_general_string (const unsigned char *p
, size_t len
,
132 heim_general_string
*str
, size_t *size
)
134 const unsigned char *p1
;
137 p1
= memchr(p
, 0, len
);
140 * Allow trailing NULs. We allow this since MIT Kerberos sends
141 * an strings in the NEED_PREAUTH case that includes a
144 while (p1
- p
< len
&& *p1
== '\0')
147 return ASN1_BAD_CHARACTER
;
150 return ASN1_BAD_LENGTH
;
152 s
= malloc (len
+ 1);
158 if(size
) *size
= len
;
163 der_get_utf8string (const unsigned char *p
, size_t len
,
164 heim_utf8_string
*str
, size_t *size
)
166 return der_get_general_string(p
, len
, str
, size
);
170 der_get_printable_string (const unsigned char *p
, size_t len
,
171 heim_printable_string
*str
, size_t *size
)
173 return der_get_general_string(p
, len
, str
, size
);
177 der_get_ia5_string (const unsigned char *p
, size_t len
,
178 heim_ia5_string
*str
, size_t *size
)
180 return der_get_general_string(p
, len
, str
, size
);
184 der_get_bmp_string (const unsigned char *p
, size_t len
,
185 heim_bmp_string
*data
, size_t *size
)
190 return ASN1_BAD_FORMAT
;
191 data
->length
= len
/ 2;
192 if (data
->length
> UINT_MAX
/sizeof(data
->data
[0]))
194 data
->data
= malloc(data
->length
* sizeof(data
->data
[0]));
195 if (data
->data
== NULL
&& data
->length
!= 0)
198 for (i
= 0; i
< data
->length
; i
++) {
199 data
->data
[i
] = (p
[0] << 8) | p
[1];
201 /* check for NUL in the middle of the string */
202 if (data
->data
[i
] == 0 && i
!= (data
->length
- 1)) {
206 return ASN1_BAD_CHARACTER
;
209 if (size
) *size
= len
;
215 der_get_universal_string (const unsigned char *p
, size_t len
,
216 heim_universal_string
*data
, size_t *size
)
221 return ASN1_BAD_FORMAT
;
222 data
->length
= len
/ 4;
223 if (data
->length
> UINT_MAX
/sizeof(data
->data
[0]))
225 data
->data
= malloc(data
->length
* sizeof(data
->data
[0]));
226 if (data
->data
== NULL
&& data
->length
!= 0)
229 for (i
= 0; i
< data
->length
; i
++) {
230 data
->data
[i
] = (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
232 /* check for NUL in the middle of the string */
233 if (data
->data
[i
] == 0 && i
!= (data
->length
- 1)) {
237 return ASN1_BAD_CHARACTER
;
240 if (size
) *size
= len
;
245 der_get_visible_string (const unsigned char *p
, size_t len
,
246 heim_visible_string
*str
, size_t *size
)
248 return der_get_general_string(p
, len
, str
, size
);
252 der_get_octet_string (const unsigned char *p
, size_t len
,
253 heim_octet_string
*data
, size_t *size
)
256 data
->data
= malloc(len
);
257 if (data
->data
== NULL
&& data
->length
!= 0)
259 memcpy (data
->data
, p
, len
);
260 if(size
) *size
= len
;
265 der_get_octet_string_ber (const unsigned char *p
, size_t len
,
266 heim_octet_string
*data
, size_t *size
)
271 unsigned int tag
, depth
= 0;
272 size_t l
, datalen
, oldlen
= len
;
278 e
= der_get_tag (p
, len
, &class, &type
, &tag
, &l
);
280 if (class != ASN1_C_UNIV
) {
284 if (type
== PRIM
&& tag
== UT_EndOfContent
) {
289 if (tag
!= UT_OctetString
) {
296 e
= der_get_length (p
, len
, &datalen
, &l
);
307 ptr
= realloc(data
->data
, data
->length
+ datalen
);
308 if (ptr
== NULL
&& data
->length
+ datalen
!= 0) {
313 memcpy(((unsigned char *)data
->data
) + data
->length
, p
, datalen
);
314 data
->length
+= datalen
;
322 return ASN1_INDEF_OVERRUN
;
323 if(size
) *size
= oldlen
- len
;
334 der_get_heim_integer (const unsigned char *p
, size_t len
,
335 heim_integer
*data
, size_t *size
)
358 data
->data
= malloc(data
->length
);
359 if (data
->data
== NULL
) {
365 q
= &((unsigned char*)data
->data
)[data
->length
- 1];
366 p
+= data
->length
- 1;
367 while (q
>= (unsigned char*)data
->data
) {
383 data
->data
= malloc(data
->length
);
384 if (data
->data
== NULL
&& data
->length
!= 0) {
390 memcpy(data
->data
, p
, data
->length
);
398 generalizedtime2time (const char *s
, time_t *t
)
402 memset(&tm
, 0, sizeof(tm
));
403 if (sscanf (s
, "%04d%02d%02d%02d%02d%02dZ",
404 &tm
.tm_year
, &tm
.tm_mon
, &tm
.tm_mday
, &tm
.tm_hour
,
405 &tm
.tm_min
, &tm
.tm_sec
) != 6) {
406 if (sscanf (s
, "%02d%02d%02d%02d%02d%02dZ",
407 &tm
.tm_year
, &tm
.tm_mon
, &tm
.tm_mday
, &tm
.tm_hour
,
408 &tm
.tm_min
, &tm
.tm_sec
) != 6)
409 return ASN1_BAD_TIMEFORMAT
;
417 *t
= _der_timegm (&tm
);
422 der_get_time (const unsigned char *p
, size_t len
,
423 time_t *data
, size_t *size
)
428 if (len
> len
+ 1 || len
== 0)
429 return ASN1_BAD_LENGTH
;
431 times
= malloc(len
+ 1);
434 memcpy(times
, p
, len
);
436 e
= generalizedtime2time(times
, data
);
438 if(size
) *size
= len
;
443 der_get_generalized_time (const unsigned char *p
, size_t len
,
444 time_t *data
, size_t *size
)
446 return der_get_time(p
, len
, data
, size
);
450 der_get_utctime (const unsigned char *p
, size_t len
,
451 time_t *data
, size_t *size
)
453 return der_get_time(p
, len
, data
, size
);
457 der_get_oid (const unsigned char *p
, size_t len
,
458 heim_oid
*data
, size_t *size
)
467 return ASN1_BAD_LENGTH
;
469 if (len
+ 1 > UINT_MAX
/sizeof(data
->components
[0]))
472 data
->components
= malloc((len
+ 1) * sizeof(data
->components
[0]));
473 if (data
->components
== NULL
)
475 data
->components
[0] = (*p
) / 40;
476 data
->components
[1] = (*p
) % 40;
479 for (n
= 2; len
> 0; ++n
) {
484 u1
= u
* 128 + (*p
++ % 128);
485 /* check that we don't overflow the element */
491 } while (len
> 0 && p
[-1] & 0x80);
492 data
->components
[n
] = u
;
494 if (n
> 2 && p
[-1] & 0x80) {
505 der_get_tag (const unsigned char *p
, size_t len
,
506 Der_class
*class, Der_type
*type
,
507 unsigned int *tag
, size_t *size
)
512 *class = (Der_class
)(((*p
) >> 6) & 0x03);
513 *type
= (Der_type
)(((*p
) >> 5) & 0x01);
517 unsigned int continuation
;
523 continuation
= *p
& 128;
524 tag1
= *tag
* 128 + (*p
% 128);
525 /* check that we don't overflow the tag */
527 return ASN1_OVERFLOW
;
530 } while(continuation
);
532 if(size
) *size
= ret
;
537 der_match_tag (const unsigned char *p
, size_t len
,
538 Der_class
class, Der_type type
,
539 unsigned int tag
, size_t *size
)
544 e
= der_match_tag2(p
, len
, class, &thistype
, tag
, size
);
546 if (thistype
!= type
) return ASN1_BAD_ID
;
551 der_match_tag2 (const unsigned char *p
, size_t len
,
552 Der_class
class, Der_type
*type
,
553 unsigned int tag
, size_t *size
)
557 unsigned int thistag
;
560 e
= der_get_tag (p
, len
, &thisclass
, type
, &thistag
, &l
);
562 if (class != thisclass
)
565 return ASN1_MISPLACED_FIELD
;
567 return ASN1_MISSING_FIELD
;
573 der_match_tag_and_length (const unsigned char *p
, size_t len
,
574 Der_class
class, Der_type
*type
, unsigned int tag
,
575 size_t *length_ret
, size_t *size
)
580 e
= der_match_tag2 (p
, len
, class, type
, tag
, &l
);
585 e
= der_get_length (p
, len
, length_ret
, &l
);
587 if(size
) *size
= ret
+ l
;
594 * Old versions of DCE was based on a very early beta of the MIT code,
595 * which used MAVROS for ASN.1 encoding. MAVROS had the interesting
596 * feature that it encoded data in the forward direction, which has
597 * it's problems, since you have no idea how long the data will be
598 * until after you're done. MAVROS solved this by reserving one byte
599 * for length, and later, if the actual length was longer, it reverted
600 * to indefinite, BER style, lengths. The version of MAVROS used by
601 * the DCE people could apparently generate correct X.509 DER encodings, and
602 * did this by making space for the length after encoding, but
603 * unfortunately this feature wasn't used with Kerberos.
607 _heim_fix_dce(size_t reallen
, size_t *len
)
609 if(reallen
== ASN1_INDEFINITE
)
618 der_get_bit_string (const unsigned char *p
, size_t len
,
619 heim_bit_string
*data
, size_t *size
)
624 return ASN1_BAD_FORMAT
;
625 if (len
- 1 == 0 && p
[0] != 0)
626 return ASN1_BAD_FORMAT
;
627 /* check if any of the three upper bits are set
628 * any of them will cause a interger overrun */
629 if ((len
- 1) >> (sizeof(len
) * 8 - 3))
631 data
->length
= (len
- 1) * 8;
632 data
->data
= malloc(len
- 1);
633 if (data
->data
== NULL
&& (len
- 1) != 0)
635 /* copy data is there is data to copy */
637 memcpy (data
->data
, p
+ 1, len
- 1);
638 data
->length
-= p
[0];
640 if(size
) *size
= len
;