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 | #include <stdio.h>
|
---|
11 | #include <time.h>
|
---|
12 | #include "internal/cryptlib.h"
|
---|
13 | #include <openssl/asn1.h>
|
---|
14 | #include "asn1_locl.h"
|
---|
15 |
|
---|
16 | int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d)
|
---|
17 | {
|
---|
18 | static const int min[8] = { 0, 1, 1, 0, 0, 0, 0, 0 };
|
---|
19 | static const int max[8] = { 99, 12, 31, 23, 59, 59, 12, 59 };
|
---|
20 | char *a;
|
---|
21 | int n, i, l, o;
|
---|
22 |
|
---|
23 | if (d->type != V_ASN1_UTCTIME)
|
---|
24 | return (0);
|
---|
25 | l = d->length;
|
---|
26 | a = (char *)d->data;
|
---|
27 | o = 0;
|
---|
28 |
|
---|
29 | if (l < 11)
|
---|
30 | goto err;
|
---|
31 | for (i = 0; i < 6; i++) {
|
---|
32 | if ((i == 5) && ((a[o] == 'Z') || (a[o] == '+') || (a[o] == '-'))) {
|
---|
33 | i++;
|
---|
34 | if (tm)
|
---|
35 | tm->tm_sec = 0;
|
---|
36 | break;
|
---|
37 | }
|
---|
38 | if ((a[o] < '0') || (a[o] > '9'))
|
---|
39 | goto err;
|
---|
40 | n = a[o] - '0';
|
---|
41 | if (++o > l)
|
---|
42 | goto err;
|
---|
43 |
|
---|
44 | if ((a[o] < '0') || (a[o] > '9'))
|
---|
45 | goto err;
|
---|
46 | n = (n * 10) + a[o] - '0';
|
---|
47 | if (++o > l)
|
---|
48 | goto err;
|
---|
49 |
|
---|
50 | if ((n < min[i]) || (n > max[i]))
|
---|
51 | goto err;
|
---|
52 | if (tm) {
|
---|
53 | switch (i) {
|
---|
54 | case 0:
|
---|
55 | tm->tm_year = n < 50 ? n + 100 : n;
|
---|
56 | break;
|
---|
57 | case 1:
|
---|
58 | tm->tm_mon = n - 1;
|
---|
59 | break;
|
---|
60 | case 2:
|
---|
61 | tm->tm_mday = n;
|
---|
62 | break;
|
---|
63 | case 3:
|
---|
64 | tm->tm_hour = n;
|
---|
65 | break;
|
---|
66 | case 4:
|
---|
67 | tm->tm_min = n;
|
---|
68 | break;
|
---|
69 | case 5:
|
---|
70 | tm->tm_sec = n;
|
---|
71 | break;
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 | if (a[o] == 'Z')
|
---|
76 | o++;
|
---|
77 | else if ((a[o] == '+') || (a[o] == '-')) {
|
---|
78 | int offsign = a[o] == '-' ? -1 : 1, offset = 0;
|
---|
79 | o++;
|
---|
80 | if (o + 4 > l)
|
---|
81 | goto err;
|
---|
82 | for (i = 6; i < 8; i++) {
|
---|
83 | if ((a[o] < '0') || (a[o] > '9'))
|
---|
84 | goto err;
|
---|
85 | n = a[o] - '0';
|
---|
86 | o++;
|
---|
87 | if ((a[o] < '0') || (a[o] > '9'))
|
---|
88 | goto err;
|
---|
89 | n = (n * 10) + a[o] - '0';
|
---|
90 | if ((n < min[i]) || (n > max[i]))
|
---|
91 | goto err;
|
---|
92 | if (tm) {
|
---|
93 | if (i == 6)
|
---|
94 | offset = n * 3600;
|
---|
95 | else if (i == 7)
|
---|
96 | offset += n * 60;
|
---|
97 | }
|
---|
98 | o++;
|
---|
99 | }
|
---|
100 | if (offset && !OPENSSL_gmtime_adj(tm, 0, offset * offsign))
|
---|
101 | return 0;
|
---|
102 | }
|
---|
103 | return o == l;
|
---|
104 | err:
|
---|
105 | return 0;
|
---|
106 | }
|
---|
107 |
|
---|
108 | int ASN1_UTCTIME_check(const ASN1_UTCTIME *d)
|
---|
109 | {
|
---|
110 | return asn1_utctime_to_tm(NULL, d);
|
---|
111 | }
|
---|
112 |
|
---|
113 | int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str)
|
---|
114 | {
|
---|
115 | ASN1_UTCTIME t;
|
---|
116 |
|
---|
117 | t.type = V_ASN1_UTCTIME;
|
---|
118 | t.length = strlen(str);
|
---|
119 | t.data = (unsigned char *)str;
|
---|
120 | if (ASN1_UTCTIME_check(&t)) {
|
---|
121 | if (s != NULL) {
|
---|
122 | if (!ASN1_STRING_set((ASN1_STRING *)s, str, t.length))
|
---|
123 | return 0;
|
---|
124 | s->type = V_ASN1_UTCTIME;
|
---|
125 | }
|
---|
126 | return (1);
|
---|
127 | } else
|
---|
128 | return (0);
|
---|
129 | }
|
---|
130 |
|
---|
131 | ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t)
|
---|
132 | {
|
---|
133 | return ASN1_UTCTIME_adj(s, t, 0, 0);
|
---|
134 | }
|
---|
135 |
|
---|
136 | ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
|
---|
137 | int offset_day, long offset_sec)
|
---|
138 | {
|
---|
139 | char *p;
|
---|
140 | struct tm *ts;
|
---|
141 | struct tm data;
|
---|
142 | size_t len = 20;
|
---|
143 | int free_s = 0;
|
---|
144 |
|
---|
145 | if (s == NULL) {
|
---|
146 | s = ASN1_UTCTIME_new();
|
---|
147 | if (s == NULL)
|
---|
148 | goto err;
|
---|
149 | free_s = 1;
|
---|
150 | }
|
---|
151 |
|
---|
152 | ts = OPENSSL_gmtime(&t, &data);
|
---|
153 | if (ts == NULL)
|
---|
154 | goto err;
|
---|
155 |
|
---|
156 | if (offset_day || offset_sec) {
|
---|
157 | if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
|
---|
158 | goto err;
|
---|
159 | }
|
---|
160 |
|
---|
161 | if ((ts->tm_year < 50) || (ts->tm_year >= 150))
|
---|
162 | goto err;
|
---|
163 |
|
---|
164 | p = (char *)s->data;
|
---|
165 | if ((p == NULL) || ((size_t)s->length < len)) {
|
---|
166 | p = OPENSSL_malloc(len);
|
---|
167 | if (p == NULL) {
|
---|
168 | ASN1err(ASN1_F_ASN1_UTCTIME_ADJ, ERR_R_MALLOC_FAILURE);
|
---|
169 | goto err;
|
---|
170 | }
|
---|
171 | OPENSSL_free(s->data);
|
---|
172 | s->data = (unsigned char *)p;
|
---|
173 | }
|
---|
174 |
|
---|
175 | BIO_snprintf(p, len, "%02d%02d%02d%02d%02d%02dZ", ts->tm_year % 100,
|
---|
176 | ts->tm_mon + 1, ts->tm_mday, ts->tm_hour, ts->tm_min,
|
---|
177 | ts->tm_sec);
|
---|
178 | s->length = strlen(p);
|
---|
179 | s->type = V_ASN1_UTCTIME;
|
---|
180 | #ifdef CHARSET_EBCDIC_not
|
---|
181 | ebcdic2ascii(s->data, s->data, s->length);
|
---|
182 | #endif
|
---|
183 | return (s);
|
---|
184 | err:
|
---|
185 | if (free_s)
|
---|
186 | ASN1_UTCTIME_free(s);
|
---|
187 | return NULL;
|
---|
188 | }
|
---|
189 |
|
---|
190 | int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t)
|
---|
191 | {
|
---|
192 | struct tm stm, ttm;
|
---|
193 | int day, sec;
|
---|
194 |
|
---|
195 | if (!asn1_utctime_to_tm(&stm, s))
|
---|
196 | return -2;
|
---|
197 |
|
---|
198 | if (!OPENSSL_gmtime(&t, &ttm))
|
---|
199 | return -2;
|
---|
200 |
|
---|
201 | if (!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm))
|
---|
202 | return -2;
|
---|
203 |
|
---|
204 | if (day > 0)
|
---|
205 | return 1;
|
---|
206 | if (day < 0)
|
---|
207 | return -1;
|
---|
208 | if (sec > 0)
|
---|
209 | return 1;
|
---|
210 | if (sec < 0)
|
---|
211 | return -1;
|
---|
212 | return 0;
|
---|
213 | }
|
---|
214 |
|
---|
215 | int ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm)
|
---|
216 | {
|
---|
217 | const char *v;
|
---|
218 | int gmt = 0;
|
---|
219 | int i;
|
---|
220 | int y = 0, M = 0, d = 0, h = 0, m = 0, s = 0;
|
---|
221 |
|
---|
222 | i = tm->length;
|
---|
223 | v = (const char *)tm->data;
|
---|
224 |
|
---|
225 | if (i < 10)
|
---|
226 | goto err;
|
---|
227 | if (v[i - 1] == 'Z')
|
---|
228 | gmt = 1;
|
---|
229 | for (i = 0; i < 10; i++)
|
---|
230 | if ((v[i] > '9') || (v[i] < '0'))
|
---|
231 | goto err;
|
---|
232 | y = (v[0] - '0') * 10 + (v[1] - '0');
|
---|
233 | if (y < 50)
|
---|
234 | y += 100;
|
---|
235 | M = (v[2] - '0') * 10 + (v[3] - '0');
|
---|
236 | if ((M > 12) || (M < 1))
|
---|
237 | goto err;
|
---|
238 | d = (v[4] - '0') * 10 + (v[5] - '0');
|
---|
239 | h = (v[6] - '0') * 10 + (v[7] - '0');
|
---|
240 | m = (v[8] - '0') * 10 + (v[9] - '0');
|
---|
241 | if (tm->length >= 12 &&
|
---|
242 | (v[10] >= '0') && (v[10] <= '9') && (v[11] >= '0') && (v[11] <= '9'))
|
---|
243 | s = (v[10] - '0') * 10 + (v[11] - '0');
|
---|
244 |
|
---|
245 | if (BIO_printf(bp, "%s %2d %02d:%02d:%02d %d%s",
|
---|
246 | _asn1_mon[M - 1], d, h, m, s, y + 1900,
|
---|
247 | (gmt) ? " GMT" : "") <= 0)
|
---|
248 | return (0);
|
---|
249 | else
|
---|
250 | return (1);
|
---|
251 | err:
|
---|
252 | BIO_write(bp, "Bad time value", 14);
|
---|
253 | return (0);
|
---|
254 | }
|
---|