2 * Copyright (c) 1997 - 2001 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
41 * All encoding functions take a pointer `p' to first position in
42 * which to write, from the right, `len' which means the maximum
43 * number of characters we are able to write. The function returns
44 * the number of characters written in `size' (if non-NULL).
45 * The return value is 0 or an error.
49 der_put_unsigned (unsigned char *p, size_t len, unsigned val, size_t *size)
51 unsigned char *base = p;
54 while (len > 0 && val) {
75 der_put_int (unsigned char *p, size_t len, int val, size_t *size)
77 unsigned char *base = p;
104 return ASN1_OVERFLOW;
115 der_put_length (unsigned char *p, size_t len, size_t val, size_t *size)
118 return ASN1_OVERFLOW;
127 e = der_put_unsigned (p, len - 1, val, &l);
138 der_put_general_string (unsigned char *p, size_t len,
139 const general_string *str, size_t *size)
141 size_t slen = strlen(*str);
144 return ASN1_OVERFLOW;
147 memcpy (p+1, *str, slen);
153 der_put_octet_string (unsigned char *p, size_t len,
154 const octet_string *data, size_t *size)
156 if (len < data->length)
157 return ASN1_OVERFLOW;
160 memcpy (p+1, data->data, data->length);
161 *size = data->length;
166 der_put_oid (unsigned char *p, size_t len,
167 const oid *data, size_t *size)
169 unsigned char *base = p;
172 for (n = data->length - 1; n >= 2; --n) {
173 unsigned u = data->components[n];
176 return ASN1_OVERFLOW;
182 return ASN1_OVERFLOW;
183 *p-- = 128 + u % 128;
189 return ASN1_OVERFLOW;
190 *p-- = 40 * data->components[0] + data->components[1];
196 der_put_tag (unsigned char *p, size_t len, Der_class class, Der_type type,
197 int tag, size_t *size)
200 return ASN1_OVERFLOW;
201 *p = (class << 6) | (type << 5) | tag; /* XXX */
207 der_put_length_and_tag (unsigned char *p, size_t len, size_t len_val,
208 Der_class class, Der_type type, int tag, size_t *size)
214 e = der_put_length (p, len, len_val, &l);
220 e = der_put_tag (p, len, class, type, tag, &l);
231 encode_integer (unsigned char *p, size_t len, const int *data, size_t *size)
238 e = der_put_int (p, len, num, &l);
244 e = der_put_length_and_tag (p, len, l, UNIV, PRIM, UT_Integer, &l);
255 encode_unsigned (unsigned char *p, size_t len, const unsigned *data,
258 unsigned num = *data;
263 e = der_put_unsigned (p, len, num, &l);
269 e = der_put_length_and_tag (p, len, l, UNIV, PRIM, UT_Integer, &l);
280 encode_enumerated (unsigned char *p, size_t len, const unsigned *data,
283 unsigned num = *data;
288 e = der_put_int (p, len, num, &l);
294 e = der_put_length_and_tag (p, len, l, UNIV, PRIM, UT_Enumerated, &l);
305 encode_general_string (unsigned char *p, size_t len,
306 const general_string *data, size_t *size)
312 e = der_put_general_string (p, len, data, &l);
318 e = der_put_length_and_tag (p, len, l, UNIV, PRIM, UT_GeneralString, &l);
329 encode_octet_string (unsigned char *p, size_t len,
330 const octet_string *k, size_t *size)
336 e = der_put_octet_string (p, len, k, &l);
342 e = der_put_length_and_tag (p, len, l, UNIV, PRIM, UT_OctetString, &l);
353 encode_oid(unsigned char *p, size_t len,
354 const oid *k, size_t *size)
360 e = der_put_oid (p, len, k, &l);
366 e = der_put_length_and_tag (p, len, l, UNIV, PRIM, UT_OID, &l);
377 time2generalizedtime (time_t t, octet_string *s)
384 s->data = malloc(len + 1);
389 snprintf (s->data, len + 1, "%04d%02d%02d%02d%02d%02dZ",
390 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
391 tm->tm_hour, tm->tm_min, tm->tm_sec);
396 encode_generalized_time (unsigned char *p, size_t len,
397 const time_t *t, size_t *size)
404 e = time2generalizedtime (*t, &k);
407 e = der_put_octet_string (p, len, &k, &l);
414 e = der_put_length_and_tag (p, len, k.length, UNIV, PRIM,
415 UT_GeneralizedTime, &l);