sl@0: /* crypto/asn1/a_utctm.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include "o_time.h" sl@0: #include sl@0: #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__))) sl@0: #include "libcrypto_wsd_macros.h" sl@0: #include "libcrypto_wsd.h" sl@0: #endif sl@0: sl@0: #if 0 sl@0: EXPORT_C int i2d_ASN1_UTCTIME(ASN1_UTCTIME *a, unsigned char **pp) sl@0: { sl@0: #ifndef CHARSET_EBCDIC sl@0: return(i2d_ASN1_bytes((ASN1_STRING *)a,pp, sl@0: V_ASN1_UTCTIME,V_ASN1_UNIVERSAL)); sl@0: #else sl@0: /* KLUDGE! We convert to ascii before writing DER */ sl@0: int len; sl@0: char tmp[24]; sl@0: ASN1_STRING x = *(ASN1_STRING *)a; sl@0: sl@0: len = x.length; sl@0: ebcdic2ascii(tmp, x.data, (len >= sizeof tmp) ? sizeof tmp : len); sl@0: x.data = tmp; sl@0: return i2d_ASN1_bytes(&x, pp, V_ASN1_UTCTIME,V_ASN1_UNIVERSAL); sl@0: #endif sl@0: } sl@0: sl@0: sl@0: EXPORT_C ASN1_UTCTIME *d2i_ASN1_UTCTIME(ASN1_UTCTIME **a, unsigned char **pp, sl@0: long length) sl@0: { sl@0: ASN1_UTCTIME *ret=NULL; sl@0: sl@0: ret=(ASN1_UTCTIME *)d2i_ASN1_bytes((ASN1_STRING **)a,pp,length, sl@0: V_ASN1_UTCTIME,V_ASN1_UNIVERSAL); sl@0: if (ret == NULL) sl@0: { sl@0: ASN1err(ASN1_F_D2I_ASN1_UTCTIME,ERR_R_NESTED_ASN1_ERROR); sl@0: return(NULL); sl@0: } sl@0: #ifdef CHARSET_EBCDIC sl@0: ascii2ebcdic(ret->data, ret->data, ret->length); sl@0: #endif sl@0: if (!ASN1_UTCTIME_check(ret)) sl@0: { sl@0: ASN1err(ASN1_F_D2I_ASN1_UTCTIME,ASN1_R_INVALID_TIME_FORMAT); sl@0: goto err; sl@0: } sl@0: sl@0: return(ret); sl@0: err: sl@0: if ((ret != NULL) && ((a == NULL) || (*a != ret))) sl@0: M_ASN1_UTCTIME_free(ret); sl@0: return(NULL); sl@0: } sl@0: sl@0: #endif sl@0: sl@0: sl@0: sl@0: sl@0: EXPORT_C int ASN1_UTCTIME_check(ASN1_UTCTIME *d) sl@0: { sl@0: #ifdef EMULATOR sl@0: static int min[8]={ 0, 1, 1, 0, 0, 0, 0, 0}; sl@0: static int max[8]={99,12,31,23,59,59,12,59}; sl@0: #else sl@0: static const int min[8]={ 0, 1, 1, 0, 0, 0, 0, 0}; sl@0: static const int max[8]={99,12,31,23,59,59,12,59}; sl@0: #endif sl@0: char *a; sl@0: int n,i,l,o; sl@0: sl@0: if (d->type != V_ASN1_UTCTIME) return(0); sl@0: l=d->length; sl@0: a=(char *)d->data; sl@0: o=0; sl@0: sl@0: if (l < 11) goto err; sl@0: for (i=0; i<6; i++) sl@0: { sl@0: if ((i == 5) && ((a[o] == 'Z') || sl@0: (a[o] == '+') || (a[o] == '-'))) sl@0: { i++; break; } sl@0: if ((a[o] < '0') || (a[o] > '9')) goto err; sl@0: n= a[o]-'0'; sl@0: if (++o > l) goto err; sl@0: sl@0: if ((a[o] < '0') || (a[o] > '9')) goto err; sl@0: n=(n*10)+ a[o]-'0'; sl@0: if (++o > l) goto err; sl@0: sl@0: if ((n < min[i]) || (n > max[i])) goto err; sl@0: } sl@0: if (a[o] == 'Z') sl@0: o++; sl@0: else if ((a[o] == '+') || (a[o] == '-')) sl@0: { sl@0: o++; sl@0: if (o+4 > l) goto err; sl@0: for (i=6; i<8; i++) sl@0: { sl@0: if ((a[o] < '0') || (a[o] > '9')) goto err; sl@0: n= a[o]-'0'; sl@0: o++; sl@0: if ((a[o] < '0') || (a[o] > '9')) goto err; sl@0: n=(n*10)+ a[o]-'0'; sl@0: if ((n < min[i]) || (n > max[i])) goto err; sl@0: o++; sl@0: } sl@0: } sl@0: return(o == l); sl@0: err: sl@0: return(0); sl@0: } sl@0: sl@0: EXPORT_C int ASN1_UTCTIME_set_string(ASN1_UTCTIME *s, const char *str) sl@0: { sl@0: ASN1_UTCTIME t; sl@0: sl@0: t.type=V_ASN1_UTCTIME; sl@0: t.length=strlen(str); sl@0: t.data=(unsigned char *)str; sl@0: if (ASN1_UTCTIME_check(&t)) sl@0: { sl@0: if (s != NULL) sl@0: { sl@0: if (!ASN1_STRING_set((ASN1_STRING *)s, sl@0: (unsigned char *)str,t.length)) sl@0: return 0; sl@0: s->type = V_ASN1_UTCTIME; sl@0: } sl@0: return(1); sl@0: } sl@0: else sl@0: return(0); sl@0: } sl@0: sl@0: EXPORT_C ASN1_UTCTIME *ASN1_UTCTIME_set(ASN1_UTCTIME *s, time_t t) sl@0: { sl@0: char *p; sl@0: struct tm *ts; sl@0: struct tm data; sl@0: size_t len = 20; sl@0: sl@0: if (s == NULL) sl@0: s=M_ASN1_UTCTIME_new(); sl@0: if (s == NULL) sl@0: return(NULL); sl@0: sl@0: ts=OPENSSL_gmtime(&t, &data); sl@0: if (ts == NULL) sl@0: return(NULL); sl@0: sl@0: p=(char *)s->data; sl@0: if ((p == NULL) || ((size_t)s->length < len)) sl@0: { sl@0: p=OPENSSL_malloc(len); sl@0: if (p == NULL) sl@0: { sl@0: ASN1err(ASN1_F_ASN1_UTCTIME_SET,ERR_R_MALLOC_FAILURE); sl@0: return(NULL); sl@0: } sl@0: if (s->data != NULL) sl@0: OPENSSL_free(s->data); sl@0: s->data=(unsigned char *)p; sl@0: } sl@0: sl@0: BIO_snprintf(p,len,"%02d%02d%02d%02d%02d%02dZ",ts->tm_year%100, sl@0: ts->tm_mon+1,ts->tm_mday,ts->tm_hour,ts->tm_min,ts->tm_sec); sl@0: s->length=strlen(p); sl@0: s->type=V_ASN1_UTCTIME; sl@0: #ifdef CHARSET_EBCDIC_not sl@0: ebcdic2ascii(s->data, s->data, s->length); sl@0: #endif sl@0: return(s); sl@0: } sl@0: sl@0: sl@0: EXPORT_C int ASN1_UTCTIME_cmp_time_t(const ASN1_UTCTIME *s, time_t t) sl@0: { sl@0: struct tm *tm; sl@0: struct tm data; sl@0: int offset; sl@0: int year; sl@0: sl@0: #define g2(p) (((p)[0]-'0')*10+(p)[1]-'0') sl@0: sl@0: if (s->data[12] == 'Z') sl@0: offset=0; sl@0: else sl@0: { sl@0: offset = g2(s->data+13)*60+g2(s->data+15); sl@0: if (s->data[12] == '-') sl@0: offset = -offset; sl@0: } sl@0: sl@0: t -= offset*60; /* FIXME: may overflow in extreme cases */ sl@0: sl@0: tm = OPENSSL_gmtime(&t, &data); sl@0: sl@0: #define return_cmp(a,b) if ((a)<(b)) return -1; else if ((a)>(b)) return 1 sl@0: year = g2(s->data); sl@0: if (year < 50) sl@0: year += 100; sl@0: return_cmp(year, tm->tm_year); sl@0: return_cmp(g2(s->data+2) - 1, tm->tm_mon); sl@0: return_cmp(g2(s->data+4), tm->tm_mday); sl@0: return_cmp(g2(s->data+6), tm->tm_hour); sl@0: return_cmp(g2(s->data+8), tm->tm_min); sl@0: return_cmp(g2(s->data+10), tm->tm_sec); sl@0: #undef g2 sl@0: #undef return_cmp sl@0: sl@0: return 0; sl@0: } sl@0: sl@0: sl@0: #if 0 sl@0: EXPORT_C time_t ASN1_UTCTIME_get(const ASN1_UTCTIME *s) sl@0: { sl@0: struct tm tm; sl@0: int offset; sl@0: sl@0: memset(&tm,'\0',sizeof tm); sl@0: sl@0: #define g2(p) (((p)[0]-'0')*10+(p)[1]-'0') sl@0: tm.tm_year=g2(s->data); sl@0: if(tm.tm_year < 50) sl@0: tm.tm_year+=100; sl@0: tm.tm_mon=g2(s->data+2)-1; sl@0: tm.tm_mday=g2(s->data+4); sl@0: tm.tm_hour=g2(s->data+6); sl@0: tm.tm_min=g2(s->data+8); sl@0: tm.tm_sec=g2(s->data+10); sl@0: if(s->data[12] == 'Z') sl@0: offset=0; sl@0: else sl@0: { sl@0: offset=g2(s->data+13)*60+g2(s->data+15); sl@0: if(s->data[12] == '-') sl@0: offset= -offset; sl@0: } sl@0: #undef g2 sl@0: sl@0: return mktime(&tm)-offset*60; /* FIXME: mktime assumes the current timezone sl@0: * instead of UTC, and unless we rewrite OpenSSL sl@0: * in Lisp we cannot locally change the timezone sl@0: * without possibly interfering with other parts sl@0: * of the program. timegm, which uses UTC, is sl@0: * non-standard. sl@0: * Also time_t is inappropriate for general sl@0: * UTC times because it may a 32 bit type. */ sl@0: } sl@0: #endif