sl@0: /* crypto/asn1/t_x509.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 <stdio.h> sl@0: #include "cryptlib.h" sl@0: #include <openssl/buffer.h> sl@0: #include <openssl/bn.h> sl@0: #ifndef OPENSSL_NO_RSA sl@0: #include <openssl/rsa.h> sl@0: #endif sl@0: #ifndef OPENSSL_NO_DSA sl@0: #include <openssl/dsa.h> sl@0: #endif sl@0: #ifndef OPENSSL_NO_EC sl@0: #include <openssl/ec.h> sl@0: #endif sl@0: #include <openssl/objects.h> sl@0: #include <openssl/x509.h> sl@0: #include <openssl/x509v3.h> 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: sl@0: sl@0: #ifndef OPENSSL_NO_FP_API sl@0: EXPORT_C int X509_print_fp(FILE *fp, X509 *x) sl@0: { sl@0: return X509_print_ex_fp(fp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT); sl@0: } sl@0: sl@0: EXPORT_C int X509_print_ex_fp(FILE *fp, X509 *x, unsigned long nmflag, unsigned long cflag) sl@0: { sl@0: BIO *b; sl@0: int ret; sl@0: sl@0: if ((b=BIO_new(BIO_s_file())) == NULL) sl@0: { sl@0: X509err(X509_F_X509_PRINT_EX_FP,ERR_R_BUF_LIB); sl@0: return(0); sl@0: } sl@0: BIO_set_fp(b,fp,BIO_NOCLOSE); sl@0: ret=X509_print_ex(b, x, nmflag, cflag); sl@0: BIO_free(b); sl@0: return(ret); sl@0: } sl@0: #endif sl@0: sl@0: EXPORT_C int X509_print(BIO *bp, X509 *x) sl@0: { sl@0: return X509_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT); sl@0: } sl@0: sl@0: EXPORT_C int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, unsigned long cflag) sl@0: { sl@0: long l; sl@0: int ret=0,i; sl@0: char *m=NULL,mlch = ' '; sl@0: int nmindent = 0; sl@0: X509_CINF *ci; sl@0: ASN1_INTEGER *bs; sl@0: EVP_PKEY *pkey=NULL; sl@0: const char *neg; sl@0: ASN1_STRING *str=NULL; sl@0: sl@0: if((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) { sl@0: mlch = '\n'; sl@0: nmindent = 12; sl@0: } sl@0: sl@0: if(nmflags == X509_FLAG_COMPAT) sl@0: nmindent = 16; sl@0: sl@0: ci=x->cert_info; sl@0: if(!(cflag & X509_FLAG_NO_HEADER)) sl@0: { sl@0: if (BIO_write(bp,"Certificate:\n",13) <= 0) goto err; sl@0: if (BIO_write(bp," Data:\n",10) <= 0) goto err; sl@0: } sl@0: if(!(cflag & X509_FLAG_NO_VERSION)) sl@0: { sl@0: l=X509_get_version(x); sl@0: if (BIO_printf(bp,"%8sVersion: %lu (0x%lx)\n","",l+1,l) <= 0) goto err; sl@0: } sl@0: if(!(cflag & X509_FLAG_NO_SERIAL)) sl@0: { sl@0: sl@0: if (BIO_write(bp," Serial Number:",22) <= 0) goto err; sl@0: sl@0: bs=X509_get_serialNumber(x); sl@0: if (bs->length <= 4) sl@0: { sl@0: l=ASN1_INTEGER_get(bs); sl@0: if (l < 0) sl@0: { sl@0: l= -l; sl@0: neg="-"; sl@0: } sl@0: else sl@0: neg=""; sl@0: if (BIO_printf(bp," %s%lu (%s0x%lx)\n",neg,l,neg,l) <= 0) sl@0: goto err; sl@0: } sl@0: else sl@0: { sl@0: neg=(bs->type == V_ASN1_NEG_INTEGER)?" (Negative)":""; sl@0: if (BIO_printf(bp,"\n%12s%s","",neg) <= 0) goto err; sl@0: sl@0: for (i=0; i<bs->length; i++) sl@0: { sl@0: if (BIO_printf(bp,"%02x%c",bs->data[i], sl@0: ((i+1 == bs->length)?'\n':':')) <= 0) sl@0: goto err; sl@0: } sl@0: } sl@0: sl@0: } sl@0: sl@0: if(!(cflag & X509_FLAG_NO_SIGNAME)) sl@0: { sl@0: if (BIO_printf(bp,"%8sSignature Algorithm: ","") <= 0) sl@0: goto err; sl@0: if (i2a_ASN1_OBJECT(bp, ci->signature->algorithm) <= 0) sl@0: goto err; sl@0: if (BIO_puts(bp, "\n") <= 0) sl@0: goto err; sl@0: } sl@0: sl@0: if(!(cflag & X509_FLAG_NO_ISSUER)) sl@0: { sl@0: if (BIO_printf(bp," Issuer:%c",mlch) <= 0) goto err; sl@0: if (X509_NAME_print_ex(bp,X509_get_issuer_name(x),nmindent, nmflags) < 0) goto err; sl@0: if (BIO_write(bp,"\n",1) <= 0) goto err; sl@0: } sl@0: if(!(cflag & X509_FLAG_NO_VALIDITY)) sl@0: { sl@0: if (BIO_write(bp," Validity\n",17) <= 0) goto err; sl@0: if (BIO_write(bp," Not Before: ",24) <= 0) goto err; sl@0: if (!ASN1_TIME_print(bp,X509_get_notBefore(x))) goto err; sl@0: if (BIO_write(bp,"\n Not After : ",25) <= 0) goto err; sl@0: if (!ASN1_TIME_print(bp,X509_get_notAfter(x))) goto err; sl@0: if (BIO_write(bp,"\n",1) <= 0) goto err; sl@0: } sl@0: if(!(cflag & X509_FLAG_NO_SUBJECT)) sl@0: { sl@0: if (BIO_printf(bp," Subject:%c",mlch) <= 0) goto err; sl@0: if (X509_NAME_print_ex(bp,X509_get_subject_name(x),nmindent, nmflags) < 0) goto err; sl@0: if (BIO_write(bp,"\n",1) <= 0) goto err; sl@0: } sl@0: if(!(cflag & X509_FLAG_NO_PUBKEY)) sl@0: { sl@0: if (BIO_write(bp," Subject Public Key Info:\n",33) <= 0) sl@0: goto err; sl@0: if (BIO_printf(bp,"%12sPublic Key Algorithm: ","") <= 0) sl@0: goto err; sl@0: if (i2a_ASN1_OBJECT(bp, ci->key->algor->algorithm) <= 0) sl@0: goto err; sl@0: if (BIO_puts(bp, "\n") <= 0) sl@0: goto err; sl@0: sl@0: pkey=X509_get_pubkey(x); sl@0: if (pkey == NULL) sl@0: { sl@0: BIO_printf(bp,"%12sUnable to load Public Key\n",""); sl@0: ERR_print_errors(bp); sl@0: } sl@0: else sl@0: #ifndef OPENSSL_NO_RSA sl@0: if (pkey->type == EVP_PKEY_RSA) sl@0: { sl@0: BIO_printf(bp,"%12sRSA Public Key: (%d bit)\n","", sl@0: BN_num_bits(pkey->pkey.rsa->n)); sl@0: RSA_print(bp,pkey->pkey.rsa,16); sl@0: } sl@0: else sl@0: #endif sl@0: #ifndef OPENSSL_NO_DSA sl@0: if (pkey->type == EVP_PKEY_DSA) sl@0: { sl@0: BIO_printf(bp,"%12sDSA Public Key:\n",""); sl@0: DSA_print(bp,pkey->pkey.dsa,16); sl@0: } sl@0: else sl@0: #endif sl@0: #ifndef OPENSSL_NO_EC sl@0: if (pkey->type == EVP_PKEY_EC) sl@0: { sl@0: BIO_printf(bp, "%12sEC Public Key:\n",""); sl@0: EC_KEY_print(bp, pkey->pkey.ec, 16); sl@0: } sl@0: else sl@0: #endif sl@0: BIO_printf(bp,"%12sUnknown Public Key:\n",""); sl@0: sl@0: EVP_PKEY_free(pkey); sl@0: } sl@0: sl@0: if (!(cflag & X509_FLAG_NO_EXTENSIONS)) sl@0: X509V3_extensions_print(bp, "X509v3 extensions", sl@0: ci->extensions, cflag, 8); sl@0: sl@0: if(!(cflag & X509_FLAG_NO_SIGDUMP)) sl@0: { sl@0: if(X509_signature_print(bp, x->sig_alg, x->signature) <= 0) goto err; sl@0: } sl@0: if(!(cflag & X509_FLAG_NO_AUX)) sl@0: { sl@0: if (!X509_CERT_AUX_print(bp, x->aux, 0)) goto err; sl@0: } sl@0: ret=1; sl@0: err: sl@0: if (str != NULL) ASN1_STRING_free(str); sl@0: if (m != NULL) OPENSSL_free(m); sl@0: return(ret); sl@0: } sl@0: sl@0: EXPORT_C int X509_ocspid_print (BIO *bp, X509 *x) sl@0: { sl@0: unsigned char *der=NULL ; sl@0: unsigned char *dertmp; sl@0: int derlen; sl@0: int i; sl@0: unsigned char SHA1md[SHA_DIGEST_LENGTH]; sl@0: sl@0: /* display the hash of the subject as it would appear sl@0: in OCSP requests */ sl@0: if (BIO_printf(bp," Subject OCSP hash: ") <= 0) sl@0: goto err; sl@0: derlen = i2d_X509_NAME(x->cert_info->subject, NULL); sl@0: if ((der = dertmp = (unsigned char *)OPENSSL_malloc (derlen)) == NULL) sl@0: goto err; sl@0: i2d_X509_NAME(x->cert_info->subject, &dertmp); sl@0: sl@0: EVP_Digest(der, derlen, SHA1md, NULL, EVP_sha1(), NULL); sl@0: for (i=0; i < SHA_DIGEST_LENGTH; i++) sl@0: { sl@0: if (BIO_printf(bp,"%02X",SHA1md[i]) <= 0) goto err; sl@0: } sl@0: OPENSSL_free (der); sl@0: der=NULL; sl@0: sl@0: /* display the hash of the public key as it would appear sl@0: in OCSP requests */ sl@0: if (BIO_printf(bp,"\n Public key OCSP hash: ") <= 0) sl@0: goto err; sl@0: sl@0: EVP_Digest(x->cert_info->key->public_key->data, sl@0: x->cert_info->key->public_key->length, SHA1md, NULL, EVP_sha1(), NULL); sl@0: for (i=0; i < SHA_DIGEST_LENGTH; i++) sl@0: { sl@0: if (BIO_printf(bp,"%02X",SHA1md[i]) <= 0) sl@0: goto err; sl@0: } sl@0: BIO_printf(bp,"\n"); sl@0: sl@0: return (1); sl@0: err: sl@0: if (der != NULL) OPENSSL_free(der); sl@0: return(0); sl@0: } sl@0: sl@0: EXPORT_C int X509_signature_print(BIO *bp, X509_ALGOR *sigalg, ASN1_STRING *sig) sl@0: { sl@0: unsigned char *s; sl@0: int i, n; sl@0: if (BIO_puts(bp," Signature Algorithm: ") <= 0) return 0; sl@0: if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0) return 0; sl@0: sl@0: n=sig->length; sl@0: s=sig->data; sl@0: for (i=0; i<n; i++) sl@0: { sl@0: if ((i%18) == 0) sl@0: if (BIO_write(bp,"\n ",9) <= 0) return 0; sl@0: if (BIO_printf(bp,"%02x%s",s[i], sl@0: ((i+1) == n)?"":":") <= 0) return 0; sl@0: } sl@0: if (BIO_write(bp,"\n",1) != 1) return 0; sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C int ASN1_STRING_print(BIO *bp, ASN1_STRING *v) sl@0: { sl@0: int i,n; sl@0: char buf[80],*p;; sl@0: sl@0: if (v == NULL) return(0); sl@0: n=0; sl@0: p=(char *)v->data; sl@0: for (i=0; i<v->length; i++) sl@0: { sl@0: if ((p[i] > '~') || ((p[i] < ' ') && sl@0: (p[i] != '\n') && (p[i] != '\r'))) sl@0: buf[n]='.'; sl@0: else sl@0: buf[n]=p[i]; sl@0: n++; sl@0: if (n >= 80) sl@0: { sl@0: if (BIO_write(bp,buf,n) <= 0) sl@0: return(0); sl@0: n=0; sl@0: } sl@0: } sl@0: if (n > 0) sl@0: if (BIO_write(bp,buf,n) <= 0) sl@0: return(0); sl@0: return(1); sl@0: } sl@0: sl@0: EXPORT_C int ASN1_TIME_print(BIO *bp, ASN1_TIME *tm) sl@0: { sl@0: if(tm->type == V_ASN1_UTCTIME) return ASN1_UTCTIME_print(bp, tm); sl@0: if(tm->type == V_ASN1_GENERALIZEDTIME) sl@0: return ASN1_GENERALIZEDTIME_print(bp, tm); sl@0: BIO_write(bp,"Bad time value",14); sl@0: return(0); sl@0: } sl@0: sl@0: #ifndef EMULATOR sl@0: static const char *mon[12]= sl@0: { sl@0: "Jan","Feb","Mar","Apr","May","Jun", sl@0: "Jul","Aug","Sep","Oct","Nov","Dec" sl@0: }; sl@0: #else sl@0: static const char * const mon[12]= sl@0: { sl@0: "Jan","Feb","Mar","Apr","May","Jun", sl@0: "Jul","Aug","Sep","Oct","Nov","Dec" sl@0: }; sl@0: sl@0: #endif sl@0: EXPORT_C int ASN1_GENERALIZEDTIME_print(BIO *bp, ASN1_GENERALIZEDTIME *tm) sl@0: { sl@0: char *v; sl@0: int gmt=0; sl@0: int i; sl@0: int y=0,M=0,d=0,h=0,m=0,s=0; sl@0: sl@0: i=tm->length; sl@0: v=(char *)tm->data; sl@0: sl@0: if (i < 12) goto err; sl@0: if (v[i-1] == 'Z') gmt=1; sl@0: for (i=0; i<12; i++) sl@0: if ((v[i] > '9') || (v[i] < '0')) goto err; sl@0: y= (v[0]-'0')*1000+(v[1]-'0')*100 + (v[2]-'0')*10+(v[3]-'0'); sl@0: M= (v[4]-'0')*10+(v[5]-'0'); sl@0: if ((M > 12) || (M < 1)) goto err; sl@0: d= (v[6]-'0')*10+(v[7]-'0'); sl@0: h= (v[8]-'0')*10+(v[9]-'0'); sl@0: m= (v[10]-'0')*10+(v[11]-'0'); sl@0: if ( (v[12] >= '0') && (v[12] <= '9') && sl@0: (v[13] >= '0') && (v[13] <= '9')) sl@0: s= (v[12]-'0')*10+(v[13]-'0'); sl@0: sl@0: if (BIO_printf(bp,"%s %2d %02d:%02d:%02d %d%s", sl@0: mon[M-1],d,h,m,s,y,(gmt)?" GMT":"") <= 0) sl@0: return(0); sl@0: else sl@0: return(1); sl@0: err: sl@0: BIO_write(bp,"Bad time value",14); sl@0: return(0); sl@0: } sl@0: sl@0: EXPORT_C int ASN1_UTCTIME_print(BIO *bp, ASN1_UTCTIME *tm) sl@0: { sl@0: char *v; sl@0: int gmt=0; sl@0: int i; sl@0: int y=0,M=0,d=0,h=0,m=0,s=0; sl@0: sl@0: i=tm->length; sl@0: v=(char *)tm->data; sl@0: sl@0: if (i < 10) goto err; sl@0: if (v[i-1] == 'Z') gmt=1; sl@0: for (i=0; i<10; i++) sl@0: if ((v[i] > '9') || (v[i] < '0')) goto err; sl@0: y= (v[0]-'0')*10+(v[1]-'0'); sl@0: if (y < 50) y+=100; sl@0: M= (v[2]-'0')*10+(v[3]-'0'); sl@0: if ((M > 12) || (M < 1)) goto err; sl@0: d= (v[4]-'0')*10+(v[5]-'0'); sl@0: h= (v[6]-'0')*10+(v[7]-'0'); sl@0: m= (v[8]-'0')*10+(v[9]-'0'); sl@0: if ( (v[10] >= '0') && (v[10] <= '9') && sl@0: (v[11] >= '0') && (v[11] <= '9')) sl@0: s= (v[10]-'0')*10+(v[11]-'0'); sl@0: sl@0: if (BIO_printf(bp,"%s %2d %02d:%02d:%02d %d%s", sl@0: mon[M-1],d,h,m,s,y+1900,(gmt)?" GMT":"") <= 0) sl@0: return(0); sl@0: else sl@0: return(1); sl@0: err: sl@0: BIO_write(bp,"Bad time value",14); sl@0: return(0); sl@0: } sl@0: sl@0: EXPORT_C int X509_NAME_print(BIO *bp, X509_NAME *name, int obase) sl@0: { sl@0: char *s,*c,*b; sl@0: int ret=0,l,i; sl@0: sl@0: l=80-2-obase; sl@0: sl@0: b=s=X509_NAME_oneline(name,NULL,0); sl@0: if (!*s) sl@0: { sl@0: OPENSSL_free(b); sl@0: return 1; sl@0: } sl@0: s++; /* skip the first slash */ sl@0: sl@0: c=s; sl@0: for (;;) sl@0: { sl@0: #ifndef CHARSET_EBCDIC sl@0: if ( ((*s == '/') && sl@0: ((s[1] >= 'A') && (s[1] <= 'Z') && ( sl@0: (s[2] == '=') || sl@0: ((s[2] >= 'A') && (s[2] <= 'Z') && sl@0: (s[3] == '=')) sl@0: ))) || sl@0: (*s == '\0')) sl@0: #else sl@0: if ( ((*s == '/') && sl@0: (isupper(s[1]) && ( sl@0: (s[2] == '=') || sl@0: (isupper(s[2]) && sl@0: (s[3] == '=')) sl@0: ))) || sl@0: (*s == '\0')) sl@0: #endif sl@0: { sl@0: i=s-c; sl@0: if (BIO_write(bp,c,i) != i) goto err; sl@0: c+=i; sl@0: c++; sl@0: if (*s != '\0') sl@0: { sl@0: if (BIO_write(bp,", ",2) != 2) goto err; sl@0: } sl@0: l--; sl@0: } sl@0: if (*s == '\0') break; sl@0: s++; sl@0: l--; sl@0: } sl@0: sl@0: ret=1; sl@0: if (0) sl@0: { sl@0: err: sl@0: X509err(X509_F_X509_NAME_PRINT,ERR_R_BUF_LIB); sl@0: } sl@0: OPENSSL_free(b); sl@0: return(ret); sl@0: } sl@0: