2 * Copyright (c) 2000-2001,2011-2012,2014 Apple Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 * tpTime.c - cert related time functions
32 * Given a string containing either a UTC-style or "generalized time"
33 * time string, convert to a CFDateRef. Returns nonzero on
36 int timeStringToCfDate(
42 bool isUtc
= false; // 2-digit year
43 bool isLocal
= false; // trailing timezone offset
44 bool isCssmTime
= false; // no trailing 'Z'
45 bool noSeconds
= false;
50 CFTimeZoneRef timeZone
;
51 CFTimeInterval gmtOff
= 0;
53 if((str
== NULL
) || (len
== 0) || (cfDate
== NULL
)) {
57 /* tolerate NULL terminated or not */
58 if(str
[len
- 1] == '\0') {
62 case UTC_TIME_NOSEC_LEN
: // 2-digit year, no seconds, not y2K compliant
66 case UTC_TIME_STRLEN
: // 2-digit year, not Y2K compliant
69 case CSSM_TIME_STRLEN
:
72 case GENERALIZED_TIME_STRLEN
: // 4-digit year
74 case LOCALIZED_UTC_TIME_STRLEN
: // "YYMMDDhhmmssThhmm" (where T=[+,-])
76 // deliberate fallthrough
77 case LOCALIZED_TIME_STRLEN
: // "YYYYMMDDhhmmssThhmm" (where T=[+,-])
80 default: // unknown format
86 /* check that all characters except last (or timezone indicator, if localized) are digits */
87 for(i
=0; i
<(len
- 1); i
++) {
88 if ( !(isdigit(cp
[i
])) )
89 if ( !isLocal
|| !(cp
[i
]=='+' || cp
[i
]=='-') )
93 /* check last character is a 'Z' or digit as appropriate */
94 if(isCssmTime
|| isLocal
) {
95 if(!isdigit(cp
[len
- 1])) {
100 if(cp
[len
- 1] != 'Z' ) {
109 /* two more digits */
121 * 0 <= year < 50 : assume century 21
122 * 50 <= year < 70 : illegal per PKIX
123 * ...though we allow this as of 10/10/02...dmitch
124 * 70 < year <= 99 : assume century 20
146 /* in the string, months are from 1 to 12 */
147 if((x
> 12) || (x
<= 0)) {
157 /* 1..31 in both formats */
158 if((x
> 31) || (x
<= 0)) {
168 if((x
> 23) || (x
< 0)) {
178 if((x
> 59) || (x
< 0)) {
192 if((x
> 59) || (x
< 0)) {
214 x
= atoi( szTemp
) * 60 * 60;
220 x
= atoi( szTemp
) * 60;
228 timeZone
= CFTimeZoneCreateWithTimeIntervalFromGMT(NULL
, gmtOff
);
232 *cfDate
= CFDateCreate(NULL
, CFGregorianDateGetAbsoluteTime(gd
, timeZone
));
238 * Compare two times. Assumes they're both in GMT. Returns:
247 switch(CFDateCompare(t1
, t2
, NULL
)) {
248 case kCFCompareLessThan
:
250 case kCFCompareEqualTo
:
252 case kCFCompareGreaterThan
:
261 * Create a time string, in either UTC (2-digit) or or Generalized (4-digit)
262 * year format. Caller mallocs the output string whose length is at least
263 * (UTC_TIME_STRLEN+1), (GENERALIZED_TIME_STRLEN+1), or (CSSM_TIME_STRLEN+1)
264 * respectively. Caller must hold tpTimeLock.
266 void timeAtNowPlus(unsigned secFromNow
,
273 baseTime
= time(NULL
);
274 baseTime
+= (time_t)secFromNow
;
275 utc
= *gmtime(&baseTime
);
279 /* UTC - 2 year digits - code which parses this assumes that
280 * (2-digit) years between 0 and 49 are in century 21 */
281 if(utc
.tm_year
>= 100) {
284 sprintf(outStr
, "%02d%02d%02d%02d%02d%02dZ",
285 utc
.tm_year
/* + 1900 */, utc
.tm_mon
+ 1,
286 utc
.tm_mday
, utc
.tm_hour
, utc
.tm_min
, utc
.tm_sec
);
289 sprintf(outStr
, "%04d%02d%02d%02d%02d%02dZ",
290 /* note year is relative to 1900, hopefully it'll have
291 * four valid digits! */
292 utc
.tm_year
+ 1900, utc
.tm_mon
+ 1,
293 utc
.tm_mday
, utc
.tm_hour
, utc
.tm_min
, utc
.tm_sec
);
296 sprintf(outStr
, "%04d%02d%02d%02d%02d%02d",
297 /* note year is relative to 1900, hopefully it'll have
298 * four valid digits! */
299 utc
.tm_year
+ 1900, utc
.tm_mon
+ 1,
300 utc
.tm_mday
, utc
.tm_hour
, utc
.tm_min
, utc
.tm_sec
);
306 * Convert a time string, which can be in any of three forms (UTC,
307 * generalized, or CSSM_TIMESTRING) into a CSSM_TIMESTRING. Caller
308 * mallocs the result, which must be at least (CSSM_TIME_STRLEN+1) bytes.
309 * Returns nonzero if incoming time string is badly formed.
311 int tpTimeToCssmTimestring(
312 const char *inStr
, // not necessarily NULL terminated
313 unsigned inStrLen
, // not including possible NULL
316 if((inStrLen
== 0) || (inStr
== NULL
)) {
321 case UTC_TIME_STRLEN
:
323 /* infer century and prepend to output */
332 * 0 <= year < 50 : assume century 21
333 * 50 <= year < 70 : illegal per PKIX
334 * 70 < year <= 99 : assume century 20
338 strcpy(outTime
, "20");
345 strcpy(outTime
, "19");
347 memmove(outTime
+ 2, inStr
, inStrLen
- 1); // don't copy the Z
350 case CSSM_TIME_STRLEN
:
351 memmove(outTime
, inStr
, inStrLen
); // trivial case
353 case GENERALIZED_TIME_STRLEN
:
354 memmove(outTime
, inStr
, inStrLen
- 1); // don't copy the Z
360 outTime
[CSSM_TIME_STRLEN
] = '\0';