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.
8296341Sdelphij *
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).
15296341Sdelphij *
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.
22296341Sdelphij *
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 :-).
37296341Sdelphij * 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)"
40296341Sdelphij *
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.
52296341Sdelphij *
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
59296341Sdelphij/*
60296341Sdelphij * GENERALIZEDTIME implementation, written by Steve Henson. Based on UTCTIME
61296341Sdelphij */
6255714Skris
6355714Skris#include <stdio.h>
6455714Skris#include <time.h>
6555714Skris#include "cryptlib.h"
66109998Smarkm#include "o_time.h"
6755714Skris#include <openssl/asn1.h>
6855714Skris
69109998Smarkm#if 0
7059191Skris
7155714Skrisint i2d_ASN1_GENERALIZEDTIME(ASN1_GENERALIZEDTIME *a, unsigned char **pp)
72296341Sdelphij{
73296341Sdelphij# ifdef CHARSET_EBCDIC
74296341Sdelphij    /* KLUDGE! We convert to ascii before writing DER */
75296341Sdelphij    int len;
76296341Sdelphij    char tmp[24];
77296341Sdelphij    ASN1_STRING tmpstr = *(ASN1_STRING *)a;
7855714Skris
79296341Sdelphij    len = tmpstr.length;
80296341Sdelphij    ebcdic2ascii(tmp, tmpstr.data, (len >= sizeof tmp) ? sizeof tmp : len);
81296341Sdelphij    tmpstr.data = tmp;
8255714Skris
83296341Sdelphij    a = (ASN1_GENERALIZEDTIME *)&tmpstr;
84296341Sdelphij# endif
85296341Sdelphij    return (i2d_ASN1_bytes((ASN1_STRING *)a, pp,
86296341Sdelphij                           V_ASN1_GENERALIZEDTIME, V_ASN1_UNIVERSAL));
87296341Sdelphij}
8855714Skris
8955714SkrisASN1_GENERALIZEDTIME *d2i_ASN1_GENERALIZEDTIME(ASN1_GENERALIZEDTIME **a,
90296341Sdelphij                                               unsigned char **pp,
91296341Sdelphij                                               long length)
92296341Sdelphij{
93296341Sdelphij    ASN1_GENERALIZEDTIME *ret = NULL;
9455714Skris
95296341Sdelphij    ret =
96296341Sdelphij        (ASN1_GENERALIZEDTIME *)d2i_ASN1_bytes((ASN1_STRING **)a, pp, length,
97296341Sdelphij                                               V_ASN1_GENERALIZEDTIME,
98296341Sdelphij                                               V_ASN1_UNIVERSAL);
99296341Sdelphij    if (ret == NULL) {
100296341Sdelphij        ASN1err(ASN1_F_D2I_ASN1_GENERALIZEDTIME, ERR_R_NESTED_ASN1_ERROR);
101296341Sdelphij        return (NULL);
102296341Sdelphij    }
103296341Sdelphij# ifdef CHARSET_EBCDIC
104296341Sdelphij    ascii2ebcdic(ret->data, ret->data, ret->length);
105296341Sdelphij# endif
106296341Sdelphij    if (!ASN1_GENERALIZEDTIME_check(ret)) {
107296341Sdelphij        ASN1err(ASN1_F_D2I_ASN1_GENERALIZEDTIME, ASN1_R_INVALID_TIME_FORMAT);
108296341Sdelphij        goto err;
109296341Sdelphij    }
11055714Skris
111296341Sdelphij    return (ret);
112296341Sdelphij err:
113296341Sdelphij    if ((ret != NULL) && ((a == NULL) || (*a != ret)))
114296341Sdelphij        M_ASN1_GENERALIZEDTIME_free(ret);
115296341Sdelphij    return (NULL);
116296341Sdelphij}
11755714Skris
118109998Smarkm#endif
119109998Smarkm
12055714Skrisint ASN1_GENERALIZEDTIME_check(ASN1_GENERALIZEDTIME *d)
121296341Sdelphij{
122296341Sdelphij    static const int min[9] = { 0, 0, 1, 1, 0, 0, 0, 0, 0 };
123296341Sdelphij    static const int max[9] = { 99, 99, 12, 31, 23, 59, 59, 12, 59 };
124296341Sdelphij    char *a;
125296341Sdelphij    int n, i, l, o;
12655714Skris
127296341Sdelphij    if (d->type != V_ASN1_GENERALIZEDTIME)
128296341Sdelphij        return (0);
129296341Sdelphij    l = d->length;
130296341Sdelphij    a = (char *)d->data;
131296341Sdelphij    o = 0;
132296341Sdelphij    /*
133296341Sdelphij     * GENERALIZEDTIME is similar to UTCTIME except the year is represented
134296341Sdelphij     * as YYYY. This stuff treats everything as a two digit field so make
135296341Sdelphij     * first two fields 00 to 99
136296341Sdelphij     */
137296341Sdelphij    if (l < 13)
138296341Sdelphij        goto err;
139296341Sdelphij    for (i = 0; i < 7; i++) {
140296341Sdelphij        if ((i == 6) && ((a[o] == 'Z') || (a[o] == '+') || (a[o] == '-'))) {
141296341Sdelphij            i++;
142296341Sdelphij            break;
143296341Sdelphij        }
144296341Sdelphij        if ((a[o] < '0') || (a[o] > '9'))
145296341Sdelphij            goto err;
146296341Sdelphij        n = a[o] - '0';
147296341Sdelphij        if (++o > l)
148296341Sdelphij            goto err;
14955714Skris
150296341Sdelphij        if ((a[o] < '0') || (a[o] > '9'))
151296341Sdelphij            goto err;
152296341Sdelphij        n = (n * 10) + a[o] - '0';
153296341Sdelphij        if (++o > l)
154296341Sdelphij            goto err;
15555714Skris
156296341Sdelphij        if ((n < min[i]) || (n > max[i]))
157296341Sdelphij            goto err;
158296341Sdelphij    }
159296341Sdelphij    /*
160296341Sdelphij     * Optional fractional seconds: decimal point followed by one or more
161296341Sdelphij     * digits.
162296341Sdelphij     */
163296341Sdelphij    if (a[o] == '.') {
164296341Sdelphij        if (++o > l)
165296341Sdelphij            goto err;
166296341Sdelphij        i = o;
167296341Sdelphij        while ((a[o] >= '0') && (a[o] <= '9') && (o <= l))
168296341Sdelphij            o++;
169296341Sdelphij        /* Must have at least one digit after decimal point */
170296341Sdelphij        if (i == o)
171296341Sdelphij            goto err;
172296341Sdelphij    }
173109998Smarkm
174296341Sdelphij    if (a[o] == 'Z')
175296341Sdelphij        o++;
176296341Sdelphij    else if ((a[o] == '+') || (a[o] == '-')) {
177296341Sdelphij        o++;
178296341Sdelphij        if (o + 4 > l)
179296341Sdelphij            goto err;
180296341Sdelphij        for (i = 7; i < 9; i++) {
181296341Sdelphij            if ((a[o] < '0') || (a[o] > '9'))
182296341Sdelphij                goto err;
183296341Sdelphij            n = a[o] - '0';
184296341Sdelphij            o++;
185296341Sdelphij            if ((a[o] < '0') || (a[o] > '9'))
186296341Sdelphij                goto err;
187296341Sdelphij            n = (n * 10) + a[o] - '0';
188296341Sdelphij            if ((n < min[i]) || (n > max[i]))
189296341Sdelphij                goto err;
190296341Sdelphij            o++;
191296341Sdelphij        }
192296341Sdelphij    } else {
193296341Sdelphij        /* Missing time zone information. */
194296341Sdelphij        goto err;
195296341Sdelphij    }
196296341Sdelphij    return (o == l);
197296341Sdelphij err:
198296341Sdelphij    return (0);
199296341Sdelphij}
20055714Skris
201160814Ssimonint ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str)
202296341Sdelphij{
203296341Sdelphij    ASN1_GENERALIZEDTIME t;
20455714Skris
205296341Sdelphij    t.type = V_ASN1_GENERALIZEDTIME;
206296341Sdelphij    t.length = strlen(str);
207296341Sdelphij    t.data = (unsigned char *)str;
208296341Sdelphij    if (ASN1_GENERALIZEDTIME_check(&t)) {
209296341Sdelphij        if (s != NULL) {
210296341Sdelphij            if (!ASN1_STRING_set((ASN1_STRING *)s,
211296341Sdelphij                                 (unsigned char *)str, t.length))
212296341Sdelphij                return 0;
213296341Sdelphij            s->type = V_ASN1_GENERALIZEDTIME;
214296341Sdelphij        }
215296341Sdelphij        return (1);
216296341Sdelphij    } else
217296341Sdelphij        return (0);
218296341Sdelphij}
21955714Skris
22055714SkrisASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
221296341Sdelphij                                               time_t t)
222296341Sdelphij{
223296341Sdelphij    return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0);
224296341Sdelphij}
225238405Sjkim
226238405SjkimASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s,
227296341Sdelphij                                               time_t t, int offset_day,
228296341Sdelphij                                               long offset_sec)
229296341Sdelphij{
230296341Sdelphij    char *p;
231296341Sdelphij    struct tm *ts;
232296341Sdelphij    struct tm data;
233296341Sdelphij    size_t len = 20;
23455714Skris
235296341Sdelphij    if (s == NULL)
236296341Sdelphij        s = M_ASN1_GENERALIZEDTIME_new();
237296341Sdelphij    if (s == NULL)
238296341Sdelphij        return (NULL);
23955714Skris
240296341Sdelphij    ts = OPENSSL_gmtime(&t, &data);
241296341Sdelphij    if (ts == NULL)
242296341Sdelphij        return (NULL);
243109998Smarkm
244296341Sdelphij    if (offset_day || offset_sec) {
245296341Sdelphij        if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
246296341Sdelphij            return NULL;
247296341Sdelphij    }
248238405Sjkim
249296341Sdelphij    p = (char *)s->data;
250296341Sdelphij    if ((p == NULL) || ((size_t)s->length < len)) {
251296341Sdelphij        p = OPENSSL_malloc(len);
252296341Sdelphij        if (p == NULL) {
253296341Sdelphij            ASN1err(ASN1_F_ASN1_GENERALIZEDTIME_ADJ, ERR_R_MALLOC_FAILURE);
254296341Sdelphij            return (NULL);
255296341Sdelphij        }
256296341Sdelphij        if (s->data != NULL)
257296341Sdelphij            OPENSSL_free(s->data);
258296341Sdelphij        s->data = (unsigned char *)p;
259296341Sdelphij    }
26055714Skris
261296341Sdelphij    BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ", ts->tm_year + 1900,
262296341Sdelphij                 ts->tm_mon + 1, ts->tm_mday, ts->tm_hour, ts->tm_min,
263296341Sdelphij                 ts->tm_sec);
264296341Sdelphij    s->length = strlen(p);
265296341Sdelphij    s->type = V_ASN1_GENERALIZEDTIME;
26655714Skris#ifdef CHARSET_EBCDIC_not
267296341Sdelphij    ebcdic2ascii(s->data, s->data, s->length);
26855714Skris#endif
269296341Sdelphij    return (s);
270296341Sdelphij}
271