xref: /onnv-gate/usr/src/common/openssl/crypto/asn1/t_x509.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/asn1/t_x509.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 #include <stdio.h>
600Sstevel@tonic-gate #include "cryptlib.h"
610Sstevel@tonic-gate #include <openssl/buffer.h>
620Sstevel@tonic-gate #include <openssl/bn.h>
630Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
640Sstevel@tonic-gate #include <openssl/rsa.h>
650Sstevel@tonic-gate #endif
660Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
670Sstevel@tonic-gate #include <openssl/dsa.h>
680Sstevel@tonic-gate #endif
69*2139Sjp161948 #ifndef OPENSSL_NO_EC
70*2139Sjp161948 #include <openssl/ec.h>
71*2139Sjp161948 #endif
720Sstevel@tonic-gate #include <openssl/objects.h>
730Sstevel@tonic-gate #include <openssl/x509.h>
740Sstevel@tonic-gate #include <openssl/x509v3.h>
750Sstevel@tonic-gate 
760Sstevel@tonic-gate #ifndef OPENSSL_NO_FP_API
X509_print_fp(FILE * fp,X509 * x)770Sstevel@tonic-gate int X509_print_fp(FILE *fp, X509 *x)
780Sstevel@tonic-gate 	{
790Sstevel@tonic-gate 	return X509_print_ex_fp(fp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
800Sstevel@tonic-gate 	}
810Sstevel@tonic-gate 
X509_print_ex_fp(FILE * fp,X509 * x,unsigned long nmflag,unsigned long cflag)820Sstevel@tonic-gate int X509_print_ex_fp(FILE *fp, X509 *x, unsigned long nmflag, unsigned long cflag)
830Sstevel@tonic-gate         {
840Sstevel@tonic-gate         BIO *b;
850Sstevel@tonic-gate         int ret;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate         if ((b=BIO_new(BIO_s_file())) == NULL)
880Sstevel@tonic-gate 		{
89*2139Sjp161948 		X509err(X509_F_X509_PRINT_EX_FP,ERR_R_BUF_LIB);
900Sstevel@tonic-gate                 return(0);
910Sstevel@tonic-gate 		}
920Sstevel@tonic-gate         BIO_set_fp(b,fp,BIO_NOCLOSE);
930Sstevel@tonic-gate         ret=X509_print_ex(b, x, nmflag, cflag);
940Sstevel@tonic-gate         BIO_free(b);
950Sstevel@tonic-gate         return(ret);
960Sstevel@tonic-gate         }
970Sstevel@tonic-gate #endif
980Sstevel@tonic-gate 
X509_print(BIO * bp,X509 * x)990Sstevel@tonic-gate int X509_print(BIO *bp, X509 *x)
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate 	return X509_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate 
X509_print_ex(BIO * bp,X509 * x,unsigned long nmflags,unsigned long cflag)1040Sstevel@tonic-gate int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, unsigned long cflag)
1050Sstevel@tonic-gate 	{
1060Sstevel@tonic-gate 	long l;
1070Sstevel@tonic-gate 	int ret=0,i;
1080Sstevel@tonic-gate 	char *m=NULL,mlch = ' ';
1090Sstevel@tonic-gate 	int nmindent = 0;
1100Sstevel@tonic-gate 	X509_CINF *ci;
1110Sstevel@tonic-gate 	ASN1_INTEGER *bs;
1120Sstevel@tonic-gate 	EVP_PKEY *pkey=NULL;
1130Sstevel@tonic-gate 	const char *neg;
1140Sstevel@tonic-gate 	ASN1_STRING *str=NULL;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	if((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
1170Sstevel@tonic-gate 			mlch = '\n';
1180Sstevel@tonic-gate 			nmindent = 12;
1190Sstevel@tonic-gate 	}
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	if(nmflags == X509_FLAG_COMPAT)
1220Sstevel@tonic-gate 		nmindent = 16;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	ci=x->cert_info;
1250Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_HEADER))
1260Sstevel@tonic-gate 		{
1270Sstevel@tonic-gate 		if (BIO_write(bp,"Certificate:\n",13) <= 0) goto err;
1280Sstevel@tonic-gate 		if (BIO_write(bp,"    Data:\n",10) <= 0) goto err;
1290Sstevel@tonic-gate 		}
1300Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_VERSION))
1310Sstevel@tonic-gate 		{
1320Sstevel@tonic-gate 		l=X509_get_version(x);
1330Sstevel@tonic-gate 		if (BIO_printf(bp,"%8sVersion: %lu (0x%lx)\n","",l+1,l) <= 0) goto err;
1340Sstevel@tonic-gate 		}
1350Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_SERIAL))
1360Sstevel@tonic-gate 		{
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 		if (BIO_write(bp,"        Serial Number:",22) <= 0) goto err;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 		bs=X509_get_serialNumber(x);
1410Sstevel@tonic-gate 		if (bs->length <= 4)
1420Sstevel@tonic-gate 			{
1430Sstevel@tonic-gate 			l=ASN1_INTEGER_get(bs);
1440Sstevel@tonic-gate 			if (l < 0)
1450Sstevel@tonic-gate 				{
1460Sstevel@tonic-gate 				l= -l;
1470Sstevel@tonic-gate 				neg="-";
1480Sstevel@tonic-gate 				}
1490Sstevel@tonic-gate 			else
1500Sstevel@tonic-gate 				neg="";
1510Sstevel@tonic-gate 			if (BIO_printf(bp," %s%lu (%s0x%lx)\n",neg,l,neg,l) <= 0)
1520Sstevel@tonic-gate 				goto err;
1530Sstevel@tonic-gate 			}
1540Sstevel@tonic-gate 		else
1550Sstevel@tonic-gate 			{
1560Sstevel@tonic-gate 			neg=(bs->type == V_ASN1_NEG_INTEGER)?" (Negative)":"";
1570Sstevel@tonic-gate 			if (BIO_printf(bp,"\n%12s%s","",neg) <= 0) goto err;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 			for (i=0; i<bs->length; i++)
1600Sstevel@tonic-gate 				{
1610Sstevel@tonic-gate 				if (BIO_printf(bp,"%02x%c",bs->data[i],
1620Sstevel@tonic-gate 					((i+1 == bs->length)?'\n':':')) <= 0)
1630Sstevel@tonic-gate 					goto err;
1640Sstevel@tonic-gate 				}
1650Sstevel@tonic-gate 			}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 		}
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_SIGNAME))
1700Sstevel@tonic-gate 		{
1710Sstevel@tonic-gate 		if (BIO_printf(bp,"%8sSignature Algorithm: ","") <= 0)
1720Sstevel@tonic-gate 			goto err;
1730Sstevel@tonic-gate 		if (i2a_ASN1_OBJECT(bp, ci->signature->algorithm) <= 0)
1740Sstevel@tonic-gate 			goto err;
1750Sstevel@tonic-gate 		if (BIO_puts(bp, "\n") <= 0)
1760Sstevel@tonic-gate 			goto err;
1770Sstevel@tonic-gate 		}
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_ISSUER))
1800Sstevel@tonic-gate 		{
1810Sstevel@tonic-gate 		if (BIO_printf(bp,"        Issuer:%c",mlch) <= 0) goto err;
1820Sstevel@tonic-gate 		if (X509_NAME_print_ex(bp,X509_get_issuer_name(x),nmindent, nmflags) < 0) goto err;
1830Sstevel@tonic-gate 		if (BIO_write(bp,"\n",1) <= 0) goto err;
1840Sstevel@tonic-gate 		}
1850Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_VALIDITY))
1860Sstevel@tonic-gate 		{
1870Sstevel@tonic-gate 		if (BIO_write(bp,"        Validity\n",17) <= 0) goto err;
1880Sstevel@tonic-gate 		if (BIO_write(bp,"            Not Before: ",24) <= 0) goto err;
1890Sstevel@tonic-gate 		if (!ASN1_TIME_print(bp,X509_get_notBefore(x))) goto err;
1900Sstevel@tonic-gate 		if (BIO_write(bp,"\n            Not After : ",25) <= 0) goto err;
1910Sstevel@tonic-gate 		if (!ASN1_TIME_print(bp,X509_get_notAfter(x))) goto err;
1920Sstevel@tonic-gate 		if (BIO_write(bp,"\n",1) <= 0) goto err;
1930Sstevel@tonic-gate 		}
1940Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_SUBJECT))
1950Sstevel@tonic-gate 		{
1960Sstevel@tonic-gate 		if (BIO_printf(bp,"        Subject:%c",mlch) <= 0) goto err;
1970Sstevel@tonic-gate 		if (X509_NAME_print_ex(bp,X509_get_subject_name(x),nmindent, nmflags) < 0) goto err;
1980Sstevel@tonic-gate 		if (BIO_write(bp,"\n",1) <= 0) goto err;
1990Sstevel@tonic-gate 		}
2000Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_PUBKEY))
2010Sstevel@tonic-gate 		{
2020Sstevel@tonic-gate 		if (BIO_write(bp,"        Subject Public Key Info:\n",33) <= 0)
2030Sstevel@tonic-gate 			goto err;
2040Sstevel@tonic-gate 		if (BIO_printf(bp,"%12sPublic Key Algorithm: ","") <= 0)
2050Sstevel@tonic-gate 			goto err;
2060Sstevel@tonic-gate 		if (i2a_ASN1_OBJECT(bp, ci->key->algor->algorithm) <= 0)
2070Sstevel@tonic-gate 			goto err;
2080Sstevel@tonic-gate 		if (BIO_puts(bp, "\n") <= 0)
2090Sstevel@tonic-gate 			goto err;
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 		pkey=X509_get_pubkey(x);
2120Sstevel@tonic-gate 		if (pkey == NULL)
2130Sstevel@tonic-gate 			{
2140Sstevel@tonic-gate 			BIO_printf(bp,"%12sUnable to load Public Key\n","");
2150Sstevel@tonic-gate 			ERR_print_errors(bp);
2160Sstevel@tonic-gate 			}
2170Sstevel@tonic-gate 		else
2180Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
2190Sstevel@tonic-gate 		if (pkey->type == EVP_PKEY_RSA)
2200Sstevel@tonic-gate 			{
2210Sstevel@tonic-gate 			BIO_printf(bp,"%12sRSA Public Key: (%d bit)\n","",
2220Sstevel@tonic-gate 			BN_num_bits(pkey->pkey.rsa->n));
2230Sstevel@tonic-gate 			RSA_print(bp,pkey->pkey.rsa,16);
2240Sstevel@tonic-gate 			}
2250Sstevel@tonic-gate 		else
2260Sstevel@tonic-gate #endif
2270Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
2280Sstevel@tonic-gate 		if (pkey->type == EVP_PKEY_DSA)
2290Sstevel@tonic-gate 			{
2300Sstevel@tonic-gate 			BIO_printf(bp,"%12sDSA Public Key:\n","");
2310Sstevel@tonic-gate 			DSA_print(bp,pkey->pkey.dsa,16);
2320Sstevel@tonic-gate 			}
2330Sstevel@tonic-gate 		else
2340Sstevel@tonic-gate #endif
235*2139Sjp161948 #ifndef OPENSSL_NO_EC
236*2139Sjp161948 		if (pkey->type == EVP_PKEY_EC)
237*2139Sjp161948 			{
238*2139Sjp161948 			BIO_printf(bp, "%12sEC Public Key:\n","");
239*2139Sjp161948 			EC_KEY_print(bp, pkey->pkey.ec, 16);
240*2139Sjp161948 			}
241*2139Sjp161948 		else
242*2139Sjp161948 #endif
2430Sstevel@tonic-gate 			BIO_printf(bp,"%12sUnknown Public Key:\n","");
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 		EVP_PKEY_free(pkey);
2460Sstevel@tonic-gate 		}
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	if (!(cflag & X509_FLAG_NO_EXTENSIONS))
2490Sstevel@tonic-gate 		X509V3_extensions_print(bp, "X509v3 extensions",
2500Sstevel@tonic-gate 					ci->extensions, cflag, 8);
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_SIGDUMP))
2530Sstevel@tonic-gate 		{
2540Sstevel@tonic-gate 		if(X509_signature_print(bp, x->sig_alg, x->signature) <= 0) goto err;
2550Sstevel@tonic-gate 		}
2560Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_AUX))
2570Sstevel@tonic-gate 		{
2580Sstevel@tonic-gate 		if (!X509_CERT_AUX_print(bp, x->aux, 0)) goto err;
2590Sstevel@tonic-gate 		}
2600Sstevel@tonic-gate 	ret=1;
2610Sstevel@tonic-gate err:
2620Sstevel@tonic-gate 	if (str != NULL) ASN1_STRING_free(str);
2630Sstevel@tonic-gate 	if (m != NULL) OPENSSL_free(m);
2640Sstevel@tonic-gate 	return(ret);
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate 
X509_ocspid_print(BIO * bp,X509 * x)2670Sstevel@tonic-gate int X509_ocspid_print (BIO *bp, X509 *x)
2680Sstevel@tonic-gate 	{
2690Sstevel@tonic-gate 	unsigned char *der=NULL ;
2700Sstevel@tonic-gate 	unsigned char *dertmp;
2710Sstevel@tonic-gate 	int derlen;
2720Sstevel@tonic-gate 	int i;
2730Sstevel@tonic-gate 	unsigned char SHA1md[SHA_DIGEST_LENGTH];
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 	/* display the hash of the subject as it would appear
2760Sstevel@tonic-gate 	   in OCSP requests */
2770Sstevel@tonic-gate 	if (BIO_printf(bp,"        Subject OCSP hash: ") <= 0)
2780Sstevel@tonic-gate 		goto err;
2790Sstevel@tonic-gate 	derlen = i2d_X509_NAME(x->cert_info->subject, NULL);
2800Sstevel@tonic-gate 	if ((der = dertmp = (unsigned char *)OPENSSL_malloc (derlen)) == NULL)
2810Sstevel@tonic-gate 		goto err;
2820Sstevel@tonic-gate 	i2d_X509_NAME(x->cert_info->subject, &dertmp);
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	EVP_Digest(der, derlen, SHA1md, NULL, EVP_sha1(), NULL);
2850Sstevel@tonic-gate 	for (i=0; i < SHA_DIGEST_LENGTH; i++)
2860Sstevel@tonic-gate 		{
2870Sstevel@tonic-gate 		if (BIO_printf(bp,"%02X",SHA1md[i]) <= 0) goto err;
2880Sstevel@tonic-gate 		}
2890Sstevel@tonic-gate 	OPENSSL_free (der);
2900Sstevel@tonic-gate 	der=NULL;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	/* display the hash of the public key as it would appear
2930Sstevel@tonic-gate 	   in OCSP requests */
2940Sstevel@tonic-gate 	if (BIO_printf(bp,"\n        Public key OCSP hash: ") <= 0)
2950Sstevel@tonic-gate 		goto err;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	EVP_Digest(x->cert_info->key->public_key->data,
2980Sstevel@tonic-gate 		x->cert_info->key->public_key->length, SHA1md, NULL, EVP_sha1(), NULL);
2990Sstevel@tonic-gate 	for (i=0; i < SHA_DIGEST_LENGTH; i++)
3000Sstevel@tonic-gate 		{
3010Sstevel@tonic-gate 		if (BIO_printf(bp,"%02X",SHA1md[i]) <= 0)
3020Sstevel@tonic-gate 			goto err;
3030Sstevel@tonic-gate 		}
3040Sstevel@tonic-gate 	BIO_printf(bp,"\n");
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	return (1);
3070Sstevel@tonic-gate err:
3080Sstevel@tonic-gate 	if (der != NULL) OPENSSL_free(der);
3090Sstevel@tonic-gate 	return(0);
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 
X509_signature_print(BIO * bp,X509_ALGOR * sigalg,ASN1_STRING * sig)3120Sstevel@tonic-gate int X509_signature_print(BIO *bp, X509_ALGOR *sigalg, ASN1_STRING *sig)
3130Sstevel@tonic-gate {
3140Sstevel@tonic-gate 	unsigned char *s;
3150Sstevel@tonic-gate 	int i, n;
3160Sstevel@tonic-gate 	if (BIO_puts(bp,"    Signature Algorithm: ") <= 0) return 0;
3170Sstevel@tonic-gate 	if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0) return 0;
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	n=sig->length;
3200Sstevel@tonic-gate 	s=sig->data;
3210Sstevel@tonic-gate 	for (i=0; i<n; i++)
3220Sstevel@tonic-gate 		{
3230Sstevel@tonic-gate 		if ((i%18) == 0)
3240Sstevel@tonic-gate 			if (BIO_write(bp,"\n        ",9) <= 0) return 0;
3250Sstevel@tonic-gate 			if (BIO_printf(bp,"%02x%s",s[i],
3260Sstevel@tonic-gate 				((i+1) == n)?"":":") <= 0) return 0;
3270Sstevel@tonic-gate 		}
3280Sstevel@tonic-gate 	if (BIO_write(bp,"\n",1) != 1) return 0;
3290Sstevel@tonic-gate 	return 1;
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate 
ASN1_STRING_print(BIO * bp,ASN1_STRING * v)3320Sstevel@tonic-gate int ASN1_STRING_print(BIO *bp, ASN1_STRING *v)
3330Sstevel@tonic-gate 	{
3340Sstevel@tonic-gate 	int i,n;
3350Sstevel@tonic-gate 	char buf[80],*p;;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	if (v == NULL) return(0);
3380Sstevel@tonic-gate 	n=0;
3390Sstevel@tonic-gate 	p=(char *)v->data;
3400Sstevel@tonic-gate 	for (i=0; i<v->length; i++)
3410Sstevel@tonic-gate 		{
3420Sstevel@tonic-gate 		if ((p[i] > '~') || ((p[i] < ' ') &&
3430Sstevel@tonic-gate 			(p[i] != '\n') && (p[i] != '\r')))
3440Sstevel@tonic-gate 			buf[n]='.';
3450Sstevel@tonic-gate 		else
3460Sstevel@tonic-gate 			buf[n]=p[i];
3470Sstevel@tonic-gate 		n++;
3480Sstevel@tonic-gate 		if (n >= 80)
3490Sstevel@tonic-gate 			{
3500Sstevel@tonic-gate 			if (BIO_write(bp,buf,n) <= 0)
3510Sstevel@tonic-gate 				return(0);
3520Sstevel@tonic-gate 			n=0;
3530Sstevel@tonic-gate 			}
3540Sstevel@tonic-gate 		}
3550Sstevel@tonic-gate 	if (n > 0)
3560Sstevel@tonic-gate 		if (BIO_write(bp,buf,n) <= 0)
3570Sstevel@tonic-gate 			return(0);
3580Sstevel@tonic-gate 	return(1);
3590Sstevel@tonic-gate 	}
3600Sstevel@tonic-gate 
ASN1_TIME_print(BIO * bp,ASN1_TIME * tm)3610Sstevel@tonic-gate int ASN1_TIME_print(BIO *bp, ASN1_TIME *tm)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate 	if(tm->type == V_ASN1_UTCTIME) return ASN1_UTCTIME_print(bp, tm);
3640Sstevel@tonic-gate 	if(tm->type == V_ASN1_GENERALIZEDTIME)
3650Sstevel@tonic-gate 				return ASN1_GENERALIZEDTIME_print(bp, tm);
3660Sstevel@tonic-gate 	BIO_write(bp,"Bad time value",14);
3670Sstevel@tonic-gate 	return(0);
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate static const char *mon[12]=
3710Sstevel@tonic-gate     {
3720Sstevel@tonic-gate     "Jan","Feb","Mar","Apr","May","Jun",
3730Sstevel@tonic-gate     "Jul","Aug","Sep","Oct","Nov","Dec"
3740Sstevel@tonic-gate     };
3750Sstevel@tonic-gate 
ASN1_GENERALIZEDTIME_print(BIO * bp,ASN1_GENERALIZEDTIME * tm)3760Sstevel@tonic-gate int ASN1_GENERALIZEDTIME_print(BIO *bp, ASN1_GENERALIZEDTIME *tm)
3770Sstevel@tonic-gate 	{
3780Sstevel@tonic-gate 	char *v;
3790Sstevel@tonic-gate 	int gmt=0;
3800Sstevel@tonic-gate 	int i;
3810Sstevel@tonic-gate 	int y=0,M=0,d=0,h=0,m=0,s=0;
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	i=tm->length;
3840Sstevel@tonic-gate 	v=(char *)tm->data;
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	if (i < 12) goto err;
3870Sstevel@tonic-gate 	if (v[i-1] == 'Z') gmt=1;
3880Sstevel@tonic-gate 	for (i=0; i<12; i++)
3890Sstevel@tonic-gate 		if ((v[i] > '9') || (v[i] < '0')) goto err;
3900Sstevel@tonic-gate 	y= (v[0]-'0')*1000+(v[1]-'0')*100 + (v[2]-'0')*10+(v[3]-'0');
3910Sstevel@tonic-gate 	M= (v[4]-'0')*10+(v[5]-'0');
3920Sstevel@tonic-gate 	if ((M > 12) || (M < 1)) goto err;
3930Sstevel@tonic-gate 	d= (v[6]-'0')*10+(v[7]-'0');
3940Sstevel@tonic-gate 	h= (v[8]-'0')*10+(v[9]-'0');
3950Sstevel@tonic-gate 	m=  (v[10]-'0')*10+(v[11]-'0');
3960Sstevel@tonic-gate 	if (	(v[12] >= '0') && (v[12] <= '9') &&
3970Sstevel@tonic-gate 		(v[13] >= '0') && (v[13] <= '9'))
3980Sstevel@tonic-gate 		s=  (v[12]-'0')*10+(v[13]-'0');
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	if (BIO_printf(bp,"%s %2d %02d:%02d:%02d %d%s",
4010Sstevel@tonic-gate 		mon[M-1],d,h,m,s,y,(gmt)?" GMT":"") <= 0)
4020Sstevel@tonic-gate 		return(0);
4030Sstevel@tonic-gate 	else
4040Sstevel@tonic-gate 		return(1);
4050Sstevel@tonic-gate err:
4060Sstevel@tonic-gate 	BIO_write(bp,"Bad time value",14);
4070Sstevel@tonic-gate 	return(0);
4080Sstevel@tonic-gate 	}
4090Sstevel@tonic-gate 
ASN1_UTCTIME_print(BIO * bp,ASN1_UTCTIME * tm)4100Sstevel@tonic-gate int ASN1_UTCTIME_print(BIO *bp, ASN1_UTCTIME *tm)
4110Sstevel@tonic-gate 	{
4120Sstevel@tonic-gate 	char *v;
4130Sstevel@tonic-gate 	int gmt=0;
4140Sstevel@tonic-gate 	int i;
4150Sstevel@tonic-gate 	int y=0,M=0,d=0,h=0,m=0,s=0;
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	i=tm->length;
4180Sstevel@tonic-gate 	v=(char *)tm->data;
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	if (i < 10) goto err;
4210Sstevel@tonic-gate 	if (v[i-1] == 'Z') gmt=1;
4220Sstevel@tonic-gate 	for (i=0; i<10; i++)
4230Sstevel@tonic-gate 		if ((v[i] > '9') || (v[i] < '0')) goto err;
4240Sstevel@tonic-gate 	y= (v[0]-'0')*10+(v[1]-'0');
4250Sstevel@tonic-gate 	if (y < 50) y+=100;
4260Sstevel@tonic-gate 	M= (v[2]-'0')*10+(v[3]-'0');
4270Sstevel@tonic-gate 	if ((M > 12) || (M < 1)) goto err;
4280Sstevel@tonic-gate 	d= (v[4]-'0')*10+(v[5]-'0');
4290Sstevel@tonic-gate 	h= (v[6]-'0')*10+(v[7]-'0');
4300Sstevel@tonic-gate 	m=  (v[8]-'0')*10+(v[9]-'0');
4310Sstevel@tonic-gate 	if (	(v[10] >= '0') && (v[10] <= '9') &&
4320Sstevel@tonic-gate 		(v[11] >= '0') && (v[11] <= '9'))
4330Sstevel@tonic-gate 		s=  (v[10]-'0')*10+(v[11]-'0');
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	if (BIO_printf(bp,"%s %2d %02d:%02d:%02d %d%s",
4360Sstevel@tonic-gate 		mon[M-1],d,h,m,s,y+1900,(gmt)?" GMT":"") <= 0)
4370Sstevel@tonic-gate 		return(0);
4380Sstevel@tonic-gate 	else
4390Sstevel@tonic-gate 		return(1);
4400Sstevel@tonic-gate err:
4410Sstevel@tonic-gate 	BIO_write(bp,"Bad time value",14);
4420Sstevel@tonic-gate 	return(0);
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate 
X509_NAME_print(BIO * bp,X509_NAME * name,int obase)4450Sstevel@tonic-gate int X509_NAME_print(BIO *bp, X509_NAME *name, int obase)
4460Sstevel@tonic-gate 	{
4470Sstevel@tonic-gate 	char *s,*c,*b;
4480Sstevel@tonic-gate 	int ret=0,l,ll,i,first=1;
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	ll=80-2-obase;
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	b=s=X509_NAME_oneline(name,NULL,0);
4530Sstevel@tonic-gate 	if (!*s)
4540Sstevel@tonic-gate 		{
4550Sstevel@tonic-gate 		OPENSSL_free(b);
4560Sstevel@tonic-gate 		return 1;
4570Sstevel@tonic-gate 		}
4580Sstevel@tonic-gate 	s++; /* skip the first slash */
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	l=ll;
4610Sstevel@tonic-gate 	c=s;
4620Sstevel@tonic-gate 	for (;;)
4630Sstevel@tonic-gate 		{
4640Sstevel@tonic-gate #ifndef CHARSET_EBCDIC
4650Sstevel@tonic-gate 		if (	((*s == '/') &&
4660Sstevel@tonic-gate 				((s[1] >= 'A') && (s[1] <= 'Z') && (
4670Sstevel@tonic-gate 					(s[2] == '=') ||
4680Sstevel@tonic-gate 					((s[2] >= 'A') && (s[2] <= 'Z') &&
4690Sstevel@tonic-gate 					(s[3] == '='))
4700Sstevel@tonic-gate 				 ))) ||
4710Sstevel@tonic-gate 			(*s == '\0'))
4720Sstevel@tonic-gate #else
4730Sstevel@tonic-gate 		if (	((*s == '/') &&
4740Sstevel@tonic-gate 				(isupper(s[1]) && (
4750Sstevel@tonic-gate 					(s[2] == '=') ||
4760Sstevel@tonic-gate 					(isupper(s[2]) &&
4770Sstevel@tonic-gate 					(s[3] == '='))
4780Sstevel@tonic-gate 				 ))) ||
4790Sstevel@tonic-gate 			(*s == '\0'))
4800Sstevel@tonic-gate #endif
4810Sstevel@tonic-gate 			{
4820Sstevel@tonic-gate 			if ((l <= 0) && !first)
4830Sstevel@tonic-gate 				{
4840Sstevel@tonic-gate 				first=0;
4850Sstevel@tonic-gate 				if (BIO_write(bp,"\n",1) != 1) goto err;
4860Sstevel@tonic-gate 				for (i=0; i<obase; i++)
4870Sstevel@tonic-gate 					{
4880Sstevel@tonic-gate 					if (BIO_write(bp," ",1) != 1) goto err;
4890Sstevel@tonic-gate 					}
4900Sstevel@tonic-gate 				l=ll;
4910Sstevel@tonic-gate 				}
4920Sstevel@tonic-gate 			i=s-c;
4930Sstevel@tonic-gate 			if (BIO_write(bp,c,i) != i) goto err;
4940Sstevel@tonic-gate 			c+=i;
4950Sstevel@tonic-gate 			c++;
4960Sstevel@tonic-gate 			if (*s != '\0')
4970Sstevel@tonic-gate 				{
4980Sstevel@tonic-gate 				if (BIO_write(bp,", ",2) != 2) goto err;
4990Sstevel@tonic-gate 				}
5000Sstevel@tonic-gate 			l--;
5010Sstevel@tonic-gate 			}
5020Sstevel@tonic-gate 		if (*s == '\0') break;
5030Sstevel@tonic-gate 		s++;
5040Sstevel@tonic-gate 		l--;
5050Sstevel@tonic-gate 		}
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	ret=1;
5080Sstevel@tonic-gate 	if (0)
5090Sstevel@tonic-gate 		{
5100Sstevel@tonic-gate err:
5110Sstevel@tonic-gate 		X509err(X509_F_X509_NAME_PRINT,ERR_R_BUF_LIB);
5120Sstevel@tonic-gate 		}
5130Sstevel@tonic-gate 	OPENSSL_free(b);
5140Sstevel@tonic-gate 	return(ret);
5150Sstevel@tonic-gate 	}
5160Sstevel@tonic-gate 
517