155714Skris/* crypto/asn1/a_utctm.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8280304Sjkim *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15280304Sjkim *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22280304Sjkim *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37280304Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40280304Sjkim *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52280304Sjkim *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include <time.h>
6155714Skris#include "cryptlib.h"
62109998Smarkm#include "o_time.h"
6355714Skris#include <openssl/asn1.h>
6455714Skris
65109998Smarkm#if 0
6655714Skrisint i2d_ASN1_UTCTIME(ASN1_UTCTIME *a, unsigned char **pp)
67280304Sjkim{
68280304Sjkim# ifndef CHARSET_EBCDIC
69280304Sjkim    return (i2d_ASN1_bytes((ASN1_STRING *)a, pp,
70280304Sjkim                           V_ASN1_UTCTIME, V_ASN1_UNIVERSAL));
71280304Sjkim# else
72280304Sjkim    /* KLUDGE! We convert to ascii before writing DER */
73280304Sjkim    int len;
74280304Sjkim    char tmp[24];
75280304Sjkim    ASN1_STRING x = *(ASN1_STRING *)a;
7655714Skris
77280304Sjkim    len = x.length;
78280304Sjkim    ebcdic2ascii(tmp, x.data, (len >= sizeof tmp) ? sizeof tmp : len);
79280304Sjkim    x.data = tmp;
80280304Sjkim    return i2d_ASN1_bytes(&x, pp, V_ASN1_UTCTIME, V_ASN1_UNIVERSAL);
81280304Sjkim# endif
82280304Sjkim}
8355714Skris
8455714SkrisASN1_UTCTIME *d2i_ASN1_UTCTIME(ASN1_UTCTIME **a, unsigned char **pp,
85280304Sjkim                               long length)
86280304Sjkim{
87280304Sjkim    ASN1_UTCTIME *ret = NULL;
8855714Skris
89280304Sjkim    ret = (ASN1_UTCTIME *)d2i_ASN1_bytes((ASN1_STRING **)a, pp, length,
90280304Sjkim                                         V_ASN1_UTCTIME, V_ASN1_UNIVERSAL);
91280304Sjkim    if (ret == NULL) {
92280304Sjkim        ASN1err(ASN1_F_D2I_ASN1_UTCTIME, ERR_R_NESTED_ASN1_ERROR);
93280304Sjkim        return (NULL);
94280304Sjkim    }
95280304Sjkim# ifdef CHARSET_EBCDIC
96280304Sjkim    ascii2ebcdic(ret->data, ret->data, ret->length);
97280304Sjkim# endif
98280304Sjkim    if (!ASN1_UTCTIME_check(ret)) {
99280304Sjkim        ASN1err(ASN1_F_D2I_ASN1_UTCTIME, ASN1_R_INVALID_TIME_FORMAT);
100280304Sjkim        goto err;
101280304Sjkim    }
10255714Skris
103280304Sjkim    return (ret);
104280304Sjkim err:
105280304Sjkim    if ((ret != NULL) && ((a == NULL) || (*a != ret)))
106280304Sjkim        M_ASN1_UTCTIME_free(ret);
107280304Sjkim    return (NULL);
108280304Sjkim}
10955714Skris
110109998Smarkm#endif
111109998Smarkm
11255714Skrisint ASN1_UTCTIME_check(ASN1_UTCTIME *d)
113280304Sjkim{
114280304Sjkim    static const int min[8] = { 0, 1, 1, 0, 0, 0, 0, 0 };
115280304Sjkim    static const int max[8] = { 99, 12, 31, 23, 59, 59, 12, 59 };
116280304Sjkim    char *a;
117280304Sjkim    int n, i, l, o;
11855714Skris
119280304Sjkim    if (d->type != V_ASN1_UTCTIME)
120280304Sjkim        return (0);
121280304Sjkim    l = d->length;
122280304Sjkim    a = (char *)d->data;
123280304Sjkim    o = 0;
12455714Skris
125280304Sjkim    if (l < 11)
126280304Sjkim        goto err;
127280304Sjkim    for (i = 0; i < 6; i++) {
128280304Sjkim        if ((i == 5) && ((a[o] == 'Z') || (a[o] == '+') || (a[o] == '-'))) {
129280304Sjkim            i++;
130280304Sjkim            break;
131280304Sjkim        }
132280304Sjkim        if ((a[o] < '0') || (a[o] > '9'))
133280304Sjkim            goto err;
134280304Sjkim        n = a[o] - '0';
135280304Sjkim        if (++o > l)
136280304Sjkim            goto err;
13755714Skris
138280304Sjkim        if ((a[o] < '0') || (a[o] > '9'))
139280304Sjkim            goto err;
140280304Sjkim        n = (n * 10) + a[o] - '0';
141280304Sjkim        if (++o > l)
142280304Sjkim            goto err;
14355714Skris
144280304Sjkim        if ((n < min[i]) || (n > max[i]))
145280304Sjkim            goto err;
146280304Sjkim    }
147280304Sjkim    if (a[o] == 'Z')
148280304Sjkim        o++;
149280304Sjkim    else if ((a[o] == '+') || (a[o] == '-')) {
150280304Sjkim        o++;
151280304Sjkim        if (o + 4 > l)
152280304Sjkim            goto err;
153280304Sjkim        for (i = 6; i < 8; i++) {
154280304Sjkim            if ((a[o] < '0') || (a[o] > '9'))
155280304Sjkim                goto err;
156280304Sjkim            n = a[o] - '0';
157280304Sjkim            o++;
158280304Sjkim            if ((a[o] < '0') || (a[o] > '9'))
159280304Sjkim                goto err;
160280304Sjkim            n = (n * 10) + a[o] - '0';
161280304Sjkim            if ((n < min[i]) || (n > max[i]))
162280304Sjkim                goto err;
163280304Sjkim            o++;
164280304Sjkim        }
165280304Sjkim    }
166280304Sjkim    return (o == l);
167280304Sjkim err:
168280304Sjkim    return (0);
169280304Sjkim}
17055714Skris
171160814Ssimonint ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str)
172280304Sjkim{
173280304Sjkim    ASN1_UTCTIME t;
17455714Skris
175280304Sjkim    t.type = V_ASN1_UTCTIME;
176280304Sjkim    t.length = strlen(str);
177280304Sjkim    t.data = (unsigned char *)str;
178280304Sjkim    if (ASN1_UTCTIME_check(&t)) {
179280304Sjkim        if (s != NULL) {
180280304Sjkim            if (!ASN1_STRING_set((ASN1_STRING *)s,
181280304Sjkim                                 (unsigned char *)str, t.length))
182280304Sjkim                return 0;
183280304Sjkim            s->type = V_ASN1_UTCTIME;
184280304Sjkim        }
185280304Sjkim        return (1);
186280304Sjkim    } else
187280304Sjkim        return (0);
188280304Sjkim}
18955714Skris
19055714SkrisASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t)
191280304Sjkim{
192280304Sjkim    return ASN1_UTCTIME_adj(s, t, 0, 0);
193280304Sjkim}
194238405Sjkim
195238405SjkimASN1_UTCTIME *ASN1_UTCTIME_adj(ASN1_UTCTIME *s, time_t t,
196280304Sjkim                               int offset_day, long offset_sec)
197280304Sjkim{
198280304Sjkim    char *p;
199280304Sjkim    struct tm *ts;
200280304Sjkim    struct tm data;
201280304Sjkim    size_t len = 20;
202280304Sjkim    int free_s = 0;
20355714Skris
204280304Sjkim    if (s == NULL) {
205280304Sjkim        free_s = 1;
206280304Sjkim        s = M_ASN1_UTCTIME_new();
207280304Sjkim    }
208280304Sjkim    if (s == NULL)
209280304Sjkim        goto err;
21055714Skris
211280304Sjkim    ts = OPENSSL_gmtime(&t, &data);
212280304Sjkim    if (ts == NULL)
213280304Sjkim        goto err;
214269686Sjkim
215280304Sjkim    if (offset_day || offset_sec) {
216280304Sjkim        if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
217280304Sjkim            goto err;
218280304Sjkim    }
21955714Skris
220280304Sjkim    if ((ts->tm_year < 50) || (ts->tm_year >= 150))
221280304Sjkim        goto err;
222238405Sjkim
223280304Sjkim    p = (char *)s->data;
224280304Sjkim    if ((p == NULL) || ((size_t)s->length < len)) {
225280304Sjkim        p = OPENSSL_malloc(len);
226280304Sjkim        if (p == NULL) {
227280304Sjkim            ASN1err(ASN1_F_ASN1_UTCTIME_ADJ, ERR_R_MALLOC_FAILURE);
228280304Sjkim            goto err;
229280304Sjkim        }
230280304Sjkim        if (s->data != NULL)
231280304Sjkim            OPENSSL_free(s->data);
232280304Sjkim        s->data = (unsigned char *)p;
233280304Sjkim    }
234238405Sjkim
235280304Sjkim    BIO_snprintf(p, len, "%02d%02d%02d%02d%02d%02dZ", ts->tm_year % 100,
236280304Sjkim                 ts->tm_mon + 1, ts->tm_mday, ts->tm_hour, ts->tm_min,
237280304Sjkim                 ts->tm_sec);
238280304Sjkim    s->length = strlen(p);
239280304Sjkim    s->type = V_ASN1_UTCTIME;
24055714Skris#ifdef CHARSET_EBCDIC_not
241280304Sjkim    ebcdic2ascii(s->data, s->data, s->length);
24255714Skris#endif
243280304Sjkim    return (s);
244280304Sjkim err:
245280304Sjkim    if (free_s && s)
246280304Sjkim        M_ASN1_UTCTIME_free(s);
247280304Sjkim    return NULL;
248280304Sjkim}
24968651Skris
25068651Skrisint ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t)
251280304Sjkim{
252280304Sjkim    struct tm *tm;
253280304Sjkim    struct tm data;
254280304Sjkim    int offset;
255280304Sjkim    int year;
25668651Skris
25768651Skris#define g2(p) (((p)[0]-'0')*10+(p)[1]-'0')
25868651Skris
259280304Sjkim    if (s->data[12] == 'Z')
260280304Sjkim        offset = 0;
261280304Sjkim    else {
262280304Sjkim        offset = g2(s->data + 13) * 60 + g2(s->data + 15);
263280304Sjkim        if (s->data[12] == '-')
264280304Sjkim            offset = -offset;
265280304Sjkim    }
26668651Skris
267280304Sjkim    t -= offset * 60;           /* FIXME: may overflow in extreme cases */
26868651Skris
269280304Sjkim    tm = OPENSSL_gmtime(&t, &data);
270280304Sjkim    /*
271280304Sjkim     * NB: -1, 0, 1 already valid return values so use -2 to indicate error.
272280304Sjkim     */
273280304Sjkim    if (tm == NULL)
274280304Sjkim        return -2;
275280304Sjkim
27668651Skris#define return_cmp(a,b) if ((a)<(b)) return -1; else if ((a)>(b)) return 1
277280304Sjkim    year = g2(s->data);
278280304Sjkim    if (year < 50)
279280304Sjkim        year += 100;
280280304Sjkim    return_cmp(year, tm->tm_year);
281280304Sjkim    return_cmp(g2(s->data + 2) - 1, tm->tm_mon);
282280304Sjkim    return_cmp(g2(s->data + 4), tm->tm_mday);
283280304Sjkim    return_cmp(g2(s->data + 6), tm->tm_hour);
284280304Sjkim    return_cmp(g2(s->data + 8), tm->tm_min);
285280304Sjkim    return_cmp(g2(s->data + 10), tm->tm_sec);
28668651Skris#undef g2
28768651Skris#undef return_cmp
28868651Skris
289280304Sjkim    return 0;
290280304Sjkim}
29168651Skris
29268651Skris#if 0
29368651Skristime_t ASN1_UTCTIME_get(const ASN1_UTCTIME *s)
294280304Sjkim{
295280304Sjkim    struct tm tm;
296280304Sjkim    int offset;
29768651Skris
298280304Sjkim    memset(&tm, '\0', sizeof tm);
29968651Skris
300280304Sjkim# define g2(p) (((p)[0]-'0')*10+(p)[1]-'0')
301280304Sjkim    tm.tm_year = g2(s->data);
302280304Sjkim    if (tm.tm_year < 50)
303280304Sjkim        tm.tm_year += 100;
304280304Sjkim    tm.tm_mon = g2(s->data + 2) - 1;
305280304Sjkim    tm.tm_mday = g2(s->data + 4);
306280304Sjkim    tm.tm_hour = g2(s->data + 6);
307280304Sjkim    tm.tm_min = g2(s->data + 8);
308280304Sjkim    tm.tm_sec = g2(s->data + 10);
309280304Sjkim    if (s->data[12] == 'Z')
310280304Sjkim        offset = 0;
311280304Sjkim    else {
312280304Sjkim        offset = g2(s->data + 13) * 60 + g2(s->data + 15);
313280304Sjkim        if (s->data[12] == '-')
314280304Sjkim            offset = -offset;
315280304Sjkim    }
316280304Sjkim# undef g2
31768651Skris
318280304Sjkim    /*
319280304Sjkim     * FIXME: mktime assumes the current timezone
320280304Sjkim     * instead of UTC, and unless we rewrite OpenSSL
321280304Sjkim     * in Lisp we cannot locally change the timezone
322280304Sjkim     * without possibly interfering with other parts
323280304Sjkim     * of the program. timegm, which uses UTC, is
324280304Sjkim     * non-standard.
325280304Sjkim     * Also time_t is inappropriate for general
326280304Sjkim     * UTC times because it may a 32 bit type.
327280304Sjkim     */
328280304Sjkim    return mktime(&tm) - offset * 60;
329280304Sjkim}
33068651Skris#endif
331