1 /* $NetBSD: der_put.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
4 * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * 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
, const unsigned *v
, size_t *size
)
51 unsigned char *base
= p
;
55 while (len
> 0 && val
) {
81 der_put_integer (unsigned char *p
, size_t len
, const int *v
, size_t *size
)
83 unsigned char *base
= p
;
104 return ASN1_OVERFLOW
;
111 return ASN1_OVERFLOW
;
122 der_put_length (unsigned char *p
, size_t len
, size_t val
, size_t *size
)
125 return ASN1_OVERFLOW
;
135 return ASN1_OVERFLOW
;
149 der_put_boolean(unsigned char *p
, size_t len
, const int *data
, size_t *size
)
152 return ASN1_OVERFLOW
;
162 der_put_general_string (unsigned char *p
, size_t len
,
163 const heim_general_string
*str
, size_t *size
)
165 size_t slen
= strlen(*str
);
168 return ASN1_OVERFLOW
;
170 memcpy (p
+1, *str
, slen
);
176 der_put_utf8string (unsigned char *p
, size_t len
,
177 const heim_utf8_string
*str
, size_t *size
)
179 return der_put_general_string(p
, len
, str
, size
);
183 der_put_printable_string (unsigned char *p
, size_t len
,
184 const heim_printable_string
*str
, size_t *size
)
186 return der_put_octet_string(p
, len
, str
, size
);
190 der_put_ia5_string (unsigned char *p
, size_t len
,
191 const heim_ia5_string
*str
, size_t *size
)
193 return der_put_octet_string(p
, len
, str
, size
);
197 der_put_bmp_string (unsigned char *p
, size_t len
,
198 const heim_bmp_string
*data
, size_t *size
)
201 if (len
/ 2 < data
->length
)
202 return ASN1_OVERFLOW
;
203 p
-= data
->length
* 2;
204 for (i
= 0; i
< data
->length
; i
++) {
205 p
[1] = (data
->data
[i
] >> 8) & 0xff;
206 p
[2] = data
->data
[i
] & 0xff;
209 if (size
) *size
= data
->length
* 2;
214 der_put_universal_string (unsigned char *p
, size_t len
,
215 const heim_universal_string
*data
, size_t *size
)
218 if (len
/ 4 < data
->length
)
219 return ASN1_OVERFLOW
;
220 p
-= data
->length
* 4;
221 for (i
= 0; i
< data
->length
; i
++) {
222 p
[1] = (data
->data
[i
] >> 24) & 0xff;
223 p
[2] = (data
->data
[i
] >> 16) & 0xff;
224 p
[3] = (data
->data
[i
] >> 8) & 0xff;
225 p
[4] = data
->data
[i
] & 0xff;
228 if (size
) *size
= data
->length
* 4;
233 der_put_visible_string (unsigned char *p
, size_t len
,
234 const heim_visible_string
*str
, size_t *size
)
236 return der_put_general_string(p
, len
, str
, size
);
240 der_put_octet_string (unsigned char *p
, size_t len
,
241 const heim_octet_string
*data
, size_t *size
)
243 if (len
< data
->length
)
244 return ASN1_OVERFLOW
;
246 memcpy (p
+1, data
->data
, data
->length
);
247 *size
= data
->length
;
252 der_put_heim_integer (unsigned char *p
, size_t len
,
253 const heim_integer
*data
, size_t *size
)
255 unsigned char *buf
= data
->data
;
258 if (data
->length
== 0) {
260 return ASN1_OVERFLOW
;
266 if (len
< data
->length
)
267 return ASN1_OVERFLOW
;
271 if (data
->negative
) {
273 for (i
= data
->length
- 1, carry
= 1; i
>= 0; i
--) {
281 return ASN1_OVERFLOW
;
288 memcpy(p
+ 1, buf
, data
->length
);
292 return ASN1_OVERFLOW
;
299 *size
= data
->length
+ hibitset
;
304 der_put_generalized_time (unsigned char *p
, size_t len
,
305 const time_t *data
, size_t *size
)
311 e
= _heim_time2generalizedtime (*data
, &k
, 1);
314 e
= der_put_octet_string(p
, len
, &k
, &l
);
324 der_put_utctime (unsigned char *p
, size_t len
,
325 const time_t *data
, size_t *size
)
331 e
= _heim_time2generalizedtime (*data
, &k
, 0);
334 e
= der_put_octet_string(p
, len
, &k
, &l
);
344 der_put_oid (unsigned char *p
, size_t len
,
345 const heim_oid
*data
, size_t *size
)
347 unsigned char *base
= p
;
350 for (n
= data
->length
- 1; n
>= 2; --n
) {
351 unsigned u
= data
->components
[n
];
354 return ASN1_OVERFLOW
;
360 return ASN1_OVERFLOW
;
361 *p
-- = 128 + u
% 128;
367 return ASN1_OVERFLOW
;
368 *p
-- = 40 * data
->components
[0] + data
->components
[1];
374 der_put_tag (unsigned char *p
, size_t len
, Der_class
class, Der_type type
,
375 unsigned int tag
, size_t *size
)
379 return ASN1_OVERFLOW
;
380 *p
= MAKE_TAG(class, type
, tag
);
384 unsigned int continuation
= 0;
388 return ASN1_OVERFLOW
;
389 *p
-- = tag
% 128 | continuation
;
396 return ASN1_OVERFLOW
;
397 *p
-- = MAKE_TAG(class, type
, 0x1f);
405 der_put_length_and_tag (unsigned char *p
, size_t len
, size_t len_val
,
406 Der_class
class, Der_type type
,
407 unsigned int tag
, size_t *size
)
413 e
= der_put_length (p
, len
, len_val
, &l
);
419 e
= der_put_tag (p
, len
, class, type
, tag
, &l
);
429 _heim_time2generalizedtime (time_t t
, heim_octet_string
*s
, int gtimep
)
432 const size_t len
= gtimep
? 15 : 13;
434 s
->data
= malloc(len
+ 1);
438 if (_der_gmtime(t
, &tm
) == NULL
)
439 return ASN1_BAD_TIMEFORMAT
;
441 snprintf (s
->data
, len
+ 1, "%04d%02d%02d%02d%02d%02dZ",
442 tm
.tm_year
+ 1900, tm
.tm_mon
+ 1, tm
.tm_mday
,
443 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
);
445 snprintf (s
->data
, len
+ 1, "%02d%02d%02d%02d%02d%02dZ",
446 tm
.tm_year
% 100, tm
.tm_mon
+ 1, tm
.tm_mday
,
447 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
);
453 der_put_bit_string (unsigned char *p
, size_t len
,
454 const heim_bit_string
*data
, size_t *size
)
456 size_t data_size
= (data
->length
+ 7) / 8;
457 if (len
< data_size
+ 1)
458 return ASN1_OVERFLOW
;
461 memcpy (p
+2, data
->data
, data_size
);
462 if (data
->length
&& (data
->length
% 8) != 0)
463 p
[1] = 8 - (data
->length
% 8);
466 *size
= data_size
+ 1;
471 _heim_der_set_sort(const void *a1
, const void *a2
)
473 const struct heim_octet_string
*s1
= a1
, *s2
= a2
;
476 ret
= memcmp(s1
->data
, s2
->data
,
477 s1
->length
< s2
->length
? s1
->length
: s2
->length
);
480 return s1
->length
- s2
->length
;