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
44 return (y
% 4) == 0 && ((y
% 100) != 0 || (y
% 400) == 0);
48 * This is a simplifed version of timegm(3) that doesn't accept out of
49 * bound values that timegm(3) normally accepts but those are not
50 * valid in asn1 encodings.
54 _der_timegm (struct tm
*tm
)
56 static const unsigned ndays
[2][12] ={
57 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
58 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
64 if (tm
->tm_mon
< 0 || tm
->tm_mon
> 11)
66 if (tm
->tm_mday
< 1 || tm
->tm_mday
> ndays
[is_leap(tm
->tm_year
)][tm
->tm_mon
])
68 if (tm
->tm_hour
< 0 || tm
->tm_hour
> 23)
70 if (tm
->tm_min
< 0 || tm
->tm_min
> 59)
72 if (tm
->tm_sec
< 0 || tm
->tm_sec
> 59)
75 for (i
= 70; i
< tm
->tm_year
; ++i
)
76 res
+= is_leap(i
) ? 366 : 365;
78 for (i
= 0; i
< tm
->tm_mon
; ++i
)
79 res
+= ndays
[is_leap(tm
->tm_year
)][i
];
80 res
+= tm
->tm_mday
- 1;
90 * Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan
91 * (Royal Institute of Technology, Stockholm, Sweden).
92 * All rights reserved.
94 * Redistribution and use in source and binary forms, with or without
95 * modification, are permitted provided that the following conditions
98 * 1. Redistributions of source code must retain the above copyright
99 * notice, this list of conditions and the following disclaimer.
101 * 2. Redistributions in binary form must reproduce the above copyright
102 * notice, this list of conditions and the following disclaimer in the
103 * documentation and/or other materials provided with the distribution.
105 * 3. Neither the name of the Institute nor the names of its contributors
106 * may be used to endorse or promote products derived from this software
107 * without specific prior written permission.
109 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
110 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
111 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
112 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
113 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
114 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
115 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
116 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
117 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
118 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
124 * All decoding functions take a pointer `p' to first position in
125 * which to read, from the left, `len' which means the maximum number
126 * of characters we are able to read, `ret' were the value will be
127 * returned and `size' where the number of used bytes is stored.
128 * Either 0 or an error code is returned.
132 der_get_unsigned (const unsigned char *p
, size_t len
,
133 unsigned *ret
, size_t *size
)
138 if (len
== sizeof(unsigned) + 1 && p
[0] == 0)
140 else if (len
> sizeof(unsigned))
144 val
= val
* 256 + *p
++;
146 if(size
) *size
= oldlen
;
151 der_get_integer (const unsigned char *p
, size_t len
,
152 int *ret
, size_t *size
)
157 if (len
> sizeof(int))
161 val
= (signed char)*p
++;
163 val
= val
* 256 + *p
++;
166 if(size
) *size
= oldlen
;
171 der_get_length (const unsigned char *p
, size_t len
,
172 size_t *val
, size_t *size
)
189 *val
= ASN1_INDEFINITE
;
196 e
= der_get_unsigned (p
, v
, &tmp
, &l
);
199 if(size
) *size
= l
+ 1;
205 der_get_boolean(const unsigned char *p
, size_t len
, int *data
, size_t *size
)
218 der_get_general_string (const unsigned char *p
, size_t len
,
219 heim_general_string
*str
, size_t *size
)
221 const unsigned char *p1
;
224 p1
= memchr(p
, 0, len
);
227 * Allow trailing NULs. We allow this since MIT Kerberos sends
228 * an strings in the NEED_PREAUTH case that includes a
231 while (p1
- p
< len
&& *p1
== '\0')
234 return ASN1_BAD_CHARACTER
;
237 return ASN1_BAD_LENGTH
;
239 s
= malloc (len
+ 1);
245 if(size
) *size
= len
;
250 der_get_utf8string (const unsigned char *p
, size_t len
,
251 heim_utf8_string
*str
, size_t *size
)
253 return der_get_general_string(p
, len
, str
, size
);
257 der_get_printable_string (const unsigned char *p
, size_t len
,
258 heim_printable_string
*str
, size_t *size
)
260 return der_get_general_string(p
, len
, str
, size
);
264 der_get_ia5_string (const unsigned char *p
, size_t len
,
265 heim_ia5_string
*str
, size_t *size
)
267 return der_get_general_string(p
, len
, str
, size
);
271 der_get_bmp_string (const unsigned char *p
, size_t len
,
272 heim_bmp_string
*data
, size_t *size
)
277 return ASN1_BAD_FORMAT
;
278 data
->length
= len
/ 2;
279 if (data
->length
> UINT_MAX
/sizeof(data
->data
[0]))
281 data
->data
= malloc(data
->length
* sizeof(data
->data
[0]));
282 if (data
->data
== NULL
&& data
->length
!= 0)
285 for (i
= 0; i
< data
->length
; i
++) {
286 data
->data
[i
] = (p
[0] << 8) | p
[1];
288 /* check for NUL in the middle of the string */
289 if (data
->data
[i
] == 0 && i
!= (data
->length
- 1)) {
293 return ASN1_BAD_CHARACTER
;
296 if (size
) *size
= len
;
302 der_get_universal_string (const unsigned char *p
, size_t len
,
303 heim_universal_string
*data
, size_t *size
)
308 return ASN1_BAD_FORMAT
;
309 data
->length
= len
/ 4;
310 if (data
->length
> UINT_MAX
/sizeof(data
->data
[0]))
312 data
->data
= malloc(data
->length
* sizeof(data
->data
[0]));
313 if (data
->data
== NULL
&& data
->length
!= 0)
316 for (i
= 0; i
< data
->length
; i
++) {
317 data
->data
[i
] = (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
319 /* check for NUL in the middle of the string */
320 if (data
->data
[i
] == 0 && i
!= (data
->length
- 1)) {
324 return ASN1_BAD_CHARACTER
;
327 if (size
) *size
= len
;
332 der_get_visible_string (const unsigned char *p
, size_t len
,
333 heim_visible_string
*str
, size_t *size
)
335 return der_get_general_string(p
, len
, str
, size
);
339 der_get_octet_string (const unsigned char *p
, size_t len
,
340 heim_octet_string
*data
, size_t *size
)
343 data
->data
= malloc(len
);
344 if (data
->data
== NULL
&& data
->length
!= 0)
346 memcpy (data
->data
, p
, len
);
347 if(size
) *size
= len
;
352 der_get_octet_string_ber (const unsigned char *p
, size_t len
,
353 heim_octet_string
*data
, size_t *size
)
358 unsigned int tag
, depth
= 0;
359 size_t l
, datalen
, oldlen
= len
;
365 e
= der_get_tag (p
, len
, &class, &type
, &tag
, &l
);
367 if (class != ASN1_C_UNIV
) {
371 if (type
== PRIM
&& tag
== UT_EndOfContent
) {
376 if (tag
!= UT_OctetString
) {
383 e
= der_get_length (p
, len
, &datalen
, &l
);
394 ptr
= realloc(data
->data
, data
->length
+ datalen
);
400 memcpy(((unsigned char *)data
->data
) + data
->length
, p
, datalen
);
401 data
->length
+= datalen
;
409 return ASN1_INDEF_OVERRUN
;
410 if(size
) *size
= oldlen
- len
;
421 der_get_heim_integer (const unsigned char *p
, size_t len
,
422 heim_integer
*data
, size_t *size
)
444 data
->data
= malloc(data
->length
);
445 if (data
->data
== NULL
) {
451 q
= &((unsigned char*)data
->data
)[data
->length
- 1];
452 p
+= data
->length
- 1;
453 while (q
>= (unsigned char*)data
->data
) {
468 data
->data
= malloc(data
->length
);
469 if (data
->data
== NULL
&& data
->length
!= 0) {
475 memcpy(data
->data
, p
, data
->length
);
483 generalizedtime2time (const char *s
, time_t *t
)
487 memset(&tm
, 0, sizeof(tm
));
488 if (sscanf (s
, "%04d%02d%02d%02d%02d%02dZ",
489 &tm
.tm_year
, &tm
.tm_mon
, &tm
.tm_mday
, &tm
.tm_hour
,
490 &tm
.tm_min
, &tm
.tm_sec
) != 6) {
491 if (sscanf (s
, "%02d%02d%02d%02d%02d%02dZ",
492 &tm
.tm_year
, &tm
.tm_mon
, &tm
.tm_mday
, &tm
.tm_hour
,
493 &tm
.tm_min
, &tm
.tm_sec
) != 6)
494 return ASN1_BAD_TIMEFORMAT
;
502 *t
= _der_timegm (&tm
);
507 der_get_time (const unsigned char *p
, size_t len
,
508 time_t *data
, size_t *size
)
513 if (len
> len
+ 1 || len
== 0)
514 return ASN1_BAD_LENGTH
;
516 times
= malloc(len
+ 1);
519 memcpy(times
, p
, len
);
521 e
= generalizedtime2time(times
, data
);
523 if(size
) *size
= len
;
528 der_get_generalized_time (const unsigned char *p
, size_t len
,
529 time_t *data
, size_t *size
)
531 return der_get_time(p
, len
, data
, size
);
535 der_get_utctime (const unsigned char *p
, size_t len
,
536 time_t *data
, size_t *size
)
538 return der_get_time(p
, len
, data
, size
);
542 der_get_oid (const unsigned char *p
, size_t len
,
543 heim_oid
*data
, size_t *size
)
552 return ASN1_BAD_LENGTH
;
554 if (len
+ 1 > UINT_MAX
/sizeof(data
->components
[0]))
557 data
->components
= malloc((len
+ 1) * sizeof(data
->components
[0]));
558 if (data
->components
== NULL
)
560 data
->components
[0] = (*p
) / 40;
561 data
->components
[1] = (*p
) % 40;
564 for (n
= 2; len
> 0; ++n
) {
569 u1
= u
* 128 + (*p
++ % 128);
570 /* check that we don't overflow the element */
576 } while (len
> 0 && p
[-1] & 0x80);
577 data
->components
[n
] = u
;
579 if (n
> 2 && p
[-1] & 0x80) {
590 der_get_tag (const unsigned char *p
, size_t len
,
591 Der_class
*class, Der_type
*type
,
592 unsigned int *tag
, size_t *size
)
597 *class = (Der_class
)(((*p
) >> 6) & 0x03);
598 *type
= (Der_type
)(((*p
) >> 5) & 0x01);
602 unsigned int continuation
;
608 continuation
= *p
& 128;
609 tag1
= *tag
* 128 + (*p
% 128);
610 /* check that we don't overflow the tag */
612 return ASN1_OVERFLOW
;
615 } while(continuation
);
617 if(size
) *size
= ret
;
622 der_match_tag (const unsigned char *p
, size_t len
,
623 Der_class
class, Der_type type
,
624 unsigned int tag
, size_t *size
)
629 e
= der_match_tag2(p
, len
, class, &thistype
, tag
, size
);
631 if (thistype
!= type
) return ASN1_BAD_ID
;
636 der_match_tag2 (const unsigned char *p
, size_t len
,
637 Der_class
class, Der_type
*type
,
638 unsigned int tag
, size_t *size
)
642 unsigned int thistag
;
645 e
= der_get_tag (p
, len
, &thisclass
, type
, &thistag
, &l
);
647 if (class != thisclass
)
650 return ASN1_MISPLACED_FIELD
;
652 return ASN1_MISSING_FIELD
;
658 der_match_tag_and_length (const unsigned char *p
, size_t len
,
659 Der_class
class, Der_type
*type
, unsigned int tag
,
660 size_t *length_ret
, size_t *size
)
665 e
= der_match_tag2 (p
, len
, class, type
, tag
, &l
);
670 e
= der_get_length (p
, len
, length_ret
, &l
);
672 if(size
) *size
= ret
+ l
;
679 * Old versions of DCE was based on a very early beta of the MIT code,
680 * which used MAVROS for ASN.1 encoding. MAVROS had the interesting
681 * feature that it encoded data in the forward direction, which has
682 * it's problems, since you have no idea how long the data will be
683 * until after you're done. MAVROS solved this by reserving one byte
684 * for length, and later, if the actual length was longer, it reverted
685 * to indefinite, BER style, lengths. The version of MAVROS used by
686 * the DCE people could apparently generate correct X.509 DER encodings, and
687 * did this by making space for the length after encoding, but
688 * unfortunately this feature wasn't used with Kerberos.
692 _heim_fix_dce(size_t reallen
, size_t *len
)
694 if(reallen
== ASN1_INDEFINITE
)
703 der_get_bit_string (const unsigned char *p
, size_t len
,
704 heim_bit_string
*data
, size_t *size
)
709 return ASN1_BAD_FORMAT
;
710 if (len
- 1 == 0 && p
[0] != 0)
711 return ASN1_BAD_FORMAT
;
712 /* check if any of the three upper bits are set
713 * any of them will cause a interger overrun */
714 if ((len
- 1) >> (sizeof(len
) * 8 - 3))
716 data
->length
= (len
- 1) * 8;
717 data
->data
= malloc(len
- 1);
718 if (data
->data
== NULL
&& (len
- 1) != 0)
720 /* copy data is there is data to copy */
722 memcpy (data
->data
, p
+ 1, len
- 1);
723 data
->length
-= p
[0];
725 if(size
) *size
= len
;
729 * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
730 * (Royal Institute of Technology, Stockholm, Sweden).
731 * All rights reserved.
733 * Redistribution and use in source and binary forms, with or without
734 * modification, are permitted provided that the following conditions
737 * 1. Redistributions of source code must retain the above copyright
738 * notice, this list of conditions and the following disclaimer.
740 * 2. Redistributions in binary form must reproduce the above copyright
741 * notice, this list of conditions and the following disclaimer in the
742 * documentation and/or other materials provided with the distribution.
744 * 3. Neither the name of the Institute nor the names of its contributors
745 * may be used to endorse or promote products derived from this software
746 * without specific prior written permission.
748 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
749 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
750 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
751 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
752 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
753 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
754 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
755 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
756 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
757 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
765 * All encoding functions take a pointer `p' to first position in
766 * which to write, from the right, `len' which means the maximum
767 * number of characters we are able to write. The function returns
768 * the number of characters written in `size' (if non-NULL).
769 * The return value is 0 or an error.
773 der_put_unsigned (unsigned char *p
, size_t len
, const unsigned *v
, size_t *size
)
775 unsigned char *base
= p
;
779 while (len
> 0 && val
) {
785 return ASN1_OVERFLOW
;
789 return ASN1_OVERFLOW
;
796 return ASN1_OVERFLOW
;
805 der_put_integer (unsigned char *p
, size_t len
, const int *v
, size_t *size
)
807 unsigned char *base
= p
;
813 return ASN1_OVERFLOW
;
820 return ASN1_OVERFLOW
;
828 return ASN1_OVERFLOW
;
835 return ASN1_OVERFLOW
;
846 der_put_length (unsigned char *p
, size_t len
, size_t val
, size_t *size
)
849 return ASN1_OVERFLOW
;
859 return ASN1_OVERFLOW
;
873 der_put_boolean(unsigned char *p
, size_t len
, const int *data
, size_t *size
)
876 return ASN1_OVERFLOW
;
886 der_put_general_string (unsigned char *p
, size_t len
,
887 const heim_general_string
*str
, size_t *size
)
889 size_t slen
= strlen(*str
);
892 return ASN1_OVERFLOW
;
894 memcpy (p
+1, *str
, slen
);
900 der_put_utf8string (unsigned char *p
, size_t len
,
901 const heim_utf8_string
*str
, size_t *size
)
903 return der_put_general_string(p
, len
, str
, size
);
907 der_put_printable_string (unsigned char *p
, size_t len
,
908 const heim_printable_string
*str
, size_t *size
)
910 return der_put_general_string(p
, len
, str
, size
);
914 der_put_ia5_string (unsigned char *p
, size_t len
,
915 const heim_ia5_string
*str
, size_t *size
)
917 return der_put_general_string(p
, len
, str
, size
);
921 der_put_bmp_string (unsigned char *p
, size_t len
,
922 const heim_bmp_string
*data
, size_t *size
)
925 if (len
/ 2 < data
->length
)
926 return ASN1_OVERFLOW
;
927 p
-= data
->length
* 2;
928 for (i
= 0; i
< data
->length
; i
++) {
929 p
[1] = (data
->data
[i
] >> 8) & 0xff;
930 p
[2] = data
->data
[i
] & 0xff;
933 if (size
) *size
= data
->length
* 2;
938 der_put_universal_string (unsigned char *p
, size_t len
,
939 const heim_universal_string
*data
, size_t *size
)
942 if (len
/ 4 < data
->length
)
943 return ASN1_OVERFLOW
;
944 p
-= data
->length
* 4;
945 for (i
= 0; i
< data
->length
; i
++) {
946 p
[1] = (data
->data
[i
] >> 24) & 0xff;
947 p
[2] = (data
->data
[i
] >> 16) & 0xff;
948 p
[3] = (data
->data
[i
] >> 8) & 0xff;
949 p
[4] = data
->data
[i
] & 0xff;
952 if (size
) *size
= data
->length
* 4;
957 der_put_visible_string (unsigned char *p
, size_t len
,
958 const heim_visible_string
*str
, size_t *size
)
960 return der_put_general_string(p
, len
, str
, size
);
964 der_put_octet_string (unsigned char *p
, size_t len
,
965 const heim_octet_string
*data
, size_t *size
)
967 if (len
< data
->length
)
968 return ASN1_OVERFLOW
;
970 memcpy (p
+1, data
->data
, data
->length
);
971 *size
= data
->length
;
976 der_put_heim_integer (unsigned char *p
, size_t len
,
977 const heim_integer
*data
, size_t *size
)
979 unsigned char *buf
= data
->data
;
982 if (data
->length
== 0) {
984 return ASN1_OVERFLOW
;
990 if (len
< data
->length
)
991 return ASN1_OVERFLOW
;
995 if (data
->negative
) {
997 for (i
= data
->length
- 1, carry
= 1; i
>= 0; i
--) {
1005 return ASN1_OVERFLOW
;
1012 memcpy(p
+ 1, buf
, data
->length
);
1016 return ASN1_OVERFLOW
;
1023 *size
= data
->length
+ hibitset
;
1028 der_put_generalized_time (unsigned char *p
, size_t len
,
1029 const time_t *data
, size_t *size
)
1031 heim_octet_string k
;
1035 e
= _heim_time2generalizedtime (*data
, &k
, 1);
1038 e
= der_put_octet_string(p
, len
, &k
, &l
);
1048 der_put_utctime (unsigned char *p
, size_t len
,
1049 const time_t *data
, size_t *size
)
1051 heim_octet_string k
;
1055 e
= _heim_time2generalizedtime (*data
, &k
, 0);
1058 e
= der_put_octet_string(p
, len
, &k
, &l
);
1068 der_put_oid (unsigned char *p
, size_t len
,
1069 const heim_oid
*data
, size_t *size
)
1071 unsigned char *base
= p
;
1074 for (n
= data
->length
- 1; n
>= 2; --n
) {
1075 unsigned u
= data
->components
[n
];
1078 return ASN1_OVERFLOW
;
1084 return ASN1_OVERFLOW
;
1085 *p
-- = 128 + u
% 128;
1091 return ASN1_OVERFLOW
;
1092 *p
-- = 40 * data
->components
[0] + data
->components
[1];
1098 der_put_tag (unsigned char *p
, size_t len
, Der_class
class, Der_type type
,
1099 unsigned int tag
, size_t *size
)
1103 return ASN1_OVERFLOW
;
1104 *p
= MAKE_TAG(class, type
, tag
);
1108 unsigned int continuation
= 0;
1112 return ASN1_OVERFLOW
;
1113 *p
-- = tag
% 128 | continuation
;
1117 continuation
= 0x80;
1120 return ASN1_OVERFLOW
;
1121 *p
-- = MAKE_TAG(class, type
, 0x1f);
1129 der_put_length_and_tag (unsigned char *p
, size_t len
, size_t len_val
,
1130 Der_class
class, Der_type type
,
1131 unsigned int tag
, size_t *size
)
1137 e
= der_put_length (p
, len
, len_val
, &l
);
1143 e
= der_put_tag (p
, len
, class, type
, tag
, &l
);
1153 _heim_time2generalizedtime (time_t t
, heim_octet_string
*s
, int gtimep
)
1156 const size_t len
= gtimep
? 15 : 13;
1158 s
->data
= malloc(len
+ 1);
1159 if (s
->data
== NULL
)
1164 snprintf (s
->data
, len
+ 1, "%04d%02d%02d%02d%02d%02dZ",
1165 tm
->tm_year
+ 1900, tm
->tm_mon
+ 1, tm
->tm_mday
,
1166 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
1168 snprintf (s
->data
, len
+ 1, "%02d%02d%02d%02d%02d%02dZ",
1169 tm
->tm_year
% 100, tm
->tm_mon
+ 1, tm
->tm_mday
,
1170 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
1176 der_put_bit_string (unsigned char *p
, size_t len
,
1177 const heim_bit_string
*data
, size_t *size
)
1179 size_t data_size
= (data
->length
+ 7) / 8;
1180 if (len
< data_size
+ 1)
1181 return ASN1_OVERFLOW
;
1184 memcpy (p
+2, data
->data
, data_size
);
1185 if (data
->length
&& (data
->length
% 8) != 0)
1186 p
[1] = 8 - (data
->length
% 8);
1189 *size
= data_size
+ 1;
1194 _heim_der_set_sort(const void *a1
, const void *a2
)
1196 const struct heim_octet_string
*s1
= a1
, *s2
= a2
;
1199 ret
= memcmp(s1
->data
, s2
->data
,
1200 s1
->length
< s2
->length
? s1
->length
: s2
->length
);
1203 return s1
->length
- s2
->length
;
1206 * Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan
1207 * (Royal Institute of Technology, Stockholm, Sweden).
1208 * All rights reserved.
1210 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
1212 * Redistribution and use in source and binary forms, with or without
1213 * modification, are permitted provided that the following conditions
1216 * 1. Redistributions of source code must retain the above copyright
1217 * notice, this list of conditions and the following disclaimer.
1219 * 2. Redistributions in binary form must reproduce the above copyright
1220 * notice, this list of conditions and the following disclaimer in the
1221 * documentation and/or other materials provided with the distribution.
1223 * 3. Neither the name of the Institute nor the names of its contributors
1224 * may be used to endorse or promote products derived from this software
1225 * without specific prior written permission.
1227 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
1228 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1229 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1230 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
1231 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1232 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1233 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1234 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1235 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1236 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1241 /* RCSID("$Id$"); */
1244 der_free_general_string (heim_general_string
*str
)
1251 der_free_integer (int *i
)
1257 der_free_unsigned (unsigned *u
)
1263 der_free_generalized_time(time_t *t
)
1269 der_free_utctime(time_t *t
)
1276 der_free_utf8string (heim_utf8_string
*str
)
1283 der_free_printable_string (heim_printable_string
*str
)
1290 der_free_ia5_string (heim_ia5_string
*str
)
1297 der_free_bmp_string (heim_bmp_string
*k
)
1305 der_free_universal_string (heim_universal_string
*k
)
1313 der_free_visible_string (heim_visible_string
*str
)
1320 der_free_octet_string (heim_octet_string
*k
)
1328 der_free_heim_integer (heim_integer
*k
)
1336 der_free_oid (heim_oid
*k
)
1338 free(k
->components
);
1339 k
->components
= NULL
;
1344 der_free_bit_string (heim_bit_string
*k
)
1351 * Copyright (c) 1997-2005 Kungliga Tekniska Högskolan
1352 * (Royal Institute of Technology, Stockholm, Sweden).
1353 * All rights reserved.
1355 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
1357 * Redistribution and use in source and binary forms, with or without
1358 * modification, are permitted provided that the following conditions
1361 * 1. Redistributions of source code must retain the above copyright
1362 * notice, this list of conditions and the following disclaimer.
1364 * 2. Redistributions in binary form must reproduce the above copyright
1365 * notice, this list of conditions and the following disclaimer in the
1366 * documentation and/or other materials provided with the distribution.
1368 * 3. Neither the name of the Institute nor the names of its contributors
1369 * may be used to endorse or promote products derived from this software
1370 * without specific prior written permission.
1372 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
1373 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1374 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1375 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
1376 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1377 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1378 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1379 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1380 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1381 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1386 /* RCSID("$Id$"); */
1389 _heim_len_unsigned (unsigned val
)
1392 int last_val_gt_128
;
1396 last_val_gt_128
= (val
>= 128);
1407 _heim_len_int (int val
)
1434 len_oid (const heim_oid
*oid
)
1439 for (n
= 2; n
< oid
->length
; ++n
) {
1440 unsigned u
= oid
->components
[n
];
1451 der_length_len (size_t len
)
1466 der_length_tag(unsigned int tag
)
1480 der_length_integer (const int *data
)
1482 return _heim_len_int (*data
);
1486 der_length_unsigned (const unsigned *data
)
1488 return _heim_len_unsigned(*data
);
1492 der_length_enumerated (const unsigned *data
)
1494 return _heim_len_int (*data
);
1498 der_length_general_string (const heim_general_string
*data
)
1500 return strlen(*data
);
1504 der_length_utf8string (const heim_utf8_string
*data
)
1506 return strlen(*data
);
1510 der_length_printable_string (const heim_printable_string
*data
)
1512 return strlen(*data
);
1516 der_length_ia5_string (const heim_ia5_string
*data
)
1518 return strlen(*data
);
1522 der_length_bmp_string (const heim_bmp_string
*data
)
1524 return data
->length
* 2;
1528 der_length_universal_string (const heim_universal_string
*data
)
1530 return data
->length
* 4;
1534 der_length_visible_string (const heim_visible_string
*data
)
1536 return strlen(*data
);
1540 der_length_octet_string (const heim_octet_string
*k
)
1546 der_length_heim_integer (const heim_integer
*k
)
1551 return k
->length
+ (((~(((unsigned char *)k
->data
)[0])) & 0x80) ? 0 : 1);
1553 return k
->length
+ ((((unsigned char *)k
->data
)[0] & 0x80) ? 1 : 0);
1557 der_length_oid (const heim_oid
*k
)
1563 der_length_generalized_time (const time_t *t
)
1565 heim_octet_string k
;
1568 _heim_time2generalizedtime (*t
, &k
, 1);
1575 der_length_utctime (const time_t *t
)
1577 heim_octet_string k
;
1580 _heim_time2generalizedtime (*t
, &k
, 0);
1587 der_length_boolean (const int *k
)
1593 der_length_bit_string (const heim_bit_string
*k
)
1595 return (k
->length
+ 7) / 8 + 1;
1598 * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
1599 * (Royal Institute of Technology, Stockholm, Sweden).
1600 * All rights reserved.
1602 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
1604 * Redistribution and use in source and binary forms, with or without
1605 * modification, are permitted provided that the following conditions
1608 * 1. Redistributions of source code must retain the above copyright
1609 * notice, this list of conditions and the following disclaimer.
1611 * 2. Redistributions in binary form must reproduce the above copyright
1612 * notice, this list of conditions and the following disclaimer in the
1613 * documentation and/or other materials provided with the distribution.
1615 * 3. Neither the name of the Institute nor the names of its contributors
1616 * may be used to endorse or promote products derived from this software
1617 * without specific prior written permission.
1619 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
1620 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1621 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1622 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
1623 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1624 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1625 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1626 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1627 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1628 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1633 /* RCSID("$Id$"); */
1636 der_copy_general_string (const heim_general_string
*from
,
1637 heim_general_string
*to
)
1639 *to
= strdup(*from
);
1646 der_copy_integer (const int *from
, int *to
)
1653 der_copy_unsigned (const unsigned *from
, unsigned *to
)
1660 der_copy_generalized_time (const time_t *from
, time_t *to
)
1667 der_copy_utctime (const time_t *from
, time_t *to
)
1674 der_copy_utf8string (const heim_utf8_string
*from
, heim_utf8_string
*to
)
1676 return der_copy_general_string(from
, to
);
1680 der_copy_printable_string (const heim_printable_string
*from
,
1681 heim_printable_string
*to
)
1683 return der_copy_general_string(from
, to
);
1687 der_copy_ia5_string (const heim_printable_string
*from
,
1688 heim_printable_string
*to
)
1690 return der_copy_general_string(from
, to
);
1694 der_copy_bmp_string (const heim_bmp_string
*from
, heim_bmp_string
*to
)
1696 to
->length
= from
->length
;
1697 to
->data
= malloc(to
->length
* sizeof(to
->data
[0]));
1698 if(to
->length
!= 0 && to
->data
== NULL
)
1700 memcpy(to
->data
, from
->data
, to
->length
* sizeof(to
->data
[0]));
1705 der_copy_universal_string (const heim_universal_string
*from
,
1706 heim_universal_string
*to
)
1708 to
->length
= from
->length
;
1709 to
->data
= malloc(to
->length
* sizeof(to
->data
[0]));
1710 if(to
->length
!= 0 && to
->data
== NULL
)
1712 memcpy(to
->data
, from
->data
, to
->length
* sizeof(to
->data
[0]));
1717 der_copy_visible_string (const heim_visible_string
*from
,
1718 heim_visible_string
*to
)
1720 return der_copy_general_string(from
, to
);
1724 der_copy_octet_string (const heim_octet_string
*from
, heim_octet_string
*to
)
1726 to
->length
= from
->length
;
1727 to
->data
= malloc(to
->length
);
1728 if(to
->length
!= 0 && to
->data
== NULL
)
1730 memcpy(to
->data
, from
->data
, to
->length
);
1735 der_copy_heim_integer (const heim_integer
*from
, heim_integer
*to
)
1737 to
->length
= from
->length
;
1738 to
->data
= malloc(to
->length
);
1739 if(to
->length
!= 0 && to
->data
== NULL
)
1741 memcpy(to
->data
, from
->data
, to
->length
);
1742 to
->negative
= from
->negative
;
1747 der_copy_oid (const heim_oid
*from
, heim_oid
*to
)
1749 to
->length
= from
->length
;
1750 to
->components
= malloc(to
->length
* sizeof(*to
->components
));
1751 if (to
->length
!= 0 && to
->components
== NULL
)
1753 memcpy(to
->components
, from
->components
,
1754 to
->length
* sizeof(*to
->components
));
1759 der_copy_bit_string (const heim_bit_string
*from
, heim_bit_string
*to
)
1763 len
= (from
->length
+ 7) / 8;
1764 to
->length
= from
->length
;
1765 to
->data
= malloc(len
);
1766 if(len
!= 0 && to
->data
== NULL
)
1768 memcpy(to
->data
, from
->data
, len
);