xref: /onnv-gate/usr/src/common/openssl/crypto/asn1/t_req.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/asn1/t_req.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 #include <openssl/objects.h>
640Sstevel@tonic-gate #include <openssl/x509.h>
650Sstevel@tonic-gate #include <openssl/x509v3.h>
66*2139Sjp161948 #ifndef OPENSSL_NO_RSA
67*2139Sjp161948 #include <openssl/rsa.h>
68*2139Sjp161948 #endif
69*2139Sjp161948 #ifndef OPENSSL_NO_DSA
70*2139Sjp161948 #include <openssl/dsa.h>
71*2139Sjp161948 #endif
720Sstevel@tonic-gate 
730Sstevel@tonic-gate #ifndef OPENSSL_NO_FP_API
X509_REQ_print_fp(FILE * fp,X509_REQ * x)740Sstevel@tonic-gate int X509_REQ_print_fp(FILE *fp, X509_REQ *x)
750Sstevel@tonic-gate         {
760Sstevel@tonic-gate         BIO *b;
770Sstevel@tonic-gate         int ret;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate         if ((b=BIO_new(BIO_s_file())) == NULL)
800Sstevel@tonic-gate 		{
810Sstevel@tonic-gate 		X509err(X509_F_X509_REQ_PRINT_FP,ERR_R_BUF_LIB);
820Sstevel@tonic-gate                 return(0);
830Sstevel@tonic-gate 		}
840Sstevel@tonic-gate         BIO_set_fp(b,fp,BIO_NOCLOSE);
850Sstevel@tonic-gate         ret=X509_REQ_print(b, x);
860Sstevel@tonic-gate         BIO_free(b);
870Sstevel@tonic-gate         return(ret);
880Sstevel@tonic-gate         }
890Sstevel@tonic-gate #endif
900Sstevel@tonic-gate 
X509_REQ_print_ex(BIO * bp,X509_REQ * x,unsigned long nmflags,unsigned long cflag)910Sstevel@tonic-gate int X509_REQ_print_ex(BIO *bp, X509_REQ *x, unsigned long nmflags, unsigned long cflag)
920Sstevel@tonic-gate 	{
930Sstevel@tonic-gate 	unsigned long l;
940Sstevel@tonic-gate 	int i;
950Sstevel@tonic-gate 	const char *neg;
960Sstevel@tonic-gate 	X509_REQ_INFO *ri;
970Sstevel@tonic-gate 	EVP_PKEY *pkey;
980Sstevel@tonic-gate 	STACK_OF(X509_ATTRIBUTE) *sk;
990Sstevel@tonic-gate 	STACK_OF(X509_EXTENSION) *exts;
1000Sstevel@tonic-gate 	char mlch = ' ';
1010Sstevel@tonic-gate 	int nmindent = 0;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	if((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
1040Sstevel@tonic-gate 		mlch = '\n';
1050Sstevel@tonic-gate 		nmindent = 12;
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	if(nmflags == X509_FLAG_COMPAT)
1090Sstevel@tonic-gate 		nmindent = 16;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	ri=x->req_info;
1130Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_HEADER))
1140Sstevel@tonic-gate 		{
1150Sstevel@tonic-gate 		if (BIO_write(bp,"Certificate Request:\n",21) <= 0) goto err;
1160Sstevel@tonic-gate 		if (BIO_write(bp,"    Data:\n",10) <= 0) goto err;
1170Sstevel@tonic-gate 		}
1180Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_VERSION))
1190Sstevel@tonic-gate 		{
1200Sstevel@tonic-gate 		neg=(ri->version->type == V_ASN1_NEG_INTEGER)?"-":"";
1210Sstevel@tonic-gate 		l=0;
1220Sstevel@tonic-gate 		for (i=0; i<ri->version->length; i++)
1230Sstevel@tonic-gate 			{ l<<=8; l+=ri->version->data[i]; }
1240Sstevel@tonic-gate 		if(BIO_printf(bp,"%8sVersion: %s%lu (%s0x%lx)\n","",neg,l,neg,
1250Sstevel@tonic-gate 			      l) <= 0)
1260Sstevel@tonic-gate 		    goto err;
1270Sstevel@tonic-gate 		}
1280Sstevel@tonic-gate         if(!(cflag & X509_FLAG_NO_SUBJECT))
1290Sstevel@tonic-gate                 {
1300Sstevel@tonic-gate                 if (BIO_printf(bp,"        Subject:%c",mlch) <= 0) goto err;
1310Sstevel@tonic-gate                 if (X509_NAME_print_ex(bp,ri->subject,nmindent, nmflags) < 0) goto err;
1320Sstevel@tonic-gate                 if (BIO_write(bp,"\n",1) <= 0) goto err;
1330Sstevel@tonic-gate                 }
1340Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_PUBKEY))
1350Sstevel@tonic-gate 		{
1360Sstevel@tonic-gate 		if (BIO_write(bp,"        Subject Public Key Info:\n",33) <= 0)
1370Sstevel@tonic-gate 			goto err;
1380Sstevel@tonic-gate 		if (BIO_printf(bp,"%12sPublic Key Algorithm: ","") <= 0)
1390Sstevel@tonic-gate 			goto err;
1400Sstevel@tonic-gate 		if (i2a_ASN1_OBJECT(bp, ri->pubkey->algor->algorithm) <= 0)
1410Sstevel@tonic-gate 			goto err;
1420Sstevel@tonic-gate 		if (BIO_puts(bp, "\n") <= 0)
1430Sstevel@tonic-gate 			goto err;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 		pkey=X509_REQ_get_pubkey(x);
1460Sstevel@tonic-gate 		if (pkey == NULL)
1470Sstevel@tonic-gate 			{
1480Sstevel@tonic-gate 			BIO_printf(bp,"%12sUnable to load Public Key\n","");
1490Sstevel@tonic-gate 			ERR_print_errors(bp);
1500Sstevel@tonic-gate 			}
1510Sstevel@tonic-gate 		else
1520Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
1530Sstevel@tonic-gate 		if (pkey->type == EVP_PKEY_RSA)
1540Sstevel@tonic-gate 			{
1550Sstevel@tonic-gate 			BIO_printf(bp,"%12sRSA Public Key: (%d bit)\n","",
1560Sstevel@tonic-gate 			BN_num_bits(pkey->pkey.rsa->n));
1570Sstevel@tonic-gate 			RSA_print(bp,pkey->pkey.rsa,16);
1580Sstevel@tonic-gate 			}
1590Sstevel@tonic-gate 		else
1600Sstevel@tonic-gate #endif
1610Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
1620Sstevel@tonic-gate 		if (pkey->type == EVP_PKEY_DSA)
1630Sstevel@tonic-gate 			{
1640Sstevel@tonic-gate 			BIO_printf(bp,"%12sDSA Public Key:\n","");
1650Sstevel@tonic-gate 			DSA_print(bp,pkey->pkey.dsa,16);
1660Sstevel@tonic-gate 			}
1670Sstevel@tonic-gate 		else
1680Sstevel@tonic-gate #endif
169*2139Sjp161948 #ifndef OPENSSL_NO_EC
170*2139Sjp161948 		if (pkey->type == EVP_PKEY_EC)
171*2139Sjp161948 		{
172*2139Sjp161948 			BIO_printf(bp, "%12sEC Public Key: \n","");
173*2139Sjp161948 			EC_KEY_print(bp, pkey->pkey.ec, 16);
174*2139Sjp161948 		}
175*2139Sjp161948 	else
176*2139Sjp161948 #endif
1770Sstevel@tonic-gate 			BIO_printf(bp,"%12sUnknown Public Key:\n","");
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 		EVP_PKEY_free(pkey);
1800Sstevel@tonic-gate 		}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_ATTRIBUTES))
1830Sstevel@tonic-gate 		{
1840Sstevel@tonic-gate 		/* may not be */
1850Sstevel@tonic-gate 		if(BIO_printf(bp,"%8sAttributes:\n","") <= 0)
1860Sstevel@tonic-gate 		    goto err;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 		sk=x->req_info->attributes;
1890Sstevel@tonic-gate 		if (sk_X509_ATTRIBUTE_num(sk) == 0)
1900Sstevel@tonic-gate 			{
1910Sstevel@tonic-gate 			if(BIO_printf(bp,"%12sa0:00\n","") <= 0)
1920Sstevel@tonic-gate 			    goto err;
1930Sstevel@tonic-gate 			}
1940Sstevel@tonic-gate 		else
1950Sstevel@tonic-gate 			{
1960Sstevel@tonic-gate 			for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++)
1970Sstevel@tonic-gate 				{
1980Sstevel@tonic-gate 				ASN1_TYPE *at;
1990Sstevel@tonic-gate 				X509_ATTRIBUTE *a;
2000Sstevel@tonic-gate 				ASN1_BIT_STRING *bs=NULL;
2010Sstevel@tonic-gate 				ASN1_TYPE *t;
2020Sstevel@tonic-gate 				int j,type=0,count=1,ii=0;
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 				a=sk_X509_ATTRIBUTE_value(sk,i);
2050Sstevel@tonic-gate 				if(X509_REQ_extension_nid(OBJ_obj2nid(a->object)))
2060Sstevel@tonic-gate 									continue;
2070Sstevel@tonic-gate 				if(BIO_printf(bp,"%12s","") <= 0)
2080Sstevel@tonic-gate 				    goto err;
2090Sstevel@tonic-gate 				if ((j=i2a_ASN1_OBJECT(bp,a->object)) > 0)
2100Sstevel@tonic-gate 				{
2110Sstevel@tonic-gate 				if (a->single)
2120Sstevel@tonic-gate 					{
2130Sstevel@tonic-gate 					t=a->value.single;
2140Sstevel@tonic-gate 					type=t->type;
2150Sstevel@tonic-gate 					bs=t->value.bit_string;
2160Sstevel@tonic-gate 					}
2170Sstevel@tonic-gate 				else
2180Sstevel@tonic-gate 					{
2190Sstevel@tonic-gate 					ii=0;
2200Sstevel@tonic-gate 					count=sk_ASN1_TYPE_num(a->value.set);
2210Sstevel@tonic-gate get_next:
2220Sstevel@tonic-gate 					at=sk_ASN1_TYPE_value(a->value.set,ii);
2230Sstevel@tonic-gate 					type=at->type;
2240Sstevel@tonic-gate 					bs=at->value.asn1_string;
2250Sstevel@tonic-gate 					}
2260Sstevel@tonic-gate 				}
2270Sstevel@tonic-gate 				for (j=25-j; j>0; j--)
2280Sstevel@tonic-gate 					if (BIO_write(bp," ",1) != 1) goto err;
2290Sstevel@tonic-gate 				if (BIO_puts(bp,":") <= 0) goto err;
2300Sstevel@tonic-gate 				if (	(type == V_ASN1_PRINTABLESTRING) ||
2310Sstevel@tonic-gate 					(type == V_ASN1_T61STRING) ||
2320Sstevel@tonic-gate 					(type == V_ASN1_IA5STRING))
2330Sstevel@tonic-gate 					{
2340Sstevel@tonic-gate 					if (BIO_write(bp,(char *)bs->data,bs->length)
2350Sstevel@tonic-gate 						!= bs->length)
2360Sstevel@tonic-gate 						goto err;
2370Sstevel@tonic-gate 					BIO_puts(bp,"\n");
2380Sstevel@tonic-gate 					}
2390Sstevel@tonic-gate 				else
2400Sstevel@tonic-gate 					{
2410Sstevel@tonic-gate 					BIO_puts(bp,"unable to print attribute\n");
2420Sstevel@tonic-gate 					}
2430Sstevel@tonic-gate 				if (++ii < count) goto get_next;
2440Sstevel@tonic-gate 				}
2450Sstevel@tonic-gate 			}
2460Sstevel@tonic-gate 		}
2470Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_ATTRIBUTES))
2480Sstevel@tonic-gate 		{
2490Sstevel@tonic-gate 		exts = X509_REQ_get_extensions(x);
2500Sstevel@tonic-gate 		if(exts)
2510Sstevel@tonic-gate 			{
2520Sstevel@tonic-gate 			BIO_printf(bp,"%8sRequested Extensions:\n","");
2530Sstevel@tonic-gate 			for (i=0; i<sk_X509_EXTENSION_num(exts); i++)
2540Sstevel@tonic-gate 				{
2550Sstevel@tonic-gate 				ASN1_OBJECT *obj;
2560Sstevel@tonic-gate 				X509_EXTENSION *ex;
2570Sstevel@tonic-gate 				int j;
2580Sstevel@tonic-gate 				ex=sk_X509_EXTENSION_value(exts, i);
2590Sstevel@tonic-gate 				if (BIO_printf(bp,"%12s","") <= 0) goto err;
2600Sstevel@tonic-gate 				obj=X509_EXTENSION_get_object(ex);
2610Sstevel@tonic-gate 				i2a_ASN1_OBJECT(bp,obj);
2620Sstevel@tonic-gate 				j=X509_EXTENSION_get_critical(ex);
263*2139Sjp161948 				if (BIO_printf(bp,": %s\n",j?"critical":"") <= 0)
2640Sstevel@tonic-gate 					goto err;
2650Sstevel@tonic-gate 				if(!X509V3_EXT_print(bp, ex, 0, 16))
2660Sstevel@tonic-gate 					{
2670Sstevel@tonic-gate 					BIO_printf(bp, "%16s", "");
2680Sstevel@tonic-gate 					M_ASN1_OCTET_STRING_print(bp,ex->value);
2690Sstevel@tonic-gate 					}
2700Sstevel@tonic-gate 				if (BIO_write(bp,"\n",1) <= 0) goto err;
2710Sstevel@tonic-gate 				}
2720Sstevel@tonic-gate 			sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
2730Sstevel@tonic-gate 			}
2740Sstevel@tonic-gate 		}
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	if(!(cflag & X509_FLAG_NO_SIGDUMP))
2770Sstevel@tonic-gate 		{
2780Sstevel@tonic-gate 		if(!X509_signature_print(bp, x->sig_alg, x->signature)) goto err;
2790Sstevel@tonic-gate 		}
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	return(1);
2820Sstevel@tonic-gate err:
283*2139Sjp161948 	X509err(X509_F_X509_REQ_PRINT_EX,ERR_R_BUF_LIB);
2840Sstevel@tonic-gate 	return(0);
2850Sstevel@tonic-gate 	}
2860Sstevel@tonic-gate 
X509_REQ_print(BIO * bp,X509_REQ * x)2870Sstevel@tonic-gate int X509_REQ_print(BIO *bp, X509_REQ *x)
2880Sstevel@tonic-gate 	{
2890Sstevel@tonic-gate 	return X509_REQ_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
2900Sstevel@tonic-gate 	}
291