2 * Copyright (c) 1997 - 2004 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
36 RCSID("$Id: der_get.c,v 1.39 2005/05/29 14:23:00 lha Exp $");
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.
49 der_get_unsigned (const unsigned char *p, size_t len,
50 unsigned *ret, size_t *size)
56 val = val * 256 + *p++;
58 if(size) *size = oldlen;
63 der_get_int (const unsigned char *p, size_t len,
64 int *ret, size_t *size)
70 val = (signed char)*p++;
72 val = val * 256 + *p++;
75 if(size) *size = oldlen;
80 der_get_boolean(const unsigned char *p, size_t len, int *data, size_t *size)
93 der_get_length (const unsigned char *p, size_t len,
94 size_t *val, size_t *size)
111 *val = ASN1_INDEFINITE;
118 e = der_get_unsigned (p, v, &tmp, &l);
121 if(size) *size = l + 1;
127 der_get_general_string (const unsigned char *p, size_t len,
128 heim_general_string *str, size_t *size)
132 s = malloc (len + 1);
138 if(size) *size = len;
143 der_get_octet_string (const unsigned char *p, size_t len,
144 heim_octet_string *data, size_t *size)
147 data->data = malloc(len);
148 if (data->data == NULL && data->length != 0)
150 memcpy (data->data, p, len);
151 if(size) *size = len;
156 der_get_oid (const unsigned char *p, size_t len,
157 heim_oid *data, size_t *size)
165 data->components = malloc((len + 1) * sizeof(*data->components));
166 if (data->components == NULL)
168 data->components[0] = (*p) / 40;
169 data->components[1] = (*p) % 40;
172 for (n = 2; len > 0; ++n) {
177 u = u * 128 + (*p++ % 128);
178 } while (len > 0 && p[-1] & 0x80);
179 data->components[n] = u;
181 if (len > 0 && p[-1] & 0x80) {
192 der_get_tag (const unsigned char *p, size_t len,
193 Der_class *class, Der_type *type,
194 int *tag, size_t *size)
198 *class = (Der_class)(((*p) >> 6) & 0x03);
199 *type = (Der_type)(((*p) >> 5) & 0x01);
206 der_match_tag (const unsigned char *p, size_t len,
207 Der_class class, Der_type type,
208 int tag, size_t *size)
216 e = der_get_tag (p, len, &thisclass, &thistype, &thistag, &l);
218 if (class != thisclass || type != thistype)
221 return ASN1_MISPLACED_FIELD;
223 return ASN1_MISSING_FIELD;
229 der_match_tag_and_length (const unsigned char *p, size_t len,
230 Der_class class, Der_type type, int tag,
231 size_t *length_ret, size_t *size)
236 e = der_match_tag (p, len, class, type, tag, &l);
242 e = der_get_length (p, len, length_ret, &l);
247 if(size) *size = ret;
252 decode_boolean (const unsigned char *p, size_t len,
253 int *num, size_t *size)
259 e = der_match_tag (p, len, ASN1_C_UNIV, PRIM, UT_Boolean, &l);
265 e = der_get_length (p, len, &reallen, &l);
273 e = der_get_boolean (p, reallen, num, &l);
278 if(size) *size = ret;
283 decode_integer (const unsigned char *p, size_t len,
284 int *num, size_t *size)
290 e = der_match_tag (p, len, ASN1_C_UNIV, PRIM, UT_Integer, &l);
296 e = der_get_length (p, len, &reallen, &l);
304 e = der_get_int (p, reallen, num, &l);
309 if(size) *size = ret;
314 decode_unsigned (const unsigned char *p, size_t len,
315 unsigned *num, size_t *size)
321 e = der_match_tag (p, len, ASN1_C_UNIV, PRIM, UT_Integer, &l);
327 e = der_get_length (p, len, &reallen, &l);
335 e = der_get_unsigned (p, reallen, num, &l);
340 if(size) *size = ret;
345 decode_enumerated (const unsigned char *p, size_t len,
346 unsigned *num, size_t *size)
352 e = der_match_tag (p, len, ASN1_C_UNIV, PRIM, UT_Enumerated, &l);
358 e = der_get_length (p, len, &reallen, &l);
366 e = der_get_int (p, reallen, num, &l);
371 if(size) *size = ret;
376 decode_general_string (const unsigned char *p, size_t len,
377 heim_general_string *str, size_t *size)
383 e = der_match_tag (p, len, ASN1_C_UNIV, PRIM, UT_GeneralString, &l);
389 e = der_get_length (p, len, &reallen, &l);
397 e = der_get_general_string (p, reallen, str, &l);
402 if(size) *size = ret;
407 decode_octet_string (const unsigned char *p, size_t len,
408 heim_octet_string *k, size_t *size)
414 e = der_match_tag (p, len, ASN1_C_UNIV, PRIM, UT_OctetString, &l);
420 e = der_get_length (p, len, &reallen, &l);
428 e = der_get_octet_string (p, reallen, k, &l);
433 if(size) *size = ret;
438 decode_oid (const unsigned char *p, size_t len,
439 heim_oid *k, size_t *size)
445 e = der_match_tag (p, len, ASN1_C_UNIV, PRIM, UT_OID, &l);
451 e = der_get_length (p, len, &reallen, &l);
459 e = der_get_oid (p, reallen, k, &l);
464 if(size) *size = ret;
469 generalizedtime2time (const char *s, time_t *t)
473 memset(&tm, 0, sizeof(tm));
474 sscanf (s, "%04d%02d%02d%02d%02d%02dZ",
475 &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
476 &tm.tm_min, &tm.tm_sec);
483 decode_generalized_time (const unsigned char *p, size_t len,
484 time_t *t, size_t *size)
492 e = der_match_tag (p, len, ASN1_C_UNIV, PRIM, UT_GeneralizedTime, &l);
498 e = der_get_length (p, len, &reallen, &l);
506 e = der_get_octet_string (p, reallen, &k, &l);
511 times = realloc(k.data, k.length + 1);
517 generalizedtime2time (times, t);
519 if(size) *size = ret;
525 fix_dce(size_t reallen, size_t *len)
527 if(reallen == ASN1_INDEFINITE)