assert non-NULL ptrs before calling mem funcs
[heimdal.git] / lib / asn1 / der_put.c
blob8fbd6f3da1c0838b7802351206222b417ac4af9c
1 /*
2 * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
31 * SUCH DAMAGE.
34 #include "der_locl.h"
36 RCSID("$Id$");
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.
46 int ASN1CALL
47 der_put_unsigned (unsigned char *p, size_t len, const unsigned *v, size_t *size)
49 unsigned char *base = p;
50 unsigned val = *v;
52 *size = 0;
54 if (val) {
55 while (len > 0 && val) {
56 *p-- = val % 256;
57 val /= 256;
58 --len;
60 if (val != 0)
61 return ASN1_OVERFLOW;
62 else {
63 if(p[1] >= 128) {
64 if(len < 1)
65 return ASN1_OVERFLOW;
66 *p-- = 0;
68 *size = base - p;
69 return 0;
71 } else if (len < 1)
72 return ASN1_OVERFLOW;
73 else {
74 *p = 0;
75 *size = 1;
76 return 0;
80 int ASN1CALL
81 der_put_unsigned64 (unsigned char *p, size_t len, const uint64_t *v, size_t *size)
83 unsigned char *base = p;
84 uint64_t val = *v;
86 *size = 0;
88 if (val) {
89 while (len > 0 && val) {
90 *p-- = val % 256;
91 val /= 256;
92 --len;
94 if (val != 0)
95 return ASN1_OVERFLOW;
96 else {
97 if(p[1] >= 128) {
98 if(len < 1)
99 return ASN1_OVERFLOW;
100 *p-- = 0;
102 *size = base - p;
103 return 0;
105 } else if (len < 1)
106 return ASN1_OVERFLOW;
107 else {
108 *p = 0;
109 *size = 1;
110 return 0;
114 int ASN1CALL
115 der_put_integer (unsigned char *p, size_t len, const int *v, size_t *size)
117 unsigned char *base = p;
118 int val = *v;
120 *size = 0;
122 if(val >= 0) {
123 do {
124 if(len < 1)
125 return ASN1_OVERFLOW;
126 *p-- = val % 256;
127 len--;
128 val /= 256;
129 } while(val);
130 if(p[1] >= 128) {
131 if(len < 1)
132 return ASN1_OVERFLOW;
133 *p-- = 0;
134 len--;
136 } else {
137 val = ~val;
138 do {
139 if(len < 1)
140 return ASN1_OVERFLOW;
141 *p-- = ~(val % 256);
142 len--;
143 val /= 256;
144 } while(val);
145 if(p[1] < 128) {
146 if(len < 1)
147 return ASN1_OVERFLOW;
148 *p-- = 0xff;
149 len--;
152 *size = base - p;
153 return 0;
156 int ASN1CALL
157 der_put_integer64 (unsigned char *p, size_t len, const int64_t *v, size_t *size)
159 unsigned char *base = p;
160 int64_t val = *v;
162 *size = 0;
164 if(val >= 0) {
165 do {
166 if(len < 1)
167 return ASN1_OVERFLOW;
168 *p-- = val % 256;
169 len--;
170 val /= 256;
171 } while(val);
172 if(p[1] >= 128) {
173 if(len < 1)
174 return ASN1_OVERFLOW;
175 *p-- = 0;
176 len--;
178 } else {
179 val = ~val;
180 do {
181 if(len < 1)
182 return ASN1_OVERFLOW;
183 *p-- = ~(val % 256);
184 len--;
185 val /= 256;
186 } while(val);
187 if(p[1] < 128) {
188 if(len < 1)
189 return ASN1_OVERFLOW;
190 *p-- = 0xff;
191 len--;
194 *size = base - p;
195 return 0;
199 int ASN1CALL
200 der_put_length (unsigned char *p, size_t len, size_t val, size_t *size)
202 if (size)
203 *size = 0;
205 if (len < 1)
206 return ASN1_OVERFLOW;
208 if (val < 128) {
209 *p = val;
210 if (size)
211 *size = 1;
212 } else {
213 size_t l = 0;
215 while(val > 0) {
216 if(len < 2)
217 return ASN1_OVERFLOW;
218 *p-- = val % 256;
219 val /= 256;
220 len--;
221 l++;
223 *p = 0x80 | l;
224 if(size)
225 *size = l + 1;
227 return 0;
230 int ASN1CALL
231 der_put_boolean(unsigned char *p, size_t len, const int *data, size_t *size)
233 *size = 0;
235 if(len < 1)
236 return ASN1_OVERFLOW;
237 if(*data != 0)
238 *p = 0xff;
239 else
240 *p = 0;
241 *size = 1;
242 return 0;
245 int ASN1CALL
246 der_put_general_string (unsigned char *p, size_t len,
247 const heim_general_string *str, size_t *size)
249 size_t slen;
251 assert(p != NULL && str != NULL && *str != NULL && size != NULL);
252 *size = 0;
253 slen = strlen(*str);
254 if (len < slen)
255 return ASN1_OVERFLOW;
256 p -= slen;
257 memcpy (p+1, *str, slen);
258 *size = slen;
259 return 0;
262 int ASN1CALL
263 der_put_utf8string (unsigned char *p, size_t len,
264 const heim_utf8_string *str, size_t *size)
266 return der_put_general_string(p, len, str, size);
269 int ASN1CALL
270 der_put_printable_string (unsigned char *p, size_t len,
271 const heim_printable_string *str, size_t *size)
273 return der_put_octet_string(p, len, str, size);
276 int ASN1CALL
277 der_put_ia5_string (unsigned char *p, size_t len,
278 const heim_ia5_string *str, size_t *size)
280 return der_put_octet_string(p, len, str, size);
283 int ASN1CALL
284 der_put_bmp_string (unsigned char *p, size_t len,
285 const heim_bmp_string *data, size_t *size)
287 size_t i;
289 assert(p != NULL && data != NULL);
291 if (size)
292 *size = 0;
294 if (len / 2 < data->length)
295 return ASN1_OVERFLOW;
296 p -= data->length * 2;
297 for (i = 0; i < data->length; i++) {
298 p[1] = (data->data[i] >> 8) & 0xff;
299 p[2] = data->data[i] & 0xff;
300 p += 2;
302 if (size) *size = data->length * 2;
303 return 0;
306 int ASN1CALL
307 der_put_universal_string (unsigned char *p, size_t len,
308 const heim_universal_string *data, size_t *size)
310 size_t i;
312 if (size)
313 *size = 0;
315 if (len / 4 < data->length)
316 return ASN1_OVERFLOW;
317 p -= data->length * 4;
318 for (i = 0; i < data->length; i++) {
319 p[1] = (data->data[i] >> 24) & 0xff;
320 p[2] = (data->data[i] >> 16) & 0xff;
321 p[3] = (data->data[i] >> 8) & 0xff;
322 p[4] = data->data[i] & 0xff;
323 p += 4;
325 if (size) *size = data->length * 4;
326 return 0;
329 int ASN1CALL
330 der_put_visible_string (unsigned char *p, size_t len,
331 const heim_visible_string *str, size_t *size)
333 return der_put_general_string(p, len, str, size);
336 int ASN1CALL
337 der_put_octet_string (unsigned char *p, size_t len,
338 const heim_octet_string *data, size_t *size)
340 assert(p != NULL && data != NULL && size != NULL);
342 *size = 0;
343 if (len < data->length)
344 return ASN1_OVERFLOW;
345 p -= data->length;
346 memcpy (p+1, data->data, data->length);
347 *size = data->length;
348 return 0;
351 int ASN1CALL
352 der_put_heim_integer (unsigned char *p, size_t len,
353 const heim_integer *data, size_t *size)
355 unsigned char *buf;
356 int hibitset = 0;
358 assert(p != NULL);
360 if (size)
361 *size = 0;
363 if (data->length == 0) {
364 if (len < 1)
365 return ASN1_OVERFLOW;
366 *p-- = 0;
367 if (size)
368 *size = 1;
369 return 0;
371 if (len < data->length)
372 return ASN1_OVERFLOW;
374 assert(data->data != NULL);
375 buf = data->data;
376 len -= data->length;
378 if (data->negative) {
379 ssize_t i;
380 int carry;
381 for (i = data->length - 1, carry = 1; i >= 0; i--) {
382 *p = buf[i] ^ 0xff;
383 if (carry)
384 carry = !++*p;
385 p--;
387 if (p[1] < 128) {
388 if (len < 1)
389 return ASN1_OVERFLOW;
390 *p-- = 0xff;
391 len--;
392 hibitset = 1;
394 } else {
395 p -= data->length;
396 memcpy(p + 1, buf, data->length);
398 if (p[1] >= 128) {
399 if (len < 1)
400 return ASN1_OVERFLOW;
401 p[0] = 0;
402 len--;
403 hibitset = 1;
406 if (size)
407 *size = data->length + hibitset;
408 return 0;
411 int ASN1CALL
412 der_put_generalized_time (unsigned char *p, size_t len,
413 const time_t *data, size_t *size)
415 heim_octet_string k;
416 size_t l;
417 int e;
419 e = _heim_time2generalizedtime (*data, &k, 1);
420 if (e)
421 return e;
422 e = der_put_octet_string(p, len, &k, &l);
423 free(k.data);
424 if(e)
425 return e;
426 if(size)
427 *size = l;
428 return 0;
431 int ASN1CALL
432 der_put_utctime (unsigned char *p, size_t len,
433 const time_t *data, size_t *size)
435 heim_octet_string k;
436 size_t l;
437 int e;
439 e = _heim_time2generalizedtime (*data, &k, 0);
440 if (e)
441 return e;
442 e = der_put_octet_string(p, len, &k, &l);
443 free(k.data);
444 if(e)
445 return e;
446 if(size)
447 *size = l;
448 return 0;
451 int ASN1CALL
452 der_put_oid (unsigned char *p, size_t len,
453 const heim_oid *data, size_t *size)
455 unsigned char *base = p;
456 size_t n;
458 for (n = data->length - 1; n >= 2; --n) {
459 unsigned u = data->components[n];
461 if (len < 1)
462 return ASN1_OVERFLOW;
463 *p-- = u % 128;
464 u /= 128;
465 --len;
466 while (u > 0) {
467 if (len < 1)
468 return ASN1_OVERFLOW;
469 *p-- = 128 + u % 128;
470 u /= 128;
471 --len;
474 if (len < 1)
475 return ASN1_OVERFLOW;
476 *p-- = 40 * data->components[0] + data->components[1];
477 *size = base - p;
478 return 0;
482 * Output a copy of the DER TLV at `p' with a different outermost tag.
484 * This is used in the implementation of IMPLICIT tags in generated decoder
485 * functions.
487 int ASN1CALL
488 der_replace_tag(const unsigned char *p, size_t len,
489 unsigned char **out, size_t *outlen,
490 Der_class class, Der_type type,
491 unsigned int tag)
493 Der_class found_class;
494 Der_type found_type;
495 unsigned int found_tag;
496 size_t payload_len, l, tag_len, len_len;
497 int e;
499 assert(p != NULL && out != NULL && outlen != NULL);
501 e = der_get_tag(p, len, &found_class, &found_type, &found_tag, &l);
502 if (e)
503 return e;
504 if (found_type != type)
505 return ASN1_TYPE_MISMATCH;
506 /* We don't care what found_class and found_tag are though */
507 tag_len = der_length_tag(tag);
508 p += l;
509 len -= l;
510 e = der_get_length(p, len, &payload_len, &len_len);
511 if (e)
512 return e;
513 if (payload_len > len)
514 return ASN1_OVERFLOW;
516 * `p' now points at the payload; `*out' + the length of the tag points at
517 * where we should copy the DER length and the payload.
519 if ((*out = malloc(*outlen = tag_len + len_len + payload_len)) == NULL)
520 return ENOMEM;
521 memcpy(*out + tag_len, p, len_len + payload_len);
523 /* Put the new tag */
524 e = der_put_tag(*out + tag_len - 1, tag_len, class, type, tag, &l);
525 if (e)
526 return e;
527 if (l != tag_len)
528 return ASN1_OVERFLOW;
529 return 0;
532 #if 0
533 int ASN1CALL
534 der_encode_implicit(unsigned char *p, size_t len,
535 asn1_generic_encoder_f encoder,
536 void *obj, size_t *size,
537 Der_type type,
538 unsigned int ttag, Der_class iclass, unsigned int itag)
540 size_t ttaglen = der_length_tag(ttag);
541 size_t itaglen = der_length_tag(itag);
542 size_t l;
543 unsigned char *p2;
544 int e;
546 assert(p != NULL && size != NULL);
548 /* Attempt to encode in place */
549 e = encoder(p, len, obj, size);
550 if (e == 0) {
551 /* Fits! Rewrite tag, adjust reported size. */
552 e = der_put_tag(p + ttaglen - 1, itaglen, iclass, type, itag, &l);
553 if (e == 0) {
554 (*size) -= ttaglen;
555 (*size) += itaglen;
557 return e;
559 if (e != ASN1_OVERFLOW || itaglen <= ttaglen)
560 return e;
563 * Did not fit because ttaglen > itaglen and this was the last / only thing
564 * being encoded in a buffer of just the right size.
566 if ((p2 = malloc(len + ttaglen - itaglen)) == NULL)
567 e = ENOMEM;
568 if (e == 0)
569 e = encoder(p2 + len + ttaglen - itaglen - 1, len + ttaglen - itaglen,
570 obj, size);
571 if (e == 0)
572 e = der_put_tag(p2 + ttaglen - 1, itaglen, iclass, type, itag, &l);
573 if (e == 0) {
574 (*size) -= ttaglen;
575 (*size) += itaglen;
576 memcpy(p - *size, p2 + ttaglen - itaglen, *size);
578 free(p2);
579 return e;
581 #endif
583 int ASN1CALL
584 der_put_tag (unsigned char *p, size_t len, Der_class class, Der_type type,
585 unsigned int tag, size_t *size)
587 if (tag <= 30) {
588 if (len < 1)
589 return ASN1_OVERFLOW;
590 *p = MAKE_TAG(class, type, tag);
591 *size = 1;
592 } else {
593 size_t ret = 0;
594 unsigned int continuation = 0;
596 do {
597 if (len < 1)
598 return ASN1_OVERFLOW;
599 *p-- = tag % 128 | continuation;
600 len--;
601 ret++;
602 tag /= 128;
603 continuation = 0x80;
604 } while(tag > 0);
605 if (len < 1)
606 return ASN1_OVERFLOW;
607 *p-- = MAKE_TAG(class, type, 0x1f);
608 ret++;
609 *size = ret;
611 return 0;
614 int ASN1CALL
615 der_put_length_and_tag (unsigned char *p, size_t len, size_t len_val,
616 Der_class class, Der_type type,
617 unsigned int tag, size_t *size)
619 size_t ret = 0;
620 size_t l;
621 int e;
623 e = der_put_length (p, len, len_val, &l);
624 if(e)
625 return e;
626 p -= l;
627 len -= l;
628 ret += l;
629 e = der_put_tag (p, len, class, type, tag, &l);
630 if(e)
631 return e;
633 ret += l;
634 *size = ret;
635 return 0;
639 _heim_time2generalizedtime (time_t t, heim_octet_string *s, int gtimep)
641 struct tm tm;
642 const size_t len = gtimep ? 15 : 13;
643 int bytes;
645 s->data = NULL;
646 s->length = 0;
647 if (_der_gmtime(t, &tm) == NULL)
648 return ASN1_BAD_TIMEFORMAT;
649 s->data = malloc(len + 1);
650 if (s->data == NULL)
651 return ENOMEM;
652 s->length = len;
653 if (gtimep)
654 bytes = snprintf(s->data, len + 1, "%04d%02d%02d%02d%02d%02dZ",
655 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
656 tm.tm_hour, tm.tm_min, tm.tm_sec);
657 else
658 bytes = snprintf(s->data, len + 1, "%02d%02d%02d%02d%02d%02dZ",
659 tm.tm_year % 100, tm.tm_mon + 1, tm.tm_mday,
660 tm.tm_hour, tm.tm_min, tm.tm_sec);
662 if (bytes > len)
663 abort();
665 return 0;
668 int ASN1CALL
669 der_put_bit_string (unsigned char *p, size_t len,
670 const heim_bit_string *data, size_t *size)
672 size_t data_size;
674 assert(p != NULL && data != NULL && size != NULL);
676 *size = 0;
677 data_size = (data->length + 7) / 8;
678 if (len < data_size + 1)
679 return ASN1_OVERFLOW;
680 p -= data_size + 1;
682 memcpy (p+2, data->data, data_size);
683 if (data->length && (data->length % 8) != 0)
684 p[1] = 8 - (data->length % 8);
685 else
686 p[1] = 0;
687 *size = data_size + 1;
688 return 0;
692 _heim_der_set_sort(const void *a1, const void *a2)
694 const heim_octet_string *s1, *s2;
695 int ret;
697 assert(a1 != NULL && a2 != NULL);
698 s1 = a1;
699 s2 = a2;
700 ret = memcmp(s1->data, s2->data,
701 s1->length < s2->length ? s1->length : s2->length);
702 if (ret != 0)
703 return ret;
704 return (int)(s1->length - s2->length);