10Sstevel@tonic-gate /* crypto/asn1/a_gentm.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate /* GENERALIZEDTIME implementation, written by Steve Henson. Based on UTCTIME */
600Sstevel@tonic-gate
610Sstevel@tonic-gate #include <stdio.h>
620Sstevel@tonic-gate #include <time.h>
630Sstevel@tonic-gate #include "cryptlib.h"
640Sstevel@tonic-gate #include "o_time.h"
650Sstevel@tonic-gate #include <openssl/asn1.h>
660Sstevel@tonic-gate
670Sstevel@tonic-gate #if 0
680Sstevel@tonic-gate
690Sstevel@tonic-gate int i2d_ASN1_GENERALIZEDTIME(ASN1_GENERALIZEDTIME *a, unsigned char **pp)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate #ifdef CHARSET_EBCDIC
720Sstevel@tonic-gate /* KLUDGE! We convert to ascii before writing DER */
730Sstevel@tonic-gate int len;
740Sstevel@tonic-gate char tmp[24];
750Sstevel@tonic-gate ASN1_STRING tmpstr = *(ASN1_STRING *)a;
760Sstevel@tonic-gate
770Sstevel@tonic-gate len = tmpstr.length;
780Sstevel@tonic-gate ebcdic2ascii(tmp, tmpstr.data, (len >= sizeof tmp) ? sizeof tmp : len);
790Sstevel@tonic-gate tmpstr.data = tmp;
800Sstevel@tonic-gate
810Sstevel@tonic-gate a = (ASN1_GENERALIZEDTIME *) &tmpstr;
820Sstevel@tonic-gate #endif
830Sstevel@tonic-gate return(i2d_ASN1_bytes((ASN1_STRING *)a,pp,
840Sstevel@tonic-gate V_ASN1_GENERALIZEDTIME,V_ASN1_UNIVERSAL));
850Sstevel@tonic-gate }
860Sstevel@tonic-gate
870Sstevel@tonic-gate
880Sstevel@tonic-gate ASN1_GENERALIZEDTIME *d2i_ASN1_GENERALIZEDTIME(ASN1_GENERALIZEDTIME **a,
890Sstevel@tonic-gate unsigned char **pp, long length)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate ASN1_GENERALIZEDTIME *ret=NULL;
920Sstevel@tonic-gate
930Sstevel@tonic-gate ret=(ASN1_GENERALIZEDTIME *)d2i_ASN1_bytes((ASN1_STRING **)a,pp,length,
940Sstevel@tonic-gate V_ASN1_GENERALIZEDTIME,V_ASN1_UNIVERSAL);
950Sstevel@tonic-gate if (ret == NULL)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate ASN1err(ASN1_F_D2I_ASN1_GENERALIZEDTIME,ERR_R_NESTED_ASN1_ERROR);
980Sstevel@tonic-gate return(NULL);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate #ifdef CHARSET_EBCDIC
1010Sstevel@tonic-gate ascii2ebcdic(ret->data, ret->data, ret->length);
1020Sstevel@tonic-gate #endif
1030Sstevel@tonic-gate if (!ASN1_GENERALIZEDTIME_check(ret))
1040Sstevel@tonic-gate {
1050Sstevel@tonic-gate ASN1err(ASN1_F_D2I_ASN1_GENERALIZEDTIME,ASN1_R_INVALID_TIME_FORMAT);
1060Sstevel@tonic-gate goto err;
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate return(ret);
1100Sstevel@tonic-gate err:
1110Sstevel@tonic-gate if ((ret != NULL) && ((a == NULL) || (*a != ret)))
1120Sstevel@tonic-gate M_ASN1_GENERALIZEDTIME_free(ret);
1130Sstevel@tonic-gate return(NULL);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate #endif
1170Sstevel@tonic-gate
ASN1_GENERALIZEDTIME_check(ASN1_GENERALIZEDTIME * d)1180Sstevel@tonic-gate int ASN1_GENERALIZEDTIME_check(ASN1_GENERALIZEDTIME *d)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate static int min[9]={ 0, 0, 1, 1, 0, 0, 0, 0, 0};
1210Sstevel@tonic-gate static int max[9]={99, 99,12,31,23,59,59,12,59};
1220Sstevel@tonic-gate char *a;
1230Sstevel@tonic-gate int n,i,l,o;
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate if (d->type != V_ASN1_GENERALIZEDTIME) return(0);
1260Sstevel@tonic-gate l=d->length;
1270Sstevel@tonic-gate a=(char *)d->data;
1280Sstevel@tonic-gate o=0;
1290Sstevel@tonic-gate /* GENERALIZEDTIME is similar to UTCTIME except the year is
1300Sstevel@tonic-gate * represented as YYYY. This stuff treats everything as a two digit
1310Sstevel@tonic-gate * field so make first two fields 00 to 99
1320Sstevel@tonic-gate */
1330Sstevel@tonic-gate if (l < 13) goto err;
1340Sstevel@tonic-gate for (i=0; i<7; i++)
1350Sstevel@tonic-gate {
1360Sstevel@tonic-gate if ((i == 6) && ((a[o] == 'Z') ||
1370Sstevel@tonic-gate (a[o] == '+') || (a[o] == '-')))
1380Sstevel@tonic-gate { i++; break; }
1390Sstevel@tonic-gate if ((a[o] < '0') || (a[o] > '9')) goto err;
1400Sstevel@tonic-gate n= a[o]-'0';
1410Sstevel@tonic-gate if (++o > l) goto err;
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate if ((a[o] < '0') || (a[o] > '9')) goto err;
1440Sstevel@tonic-gate n=(n*10)+ a[o]-'0';
1450Sstevel@tonic-gate if (++o > l) goto err;
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate if ((n < min[i]) || (n > max[i])) goto err;
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate /* Optional fractional seconds: decimal point followed by one
1500Sstevel@tonic-gate * or more digits.
1510Sstevel@tonic-gate */
1520Sstevel@tonic-gate if (a[o] == '.')
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate if (++o > l) goto err;
1550Sstevel@tonic-gate i = o;
1560Sstevel@tonic-gate while ((a[o] >= '0') && (a[o] <= '9') && (o <= l))
1570Sstevel@tonic-gate o++;
1580Sstevel@tonic-gate /* Must have at least one digit after decimal point */
1590Sstevel@tonic-gate if (i == o) goto err;
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate if (a[o] == 'Z')
1630Sstevel@tonic-gate o++;
1640Sstevel@tonic-gate else if ((a[o] == '+') || (a[o] == '-'))
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate o++;
1670Sstevel@tonic-gate if (o+4 > l) goto err;
1680Sstevel@tonic-gate for (i=7; i<9; i++)
1690Sstevel@tonic-gate {
1700Sstevel@tonic-gate if ((a[o] < '0') || (a[o] > '9')) goto err;
1710Sstevel@tonic-gate n= a[o]-'0';
1720Sstevel@tonic-gate o++;
1730Sstevel@tonic-gate if ((a[o] < '0') || (a[o] > '9')) goto err;
1740Sstevel@tonic-gate n=(n*10)+ a[o]-'0';
1750Sstevel@tonic-gate if ((n < min[i]) || (n > max[i])) goto err;
1760Sstevel@tonic-gate o++;
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate return(o == l);
1800Sstevel@tonic-gate err:
1810Sstevel@tonic-gate return(0);
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate
ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME * s,const char * str)184*2139Sjp161948 int ASN1_GENERALIZEDTIME_set_string(ASN1_GENERALIZEDTIME *s, const char *str)
1850Sstevel@tonic-gate {
1860Sstevel@tonic-gate ASN1_GENERALIZEDTIME t;
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate t.type=V_ASN1_GENERALIZEDTIME;
1890Sstevel@tonic-gate t.length=strlen(str);
1900Sstevel@tonic-gate t.data=(unsigned char *)str;
1910Sstevel@tonic-gate if (ASN1_GENERALIZEDTIME_check(&t))
1920Sstevel@tonic-gate {
1930Sstevel@tonic-gate if (s != NULL)
1940Sstevel@tonic-gate {
195*2139Sjp161948 if (!ASN1_STRING_set((ASN1_STRING *)s,
196*2139Sjp161948 (unsigned char *)str,t.length))
197*2139Sjp161948 return 0;
1980Sstevel@tonic-gate s->type=V_ASN1_GENERALIZEDTIME;
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate return(1);
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate else
2030Sstevel@tonic-gate return(0);
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate
ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME * s,time_t t)2060Sstevel@tonic-gate ASN1_GENERALIZEDTIME *ASN1_GENERALIZEDTIME_set(ASN1_GENERALIZEDTIME *s,
2070Sstevel@tonic-gate time_t t)
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate char *p;
2100Sstevel@tonic-gate struct tm *ts;
2110Sstevel@tonic-gate struct tm data;
2120Sstevel@tonic-gate size_t len = 20;
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate if (s == NULL)
2150Sstevel@tonic-gate s=M_ASN1_GENERALIZEDTIME_new();
2160Sstevel@tonic-gate if (s == NULL)
2170Sstevel@tonic-gate return(NULL);
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate ts=OPENSSL_gmtime(&t, &data);
2200Sstevel@tonic-gate if (ts == NULL)
2210Sstevel@tonic-gate return(NULL);
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate p=(char *)s->data;
2240Sstevel@tonic-gate if ((p == NULL) || ((size_t)s->length < len))
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate p=OPENSSL_malloc(len);
227*2139Sjp161948 if (p == NULL)
228*2139Sjp161948 {
229*2139Sjp161948 ASN1err(ASN1_F_ASN1_GENERALIZEDTIME_SET,
230*2139Sjp161948 ERR_R_MALLOC_FAILURE);
231*2139Sjp161948 return(NULL);
232*2139Sjp161948 }
2330Sstevel@tonic-gate if (s->data != NULL)
2340Sstevel@tonic-gate OPENSSL_free(s->data);
2350Sstevel@tonic-gate s->data=(unsigned char *)p;
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate BIO_snprintf(p,len,"%04d%02d%02d%02d%02d%02dZ",ts->tm_year + 1900,
2390Sstevel@tonic-gate ts->tm_mon+1,ts->tm_mday,ts->tm_hour,ts->tm_min,ts->tm_sec);
2400Sstevel@tonic-gate s->length=strlen(p);
2410Sstevel@tonic-gate s->type=V_ASN1_GENERALIZEDTIME;
2420Sstevel@tonic-gate #ifdef CHARSET_EBCDIC_not
2430Sstevel@tonic-gate ebcdic2ascii(s->data, s->data, s->length);
2440Sstevel@tonic-gate #endif
2450Sstevel@tonic-gate return(s);
2460Sstevel@tonic-gate }
247