make scrypt ASN.1 parameter functions public
[openssl.git] / crypto / asn1 / a_time.c
1 /*
2  * Copyright 1999-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /*-
11  * This is an implementation of the ASN1 Time structure which is:
12  *    Time ::= CHOICE {
13  *      utcTime        UTCTime,
14  *      generalTime    GeneralizedTime }
15  */
16
17 #include <stdio.h>
18 #include <time.h>
19 #include "internal/cryptlib.h"
20 #include <openssl/asn1t.h>
21 #include "asn1_locl.h"
22
23 IMPLEMENT_ASN1_MSTRING(ASN1_TIME, B_ASN1_TIME)
24
25 IMPLEMENT_ASN1_FUNCTIONS(ASN1_TIME)
26
27 static int leap_year(const int year)
28 {
29     if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
30         return 1;
31     return 0;
32 }
33
34 /*
35  * Compute the day of the week and the day of the year from the year, month
36  * and day.  The day of the year is straightforward, the day of the week uses
37  * a form of Zeller's congruence.  For this months start with March and are
38  * numbered 4 through 15.
39  */
40 static void determine_days(struct tm *tm)
41 {
42     static const int ydays[12] = {
43         0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
44     };
45     int y = tm->tm_year + 1900;
46     int m = tm->tm_mon;
47     int d = tm->tm_mday;
48     int c;
49
50     tm->tm_yday = ydays[m] + d - 1;
51     if (m >= 2) {
52         /* March and onwards can be one day further into the year */
53         tm->tm_yday += leap_year(y);
54         m += 2;
55     } else {
56         /* Treat January and February as part of the previous year */
57         m += 14;
58         y--;
59     }
60     c = y / 100;
61     y %= 100;
62     /* Zeller's congruance */
63     tm->tm_wday = (d + (13 * m) / 5 + y + y / 4 + c / 4 + 5 * c + 6) % 7;
64 }
65
66 int asn1_time_to_tm(struct tm *tm, const ASN1_TIME *d)
67 {
68     static const int min[9] = { 0, 0, 1, 1, 0, 0, 0, 0, 0 };
69     static const int max[9] = { 99, 99, 12, 31, 23, 59, 59, 12, 59 };
70     static const int mdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
71     char *a;
72     int n, i, i2, l, o, min_l = 11, strict = 0, end = 6, btz = 5, md;
73     struct tm tmp;
74
75     /*
76      * ASN1_STRING_FLAG_X509_TIME is used to enforce RFC 5280
77      * time string format, in which:
78      *
79      * 1. "seconds" is a 'MUST'
80      * 2. "Zulu" timezone is a 'MUST'
81      * 3. "+|-" is not allowed to indicate a time zone
82      */
83     if (d->type == V_ASN1_UTCTIME) {
84         if (d->flags & ASN1_STRING_FLAG_X509_TIME) {
85             min_l = 13;
86             strict = 1;
87         }
88     } else if (d->type == V_ASN1_GENERALIZEDTIME) {
89         end = 7;
90         btz = 6;
91         if (d->flags & ASN1_STRING_FLAG_X509_TIME) {
92             min_l = 15;
93             strict = 1;
94         } else {
95             min_l = 13;
96         }
97     } else {
98         return 0;
99     }
100
101     l = d->length;
102     a = (char *)d->data;
103     o = 0;
104     memset(&tmp, 0, sizeof(tmp));
105
106     /*
107      * GENERALIZEDTIME is similar to UTCTIME except the year is represented
108      * as YYYY. This stuff treats everything as a two digit field so make
109      * first two fields 00 to 99
110      */
111
112     if (l < min_l)
113         goto err;
114     for (i = 0; i < end; i++) {
115         if (!strict && (i == btz) && ((a[o] == 'Z') || (a[o] == '+') || (a[o] == '-'))) {
116             i++;
117             break;
118         }
119         if ((a[o] < '0') || (a[o] > '9'))
120             goto err;
121         n = a[o] - '0';
122         /* incomplete 2-digital number */
123         if (++o == l)
124             goto err;
125
126         if ((a[o] < '0') || (a[o] > '9'))
127             goto err;
128         n = (n * 10) + a[o] - '0';
129         /* no more bytes to read, but we haven't seen time-zone yet */
130         if (++o == l)
131             goto err;
132
133         i2 = (d->type == V_ASN1_UTCTIME) ? i + 1 : i;
134
135         if ((n < min[i2]) || (n > max[i2]))
136             goto err;
137         switch (i2) {
138         case 0:
139             /* UTC will never be here */
140             tmp.tm_year = n * 100 - 1900;
141             break;
142         case 1:
143             if (d->type == V_ASN1_UTCTIME)
144                 tmp.tm_year = n < 50 ? n + 100 : n;
145             else
146                 tmp.tm_year += n;
147             break;
148         case 2:
149             tmp.tm_mon = n - 1;
150             break;
151         case 3:
152             /* check if tm_mday is valid in tm_mon */
153             if (tmp.tm_mon == 1) {
154                 /* it's February */
155                 md = mdays[1] + leap_year(tmp.tm_year + 1900);
156             } else {
157                 md = mdays[tmp.tm_mon];
158             }
159             if (n > md)
160                 goto err;
161             tmp.tm_mday = n;
162             determine_days(&tmp);
163             break;
164         case 4:
165             tmp.tm_hour = n;
166             break;
167         case 5:
168             tmp.tm_min = n;
169             break;
170         case 6:
171             tmp.tm_sec = n;
172             break;
173         }
174     }
175
176     /*
177      * Optional fractional seconds: decimal point followed by one or more
178      * digits.
179      */
180     if (d->type == V_ASN1_GENERALIZEDTIME && a[o] == '.') {
181         if (strict)
182             /* RFC 5280 forbids fractional seconds */
183             goto err;
184         if (++o == l)
185             goto err;
186         i = o;
187         while ((o < l) && (a[o] >= '0') && (a[o] <= '9'))
188             o++;
189         /* Must have at least one digit after decimal point */
190         if (i == o)
191             goto err;
192         /* no more bytes to read, but we haven't seen time-zone yet */
193         if (o == l)
194             goto err;
195     }
196
197     /*
198      * 'o' will never point to '\0' at this point, the only chance
199      * 'o' can point to '\0' is either the subsequent if or the first
200      * else if is true.
201      */
202     if (a[o] == 'Z') {
203         o++;
204     } else if (!strict && ((a[o] == '+') || (a[o] == '-'))) {
205         int offsign = a[o] == '-' ? 1 : -1;
206         int offset = 0;
207
208         o++;
209         /*
210          * if not equal, no need to do subsequent checks
211          * since the following for-loop will add 'o' by 4
212          * and the final return statement will check if 'l'
213          * and 'o' are equal.
214          */
215         if (o + 4 != l)
216             goto err;
217         for (i = end; i < end + 2; i++) {
218             if ((a[o] < '0') || (a[o] > '9'))
219                 goto err;
220             n = a[o] - '0';
221             o++;
222             if ((a[o] < '0') || (a[o] > '9'))
223                 goto err;
224             n = (n * 10) + a[o] - '0';
225             i2 = (d->type == V_ASN1_UTCTIME) ? i + 1 : i;
226             if ((n < min[i2]) || (n > max[i2]))
227                 goto err;
228             /* if tm is NULL, no need to adjust */
229             if (tm != NULL) {
230                 if (i == end)
231                     offset = n * 3600;
232                 else if (i == end + 1)
233                     offset += n * 60;
234             }
235             o++;
236         }
237         if (offset && !OPENSSL_gmtime_adj(&tmp, 0, offset * offsign))
238             goto err;
239     } else {
240         /* not Z, or not +/- in non-strict mode */
241         goto err;
242     }
243     if (o == l) {
244         /* success, check if tm should be filled */
245         if (tm != NULL)
246             *tm = tmp;
247         return 1;
248     }
249  err:
250     return 0;
251 }
252
253 ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t)
254 {
255     return ASN1_TIME_adj(s, t, 0, 0);
256 }
257
258 ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t,
259                          int offset_day, long offset_sec)
260 {
261     struct tm *ts;
262     struct tm data;
263
264     ts = OPENSSL_gmtime(&t, &data);
265     if (ts == NULL) {
266         ASN1err(ASN1_F_ASN1_TIME_ADJ, ASN1_R_ERROR_GETTING_TIME);
267         return NULL;
268     }
269     if (offset_day || offset_sec) {
270         if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
271             return NULL;
272     }
273     if ((ts->tm_year >= 50) && (ts->tm_year < 150))
274         return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
275     return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);
276 }
277
278 int ASN1_TIME_check(const ASN1_TIME *t)
279 {
280     if (t->type == V_ASN1_GENERALIZEDTIME)
281         return ASN1_GENERALIZEDTIME_check(t);
282     else if (t->type == V_ASN1_UTCTIME)
283         return ASN1_UTCTIME_check(t);
284     return 0;
285 }
286
287 /* Convert an ASN1_TIME structure to GeneralizedTime */
288 ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(const ASN1_TIME *t,
289                                                    ASN1_GENERALIZEDTIME **out)
290 {
291     ASN1_GENERALIZEDTIME *ret = NULL;
292     char *str;
293
294     if (!ASN1_TIME_check(t))
295         return NULL;
296
297     if (out == NULL || *out == NULL) {
298         if ((ret = ASN1_GENERALIZEDTIME_new()) == NULL)
299             goto err;
300     } else
301         ret = *out;
302
303     /* If already GeneralizedTime just copy across */
304     if (t->type == V_ASN1_GENERALIZEDTIME) {
305         if (!ASN1_STRING_set(ret, t->data, t->length))
306             goto err;
307         goto done;
308     }
309
310     /*
311      * Grow the string by two bytes.
312      * The actual allocation is t->length + 3 to include a terminator byte.
313      */
314     if (!ASN1_STRING_set(ret, NULL, t->length + 2))
315         goto err;
316     str = (char *)ret->data;
317     /* Work out the century and prepend */
318     memcpy(str, t->data[0] >= '5' ? "19" : "20", 2);
319     /*
320      * t->length + 1 is the size of the data and the allocated buffer has
321      * this much space after the first two characters.
322      */
323     OPENSSL_strlcpy(str + 2, (const char *)t->data, t->length + 1);
324
325  done:
326    if (out != NULL && *out == NULL)
327        *out = ret;
328    return ret;
329
330  err:
331     if (out == NULL || *out != ret)
332         ASN1_GENERALIZEDTIME_free(ret);
333     return NULL;
334 }
335
336 int ASN1_TIME_set_string(ASN1_TIME *s, const char *str)
337 {
338     ASN1_TIME t;
339
340     t.length = strlen(str);
341     t.data = (unsigned char *)str;
342     t.flags = 0;
343
344     t.type = V_ASN1_UTCTIME;
345
346     if (!ASN1_TIME_check(&t)) {
347         t.type = V_ASN1_GENERALIZEDTIME;
348         if (!ASN1_TIME_check(&t))
349             return 0;
350     }
351
352     if (s && !ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t))
353         return 0;
354
355     return 1;
356 }
357
358 int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str)
359 {
360     ASN1_TIME t;
361     struct tm tm;
362     int rv = 0;
363
364     t.length = strlen(str);
365     t.data = (unsigned char *)str;
366     t.flags = ASN1_STRING_FLAG_X509_TIME;
367
368     t.type = V_ASN1_UTCTIME;
369
370     if (!ASN1_TIME_check(&t)) {
371         t.type = V_ASN1_GENERALIZEDTIME;
372         if (!ASN1_TIME_check(&t))
373             goto out;
374     }
375
376     /*
377      * Per RFC 5280 (section 4.1.2.5.), the valid input time
378      * strings should be encoded with the following rules:
379      *
380      * 1. UTC: YYMMDDHHMMSSZ, if YY < 50 (20YY) --> UTC: YYMMDDHHMMSSZ
381      * 2. UTC: YYMMDDHHMMSSZ, if YY >= 50 (19YY) --> UTC: YYMMDDHHMMSSZ
382      * 3. G'd: YYYYMMDDHHMMSSZ, if YYYY >= 2050 --> G'd: YYYYMMDDHHMMSSZ
383      * 4. G'd: YYYYMMDDHHMMSSZ, if YYYY < 2050 --> UTC: YYMMDDHHMMSSZ
384      *
385      * Only strings of the 4th rule should be reformatted, but since a
386      * UTC can only present [1950, 2050), so if the given time string
387      * is less than 1950 (e.g. 19230419000000Z), we do nothing...
388      */
389
390     if (s != NULL && t.type == V_ASN1_GENERALIZEDTIME) {
391         if (!asn1_time_to_tm(&tm, &t))
392             goto out;
393         if (tm.tm_year >= 50 && tm.tm_year < 150) {
394             t.length -= 2;
395             /*
396              * it's OK to let original t.data go since that's assigned
397              * to a piece of memory allocated outside of this function.
398              * new t.data would be freed after ASN1_STRING_copy is done.
399              */
400             t.data = OPENSSL_zalloc(t.length + 1);
401             if (t.data == NULL)
402                 goto out;
403             memcpy(t.data, str + 2, t.length);
404             t.type = V_ASN1_UTCTIME;
405         }
406     }
407
408     if (s == NULL || ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t))
409         rv = 1;
410
411     if (t.data != (unsigned char *)str)
412         OPENSSL_free(t.data);
413 out:
414     return rv;
415 }
416
417 int ASN1_TIME_to_tm(const ASN1_TIME *s, struct tm *tm)
418 {
419     if (s == NULL) {
420         time_t now_t;
421
422         time(&now_t);
423         memset(tm, 0, sizeof(*tm));
424         if (OPENSSL_gmtime(&now_t, tm))
425             return 1;
426         return 0;
427     }
428
429     return asn1_time_to_tm(tm, s);
430 }
431
432 int ASN1_TIME_diff(int *pday, int *psec,
433                    const ASN1_TIME *from, const ASN1_TIME *to)
434 {
435     struct tm tm_from, tm_to;
436
437     if (!ASN1_TIME_to_tm(from, &tm_from))
438         return 0;
439     if (!ASN1_TIME_to_tm(to, &tm_to))
440         return 0;
441     return OPENSSL_gmtime_diff(pday, psec, &tm_from, &tm_to);
442 }
443
444 int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
445 {
446     if (tm->type == V_ASN1_UTCTIME)
447         return ASN1_UTCTIME_print(bp, tm);
448     if (tm->type == V_ASN1_GENERALIZEDTIME)
449         return ASN1_GENERALIZEDTIME_print(bp, tm);
450     BIO_write(bp, "Bad time value", 14);
451     return (0);
452 }