2 * Copyright (c) 1997 - 2002 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
43 * All decoding functions take a pointer `p' to first position in
44 * which to read, from the left, `len' which means the maximum number
45 * of characters we are able to read, `ret' were the value will be
46 * returned and `size' where the number of used bytes is stored.
47 * Either 0 or an error code is returned.
51 der_get_unsigned (const unsigned char *p, size_t len,
52 unsigned *ret, size_t *size)
58 val = val * 256 + *p++;
60 if(size) *size = oldlen;
65 der_get_int (const unsigned char *p, size_t len,
66 int *ret, size_t *size)
72 val = (signed char)*p++;
74 val = val * 256 + *p++;
77 if(size) *size = oldlen;
82 der_get_length (const unsigned char *p, size_t len,
83 size_t *val, size_t *size)
100 *val = ASN1_INDEFINITE;
107 e = der_get_unsigned (p, v, &tmp, &l);
110 if(size) *size = l + 1;
116 der_get_general_string (const unsigned char *p, size_t len,
117 general_string *str, size_t *size)
121 s = malloc (len + 1);
127 if(size) *size = len;
132 der_get_octet_string (const unsigned char *p, size_t len,
133 octet_string *data, size_t *size)
136 data->data = malloc(len);
137 if (data->data == NULL && data->length != 0)
139 memcpy (data->data, p, len);
140 if(size) *size = len;
145 der_get_oid (const unsigned char *p, size_t len,
146 oid *data, size_t *size)
154 data->components = malloc((len + 1) * sizeof(*data->components));
155 if (data->components == NULL && len != 0)
157 data->components[0] = (*p) / 40;
158 data->components[1] = (*p) % 40;
161 for (n = 2; len > 0; ++n) {
166 u = u * 128 + (*p++ % 128);
167 } while (len > 0 && p[-1] & 0x80);
168 data->components[n] = u;
181 der_get_tag (const unsigned char *p, size_t len,
182 Der_class *class, Der_type *type,
183 int *tag, size_t *size)
187 *class = (Der_class)(((*p) >> 6) & 0x03);
188 *type = (Der_type)(((*p) >> 5) & 0x01);
195 der_match_tag (const unsigned char *p, size_t len,
196 Der_class class, Der_type type,
197 int tag, size_t *size)
205 e = der_get_tag (p, len, &thisclass, &thistype, &thistag, &l);
207 if (class != thisclass || type != thistype)
210 return ASN1_MISPLACED_FIELD;
212 return ASN1_MISSING_FIELD;
218 der_match_tag_and_length (const unsigned char *p, size_t len,
219 Der_class class, Der_type type, int tag,
220 size_t *length_ret, size_t *size)
225 e = der_match_tag (p, len, class, type, tag, &l);
230 e = der_get_length (p, len, length_ret, &l);
235 if(size) *size = ret;
240 decode_integer (const unsigned char *p, size_t len,
241 int *num, size_t *size)
247 e = der_match_tag (p, len, UNIV, PRIM, UT_Integer, &l);
252 e = der_get_length (p, len, &reallen, &l);
259 e = der_get_int (p, reallen, num, &l);
264 if(size) *size = ret;
269 decode_unsigned (const unsigned char *p, size_t len,
270 unsigned *num, size_t *size)
276 e = der_match_tag (p, len, UNIV, PRIM, UT_Integer, &l);
281 e = der_get_length (p, len, &reallen, &l);
288 e = der_get_unsigned (p, reallen, num, &l);
293 if(size) *size = ret;
298 decode_enumerated (const unsigned char *p, size_t len,
299 unsigned *num, size_t *size)
305 e = der_match_tag (p, len, UNIV, PRIM, UT_Enumerated, &l);
310 e = der_get_length (p, len, &reallen, &l);
315 e = der_get_int (p, reallen, num, &l);
320 if(size) *size = ret;
325 decode_general_string (const unsigned char *p, size_t len,
326 general_string *str, size_t *size)
333 e = der_match_tag (p, len, UNIV, PRIM, UT_GeneralString, &l);
339 e = der_get_length (p, len, &slen, &l);
347 e = der_get_general_string (p, slen, str, &l);
352 if(size) *size = ret;
357 decode_octet_string (const unsigned char *p, size_t len,
358 octet_string *k, size_t *size)
365 e = der_match_tag (p, len, UNIV, PRIM, UT_OctetString, &l);
371 e = der_get_length (p, len, &slen, &l);
379 e = der_get_octet_string (p, slen, k, &l);
384 if(size) *size = ret;
389 decode_oid (const unsigned char *p, size_t len,
390 oid *k, size_t *size)
397 e = der_match_tag (p, len, UNIV, PRIM, UT_OID, &l);
403 e = der_get_length (p, len, &slen, &l);
411 e = der_get_oid (p, slen, k, &l);
416 if(size) *size = ret;
421 generalizedtime2time (const char *s, time_t *t)
425 memset(&tm, 0, sizeof(tm));
426 sscanf (s, "%04d%02d%02d%02d%02d%02dZ",
427 &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
428 &tm.tm_min, &tm.tm_sec);
435 decode_generalized_time (const unsigned char *p, size_t len,
436 time_t *t, size_t *size)
445 e = der_match_tag (p, len, UNIV, PRIM, UT_GeneralizedTime, &l);
451 e = der_get_length (p, len, &slen, &l);
458 e = der_get_octet_string (p, slen, &k, &l);
463 times = realloc(k.data, k.length + 1);
469 generalizedtime2time (times, t);
471 if(size) *size = ret;
477 fix_dce(size_t reallen, size_t *len)
479 if(reallen == ASN1_INDEFINITE)