5 * Copyright (c) 1997 Kungliga Tekniska Högskolan
6 * (Royal Institute of Technology, Stockholm, Sweden).
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the Institute nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #define ASN1_MAX_YEAR 2000
44 return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0);
47 static const unsigned ndays[2][12] ={
48 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
49 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
52 * This is a simplifed version of timegm(3) that doesn't accept out of
53 * bound values that timegm(3) normally accepts but those are not
54 * valid in asn1 encodings.
58 _der_timegm (struct tm *tm)
64 * See comment in _der_gmtime
66 if (tm->tm_year > ASN1_MAX_YEAR)
71 if (tm->tm_mon < 0 || tm->tm_mon > 11)
73 if (tm->tm_mday < 1 || tm->tm_mday > (int)ndays[is_leap(tm->tm_year)][tm->tm_mon])
75 if (tm->tm_hour < 0 || tm->tm_hour > 23)
77 if (tm->tm_min < 0 || tm->tm_min > 59)
79 if (tm->tm_sec < 0 || tm->tm_sec > 59)
82 for (i = 70; i < tm->tm_year; ++i)
83 res += is_leap(i) ? 366 : 365;
85 for (i = 0; i < tm->tm_mon; ++i)
86 res += ndays[is_leap(tm->tm_year)][i];
87 res += tm->tm_mday - 1;
98 _der_gmtime(time_t t, struct tm *tm)
100 time_t secday = t % (3600 * 24);
101 time_t days = t / (3600 * 24);
103 memset(tm, 0, sizeof(*tm));
105 tm->tm_sec = secday % 60;
106 tm->tm_min = (secday % 3600) / 60;
107 tm->tm_hour = (int)(secday / 3600);
110 * Refuse to calculate time ~ 2000 years into the future, this is
111 * not possible for systems where time_t is a int32_t, however,
112 * when time_t is a int64_t, that can happen, and this becomes a
115 if (days > (ASN1_MAX_YEAR * 365))
120 unsigned dayinyear = (is_leap(tm->tm_year) ? 366 : 365);
121 if (days < dayinyear)
129 unsigned daysinmonth = ndays[is_leap(tm->tm_year)][tm->tm_mon];
130 if (days < daysinmonth)
135 tm->tm_mday = (int)(days + 1);
140 * Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan
141 * (Royal Institute of Technology, Stockholm, Sweden).
142 * All rights reserved.
144 * Redistribution and use in source and binary forms, with or without
145 * modification, are permitted provided that the following conditions
148 * 1. Redistributions of source code must retain the above copyright
149 * notice, this list of conditions and the following disclaimer.
151 * 2. Redistributions in binary form must reproduce the above copyright
152 * notice, this list of conditions and the following disclaimer in the
153 * documentation and/or other materials provided with the distribution.
155 * 3. Neither the name of the Institute nor the names of its contributors
156 * may be used to endorse or promote products derived from this software
157 * without specific prior written permission.
159 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
160 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
162 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
163 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
164 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
165 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
166 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
167 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
168 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
174 * All decoding functions take a pointer `p' to first position in
175 * which to read, from the left, `len' which means the maximum number
176 * of characters we are able to read, `ret' were the value will be
177 * returned and `size' where the number of used bytes is stored.
178 * Either 0 or an error code is returned.
182 der_get_unsigned (const unsigned char *p, size_t len,
183 unsigned *ret, size_t *size)
188 if (len == sizeof(val) + 1 && p[0] == 0)
190 else if (len > sizeof(val))
194 val = val * 256 + *p++;
196 if(size) *size = oldlen;
201 der_get_unsigned64 (const unsigned char *p, size_t len,
202 uint64_t *ret, size_t *size)
207 if (len == sizeof(val) + 1 && p[0] == 0)
209 else if (len > sizeof(val))
213 val = val * 256 + *p++;
215 if(size) *size = oldlen;
220 der_get_integer (const unsigned char *p, size_t len,
221 int *ret, size_t *size)
226 if (len > sizeof(val))
230 val = (signed char)*p++;
232 val = val * 256 + *p++;
235 if(size) *size = oldlen;
240 der_get_integer64 (const unsigned char *p, size_t len,
241 int64_t *ret, size_t *size)
246 if (len > sizeof(val))
250 val = (signed char)*p++;
252 val = val * 256 + *p++;
255 if(size) *size = oldlen;
261 der_get_length (const unsigned char *p, size_t len,
262 size_t *val, size_t *size)
279 *val = ASN1_INDEFINITE;
286 e = der_get_unsigned (p, v, &tmp, &l);
289 if(size) *size = l + 1;
295 der_get_boolean(const unsigned char *p, size_t len, int *data, size_t *size)
308 der_get_general_string (const unsigned char *p, size_t len,
309 heim_general_string *str, size_t *size)
311 const unsigned char *p1;
314 p1 = memchr(p, 0, len);
317 * Allow trailing NULs. We allow this since MIT Kerberos sends
318 * an strings in the NEED_PREAUTH case that includes a
321 while ((size_t)(p1 - p) < len && *p1 == '\0')
323 if ((size_t)(p1 - p) != len) {
325 return ASN1_BAD_CHARACTER;
328 if (len == SIZE_MAX) {
330 return ASN1_BAD_LENGTH;
333 *str = s = malloc (len + 1);
339 if(size) *size = len;
344 der_get_utf8string (const unsigned char *p, size_t len,
345 heim_utf8_string *str, size_t *size)
347 return der_get_general_string(p, len, str, size);
350 #define gen_data_zero(_data) \
351 do { (_data)->length = 0; (_data)->data = NULL; } while(0)
354 der_get_printable_string(const unsigned char *p, size_t len,
355 heim_printable_string *str, size_t *size)
357 if (len == SIZE_MAX) {
359 return ASN1_BAD_LENGTH;
362 str->data = malloc(len + 1);
363 if (str->data == NULL) {
367 memcpy(str->data, p, len);
368 ((char *)str->data)[len] = '\0';
369 if(size) *size = len;
374 der_get_ia5_string(const unsigned char *p, size_t len,
375 heim_ia5_string *str, size_t *size)
377 return der_get_printable_string(p, len, str, size);
381 der_get_bmp_string (const unsigned char *p, size_t len,
382 heim_bmp_string *data, size_t *size)
388 return ASN1_BAD_FORMAT;
390 data->length = len / 2;
391 if (data->length > UINT_MAX/sizeof(data->data[0])) {
395 data->data = malloc(data->length * sizeof(data->data[0]));
396 if (data->data == NULL && data->length != 0) {
401 for (i = 0; i < data->length; i++) {
402 data->data[i] = (p[0] << 8) | p[1];
404 /* check for NUL in the middle of the string */
405 if (data->data[i] == 0 && i != (data->length - 1)) {
408 return ASN1_BAD_CHARACTER;
411 if (size) *size = len;
417 der_get_universal_string (const unsigned char *p, size_t len,
418 heim_universal_string *data, size_t *size)
424 return ASN1_BAD_FORMAT;
426 data->length = len / 4;
427 if (data->length > UINT_MAX/sizeof(data->data[0])) {
431 data->data = malloc(data->length * sizeof(data->data[0]));
432 if (data->data == NULL && data->length != 0) {
437 for (i = 0; i < data->length; i++) {
438 data->data[i] = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
440 /* check for NUL in the middle of the string */
441 if (data->data[i] == 0 && i != (data->length - 1)) {
444 return ASN1_BAD_CHARACTER;
447 if (size) *size = len;
452 der_get_visible_string (const unsigned char *p, size_t len,
453 heim_visible_string *str, size_t *size)
455 return der_get_general_string(p, len, str, size);
459 der_get_octet_string (const unsigned char *p, size_t len,
460 heim_octet_string *data, size_t *size)
463 data->data = malloc(len);
464 if (data->data == NULL && data->length != 0)
466 memcpy (data->data, p, len);
467 if(size) *size = len;
472 der_get_octet_string_ber (const unsigned char *p, size_t len,
473 heim_octet_string *data, size_t *size)
478 unsigned int tag, depth = 0;
479 size_t l, datalen, oldlen = len;
485 e = der_get_tag (p, len, &cls, &type, &tag, &l);
487 if (cls != ASN1_C_UNIV) {
491 if (type == PRIM && tag == UT_EndOfContent) {
496 if (tag != UT_OctetString) {
503 e = der_get_length (p, len, &datalen, &l);
514 ptr = realloc(data->data, data->length + datalen);
520 memcpy(((unsigned char *)data->data) + data->length, p, datalen);
521 data->length += datalen;
529 return ASN1_INDEF_OVERRUN;
530 if(size) *size = oldlen - len;
541 der_get_heim_integer (const unsigned char *p, size_t len,
542 heim_integer *data, size_t *size)
564 data->data = malloc(data->length);
565 if (data->data == NULL) {
571 q = &((unsigned char*)data->data)[data->length - 1];
572 p += data->length - 1;
573 while (q >= (unsigned char*)data->data) {
588 data->data = malloc(data->length);
589 if (data->data == NULL && data->length != 0) {
595 memcpy(data->data, p, data->length);
603 generalizedtime2time (const char *s, time_t *t)
607 memset(&tm, 0, sizeof(tm));
608 if (sscanf (s, "%04d%02d%02d%02d%02d%02dZ",
609 &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
610 &tm.tm_min, &tm.tm_sec) != 6) {
611 if (sscanf (s, "%02d%02d%02d%02d%02d%02dZ",
612 &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
613 &tm.tm_min, &tm.tm_sec) != 6)
614 return ASN1_BAD_TIMEFORMAT;
622 *t = _der_timegm (&tm);
627 der_get_time (const unsigned char *p, size_t len,
628 time_t *data, size_t *size)
633 if (len == SIZE_MAX || len == 0)
634 return ASN1_BAD_LENGTH;
636 times = malloc(len + 1);
639 memcpy(times, p, len);
641 e = generalizedtime2time(times, data);
643 if(size) *size = len;
648 der_get_generalized_time (const unsigned char *p, size_t len,
649 time_t *data, size_t *size)
651 return der_get_time(p, len, data, size);
655 der_get_utctime (const unsigned char *p, size_t len,
656 time_t *data, size_t *size)
658 return der_get_time(p, len, data, size);
662 der_get_oid (const unsigned char *p, size_t len,
663 heim_oid *data, size_t *size)
672 return ASN1_BAD_LENGTH;
674 if (len + 1 > UINT_MAX/sizeof(data->components[0]))
677 data->components = malloc((len + 1) * sizeof(data->components[0]));
678 if (data->components == NULL)
680 data->components[0] = (*p) / 40;
681 data->components[1] = (*p) % 40;
684 for (n = 2; len > 0; ++n) {
689 u1 = u * 128 + (*p++ % 128);
690 /* check that we don't overflow the element */
696 } while (len > 0 && p[-1] & 0x80);
697 data->components[n] = u;
699 if (n > 2 && p[-1] & 0x80) {
710 der_get_tag (const unsigned char *p, size_t len,
711 Der_class *cls, Der_type *type,
712 unsigned int *tag, size_t *size)
717 *cls = (Der_class)(((*p) >> 6) & 0x03);
718 *type = (Der_type)(((*p) >> 5) & 0x01);
722 unsigned int continuation;
728 continuation = *p & 128;
729 tag1 = *tag * 128 + (*p % 128);
730 /* check that we don't overflow the tag */
732 return ASN1_OVERFLOW;
735 } while(continuation);
737 if(size) *size = ret;
742 der_match_tag (const unsigned char *p, size_t len,
743 Der_class cls, Der_type type,
744 unsigned int tag, size_t *size)
749 e = der_match_tag2(p, len, cls, &thistype, tag, size);
751 if (thistype != type) return ASN1_BAD_ID;
756 der_match_tag2 (const unsigned char *p, size_t len,
757 Der_class cls, Der_type *type,
758 unsigned int tag, size_t *size)
762 unsigned int thistag;
765 e = der_get_tag (p, len, &thisclass, type, &thistag, &l);
767 if (cls != thisclass)
770 return ASN1_MISPLACED_FIELD;
772 return ASN1_MISSING_FIELD;
778 der_match_tag_and_length (const unsigned char *p, size_t len,
779 Der_class cls, Der_type *type, unsigned int tag,
780 size_t *length_ret, size_t *size)
785 e = der_match_tag2 (p, len, cls, type, tag, &l);
790 e = der_get_length (p, len, length_ret, &l);
792 if(size) *size = ret + l;
799 * Old versions of DCE was based on a very early beta of the MIT code,
800 * which used MAVROS for ASN.1 encoding. MAVROS had the interesting
801 * feature that it encoded data in the forward direction, which has
802 * it's problems, since you have no idea how long the data will be
803 * until after you're done. MAVROS solved this by reserving one byte
804 * for length, and later, if the actual length was longer, it reverted
805 * to indefinite, BER style, lengths. The version of MAVROS used by
806 * the DCE people could apparently generate correct X.509 DER encodings, and
807 * did this by making space for the length after encoding, but
808 * unfortunately this feature wasn't used with Kerberos.
812 _heim_fix_dce(size_t reallen, size_t *len)
814 if(reallen == ASN1_INDEFINITE)
823 der_get_bit_string (const unsigned char *p, size_t len,
824 heim_bit_string *data, size_t *size)
829 return ASN1_BAD_FORMAT;
830 if (len - 1 == 0 && p[0] != 0)
831 return ASN1_BAD_FORMAT;
832 /* check if any of the three upper bits are set
833 * any of them will cause a interger overrun */
834 if ((len - 1) >> (sizeof(len) * 8 - 3))
837 * If there is data to copy, do that now.
840 data->length = (len - 1) * 8;
841 data->data = malloc(len - 1);
842 if (data->data == NULL)
844 memcpy (data->data, p + 1, len - 1);
845 data->length -= p[0];
850 if(size) *size = len;
854 * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
855 * (Royal Institute of Technology, Stockholm, Sweden).
856 * All rights reserved.
858 * Redistribution and use in source and binary forms, with or without
859 * modification, are permitted provided that the following conditions
862 * 1. Redistributions of source code must retain the above copyright
863 * notice, this list of conditions and the following disclaimer.
865 * 2. Redistributions in binary form must reproduce the above copyright
866 * notice, this list of conditions and the following disclaimer in the
867 * documentation and/or other materials provided with the distribution.
869 * 3. Neither the name of the Institute nor the names of its contributors
870 * may be used to endorse or promote products derived from this software
871 * without specific prior written permission.
873 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
874 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
875 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
876 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
877 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
878 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
879 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
880 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
881 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
882 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
890 * All encoding functions take a pointer `p' to first position in
891 * which to write, from the right, `len' which means the maximum
892 * number of characters we are able to write. The function returns
893 * the number of characters written in `size' (if non-NULL).
894 * The return value is 0 or an error.
898 der_put_unsigned (unsigned char *p, size_t len, const unsigned *v, size_t *size)
900 unsigned char *base = p;
904 while (len > 0 && val) {
910 return ASN1_OVERFLOW;
914 return ASN1_OVERFLOW;
921 return ASN1_OVERFLOW;
930 der_put_unsigned64 (unsigned char *p, size_t len, const uint64_t *v, size_t *size)
932 unsigned char *base = p;
936 while (len > 0 && val) {
942 return ASN1_OVERFLOW;
946 return ASN1_OVERFLOW;
953 return ASN1_OVERFLOW;
962 der_put_integer (unsigned char *p, size_t len, const int *v, size_t *size)
964 unsigned char *base = p;
970 return ASN1_OVERFLOW;
977 return ASN1_OVERFLOW;
985 return ASN1_OVERFLOW;
992 return ASN1_OVERFLOW;
1002 der_put_integer64 (unsigned char *p, size_t len, const int64_t *v, size_t *size)
1004 unsigned char *base = p;
1010 return ASN1_OVERFLOW;
1017 return ASN1_OVERFLOW;
1025 return ASN1_OVERFLOW;
1026 *p-- = ~(val % 256);
1032 return ASN1_OVERFLOW;
1043 der_put_length (unsigned char *p, size_t len, size_t val, size_t *size)
1046 return ASN1_OVERFLOW;
1056 return ASN1_OVERFLOW;
1070 der_put_boolean(unsigned char *p, size_t len, const int *data, size_t *size)
1073 return ASN1_OVERFLOW;
1083 der_put_general_string (unsigned char *p, size_t len,
1084 const heim_general_string *str, size_t *size)
1086 size_t slen = strlen(*str);
1089 return ASN1_OVERFLOW;
1091 memcpy (p+1, *str, slen);
1097 der_put_utf8string (unsigned char *p, size_t len,
1098 const heim_utf8_string *str, size_t *size)
1100 return der_put_general_string(p, len, str, size);
1104 der_put_printable_string (unsigned char *p, size_t len,
1105 const heim_printable_string *str, size_t *size)
1107 return der_put_octet_string(p, len, str, size);
1111 der_put_ia5_string (unsigned char *p, size_t len,
1112 const heim_ia5_string *str, size_t *size)
1114 return der_put_octet_string(p, len, str, size);
1118 der_put_bmp_string (unsigned char *p, size_t len,
1119 const heim_bmp_string *data, size_t *size)
1122 if (len / 2 < data->length)
1123 return ASN1_OVERFLOW;
1124 p -= data->length * 2;
1125 for (i = 0; i < data->length; i++) {
1126 p[1] = (data->data[i] >> 8) & 0xff;
1127 p[2] = data->data[i] & 0xff;
1130 if (size) *size = data->length * 2;
1135 der_put_universal_string (unsigned char *p, size_t len,
1136 const heim_universal_string *data, size_t *size)
1139 if (len / 4 < data->length)
1140 return ASN1_OVERFLOW;
1141 p -= data->length * 4;
1142 for (i = 0; i < data->length; i++) {
1143 p[1] = (data->data[i] >> 24) & 0xff;
1144 p[2] = (data->data[i] >> 16) & 0xff;
1145 p[3] = (data->data[i] >> 8) & 0xff;
1146 p[4] = data->data[i] & 0xff;
1149 if (size) *size = data->length * 4;
1154 der_put_visible_string (unsigned char *p, size_t len,
1155 const heim_visible_string *str, size_t *size)
1157 return der_put_general_string(p, len, str, size);
1161 der_put_octet_string (unsigned char *p, size_t len,
1162 const heim_octet_string *data, size_t *size)
1164 if (len < data->length)
1165 return ASN1_OVERFLOW;
1167 memcpy (p+1, data->data, data->length);
1168 *size = data->length;
1173 der_put_heim_integer (unsigned char *p, size_t len,
1174 const heim_integer *data, size_t *size)
1176 unsigned char *buf = data->data;
1179 if (data->length == 0) {
1181 return ASN1_OVERFLOW;
1187 if (len < data->length)
1188 return ASN1_OVERFLOW;
1190 len -= data->length;
1192 if (data->negative) {
1195 for (i = data->length - 1, carry = 1; i >= 0; i--) {
1203 return ASN1_OVERFLOW;
1210 memcpy(p + 1, buf, data->length);
1214 return ASN1_OVERFLOW;
1221 *size = data->length + hibitset;
1226 der_put_generalized_time (unsigned char *p, size_t len,
1227 const time_t *data, size_t *size)
1229 heim_octet_string k;
1233 e = _heim_time2generalizedtime (*data, &k, 1);
1236 e = der_put_octet_string(p, len, &k, &l);
1246 der_put_utctime (unsigned char *p, size_t len,
1247 const time_t *data, size_t *size)
1249 heim_octet_string k;
1253 e = _heim_time2generalizedtime (*data, &k, 0);
1256 e = der_put_octet_string(p, len, &k, &l);
1266 der_put_oid (unsigned char *p, size_t len,
1267 const heim_oid *data, size_t *size)
1269 unsigned char *base = p;
1272 for (n = data->length - 1; n >= 2; --n) {
1273 unsigned u = data->components[n];
1276 return ASN1_OVERFLOW;
1282 return ASN1_OVERFLOW;
1283 *p-- = 128 + u % 128;
1289 return ASN1_OVERFLOW;
1290 *p-- = 40 * data->components[0] + data->components[1];
1296 der_put_tag (unsigned char *p, size_t len, Der_class class, Der_type type,
1297 unsigned int tag, size_t *size)
1301 return ASN1_OVERFLOW;
1302 *p = MAKE_TAG(class, type, tag);
1306 unsigned int continuation = 0;
1310 return ASN1_OVERFLOW;
1311 *p-- = tag % 128 | continuation;
1315 continuation = 0x80;
1318 return ASN1_OVERFLOW;
1319 *p-- = MAKE_TAG(class, type, 0x1f);
1327 der_put_length_and_tag (unsigned char *p, size_t len, size_t len_val,
1328 Der_class class, Der_type type,
1329 unsigned int tag, size_t *size)
1335 e = der_put_length (p, len, len_val, &l);
1341 e = der_put_tag (p, len, class, type, tag, &l);
1351 _heim_time2generalizedtime (time_t t, heim_octet_string *s, int gtimep)
1354 const size_t len = gtimep ? 15 : 13;
1358 if (_der_gmtime(t, &tm) == NULL)
1359 return ASN1_BAD_TIMEFORMAT;
1360 s->data = malloc(len + 1);
1361 if (s->data == NULL)
1365 snprintf (s->data, len + 1, "%04d%02d%02d%02d%02d%02dZ",
1366 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
1367 tm.tm_hour, tm.tm_min, tm.tm_sec);
1369 snprintf (s->data, len + 1, "%02d%02d%02d%02d%02d%02dZ",
1370 tm.tm_year % 100, tm.tm_mon + 1, tm.tm_mday,
1371 tm.tm_hour, tm.tm_min, tm.tm_sec);
1377 der_put_bit_string (unsigned char *p, size_t len,
1378 const heim_bit_string *data, size_t *size)
1380 size_t data_size = (data->length + 7) / 8;
1381 if (len < data_size + 1)
1382 return ASN1_OVERFLOW;
1385 memcpy (p+2, data->data, data_size);
1386 if (data->length && (data->length % 8) != 0)
1387 p[1] = 8 - (data->length % 8);
1390 *size = data_size + 1;
1395 _heim_der_set_sort(const void *a1, const void *a2)
1397 const heim_octet_string *s1 = a1, *s2 = a2;
1400 ret = memcmp(s1->data, s2->data,
1401 s1->length < s2->length ? s1->length : s2->length);
1404 return (int)(s1->length - s2->length);
1407 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
1408 * (Royal Institute of Technology, Stockholm, Sweden).
1409 * All rights reserved.
1411 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
1413 * Redistribution and use in source and binary forms, with or without
1414 * modification, are permitted provided that the following conditions
1417 * 1. Redistributions of source code must retain the above copyright
1418 * notice, this list of conditions and the following disclaimer.
1420 * 2. Redistributions in binary form must reproduce the above copyright
1421 * notice, this list of conditions and the following disclaimer in the
1422 * documentation and/or other materials provided with the distribution.
1424 * 3. Neither the name of the Institute nor the names of its contributors
1425 * may be used to endorse or promote products derived from this software
1426 * without specific prior written permission.
1428 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
1429 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1430 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1431 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
1432 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1433 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1434 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1435 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1436 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1437 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1442 /* RCSID("$Id$"); */
1445 der_free_general_string (heim_general_string *str)
1452 der_free_integer (int *i)
1458 der_free_integer64 (int64_t *i)
1464 der_free_unsigned (unsigned *u)
1470 der_free_unsigned64 (uint64_t *u)
1476 der_free_generalized_time(time_t *t)
1482 der_free_utctime(time_t *t)
1489 der_free_utf8string (heim_utf8_string *str)
1496 der_free_printable_string (heim_printable_string *str)
1498 der_free_octet_string(str);
1502 der_free_ia5_string (heim_ia5_string *str)
1504 der_free_octet_string(str);
1508 der_free_bmp_string (heim_bmp_string *k)
1516 der_free_universal_string (heim_universal_string *k)
1524 der_free_visible_string (heim_visible_string *str)
1531 der_free_octet_string (heim_octet_string *k)
1539 der_free_heim_integer (heim_integer *k)
1547 der_free_oid (heim_oid *k)
1549 free(k->components);
1550 k->components = NULL;
1555 der_free_bit_string (heim_bit_string *k)
1562 * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
1563 * (Royal Institute of Technology, Stockholm, Sweden).
1564 * All rights reserved.
1566 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
1568 * Redistribution and use in source and binary forms, with or without
1569 * modification, are permitted provided that the following conditions
1572 * 1. Redistributions of source code must retain the above copyright
1573 * notice, this list of conditions and the following disclaimer.
1575 * 2. Redistributions in binary form must reproduce the above copyright
1576 * notice, this list of conditions and the following disclaimer in the
1577 * documentation and/or other materials provided with the distribution.
1579 * 3. Neither the name of the Institute nor the names of its contributors
1580 * may be used to endorse or promote products derived from this software
1581 * without specific prior written permission.
1583 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
1584 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1585 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1586 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
1587 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1588 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1589 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1590 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1591 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1592 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1597 /* RCSID("$Id$"); */
1600 _heim_len_unsigned (unsigned val)
1603 int last_val_gt_128;
1607 last_val_gt_128 = (val >= 128);
1618 _heim_len_unsigned64 (uint64_t val)
1621 int last_val_gt_128;
1625 last_val_gt_128 = (val >= 128);
1636 _heim_len_int (int val)
1663 _heim_len_int64 (int64_t val)
1690 len_oid (const heim_oid *oid)
1695 for (n = 2; n < oid->length; ++n) {
1696 unsigned u = oid->components[n];
1707 der_length_len (size_t len)
1722 der_length_tag(unsigned int tag)
1736 der_length_integer (const int *data)
1738 return _heim_len_int (*data);
1742 der_length_integer64 (const int64_t *data)
1744 return _heim_len_int64 (*data);
1748 der_length_unsigned (const unsigned *data)
1750 return _heim_len_unsigned(*data);
1754 der_length_unsigned64 (const uint64_t *data)
1756 return _heim_len_unsigned64(*data);
1760 der_length_enumerated (const unsigned *data)
1762 return _heim_len_int (*data);
1766 der_length_general_string (const heim_general_string *data)
1768 return strlen(*data);
1772 der_length_utf8string (const heim_utf8_string *data)
1774 return strlen(*data);
1778 der_length_printable_string (const heim_printable_string *data)
1780 return data->length;
1784 der_length_ia5_string (const heim_ia5_string *data)
1786 return data->length;
1790 der_length_bmp_string (const heim_bmp_string *data)
1792 return data->length * 2;
1796 der_length_universal_string (const heim_universal_string *data)
1798 return data->length * 4;
1802 der_length_visible_string (const heim_visible_string *data)
1804 return strlen(*data);
1808 der_length_octet_string (const heim_octet_string *k)
1814 der_length_heim_integer (const heim_integer *k)
1819 return k->length + (((~(((unsigned char *)k->data)[0])) & 0x80) ? 0 : 1);
1821 return k->length + ((((unsigned char *)k->data)[0] & 0x80) ? 1 : 0);
1825 der_length_oid (const heim_oid *k)
1831 der_length_generalized_time (const time_t *t)
1833 heim_octet_string k;
1836 _heim_time2generalizedtime (*t, &k, 1);
1843 der_length_utctime (const time_t *t)
1845 heim_octet_string k;
1848 _heim_time2generalizedtime (*t, &k, 0);
1855 der_length_boolean (const int *k)
1861 der_length_bit_string (const heim_bit_string *k)
1863 return (k->length + 7) / 8 + 1;
1866 * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
1867 * (Royal Institute of Technology, Stockholm, Sweden).
1868 * All rights reserved.
1870 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
1872 * Redistribution and use in source and binary forms, with or without
1873 * modification, are permitted provided that the following conditions
1876 * 1. Redistributions of source code must retain the above copyright
1877 * notice, this list of conditions and the following disclaimer.
1879 * 2. Redistributions in binary form must reproduce the above copyright
1880 * notice, this list of conditions and the following disclaimer in the
1881 * documentation and/or other materials provided with the distribution.
1883 * 3. Neither the name of the Institute nor the names of its contributors
1884 * may be used to endorse or promote products derived from this software
1885 * without specific prior written permission.
1887 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
1888 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1889 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1890 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
1891 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1892 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1893 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1894 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1895 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1896 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1901 /* RCSID("$Id$"); */
1904 der_copy_general_string (const heim_general_string *from,
1905 heim_general_string *to)
1907 *to = strdup(*from);
1914 der_copy_integer (const int *from, int *to)
1921 der_copy_integer64 (const int64_t *from, int64_t *to)
1928 der_copy_unsigned (const unsigned *from, unsigned *to)
1935 der_copy_unsigned64 (const uint64_t *from, uint64_t *to)
1942 der_copy_generalized_time (const time_t *from, time_t *to)
1949 der_copy_utctime (const time_t *from, time_t *to)
1956 der_copy_utf8string (const heim_utf8_string *from, heim_utf8_string *to)
1958 return der_copy_general_string(from, to);
1962 der_copy_printable_string (const heim_printable_string *from,
1963 heim_printable_string *to)
1965 to->length = from->length;
1966 to->data = malloc(to->length + 1);
1967 if(to->data == NULL)
1969 memcpy(to->data, from->data, to->length);
1970 ((char *)to->data)[to->length] = '\0';
1975 der_copy_ia5_string (const heim_ia5_string *from,
1976 heim_ia5_string *to)
1978 return der_copy_printable_string(from, to);
1982 der_copy_bmp_string (const heim_bmp_string *from, heim_bmp_string *to)
1984 to->length = from->length;
1985 to->data = malloc(to->length * sizeof(to->data[0]));
1986 if(to->length != 0 && to->data == NULL)
1988 memcpy(to->data, from->data, to->length * sizeof(to->data[0]));
1993 der_copy_universal_string (const heim_universal_string *from,
1994 heim_universal_string *to)
1996 to->length = from->length;
1997 to->data = malloc(to->length * sizeof(to->data[0]));
1998 if(to->length != 0 && to->data == NULL)
2000 memcpy(to->data, from->data, to->length * sizeof(to->data[0]));
2005 der_copy_visible_string (const heim_visible_string *from,
2006 heim_visible_string *to)
2008 return der_copy_general_string(from, to);
2012 der_copy_octet_string (const heim_octet_string *from, heim_octet_string *to)
2014 to->length = from->length;
2015 to->data = malloc(to->length);
2016 if(to->length != 0 && to->data == NULL)
2018 memcpy(to->data, from->data, to->length);
2023 der_copy_heim_integer (const heim_integer *from, heim_integer *to)
2025 to->length = from->length;
2026 to->data = malloc(to->length);
2027 if(to->length != 0 && to->data == NULL)
2029 memcpy(to->data, from->data, to->length);
2030 to->negative = from->negative;
2035 der_copy_oid (const heim_oid *from, heim_oid *to)
2037 to->length = from->length;
2038 to->components = malloc(to->length * sizeof(*to->components));
2039 if (to->length != 0 && to->components == NULL)
2041 memcpy(to->components, from->components,
2042 to->length * sizeof(*to->components));
2047 der_copy_bit_string (const heim_bit_string *from, heim_bit_string *to)
2051 len = (from->length + 7) / 8;
2052 to->length = from->length;
2053 to->data = malloc(len);
2054 if(len != 0 && to->data == NULL)
2056 memcpy(to->data, from->data, len);