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. 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 59280304Sjkim/* 60280304Sjkim * GENERALIZEDTIME implementation, written by Steve Henson. Based on UTCTIME 61280304Sjkim */ 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) 72280304Sjkim{ 73280304Sjkim# ifdef CHARSET_EBCDIC 74280304Sjkim /* KLUDGE! We convert to ascii before writing DER */ 75280304Sjkim int len; 76280304Sjkim char tmp[24]; 77280304Sjkim ASN1_STRING tmpstr = *(ASN1_STRING *)a; 7855714Skris 79280304Sjkim len = tmpstr.length; 80280304Sjkim ebcdic2ascii(tmp, tmpstr.data, (len >= sizeof tmp) ? sizeof tmp : len); 81280304Sjkim tmpstr.data = tmp; 8255714Skris 83280304Sjkim a = (ASN1_GENERALIZEDTIME *)&tmpstr; 84280304Sjkim# endif 85280304Sjkim return (i2d_ASN1_bytes((ASN1_STRING *)a, pp, 86280304Sjkim V_ASN1_GENERALIZEDTIME, V_ASN1_UNIVERSAL)); 87280304Sjkim} 8855714Skris 8955714SkrisASN1_GENERALIZEDTIME *d2i_ASN1_GENERALIZEDTIME(ASN1_GENERALIZEDTIME **a, 90280304Sjkim unsigned char **pp, 91280304Sjkim long length) 92280304Sjkim{ 93280304Sjkim ASN1_GENERALIZEDTIME *ret = NULL; 9455714Skris 95280304Sjkim ret = 96280304Sjkim (ASN1_GENERALIZEDTIME *)d2i_ASN1_bytes((ASN1_STRING **)a, pp, length, 97280304Sjkim V_ASN1_GENERALIZEDTIME, 98280304Sjkim V_ASN1_UNIVERSAL); 99280304Sjkim if (ret == NULL) { 100280304Sjkim ASN1err(ASN1_F_D2I_ASN1_GENERALIZEDTIME, ERR_R_NESTED_ASN1_ERROR); 101280304Sjkim return (NULL); 102280304Sjkim } 103280304Sjkim# ifdef CHARSET_EBCDIC 104280304Sjkim ascii2ebcdic(ret->data, ret->data, ret->length); 105280304Sjkim# endif 106280304Sjkim if (!ASN1_GENERALIZEDTIME_check(ret)) { 107280304Sjkim ASN1err(ASN1_F_D2I_ASN1_GENERALIZEDTIME, ASN1_R_INVALID_TIME_FORMAT); 108280304Sjkim goto err; 109280304Sjkim } 11055714Skris 111280304Sjkim return (ret); 112280304Sjkim err: 113280304Sjkim if ((ret != NULL) && ((a == NULL) || (*a != ret))) 114280304Sjkim M_ASN1_GENERALIZEDTIME_free(ret); 115280304Sjkim return (NULL); 116280304Sjkim} 11755714Skris 118109998Smarkm#endif 119109998Smarkm 12055714Skrisint ASN1_GENERALIZEDTIME_check(ASN1_GENERALIZEDTIME *d) 121280304Sjkim{ 122280304Sjkim static const int min[9] = { 0, 0, 1, 1, 0, 0, 0, 0, 0 }; 123280304Sjkim static const int max[9] = { 99, 99, 12, 31, 23, 59, 59, 12, 59 }; 124280304Sjkim char *a; 125280304Sjkim int n, i, l, o; 12655714Skris 127280304Sjkim if (d->type != V_ASN1_GENERALIZEDTIME) 128280304Sjkim return (0); 129280304Sjkim l = d->length; 130280304Sjkim a = (char *)d->data; 131280304Sjkim o = 0; 132280304Sjkim /* 133280304Sjkim * GENERALIZEDTIME is similar to UTCTIME except the year is represented 134280304Sjkim * as YYYY. This stuff treats everything as a two digit field so make 135280304Sjkim * first two fields 00 to 99 136280304Sjkim */ 137280304Sjkim if (l < 13) 138280304Sjkim goto err; 139280304Sjkim for (i = 0; i < 7; i++) { 140280304Sjkim if ((i == 6) && ((a[o] == 'Z') || (a[o] == '+') || (a[o] == '-'))) { 141280304Sjkim i++; 142280304Sjkim break; 143280304Sjkim } 144280304Sjkim if ((a[o] < '0') || (a[o] > '9')) 145280304Sjkim goto err; 146280304Sjkim n = a[o] - '0'; 147280304Sjkim if (++o > l) 148280304Sjkim goto err; 14955714Skris 150280304Sjkim if ((a[o] < '0') || (a[o] > '9')) 151280304Sjkim goto err; 152280304Sjkim n = (n * 10) + a[o] - '0'; 153280304Sjkim if (++o > l) 154280304Sjkim goto err; 15555714Skris 156280304Sjkim if ((n < min[i]) || (n > max[i])) 157280304Sjkim goto err; 158280304Sjkim } 159280304Sjkim /* 160280304Sjkim * Optional fractional seconds: decimal point followed by one or more 161280304Sjkim * digits. 162280304Sjkim */ 163280304Sjkim if (a[o] == '.') { 164280304Sjkim if (++o > l) 165280304Sjkim goto err; 166280304Sjkim i = o; 167280304Sjkim while ((a[o] >= '0') && (a[o] <= '9') && (o <= l)) 168280304Sjkim o++; 169280304Sjkim /* Must have at least one digit after decimal point */ 170280304Sjkim if (i == o) 171280304Sjkim goto err; 172280304Sjkim } 173109998Smarkm 174280304Sjkim if (a[o] == 'Z') 175280304Sjkim o++; 176280304Sjkim else if ((a[o] == '+') || (a[o] == '-')) { 177280304Sjkim o++; 178280304Sjkim if (o + 4 > l) 179280304Sjkim goto err; 180280304Sjkim for (i = 7; i < 9; i++) { 181280304Sjkim if ((a[o] < '0') || (a[o] > '9')) 182280304Sjkim goto err; 183280304Sjkim n = a[o] - '0'; 184280304Sjkim o++; 185280304Sjkim if ((a[o] < '0') || (a[o] > '9')) 186280304Sjkim goto err; 187280304Sjkim n = (n * 10) + a[o] - '0'; 188280304Sjkim if ((n < min[i]) || (n > max[i])) 189280304Sjkim goto err; 190280304Sjkim o++; 191280304Sjkim } 192280304Sjkim } else { 193280304Sjkim /* Missing time zone information. */ 194280304Sjkim goto err; 195280304Sjkim } 196280304Sjkim return (o == l); 197280304Sjkim err: 198280304Sjkim return (0); 199280304Sjkim} 20055714Skris 201160814Ssimonint ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str) 202280304Sjkim{ 203280304Sjkim ASN1_GENERALIZEDTIME t; 20455714Skris 205280304Sjkim t.type = V_ASN1_GENERALIZEDTIME; 206280304Sjkim t.length = strlen(str); 207280304Sjkim t.data = (unsigned char *)str; 208280304Sjkim if (ASN1_GENERALIZEDTIME_check(&t)) { 209280304Sjkim if (s != NULL) { 210280304Sjkim if (!ASN1_STRING_set((ASN1_STRING *)s, 211280304Sjkim (unsigned char *)str, t.length)) 212280304Sjkim return 0; 213280304Sjkim s->type = V_ASN1_GENERALIZEDTIME; 214280304Sjkim } 215280304Sjkim return (1); 216280304Sjkim } else 217280304Sjkim return (0); 218280304Sjkim} 21955714Skris 22055714SkrisASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s, 221280304Sjkim time_t t) 222280304Sjkim{ 223280304Sjkim return ASN1_GENERALIZEDTIME_adj(s, t, 0, 0); 224280304Sjkim} 225238405Sjkim 226238405SjkimASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_adj(ASN1_GENERALIZEDTIME *s, 227280304Sjkim time_t t, int offset_day, 228280304Sjkim long offset_sec) 229280304Sjkim{ 230280304Sjkim char *p; 231280304Sjkim struct tm *ts; 232280304Sjkim struct tm data; 233280304Sjkim size_t len = 20; 23455714Skris 235280304Sjkim if (s == NULL) 236280304Sjkim s = M_ASN1_GENERALIZEDTIME_new(); 237280304Sjkim if (s == NULL) 238280304Sjkim return (NULL); 23955714Skris 240280304Sjkim ts = OPENSSL_gmtime(&t, &data); 241280304Sjkim if (ts == NULL) 242280304Sjkim return (NULL); 243109998Smarkm 244280304Sjkim if (offset_day || offset_sec) { 245280304Sjkim if (!OPENSSL_gmtime_adj(ts, offset_day, offset_sec)) 246280304Sjkim return NULL; 247280304Sjkim } 248238405Sjkim 249280304Sjkim p = (char *)s->data; 250280304Sjkim if ((p == NULL) || ((size_t)s->length < len)) { 251280304Sjkim p = OPENSSL_malloc(len); 252280304Sjkim if (p == NULL) { 253280304Sjkim ASN1err(ASN1_F_ASN1_GENERALIZEDTIME_ADJ, ERR_R_MALLOC_FAILURE); 254280304Sjkim return (NULL); 255280304Sjkim } 256280304Sjkim if (s->data != NULL) 257280304Sjkim OPENSSL_free(s->data); 258280304Sjkim s->data = (unsigned char *)p; 259280304Sjkim } 26055714Skris 261280304Sjkim BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ", ts->tm_year + 1900, 262280304Sjkim ts->tm_mon + 1, ts->tm_mday, ts->tm_hour, ts->tm_min, 263280304Sjkim ts->tm_sec); 264280304Sjkim s->length = strlen(p); 265280304Sjkim s->type = V_ASN1_GENERALIZEDTIME; 26655714Skris#ifdef CHARSET_EBCDIC_not 267280304Sjkim ebcdic2ascii(s->data, s->data, s->length); 26855714Skris#endif 269280304Sjkim return (s); 270280304Sjkim} 271