Memory bounds checking in asn1 code.
[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 ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t)
28 {
29     return ASN1_TIME_adj(s, t, 0, 0);
30 }
31
32 ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t,
33                          int offset_day, long offset_sec)
34 {
35     struct tm *ts;
36     struct tm data;
37
38     ts = OPENSSL_gmtime(&t, &data);
39     if (ts == NULL) {
40         ASN1err(ASN1_F_ASN1_TIME_ADJ, ASN1_R_ERROR_GETTING_TIME);
41         return NULL;
42     }
43     if (offset_day || offset_sec) {
44         if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
45             return NULL;
46     }
47     if ((ts->tm_year >= 50) && (ts->tm_year < 150))
48         return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
49     return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);
50 }
51
52 int ASN1_TIME_check(const ASN1_TIME *t)
53 {
54     if (t->type == V_ASN1_GENERALIZEDTIME)
55         return ASN1_GENERALIZEDTIME_check(t);
56     else if (t->type == V_ASN1_UTCTIME)
57         return ASN1_UTCTIME_check(t);
58     return 0;
59 }
60
61 /* Convert an ASN1_TIME structure to GeneralizedTime */
62 ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(const ASN1_TIME *t,
63                                                    ASN1_GENERALIZEDTIME **out)
64 {
65     ASN1_GENERALIZEDTIME *ret = NULL;
66     char *str;
67
68     if (!ASN1_TIME_check(t))
69         return NULL;
70
71     if (out == NULL || *out == NULL) {
72         if ((ret = ASN1_GENERALIZEDTIME_new()) == NULL)
73             goto err;
74     } else
75         ret = *out;
76
77     /* If already GeneralizedTime just copy across */
78     if (t->type == V_ASN1_GENERALIZEDTIME) {
79         if (!ASN1_STRING_set(ret, t->data, t->length))
80             goto err;
81         goto done;
82     }
83
84     /*
85      * Grow the string by two bytes.
86      * The actual allocation is t->length + 3 to include a terminator byte.
87      */
88     if (!ASN1_STRING_set(ret, NULL, t->length + 2))
89         goto err;
90     str = (char *)ret->data;
91     /* Work out the century and prepend */
92     memcpy(str, t->data[0] >= '5' ? "19" : "20", 2);
93     /*
94      * t->length + 1 is the size of the data and the allocated buffer has
95      * this much space after the first two characters.
96      */
97     OPENSSL_strlcpy(str + 2, (const char *)t->data, t->length + 1);
98
99  done:
100    if (out != NULL && *out == NULL)
101        *out = ret;
102    return ret;
103
104  err:
105     if (out == NULL || *out != ret)
106         ASN1_GENERALIZEDTIME_free(ret);
107     return NULL;
108 }
109
110 int ASN1_TIME_set_string(ASN1_TIME *s, const char *str)
111 {
112     ASN1_TIME t;
113
114     t.length = strlen(str);
115     t.data = (unsigned char *)str;
116     t.flags = 0;
117
118     t.type = V_ASN1_UTCTIME;
119
120     if (!ASN1_TIME_check(&t)) {
121         t.type = V_ASN1_GENERALIZEDTIME;
122         if (!ASN1_TIME_check(&t))
123             return 0;
124     }
125
126     if (s && !ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t))
127         return 0;
128
129     return 1;
130 }
131
132 int ASN1_TIME_set_string_X509(ASN1_TIME *s, const char *str)
133 {
134     ASN1_TIME t;
135     struct tm tm;
136     int rv = 0;
137
138     t.length = strlen(str);
139     t.data = (unsigned char *)str;
140     t.flags = ASN1_STRING_FLAG_X509_TIME;
141
142     t.type = V_ASN1_UTCTIME;
143
144     if (!ASN1_TIME_check(&t)) {
145         t.type = V_ASN1_GENERALIZEDTIME;
146         if (!ASN1_TIME_check(&t))
147             goto out;
148     }
149
150     /*
151      * Per RFC 5280 (section 4.1.2.5.), the valid input time
152      * strings should be encoded with the following rules:
153      *
154      * 1. UTC: YYMMDDHHMMSSZ, if YY < 50 (20YY) --> UTC: YYMMDDHHMMSSZ
155      * 2. UTC: YYMMDDHHMMSSZ, if YY >= 50 (19YY) --> UTC: YYMMDDHHMMSSZ
156      * 3. G'd: YYYYMMDDHHMMSSZ, if YYYY >= 2050 --> G'd: YYYYMMDDHHMMSSZ
157      * 4. G'd: YYYYMMDDHHMMSSZ, if YYYY < 2050 --> UTC: YYMMDDHHMMSSZ
158      *
159      * Only strings of the 4th rule should be reformatted, but since a
160      * UTC can only present [1950, 2050), so if the given time string
161      * is less than 1950 (e.g. 19230419000000Z), we do nothing...
162      */
163
164     if (s != NULL && t.type == V_ASN1_GENERALIZEDTIME) {
165         if (!asn1_generalizedtime_to_tm(&tm, &t))
166             goto out;
167         if (tm.tm_year >= 50 && tm.tm_year < 150) {
168             t.length -= 2;
169             /*
170              * it's OK to let original t.data go since that's assigned
171              * to a piece of memory allocated outside of this function.
172              * new t.data would be freed after ASN1_STRING_copy is done.
173              */
174             t.data = OPENSSL_zalloc(t.length + 1);
175             if (t.data == NULL)
176                 goto out;
177             memcpy(t.data, str + 2, t.length);
178             t.type = V_ASN1_UTCTIME;
179         }
180     }
181
182     if (s == NULL || ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t))
183         rv = 1;
184
185     if (t.data != (unsigned char *)str)
186         OPENSSL_free(t.data);
187 out:
188     return rv;
189 }
190
191 int ASN1_TIME_to_tm(const ASN1_TIME *s, struct tm *tm)
192 {
193     if (s == NULL) {
194         time_t now_t;
195
196         time(&now_t);
197         memset(tm, 0, sizeof(*tm));
198         if (OPENSSL_gmtime(&now_t, tm))
199             return 1;
200         return 0;
201     }
202
203     if (s->type == V_ASN1_UTCTIME) {
204         memset(tm, 0, sizeof(*tm));
205         return asn1_utctime_to_tm(tm, s);
206     }
207     if (s->type == V_ASN1_GENERALIZEDTIME) {
208         memset(tm, 0, sizeof(*tm));
209         return asn1_generalizedtime_to_tm(tm, s);
210     }
211
212     return 0;
213 }
214
215 int ASN1_TIME_diff(int *pday, int *psec,
216                    const ASN1_TIME *from, const ASN1_TIME *to)
217 {
218     struct tm tm_from, tm_to;
219
220     if (!ASN1_TIME_to_tm(from, &tm_from))
221         return 0;
222     if (!ASN1_TIME_to_tm(to, &tm_to))
223         return 0;
224     return OPENSSL_gmtime_diff(pday, psec, &tm_from, &tm_to);
225 }
226
227 int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
228 {
229     if (tm->type == V_ASN1_UTCTIME)
230         return ASN1_UTCTIME_print(bp, tm);
231     if (tm->type == V_ASN1_GENERALIZEDTIME)
232         return ASN1_GENERALIZEDTIME_print(bp, tm);
233     BIO_write(bp, "Bad time value", 14);
234     return (0);
235 }