2 * Copyright (c) 1997 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. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Kungliga Tekniska
20 * Högskolan and its contributors.
22 * 4. Neither the name of the Institute nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * All encoding functions take a pointer `p' to first position in
45 * which to write, from the right, `len' which means the maximum
46 * number of characters we are able to write and return an int
47 * indicating how many actually got written, or <0 in case of errors.
51 der_put_int (unsigned char *p
, size_t len
, unsigned val
, size_t *size
)
53 unsigned char *base
= p
;
56 while (len
> 0 && val
) {
77 der_put_length (unsigned char *p
, size_t len
, size_t val
, size_t *size
)
91 e
= der_put_int (p
, len
- 1, val
, &l
);
102 der_put_general_string (unsigned char *p
, size_t len
,
103 general_string
*str
, size_t *size
)
105 size_t slen
= strlen(*str
);
110 return ASN1_OVERFLOW
;
113 memcpy (p
+1, *str
, slen
);
114 e
= der_put_length (p
, len
, slen
, &l
);
122 der_put_octet_string (unsigned char *p
, size_t len
,
123 octet_string
*data
, size_t *size
)
128 if (len
< data
->length
)
129 return ASN1_OVERFLOW
;
132 memcpy (p
+1, data
->data
, data
->length
);
133 e
= der_put_length (p
, len
, data
->length
, &l
);
136 *size
= l
+ data
->length
;
141 der_put_tag (unsigned char *p
, size_t len
, Der_class
class, Der_type type
,
142 int tag
, size_t *size
)
145 return ASN1_OVERFLOW
;
146 *p
= (class << 6) | (type
<< 5) | tag
; /* XXX */
152 der_put_length_and_tag (unsigned char *p
, size_t len
, size_t len_val
,
153 Der_class
class, Der_type type
, int tag
, size_t *size
)
159 e
= der_put_length (p
, len
, len_val
, &l
);
165 e
= der_put_tag (p
, len
, class, type
, tag
, &l
);
176 encode_integer (unsigned char *p
, size_t len
, unsigned *data
, size_t *size
)
178 unsigned num
= *data
;
183 e
= der_put_int (p
, len
, num
, &l
);
189 e
= der_put_length (p
, len
, l
, &l
);
195 e
= der_put_tag (p
, len
, UNIV
, PRIM
, UT_Integer
, &l
);
206 encode_general_string (unsigned char *p
, size_t len
,
207 general_string
*data
, size_t *size
)
213 e
= der_put_general_string (p
, len
, data
, &l
);
219 e
= der_put_tag (p
, len
, UNIV
, PRIM
, UT_GeneralString
, &l
);
230 encode_octet_string (unsigned char *p
, size_t len
,
231 octet_string
*k
, size_t *size
)
237 e
= der_put_octet_string (p
, len
, k
, &l
);
243 e
= der_put_tag (p
, len
, UNIV
, PRIM
, UT_OctetString
, &l
);
254 time2generalizedtime (time_t t
, octet_string
*s
)
258 s
->data
= malloc(16);
261 sprintf (s
->data
, "%04d%02d%02d%02d%02d%02dZ", tm
->tm_year
+ 1900,
262 tm
->tm_mon
+ 1, tm
->tm_mday
, tm
->tm_hour
, tm
->tm_min
,
267 encode_generalized_time (unsigned char *p
, size_t len
, time_t *t
, size_t *size
)
274 time2generalizedtime (*t
, &k
);
275 e
= der_put_octet_string (p
, len
, &k
, &l
);
282 e
= der_put_tag (p
, len
, UNIV
, PRIM
, UT_GeneralizedTime
, &l
);