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
36 RCSID("$Id: der_put.c,v 1.28 2003/04/17 07:12:24 lha Exp $");
39 * All encoding functions take a pointer `p' to first position in
40 * which to write, from the right, `len' which means the maximum
41 * number of characters we are able to write. The function returns
42 * the number of characters written in `size' (if non-NULL).
43 * The return value is 0 or an error.
47 der_put_unsigned (unsigned char *p, size_t len, unsigned val, size_t *size)
49 unsigned char *base = p;
52 while (len > 0 && val) {
73 der_put_int (unsigned char *p, size_t len, int val, size_t *size)
75 unsigned char *base = p;
102 return ASN1_OVERFLOW;
113 der_put_length (unsigned char *p, size_t len, size_t val, size_t *size)
116 return ASN1_OVERFLOW;
125 e = der_put_unsigned (p, len - 1, val, &l);
136 der_put_general_string (unsigned char *p, size_t len,
137 const general_string *str, size_t *size)
139 size_t slen = strlen(*str);
142 return ASN1_OVERFLOW;
145 memcpy (p+1, *str, slen);
151 der_put_octet_string (unsigned char *p, size_t len,
152 const octet_string *data, size_t *size)
154 if (len < data->length)
155 return ASN1_OVERFLOW;
158 memcpy (p+1, data->data, data->length);
159 *size = data->length;
164 der_put_oid (unsigned char *p, size_t len,
165 const oid *data, size_t *size)
167 unsigned char *base = p;
170 for (n = data->length - 1; n >= 2; --n) {
171 unsigned u = data->components[n];
174 return ASN1_OVERFLOW;
180 return ASN1_OVERFLOW;
181 *p-- = 128 + u % 128;
187 return ASN1_OVERFLOW;
188 *p-- = 40 * data->components[0] + data->components[1];
194 der_put_tag (unsigned char *p, size_t len, Der_class class, Der_type type,
195 int tag, size_t *size)
198 return ASN1_OVERFLOW;
199 *p = (class << 6) | (type << 5) | tag; /* XXX */
205 der_put_length_and_tag (unsigned char *p, size_t len, size_t len_val,
206 Der_class class, Der_type type, int tag, size_t *size)
212 e = der_put_length (p, len, len_val, &l);
218 e = der_put_tag (p, len, class, type, tag, &l);
229 encode_integer (unsigned char *p, size_t len, const int *data, size_t *size)
236 e = der_put_int (p, len, num, &l);
242 e = der_put_length_and_tag (p, len, l, UNIV, PRIM, UT_Integer, &l);
253 encode_unsigned (unsigned char *p, size_t len, const unsigned *data,
256 unsigned num = *data;
261 e = der_put_unsigned (p, len, num, &l);
267 e = der_put_length_and_tag (p, len, l, UNIV, PRIM, UT_Integer, &l);
278 encode_enumerated (unsigned char *p, size_t len, const unsigned *data,
281 unsigned num = *data;
286 e = der_put_int (p, len, num, &l);
292 e = der_put_length_and_tag (p, len, l, UNIV, PRIM, UT_Enumerated, &l);
303 encode_general_string (unsigned char *p, size_t len,
304 const general_string *data, size_t *size)
310 e = der_put_general_string (p, len, data, &l);
316 e = der_put_length_and_tag (p, len, l, UNIV, PRIM, UT_GeneralString, &l);
327 encode_octet_string (unsigned char *p, size_t len,
328 const octet_string *k, size_t *size)
334 e = der_put_octet_string (p, len, k, &l);
340 e = der_put_length_and_tag (p, len, l, UNIV, PRIM, UT_OctetString, &l);
351 encode_oid(unsigned char *p, size_t len,
352 const oid *k, size_t *size)
358 e = der_put_oid (p, len, k, &l);
364 e = der_put_length_and_tag (p, len, l, UNIV, PRIM, UT_OID, &l);
375 time2generalizedtime (time_t t, octet_string *s)
382 s->data = malloc(len + 1);
387 snprintf (s->data, len + 1, "%04d%02d%02d%02d%02d%02dZ",
388 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
389 tm->tm_hour, tm->tm_min, tm->tm_sec);
394 encode_generalized_time (unsigned char *p, size_t len,
395 const time_t *t, size_t *size)
402 e = time2generalizedtime (*t, &k);
405 e = der_put_octet_string (p, len, &k, &l);
412 e = der_put_length_and_tag (p, len, k.length, UNIV, PRIM,
413 UT_GeneralizedTime, &l);