OLD | NEW |
1 /* This Source Code Form is subject to the terms of the Mozilla Public | 1 /* This Source Code Form is subject to the terms of the Mozilla Public |
2 * License, v. 2.0. If a copy of the MPL was not distributed with this | 2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | 4 |
5 #include "prtypes.h" | 5 #include "prtypes.h" |
6 #include "prtime.h" | 6 #include "prtime.h" |
7 #include "secder.h" | 7 #include "secder.h" |
8 #include "prlong.h" | 8 #include "prlong.h" |
9 #include "secerr.h" | 9 #include "secerr.h" |
10 | 10 |
11 #define HIDIGIT(v) (((v) / 10) + '0') | 11 #define HIDIGIT(v) (((v) / 10) + '0') |
12 #define LODIGIT(v) (((v) % 10) + '0') | 12 #define LODIGIT(v) (((v) % 10) + '0') |
13 | 13 |
14 #define ISDIGIT(dig) (((dig) >= '0') && ((dig) <= '9')) | 14 #define ISDIGIT(dig) (((dig) >= '0') && ((dig) <= '9')) |
15 #define CAPTURE(var,p,label) \ | 15 #define CAPTURE(var,p,label) \ |
16 { \ | 16 { \ |
17 if (!ISDIGIT((p)[0]) || !ISDIGIT((p)[1])) goto label; \ | 17 if (!ISDIGIT((p)[0]) || !ISDIGIT((p)[1])) goto label; \ |
18 (var) = ((p)[0] - '0') * 10 + ((p)[1] - '0'); \ | 18 (var) = ((p)[0] - '0') * 10 + ((p)[1] - '0'); \ |
19 p += 2; \ | 19 p += 2; \ |
20 } | 20 } |
21 | 21 |
22 static const PRTime January1st1 = (PRTime) LL_INIT(0xff234001U, 0x00d44000U)
; | 22 static const PRTime January1st1 = (PRTime) LL_INIT(0xff234001U, 0x00d44000U)
; |
23 static const PRTime January1st1950 = (PRTime) LL_INIT(0xfffdc1f8U, 0x793da000U)
; | 23 static const PRTime January1st1950 = (PRTime) LL_INIT(0xfffdc1f8U, 0x793da000U)
; |
24 static const PRTime January1st2050 = LL_INIT(0x0008f81e, 0x1b098000); | 24 static const PRTime January1st2050 = LL_INIT(0x0008f81e, 0x1b098000); |
25 static const PRTime January1st10000 = LL_INIT(0x0384440c, 0xcc736000); | 25 static const PRTime January1st10000 = LL_INIT(0x0384440c, 0xcc736000); |
26 | 26 |
27 /* gmttime must contains UTC time in micro-seconds unit */ | 27 /* gmttime must contains UTC time in micro-seconds unit */ |
28 SECStatus | 28 SECStatus |
29 DER_TimeToUTCTimeArena(PRArenaPool* arenaOpt, SECItem *dst, int64 gmttime) | 29 DER_TimeToUTCTimeArena(PLArenaPool* arenaOpt, SECItem *dst, PRTime gmttime) |
30 { | 30 { |
31 PRExplodedTime printableTime; | 31 PRExplodedTime printableTime; |
32 unsigned char *d; | 32 unsigned char *d; |
33 | 33 |
34 if ( (gmttime < January1st1950) || (gmttime >= January1st2050) ) { | 34 if ( (gmttime < January1st1950) || (gmttime >= January1st2050) ) { |
35 PORT_SetError(SEC_ERROR_INVALID_ARGS); | 35 PORT_SetError(SEC_ERROR_INVALID_ARGS); |
36 return SECFailure; | 36 return SECFailure; |
37 } | 37 } |
38 | 38 |
39 dst->len = 13; | 39 dst->len = 13; |
40 if (arenaOpt) { | 40 if (arenaOpt) { |
41 dst->data = d = (unsigned char*) PORT_ArenaAlloc(arenaOpt, dst->len); | 41 dst->data = d = (unsigned char*) PORT_ArenaAlloc(arenaOpt, dst->len); |
42 } else { | 42 } else { |
43 dst->data = d = (unsigned char*) PORT_Alloc(dst->len); | 43 dst->data = d = (unsigned char*) PORT_Alloc(dst->len); |
44 } | 44 } |
45 dst->type = siUTCTime; | 45 dst->type = siUTCTime; |
46 if (!d) { | 46 if (!d) { |
47 return SECFailure; | 47 return SECFailure; |
48 } | 48 } |
49 | 49 |
50 /* Convert an int64 time to a printable format. */ | 50 /* Convert a PRTime to a printable format. */ |
51 PR_ExplodeTime(gmttime, PR_GMTParameters, &printableTime); | 51 PR_ExplodeTime(gmttime, PR_GMTParameters, &printableTime); |
52 | 52 |
53 /* The month in UTC time is base one */ | 53 /* The month in UTC time is base one */ |
54 printableTime.tm_month++; | 54 printableTime.tm_month++; |
55 | 55 |
56 /* remove the century since it's added to the tm_year by the | 56 /* remove the century since it's added to the tm_year by the |
57 PR_ExplodeTime routine, but is not needed for UTC time */ | 57 PR_ExplodeTime routine, but is not needed for UTC time */ |
58 printableTime.tm_year %= 100; | 58 printableTime.tm_year %= 100; |
59 | 59 |
60 d[0] = HIDIGIT(printableTime.tm_year); | 60 d[0] = HIDIGIT(printableTime.tm_year); |
61 d[1] = LODIGIT(printableTime.tm_year); | 61 d[1] = LODIGIT(printableTime.tm_year); |
62 d[2] = HIDIGIT(printableTime.tm_month); | 62 d[2] = HIDIGIT(printableTime.tm_month); |
63 d[3] = LODIGIT(printableTime.tm_month); | 63 d[3] = LODIGIT(printableTime.tm_month); |
64 d[4] = HIDIGIT(printableTime.tm_mday); | 64 d[4] = HIDIGIT(printableTime.tm_mday); |
65 d[5] = LODIGIT(printableTime.tm_mday); | 65 d[5] = LODIGIT(printableTime.tm_mday); |
66 d[6] = HIDIGIT(printableTime.tm_hour); | 66 d[6] = HIDIGIT(printableTime.tm_hour); |
67 d[7] = LODIGIT(printableTime.tm_hour); | 67 d[7] = LODIGIT(printableTime.tm_hour); |
68 d[8] = HIDIGIT(printableTime.tm_min); | 68 d[8] = HIDIGIT(printableTime.tm_min); |
69 d[9] = LODIGIT(printableTime.tm_min); | 69 d[9] = LODIGIT(printableTime.tm_min); |
70 d[10] = HIDIGIT(printableTime.tm_sec); | 70 d[10] = HIDIGIT(printableTime.tm_sec); |
71 d[11] = LODIGIT(printableTime.tm_sec); | 71 d[11] = LODIGIT(printableTime.tm_sec); |
72 d[12] = 'Z'; | 72 d[12] = 'Z'; |
73 return SECSuccess; | 73 return SECSuccess; |
74 } | 74 } |
75 | 75 |
76 SECStatus | 76 SECStatus |
77 DER_TimeToUTCTime(SECItem *dst, int64 gmttime) | 77 DER_TimeToUTCTime(SECItem *dst, PRTime gmttime) |
78 { | 78 { |
79 return DER_TimeToUTCTimeArena(NULL, dst, gmttime); | 79 return DER_TimeToUTCTimeArena(NULL, dst, gmttime); |
80 } | 80 } |
81 | 81 |
82 static SECStatus /* forward */ | 82 static SECStatus /* forward */ |
83 der_TimeStringToTime(PRTime *dst, const char *string, int generalized, | 83 der_TimeStringToTime(PRTime *dst, const char *string, int generalized, |
84 const char **endptr); | 84 const char **endptr); |
85 | 85 |
86 #define GEN_STRING 2 /* TimeString is a GeneralizedTime */ | 86 #define GEN_STRING 2 /* TimeString is a GeneralizedTime */ |
87 #define UTC_STRING 0 /* TimeString is a UTCTime */ | 87 #define UTC_STRING 0 /* TimeString is a UTCTime */ |
88 | 88 |
89 /* The caller of DER_AsciiToItem MUST ENSURE that either | 89 /* The caller of DER_AsciiToItem MUST ENSURE that either |
90 ** a) "string" points to a null-terminated ASCII string, or | 90 ** a) "string" points to a null-terminated ASCII string, or |
91 ** b) "string" points to a buffer containing a valid UTCTime, | 91 ** b) "string" points to a buffer containing a valid UTCTime, |
92 ** whether null terminated or not, or | 92 ** whether null terminated or not, or |
93 ** c) "string" contains at least 19 characters, with or without null char. | 93 ** c) "string" contains at least 19 characters, with or without null char. |
94 ** otherwise, this function may UMR and/or crash. | 94 ** otherwise, this function may UMR and/or crash. |
95 ** It suffices to ensure that the input "string" is at least 17 bytes long. | 95 ** It suffices to ensure that the input "string" is at least 17 bytes long. |
96 */ | 96 */ |
97 SECStatus | 97 SECStatus |
98 DER_AsciiToTime(int64 *dst, const char *string) | 98 DER_AsciiToTime(PRTime *dst, const char *string) |
99 { | 99 { |
100 return der_TimeStringToTime(dst, string, UTC_STRING, NULL); | 100 return der_TimeStringToTime(dst, string, UTC_STRING, NULL); |
101 } | 101 } |
102 | 102 |
103 SECStatus | 103 SECStatus |
104 DER_UTCTimeToTime(int64 *dst, const SECItem *time) | 104 DER_UTCTimeToTime(PRTime *dst, const SECItem *time) |
105 { | 105 { |
106 /* Minimum valid UTCTime is yymmddhhmmZ which is 11 bytes. | 106 /* Minimum valid UTCTime is yymmddhhmmZ which is 11 bytes. |
107 ** Maximum valid UTCTime is yymmddhhmmss+0000 which is 17 bytes. | 107 ** Maximum valid UTCTime is yymmddhhmmss+0000 which is 17 bytes. |
108 ** 20 should be large enough for all valid encoded times. | 108 ** 20 should be large enough for all valid encoded times. |
109 */ | 109 */ |
110 unsigned int i; | 110 unsigned int i; |
111 char localBuf[20]; | 111 char localBuf[20]; |
112 const char *end = NULL; | 112 const char *end = NULL; |
113 SECStatus rv; | 113 SECStatus rv; |
114 | 114 |
(...skipping 21 matching lines...) Expand all Loading... |
136 | 136 |
137 /* | 137 /* |
138 gmttime must contains UTC time in micro-seconds unit. | 138 gmttime must contains UTC time in micro-seconds unit. |
139 Note: the caller should make sure that Generalized time | 139 Note: the caller should make sure that Generalized time |
140 should only be used for certifiate validities after the | 140 should only be used for certifiate validities after the |
141 year 2049. Otherwise, UTC time should be used. This routine | 141 year 2049. Otherwise, UTC time should be used. This routine |
142 does not check this case, since it can be used to encode | 142 does not check this case, since it can be used to encode |
143 certificate extension, which does not have this restriction. | 143 certificate extension, which does not have this restriction. |
144 */ | 144 */ |
145 SECStatus | 145 SECStatus |
146 DER_TimeToGeneralizedTimeArena(PRArenaPool* arenaOpt, SECItem *dst, int64 gmttim
e) | 146 DER_TimeToGeneralizedTimeArena(PLArenaPool* arenaOpt, SECItem *dst, PRTime gmtti
me) |
147 { | 147 { |
148 PRExplodedTime printableTime; | 148 PRExplodedTime printableTime; |
149 unsigned char *d; | 149 unsigned char *d; |
150 | 150 |
151 if ( (gmttime<January1st1) || (gmttime>=January1st10000) ) { | 151 if ( (gmttime<January1st1) || (gmttime>=January1st10000) ) { |
152 PORT_SetError(SEC_ERROR_INVALID_ARGS); | 152 PORT_SetError(SEC_ERROR_INVALID_ARGS); |
153 return SECFailure; | 153 return SECFailure; |
154 } | 154 } |
155 dst->len = 15; | 155 dst->len = 15; |
156 if (arenaOpt) { | 156 if (arenaOpt) { |
157 dst->data = d = (unsigned char*) PORT_ArenaAlloc(arenaOpt, dst->len); | 157 dst->data = d = (unsigned char*) PORT_ArenaAlloc(arenaOpt, dst->len); |
158 } else { | 158 } else { |
159 dst->data = d = (unsigned char*) PORT_Alloc(dst->len); | 159 dst->data = d = (unsigned char*) PORT_Alloc(dst->len); |
160 } | 160 } |
161 dst->type = siGeneralizedTime; | 161 dst->type = siGeneralizedTime; |
162 if (!d) { | 162 if (!d) { |
163 return SECFailure; | 163 return SECFailure; |
164 } | 164 } |
165 | 165 |
166 /* Convert an int64 time to a printable format. */ | 166 /* Convert a PRTime to a printable format. */ |
167 PR_ExplodeTime(gmttime, PR_GMTParameters, &printableTime); | 167 PR_ExplodeTime(gmttime, PR_GMTParameters, &printableTime); |
168 | 168 |
169 /* The month in Generalized time is base one */ | 169 /* The month in Generalized time is base one */ |
170 printableTime.tm_month++; | 170 printableTime.tm_month++; |
171 | 171 |
172 d[0] = (printableTime.tm_year /1000) + '0'; | 172 d[0] = (printableTime.tm_year /1000) + '0'; |
173 d[1] = ((printableTime.tm_year % 1000) / 100) + '0'; | 173 d[1] = ((printableTime.tm_year % 1000) / 100) + '0'; |
174 d[2] = ((printableTime.tm_year % 100) / 10) + '0'; | 174 d[2] = ((printableTime.tm_year % 100) / 10) + '0'; |
175 d[3] = (printableTime.tm_year % 10) + '0'; | 175 d[3] = (printableTime.tm_year % 10) + '0'; |
176 d[4] = HIDIGIT(printableTime.tm_month); | 176 d[4] = HIDIGIT(printableTime.tm_month); |
177 d[5] = LODIGIT(printableTime.tm_month); | 177 d[5] = LODIGIT(printableTime.tm_month); |
178 d[6] = HIDIGIT(printableTime.tm_mday); | 178 d[6] = HIDIGIT(printableTime.tm_mday); |
179 d[7] = LODIGIT(printableTime.tm_mday); | 179 d[7] = LODIGIT(printableTime.tm_mday); |
180 d[8] = HIDIGIT(printableTime.tm_hour); | 180 d[8] = HIDIGIT(printableTime.tm_hour); |
181 d[9] = LODIGIT(printableTime.tm_hour); | 181 d[9] = LODIGIT(printableTime.tm_hour); |
182 d[10] = HIDIGIT(printableTime.tm_min); | 182 d[10] = HIDIGIT(printableTime.tm_min); |
183 d[11] = LODIGIT(printableTime.tm_min); | 183 d[11] = LODIGIT(printableTime.tm_min); |
184 d[12] = HIDIGIT(printableTime.tm_sec); | 184 d[12] = HIDIGIT(printableTime.tm_sec); |
185 d[13] = LODIGIT(printableTime.tm_sec); | 185 d[13] = LODIGIT(printableTime.tm_sec); |
186 d[14] = 'Z'; | 186 d[14] = 'Z'; |
187 return SECSuccess; | 187 return SECSuccess; |
188 } | 188 } |
189 | 189 |
190 SECStatus | 190 SECStatus |
191 DER_TimeToGeneralizedTime(SECItem *dst, int64 gmttime) | 191 DER_TimeToGeneralizedTime(SECItem *dst, PRTime gmttime) |
192 { | 192 { |
193 return DER_TimeToGeneralizedTimeArena(NULL, dst, gmttime); | 193 return DER_TimeToGeneralizedTimeArena(NULL, dst, gmttime); |
194 } | 194 } |
195 | 195 |
196 | 196 |
197 SECStatus | 197 SECStatus |
198 DER_GeneralizedTimeToTime(int64 *dst, const SECItem *time) | 198 DER_GeneralizedTimeToTime(PRTime *dst, const SECItem *time) |
199 { | 199 { |
200 /* Minimum valid GeneralizedTime is ccyymmddhhmmZ which is 13 bytes. | 200 /* Minimum valid GeneralizedTime is ccyymmddhhmmZ which is 13 bytes. |
201 ** Maximum valid GeneralizedTime is ccyymmddhhmmss+0000 which is 19 bytes. | 201 ** Maximum valid GeneralizedTime is ccyymmddhhmmss+0000 which is 19 bytes. |
202 ** 20 should be large enough for all valid encoded times. | 202 ** 20 should be large enough for all valid encoded times. |
203 */ | 203 */ |
204 unsigned int i; | 204 unsigned int i; |
205 char localBuf[20]; | 205 char localBuf[20]; |
206 const char *end = NULL; | 206 const char *end = NULL; |
207 SECStatus rv; | 207 SECStatus rv; |
208 | 208 |
(...skipping 18 matching lines...) Expand all Loading... |
227 } | 227 } |
228 return rv; | 228 return rv; |
229 } | 229 } |
230 | 230 |
231 static SECStatus | 231 static SECStatus |
232 der_TimeStringToTime(PRTime *dst, const char *string, int generalized, | 232 der_TimeStringToTime(PRTime *dst, const char *string, int generalized, |
233 const char **endptr) | 233 const char **endptr) |
234 { | 234 { |
235 PRExplodedTime genTime; | 235 PRExplodedTime genTime; |
236 long hourOff = 0, minOff = 0; | 236 long hourOff = 0, minOff = 0; |
237 uint16 century; | 237 PRUint16 century; |
238 char signum; | 238 char signum; |
239 | 239 |
240 if (string == NULL || dst == NULL) { | 240 if (string == NULL || dst == NULL) { |
241 PORT_SetError(SEC_ERROR_INVALID_ARGS); | 241 PORT_SetError(SEC_ERROR_INVALID_ARGS); |
242 return SECFailure; | 242 return SECFailure; |
243 } | 243 } |
244 | 244 |
245 /* Verify time is formatted properly and capture information */ | 245 /* Verify time is formatted properly and capture information */ |
246 memset(&genTime, 0, sizeof genTime); | 246 memset(&genTime, 0, sizeof genTime); |
247 | 247 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 * for the implode time call. | 301 * for the implode time call. |
302 */ | 302 */ |
303 genTime.tm_params.tp_gmt_offset = (PRInt32)((hourOff * 60L + minOff) * 60L); | 303 genTime.tm_params.tp_gmt_offset = (PRInt32)((hourOff * 60L + minOff) * 60L); |
304 *dst = PR_ImplodeTime(&genTime); | 304 *dst = PR_ImplodeTime(&genTime); |
305 return SECSuccess; | 305 return SECSuccess; |
306 | 306 |
307 loser: | 307 loser: |
308 PORT_SetError(SEC_ERROR_INVALID_TIME); | 308 PORT_SetError(SEC_ERROR_INVALID_TIME); |
309 return SECFailure; | 309 return SECFailure; |
310 } | 310 } |
OLD | NEW |