2 * Copyright (c) 2011-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 #include <CoreFoundation/CoreFoundation.h>
28 #include <Security/SecCertificateInternal.h>
29 #include <Security/SecInternal.h>
30 #include <libDER/asn1Types.h>
34 #include "Security_regressions.h"
36 #define dateparse(TTYPE, DATE) SecAbsoluteTimeFromDateContent(ASN1_ ## TTYPE ## _TIME, (const uint8_t *)DATE, sizeof(DATE) - 1); \
38 #define dateequals(TTYPE, DATE, EXPECTED) do { \
40 CFIndex string_len = 63; \
41 CFAbsoluteTime at = dateparse(TTYPE, DATE); \
42 if (at == NULL_TIME) { \
43 eq_string("NULL_TIME", EXPECTED, "input: " DATE); \
46 ds = CFDateFormatterCreateStringWithAbsoluteTime(NULL, df, at); \
47 CFStringGetBytes(ds, CFRangeMake(0, CFStringGetLength(ds)), kCFStringEncodingASCII, ' ', false, string, 63, &string_len); \
48 string[string_len] = 0; \
49 eq_string((const char *)string, EXPECTED, "input: " DATE); \
53 /* Test SecAbsoluteTimeFromDateContent. */
54 static void tests(void)
56 CFLocaleRef ls
= CFLocaleGetSystem();
57 CFDateFormatterRef df
= CFDateFormatterCreate(NULL
, ls
, kCFDateFormatterMediumStyle
, kCFDateFormatterLongStyle
);
58 CFStringRef ds
= NULL
;
59 CFTimeZoneRef tz
= CFTimeZoneCreateWithTimeIntervalFromGMT(NULL
, 0.0);
61 CFDateFormatterSetProperty(df
, kCFDateFormatterTimeZone
, tz
);
64 CFDateFormatterSetFormat(df
, CFSTR("yyyyMMddHHmmss"));
66 dateequals(UTC
, "1101010000Z", "20110101000000");
67 dateequals(UTC
, "010101000001Z", "20010101000001");
68 dateequals(GENERALIZED
, "201101010000Z", "NULL_TIME"); // Invalid date
69 dateequals(GENERALIZED
, "20010101000001Z", "20010101000001");
71 dateequals(GENERALIZED
, "20000101000000Z", "20000101000000");
72 dateequals(UTC
, "990101000000Z", "19990101000000");
73 dateequals(UTC
, "710101000000Z", "19710101000000");
75 dateequals(UTC
, "020101000000+0000", "20020101000000");
76 dateequals(UTC
, "020101000000-0800", "20020101080000");
77 dateequals(UTC
, "020101000000+0800", "20011231160000");
78 dateequals(UTC
, "020101000000-0420", "20020101042000");
79 dateequals(UTC
, "020101000013+0430", "20011231193013");
81 dateequals(UTC
, "0201010000+0000", "NULL_TIME");
83 dateequals(GENERALIZED
, "20020101000000+0000", "20020101000000");
84 dateequals(GENERALIZED
, "20020101000000-0800", "20020101080000");
85 dateequals(GENERALIZED
, "20020101000000+0800", "20011231160000");
86 dateequals(GENERALIZED
, "20020101000000-0420", "20020101042000");
87 dateequals(GENERALIZED
, "20020101000013+0430", "20011231193013");
89 dateequals(GENERALIZED
, "20060101000013+0900", "20051231150013");
90 dateequals(GENERALIZED
, "20090101000013+0900", "20081231150013");
91 dateequals(GENERALIZED
, "20110101000013+0900", "20101231150013");
93 /* I'd expect these to be off by one second but since they aren't it
94 seems we don't support leap seconds. */
95 dateequals(GENERALIZED
, "20051231200013-0900", "20060101050013");
96 dateequals(GENERALIZED
, "20081231200013-0900", "20090101050013");
97 dateequals(GENERALIZED
, "20101231200013-0900", "20110101050013");
99 dateequals(GENERALIZED
, "20051231200013+0900", "20051231110013");
100 dateequals(GENERALIZED
, "20081231200013+0900", "20081231110013");
101 dateequals(GENERALIZED
, "20101231200013+0900", "20101231110013");
103 dateequals(GENERALIZED
, "19001231200013Z", "19001231200013");
104 dateequals(GENERALIZED
, "19811231200013Z", "19811231200013");
105 dateequals(GENERALIZED
, "19840229000001Z", "19840229000001");
106 dateequals(GENERALIZED
, "19810229000002Z", "NULL_TIME"); // Feb 29 in a non leap-year
107 dateequals(GENERALIZED
, "20000001000000Z", "NULL_TIME"); // Month 0
108 dateequals(GENERALIZED
, "20000100000000Z", "NULL_TIME"); // Day 0
109 dateequals(GENERALIZED
, "20000131000000Z", "20000131000000"); // Day 1/31
110 dateequals(GENERALIZED
, "20000132000000Z", "NULL_TIME"); // Day 1/31
111 dateequals(GENERALIZED
, "20000229000000Z", "20000229000000"); // Day 2/29
112 dateequals(GENERALIZED
, "20000230000000Z", "NULL_TIME"); // Day 2/30
113 dateequals(GENERALIZED
, "20010331000000Z", "20010331000000");
114 dateequals(GENERALIZED
, "20010332000000Z", "NULL_TIME");
115 dateequals(GENERALIZED
, "20010430000000Z", "20010430000000");
116 dateequals(GENERALIZED
, "20010431000000Z", "NULL_TIME");
117 dateequals(GENERALIZED
, "20010531000000Z", "20010531000000");
118 dateequals(GENERALIZED
, "20010532000000Z", "NULL_TIME");
119 dateequals(GENERALIZED
, "20010630000000Z", "20010630000000");
120 dateequals(GENERALIZED
, "20010631000000Z", "NULL_TIME");
121 dateequals(GENERALIZED
, "20010731000000Z", "20010731000000");
122 dateequals(GENERALIZED
, "20010732000000Z", "NULL_TIME");
123 dateequals(GENERALIZED
, "20010831000000Z", "20010831000000");
124 dateequals(GENERALIZED
, "20010832000000Z", "NULL_TIME");
125 dateequals(GENERALIZED
, "20010930000000Z", "20010930000000");
126 dateequals(GENERALIZED
, "20010931000000Z", "NULL_TIME");
127 dateequals(GENERALIZED
, "20011031000000Z", "20011031000000");
128 dateequals(GENERALIZED
, "20011032000000Z", "NULL_TIME");
129 dateequals(GENERALIZED
, "20011130000000Z", "20011130000000");
130 dateequals(GENERALIZED
, "20011131000000Z", "NULL_TIME");
131 dateequals(GENERALIZED
, "20011231000000Z", "20011231000000");
132 dateequals(GENERALIZED
, "20011232000000Z", "NULL_TIME");
133 dateequals(GENERALIZED
, "20011301000000Z", "NULL_TIME");
135 dateequals(GENERALIZED
, "21000301120000Z", "21000301120000");
136 dateequals(GENERALIZED
, "23000301120000Z", "23000301120000");
137 dateequals(GENERALIZED
, "24000301120000Z", "24000301120000");
138 dateequals(GENERALIZED
, "30000301120000Z", "30000301120000");
139 dateequals(GENERALIZED
, "60000228120000Z", "60000228120000");
140 dateequals(GENERALIZED
, "60800301120000Z", "60800301120000");
145 int si_14_dateparse(int argc
, char *const *argv
)