Constify some input buffers in asn1
[openssl.git] / crypto / asn1 / a_gentm.c
1 /*
2  * Copyright 1995-2016 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  * GENERALIZEDTIME implementation. Based on UTCTIME
12  */
13
14 #include <stdio.h>
15 #include <time.h>
16 #include "internal/cryptlib.h"
17 #include <openssl/asn1.h>
18 #include "asn1_locl.h"
19
20 int asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d)
21 {
22     static const int min[9] = { 0, 0, 1, 1, 0, 0, 0, 0, 0 };
23     static const int max[9] = { 99, 99, 12, 31, 23, 59, 59, 12, 59 };
24     char *a;
25     int n, i, l, o;
26
27     if (d->type != V_ASN1_GENERALIZEDTIME)
28         return (0);
29     l = d->length;
30     a = (char *)d->data;
31     o = 0;
32     /*
33      * GENERALIZEDTIME is similar to UTCTIME except the year is represented
34      * as YYYY. This stuff treats everything as a two digit field so make
35      * first two fields 00 to 99
36      */
37     if (l < 13)
38         goto err;
39     for (i = 0; i < 7; i++) {
40         if ((i == 6) && ((a[o] == 'Z') || (a[o] == '+') || (a[o] == '-'))) {
41             i++;
42             if (tm)
43                 tm->tm_sec = 0;
44             break;
45         }
46         if ((a[o] < '0') || (a[o] > '9'))
47             goto err;
48         n = a[o] - '0';
49         if (++o > l)
50             goto err;
51
52         if ((a[o] < '0') || (a[o] > '9'))
53             goto err;
54         n = (n * 10) + a[o] - '0';
55         if (++o > l)
56             goto err;
57
58         if ((n < min[i]) || (n > max[i]))
59             goto err;
60         if (tm) {
61             switch (i) {
62             case 0:
63                 tm->tm_year = n * 100 - 1900;
64                 break;
65             case 1:
66                 tm->tm_year += n;
67                 break;
68             case 2:
69                 tm->tm_mon = n - 1;
70                 break;
71             case 3:
72                 tm->tm_mday = n;
73                 break;
74             case 4:
75                 tm->tm_hour = n;
76                 break;
77             case 5:
78                 tm->tm_min = n;
79                 break;
80             case 6:
81                 tm->tm_sec = n;
82                 break;
83             }
84         }
85     }
86     /*
87      * Optional fractional seconds: decimal point followed by one or more
88      * digits.
89      */
90     if (a[o] == '.') {
91         if (++o > l)
92             goto err;
93         i = o;
94         while ((a[o] >= '0') && (a[o] <= '9') && (o <= l))
95             o++;
96         /* Must have at least one digit after decimal point */
97         if (i == o)
98             goto err;
99     }
100
101     if (a[o] == 'Z')
102         o++;
103     else if ((a[o] == '+') || (a[o] == '-')) {
104         int offsign = a[o] == '-' ? -1 : 1, offset = 0;
105         o++;
106         if (o + 4 > l)
107             goto err;
108         for (i = 7; i < 9; i++) {
109             if ((a[o] < '0') || (a[o] > '9'))
110                 goto err;
111             n = a[o] - '0';
112             o++;
113             if ((a[o] < '0') || (a[o] > '9'))
114                 goto err;
115             n = (n * 10) + a[o] - '0';
116             if ((n < min[i]) || (n > max[i]))
117                 goto err;
118             if (tm) {
119                 if (i == 7)
120                     offset = n * 3600;
121                 else if (i == 8)
122                     offset += n * 60;
123             }
124             o++;
125         }
126         if (offset && !OPENSSL_gmtime_adj(tm, 0, offset * offsign))
127             return 0;
128     } else if (a[o]) {
129         /* Missing time zone information. */
130         goto err;
131     }
132     return (o == l);
133  err:
134     return (0);
135 }
136
137 int ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d)
138 {
139     return asn1_generalizedtime_to_tm(NULL, d);
140 }
141
142 int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str)
143 {
144     ASN1_GENERALIZEDTIME t;
145
146     t.type = V_ASN1_GENERALIZEDTIME;
147     t.length = strlen(str);
148     t.data = (unsigned char *)str;
149     if (ASN1_GENERALIZEDTIME_check(&t)) {
150         if (s != NULL) {
151             if (!ASN1_STRING_set((ASN1_STRING *)s,
152                                  (unsigned char *)str, t.length))
153                 return 0;
154             s->type = V_ASN1_GENERALIZEDTIME;
155         }
156         return (1);
157     } else
158         return (0);
159 }
160
161 ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
162                                                time_t t)
163 {
164     return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0);
165 }
166
167 ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
168                                                time_t t, int offset_day,
169                                                long offset_sec)
170 {
171     char *p;
172     struct tm *ts;
173     struct tm data;
174     size_t len = 20;
175     ASN1_GENERALIZEDTIME *tmps = NULL;
176
177     if (s == NULL)
178         tmps = ASN1_GENERALIZEDTIME_new();
179     else
180         tmps = s;
181     if (tmps == NULL)
182         return NULL;
183
184     ts = OPENSSL_gmtime(&t, &data);
185     if (ts == NULL)
186         goto err;
187
188     if (offset_day || offset_sec) {
189         if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
190             goto err;
191     }
192
193     p = (char *)tmps->data;
194     if ((p == NULL) || ((size_t)tmps->length < len)) {
195         p = OPENSSL_malloc(len);
196         if (p == NULL) {
197             ASN1err(ASN1_F_ASN1_GENERALIZEDTIME_ADJ, ERR_R_MALLOC_FAILURE);
198             goto err;
199         }
200         OPENSSL_free(tmps->data);
201         tmps->data = (unsigned char *)p;
202     }
203
204     BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ", ts->tm_year + 1900,
205                  ts->tm_mon + 1, ts->tm_mday, ts->tm_hour, ts->tm_min,
206                  ts->tm_sec);
207     tmps->length = strlen(p);
208     tmps->type = V_ASN1_GENERALIZEDTIME;
209 #ifdef CHARSET_EBCDIC_not
210     ebcdic2ascii(tmps->data, tmps->data, tmps->length);
211 #endif
212     return tmps;
213  err:
214     if (s == NULL)
215         ASN1_GENERALIZEDTIME_free(tmps);
216     return NULL;
217 }
218
219 const char *_asn1_mon[12] = {
220     "Jan", "Feb", "Mar", "Apr", "May", "Jun",
221     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
222 };
223
224 int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
225 {
226     char *v;
227     int gmt = 0;
228     int i;
229     int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0;
230     char *f = NULL;
231     int f_len = 0;
232
233     i = tm->length;
234     v = (char *)tm->data;
235
236     if (i < 12)
237         goto err;
238     if (v[i - 1] == 'Z')
239         gmt = 1;
240     for (i = 0; i < 12; i++)
241         if ((v[i] > '9') || (v[i] < '0'))
242             goto err;
243     y = (v[0] - '0') * 1000 + (v[1] - '0') * 100
244         + (v[2] - '0') * 10 + (v[3] - '0');
245     M = (v[4] - '0') * 10 + (v[5] - '0');
246     if ((M > 12) || (M < 1))
247         goto err;
248     d = (v[6] - '0') * 10 + (v[7] - '0');
249     h = (v[8] - '0') * 10 + (v[9] - '0');
250     m = (v[10] - '0') * 10 + (v[11] - '0');
251     if (tm->length >= 14 &&
252         (v[12] >= '0') && (v[12] <= '9') &&
253         (v[13] >= '0') && (v[13] <= '9')) {
254         s = (v[12] - '0') * 10 + (v[13] - '0');
255         /* Check for fractions of seconds. */
256         if (tm->length >= 15 && v[14] == '.') {
257             int l = tm->length;
258             f = &v[14];         /* The decimal point. */
259             f_len = 1;
260             while (14 + f_len < l && f[f_len] >= '0' && f[f_len] <= '9')
261                 ++f_len;
262         }
263     }
264
265     if (BIO_printf(bp, "%s %2d %02d:%02d:%02d%.*s %d%s",
266                    _asn1_mon[M - 1], d, h, m, s, f_len, f, y,
267                    (gmt) ? " GMT" : "") <= 0)
268         return (0);
269     else
270         return (1);
271  err:
272     BIO_write(bp, "Bad time value", 14);
273     return (0);
274 }