]> git.saurik.com Git - apple/security.git/blob - AppleX509TP/tpTime.c
Security-54.1.9.tar.gz
[apple/security.git] / AppleX509TP / tpTime.c
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 /*
20 * tpTime.c - cert related time functions
21 *
22 * Written 10/10/2000 by Doug Mitchell.
23 */
24
25 #include "tpTime.h"
26 #include <string.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <stdbool.h>
31
32 /*
33 * Given a string containing either a UTC-style or "generalized time"
34 * time string, convert to a struct tm (in GMT/UTC). Returns nonzero on
35 * error.
36 */
37 int timeStringToTm(
38 const char *str,
39 unsigned len,
40 struct tm *tmp)
41 {
42 char szTemp[5];
43 bool isUtc = false; // 2-digit year
44 bool isCssmTime = false; // no trailing 'Z'
45 unsigned x;
46 unsigned i;
47 char *cp;
48
49 if((str == NULL) || (len == 0) || (tmp == NULL)) {
50 return 1;
51 }
52
53 /* tolerate NULL terminated or not */
54 if(str[len - 1] == '\0') {
55 len--;
56 }
57 switch(len) {
58 case UTC_TIME_STRLEN: // 2-digit year, not Y2K compliant
59 isUtc = true;
60 break;
61 case CSSM_TIME_STRLEN:
62 isCssmTime = true;
63 break;
64 case GENERALIZED_TIME_STRLEN: // 4-digit year
65 break;
66 default: // unknown format
67 return 1;
68 }
69
70 cp = (char *)str;
71
72 /* check that all characters except last are digits */
73 for(i=0; i<(len - 1); i++) {
74 if ( !(isdigit(cp[i])) ) {
75 return 1;
76 }
77 }
78
79 /* check last character is a 'Z' or digit as appropriate */
80 if(isCssmTime) {
81 if(!isdigit(cp[len - 1])) {
82 return 1;
83 }
84 }
85 else {
86 if(cp[len - 1] != 'Z' ) {
87 return 1;
88 }
89 }
90
91 /* YEAR */
92 szTemp[0] = *cp++;
93 szTemp[1] = *cp++;
94 if(!isUtc) {
95 /* two more digits */
96 szTemp[2] = *cp++;
97 szTemp[3] = *cp++;
98 szTemp[4] = '\0';
99 }
100 else {
101 szTemp[2] = '\0';
102 }
103 x = atoi( szTemp );
104 if(isUtc) {
105 /*
106 * 2-digit year.
107 * 0 <= year < 50 : assume century 21
108 * 50 <= year < 70 : illegal per PKIX
109 * 70 < year <= 99 : assume century 20
110 */
111 if(x < 50) {
112 x += 2000;
113 }
114 else if(x < 70) {
115 return 1;
116 }
117 else {
118 /* century 20 */
119 x += 1900;
120 }
121 }
122 /* by definition - tm_year is year - 1900 */
123 tmp->tm_year = x - 1900;
124
125 /* MONTH */
126 szTemp[0] = *cp++;
127 szTemp[1] = *cp++;
128 szTemp[2] = '\0';
129 x = atoi( szTemp );
130 /* in the string, months are from 1 to 12 */
131 if((x > 12) || (x <= 0)) {
132 return 1;
133 }
134 /* in a tm, 0 to 11 */
135 tmp->tm_mon = x - 1;
136
137 /* DAY */
138 szTemp[0] = *cp++;
139 szTemp[1] = *cp++;
140 szTemp[2] = '\0';
141 x = atoi( szTemp );
142 /* 1..31 in both formats */
143 if((x > 31) || (x <= 0)) {
144 return 1;
145 }
146 tmp->tm_mday = x;
147
148 /* HOUR */
149 szTemp[0] = *cp++;
150 szTemp[1] = *cp++;
151 szTemp[2] = '\0';
152 x = atoi( szTemp );
153 if((x > 23) || (x < 0)) {
154 return 1;
155 }
156 tmp->tm_hour = x;
157
158 /* MINUTE */
159 szTemp[0] = *cp++;
160 szTemp[1] = *cp++;
161 szTemp[2] = '\0';
162 x = atoi( szTemp );
163 if((x > 59) || (x < 0)) {
164 return 1;
165 }
166 tmp->tm_min = x;
167
168 /* SECOND */
169 szTemp[0] = *cp++;
170 szTemp[1] = *cp++;
171 szTemp[2] = '\0';
172 x = atoi( szTemp );
173 if((x > 59) || (x < 0)) {
174 return 1;
175 }
176 tmp->tm_sec = x;
177 return 0;
178 }
179
180 /*
181 * Return current GMT time as a struct tm.
182 * Caller must hold tpTimeLock.
183 */
184 void nowTime(
185 struct tm *now)
186 {
187 time_t nowTime = time(NULL);
188 *now = *gmtime(&nowTime);
189 }
190
191 /*
192 * Compare two times. Assumes they're both in GMT. Returns:
193 * -1 if t1 < t2
194 * 0 if t1 == t2
195 * 1 if t1 > t2
196 */
197 int compareTimes(
198 const struct tm *t1,
199 const struct tm *t2)
200 {
201 if(t1->tm_year > t2->tm_year) {
202 return 1;
203 }
204 else if(t1->tm_year < t2->tm_year) {
205 return -1;
206 }
207 /* year equal */
208 else if(t1->tm_mon > t2->tm_mon) {
209 return 1;
210 }
211 else if(t1->tm_mon < t2->tm_mon) {
212 return -1;
213 }
214 /* month equal */
215 else if(t1->tm_mday > t2->tm_mday) {
216 return 1;
217 }
218 else if(t1->tm_mday < t2->tm_mday) {
219 return -1;
220 }
221 /* day of month equal */
222 else if(t1->tm_hour > t2->tm_hour) {
223 return 1;
224 }
225 else if(t1->tm_hour < t2->tm_hour) {
226 return -1;
227 }
228 /* hour equal */
229 else if(t1->tm_min > t2->tm_min) {
230 return 1;
231 }
232 else if(t1->tm_min < t2->tm_min) {
233 return -1;
234 }
235 /* minute equal */
236 else if(t1->tm_sec > t2->tm_sec) {
237 return 1;
238 }
239 else if(t1->tm_sec < t2->tm_sec) {
240 return -1;
241 }
242 /* equal */
243 return 0;
244 }
245
246 /*
247 * Create a time string, in either UTC (2-digit) or or Generalized (4-digit)
248 * year format. Caller mallocs the output string whose length is at least
249 * (UTC_TIME_STRLEN+1) or (GENERALIZED_TIME_STRLEN+1) respectively.
250 * Caller must hold tpTimeLock.
251 */
252 void timeAtNowPlus(unsigned secFromNow,
253 TpTimeSpec timeSpec,
254 char *outStr)
255 {
256 struct tm utc;
257 time_t baseTime;
258
259 baseTime = time(NULL);
260 baseTime += (time_t)secFromNow;
261 utc = *gmtime(&baseTime);
262
263 if(timeSpec == TIME_UTC) {
264 /* UTC - 2 year digits - code which parses this assumes that
265 * (2-digit) years between 0 and 49 are in century 21 */
266 if(utc.tm_year >= 100) {
267 utc.tm_year -= 100;
268 }
269 sprintf(outStr, "%02d%02d%02d%02d%02d%02dZ",
270 utc.tm_year /* + 1900 */, utc.tm_mon + 1,
271 utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec);
272 }
273 else {
274 sprintf(outStr, "%04d%02d%02d%02d%02d%02dZ",
275 /* note year is relative to 1900, hopefully it'll have
276 * four valid digits! */
277 utc.tm_year + 1900, utc.tm_mon + 1,
278 utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec);
279 }
280 }