]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/a_utctm.c
Update SEE ALSO sections.
[thirdparty/openssl.git] / crypto / asn1 / a_utctm.c
CommitLineData
d02b48c6 1/* crypto/asn1/a_utctm.c */
58964a49 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
0f113f3e 8 *
d02b48c6
RE
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
0f113f3e 15 *
d02b48c6
RE
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
0f113f3e 22 *
d02b48c6
RE
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
0f113f3e 37 * 4. If you include any Windows specific code (or a derivative thereof) from
d02b48c6
RE
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
0f113f3e 40 *
d02b48c6
RE
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
0f113f3e 52 *
d02b48c6
RE
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <time.h>
b39fc560 61#include "internal/cryptlib.h"
7ab1a391 62#include <openssl/asn1.h>
1c455bc0 63#include "asn1_locl.h"
d02b48c6 64
1c455bc0 65int asn1_utctime_to_tm(struct tm *tm, const ASN1_UTCTIME *d)
0f113f3e
MC
66{
67 static const int min[8] = { 0, 1, 1, 0, 0, 0, 0, 0 };
68 static const int max[8] = { 99, 12, 31, 23, 59, 59, 12, 59 };
69 char *a;
70 int n, i, l, o;
71
72 if (d->type != V_ASN1_UTCTIME)
73 return (0);
74 l = d->length;
75 a = (char *)d->data;
76 o = 0;
77
78 if (l < 11)
79 goto err;
80 for (i = 0; i < 6; i++) {
81 if ((i == 5) && ((a[o] == 'Z') || (a[o] == '+') || (a[o] == '-'))) {
82 i++;
83 if (tm)
84 tm->tm_sec = 0;
85 break;
86 }
87 if ((a[o] < '0') || (a[o] > '9'))
88 goto err;
89 n = a[o] - '0';
90 if (++o > l)
91 goto err;
92
93 if ((a[o] < '0') || (a[o] > '9'))
94 goto err;
95 n = (n * 10) + a[o] - '0';
96 if (++o > l)
97 goto err;
98
99 if ((n < min[i]) || (n > max[i]))
100 goto err;
101 if (tm) {
102 switch (i) {
103 case 0:
104 tm->tm_year = n < 50 ? n + 100 : n;
105 break;
106 case 1:
107 tm->tm_mon = n - 1;
108 break;
109 case 2:
110 tm->tm_mday = n;
111 break;
112 case 3:
113 tm->tm_hour = n;
114 break;
115 case 4:
116 tm->tm_min = n;
117 break;
118 case 5:
119 tm->tm_sec = n;
120 break;
121 }
122 }
123 }
124 if (a[o] == 'Z')
125 o++;
126 else if ((a[o] == '+') || (a[o] == '-')) {
127 int offsign = a[o] == '-' ? -1 : 1, offset = 0;
128 o++;
129 if (o + 4 > l)
130 goto err;
131 for (i = 6; i < 8; i++) {
132 if ((a[o] < '0') || (a[o] > '9'))
133 goto err;
134 n = a[o] - '0';
135 o++;
136 if ((a[o] < '0') || (a[o] > '9'))
137 goto err;
138 n = (n * 10) + a[o] - '0';
139 if ((n < min[i]) || (n > max[i]))
140 goto err;
141 if (tm) {
142 if (i == 6)
143 offset = n * 3600;
144 else if (i == 7)
145 offset += n * 60;
146 }
147 o++;
148 }
149 if (offset && !OPENSSL_gmtime_adj(tm, 0, offset * offsign))
150 return 0;
151 }
152 return o == l;
153 err:
154 return 0;
155}
1c455bc0
DSH
156
157int ASN1_UTCTIME_check(const ASN1_UTCTIME *d)
0f113f3e
MC
158{
159 return asn1_utctime_to_tm(NULL, d);
160}
d02b48c6 161
875a644a 162int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str)
0f113f3e
MC
163{
164 ASN1_UTCTIME t;
165
166 t.type = V_ASN1_UTCTIME;
167 t.length = strlen(str);
168 t.data = (unsigned char *)str;
169 if (ASN1_UTCTIME_check(&t)) {
170 if (s != NULL) {
171 if (!ASN1_STRING_set((ASN1_STRING *)s,
172 (unsigned char *)str, t.length))
173 return 0;
174 s->type = V_ASN1_UTCTIME;
175 }
176 return (1);
177 } else
178 return (0);
179}
58964a49 180
6b691a5c 181ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t)
0f113f3e
MC
182{
183 return ASN1_UTCTIME_adj(s, t, 0, 0);
184}
87d3a0cd
DSH
185
186ASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
0f113f3e
MC
187 int offset_day, long offset_sec)
188{
189 char *p;
190 struct tm *ts;
191 struct tm data;
192 size_t len = 20;
193 int free_s = 0;
194
195 if (s == NULL) {
f422a514 196 s = ASN1_UTCTIME_new();
0dfb9398
RS
197 if (s == NULL)
198 goto err;
199 free_s = 1;
0f113f3e 200 }
0f113f3e
MC
201
202 ts = OPENSSL_gmtime(&t, &data);
203 if (ts == NULL)
204 goto err;
205
206 if (offset_day || offset_sec) {
207 if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
208 goto err;
209 }
210
211 if ((ts->tm_year < 50) || (ts->tm_year >= 150))
212 goto err;
213
214 p = (char *)s->data;
215 if ((p == NULL) || ((size_t)s->length < len)) {
216 p = OPENSSL_malloc(len);
217 if (p == NULL) {
218 ASN1err(ASN1_F_ASN1_UTCTIME_ADJ, ERR_R_MALLOC_FAILURE);
219 goto err;
220 }
b548a1f1 221 OPENSSL_free(s->data);
0f113f3e
MC
222 s->data = (unsigned char *)p;
223 }
224
225 BIO_snprintf(p, len, "%02d%02d%02d%02d%02d%02dZ", ts->tm_year % 100,
226 ts->tm_mon + 1, ts->tm_mday, ts->tm_hour, ts->tm_min,
227 ts->tm_sec);
228 s->length = strlen(p);
229 s->type = V_ASN1_UTCTIME;
a53955d8 230#ifdef CHARSET_EBCDIC_not
0f113f3e 231 ebcdic2ascii(s->data, s->data, s->length);
a53955d8 232#endif
0f113f3e
MC
233 return (s);
234 err:
0dfb9398 235 if (free_s)
f422a514 236 ASN1_UTCTIME_free(s);
0f113f3e
MC
237 return NULL;
238}
61f175f4
BM
239
240int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t)
0f113f3e
MC
241{
242 struct tm stm, ttm;
243 int day, sec;
244
245 if (!asn1_utctime_to_tm(&stm, s))
246 return -2;
247
248 if (!OPENSSL_gmtime(&t, &ttm))
249 return -2;
250
da27006d 251 if (!OPENSSL_gmtime_diff(&day, &sec, &ttm, &stm))
0f113f3e
MC
252 return -2;
253
254 if (day > 0)
255 return 1;
256 if (day < 0)
257 return -1;
258 if (sec > 0)
259 return 1;
260 if (sec < 0)
261 return -1;
262 return 0;
263}