155714Skris/* crypto/asn1/t_req.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
855714Skris *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1555714Skris *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
2255714Skris *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4055714Skris *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
5255714Skris *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include "cryptlib.h"
6155714Skris#include <openssl/buffer.h>
6255714Skris#include <openssl/bn.h>
6355714Skris#include <openssl/objects.h>
6455714Skris#include <openssl/x509.h>
6559191Skris#include <openssl/x509v3.h>
66160814Ssimon#ifndef OPENSSL_NO_RSA
67160814Ssimon#include <openssl/rsa.h>
68160814Ssimon#endif
69160814Ssimon#ifndef OPENSSL_NO_DSA
70160814Ssimon#include <openssl/dsa.h>
71160814Ssimon#endif
7255714Skris
73109998Smarkm#ifndef OPENSSL_NO_FP_API
7455714Skrisint X509_REQ_print_fp(FILE *fp, X509_REQ *x)
7555714Skris        {
7655714Skris        BIO *b;
7755714Skris        int ret;
7855714Skris
7955714Skris        if ((b=BIO_new(BIO_s_file())) == NULL)
8055714Skris		{
8155714Skris		X509err(X509_F_X509_REQ_PRINT_FP,ERR_R_BUF_LIB);
8255714Skris                return(0);
8355714Skris		}
8455714Skris        BIO_set_fp(b,fp,BIO_NOCLOSE);
8555714Skris        ret=X509_REQ_print(b, x);
8655714Skris        BIO_free(b);
8755714Skris        return(ret);
8855714Skris        }
8955714Skris#endif
9055714Skris
91109998Smarkmint X509_REQ_print_ex(BIO *bp, X509_REQ *x, unsigned long nmflags, unsigned long cflag)
9255714Skris	{
9355714Skris	unsigned long l;
94109998Smarkm	int i;
9555714Skris	const char *neg;
9655714Skris	X509_REQ_INFO *ri;
9755714Skris	EVP_PKEY *pkey;
9855714Skris	STACK_OF(X509_ATTRIBUTE) *sk;
9959191Skris	STACK_OF(X509_EXTENSION) *exts;
100109998Smarkm	char mlch = ' ';
101109998Smarkm	int nmindent = 0;
10255714Skris
103109998Smarkm	if((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
104109998Smarkm		mlch = '\n';
105109998Smarkm		nmindent = 12;
106109998Smarkm	}
10755714Skris
108109998Smarkm	if(nmflags == X509_FLAG_COMPAT)
109109998Smarkm		nmindent = 16;
11055714Skris
11155714Skris
112109998Smarkm	ri=x->req_info;
113109998Smarkm	if(!(cflag & X509_FLAG_NO_HEADER))
11455714Skris		{
115109998Smarkm		if (BIO_write(bp,"Certificate Request:\n",21) <= 0) goto err;
116109998Smarkm		if (BIO_write(bp,"    Data:\n",10) <= 0) goto err;
11755714Skris		}
118109998Smarkm	if(!(cflag & X509_FLAG_NO_VERSION))
11955714Skris		{
120109998Smarkm		neg=(ri->version->type == V_ASN1_NEG_INTEGER)?"-":"";
121109998Smarkm		l=0;
122109998Smarkm		for (i=0; i<ri->version->length; i++)
123109998Smarkm			{ l<<=8; l+=ri->version->data[i]; }
124109998Smarkm		if(BIO_printf(bp,"%8sVersion: %s%lu (%s0x%lx)\n","",neg,l,neg,
125109998Smarkm			      l) <= 0)
126109998Smarkm		    goto err;
12755714Skris		}
128109998Smarkm        if(!(cflag & X509_FLAG_NO_SUBJECT))
129109998Smarkm                {
130109998Smarkm                if (BIO_printf(bp,"        Subject:%c",mlch) <= 0) goto err;
131109998Smarkm                if (X509_NAME_print_ex(bp,ri->subject,nmindent, nmflags) < 0) goto err;
132109998Smarkm                if (BIO_write(bp,"\n",1) <= 0) goto err;
133109998Smarkm                }
134109998Smarkm	if(!(cflag & X509_FLAG_NO_PUBKEY))
135109998Smarkm		{
136109998Smarkm		if (BIO_write(bp,"        Subject Public Key Info:\n",33) <= 0)
137109998Smarkm			goto err;
138109998Smarkm		if (BIO_printf(bp,"%12sPublic Key Algorithm: ","") <= 0)
139109998Smarkm			goto err;
140109998Smarkm		if (i2a_ASN1_OBJECT(bp, ri->pubkey->algor->algorithm) <= 0)
141109998Smarkm			goto err;
142109998Smarkm		if (BIO_puts(bp, "\n") <= 0)
143109998Smarkm			goto err;
144109998Smarkm
145109998Smarkm		pkey=X509_REQ_get_pubkey(x);
146109998Smarkm		if (pkey == NULL)
147109998Smarkm			{
148109998Smarkm			BIO_printf(bp,"%12sUnable to load Public Key\n","");
149109998Smarkm			ERR_print_errors(bp);
150109998Smarkm			}
151109998Smarkm		else
152109998Smarkm			{
153238405Sjkim			EVP_PKEY_print_public(bp, pkey, 16, NULL);
154238405Sjkim			EVP_PKEY_free(pkey);
155109998Smarkm			}
156160814Ssimon		}
15755714Skris
158109998Smarkm	if(!(cflag & X509_FLAG_NO_ATTRIBUTES))
159109998Smarkm		{
160109998Smarkm		/* may not be */
161109998Smarkm		if(BIO_printf(bp,"%8sAttributes:\n","") <= 0)
162109998Smarkm		    goto err;
16355714Skris
164109998Smarkm		sk=x->req_info->attributes;
165109998Smarkm		if (sk_X509_ATTRIBUTE_num(sk) == 0)
16655714Skris			{
167109998Smarkm			if(BIO_printf(bp,"%12sa0:00\n","") <= 0)
168109998Smarkm			    goto err;
16955714Skris			}
170109998Smarkm		else
17155714Skris			{
172109998Smarkm			for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++)
173109998Smarkm				{
174109998Smarkm				ASN1_TYPE *at;
175109998Smarkm				X509_ATTRIBUTE *a;
176109998Smarkm				ASN1_BIT_STRING *bs=NULL;
177109998Smarkm				ASN1_TYPE *t;
178109998Smarkm				int j,type=0,count=1,ii=0;
17955714Skris
180109998Smarkm				a=sk_X509_ATTRIBUTE_value(sk,i);
181109998Smarkm				if(X509_REQ_extension_nid(OBJ_obj2nid(a->object)))
182109998Smarkm									continue;
183109998Smarkm				if(BIO_printf(bp,"%12s","") <= 0)
184109998Smarkm				    goto err;
185109998Smarkm				if ((j=i2a_ASN1_OBJECT(bp,a->object)) > 0)
18655714Skris				{
187109998Smarkm				if (a->single)
188109998Smarkm					{
189109998Smarkm					t=a->value.single;
190109998Smarkm					type=t->type;
191109998Smarkm					bs=t->value.bit_string;
192109998Smarkm					}
193109998Smarkm				else
194109998Smarkm					{
195109998Smarkm					ii=0;
196109998Smarkm					count=sk_ASN1_TYPE_num(a->value.set);
19755714Skrisget_next:
198109998Smarkm					at=sk_ASN1_TYPE_value(a->value.set,ii);
199109998Smarkm					type=at->type;
200109998Smarkm					bs=at->value.asn1_string;
201109998Smarkm					}
20255714Skris				}
203109998Smarkm				for (j=25-j; j>0; j--)
204109998Smarkm					if (BIO_write(bp," ",1) != 1) goto err;
205109998Smarkm				if (BIO_puts(bp,":") <= 0) goto err;
206109998Smarkm				if (	(type == V_ASN1_PRINTABLESTRING) ||
207109998Smarkm					(type == V_ASN1_T61STRING) ||
208109998Smarkm					(type == V_ASN1_IA5STRING))
209109998Smarkm					{
210109998Smarkm					if (BIO_write(bp,(char *)bs->data,bs->length)
211109998Smarkm						!= bs->length)
212109998Smarkm						goto err;
213109998Smarkm					BIO_puts(bp,"\n");
214109998Smarkm					}
215109998Smarkm				else
216109998Smarkm					{
217109998Smarkm					BIO_puts(bp,"unable to print attribute\n");
218109998Smarkm					}
219109998Smarkm				if (++ii < count) goto get_next;
22055714Skris				}
22155714Skris			}
222109998Smarkm		}
223194206Ssimon	if(!(cflag & X509_FLAG_NO_EXTENSIONS))
224109998Smarkm		{
225109998Smarkm		exts = X509_REQ_get_extensions(x);
226109998Smarkm		if(exts)
227109998Smarkm			{
228109998Smarkm			BIO_printf(bp,"%8sRequested Extensions:\n","");
229109998Smarkm			for (i=0; i<sk_X509_EXTENSION_num(exts); i++)
23055714Skris				{
231109998Smarkm				ASN1_OBJECT *obj;
232109998Smarkm				X509_EXTENSION *ex;
233109998Smarkm				int j;
234109998Smarkm				ex=sk_X509_EXTENSION_value(exts, i);
235109998Smarkm				if (BIO_printf(bp,"%12s","") <= 0) goto err;
236109998Smarkm				obj=X509_EXTENSION_get_object(ex);
237109998Smarkm				i2a_ASN1_OBJECT(bp,obj);
238109998Smarkm				j=X509_EXTENSION_get_critical(ex);
239160814Ssimon				if (BIO_printf(bp,": %s\n",j?"critical":"") <= 0)
24055714Skris					goto err;
241194206Ssimon				if(!X509V3_EXT_print(bp, ex, cflag, 16))
242109998Smarkm					{
243109998Smarkm					BIO_printf(bp, "%16s", "");
244109998Smarkm					M_ASN1_OCTET_STRING_print(bp,ex->value);
245109998Smarkm					}
246109998Smarkm				if (BIO_write(bp,"\n",1) <= 0) goto err;
24755714Skris				}
248109998Smarkm			sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
24955714Skris			}
25055714Skris		}
25155714Skris
252109998Smarkm	if(!(cflag & X509_FLAG_NO_SIGDUMP))
253109998Smarkm		{
254109998Smarkm		if(!X509_signature_print(bp, x->sig_alg, x->signature)) goto err;
25559191Skris		}
25659191Skris
25755714Skris	return(1);
25855714Skriserr:
259160814Ssimon	X509err(X509_F_X509_REQ_PRINT_EX,ERR_R_BUF_LIB);
26055714Skris	return(0);
26155714Skris	}
262109998Smarkm
263109998Smarkmint X509_REQ_print(BIO *bp, X509_REQ *x)
264109998Smarkm	{
265109998Smarkm	return X509_REQ_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
266109998Smarkm	}
267