a_gentm.c revision 296465
11839Swollman/* crypto/asn1/a_gentm.c */ 21839Swollman/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 31839Swollman * All rights reserved. 41839Swollman * 51839Swollman * This package is an SSL implementation written 61839Swollman * by Eric Young (eay@cryptsoft.com). 71839Swollman * The implementation was written so as to conform with Netscapes SSL. 81839Swollman * 91839Swollman * This library is free for commercial and non-commercial use as long as 101839Swollman * the following conditions are aheared to. The following conditions 111839Swollman * apply to all code found in this distribution, be it the RC4, RSA, 121839Swollman * lhash, DES, etc., code; not just the SSL code. The SSL documentation 131839Swollman * included with this distribution is covered by the same copyright terms 141839Swollman * except that the holder is Tim Hudson (tjh@cryptsoft.com). 151839Swollman * 161839Swollman * Copyright remains Eric Young's, and as such any Copyright notices in 171839Swollman * the code are not to be removed. 181839Swollman * If this package is used in a product, Eric Young should be given attribution 191839Swollman * as the author of the parts of the library used. 201839Swollman * This can be in the form of a textual message at program startup or 211839Swollman * in documentation (online or textual) provided with the package. 221839Swollman * 231839Swollman * Redistribution and use in source and binary forms, with or without 241839Swollman * modification, are permitted provided that the following conditions 251839Swollman * are met: 261839Swollman * 1. Redistributions of source code must retain the copyright 271839Swollman * notice, this list of conditions and the following disclaimer. 281903Swollman * 2. Redistributions in binary form must reproduce the above copyright 291903Swollman * notice, this list of conditions and the following disclaimer in the 301903Swollman * documentation and/or other materials provided with the distribution. 311903Swollman * 3. All advertising materials mentioning features or use of this software 321839Swollman * must display the following acknowledgement: 331839Swollman * "This product includes cryptographic software written by 341839Swollman * Eric Young (eay@cryptsoft.com)" 351839Swollman * The word 'cryptographic' can be left out if the rouines from the library 361839Swollman * being used are not cryptographic related :-). 371839Swollman * 4. If you include any Windows specific code (or a derivative thereof) from 381839Swollman * the apps directory (application code) you must include an acknowledgement: 391839Swollman * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 401839Swollman * 411839Swollman * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 421839Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 431839Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 441839Swollman * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 451839Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 461839Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 471839Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 481839Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 491839Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 501839Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 511839Swollman * SUCH DAMAGE. 521839Swollman * 531839Swollman * The licence and distribution terms for any publically available version or 541839Swollman * derivative of this code cannot be changed. i.e. this code cannot simply be 551839Swollman * copied and put under another distribution licence 561839Swollman * [including the GNU Public Licence.] 571839Swollman */ 581839Swollman 591839Swollman/* 601839Swollman * GENERALIZEDTIME implementation, written by Steve Henson. Based on UTCTIME 611839Swollman */ 621839Swollman 631839Swollman#include <stdio.h> 641839Swollman#include <time.h> 651839Swollman#include "cryptlib.h" 661839Swollman#include "o_time.h" 671839Swollman#include <openssl/asn1.h> 681839Swollman 691839Swollman#if 0 701839Swollman 711903Swollmanint i2d_ASN1_GENERALIZEDTIME(ASN1_GENERALIZEDTIME *a, unsigned char **pp) 721903Swollman{ 731903Swollman# ifdef CHARSET_EBCDIC 741903Swollman /* KLUDGE! We convert to ascii before writing DER */ 751839Swollman int len; 761839Swollman char tmp[24]; 771839Swollman ASN1_STRING tmpstr = *(ASN1_STRING *)a; 781839Swollman 791839Swollman len = tmpstr.length; 801839Swollman ebcdic2ascii(tmp, tmpstr.data, (len >= sizeof tmp) ? sizeof tmp : len); 811839Swollman tmpstr.data = tmp; 821839Swollman 831839Swollman a = (ASN1_GENERALIZEDTIME *)&tmpstr; 841839Swollman# endif 851839Swollman return (i2d_ASN1_bytes((ASN1_STRING *)a, pp, 861839Swollman V_ASN1_GENERALIZEDTIME, V_ASN1_UNIVERSAL)); 871839Swollman} 881839Swollman 891839SwollmanASN1_GENERALIZEDTIME *d2i_ASN1_GENERALIZEDTIME(ASN1_GENERALIZEDTIME **a, 901839Swollman unsigned char **pp, 911839Swollman long length) 921839Swollman{ 931839Swollman ASN1_GENERALIZEDTIME *ret = NULL; 941839Swollman 951839Swollman ret = 961839Swollman (ASN1_GENERALIZEDTIME *)d2i_ASN1_bytes((ASN1_STRING **)a, pp, length, 971839Swollman V_ASN1_GENERALIZEDTIME, 981839Swollman V_ASN1_UNIVERSAL); 991903Swollman if (ret == NULL) { 1001903Swollman ASN1err(ASN1_F_D2I_ASN1_GENERALIZEDTIME, ERR_R_NESTED_ASN1_ERROR); 1011903Swollman return (NULL); 1021903Swollman } 1031903Swollman# ifdef CHARSET_EBCDIC 1041903Swollman ascii2ebcdic(ret->data, ret->data, ret->length); 105# endif 106 if (!ASN1_GENERALIZEDTIME_check(ret)) { 107 ASN1err(ASN1_F_D2I_ASN1_GENERALIZEDTIME, ASN1_R_INVALID_TIME_FORMAT); 108 goto err; 109 } 110 111 return (ret); 112 err: 113 if ((ret != NULL) && ((a == NULL) || (*a != ret))) 114 M_ASN1_GENERALIZEDTIME_free(ret); 115 return (NULL); 116} 117 118#endif 119 120int ASN1_GENERALIZEDTIME_check(ASN1_GENERALIZEDTIME *d) 121{ 122 static int min[9] = { 0, 0, 1, 1, 0, 0, 0, 0, 0 }; 123 static int max[9] = { 99, 99, 12, 31, 23, 59, 59, 12, 59 }; 124 char *a; 125 int n, i, l, o; 126 127 if (d->type != V_ASN1_GENERALIZEDTIME) 128 return (0); 129 l = d->length; 130 a = (char *)d->data; 131 o = 0; 132 /* 133 * GENERALIZEDTIME is similar to UTCTIME except the year is represented 134 * as YYYY. This stuff treats everything as a two digit field so make 135 * first two fields 00 to 99 136 */ 137 if (l < 13) 138 goto err; 139 for (i = 0; i < 7; i++) { 140 if ((i == 6) && ((a[o] == 'Z') || (a[o] == '+') || (a[o] == '-'))) { 141 i++; 142 break; 143 } 144 if ((a[o] < '0') || (a[o] > '9')) 145 goto err; 146 n = a[o] - '0'; 147 if (++o > l) 148 goto err; 149 150 if ((a[o] < '0') || (a[o] > '9')) 151 goto err; 152 n = (n * 10) + a[o] - '0'; 153 if (++o > l) 154 goto err; 155 156 if ((n < min[i]) || (n > max[i])) 157 goto err; 158 } 159 /* 160 * Optional fractional seconds: decimal point followed by one or more 161 * digits. 162 */ 163 if (a[o] == '.') { 164 if (++o > l) 165 goto err; 166 i = o; 167 while ((a[o] >= '0') && (a[o] <= '9') && (o <= l)) 168 o++; 169 /* Must have at least one digit after decimal point */ 170 if (i == o) 171 goto err; 172 } 173 174 if (a[o] == 'Z') 175 o++; 176 else if ((a[o] == '+') || (a[o] == '-')) { 177 o++; 178 if (o + 4 > l) 179 goto err; 180 for (i = 7; i < 9; i++) { 181 if ((a[o] < '0') || (a[o] > '9')) 182 goto err; 183 n = a[o] - '0'; 184 o++; 185 if ((a[o] < '0') || (a[o] > '9')) 186 goto err; 187 n = (n * 10) + a[o] - '0'; 188 if ((n < min[i]) || (n > max[i])) 189 goto err; 190 o++; 191 } 192 } 193 return (o == l); 194 err: 195 return (0); 196} 197 198int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str) 199{ 200 ASN1_GENERALIZEDTIME t; 201 202 t.type = V_ASN1_GENERALIZEDTIME; 203 t.length = strlen(str); 204 t.data = (unsigned char *)str; 205 if (ASN1_GENERALIZEDTIME_check(&t)) { 206 if (s != NULL) { 207 if (!ASN1_STRING_set((ASN1_STRING *)s, 208 (unsigned char *)str, t.length)) 209 return 0; 210 s->type = V_ASN1_GENERALIZEDTIME; 211 } 212 return (1); 213 } else 214 return (0); 215} 216 217ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s, 218 time_t t) 219{ 220 char *p; 221 struct tm *ts; 222 struct tm data; 223 size_t len = 20; 224 225 if (s == NULL) 226 s = M_ASN1_GENERALIZEDTIME_new(); 227 if (s == NULL) 228 return (NULL); 229 230 ts = OPENSSL_gmtime(&t, &data); 231 if (ts == NULL) 232 return (NULL); 233 234 p = (char *)s->data; 235 if ((p == NULL) || ((size_t)s->length < len)) { 236 p = OPENSSL_malloc(len); 237 if (p == NULL) { 238 ASN1err(ASN1_F_ASN1_GENERALIZEDTIME_SET, ERR_R_MALLOC_FAILURE); 239 return (NULL); 240 } 241 if (s->data != NULL) 242 OPENSSL_free(s->data); 243 s->data = (unsigned char *)p; 244 } 245 246 BIO_snprintf(p, len, "%04d%02d%02d%02d%02d%02dZ", ts->tm_year + 1900, 247 ts->tm_mon + 1, ts->tm_mday, ts->tm_hour, ts->tm_min, 248 ts->tm_sec); 249 s->length = strlen(p); 250 s->type = V_ASN1_GENERALIZEDTIME; 251#ifdef CHARSET_EBCDIC_not 252 ebcdic2ascii(s->data, s->data, s->length); 253#endif 254 return (s); 255} 256