1 /* ====================================================================
2 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * licensing@OpenSSL.org.
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
49 * This product includes cryptographic software written by Eric Young
50 * (eay@cryptsoft.com). This product includes software written by Tim
51 * Hudson (tjh@cryptsoft.com).
56 * This is an implementation of the ASN1 Time structure which is:
59 * generalTime GeneralizedTime }
60 * written by Steve Henson.
65 #include "internal/cryptlib.h"
66 #include <openssl/asn1t.h>
67 #include "asn1_locl.h"
69 IMPLEMENT_ASN1_MSTRING(ASN1_TIME, B_ASN1_TIME)
71 IMPLEMENT_ASN1_FUNCTIONS(ASN1_TIME)
73 ASN1_TIME *ASN1_TIME_set(ASN1_TIME *s, time_t t)
75 return ASN1_TIME_adj(s, t, 0, 0);
78 ASN1_TIME *ASN1_TIME_adj(ASN1_TIME *s, time_t t,
79 int offset_day, long offset_sec)
84 ts = OPENSSL_gmtime(&t, &data);
86 ASN1err(ASN1_F_ASN1_TIME_ADJ, ASN1_R_ERROR_GETTING_TIME);
89 if (offset_day || offset_sec) {
90 if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec))
93 if ((ts->tm_year >= 50) && (ts->tm_year < 150))
94 return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
95 return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);
98 int ASN1_TIME_check(const ASN1_TIME *t)
100 if (t->type == V_ASN1_GENERALIZEDTIME)
101 return ASN1_GENERALIZEDTIME_check(t);
102 else if (t->type == V_ASN1_UTCTIME)
103 return ASN1_UTCTIME_check(t);
107 /* Convert an ASN1_TIME structure to GeneralizedTime */
108 ASN1_GENERALIZEDTIME *ASN1_TIME_to_generalizedtime(ASN1_TIME *t,
109 ASN1_GENERALIZEDTIME **out)
111 ASN1_GENERALIZEDTIME *ret;
115 if (!ASN1_TIME_check(t))
118 if (out == NULL || *out == NULL) {
119 if ((ret = ASN1_GENERALIZEDTIME_new()) == NULL)
126 /* If already GeneralizedTime just copy across */
127 if (t->type == V_ASN1_GENERALIZEDTIME) {
128 if (!ASN1_STRING_set(ret, t->data, t->length))
133 /* grow the string */
134 if (!ASN1_STRING_set(ret, NULL, t->length + 2))
136 /* ASN1_STRING_set() allocated 'len + 1' bytes. */
137 newlen = t->length + 2 + 1;
138 str = (char *)ret->data;
139 /* Work out the century and prepend */
140 if (t->data[0] >= '5')
141 OPENSSL_strlcpy(str, "19", newlen);
143 OPENSSL_strlcpy(str, "20", newlen);
145 OPENSSL_strlcat(str, (char *)t->data, newlen);
150 int ASN1_TIME_set_string(ASN1_TIME *s, const char *str)
154 t.length = strlen(str);
155 t.data = (unsigned char *)str;
158 t.type = V_ASN1_UTCTIME;
160 if (!ASN1_TIME_check(&t)) {
161 t.type = V_ASN1_GENERALIZEDTIME;
162 if (!ASN1_TIME_check(&t))
166 if (s && !ASN1_STRING_copy((ASN1_STRING *)s, (ASN1_STRING *)&t))
172 static int asn1_time_to_tm(struct tm *tm, const ASN1_TIME *t)
177 if (OPENSSL_gmtime(&now_t, tm))
182 if (t->type == V_ASN1_UTCTIME)
183 return asn1_utctime_to_tm(tm, t);
184 else if (t->type == V_ASN1_GENERALIZEDTIME)
185 return asn1_generalizedtime_to_tm(tm, t);
190 int ASN1_TIME_diff(int *pday, int *psec,
191 const ASN1_TIME *from, const ASN1_TIME *to)
193 struct tm tm_from, tm_to;
194 if (!asn1_time_to_tm(&tm_from, from))
196 if (!asn1_time_to_tm(&tm_to, to))
198 return OPENSSL_gmtime_diff(pday, psec, &tm_from, &tm_to);
201 int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
203 if (tm->type == V_ASN1_UTCTIME)
204 return ASN1_UTCTIME_print(bp, tm);
205 if (tm->type == V_ASN1_GENERALIZEDTIME)
206 return ASN1_GENERALIZEDTIME_print(bp, tm);
207 BIO_write(bp, "Bad time value", 14);