xref: /onnv-gate/usr/src/common/openssl/crypto/asn1/t_x509.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* crypto/asn1/t_x509.c */
2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3*0Sstevel@tonic-gate  * All rights reserved.
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  * This package is an SSL implementation written
6*0Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
7*0Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
10*0Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
11*0Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
12*0Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13*0Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
14*0Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15*0Sstevel@tonic-gate  *
16*0Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
17*0Sstevel@tonic-gate  * the code are not to be removed.
18*0Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
19*0Sstevel@tonic-gate  * as the author of the parts of the library used.
20*0Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
21*0Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
22*0Sstevel@tonic-gate  *
23*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
24*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
25*0Sstevel@tonic-gate  * are met:
26*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
27*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
28*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
29*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
30*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
31*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
32*0Sstevel@tonic-gate  *    must display the following acknowledgement:
33*0Sstevel@tonic-gate  *    "This product includes cryptographic software written by
34*0Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
35*0Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
36*0Sstevel@tonic-gate  *    being used are not cryptographic related :-).
37*0Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
38*0Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
39*0Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42*0Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44*0Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45*0Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*0Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47*0Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*0Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*0Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*0Sstevel@tonic-gate  * SUCH DAMAGE.
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
54*0Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55*0Sstevel@tonic-gate  * copied and put under another distribution licence
56*0Sstevel@tonic-gate  * [including the GNU Public Licence.]
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #include <stdio.h>
60*0Sstevel@tonic-gate #include "cryptlib.h"
61*0Sstevel@tonic-gate #include <openssl/buffer.h>
62*0Sstevel@tonic-gate #include <openssl/bn.h>
63*0Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
64*0Sstevel@tonic-gate #include <openssl/rsa.h>
65*0Sstevel@tonic-gate #endif
66*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
67*0Sstevel@tonic-gate #include <openssl/dsa.h>
68*0Sstevel@tonic-gate #endif
69*0Sstevel@tonic-gate #include <openssl/objects.h>
70*0Sstevel@tonic-gate #include <openssl/x509.h>
71*0Sstevel@tonic-gate #include <openssl/x509v3.h>
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate #ifndef OPENSSL_NO_FP_API
74*0Sstevel@tonic-gate int X509_print_fp(FILE *fp, X509 *x)
75*0Sstevel@tonic-gate 	{
76*0Sstevel@tonic-gate 	return X509_print_ex_fp(fp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
77*0Sstevel@tonic-gate 	}
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate int X509_print_ex_fp(FILE *fp, X509 *x, unsigned long nmflag, unsigned long cflag)
80*0Sstevel@tonic-gate         {
81*0Sstevel@tonic-gate         BIO *b;
82*0Sstevel@tonic-gate         int ret;
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate         if ((b=BIO_new(BIO_s_file())) == NULL)
85*0Sstevel@tonic-gate 		{
86*0Sstevel@tonic-gate 		X509err(X509_F_X509_PRINT_FP,ERR_R_BUF_LIB);
87*0Sstevel@tonic-gate                 return(0);
88*0Sstevel@tonic-gate 		}
89*0Sstevel@tonic-gate         BIO_set_fp(b,fp,BIO_NOCLOSE);
90*0Sstevel@tonic-gate         ret=X509_print_ex(b, x, nmflag, cflag);
91*0Sstevel@tonic-gate         BIO_free(b);
92*0Sstevel@tonic-gate         return(ret);
93*0Sstevel@tonic-gate         }
94*0Sstevel@tonic-gate #endif
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate int X509_print(BIO *bp, X509 *x)
97*0Sstevel@tonic-gate {
98*0Sstevel@tonic-gate 	return X509_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
99*0Sstevel@tonic-gate }
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, unsigned long cflag)
102*0Sstevel@tonic-gate 	{
103*0Sstevel@tonic-gate 	long l;
104*0Sstevel@tonic-gate 	int ret=0,i;
105*0Sstevel@tonic-gate 	char *m=NULL,mlch = ' ';
106*0Sstevel@tonic-gate 	int nmindent = 0;
107*0Sstevel@tonic-gate 	X509_CINF *ci;
108*0Sstevel@tonic-gate 	ASN1_INTEGER *bs;
109*0Sstevel@tonic-gate 	EVP_PKEY *pkey=NULL;
110*0Sstevel@tonic-gate 	const char *neg;
111*0Sstevel@tonic-gate 	ASN1_STRING *str=NULL;
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	if((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
114*0Sstevel@tonic-gate 			mlch = '\n';
115*0Sstevel@tonic-gate 			nmindent = 12;
116*0Sstevel@tonic-gate 	}
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate 	if(nmflags == X509_FLAG_COMPAT)
119*0Sstevel@tonic-gate 		nmindent = 16;
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	ci=x->cert_info;
122*0Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_HEADER))
123*0Sstevel@tonic-gate 		{
124*0Sstevel@tonic-gate 		if (BIO_write(bp,"Certificate:\n",13) <= 0) goto err;
125*0Sstevel@tonic-gate 		if (BIO_write(bp,"    Data:\n",10) <= 0) goto err;
126*0Sstevel@tonic-gate 		}
127*0Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_VERSION))
128*0Sstevel@tonic-gate 		{
129*0Sstevel@tonic-gate 		l=X509_get_version(x);
130*0Sstevel@tonic-gate 		if (BIO_printf(bp,"%8sVersion: %lu (0x%lx)\n","",l+1,l) <= 0) goto err;
131*0Sstevel@tonic-gate 		}
132*0Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_SERIAL))
133*0Sstevel@tonic-gate 		{
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 		if (BIO_write(bp,"        Serial Number:",22) <= 0) goto err;
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 		bs=X509_get_serialNumber(x);
138*0Sstevel@tonic-gate 		if (bs->length <= 4)
139*0Sstevel@tonic-gate 			{
140*0Sstevel@tonic-gate 			l=ASN1_INTEGER_get(bs);
141*0Sstevel@tonic-gate 			if (l < 0)
142*0Sstevel@tonic-gate 				{
143*0Sstevel@tonic-gate 				l= -l;
144*0Sstevel@tonic-gate 				neg="-";
145*0Sstevel@tonic-gate 				}
146*0Sstevel@tonic-gate 			else
147*0Sstevel@tonic-gate 				neg="";
148*0Sstevel@tonic-gate 			if (BIO_printf(bp," %s%lu (%s0x%lx)\n",neg,l,neg,l) <= 0)
149*0Sstevel@tonic-gate 				goto err;
150*0Sstevel@tonic-gate 			}
151*0Sstevel@tonic-gate 		else
152*0Sstevel@tonic-gate 			{
153*0Sstevel@tonic-gate 			neg=(bs->type == V_ASN1_NEG_INTEGER)?" (Negative)":"";
154*0Sstevel@tonic-gate 			if (BIO_printf(bp,"\n%12s%s","",neg) <= 0) goto err;
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 			for (i=0; i<bs->length; i++)
157*0Sstevel@tonic-gate 				{
158*0Sstevel@tonic-gate 				if (BIO_printf(bp,"%02x%c",bs->data[i],
159*0Sstevel@tonic-gate 					((i+1 == bs->length)?'\n':':')) <= 0)
160*0Sstevel@tonic-gate 					goto err;
161*0Sstevel@tonic-gate 				}
162*0Sstevel@tonic-gate 			}
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 		}
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_SIGNAME))
167*0Sstevel@tonic-gate 		{
168*0Sstevel@tonic-gate 		if (BIO_printf(bp,"%8sSignature Algorithm: ","") <= 0)
169*0Sstevel@tonic-gate 			goto err;
170*0Sstevel@tonic-gate 		if (i2a_ASN1_OBJECT(bp, ci->signature->algorithm) <= 0)
171*0Sstevel@tonic-gate 			goto err;
172*0Sstevel@tonic-gate 		if (BIO_puts(bp, "\n") <= 0)
173*0Sstevel@tonic-gate 			goto err;
174*0Sstevel@tonic-gate 		}
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_ISSUER))
177*0Sstevel@tonic-gate 		{
178*0Sstevel@tonic-gate 		if (BIO_printf(bp,"        Issuer:%c",mlch) <= 0) goto err;
179*0Sstevel@tonic-gate 		if (X509_NAME_print_ex(bp,X509_get_issuer_name(x),nmindent, nmflags) < 0) goto err;
180*0Sstevel@tonic-gate 		if (BIO_write(bp,"\n",1) <= 0) goto err;
181*0Sstevel@tonic-gate 		}
182*0Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_VALIDITY))
183*0Sstevel@tonic-gate 		{
184*0Sstevel@tonic-gate 		if (BIO_write(bp,"        Validity\n",17) <= 0) goto err;
185*0Sstevel@tonic-gate 		if (BIO_write(bp,"            Not Before: ",24) <= 0) goto err;
186*0Sstevel@tonic-gate 		if (!ASN1_TIME_print(bp,X509_get_notBefore(x))) goto err;
187*0Sstevel@tonic-gate 		if (BIO_write(bp,"\n            Not After : ",25) <= 0) goto err;
188*0Sstevel@tonic-gate 		if (!ASN1_TIME_print(bp,X509_get_notAfter(x))) goto err;
189*0Sstevel@tonic-gate 		if (BIO_write(bp,"\n",1) <= 0) goto err;
190*0Sstevel@tonic-gate 		}
191*0Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_SUBJECT))
192*0Sstevel@tonic-gate 		{
193*0Sstevel@tonic-gate 		if (BIO_printf(bp,"        Subject:%c",mlch) <= 0) goto err;
194*0Sstevel@tonic-gate 		if (X509_NAME_print_ex(bp,X509_get_subject_name(x),nmindent, nmflags) < 0) goto err;
195*0Sstevel@tonic-gate 		if (BIO_write(bp,"\n",1) <= 0) goto err;
196*0Sstevel@tonic-gate 		}
197*0Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_PUBKEY))
198*0Sstevel@tonic-gate 		{
199*0Sstevel@tonic-gate 		if (BIO_write(bp,"        Subject Public Key Info:\n",33) <= 0)
200*0Sstevel@tonic-gate 			goto err;
201*0Sstevel@tonic-gate 		if (BIO_printf(bp,"%12sPublic Key Algorithm: ","") <= 0)
202*0Sstevel@tonic-gate 			goto err;
203*0Sstevel@tonic-gate 		if (i2a_ASN1_OBJECT(bp, ci->key->algor->algorithm) <= 0)
204*0Sstevel@tonic-gate 			goto err;
205*0Sstevel@tonic-gate 		if (BIO_puts(bp, "\n") <= 0)
206*0Sstevel@tonic-gate 			goto err;
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 		pkey=X509_get_pubkey(x);
209*0Sstevel@tonic-gate 		if (pkey == NULL)
210*0Sstevel@tonic-gate 			{
211*0Sstevel@tonic-gate 			BIO_printf(bp,"%12sUnable to load Public Key\n","");
212*0Sstevel@tonic-gate 			ERR_print_errors(bp);
213*0Sstevel@tonic-gate 			}
214*0Sstevel@tonic-gate 		else
215*0Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
216*0Sstevel@tonic-gate 		if (pkey->type == EVP_PKEY_RSA)
217*0Sstevel@tonic-gate 			{
218*0Sstevel@tonic-gate 			BIO_printf(bp,"%12sRSA Public Key: (%d bit)\n","",
219*0Sstevel@tonic-gate 			BN_num_bits(pkey->pkey.rsa->n));
220*0Sstevel@tonic-gate 			RSA_print(bp,pkey->pkey.rsa,16);
221*0Sstevel@tonic-gate 			}
222*0Sstevel@tonic-gate 		else
223*0Sstevel@tonic-gate #endif
224*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
225*0Sstevel@tonic-gate 		if (pkey->type == EVP_PKEY_DSA)
226*0Sstevel@tonic-gate 			{
227*0Sstevel@tonic-gate 			BIO_printf(bp,"%12sDSA Public Key:\n","");
228*0Sstevel@tonic-gate 			DSA_print(bp,pkey->pkey.dsa,16);
229*0Sstevel@tonic-gate 			}
230*0Sstevel@tonic-gate 		else
231*0Sstevel@tonic-gate #endif
232*0Sstevel@tonic-gate 			BIO_printf(bp,"%12sUnknown Public Key:\n","");
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 		EVP_PKEY_free(pkey);
235*0Sstevel@tonic-gate 		}
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 	if (!(cflag & X509_FLAG_NO_EXTENSIONS))
238*0Sstevel@tonic-gate 		X509V3_extensions_print(bp, "X509v3 extensions",
239*0Sstevel@tonic-gate 					ci->extensions, cflag, 8);
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_SIGDUMP))
242*0Sstevel@tonic-gate 		{
243*0Sstevel@tonic-gate 		if(X509_signature_print(bp, x->sig_alg, x->signature) <= 0) goto err;
244*0Sstevel@tonic-gate 		}
245*0Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_AUX))
246*0Sstevel@tonic-gate 		{
247*0Sstevel@tonic-gate 		if (!X509_CERT_AUX_print(bp, x->aux, 0)) goto err;
248*0Sstevel@tonic-gate 		}
249*0Sstevel@tonic-gate 	ret=1;
250*0Sstevel@tonic-gate err:
251*0Sstevel@tonic-gate 	if (str != NULL) ASN1_STRING_free(str);
252*0Sstevel@tonic-gate 	if (m != NULL) OPENSSL_free(m);
253*0Sstevel@tonic-gate 	return(ret);
254*0Sstevel@tonic-gate 	}
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate int X509_ocspid_print (BIO *bp, X509 *x)
257*0Sstevel@tonic-gate 	{
258*0Sstevel@tonic-gate 	unsigned char *der=NULL ;
259*0Sstevel@tonic-gate 	unsigned char *dertmp;
260*0Sstevel@tonic-gate 	int derlen;
261*0Sstevel@tonic-gate 	int i;
262*0Sstevel@tonic-gate 	unsigned char SHA1md[SHA_DIGEST_LENGTH];
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 	/* display the hash of the subject as it would appear
265*0Sstevel@tonic-gate 	   in OCSP requests */
266*0Sstevel@tonic-gate 	if (BIO_printf(bp,"        Subject OCSP hash: ") <= 0)
267*0Sstevel@tonic-gate 		goto err;
268*0Sstevel@tonic-gate 	derlen = i2d_X509_NAME(x->cert_info->subject, NULL);
269*0Sstevel@tonic-gate 	if ((der = dertmp = (unsigned char *)OPENSSL_malloc (derlen)) == NULL)
270*0Sstevel@tonic-gate 		goto err;
271*0Sstevel@tonic-gate 	i2d_X509_NAME(x->cert_info->subject, &dertmp);
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate 	EVP_Digest(der, derlen, SHA1md, NULL, EVP_sha1(), NULL);
274*0Sstevel@tonic-gate 	for (i=0; i < SHA_DIGEST_LENGTH; i++)
275*0Sstevel@tonic-gate 		{
276*0Sstevel@tonic-gate 		if (BIO_printf(bp,"%02X",SHA1md[i]) <= 0) goto err;
277*0Sstevel@tonic-gate 		}
278*0Sstevel@tonic-gate 	OPENSSL_free (der);
279*0Sstevel@tonic-gate 	der=NULL;
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 	/* display the hash of the public key as it would appear
282*0Sstevel@tonic-gate 	   in OCSP requests */
283*0Sstevel@tonic-gate 	if (BIO_printf(bp,"\n        Public key OCSP hash: ") <= 0)
284*0Sstevel@tonic-gate 		goto err;
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 	EVP_Digest(x->cert_info->key->public_key->data,
287*0Sstevel@tonic-gate 		x->cert_info->key->public_key->length, SHA1md, NULL, EVP_sha1(), NULL);
288*0Sstevel@tonic-gate 	for (i=0; i < SHA_DIGEST_LENGTH; i++)
289*0Sstevel@tonic-gate 		{
290*0Sstevel@tonic-gate 		if (BIO_printf(bp,"%02X",SHA1md[i]) <= 0)
291*0Sstevel@tonic-gate 			goto err;
292*0Sstevel@tonic-gate 		}
293*0Sstevel@tonic-gate 	BIO_printf(bp,"\n");
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate 	return (1);
296*0Sstevel@tonic-gate err:
297*0Sstevel@tonic-gate 	if (der != NULL) OPENSSL_free(der);
298*0Sstevel@tonic-gate 	return(0);
299*0Sstevel@tonic-gate 	}
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate int X509_signature_print(BIO *bp, X509_ALGOR *sigalg, ASN1_STRING *sig)
302*0Sstevel@tonic-gate {
303*0Sstevel@tonic-gate 	unsigned char *s;
304*0Sstevel@tonic-gate 	int i, n;
305*0Sstevel@tonic-gate 	if (BIO_puts(bp,"    Signature Algorithm: ") <= 0) return 0;
306*0Sstevel@tonic-gate 	if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0) return 0;
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 	n=sig->length;
309*0Sstevel@tonic-gate 	s=sig->data;
310*0Sstevel@tonic-gate 	for (i=0; i<n; i++)
311*0Sstevel@tonic-gate 		{
312*0Sstevel@tonic-gate 		if ((i%18) == 0)
313*0Sstevel@tonic-gate 			if (BIO_write(bp,"\n        ",9) <= 0) return 0;
314*0Sstevel@tonic-gate 			if (BIO_printf(bp,"%02x%s",s[i],
315*0Sstevel@tonic-gate 				((i+1) == n)?"":":") <= 0) return 0;
316*0Sstevel@tonic-gate 		}
317*0Sstevel@tonic-gate 	if (BIO_write(bp,"\n",1) != 1) return 0;
318*0Sstevel@tonic-gate 	return 1;
319*0Sstevel@tonic-gate }
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate int ASN1_STRING_print(BIO *bp, ASN1_STRING *v)
322*0Sstevel@tonic-gate 	{
323*0Sstevel@tonic-gate 	int i,n;
324*0Sstevel@tonic-gate 	char buf[80],*p;;
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate 	if (v == NULL) return(0);
327*0Sstevel@tonic-gate 	n=0;
328*0Sstevel@tonic-gate 	p=(char *)v->data;
329*0Sstevel@tonic-gate 	for (i=0; i<v->length; i++)
330*0Sstevel@tonic-gate 		{
331*0Sstevel@tonic-gate 		if ((p[i] > '~') || ((p[i] < ' ') &&
332*0Sstevel@tonic-gate 			(p[i] != '\n') && (p[i] != '\r')))
333*0Sstevel@tonic-gate 			buf[n]='.';
334*0Sstevel@tonic-gate 		else
335*0Sstevel@tonic-gate 			buf[n]=p[i];
336*0Sstevel@tonic-gate 		n++;
337*0Sstevel@tonic-gate 		if (n >= 80)
338*0Sstevel@tonic-gate 			{
339*0Sstevel@tonic-gate 			if (BIO_write(bp,buf,n) <= 0)
340*0Sstevel@tonic-gate 				return(0);
341*0Sstevel@tonic-gate 			n=0;
342*0Sstevel@tonic-gate 			}
343*0Sstevel@tonic-gate 		}
344*0Sstevel@tonic-gate 	if (n > 0)
345*0Sstevel@tonic-gate 		if (BIO_write(bp,buf,n) <= 0)
346*0Sstevel@tonic-gate 			return(0);
347*0Sstevel@tonic-gate 	return(1);
348*0Sstevel@tonic-gate 	}
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate int ASN1_TIME_print(BIO *bp, ASN1_TIME *tm)
351*0Sstevel@tonic-gate {
352*0Sstevel@tonic-gate 	if(tm->type == V_ASN1_UTCTIME) return ASN1_UTCTIME_print(bp, tm);
353*0Sstevel@tonic-gate 	if(tm->type == V_ASN1_GENERALIZEDTIME)
354*0Sstevel@tonic-gate 				return ASN1_GENERALIZEDTIME_print(bp, tm);
355*0Sstevel@tonic-gate 	BIO_write(bp,"Bad time value",14);
356*0Sstevel@tonic-gate 	return(0);
357*0Sstevel@tonic-gate }
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate static const char *mon[12]=
360*0Sstevel@tonic-gate     {
361*0Sstevel@tonic-gate     "Jan","Feb","Mar","Apr","May","Jun",
362*0Sstevel@tonic-gate     "Jul","Aug","Sep","Oct","Nov","Dec"
363*0Sstevel@tonic-gate     };
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate int ASN1_GENERALIZEDTIME_print(BIO *bp, ASN1_GENERALIZEDTIME *tm)
366*0Sstevel@tonic-gate 	{
367*0Sstevel@tonic-gate 	char *v;
368*0Sstevel@tonic-gate 	int gmt=0;
369*0Sstevel@tonic-gate 	int i;
370*0Sstevel@tonic-gate 	int y=0,M=0,d=0,h=0,m=0,s=0;
371*0Sstevel@tonic-gate 
372*0Sstevel@tonic-gate 	i=tm->length;
373*0Sstevel@tonic-gate 	v=(char *)tm->data;
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 	if (i < 12) goto err;
376*0Sstevel@tonic-gate 	if (v[i-1] == 'Z') gmt=1;
377*0Sstevel@tonic-gate 	for (i=0; i<12; i++)
378*0Sstevel@tonic-gate 		if ((v[i] > '9') || (v[i] < '0')) goto err;
379*0Sstevel@tonic-gate 	y= (v[0]-'0')*1000+(v[1]-'0')*100 + (v[2]-'0')*10+(v[3]-'0');
380*0Sstevel@tonic-gate 	M= (v[4]-'0')*10+(v[5]-'0');
381*0Sstevel@tonic-gate 	if ((M > 12) || (M < 1)) goto err;
382*0Sstevel@tonic-gate 	d= (v[6]-'0')*10+(v[7]-'0');
383*0Sstevel@tonic-gate 	h= (v[8]-'0')*10+(v[9]-'0');
384*0Sstevel@tonic-gate 	m=  (v[10]-'0')*10+(v[11]-'0');
385*0Sstevel@tonic-gate 	if (	(v[12] >= '0') && (v[12] <= '9') &&
386*0Sstevel@tonic-gate 		(v[13] >= '0') && (v[13] <= '9'))
387*0Sstevel@tonic-gate 		s=  (v[12]-'0')*10+(v[13]-'0');
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate 	if (BIO_printf(bp,"%s %2d %02d:%02d:%02d %d%s",
390*0Sstevel@tonic-gate 		mon[M-1],d,h,m,s,y,(gmt)?" GMT":"") <= 0)
391*0Sstevel@tonic-gate 		return(0);
392*0Sstevel@tonic-gate 	else
393*0Sstevel@tonic-gate 		return(1);
394*0Sstevel@tonic-gate err:
395*0Sstevel@tonic-gate 	BIO_write(bp,"Bad time value",14);
396*0Sstevel@tonic-gate 	return(0);
397*0Sstevel@tonic-gate 	}
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate int ASN1_UTCTIME_print(BIO *bp, ASN1_UTCTIME *tm)
400*0Sstevel@tonic-gate 	{
401*0Sstevel@tonic-gate 	char *v;
402*0Sstevel@tonic-gate 	int gmt=0;
403*0Sstevel@tonic-gate 	int i;
404*0Sstevel@tonic-gate 	int y=0,M=0,d=0,h=0,m=0,s=0;
405*0Sstevel@tonic-gate 
406*0Sstevel@tonic-gate 	i=tm->length;
407*0Sstevel@tonic-gate 	v=(char *)tm->data;
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 	if (i < 10) goto err;
410*0Sstevel@tonic-gate 	if (v[i-1] == 'Z') gmt=1;
411*0Sstevel@tonic-gate 	for (i=0; i<10; i++)
412*0Sstevel@tonic-gate 		if ((v[i] > '9') || (v[i] < '0')) goto err;
413*0Sstevel@tonic-gate 	y= (v[0]-'0')*10+(v[1]-'0');
414*0Sstevel@tonic-gate 	if (y < 50) y+=100;
415*0Sstevel@tonic-gate 	M= (v[2]-'0')*10+(v[3]-'0');
416*0Sstevel@tonic-gate 	if ((M > 12) || (M < 1)) goto err;
417*0Sstevel@tonic-gate 	d= (v[4]-'0')*10+(v[5]-'0');
418*0Sstevel@tonic-gate 	h= (v[6]-'0')*10+(v[7]-'0');
419*0Sstevel@tonic-gate 	m=  (v[8]-'0')*10+(v[9]-'0');
420*0Sstevel@tonic-gate 	if (	(v[10] >= '0') && (v[10] <= '9') &&
421*0Sstevel@tonic-gate 		(v[11] >= '0') && (v[11] <= '9'))
422*0Sstevel@tonic-gate 		s=  (v[10]-'0')*10+(v[11]-'0');
423*0Sstevel@tonic-gate 
424*0Sstevel@tonic-gate 	if (BIO_printf(bp,"%s %2d %02d:%02d:%02d %d%s",
425*0Sstevel@tonic-gate 		mon[M-1],d,h,m,s,y+1900,(gmt)?" GMT":"") <= 0)
426*0Sstevel@tonic-gate 		return(0);
427*0Sstevel@tonic-gate 	else
428*0Sstevel@tonic-gate 		return(1);
429*0Sstevel@tonic-gate err:
430*0Sstevel@tonic-gate 	BIO_write(bp,"Bad time value",14);
431*0Sstevel@tonic-gate 	return(0);
432*0Sstevel@tonic-gate 	}
433*0Sstevel@tonic-gate 
434*0Sstevel@tonic-gate int X509_NAME_print(BIO *bp, X509_NAME *name, int obase)
435*0Sstevel@tonic-gate 	{
436*0Sstevel@tonic-gate 	char *s,*c,*b;
437*0Sstevel@tonic-gate 	int ret=0,l,ll,i,first=1;
438*0Sstevel@tonic-gate 
439*0Sstevel@tonic-gate 	ll=80-2-obase;
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate 	b=s=X509_NAME_oneline(name,NULL,0);
442*0Sstevel@tonic-gate 	if (!*s)
443*0Sstevel@tonic-gate 		{
444*0Sstevel@tonic-gate 		OPENSSL_free(b);
445*0Sstevel@tonic-gate 		return 1;
446*0Sstevel@tonic-gate 		}
447*0Sstevel@tonic-gate 	s++; /* skip the first slash */
448*0Sstevel@tonic-gate 
449*0Sstevel@tonic-gate 	l=ll;
450*0Sstevel@tonic-gate 	c=s;
451*0Sstevel@tonic-gate 	for (;;)
452*0Sstevel@tonic-gate 		{
453*0Sstevel@tonic-gate #ifndef CHARSET_EBCDIC
454*0Sstevel@tonic-gate 		if (	((*s == '/') &&
455*0Sstevel@tonic-gate 				((s[1] >= 'A') && (s[1] <= 'Z') && (
456*0Sstevel@tonic-gate 					(s[2] == '=') ||
457*0Sstevel@tonic-gate 					((s[2] >= 'A') && (s[2] <= 'Z') &&
458*0Sstevel@tonic-gate 					(s[3] == '='))
459*0Sstevel@tonic-gate 				 ))) ||
460*0Sstevel@tonic-gate 			(*s == '\0'))
461*0Sstevel@tonic-gate #else
462*0Sstevel@tonic-gate 		if (	((*s == '/') &&
463*0Sstevel@tonic-gate 				(isupper(s[1]) && (
464*0Sstevel@tonic-gate 					(s[2] == '=') ||
465*0Sstevel@tonic-gate 					(isupper(s[2]) &&
466*0Sstevel@tonic-gate 					(s[3] == '='))
467*0Sstevel@tonic-gate 				 ))) ||
468*0Sstevel@tonic-gate 			(*s == '\0'))
469*0Sstevel@tonic-gate #endif
470*0Sstevel@tonic-gate 			{
471*0Sstevel@tonic-gate 			if ((l <= 0) && !first)
472*0Sstevel@tonic-gate 				{
473*0Sstevel@tonic-gate 				first=0;
474*0Sstevel@tonic-gate 				if (BIO_write(bp,"\n",1) != 1) goto err;
475*0Sstevel@tonic-gate 				for (i=0; i<obase; i++)
476*0Sstevel@tonic-gate 					{
477*0Sstevel@tonic-gate 					if (BIO_write(bp," ",1) != 1) goto err;
478*0Sstevel@tonic-gate 					}
479*0Sstevel@tonic-gate 				l=ll;
480*0Sstevel@tonic-gate 				}
481*0Sstevel@tonic-gate 			i=s-c;
482*0Sstevel@tonic-gate 			if (BIO_write(bp,c,i) != i) goto err;
483*0Sstevel@tonic-gate 			c+=i;
484*0Sstevel@tonic-gate 			c++;
485*0Sstevel@tonic-gate 			if (*s != '\0')
486*0Sstevel@tonic-gate 				{
487*0Sstevel@tonic-gate 				if (BIO_write(bp,", ",2) != 2) goto err;
488*0Sstevel@tonic-gate 				}
489*0Sstevel@tonic-gate 			l--;
490*0Sstevel@tonic-gate 			}
491*0Sstevel@tonic-gate 		if (*s == '\0') break;
492*0Sstevel@tonic-gate 		s++;
493*0Sstevel@tonic-gate 		l--;
494*0Sstevel@tonic-gate 		}
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate 	ret=1;
497*0Sstevel@tonic-gate 	if (0)
498*0Sstevel@tonic-gate 		{
499*0Sstevel@tonic-gate err:
500*0Sstevel@tonic-gate 		X509err(X509_F_X509_NAME_PRINT,ERR_R_BUF_LIB);
501*0Sstevel@tonic-gate 		}
502*0Sstevel@tonic-gate 	OPENSSL_free(b);
503*0Sstevel@tonic-gate 	return(ret);
504*0Sstevel@tonic-gate 	}
505*0Sstevel@tonic-gate 
506