4 * Copyright (c) 1997 - 2002 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
37 /* RCSID("Heimdal: der_get.c,v 1.33 2002/09/03 16:21:49 nectar Exp $"); */
41 * All decoding functions take a pointer `p' to first position in
42 * which to read, from the left, `len' which means the maximum number
43 * of characters we are able to read, `ret' were the value will be
44 * returned and `size' where the number of used bytes is stored.
45 * Either 0 or an error code is returned.
49 der_get_unsigned(const unsigned char *p, size_t len, unsigned *ret,
56 val = val * 256 + *p++;
64 der_get_int(const unsigned char *p, size_t len, int *ret, size_t * size)
70 val = (signed char)*p++;
72 val = val * 256 + *p++;
81 der_get_length(const unsigned char *p, size_t len, size_t * val,
100 *val = ASN1_INDEFINITE;
108 e = der_get_unsigned(p, v, &tmp, &l);
119 der_get_general_string(const unsigned char *p, size_t len,
120 general_string * str, size_t * size)
136 der_get_octet_string(const unsigned char *p, size_t len, octet_string * data,
140 data->data = malloc(len);
141 if (data->data == NULL && data->length != 0)
143 memcpy(data->data, p, len);
150 der_get_oid(const unsigned char *p, size_t len, oid * data, size_t * size)
158 data->components = malloc(len * sizeof(*data->components));
159 if (data->components == NULL && len != 0)
161 data->components[0] = (*p) / 40;
162 data->components[1] = (*p) % 40;
165 for (n = 2; len > 0; ++n) {
170 u = u * 128 + (*p++ % 128);
171 } while (len > 0 && p[-1] & 0x80);
172 data->components[n] = u;
185 der_get_tag(const unsigned char *p, size_t len, Der_class * class,
186 Der_type * type, int *tag, size_t * size)
190 *class = (Der_class) (((*p) >> 6) & 0x03);
191 *type = (Der_type) (((*p) >> 5) & 0x01);
199 der_match_tag(const unsigned char *p, size_t len, Der_class class,
200 Der_type type, int tag, size_t * size)
208 e = der_get_tag(p, len, &thisclass, &thistype, &thistag, &l);
211 if (class != thisclass || type != thistype)
214 return ASN1_MISPLACED_FIELD;
216 return ASN1_MISSING_FIELD;
223 der_match_tag_and_length(const unsigned char *p, size_t len, Der_class class,
224 Der_type type, int tag, size_t * length_ret,
230 e = der_match_tag(p, len, class, type, tag, &l);
236 e = der_get_length(p, len, length_ret, &l);
248 decode_integer(const unsigned char *p, size_t len, int *num, size_t * size)
254 e = der_match_tag(p, len, UNIV, PRIM, UT_Integer, &l);
260 e = der_get_length(p, len, &reallen, &l);
268 e = der_get_int(p, reallen, num, &l);
280 decode_unsigned(const unsigned char *p, size_t len, unsigned *num,
287 e = der_match_tag(p, len, UNIV, PRIM, UT_Integer, &l);
293 e = der_get_length(p, len, &reallen, &l);
301 e = der_get_unsigned(p, reallen, num, &l);
313 decode_enumerated(const unsigned char *p, size_t len, unsigned *num,
320 e = der_match_tag(p, len, UNIV, PRIM, UT_Enumerated, &l);
326 e = der_get_length(p, len, &reallen, &l);
332 e = der_get_unsigned(p, reallen, num, &l);
344 decode_general_string(const unsigned char *p, size_t len,
345 general_string * str, size_t * size)
352 e = der_match_tag(p, len, UNIV, PRIM, UT_GeneralString, &l);
359 e = der_get_length(p, len, &slen, &l);
368 e = der_get_general_string(p, slen, str, &l);
380 decode_octet_string(const unsigned char *p, size_t len, octet_string * k,
388 e = der_match_tag(p, len, UNIV, PRIM, UT_OctetString, &l);
395 e = der_get_length(p, len, &slen, &l);
404 e = der_get_octet_string(p, slen, k, &l);
416 decode_oid(const unsigned char *p, size_t len, oid * k, size_t * size)
423 e = der_match_tag(p, len, UNIV, PRIM, UT_OID, &l);
430 e = der_get_length(p, len, &slen, &l);
439 e = der_get_oid(p, slen, k, &l);
451 generalizedtime2time(const char *s, time_t * t)
455 memset(&tm, 0, sizeof(tm));
456 sscanf(s, "%04d%02d%02d%02d%02d%02dZ", &tm.tm_year, &tm.tm_mon,
457 &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
464 decode_generalized_time(const unsigned char *p, size_t len, time_t * t,
474 e = der_match_tag(p, len, UNIV, PRIM, UT_GeneralizedTime, &l);
481 e = der_get_length(p, len, &slen, &l);
489 e = der_get_octet_string(p, slen, &k, &l);
495 times = realloc(k.data, k.length + 1);
501 generalizedtime2time(times, t);
510 fix_dce(size_t reallen, size_t * len)
512 if (reallen == ASN1_INDEFINITE)
521 * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
522 * (Royal Institute of Technology, Stockholm, Sweden).
523 * All rights reserved.
525 * Redistribution and use in source and binary forms, with or without
526 * modification, are permitted provided that the following conditions
529 * 1. Redistributions of source code must retain the above copyright
530 * notice, this list of conditions and the following disclaimer.
532 * 2. Redistributions in binary form must reproduce the above copyright
533 * notice, this list of conditions and the following disclaimer in the
534 * documentation and/or other materials provided with the distribution.
536 * 3. Neither the name of the Institute nor the names of its contributors
537 * may be used to endorse or promote products derived from this software
538 * without specific prior written permission.
540 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
541 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
542 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
543 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
544 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
545 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
546 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
547 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
548 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
549 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
554 /* RCSID("Heimdal: der_put.c,v 1.27 2001/09/25 23:37:25 assar Exp $"); */
557 * All encoding functions take a pointer `p' to first position in
558 * which to write, from the right, `len' which means the maximum
559 * number of characters we are able to write. The function returns
560 * the number of characters written in `size' (if non-NULL).
561 * The return value is 0 or an error.
565 der_put_unsigned(unsigned char *p, size_t len, unsigned val, size_t * size)
567 unsigned char *base = p;
570 while (len > 0 && val) {
576 return ASN1_OVERFLOW;
582 return ASN1_OVERFLOW;
591 der_put_int(unsigned char *p, size_t len, int val, size_t * size)
593 unsigned char *base = p;
598 return ASN1_OVERFLOW;
605 return ASN1_OVERFLOW;
613 return ASN1_OVERFLOW;
620 return ASN1_OVERFLOW;
631 der_put_length(unsigned char *p, size_t len, size_t val, size_t * size)
634 return ASN1_OVERFLOW;
643 e = der_put_unsigned(p, len - 1, val, &l);
654 der_put_general_string(unsigned char *p, size_t len,
655 const general_string * str, size_t * size)
657 size_t slen = strlen(*str);
660 return ASN1_OVERFLOW;
663 memcpy(p + 1, *str, slen);
669 der_put_octet_string(unsigned char *p, size_t len, const octet_string * data,
672 if (len < data->length)
673 return ASN1_OVERFLOW;
676 memcpy(p + 1, data->data, data->length);
677 *size = data->length;
682 der_put_oid(unsigned char *p, size_t len, const oid * data, size_t * size)
684 unsigned char *base = p;
687 for (n = data->length - 1; n >= 2; --n) {
688 unsigned u = data->components[n];
691 return ASN1_OVERFLOW;
697 return ASN1_OVERFLOW;
698 *p-- = 128 + u % 128;
704 return ASN1_OVERFLOW;
705 *p-- = 40 * data->components[0] + data->components[1];
711 der_put_tag(unsigned char *p, size_t len, Der_class class, Der_type type,
712 int tag, size_t * size)
715 return ASN1_OVERFLOW;
716 *p = (class << 6) | (type << 5) | tag; /* XXX */
722 der_put_length_and_tag(unsigned char *p, size_t len, size_t len_val,
723 Der_class class, Der_type type, int tag, size_t * size)
729 e = der_put_length(p, len, len_val, &l);
735 e = der_put_tag(p, len, class, type, tag, &l);
746 encode_integer(unsigned char *p, size_t len, const int *data, size_t * size)
753 e = der_put_int(p, len, num, &l);
759 e = der_put_length_and_tag(p, len, l, UNIV, PRIM, UT_Integer, &l);
770 encode_unsigned(unsigned char *p, size_t len, const unsigned *data,
773 unsigned num = *data;
778 e = der_put_unsigned(p, len, num, &l);
784 e = der_put_length_and_tag(p, len, l, UNIV, PRIM, UT_Integer, &l);
795 encode_enumerated(unsigned char *p, size_t len, const unsigned *data,
798 unsigned num = *data;
803 e = der_put_int(p, len, num, &l);
809 e = der_put_length_and_tag(p, len, l, UNIV, PRIM, UT_Enumerated, &l);
820 encode_general_string(unsigned char *p, size_t len,
821 const general_string * data, size_t * size)
827 e = der_put_general_string(p, len, data, &l);
833 e = der_put_length_and_tag(p, len, l, UNIV, PRIM, UT_GeneralString, &l);
844 encode_octet_string(unsigned char *p, size_t len, const octet_string * k,
851 e = der_put_octet_string(p, len, k, &l);
857 e = der_put_length_and_tag(p, len, l, UNIV, PRIM, UT_OctetString, &l);
868 encode_oid(unsigned char *p, size_t len, const oid * k, size_t * size)
874 e = der_put_oid(p, len, k, &l);
880 e = der_put_length_and_tag(p, len, l, UNIV, PRIM, UT_OID, &l);
891 time2generalizedtime(time_t t, octet_string * s)
895 s->data = malloc(16);
900 sprintf(s->data, "%04d%02d%02d%02d%02d%02dZ", tm->tm_year + 1900,
901 tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
906 encode_generalized_time(unsigned char *p, size_t len, const time_t * t,
914 e = time2generalizedtime(*t, &k);
917 e = der_put_octet_string(p, len, &k, &l);
924 e = der_put_length_and_tag(p, len, k.length, UNIV, PRIM,
925 UT_GeneralizedTime, &l);
936 * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
937 * (Royal Institute of Technology, Stockholm, Sweden).
938 * All rights reserved.
940 * Redistribution and use in source and binary forms, with or without
941 * modification, are permitted provided that the following conditions
944 * 1. Redistributions of source code must retain the above copyright
945 * notice, this list of conditions and the following disclaimer.
947 * 2. Redistributions in binary form must reproduce the above copyright
948 * notice, this list of conditions and the following disclaimer in the
949 * documentation and/or other materials provided with the distribution.
951 * 3. Neither the name of the Institute nor the names of its contributors
952 * may be used to endorse or promote products derived from this software
953 * without specific prior written permission.
955 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
956 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
957 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
958 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
959 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
960 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
961 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
962 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
963 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
964 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
969 /* RCSID("Heimdal: der_free.c,v 1.8 2001/09/25 13:39:26 assar Exp $"); */
972 free_general_string(general_string * str)
978 free_octet_string(octet_string * k)
990 * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
991 * (Royal Institute of Technology, Stockholm, Sweden).
992 * All rights reserved.
994 * Redistribution and use in source and binary forms, with or without
995 * modification, are permitted provided that the following conditions
998 * 1. Redistributions of source code must retain the above copyright
999 * notice, this list of conditions and the following disclaimer.
1001 * 2. Redistributions in binary form must reproduce the above copyright
1002 * notice, this list of conditions and the following disclaimer in the
1003 * documentation and/or other materials provided with the distribution.
1005 * 3. Neither the name of the Institute nor the names of its contributors
1006 * may be used to endorse or promote products derived from this software
1007 * without specific prior written permission.
1009 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
1010 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1011 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1012 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
1013 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1014 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1015 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1016 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1017 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1018 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1023 /* RCSID("Heimdal: der_length.c,v 1.12 2001/09/25 13:39:26 assar Exp $"); */
1026 len_unsigned(unsigned val)
1044 while (val > 255 || val < -255) {
1050 if ((signed char)val != val)
1058 len_oid(const oid * oid)
1063 for (n = 2; n < oid->length; ++n) {
1064 unsigned u = oid->components[n];
1077 length_len(size_t len)
1082 return len_unsigned(len) + 1;
1086 length_integer(const int *data)
1088 size_t len = len_int(*data);
1090 return 1 + length_len(len) + len;
1094 length_unsigned(const unsigned *data)
1096 size_t len = len_unsigned(*data);
1098 return 1 + length_len(len) + len;
1102 length_enumerated(const unsigned *data)
1104 size_t len = len_int(*data);
1106 return 1 + length_len(len) + len;
1110 length_general_string(const general_string * data)
1113 size_t len = strlen(str);
1114 return 1 + length_len(len) + len;
1118 length_octet_string(const octet_string * k)
1120 return 1 + length_len(k->length) + k->length;
1124 length_oid(const oid * k)
1126 size_t len = len_oid(k);
1128 return 1 + length_len(len) + len;
1132 length_generalized_time(const time_t * t)
1137 time2generalizedtime(*t, &k);
1138 ret = 1 + length_len(k.length) + k.length;
1144 * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
1145 * (Royal Institute of Technology, Stockholm, Sweden).
1146 * All rights reserved.
1148 * Redistribution and use in source and binary forms, with or without
1149 * modification, are permitted provided that the following conditions
1152 * 1. Redistributions of source code must retain the above copyright
1153 * notice, this list of conditions and the following disclaimer.
1155 * 2. Redistributions in binary form must reproduce the above copyright
1156 * notice, this list of conditions and the following disclaimer in the
1157 * documentation and/or other materials provided with the distribution.
1159 * 3. Neither the name of the Institute nor the names of its contributors
1160 * may be used to endorse or promote products derived from this software
1161 * without specific prior written permission.
1163 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
1164 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1165 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1166 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
1167 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1168 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1169 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1170 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1171 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1172 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1177 /* RCSID("Heimdal: der_copy.c,v 1.9 2001/09/25 13:39:25 assar Exp $"); */
1180 copy_general_string(const general_string * from, general_string * to)
1182 *to = malloc(strlen(*from) + 1);
1190 copy_octet_string(const octet_string * from, octet_string * to)
1192 to->length = from->length;
1193 to->data = malloc(to->length);
1194 if (to->length != 0 && to->data == NULL)
1196 memcpy(to->data, from->data, to->length);
1201 copy_oid(const oid * from, oid * to)
1203 to->length = from->length;
1204 to->components = malloc(to->length * sizeof(*to->components));
1205 if (to->length != 0 && to->components == NULL)
1207 memcpy(to->components, from->components, to->length);
1212 * Copyright (c) 1997 Kungliga Tekniska Högskolan
1213 * (Royal Institute of Technology, Stockholm, Sweden).
1214 * All rights reserved.
1216 * Redistribution and use in source and binary forms, with or without
1217 * modification, are permitted provided that the following conditions
1220 * 1. Redistributions of source code must retain the above copyright
1221 * notice, this list of conditions and the following disclaimer.
1223 * 2. Redistributions in binary form must reproduce the above copyright
1224 * notice, this list of conditions and the following disclaimer in the
1225 * documentation and/or other materials provided with the distribution.
1227 * 3. Neither the name of the Institute nor the names of its contributors
1228 * may be used to endorse or promote products derived from this software
1229 * without specific prior written permission.
1231 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
1232 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1233 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1234 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
1235 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1236 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1237 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1238 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1239 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1240 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1245 /* RCSID("Heimdal: timegm.c,v 1.7 1999/12/02 17:05:02 joda Exp $"); */
1253 return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0);
1257 timegm(struct tm * tm)
1259 static const unsigned ndays[2][12] = {
1260 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
1261 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
1266 for (i = 70; i < tm->tm_year; ++i)
1267 res += is_leap(i) ? 366 : 365;
1269 for (i = 0; i < tm->tm_mon; ++i)
1270 res += ndays[is_leap(tm->tm_year)][i];
1271 res += tm->tm_mday - 1;
1281 #endif /* HAVE_TIMEGM */