4 * Copyright (c) 1997 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
43 return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0);
47 * This is a simplifed version of timegm(3) that doesn't accept out of
48 * bound values that timegm(3) normally accepts but those are not
49 * valid in asn1 encodings.
53 _der_timegm (struct tm *tm)
55 static const unsigned ndays[2][12] ={
56 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
57 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
63 if (tm->tm_mon < 0 || tm->tm_mon > 11)
65 if (tm->tm_mday < 1 || tm->tm_mday > ndays[is_leap(tm->tm_year)][tm->tm_mon])
67 if (tm->tm_hour < 0 || tm->tm_hour > 23)
69 if (tm->tm_min < 0 || tm->tm_min > 59)
71 if (tm->tm_sec < 0 || tm->tm_sec > 59)
74 for (i = 70; i < tm->tm_year; ++i)
75 res += is_leap(i) ? 366 : 365;
77 for (i = 0; i < tm->tm_mon; ++i)
78 res += ndays[is_leap(tm->tm_year)][i];
79 res += tm->tm_mday - 1;
89 * Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan
90 * (Royal Institute of Technology, Stockholm, Sweden).
91 * All rights reserved.
93 * Redistribution and use in source and binary forms, with or without
94 * modification, are permitted provided that the following conditions
97 * 1. Redistributions of source code must retain the above copyright
98 * notice, this list of conditions and the following disclaimer.
100 * 2. Redistributions in binary form must reproduce the above copyright
101 * notice, this list of conditions and the following disclaimer in the
102 * documentation and/or other materials provided with the distribution.
104 * 3. Neither the name of the Institute nor the names of its contributors
105 * may be used to endorse or promote products derived from this software
106 * without specific prior written permission.
108 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
109 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
110 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
111 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
112 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
113 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
114 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
115 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
116 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
117 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
123 * All decoding functions take a pointer `p' to first position in
124 * which to read, from the left, `len' which means the maximum number
125 * of characters we are able to read, `ret' were the value will be
126 * returned and `size' where the number of used bytes is stored.
127 * Either 0 or an error code is returned.
131 der_get_unsigned (const unsigned char *p, size_t len,
132 unsigned *ret, size_t *size)
137 if (len == sizeof(unsigned) + 1 && p[0] == 0)
139 else if (len > sizeof(unsigned))
143 val = val * 256 + *p++;
145 if(size) *size = oldlen;
150 der_get_integer (const unsigned char *p, size_t len,
151 int *ret, size_t *size)
156 if (len > sizeof(int))
160 val = (signed char)*p++;
162 val = val * 256 + *p++;
165 if(size) *size = oldlen;
170 der_get_length (const unsigned char *p, size_t len,
171 size_t *val, size_t *size)
188 *val = ASN1_INDEFINITE;
195 e = der_get_unsigned (p, v, &tmp, &l);
198 if(size) *size = l + 1;
204 der_get_boolean(const unsigned char *p, size_t len, int *data, size_t *size)
217 der_get_general_string (const unsigned char *p, size_t len,
218 heim_general_string *str, size_t *size)
220 const unsigned char *p1;
223 p1 = memchr(p, 0, len);
226 * Allow trailing NULs. We allow this since MIT Kerberos sends
227 * an strings in the NEED_PREAUTH case that includes a
230 while (p1 - p < len && *p1 == '\0')
233 return ASN1_BAD_CHARACTER;
236 return ASN1_BAD_LENGTH;
238 s = malloc (len + 1);
244 if(size) *size = len;
249 der_get_utf8string (const unsigned char *p, size_t len,
250 heim_utf8_string *str, size_t *size)
252 return der_get_general_string(p, len, str, size);
256 der_get_printable_string (const unsigned char *p, size_t len,
257 heim_printable_string *str, size_t *size)
259 return der_get_general_string(p, len, str, size);
263 der_get_ia5_string (const unsigned char *p, size_t len,
264 heim_ia5_string *str, size_t *size)
266 return der_get_general_string(p, len, str, size);
270 der_get_bmp_string (const unsigned char *p, size_t len,
271 heim_bmp_string *data, size_t *size)
276 return ASN1_BAD_FORMAT;
277 data->length = len / 2;
278 if (data->length > UINT_MAX/sizeof(data->data[0]))
280 data->data = malloc(data->length * sizeof(data->data[0]));
281 if (data->data == NULL && data->length != 0)
284 for (i = 0; i < data->length; i++) {
285 data->data[i] = (p[0] << 8) | p[1];
287 /* check for NUL in the middle of the string */
288 if (data->data[i] == 0 && i != (data->length - 1)) {
292 return ASN1_BAD_CHARACTER;
295 if (size) *size = len;
301 der_get_universal_string (const unsigned char *p, size_t len,
302 heim_universal_string *data, size_t *size)
307 return ASN1_BAD_FORMAT;
308 data->length = len / 4;
309 if (data->length > UINT_MAX/sizeof(data->data[0]))
311 data->data = malloc(data->length * sizeof(data->data[0]));
312 if (data->data == NULL && data->length != 0)
315 for (i = 0; i < data->length; i++) {
316 data->data[i] = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
318 /* check for NUL in the middle of the string */
319 if (data->data[i] == 0 && i != (data->length - 1)) {
323 return ASN1_BAD_CHARACTER;
326 if (size) *size = len;
331 der_get_visible_string (const unsigned char *p, size_t len,
332 heim_visible_string *str, size_t *size)
334 return der_get_general_string(p, len, str, size);
338 der_get_octet_string (const unsigned char *p, size_t len,
339 heim_octet_string *data, size_t *size)
342 data->data = malloc(len);
343 if (data->data == NULL && data->length != 0)
345 memcpy (data->data, p, len);
346 if(size) *size = len;
351 der_get_octet_string_ber (const unsigned char *p, size_t len,
352 heim_octet_string *data, size_t *size)
357 unsigned int tag, depth = 0;
358 size_t l, datalen, oldlen = len;
364 e = der_get_tag (p, len, &class, &type, &tag, &l);
366 if (class != ASN1_C_UNIV) {
370 if (type == PRIM && tag == UT_EndOfContent) {
375 if (tag != UT_OctetString) {
382 e = der_get_length (p, len, &datalen, &l);
393 ptr = realloc(data->data, data->length + datalen);
399 memcpy(((unsigned char *)data->data) + data->length, p, datalen);
400 data->length += datalen;
408 return ASN1_INDEF_OVERRUN;
409 if(size) *size = oldlen - len;
420 der_get_heim_integer (const unsigned char *p, size_t len,
421 heim_integer *data, size_t *size)
443 data->data = malloc(data->length);
444 if (data->data == NULL) {
450 q = &((unsigned char*)data->data)[data->length - 1];
451 p += data->length - 1;
452 while (q >= (unsigned char*)data->data) {
467 data->data = malloc(data->length);
468 if (data->data == NULL && data->length != 0) {
474 memcpy(data->data, p, data->length);
482 generalizedtime2time (const char *s, time_t *t)
486 memset(&tm, 0, sizeof(tm));
487 if (sscanf (s, "%04d%02d%02d%02d%02d%02dZ",
488 &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
489 &tm.tm_min, &tm.tm_sec) != 6) {
490 if (sscanf (s, "%02d%02d%02d%02d%02d%02dZ",
491 &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
492 &tm.tm_min, &tm.tm_sec) != 6)
493 return ASN1_BAD_TIMEFORMAT;
501 *t = _der_timegm (&tm);
506 der_get_time (const unsigned char *p, size_t len,
507 time_t *data, size_t *size)
512 if (len > len + 1 || len == 0)
513 return ASN1_BAD_LENGTH;
515 times = malloc(len + 1);
518 memcpy(times, p, len);
520 e = generalizedtime2time(times, data);
522 if(size) *size = len;
527 der_get_generalized_time (const unsigned char *p, size_t len,
528 time_t *data, size_t *size)
530 return der_get_time(p, len, data, size);
534 der_get_utctime (const unsigned char *p, size_t len,
535 time_t *data, size_t *size)
537 return der_get_time(p, len, data, size);
541 der_get_oid (const unsigned char *p, size_t len,
542 heim_oid *data, size_t *size)
551 return ASN1_BAD_LENGTH;
553 if (len + 1 > UINT_MAX/sizeof(data->components[0]))
556 data->components = malloc((len + 1) * sizeof(data->components[0]));
557 if (data->components == NULL)
559 data->components[0] = (*p) / 40;
560 data->components[1] = (*p) % 40;
563 for (n = 2; len > 0; ++n) {
568 u1 = u * 128 + (*p++ % 128);
569 /* check that we don't overflow the element */
575 } while (len > 0 && p[-1] & 0x80);
576 data->components[n] = u;
578 if (n > 2 && p[-1] & 0x80) {
589 der_get_tag (const unsigned char *p, size_t len,
590 Der_class *class, Der_type *type,
591 unsigned int *tag, size_t *size)
596 *class = (Der_class)(((*p) >> 6) & 0x03);
597 *type = (Der_type)(((*p) >> 5) & 0x01);
601 unsigned int continuation;
607 continuation = *p & 128;
608 tag1 = *tag * 128 + (*p % 128);
609 /* check that we don't overflow the tag */
611 return ASN1_OVERFLOW;
614 } while(continuation);
616 if(size) *size = ret;
621 der_match_tag (const unsigned char *p, size_t len,
622 Der_class class, Der_type type,
623 unsigned int tag, size_t *size)
628 e = der_match_tag2(p, len, class, &thistype, tag, size);
630 if (thistype != type) return ASN1_BAD_ID;
635 der_match_tag2 (const unsigned char *p, size_t len,
636 Der_class class, Der_type *type,
637 unsigned int tag, size_t *size)
641 unsigned int thistag;
644 e = der_get_tag (p, len, &thisclass, type, &thistag, &l);
646 if (class != thisclass)
649 return ASN1_MISPLACED_FIELD;
651 return ASN1_MISSING_FIELD;
657 der_match_tag_and_length (const unsigned char *p, size_t len,
658 Der_class class, Der_type *type, unsigned int tag,
659 size_t *length_ret, size_t *size)
664 e = der_match_tag2 (p, len, class, type, tag, &l);
669 e = der_get_length (p, len, length_ret, &l);
671 if(size) *size = ret + l;
678 * Old versions of DCE was based on a very early beta of the MIT code,
679 * which used MAVROS for ASN.1 encoding. MAVROS had the interesting
680 * feature that it encoded data in the forward direction, which has
681 * it's problems, since you have no idea how long the data will be
682 * until after you're done. MAVROS solved this by reserving one byte
683 * for length, and later, if the actual length was longer, it reverted
684 * to indefinite, BER style, lengths. The version of MAVROS used by
685 * the DCE people could apparently generate correct X.509 DER encodings, and
686 * did this by making space for the length after encoding, but
687 * unfortunately this feature wasn't used with Kerberos.
691 _heim_fix_dce(size_t reallen, size_t *len)
693 if(reallen == ASN1_INDEFINITE)
702 der_get_bit_string (const unsigned char *p, size_t len,
703 heim_bit_string *data, size_t *size)
708 return ASN1_BAD_FORMAT;
709 if (len - 1 == 0 && p[0] != 0)
710 return ASN1_BAD_FORMAT;
711 /* check if any of the three upper bits are set
712 * any of them will cause a interger overrun */
713 if ((len - 1) >> (sizeof(len) * 8 - 3))
715 data->length = (len - 1) * 8;
716 data->data = malloc(len - 1);
717 if (data->data == NULL && (len - 1) != 0)
719 /* copy data is there is data to copy */
721 memcpy (data->data, p + 1, len - 1);
722 data->length -= p[0];
724 if(size) *size = len;
728 * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
729 * (Royal Institute of Technology, Stockholm, Sweden).
730 * All rights reserved.
732 * Redistribution and use in source and binary forms, with or without
733 * modification, are permitted provided that the following conditions
736 * 1. Redistributions of source code must retain the above copyright
737 * notice, this list of conditions and the following disclaimer.
739 * 2. Redistributions in binary form must reproduce the above copyright
740 * notice, this list of conditions and the following disclaimer in the
741 * documentation and/or other materials provided with the distribution.
743 * 3. Neither the name of the Institute nor the names of its contributors
744 * may be used to endorse or promote products derived from this software
745 * without specific prior written permission.
747 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
748 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
749 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
750 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
751 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
752 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
753 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
754 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
755 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
756 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
764 * All encoding functions take a pointer `p' to first position in
765 * which to write, from the right, `len' which means the maximum
766 * number of characters we are able to write. The function returns
767 * the number of characters written in `size' (if non-NULL).
768 * The return value is 0 or an error.
772 der_put_unsigned (unsigned char *p, size_t len, const unsigned *v, size_t *size)
774 unsigned char *base = p;
778 while (len > 0 && val) {
784 return ASN1_OVERFLOW;
788 return ASN1_OVERFLOW;
795 return ASN1_OVERFLOW;
804 der_put_integer (unsigned char *p, size_t len, const int *v, size_t *size)
806 unsigned char *base = p;
812 return ASN1_OVERFLOW;
819 return ASN1_OVERFLOW;
827 return ASN1_OVERFLOW;
834 return ASN1_OVERFLOW;
845 der_put_length (unsigned char *p, size_t len, size_t val, size_t *size)
848 return ASN1_OVERFLOW;
858 return ASN1_OVERFLOW;
872 der_put_boolean(unsigned char *p, size_t len, const int *data, size_t *size)
875 return ASN1_OVERFLOW;
885 der_put_general_string (unsigned char *p, size_t len,
886 const heim_general_string *str, size_t *size)
888 size_t slen = strlen(*str);
891 return ASN1_OVERFLOW;
893 memcpy (p+1, *str, slen);
899 der_put_utf8string (unsigned char *p, size_t len,
900 const heim_utf8_string *str, size_t *size)
902 return der_put_general_string(p, len, str, size);
906 der_put_printable_string (unsigned char *p, size_t len,
907 const heim_printable_string *str, size_t *size)
909 return der_put_general_string(p, len, str, size);
913 der_put_ia5_string (unsigned char *p, size_t len,
914 const heim_ia5_string *str, size_t *size)
916 return der_put_general_string(p, len, str, size);
920 der_put_bmp_string (unsigned char *p, size_t len,
921 const heim_bmp_string *data, size_t *size)
924 if (len / 2 < data->length)
925 return ASN1_OVERFLOW;
926 p -= data->length * 2;
927 for (i = 0; i < data->length; i++) {
928 p[1] = (data->data[i] >> 8) & 0xff;
929 p[2] = data->data[i] & 0xff;
932 if (size) *size = data->length * 2;
937 der_put_universal_string (unsigned char *p, size_t len,
938 const heim_universal_string *data, size_t *size)
941 if (len / 4 < data->length)
942 return ASN1_OVERFLOW;
943 p -= data->length * 4;
944 for (i = 0; i < data->length; i++) {
945 p[1] = (data->data[i] >> 24) & 0xff;
946 p[2] = (data->data[i] >> 16) & 0xff;
947 p[3] = (data->data[i] >> 8) & 0xff;
948 p[4] = data->data[i] & 0xff;
951 if (size) *size = data->length * 4;
956 der_put_visible_string (unsigned char *p, size_t len,
957 const heim_visible_string *str, size_t *size)
959 return der_put_general_string(p, len, str, size);
963 der_put_octet_string (unsigned char *p, size_t len,
964 const heim_octet_string *data, size_t *size)
966 if (len < data->length)
967 return ASN1_OVERFLOW;
969 memcpy (p+1, data->data, data->length);
970 *size = data->length;
975 der_put_heim_integer (unsigned char *p, size_t len,
976 const heim_integer *data, size_t *size)
978 unsigned char *buf = data->data;
981 if (data->length == 0) {
983 return ASN1_OVERFLOW;
989 if (len < data->length)
990 return ASN1_OVERFLOW;
994 if (data->negative) {
996 for (i = data->length - 1, carry = 1; i >= 0; i--) {
1004 return ASN1_OVERFLOW;
1011 memcpy(p + 1, buf, data->length);
1015 return ASN1_OVERFLOW;
1022 *size = data->length + hibitset;
1027 der_put_generalized_time (unsigned char *p, size_t len,
1028 const time_t *data, size_t *size)
1030 heim_octet_string k;
1034 e = _heim_time2generalizedtime (*data, &k, 1);
1037 e = der_put_octet_string(p, len, &k, &l);
1047 der_put_utctime (unsigned char *p, size_t len,
1048 const time_t *data, size_t *size)
1050 heim_octet_string k;
1054 e = _heim_time2generalizedtime (*data, &k, 0);
1057 e = der_put_octet_string(p, len, &k, &l);
1067 der_put_oid (unsigned char *p, size_t len,
1068 const heim_oid *data, size_t *size)
1070 unsigned char *base = p;
1073 for (n = data->length - 1; n >= 2; --n) {
1074 unsigned u = data->components[n];
1077 return ASN1_OVERFLOW;
1083 return ASN1_OVERFLOW;
1084 *p-- = 128 + u % 128;
1090 return ASN1_OVERFLOW;
1091 *p-- = 40 * data->components[0] + data->components[1];
1097 der_put_tag (unsigned char *p, size_t len, Der_class class, Der_type type,
1098 unsigned int tag, size_t *size)
1102 return ASN1_OVERFLOW;
1103 *p = MAKE_TAG(class, type, tag);
1107 unsigned int continuation = 0;
1111 return ASN1_OVERFLOW;
1112 *p-- = tag % 128 | continuation;
1116 continuation = 0x80;
1119 return ASN1_OVERFLOW;
1120 *p-- = MAKE_TAG(class, type, 0x1f);
1128 der_put_length_and_tag (unsigned char *p, size_t len, size_t len_val,
1129 Der_class class, Der_type type,
1130 unsigned int tag, size_t *size)
1136 e = der_put_length (p, len, len_val, &l);
1142 e = der_put_tag (p, len, class, type, tag, &l);
1152 _heim_time2generalizedtime (time_t t, heim_octet_string *s, int gtimep)
1155 const size_t len = gtimep ? 15 : 13;
1157 s->data = malloc(len + 1);
1158 if (s->data == NULL)
1163 snprintf (s->data, len + 1, "%04d%02d%02d%02d%02d%02dZ",
1164 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
1165 tm->tm_hour, tm->tm_min, tm->tm_sec);
1167 snprintf (s->data, len + 1, "%02d%02d%02d%02d%02d%02dZ",
1168 tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday,
1169 tm->tm_hour, tm->tm_min, tm->tm_sec);
1175 der_put_bit_string (unsigned char *p, size_t len,
1176 const heim_bit_string *data, size_t *size)
1178 size_t data_size = (data->length + 7) / 8;
1179 if (len < data_size + 1)
1180 return ASN1_OVERFLOW;
1183 memcpy (p+2, data->data, data_size);
1184 if (data->length && (data->length % 8) != 0)
1185 p[1] = 8 - (data->length % 8);
1188 *size = data_size + 1;
1193 _heim_der_set_sort(const void *a1, const void *a2)
1195 const struct heim_octet_string *s1 = a1, *s2 = a2;
1198 ret = memcmp(s1->data, s2->data,
1199 s1->length < s2->length ? s1->length : s2->length);
1202 return s1->length - s2->length;
1205 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
1206 * (Royal Institute of Technology, Stockholm, Sweden).
1207 * All rights reserved.
1209 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
1211 * Redistribution and use in source and binary forms, with or without
1212 * modification, are permitted provided that the following conditions
1215 * 1. Redistributions of source code must retain the above copyright
1216 * notice, this list of conditions and the following disclaimer.
1218 * 2. Redistributions in binary form must reproduce the above copyright
1219 * notice, this list of conditions and the following disclaimer in the
1220 * documentation and/or other materials provided with the distribution.
1222 * 3. Neither the name of the Institute nor the names of its contributors
1223 * may be used to endorse or promote products derived from this software
1224 * without specific prior written permission.
1226 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
1227 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1228 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1229 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
1230 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1231 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1232 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1233 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1234 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1235 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1240 /* RCSID("$Id$"); */
1243 der_free_general_string (heim_general_string *str)
1250 der_free_integer (int *i)
1256 der_free_unsigned (unsigned *u)
1262 der_free_generalized_time(time_t *t)
1268 der_free_utctime(time_t *t)
1275 der_free_utf8string (heim_utf8_string *str)
1282 der_free_printable_string (heim_printable_string *str)
1289 der_free_ia5_string (heim_ia5_string *str)
1296 der_free_bmp_string (heim_bmp_string *k)
1304 der_free_universal_string (heim_universal_string *k)
1312 der_free_visible_string (heim_visible_string *str)
1319 der_free_octet_string (heim_octet_string *k)
1327 der_free_heim_integer (heim_integer *k)
1335 der_free_oid (heim_oid *k)
1337 free(k->components);
1338 k->components = NULL;
1343 der_free_bit_string (heim_bit_string *k)
1350 * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
1351 * (Royal Institute of Technology, Stockholm, Sweden).
1352 * All rights reserved.
1354 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
1356 * Redistribution and use in source and binary forms, with or without
1357 * modification, are permitted provided that the following conditions
1360 * 1. Redistributions of source code must retain the above copyright
1361 * notice, this list of conditions and the following disclaimer.
1363 * 2. Redistributions in binary form must reproduce the above copyright
1364 * notice, this list of conditions and the following disclaimer in the
1365 * documentation and/or other materials provided with the distribution.
1367 * 3. Neither the name of the Institute nor the names of its contributors
1368 * may be used to endorse or promote products derived from this software
1369 * without specific prior written permission.
1371 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
1372 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1373 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1374 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
1375 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1376 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1377 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1378 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1379 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1380 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1385 /* RCSID("$Id$"); */
1388 _heim_len_unsigned (unsigned val)
1391 int last_val_gt_128;
1395 last_val_gt_128 = (val >= 128);
1406 _heim_len_int (int val)
1433 len_oid (const heim_oid *oid)
1438 for (n = 2; n < oid->length; ++n) {
1439 unsigned u = oid->components[n];
1450 der_length_len (size_t len)
1465 der_length_tag(unsigned int tag)
1479 der_length_integer (const int *data)
1481 return _heim_len_int (*data);
1485 der_length_unsigned (const unsigned *data)
1487 return _heim_len_unsigned(*data);
1491 der_length_enumerated (const unsigned *data)
1493 return _heim_len_int (*data);
1497 der_length_general_string (const heim_general_string *data)
1499 return strlen(*data);
1503 der_length_utf8string (const heim_utf8_string *data)
1505 return strlen(*data);
1509 der_length_printable_string (const heim_printable_string *data)
1511 return strlen(*data);
1515 der_length_ia5_string (const heim_ia5_string *data)
1517 return strlen(*data);
1521 der_length_bmp_string (const heim_bmp_string *data)
1523 return data->length * 2;
1527 der_length_universal_string (const heim_universal_string *data)
1529 return data->length * 4;
1533 der_length_visible_string (const heim_visible_string *data)
1535 return strlen(*data);
1539 der_length_octet_string (const heim_octet_string *k)
1545 der_length_heim_integer (const heim_integer *k)
1550 return k->length + (((~(((unsigned char *)k->data)[0])) & 0x80) ? 0 : 1);
1552 return k->length + ((((unsigned char *)k->data)[0] & 0x80) ? 1 : 0);
1556 der_length_oid (const heim_oid *k)
1562 der_length_generalized_time (const time_t *t)
1564 heim_octet_string k;
1567 _heim_time2generalizedtime (*t, &k, 1);
1574 der_length_utctime (const time_t *t)
1576 heim_octet_string k;
1579 _heim_time2generalizedtime (*t, &k, 0);
1586 der_length_boolean (const int *k)
1592 der_length_bit_string (const heim_bit_string *k)
1594 return (k->length + 7) / 8 + 1;
1597 * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
1598 * (Royal Institute of Technology, Stockholm, Sweden).
1599 * All rights reserved.
1601 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
1603 * Redistribution and use in source and binary forms, with or without
1604 * modification, are permitted provided that the following conditions
1607 * 1. Redistributions of source code must retain the above copyright
1608 * notice, this list of conditions and the following disclaimer.
1610 * 2. Redistributions in binary form must reproduce the above copyright
1611 * notice, this list of conditions and the following disclaimer in the
1612 * documentation and/or other materials provided with the distribution.
1614 * 3. Neither the name of the Institute nor the names of its contributors
1615 * may be used to endorse or promote products derived from this software
1616 * without specific prior written permission.
1618 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
1619 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1620 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1621 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
1622 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1623 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1624 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1625 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1626 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1627 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1632 /* RCSID("$Id$"); */
1635 der_copy_general_string (const heim_general_string *from,
1636 heim_general_string *to)
1638 *to = strdup(*from);
1645 der_copy_integer (const int *from, int *to)
1652 der_copy_unsigned (const unsigned *from, unsigned *to)
1659 der_copy_generalized_time (const time_t *from, time_t *to)
1666 der_copy_utctime (const time_t *from, time_t *to)
1673 der_copy_utf8string (const heim_utf8_string *from, heim_utf8_string *to)
1675 return der_copy_general_string(from, to);
1679 der_copy_printable_string (const heim_printable_string *from,
1680 heim_printable_string *to)
1682 return der_copy_general_string(from, to);
1686 der_copy_ia5_string (const heim_printable_string *from,
1687 heim_printable_string *to)
1689 return der_copy_general_string(from, to);
1693 der_copy_bmp_string (const heim_bmp_string *from, heim_bmp_string *to)
1695 to->length = from->length;
1696 to->data = malloc(to->length * sizeof(to->data[0]));
1697 if(to->length != 0 && to->data == NULL)
1699 memcpy(to->data, from->data, to->length * sizeof(to->data[0]));
1704 der_copy_universal_string (const heim_universal_string *from,
1705 heim_universal_string *to)
1707 to->length = from->length;
1708 to->data = malloc(to->length * sizeof(to->data[0]));
1709 if(to->length != 0 && to->data == NULL)
1711 memcpy(to->data, from->data, to->length * sizeof(to->data[0]));
1716 der_copy_visible_string (const heim_visible_string *from,
1717 heim_visible_string *to)
1719 return der_copy_general_string(from, to);
1723 der_copy_octet_string (const heim_octet_string *from, heim_octet_string *to)
1725 to->length = from->length;
1726 to->data = malloc(to->length);
1727 if(to->length != 0 && to->data == NULL)
1729 memcpy(to->data, from->data, to->length);
1734 der_copy_heim_integer (const heim_integer *from, heim_integer *to)
1736 to->length = from->length;
1737 to->data = malloc(to->length);
1738 if(to->length != 0 && to->data == NULL)
1740 memcpy(to->data, from->data, to->length);
1741 to->negative = from->negative;
1746 der_copy_oid (const heim_oid *from, heim_oid *to)
1748 to->length = from->length;
1749 to->components = malloc(to->length * sizeof(*to->components));
1750 if (to->length != 0 && to->components == NULL)
1752 memcpy(to->components, from->components,
1753 to->length * sizeof(*to->components));
1758 der_copy_bit_string (const heim_bit_string *from, heim_bit_string *to)
1762 len = (from->length + 7) / 8;
1763 to->length = from->length;
1764 to->data = malloc(len);
1765 if(len != 0 && to->data == NULL)
1767 memcpy(to->data, from->data, len);