2 * Copyright (c) 1997-2005 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
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
, const unsigned *v
, size_t *size
)
49 unsigned char *base
= p
;
53 while (len
> 0 && val
) {
79 der_put_unsigned64 (unsigned char *p
, size_t len
, const uint64_t *v
, size_t *size
)
81 unsigned char *base
= p
;
85 while (len
> 0 && val
) {
102 return ASN1_OVERFLOW
;
111 der_put_integer (unsigned char *p
, size_t len
, const int *v
, size_t *size
)
113 unsigned char *base
= p
;
119 return ASN1_OVERFLOW
;
126 return ASN1_OVERFLOW
;
134 return ASN1_OVERFLOW
;
141 return ASN1_OVERFLOW
;
151 der_put_integer64 (unsigned char *p
, size_t len
, const int64_t *v
, size_t *size
)
153 unsigned char *base
= p
;
159 return ASN1_OVERFLOW
;
166 return ASN1_OVERFLOW
;
174 return ASN1_OVERFLOW
;
181 return ASN1_OVERFLOW
;
192 der_put_length (unsigned char *p
, size_t len
, size_t val
, size_t *size
)
195 return ASN1_OVERFLOW
;
205 return ASN1_OVERFLOW
;
219 der_put_boolean(unsigned char *p
, size_t len
, const int *data
, size_t *size
)
222 return ASN1_OVERFLOW
;
232 der_put_general_string (unsigned char *p
, size_t len
,
233 const heim_general_string
*str
, size_t *size
)
235 size_t slen
= strlen(*str
);
238 return ASN1_OVERFLOW
;
240 memcpy (p
+1, *str
, slen
);
246 der_put_utf8string (unsigned char *p
, size_t len
,
247 const heim_utf8_string
*str
, size_t *size
)
249 return der_put_general_string(p
, len
, str
, size
);
253 der_put_printable_string (unsigned char *p
, size_t len
,
254 const heim_printable_string
*str
, size_t *size
)
256 return der_put_octet_string(p
, len
, str
, size
);
260 der_put_ia5_string (unsigned char *p
, size_t len
,
261 const heim_ia5_string
*str
, size_t *size
)
263 return der_put_octet_string(p
, len
, str
, size
);
267 der_put_bmp_string (unsigned char *p
, size_t len
,
268 const heim_bmp_string
*data
, size_t *size
)
271 if (len
/ 2 < data
->length
)
272 return ASN1_OVERFLOW
;
273 p
-= data
->length
* 2;
274 for (i
= 0; i
< data
->length
; i
++) {
275 p
[1] = (data
->data
[i
] >> 8) & 0xff;
276 p
[2] = data
->data
[i
] & 0xff;
279 if (size
) *size
= data
->length
* 2;
284 der_put_universal_string (unsigned char *p
, size_t len
,
285 const heim_universal_string
*data
, size_t *size
)
288 if (len
/ 4 < data
->length
)
289 return ASN1_OVERFLOW
;
290 p
-= data
->length
* 4;
291 for (i
= 0; i
< data
->length
; i
++) {
292 p
[1] = (data
->data
[i
] >> 24) & 0xff;
293 p
[2] = (data
->data
[i
] >> 16) & 0xff;
294 p
[3] = (data
->data
[i
] >> 8) & 0xff;
295 p
[4] = data
->data
[i
] & 0xff;
298 if (size
) *size
= data
->length
* 4;
303 der_put_visible_string (unsigned char *p
, size_t len
,
304 const heim_visible_string
*str
, size_t *size
)
306 return der_put_general_string(p
, len
, str
, size
);
310 der_put_octet_string (unsigned char *p
, size_t len
,
311 const heim_octet_string
*data
, size_t *size
)
313 if (len
< data
->length
)
314 return ASN1_OVERFLOW
;
316 memcpy (p
+1, data
->data
, data
->length
);
317 *size
= data
->length
;
322 der_put_heim_integer (unsigned char *p
, size_t len
,
323 const heim_integer
*data
, size_t *size
)
325 unsigned char *buf
= data
->data
;
328 if (data
->length
== 0) {
330 return ASN1_OVERFLOW
;
336 if (len
< data
->length
)
337 return ASN1_OVERFLOW
;
341 if (data
->negative
) {
343 for (i
= data
->length
- 1, carry
= 1; i
>= 0; i
--) {
351 return ASN1_OVERFLOW
;
358 memcpy(p
+ 1, buf
, data
->length
);
362 return ASN1_OVERFLOW
;
369 *size
= data
->length
+ hibitset
;
374 der_put_generalized_time (unsigned char *p
, size_t len
,
375 const time_t *data
, size_t *size
)
381 e
= _heim_time2generalizedtime (*data
, &k
, 1);
384 e
= der_put_octet_string(p
, len
, &k
, &l
);
394 der_put_utctime (unsigned char *p
, size_t len
,
395 const time_t *data
, size_t *size
)
401 e
= _heim_time2generalizedtime (*data
, &k
, 0);
404 e
= der_put_octet_string(p
, len
, &k
, &l
);
414 der_put_oid (unsigned char *p
, size_t len
,
415 const heim_oid
*data
, size_t *size
)
417 unsigned char *base
= p
;
420 for (n
= data
->length
- 1; n
>= 2; --n
) {
421 unsigned u
= data
->components
[n
];
424 return ASN1_OVERFLOW
;
430 return ASN1_OVERFLOW
;
431 *p
-- = 128 + u
% 128;
437 return ASN1_OVERFLOW
;
438 *p
-- = 40 * data
->components
[0] + data
->components
[1];
444 der_put_tag (unsigned char *p
, size_t len
, Der_class
class, Der_type type
,
445 unsigned int tag
, size_t *size
)
449 return ASN1_OVERFLOW
;
450 *p
= MAKE_TAG(class, type
, tag
);
454 unsigned int continuation
= 0;
458 return ASN1_OVERFLOW
;
459 *p
-- = tag
% 128 | continuation
;
466 return ASN1_OVERFLOW
;
467 *p
-- = MAKE_TAG(class, type
, 0x1f);
475 der_put_length_and_tag (unsigned char *p
, size_t len
, size_t len_val
,
476 Der_class
class, Der_type type
,
477 unsigned int tag
, size_t *size
)
483 e
= der_put_length (p
, len
, len_val
, &l
);
489 e
= der_put_tag (p
, len
, class, type
, tag
, &l
);
499 _heim_time2generalizedtime (time_t t
, heim_octet_string
*s
, int gtimep
)
502 const size_t len
= gtimep
? 15 : 13;
504 s
->data
= malloc(len
+ 1);
508 if (_der_gmtime(t
, &tm
) == NULL
)
509 return ASN1_BAD_TIMEFORMAT
;
511 snprintf (s
->data
, len
+ 1, "%04d%02d%02d%02d%02d%02dZ",
512 tm
.tm_year
+ 1900, tm
.tm_mon
+ 1, tm
.tm_mday
,
513 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
);
515 snprintf (s
->data
, len
+ 1, "%02d%02d%02d%02d%02d%02dZ",
516 tm
.tm_year
% 100, tm
.tm_mon
+ 1, tm
.tm_mday
,
517 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
);
523 der_put_bit_string (unsigned char *p
, size_t len
,
524 const heim_bit_string
*data
, size_t *size
)
526 size_t data_size
= (data
->length
+ 7) / 8;
527 if (len
< data_size
+ 1)
528 return ASN1_OVERFLOW
;
531 memcpy (p
+2, data
->data
, data_size
);
532 if (data
->length
&& (data
->length
% 8) != 0)
533 p
[1] = 8 - (data
->length
% 8);
536 *size
= data_size
+ 1;
541 _heim_der_set_sort(const void *a1
, const void *a2
)
543 const struct heim_octet_string
*s1
= a1
, *s2
= a2
;
546 ret
= memcmp(s1
->data
, s2
->data
,
547 s1
->length
< s2
->length
? s1
->length
: s2
->length
);
550 return s1
->length
- s2
->length
;