155714Skris/* crypto/asn1/a_gentm.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.
8280297Sjkim *
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).
15280297Sjkim *
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.
22280297Sjkim *
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 :-).
37280297Sjkim * 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)"
40280297Sjkim *
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.
52280297Sjkim *
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
59280297Sjkim/*
60280297Sjkim * GENERALIZEDTIME implementation, written by Steve Henson. Based on UTCTIME
61280297Sjkim */
6255714Skris
6355714Skris#include <stdio.h>
6455714Skris#include <time.h>
6555714Skris#include "cryptlib.h"
66109998Smarkm#include "o_time.h"
6755714Skris#include <openssl/asn1.h>
68290207Sjkim#include "asn1_locl.h"
6955714Skris
70109998Smarkm#if 0
7159191Skris
7255714Skrisint i2d_ASN1_GENERALIZEDTIME(ASN1_GENERALIZEDTIME *a, unsigned char **pp)
73280297Sjkim{
74280297Sjkim# ifdef CHARSET_EBCDIC
75280297Sjkim    /* KLUDGE! We convert to ascii before writing DER */
76280297Sjkim    int len;
77280297Sjkim    char tmp[24];
78280297Sjkim    ASN1_STRING tmpstr = *(ASN1_STRING *)a;
7955714Skris
80280297Sjkim    len = tmpstr.length;
81331638Sjkim    ebcdic2ascii(tmp, tmpstr.data, (len >= sizeof(tmp)) ? sizeof(tmp) : len);
82280297Sjkim    tmpstr.data = tmp;
8355714Skris
84280297Sjkim    a = (ASN1_GENERALIZEDTIME *)&tmpstr;
85280297Sjkim# endif
86280297Sjkim    return (i2d_ASN1_bytes((ASN1_STRING *)a, pp,
87280297Sjkim                           V_ASN1_GENERALIZEDTIME, V_ASN1_UNIVERSAL));
88280297Sjkim}
8955714Skris
9055714SkrisASN1_GENERALIZEDTIME *d2i_ASN1_GENERALIZEDTIME(ASN1_GENERALIZEDTIME **a,
91280297Sjkim                                               unsigned char **pp,
92280297Sjkim                                               long length)
93280297Sjkim{
94280297Sjkim    ASN1_GENERALIZEDTIME *ret = NULL;
9555714Skris
96280297Sjkim    ret =
97280297Sjkim        (ASN1_GENERALIZEDTIME *)d2i_ASN1_bytes((ASN1_STRING **)a, pp, length,
98280297Sjkim                                               V_ASN1_GENERALIZEDTIME,
99280297Sjkim                                               V_ASN1_UNIVERSAL);
100280297Sjkim    if (ret == NULL) {
101280297Sjkim        ASN1err(ASN1_F_D2I_ASN1_GENERALIZEDTIME, ERR_R_NESTED_ASN1_ERROR);
102280297Sjkim        return (NULL);
103280297Sjkim    }
104280297Sjkim# ifdef CHARSET_EBCDIC
105280297Sjkim    ascii2ebcdic(ret->data, ret->data, ret->length);
106280297Sjkim# endif
107280297Sjkim    if (!ASN1_GENERALIZEDTIME_check(ret)) {
108280297Sjkim        ASN1err(ASN1_F_D2I_ASN1_GENERALIZEDTIME, ASN1_R_INVALID_TIME_FORMAT);
109280297Sjkim        goto err;
110280297Sjkim    }
11155714Skris
112280297Sjkim    return (ret);
113280297Sjkim err:
114280297Sjkim    if ((ret != NULL) && ((a == NULL) || (*a != ret)))
115280297Sjkim        M_ASN1_GENERALIZEDTIME_free(ret);
116280297Sjkim    return (NULL);
117280297Sjkim}
11855714Skris
119109998Smarkm#endif
120109998Smarkm
121290207Sjkimint asn1_generalizedtime_to_tm(struct tm *tm, const ASN1_GENERALIZEDTIME *d)
122280297Sjkim{
123280297Sjkim    static const int min[9] = { 0, 0, 1, 1, 0, 0, 0, 0, 0 };
124280297Sjkim    static const int max[9] = { 99, 99, 12, 31, 23, 59, 59, 12, 59 };
125280297Sjkim    char *a;
126280297Sjkim    int n, i, l, o;
12755714Skris
128280297Sjkim    if (d->type != V_ASN1_GENERALIZEDTIME)
129280297Sjkim        return (0);
130280297Sjkim    l = d->length;
131280297Sjkim    a = (char *)d->data;
132280297Sjkim    o = 0;
133280297Sjkim    /*
134280297Sjkim     * GENERALIZEDTIME is similar to UTCTIME except the year is represented
135280297Sjkim     * as YYYY. This stuff treats everything as a two digit field so make
136280297Sjkim     * first two fields 00 to 99
137280297Sjkim     */
138280297Sjkim    if (l < 13)
139280297Sjkim        goto err;
140280297Sjkim    for (i = 0; i < 7; i++) {
141280297Sjkim        if ((i == 6) && ((a[o] == 'Z') || (a[o] == '+') || (a[o] == '-'))) {
142280297Sjkim            i++;
143290207Sjkim            if (tm)
144290207Sjkim                tm->tm_sec = 0;
145280297Sjkim            break;
146280297Sjkim        }
147280297Sjkim        if ((a[o] < '0') || (a[o] > '9'))
148280297Sjkim            goto err;
149280297Sjkim        n = a[o] - '0';
150280297Sjkim        if (++o > l)
151280297Sjkim            goto err;
15255714Skris
153280297Sjkim        if ((a[o] < '0') || (a[o] > '9'))
154280297Sjkim            goto err;
155280297Sjkim        n = (n * 10) + a[o] - '0';
156280297Sjkim        if (++o > l)
157280297Sjkim            goto err;
15855714Skris
159280297Sjkim        if ((n < min[i]) || (n > max[i]))
160280297Sjkim            goto err;
161290207Sjkim        if (tm) {
162290207Sjkim            switch (i) {
163290207Sjkim            case 0:
164290207Sjkim                tm->tm_year = n * 100 - 1900;
165290207Sjkim                break;
166290207Sjkim            case 1:
167290207Sjkim                tm->tm_year += n;
168290207Sjkim                break;
169290207Sjkim            case 2:
170290207Sjkim                tm->tm_mon = n - 1;
171290207Sjkim                break;
172290207Sjkim            case 3:
173290207Sjkim                tm->tm_mday = n;
174290207Sjkim                break;
175290207Sjkim            case 4:
176290207Sjkim                tm->tm_hour = n;
177290207Sjkim                break;
178290207Sjkim            case 5:
179290207Sjkim                tm->tm_min = n;
180290207Sjkim                break;
181290207Sjkim            case 6:
182290207Sjkim                tm->tm_sec = n;
183290207Sjkim                break;
184290207Sjkim            }
185290207Sjkim        }
186280297Sjkim    }
187280297Sjkim    /*
188280297Sjkim     * Optional fractional seconds: decimal point followed by one or more
189280297Sjkim     * digits.
190280297Sjkim     */
191280297Sjkim    if (a[o] == '.') {
192280297Sjkim        if (++o > l)
193280297Sjkim            goto err;
194280297Sjkim        i = o;
195280297Sjkim        while ((a[o] >= '0') && (a[o] <= '9') && (o <= l))
196280297Sjkim            o++;
197280297Sjkim        /* Must have at least one digit after decimal point */
198280297Sjkim        if (i == o)
199280297Sjkim            goto err;
200280297Sjkim    }
201109998Smarkm
202280297Sjkim    if (a[o] == 'Z')
203280297Sjkim        o++;
204280297Sjkim    else if ((a[o] == '+') || (a[o] == '-')) {
205325335Sjkim        int offsign = a[o] == '-' ? 1 : -1, offset = 0;
206280297Sjkim        o++;
207280297Sjkim        if (o + 4 > l)
208280297Sjkim            goto err;
209280297Sjkim        for (i = 7; i < 9; i++) {
210280297Sjkim            if ((a[o] < '0') || (a[o] > '9'))
211280297Sjkim                goto err;
212280297Sjkim            n = a[o] - '0';
213280297Sjkim            o++;
214280297Sjkim            if ((a[o] < '0') || (a[o] > '9'))
215280297Sjkim                goto err;
216280297Sjkim            n = (n * 10) + a[o] - '0';
217280297Sjkim            if ((n < min[i]) || (n > max[i]))
218280297Sjkim                goto err;
219290207Sjkim            if (tm) {
220290207Sjkim                if (i == 7)
221290207Sjkim                    offset = n * 3600;
222290207Sjkim                else if (i == 8)
223290207Sjkim                    offset += n * 60;
224290207Sjkim            }
225280297Sjkim            o++;
226280297Sjkim        }
227290207Sjkim        if (offset && !OPENSSL_gmtime_adj(tm, 0, offset * offsign))
228290207Sjkim            return 0;
229290207Sjkim    } else if (a[o]) {
230280297Sjkim        /* Missing time zone information. */
231280297Sjkim        goto err;
232280297Sjkim    }
233280297Sjkim    return (o == l);
234280297Sjkim err:
235280297Sjkim    return (0);
236280297Sjkim}
23755714Skris
238290207Sjkimint ASN1_GENERALIZEDTIME_check(const ASN1_GENERALIZEDTIME *d)
239290207Sjkim{
240290207Sjkim    return asn1_generalizedtime_to_tm(NULL, d);
241290207Sjkim}
242290207Sjkim
243160814Ssimonint ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str)
244280297Sjkim{
245280297Sjkim    ASN1_GENERALIZEDTIME t;
24655714Skris
247280297Sjkim    t.type = V_ASN1_GENERALIZEDTIME;
248280297Sjkim    t.length = strlen(str);
249280297Sjkim    t.data = (unsigned char *)str;
250280297Sjkim    if (ASN1_GENERALIZEDTIME_check(&t)) {
251280297Sjkim        if (s != NULL) {
252280297Sjkim            if (!ASN1_STRING_set((ASN1_STRING *)s,
253280297Sjkim                                 (unsigned char *)str, t.length))
254280297Sjkim                return 0;
255280297Sjkim            s->type = V_ASN1_GENERALIZEDTIME;
256280297Sjkim        }
257280297Sjkim        return (1);
258280297Sjkim    } else
259280297Sjkim        return (0);
260280297Sjkim}
26155714Skris
26255714SkrisASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
263280297Sjkim                                               time_t t)
264280297Sjkim{
265280297Sjkim    return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0);
266280297Sjkim}
267238405Sjkim
268238405SjkimASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
269280297Sjkim                                               time_t t, int offset_day,
270280297Sjkim                                               long offset_sec)
271280297Sjkim{
272280297Sjkim    char *p;
273280297Sjkim    struct tm *ts;
274280297Sjkim    struct tm data;
275280297Sjkim    size_t len = 20;
27655714Skris
277280297Sjkim    if (s == NULL)
278280297Sjkim        s = M_ASN1_GENERALIZEDTIME_new();
279280297Sjkim    if (s == NULL)
280280297Sjkim        return (NULL);
28155714Skris
282280297Sjkim    ts = OPENSSL_gmtime(&t, &data);
283280297Sjkim    if (ts == NULL)
284280297Sjkim        return (NULL);
285109998Smarkm
286280297Sjkim    if (offset_day || offset_sec) {
287280297Sjkim        if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
288280297Sjkim            return NULL;
289280297Sjkim    }
290238405Sjkim
291280297Sjkim    p = (char *)s->data;
292280297Sjkim    if ((p == NULL) || ((size_t)s->length < len)) {
293280297Sjkim        p = OPENSSL_malloc(len);
294280297Sjkim        if (p == NULL) {
295280297Sjkim            ASN1err(ASN1_F_ASN1_GENERALIZEDTIME_ADJ, ERR_R_MALLOC_FAILURE);
296280297Sjkim            return (NULL);
297280297Sjkim        }
298280297Sjkim        if (s->data != NULL)
299280297Sjkim            OPENSSL_free(s->data);
300280297Sjkim        s->data = (unsigned char *)p;
301280297Sjkim    }
30255714Skris
303280297Sjkim    BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ", ts->tm_year + 1900,
304280297Sjkim                 ts->tm_mon + 1, ts->tm_mday, ts->tm_hour, ts->tm_min,
305280297Sjkim                 ts->tm_sec);
306280297Sjkim    s->length = strlen(p);
307280297Sjkim    s->type = V_ASN1_GENERALIZEDTIME;
30855714Skris#ifdef CHARSET_EBCDIC_not
309280297Sjkim    ebcdic2ascii(s->data, s->data, s->length);
31055714Skris#endif
311280297Sjkim    return (s);
312280297Sjkim}
313