1*0Sstevel@tonic-gate /* crypto/asn1/a_time.c */
2*0Sstevel@tonic-gate /* ====================================================================
3*0Sstevel@tonic-gate * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
4*0Sstevel@tonic-gate *
5*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
6*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions
7*0Sstevel@tonic-gate * are met:
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
10*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
11*0Sstevel@tonic-gate *
12*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
13*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
14*0Sstevel@tonic-gate * the documentation and/or other materials provided with the
15*0Sstevel@tonic-gate * distribution.
16*0Sstevel@tonic-gate *
17*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
18*0Sstevel@tonic-gate * software must display the following acknowledgment:
19*0Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
20*0Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21*0Sstevel@tonic-gate *
22*0Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23*0Sstevel@tonic-gate * endorse or promote products derived from this software without
24*0Sstevel@tonic-gate * prior written permission. For written permission, please contact
25*0Sstevel@tonic-gate * licensing@OpenSSL.org.
26*0Sstevel@tonic-gate *
27*0Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
28*0Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
29*0Sstevel@tonic-gate * permission of the OpenSSL Project.
30*0Sstevel@tonic-gate *
31*0Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
32*0Sstevel@tonic-gate * acknowledgment:
33*0Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
34*0Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35*0Sstevel@tonic-gate *
36*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37*0Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39*0Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40*0Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41*0Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42*0Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43*0Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45*0Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46*0Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47*0Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
48*0Sstevel@tonic-gate * ====================================================================
49*0Sstevel@tonic-gate *
50*0Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
51*0Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
52*0Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
53*0Sstevel@tonic-gate *
54*0Sstevel@tonic-gate */
55*0Sstevel@tonic-gate
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gate /* This is an implementation of the ASN1 Time structure which is:
58*0Sstevel@tonic-gate * Time ::= CHOICE {
59*0Sstevel@tonic-gate * utcTime UTCTime,
60*0Sstevel@tonic-gate * generalTime GeneralizedTime }
61*0Sstevel@tonic-gate * written by Steve Henson.
62*0Sstevel@tonic-gate */
63*0Sstevel@tonic-gate
64*0Sstevel@tonic-gate #include <stdio.h>
65*0Sstevel@tonic-gate #include <time.h>
66*0Sstevel@tonic-gate #include "cryptlib.h"
67*0Sstevel@tonic-gate #include "o_time.h"
68*0Sstevel@tonic-gate #include <openssl/asn1t.h>
69*0Sstevel@tonic-gate
IMPLEMENT_ASN1_MSTRING(ASN1_TIME,B_ASN1_TIME)70*0Sstevel@tonic-gate IMPLEMENT_ASN1_MSTRING(ASN1_TIME, B_ASN1_TIME)
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gate IMPLEMENT_ASN1_FUNCTIONS(ASN1_TIME)
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate #if 0
75*0Sstevel@tonic-gate int i2d_ASN1_TIME(ASN1_TIME *a, unsigned char **pp)
76*0Sstevel@tonic-gate {
77*0Sstevel@tonic-gate #ifdef CHARSET_EBCDIC
78*0Sstevel@tonic-gate /* KLUDGE! We convert to ascii before writing DER */
79*0Sstevel@tonic-gate char tmp[24];
80*0Sstevel@tonic-gate ASN1_STRING tmpstr;
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate if(a->type == V_ASN1_UTCTIME || a->type == V_ASN1_GENERALIZEDTIME) {
83*0Sstevel@tonic-gate int len;
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gate tmpstr = *(ASN1_STRING *)a;
86*0Sstevel@tonic-gate len = tmpstr.length;
87*0Sstevel@tonic-gate ebcdic2ascii(tmp, tmpstr.data, (len >= sizeof tmp) ? sizeof tmp : len);
88*0Sstevel@tonic-gate tmpstr.data = tmp;
89*0Sstevel@tonic-gate a = (ASN1_GENERALIZEDTIME *) &tmpstr;
90*0Sstevel@tonic-gate }
91*0Sstevel@tonic-gate #endif
92*0Sstevel@tonic-gate if(a->type == V_ASN1_UTCTIME || a->type == V_ASN1_GENERALIZEDTIME)
93*0Sstevel@tonic-gate return(i2d_ASN1_bytes((ASN1_STRING *)a,pp,
94*0Sstevel@tonic-gate a->type ,V_ASN1_UNIVERSAL));
95*0Sstevel@tonic-gate ASN1err(ASN1_F_I2D_ASN1_TIME,ASN1_R_EXPECTING_A_TIME);
96*0Sstevel@tonic-gate return -1;
97*0Sstevel@tonic-gate }
98*0Sstevel@tonic-gate #endif
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gate
101*0Sstevel@tonic-gate ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t)
102*0Sstevel@tonic-gate {
103*0Sstevel@tonic-gate struct tm *ts;
104*0Sstevel@tonic-gate struct tm data;
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate ts=OPENSSL_gmtime(&t,&data);
107*0Sstevel@tonic-gate if (ts == NULL)
108*0Sstevel@tonic-gate {
109*0Sstevel@tonic-gate ASN1err(ASN1_F_ASN1_TIME_SET, ASN1_R_ERROR_GETTING_TIME);
110*0Sstevel@tonic-gate return NULL;
111*0Sstevel@tonic-gate }
112*0Sstevel@tonic-gate if((ts->tm_year >= 50) && (ts->tm_year < 150))
113*0Sstevel@tonic-gate return ASN1_UTCTIME_set(s, t);
114*0Sstevel@tonic-gate return ASN1_GENERALIZEDTIME_set(s,t);
115*0Sstevel@tonic-gate }
116*0Sstevel@tonic-gate
ASN1_TIME_check(ASN1_TIME * t)117*0Sstevel@tonic-gate int ASN1_TIME_check(ASN1_TIME *t)
118*0Sstevel@tonic-gate {
119*0Sstevel@tonic-gate if (t->type == V_ASN1_GENERALIZEDTIME)
120*0Sstevel@tonic-gate return ASN1_GENERALIZEDTIME_check(t);
121*0Sstevel@tonic-gate else if (t->type == V_ASN1_UTCTIME)
122*0Sstevel@tonic-gate return ASN1_UTCTIME_check(t);
123*0Sstevel@tonic-gate return 0;
124*0Sstevel@tonic-gate }
125*0Sstevel@tonic-gate
126*0Sstevel@tonic-gate /* Convert an ASN1_TIME structure to GeneralizedTime */
ASN1_TIME_to_generalizedtime(ASN1_TIME * t,ASN1_GENERALIZEDTIME ** out)127*0Sstevel@tonic-gate ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(ASN1_TIME *t, ASN1_GENERALIZEDTIME **out)
128*0Sstevel@tonic-gate {
129*0Sstevel@tonic-gate ASN1_GENERALIZEDTIME *ret;
130*0Sstevel@tonic-gate char *str;
131*0Sstevel@tonic-gate int newlen;
132*0Sstevel@tonic-gate
133*0Sstevel@tonic-gate if (!ASN1_TIME_check(t)) return NULL;
134*0Sstevel@tonic-gate
135*0Sstevel@tonic-gate if (!out || !*out)
136*0Sstevel@tonic-gate {
137*0Sstevel@tonic-gate if (!(ret = ASN1_GENERALIZEDTIME_new ()))
138*0Sstevel@tonic-gate return NULL;
139*0Sstevel@tonic-gate if (out) *out = ret;
140*0Sstevel@tonic-gate }
141*0Sstevel@tonic-gate else ret = *out;
142*0Sstevel@tonic-gate
143*0Sstevel@tonic-gate /* If already GeneralizedTime just copy across */
144*0Sstevel@tonic-gate if (t->type == V_ASN1_GENERALIZEDTIME)
145*0Sstevel@tonic-gate {
146*0Sstevel@tonic-gate if(!ASN1_STRING_set(ret, t->data, t->length))
147*0Sstevel@tonic-gate return NULL;
148*0Sstevel@tonic-gate return ret;
149*0Sstevel@tonic-gate }
150*0Sstevel@tonic-gate
151*0Sstevel@tonic-gate /* grow the string */
152*0Sstevel@tonic-gate if (!ASN1_STRING_set(ret, NULL, t->length + 2))
153*0Sstevel@tonic-gate return NULL;
154*0Sstevel@tonic-gate /* ASN1_STRING_set() allocated 'len + 1' bytes. */
155*0Sstevel@tonic-gate newlen = t->length + 2 + 1;
156*0Sstevel@tonic-gate str = (char *)ret->data;
157*0Sstevel@tonic-gate /* Work out the century and prepend */
158*0Sstevel@tonic-gate if (t->data[0] >= '5') BUF_strlcpy(str, "19", newlen);
159*0Sstevel@tonic-gate else BUF_strlcpy(str, "20", newlen);
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate BUF_strlcat(str, (char *)t->data, newlen);
162*0Sstevel@tonic-gate
163*0Sstevel@tonic-gate return ret;
164*0Sstevel@tonic-gate }
165