xref: /freebsd/crypto/openssl/apps/req.c (revision ab8565e2671f6e9101b4b855b9614c95f0810eb6)
174664626SKris Kennaway /* apps/req.c */
274664626SKris Kennaway /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
374664626SKris Kennaway  * All rights reserved.
474664626SKris Kennaway  *
574664626SKris Kennaway  * This package is an SSL implementation written
674664626SKris Kennaway  * by Eric Young (eay@cryptsoft.com).
774664626SKris Kennaway  * The implementation was written so as to conform with Netscapes SSL.
874664626SKris Kennaway  *
974664626SKris Kennaway  * This library is free for commercial and non-commercial use as long as
1074664626SKris Kennaway  * the following conditions are aheared to.  The following conditions
1174664626SKris Kennaway  * apply to all code found in this distribution, be it the RC4, RSA,
1274664626SKris Kennaway  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1374664626SKris Kennaway  * included with this distribution is covered by the same copyright terms
1474664626SKris Kennaway  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1574664626SKris Kennaway  *
1674664626SKris Kennaway  * Copyright remains Eric Young's, and as such any Copyright notices in
1774664626SKris Kennaway  * the code are not to be removed.
1874664626SKris Kennaway  * If this package is used in a product, Eric Young should be given attribution
1974664626SKris Kennaway  * as the author of the parts of the library used.
2074664626SKris Kennaway  * This can be in the form of a textual message at program startup or
2174664626SKris Kennaway  * in documentation (online or textual) provided with the package.
2274664626SKris Kennaway  *
2374664626SKris Kennaway  * Redistribution and use in source and binary forms, with or without
2474664626SKris Kennaway  * modification, are permitted provided that the following conditions
2574664626SKris Kennaway  * are met:
2674664626SKris Kennaway  * 1. Redistributions of source code must retain the copyright
2774664626SKris Kennaway  *    notice, this list of conditions and the following disclaimer.
2874664626SKris Kennaway  * 2. Redistributions in binary form must reproduce the above copyright
2974664626SKris Kennaway  *    notice, this list of conditions and the following disclaimer in the
3074664626SKris Kennaway  *    documentation and/or other materials provided with the distribution.
3174664626SKris Kennaway  * 3. All advertising materials mentioning features or use of this software
3274664626SKris Kennaway  *    must display the following acknowledgement:
3374664626SKris Kennaway  *    "This product includes cryptographic software written by
3474664626SKris Kennaway  *     Eric Young (eay@cryptsoft.com)"
3574664626SKris Kennaway  *    The word 'cryptographic' can be left out if the rouines from the library
3674664626SKris Kennaway  *    being used are not cryptographic related :-).
3774664626SKris Kennaway  * 4. If you include any Windows specific code (or a derivative thereof) from
3874664626SKris Kennaway  *    the apps directory (application code) you must include an acknowledgement:
3974664626SKris Kennaway  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4074664626SKris Kennaway  *
4174664626SKris Kennaway  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4274664626SKris Kennaway  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4374664626SKris Kennaway  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4474664626SKris Kennaway  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4574664626SKris Kennaway  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4674664626SKris Kennaway  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4774664626SKris Kennaway  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4874664626SKris Kennaway  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4974664626SKris Kennaway  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5074664626SKris Kennaway  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5174664626SKris Kennaway  * SUCH DAMAGE.
5274664626SKris Kennaway  *
5374664626SKris Kennaway  * The licence and distribution terms for any publically available version or
5474664626SKris Kennaway  * derivative of this code cannot be changed.  i.e. this code cannot simply be
5574664626SKris Kennaway  * copied and put under another distribution licence
5674664626SKris Kennaway  * [including the GNU Public Licence.]
5774664626SKris Kennaway  */
5874664626SKris Kennaway 
593b4e3dcbSSimon L. B. Nielsen /* Until the key-gen callbacks are modified to use newer prototypes, we allow
603b4e3dcbSSimon L. B. Nielsen  * deprecated functions for openssl-internal code */
613b4e3dcbSSimon L. B. Nielsen #ifdef OPENSSL_NO_DEPRECATED
623b4e3dcbSSimon L. B. Nielsen #undef OPENSSL_NO_DEPRECATED
633b4e3dcbSSimon L. B. Nielsen #endif
643b4e3dcbSSimon L. B. Nielsen 
6574664626SKris Kennaway #include <stdio.h>
6674664626SKris Kennaway #include <stdlib.h>
6774664626SKris Kennaway #include <time.h>
6874664626SKris Kennaway #include <string.h>
695c87c606SMark Murray #ifdef OPENSSL_NO_STDIO
7074664626SKris Kennaway #define APPS_WIN16
7174664626SKris Kennaway #endif
7274664626SKris Kennaway #include "apps.h"
7374664626SKris Kennaway #include <openssl/bio.h>
7474664626SKris Kennaway #include <openssl/evp.h>
7574664626SKris Kennaway #include <openssl/conf.h>
7674664626SKris Kennaway #include <openssl/err.h>
7774664626SKris Kennaway #include <openssl/asn1.h>
7874664626SKris Kennaway #include <openssl/x509.h>
7974664626SKris Kennaway #include <openssl/x509v3.h>
8074664626SKris Kennaway #include <openssl/objects.h>
8174664626SKris Kennaway #include <openssl/pem.h>
823b4e3dcbSSimon L. B. Nielsen #include <openssl/bn.h>
833b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_RSA
843b4e3dcbSSimon L. B. Nielsen #include <openssl/rsa.h>
853b4e3dcbSSimon L. B. Nielsen #endif
863b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_DSA
873b4e3dcbSSimon L. B. Nielsen #include <openssl/dsa.h>
883b4e3dcbSSimon L. B. Nielsen #endif
8974664626SKris Kennaway 
9074664626SKris Kennaway #define SECTION		"req"
9174664626SKris Kennaway 
9274664626SKris Kennaway #define BITS		"default_bits"
9374664626SKris Kennaway #define KEYFILE		"default_keyfile"
94f579bf8eSKris Kennaway #define PROMPT		"prompt"
9574664626SKris Kennaway #define DISTINGUISHED_NAME	"distinguished_name"
9674664626SKris Kennaway #define ATTRIBUTES	"attributes"
9774664626SKris Kennaway #define V3_EXTENSIONS	"x509_extensions"
98f579bf8eSKris Kennaway #define REQ_EXTENSIONS	"req_extensions"
99f579bf8eSKris Kennaway #define STRING_MASK	"string_mask"
1005c87c606SMark Murray #define UTF8_IN		"utf8"
10174664626SKris Kennaway 
10274664626SKris Kennaway #define DEFAULT_KEY_LENGTH	512
10374664626SKris Kennaway #define MIN_KEY_LENGTH		384
10474664626SKris Kennaway 
10574664626SKris Kennaway #undef PROG
10674664626SKris Kennaway #define PROG	req_main
10774664626SKris Kennaway 
108f579bf8eSKris Kennaway /* -inform arg	- input format - default PEM (DER or PEM)
10974664626SKris Kennaway  * -outform arg - output format - default PEM
11074664626SKris Kennaway  * -in arg	- input file - default stdin
11174664626SKris Kennaway  * -out arg	- output file - default stdout
11274664626SKris Kennaway  * -verify	- check request signature
11374664626SKris Kennaway  * -noout	- don't print stuff out.
11474664626SKris Kennaway  * -text	- print out human readable text.
11574664626SKris Kennaway  * -nodes	- no des encryption
11674664626SKris Kennaway  * -config file	- Load configuration file.
11774664626SKris Kennaway  * -key file	- make a request using key in file (or use it for verification).
1185c87c606SMark Murray  * -keyform arg	- key file format.
119ddd58736SKris Kennaway  * -rand file(s) - load the file(s) into the PRNG.
12074664626SKris Kennaway  * -newkey	- make a key and a request.
12174664626SKris Kennaway  * -modulus	- print RSA modulus.
1225c87c606SMark Murray  * -pubkey	- output Public Key.
12374664626SKris Kennaway  * -x509	- output a self signed X509 structure instead.
12474664626SKris Kennaway  * -asn1-kludge	- output new certificate request in a format that some CA's
12574664626SKris Kennaway  *		  require.  This format is wrong
12674664626SKris Kennaway  */
12774664626SKris Kennaway 
1283b4e3dcbSSimon L. B. Nielsen static int make_REQ(X509_REQ *req,EVP_PKEY *pkey,char *dn,int mutlirdn,
1293b4e3dcbSSimon L. B. Nielsen 		int attribs,unsigned long chtype);
1303b4e3dcbSSimon L. B. Nielsen static int build_subject(X509_REQ *req, char *subj, unsigned long chtype,
1313b4e3dcbSSimon L. B. Nielsen 		int multirdn);
132f579bf8eSKris Kennaway static int prompt_info(X509_REQ *req,
133f579bf8eSKris Kennaway 		STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect,
1345c87c606SMark Murray 		STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect, int attribs,
1355c87c606SMark Murray 		unsigned long chtype);
136f579bf8eSKris Kennaway static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *sk,
1375c87c606SMark Murray 				STACK_OF(CONF_VALUE) *attr, int attribs,
1385c87c606SMark Murray 				unsigned long chtype);
1393b4e3dcbSSimon L. B. Nielsen static int add_attribute_object(X509_REQ *req, char *text, const char *def,
1403b4e3dcbSSimon L. B. Nielsen 				char *value, int nid, int n_min,
1415c87c606SMark Murray 				int n_max, unsigned long chtype);
1423b4e3dcbSSimon L. B. Nielsen static int add_DN_object(X509_NAME *n, char *text, const char *def, char *value,
1433b4e3dcbSSimon L. B. Nielsen 	int nid,int n_min,int n_max, unsigned long chtype, int mval);
1445c87c606SMark Murray #ifndef OPENSSL_NO_RSA
1453b4e3dcbSSimon L. B. Nielsen static int MS_CALLBACK req_cb(int p, int n, BN_GENCB *cb);
146f579bf8eSKris Kennaway #endif
1475c87c606SMark Murray static int req_check_len(int len,int n_min,int n_max);
1483b4e3dcbSSimon L. B. Nielsen static int check_end(const char *str, const char *end);
14974664626SKris Kennaway #ifndef MONOLITH
15074664626SKris Kennaway static char *default_config_file=NULL;
15174664626SKris Kennaway #endif
1525c87c606SMark Murray static CONF *req_conf=NULL;
1535c87c606SMark Murray static int batch=0;
15474664626SKris Kennaway 
15574664626SKris Kennaway #define TYPE_RSA	1
15674664626SKris Kennaway #define TYPE_DSA	2
15774664626SKris Kennaway #define TYPE_DH		3
1583b4e3dcbSSimon L. B. Nielsen #define TYPE_EC		4
15974664626SKris Kennaway 
160f579bf8eSKris Kennaway int MAIN(int, char **);
161f579bf8eSKris Kennaway 
16274664626SKris Kennaway int MAIN(int argc, char **argv)
16374664626SKris Kennaway 	{
1645c87c606SMark Murray 	ENGINE *e = NULL;
1655c87c606SMark Murray #ifndef OPENSSL_NO_DSA
16674664626SKris Kennaway 	DSA *dsa_params=NULL;
16774664626SKris Kennaway #endif
1683b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_ECDSA
1693b4e3dcbSSimon L. B. Nielsen 	EC_KEY *ec_params = NULL;
1703b4e3dcbSSimon L. B. Nielsen #endif
1715c87c606SMark Murray 	unsigned long nmflag = 0, reqflag = 0;
17274664626SKris Kennaway 	int ex=1,x509=0,days=30;
17374664626SKris Kennaway 	X509 *x509ss=NULL;
17474664626SKris Kennaway 	X509_REQ *req=NULL;
17574664626SKris Kennaway 	EVP_PKEY *pkey=NULL;
1765c87c606SMark Murray 	int i=0,badops=0,newreq=0,verbose=0,pkey_type=TYPE_RSA;
1775c87c606SMark Murray 	long newkey = -1;
17874664626SKris Kennaway 	BIO *in=NULL,*out=NULL;
17974664626SKris Kennaway 	int informat,outformat,verify=0,noout=0,text=0,keyform=FORMAT_PEM;
1805c87c606SMark Murray 	int nodes=0,kludge=0,newhdr=0,subject=0,pubkey=0;
18174664626SKris Kennaway 	char *infile,*outfile,*prog,*keyfile=NULL,*template=NULL,*keyout=NULL;
182fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE
1835c87c606SMark Murray 	char *engine=NULL;
184fceca8a3SJacques Vidrine #endif
18574664626SKris Kennaway 	char *extensions = NULL;
186f579bf8eSKris Kennaway 	char *req_exts = NULL;
1875c87c606SMark Murray 	const EVP_CIPHER *cipher=NULL;
1885c87c606SMark Murray 	ASN1_INTEGER *serial = NULL;
18974664626SKris Kennaway 	int modulus=0;
190ddd58736SKris Kennaway 	char *inrand=NULL;
191f579bf8eSKris Kennaway 	char *passargin = NULL, *passargout = NULL;
192f579bf8eSKris Kennaway 	char *passin = NULL, *passout = NULL;
19374664626SKris Kennaway 	char *p;
1945c87c606SMark Murray 	char *subj = NULL;
1953b4e3dcbSSimon L. B. Nielsen 	int multirdn = 0;
1963b4e3dcbSSimon L. B. Nielsen 	const EVP_MD *md_alg=NULL,*digest=EVP_sha1();
1975c87c606SMark Murray 	unsigned long chtype = MBSTRING_ASC;
19874664626SKris Kennaway #ifndef MONOLITH
1995c87c606SMark Murray 	char *to_free;
2005c87c606SMark Murray 	long errline;
20174664626SKris Kennaway #endif
20274664626SKris Kennaway 
203f579bf8eSKris Kennaway 	req_conf = NULL;
2045c87c606SMark Murray #ifndef OPENSSL_NO_DES
20574664626SKris Kennaway 	cipher=EVP_des_ede3_cbc();
20674664626SKris Kennaway #endif
20774664626SKris Kennaway 	apps_startup();
20874664626SKris Kennaway 
20974664626SKris Kennaway 	if (bio_err == NULL)
21074664626SKris Kennaway 		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
21174664626SKris Kennaway 			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
21274664626SKris Kennaway 
21374664626SKris Kennaway 	infile=NULL;
21474664626SKris Kennaway 	outfile=NULL;
21574664626SKris Kennaway 	informat=FORMAT_PEM;
21674664626SKris Kennaway 	outformat=FORMAT_PEM;
21774664626SKris Kennaway 
21874664626SKris Kennaway 	prog=argv[0];
21974664626SKris Kennaway 	argc--;
22074664626SKris Kennaway 	argv++;
22174664626SKris Kennaway 	while (argc >= 1)
22274664626SKris Kennaway 		{
22374664626SKris Kennaway 		if 	(strcmp(*argv,"-inform") == 0)
22474664626SKris Kennaway 			{
22574664626SKris Kennaway 			if (--argc < 1) goto bad;
22674664626SKris Kennaway 			informat=str2fmt(*(++argv));
22774664626SKris Kennaway 			}
22874664626SKris Kennaway 		else if (strcmp(*argv,"-outform") == 0)
22974664626SKris Kennaway 			{
23074664626SKris Kennaway 			if (--argc < 1) goto bad;
23174664626SKris Kennaway 			outformat=str2fmt(*(++argv));
23274664626SKris Kennaway 			}
233fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE
2345c87c606SMark Murray 		else if (strcmp(*argv,"-engine") == 0)
2355c87c606SMark Murray 			{
2365c87c606SMark Murray 			if (--argc < 1) goto bad;
2375c87c606SMark Murray 			engine= *(++argv);
2385c87c606SMark Murray 			}
239fceca8a3SJacques Vidrine #endif
24074664626SKris Kennaway 		else if (strcmp(*argv,"-key") == 0)
24174664626SKris Kennaway 			{
24274664626SKris Kennaway 			if (--argc < 1) goto bad;
24374664626SKris Kennaway 			keyfile= *(++argv);
24474664626SKris Kennaway 			}
2455c87c606SMark Murray 		else if (strcmp(*argv,"-pubkey") == 0)
2465c87c606SMark Murray 			{
2475c87c606SMark Murray 			pubkey=1;
2485c87c606SMark Murray 			}
24974664626SKris Kennaway 		else if (strcmp(*argv,"-new") == 0)
25074664626SKris Kennaway 			{
25174664626SKris Kennaway 			newreq=1;
25274664626SKris Kennaway 			}
25374664626SKris Kennaway 		else if (strcmp(*argv,"-config") == 0)
25474664626SKris Kennaway 			{
25574664626SKris Kennaway 			if (--argc < 1) goto bad;
25674664626SKris Kennaway 			template= *(++argv);
25774664626SKris Kennaway 			}
25874664626SKris Kennaway 		else if (strcmp(*argv,"-keyform") == 0)
25974664626SKris Kennaway 			{
26074664626SKris Kennaway 			if (--argc < 1) goto bad;
26174664626SKris Kennaway 			keyform=str2fmt(*(++argv));
26274664626SKris Kennaway 			}
26374664626SKris Kennaway 		else if (strcmp(*argv,"-in") == 0)
26474664626SKris Kennaway 			{
26574664626SKris Kennaway 			if (--argc < 1) goto bad;
26674664626SKris Kennaway 			infile= *(++argv);
26774664626SKris Kennaway 			}
26874664626SKris Kennaway 		else if (strcmp(*argv,"-out") == 0)
26974664626SKris Kennaway 			{
27074664626SKris Kennaway 			if (--argc < 1) goto bad;
27174664626SKris Kennaway 			outfile= *(++argv);
27274664626SKris Kennaway 			}
27374664626SKris Kennaway 		else if (strcmp(*argv,"-keyout") == 0)
27474664626SKris Kennaway 			{
27574664626SKris Kennaway 			if (--argc < 1) goto bad;
27674664626SKris Kennaway 			keyout= *(++argv);
27774664626SKris Kennaway 			}
278f579bf8eSKris Kennaway 		else if (strcmp(*argv,"-passin") == 0)
279f579bf8eSKris Kennaway 			{
280f579bf8eSKris Kennaway 			if (--argc < 1) goto bad;
281f579bf8eSKris Kennaway 			passargin= *(++argv);
282f579bf8eSKris Kennaway 			}
283f579bf8eSKris Kennaway 		else if (strcmp(*argv,"-passout") == 0)
284f579bf8eSKris Kennaway 			{
285f579bf8eSKris Kennaway 			if (--argc < 1) goto bad;
286f579bf8eSKris Kennaway 			passargout= *(++argv);
287f579bf8eSKris Kennaway 			}
288ddd58736SKris Kennaway 		else if (strcmp(*argv,"-rand") == 0)
289ddd58736SKris Kennaway 			{
290ddd58736SKris Kennaway 			if (--argc < 1) goto bad;
291ddd58736SKris Kennaway 			inrand= *(++argv);
292ddd58736SKris Kennaway 			}
29374664626SKris Kennaway 		else if (strcmp(*argv,"-newkey") == 0)
29474664626SKris Kennaway 			{
29574664626SKris Kennaway 			int is_numeric;
29674664626SKris Kennaway 
29774664626SKris Kennaway 			if (--argc < 1) goto bad;
29874664626SKris Kennaway 			p= *(++argv);
29974664626SKris Kennaway 			is_numeric = p[0] >= '0' && p[0] <= '9';
30074664626SKris Kennaway 			if (strncmp("rsa:",p,4) == 0 || is_numeric)
30174664626SKris Kennaway 				{
30274664626SKris Kennaway 				pkey_type=TYPE_RSA;
30374664626SKris Kennaway 				if(!is_numeric)
30474664626SKris Kennaway 				    p+=4;
30574664626SKris Kennaway 				newkey= atoi(p);
30674664626SKris Kennaway 				}
30774664626SKris Kennaway 			else
3085c87c606SMark Murray #ifndef OPENSSL_NO_DSA
30974664626SKris Kennaway 				if (strncmp("dsa:",p,4) == 0)
31074664626SKris Kennaway 				{
31174664626SKris Kennaway 				X509 *xtmp=NULL;
31274664626SKris Kennaway 				EVP_PKEY *dtmp;
31374664626SKris Kennaway 
31474664626SKris Kennaway 				pkey_type=TYPE_DSA;
31574664626SKris Kennaway 				p+=4;
31674664626SKris Kennaway 				if ((in=BIO_new_file(p,"r")) == NULL)
31774664626SKris Kennaway 					{
31874664626SKris Kennaway 					perror(p);
31974664626SKris Kennaway 					goto end;
32074664626SKris Kennaway 					}
32174664626SKris Kennaway 				if ((dsa_params=PEM_read_bio_DSAparams(in,NULL,NULL,NULL)) == NULL)
32274664626SKris Kennaway 					{
32374664626SKris Kennaway 					ERR_clear_error();
32474664626SKris Kennaway 					(void)BIO_reset(in);
32574664626SKris Kennaway 					if ((xtmp=PEM_read_bio_X509(in,NULL,NULL,NULL)) == NULL)
32674664626SKris Kennaway 						{
32774664626SKris Kennaway 						BIO_printf(bio_err,"unable to load DSA parameters from file\n");
32874664626SKris Kennaway 						goto end;
32974664626SKris Kennaway 						}
33074664626SKris Kennaway 
331c1803d78SJacques Vidrine 					if ((dtmp=X509_get_pubkey(xtmp)) == NULL) goto end;
33274664626SKris Kennaway 					if (dtmp->type == EVP_PKEY_DSA)
33374664626SKris Kennaway 						dsa_params=DSAparams_dup(dtmp->pkey.dsa);
33474664626SKris Kennaway 					EVP_PKEY_free(dtmp);
33574664626SKris Kennaway 					X509_free(xtmp);
33674664626SKris Kennaway 					if (dsa_params == NULL)
33774664626SKris Kennaway 						{
33874664626SKris Kennaway 						BIO_printf(bio_err,"Certificate does not contain DSA parameters\n");
33974664626SKris Kennaway 						goto end;
34074664626SKris Kennaway 						}
34174664626SKris Kennaway 					}
34274664626SKris Kennaway 				BIO_free(in);
34374664626SKris Kennaway 				in=NULL;
3443b4e3dcbSSimon L. B. Nielsen 				newkey=BN_num_bits(dsa_params->p);
3453b4e3dcbSSimon L. B. Nielsen 				}
3463b4e3dcbSSimon L. B. Nielsen 			else
3473b4e3dcbSSimon L. B. Nielsen #endif
3483b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_ECDSA
3493b4e3dcbSSimon L. B. Nielsen 				if (strncmp("ec:",p,3) == 0)
3503b4e3dcbSSimon L. B. Nielsen 				{
3513b4e3dcbSSimon L. B. Nielsen 				X509 *xtmp=NULL;
3523b4e3dcbSSimon L. B. Nielsen 				EVP_PKEY *dtmp;
3533b4e3dcbSSimon L. B. Nielsen 				EC_GROUP *group;
3543b4e3dcbSSimon L. B. Nielsen 
3553b4e3dcbSSimon L. B. Nielsen 				pkey_type=TYPE_EC;
3563b4e3dcbSSimon L. B. Nielsen 				p+=3;
3573b4e3dcbSSimon L. B. Nielsen 				if ((in=BIO_new_file(p,"r")) == NULL)
3583b4e3dcbSSimon L. B. Nielsen 					{
3593b4e3dcbSSimon L. B. Nielsen 					perror(p);
3603b4e3dcbSSimon L. B. Nielsen 					goto end;
3613b4e3dcbSSimon L. B. Nielsen 					}
3623b4e3dcbSSimon L. B. Nielsen 				if ((ec_params = EC_KEY_new()) == NULL)
3633b4e3dcbSSimon L. B. Nielsen 					goto end;
3643b4e3dcbSSimon L. B. Nielsen 				group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL);
3653b4e3dcbSSimon L. B. Nielsen 				if (group == NULL)
3663b4e3dcbSSimon L. B. Nielsen 					{
3673b4e3dcbSSimon L. B. Nielsen 					EC_KEY_free(ec_params);
3683b4e3dcbSSimon L. B. Nielsen 					ERR_clear_error();
3693b4e3dcbSSimon L. B. Nielsen 					(void)BIO_reset(in);
3703b4e3dcbSSimon L. B. Nielsen 					if ((xtmp=PEM_read_bio_X509(in,NULL,NULL,NULL)) == NULL)
3713b4e3dcbSSimon L. B. Nielsen 						{
3723b4e3dcbSSimon L. B. Nielsen 						BIO_printf(bio_err,"unable to load EC parameters from file\n");
3733b4e3dcbSSimon L. B. Nielsen 						goto end;
3743b4e3dcbSSimon L. B. Nielsen 						}
3753b4e3dcbSSimon L. B. Nielsen 
3763b4e3dcbSSimon L. B. Nielsen 					if ((dtmp=X509_get_pubkey(xtmp))==NULL)
3773b4e3dcbSSimon L. B. Nielsen 						goto end;
3783b4e3dcbSSimon L. B. Nielsen 					if (dtmp->type == EVP_PKEY_EC)
3793b4e3dcbSSimon L. B. Nielsen 						ec_params = EC_KEY_dup(dtmp->pkey.ec);
3803b4e3dcbSSimon L. B. Nielsen 					EVP_PKEY_free(dtmp);
3813b4e3dcbSSimon L. B. Nielsen 					X509_free(xtmp);
3823b4e3dcbSSimon L. B. Nielsen 					if (ec_params == NULL)
3833b4e3dcbSSimon L. B. Nielsen 						{
3843b4e3dcbSSimon L. B. Nielsen 						BIO_printf(bio_err,"Certificate does not contain EC parameters\n");
3853b4e3dcbSSimon L. B. Nielsen 						goto end;
3863b4e3dcbSSimon L. B. Nielsen 						}
3873b4e3dcbSSimon L. B. Nielsen 					}
3883b4e3dcbSSimon L. B. Nielsen 				else
3893b4e3dcbSSimon L. B. Nielsen 					{
3903b4e3dcbSSimon L. B. Nielsen 					if (EC_KEY_set_group(ec_params, group) == 0)
3913b4e3dcbSSimon L. B. Nielsen 						goto end;
3923b4e3dcbSSimon L. B. Nielsen 					EC_GROUP_free(group);
3933b4e3dcbSSimon L. B. Nielsen 					}
3943b4e3dcbSSimon L. B. Nielsen 
3953b4e3dcbSSimon L. B. Nielsen 				BIO_free(in);
3963b4e3dcbSSimon L. B. Nielsen 				in=NULL;
3973b4e3dcbSSimon L. B. Nielsen 				newkey = EC_GROUP_get_degree(EC_KEY_get0_group(ec_params));
39874664626SKris Kennaway 				}
39974664626SKris Kennaway 			else
40074664626SKris Kennaway #endif
4015c87c606SMark Murray #ifndef OPENSSL_NO_DH
40274664626SKris Kennaway 				if (strncmp("dh:",p,4) == 0)
40374664626SKris Kennaway 				{
40474664626SKris Kennaway 				pkey_type=TYPE_DH;
40574664626SKris Kennaway 				p+=3;
40674664626SKris Kennaway 				}
40774664626SKris Kennaway 			else
40874664626SKris Kennaway #endif
4093b4e3dcbSSimon L. B. Nielsen 				{
4103b4e3dcbSSimon L. B. Nielsen 				goto bad;
4113b4e3dcbSSimon L. B. Nielsen 				}
41274664626SKris Kennaway 
41374664626SKris Kennaway 			newreq=1;
41474664626SKris Kennaway 			}
4155c87c606SMark Murray 		else if (strcmp(*argv,"-batch") == 0)
4165c87c606SMark Murray 			batch=1;
417f579bf8eSKris Kennaway 		else if (strcmp(*argv,"-newhdr") == 0)
418f579bf8eSKris Kennaway 			newhdr=1;
41974664626SKris Kennaway 		else if (strcmp(*argv,"-modulus") == 0)
42074664626SKris Kennaway 			modulus=1;
42174664626SKris Kennaway 		else if (strcmp(*argv,"-verify") == 0)
42274664626SKris Kennaway 			verify=1;
42374664626SKris Kennaway 		else if (strcmp(*argv,"-nodes") == 0)
42474664626SKris Kennaway 			nodes=1;
42574664626SKris Kennaway 		else if (strcmp(*argv,"-noout") == 0)
42674664626SKris Kennaway 			noout=1;
4275c87c606SMark Murray 		else if (strcmp(*argv,"-verbose") == 0)
4285c87c606SMark Murray 			verbose=1;
4295c87c606SMark Murray 		else if (strcmp(*argv,"-utf8") == 0)
4305c87c606SMark Murray 			chtype = MBSTRING_UTF8;
4315c87c606SMark Murray 		else if (strcmp(*argv,"-nameopt") == 0)
4325c87c606SMark Murray 			{
4335c87c606SMark Murray 			if (--argc < 1) goto bad;
4345c87c606SMark Murray 			if (!set_name_ex(&nmflag, *(++argv))) goto bad;
4355c87c606SMark Murray 			}
4365c87c606SMark Murray 		else if (strcmp(*argv,"-reqopt") == 0)
4375c87c606SMark Murray 			{
4385c87c606SMark Murray 			if (--argc < 1) goto bad;
4395c87c606SMark Murray 			if (!set_cert_ex(&reqflag, *(++argv))) goto bad;
4405c87c606SMark Murray 			}
4415c87c606SMark Murray 		else if (strcmp(*argv,"-subject") == 0)
4425c87c606SMark Murray 			subject=1;
44374664626SKris Kennaway 		else if (strcmp(*argv,"-text") == 0)
44474664626SKris Kennaway 			text=1;
44574664626SKris Kennaway 		else if (strcmp(*argv,"-x509") == 0)
44674664626SKris Kennaway 			x509=1;
44774664626SKris Kennaway 		else if (strcmp(*argv,"-asn1-kludge") == 0)
44874664626SKris Kennaway 			kludge=1;
44974664626SKris Kennaway 		else if (strcmp(*argv,"-no-asn1-kludge") == 0)
45074664626SKris Kennaway 			kludge=0;
4515c87c606SMark Murray 		else if (strcmp(*argv,"-subj") == 0)
4525c87c606SMark Murray 			{
4535c87c606SMark Murray 			if (--argc < 1) goto bad;
4545c87c606SMark Murray 			subj= *(++argv);
4555c87c606SMark Murray 			}
4563b4e3dcbSSimon L. B. Nielsen 		else if (strcmp(*argv,"-multivalue-rdn") == 0)
4573b4e3dcbSSimon L. B. Nielsen 			multirdn=1;
45874664626SKris Kennaway 		else if (strcmp(*argv,"-days") == 0)
45974664626SKris Kennaway 			{
46074664626SKris Kennaway 			if (--argc < 1) goto bad;
46174664626SKris Kennaway 			days= atoi(*(++argv));
46274664626SKris Kennaway 			if (days == 0) days=30;
46374664626SKris Kennaway 			}
4645c87c606SMark Murray 		else if (strcmp(*argv,"-set_serial") == 0)
4655c87c606SMark Murray 			{
4665c87c606SMark Murray 			if (--argc < 1) goto bad;
4675c87c606SMark Murray 			serial = s2i_ASN1_INTEGER(NULL, *(++argv));
4685c87c606SMark Murray 			if (!serial) goto bad;
4695c87c606SMark Murray 			}
47074664626SKris Kennaway 		else if ((md_alg=EVP_get_digestbyname(&((*argv)[1]))) != NULL)
47174664626SKris Kennaway 			{
47274664626SKris Kennaway 			/* ok */
47374664626SKris Kennaway 			digest=md_alg;
47474664626SKris Kennaway 			}
475f579bf8eSKris Kennaway 		else if (strcmp(*argv,"-extensions") == 0)
476f579bf8eSKris Kennaway 			{
477f579bf8eSKris Kennaway 			if (--argc < 1) goto bad;
478f579bf8eSKris Kennaway 			extensions = *(++argv);
479f579bf8eSKris Kennaway 			}
480f579bf8eSKris Kennaway 		else if (strcmp(*argv,"-reqexts") == 0)
481f579bf8eSKris Kennaway 			{
482f579bf8eSKris Kennaway 			if (--argc < 1) goto bad;
483f579bf8eSKris Kennaway 			req_exts = *(++argv);
484f579bf8eSKris Kennaway 			}
48574664626SKris Kennaway 		else
48674664626SKris Kennaway 			{
48774664626SKris Kennaway 			BIO_printf(bio_err,"unknown option %s\n",*argv);
48874664626SKris Kennaway 			badops=1;
48974664626SKris Kennaway 			break;
49074664626SKris Kennaway 			}
49174664626SKris Kennaway 		argc--;
49274664626SKris Kennaway 		argv++;
49374664626SKris Kennaway 		}
49474664626SKris Kennaway 
49574664626SKris Kennaway 	if (badops)
49674664626SKris Kennaway 		{
49774664626SKris Kennaway bad:
49874664626SKris Kennaway 		BIO_printf(bio_err,"%s [options] <infile >outfile\n",prog);
49974664626SKris Kennaway 		BIO_printf(bio_err,"where options  are\n");
500f579bf8eSKris Kennaway 		BIO_printf(bio_err," -inform arg    input format - DER or PEM\n");
501f579bf8eSKris Kennaway 		BIO_printf(bio_err," -outform arg   output format - DER or PEM\n");
50274664626SKris Kennaway 		BIO_printf(bio_err," -in arg        input file\n");
50374664626SKris Kennaway 		BIO_printf(bio_err," -out arg       output file\n");
50474664626SKris Kennaway 		BIO_printf(bio_err," -text          text form of request\n");
5055c87c606SMark Murray 		BIO_printf(bio_err," -pubkey        output public key\n");
50674664626SKris Kennaway 		BIO_printf(bio_err," -noout         do not output REQ\n");
50774664626SKris Kennaway 		BIO_printf(bio_err," -verify        verify signature on REQ\n");
50874664626SKris Kennaway 		BIO_printf(bio_err," -modulus       RSA modulus\n");
50974664626SKris Kennaway 		BIO_printf(bio_err," -nodes         don't encrypt the output key\n");
510fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE
5115c87c606SMark Murray 		BIO_printf(bio_err," -engine e      use engine e, possibly a hardware device\n");
512fceca8a3SJacques Vidrine #endif
5135c87c606SMark Murray 		BIO_printf(bio_err," -subject       output the request's subject\n");
5145c87c606SMark Murray 		BIO_printf(bio_err," -passin        private key password source\n");
51574664626SKris Kennaway 		BIO_printf(bio_err," -key file      use the private key contained in file\n");
51674664626SKris Kennaway 		BIO_printf(bio_err," -keyform arg   key file format\n");
51774664626SKris Kennaway 		BIO_printf(bio_err," -keyout arg    file to send the key to\n");
518ddd58736SKris Kennaway 		BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
519ddd58736SKris Kennaway 		BIO_printf(bio_err,"                load the file (or the files in the directory) into\n");
520ddd58736SKris Kennaway 		BIO_printf(bio_err,"                the random number generator\n");
52174664626SKris Kennaway 		BIO_printf(bio_err," -newkey rsa:bits generate a new RSA key of 'bits' in size\n");
52274664626SKris Kennaway 		BIO_printf(bio_err," -newkey dsa:file generate a new DSA key, parameters taken from CA in 'file'\n");
5233b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_ECDSA
5243b4e3dcbSSimon L. B. Nielsen 		BIO_printf(bio_err," -newkey ec:file generate a new EC key, parameters taken from CA in 'file'\n");
5253b4e3dcbSSimon L. B. Nielsen #endif
526a21b1b38SKris Kennaway 		BIO_printf(bio_err," -[digest]      Digest to sign with (md5, sha1, md2, mdc2, md4)\n");
52774664626SKris Kennaway 		BIO_printf(bio_err," -config file   request template file.\n");
5285c87c606SMark Murray 		BIO_printf(bio_err," -subj arg      set or modify request subject\n");
5293b4e3dcbSSimon L. B. Nielsen 		BIO_printf(bio_err," -multivalue-rdn enable support for multivalued RDNs\n");
53074664626SKris Kennaway 		BIO_printf(bio_err," -new           new request.\n");
5315c87c606SMark Murray 		BIO_printf(bio_err," -batch         do not ask anything during request generation\n");
53274664626SKris Kennaway 		BIO_printf(bio_err," -x509          output a x509 structure instead of a cert. req.\n");
5335c87c606SMark Murray 		BIO_printf(bio_err," -days          number of days a certificate generated by -x509 is valid for.\n");
5345c87c606SMark Murray 		BIO_printf(bio_err," -set_serial    serial number to use for a certificate generated by -x509.\n");
535f579bf8eSKris Kennaway 		BIO_printf(bio_err," -newhdr        output \"NEW\" in the header lines\n");
53674664626SKris Kennaway 		BIO_printf(bio_err," -asn1-kludge   Output the 'request' in a format that is wrong but some CA's\n");
53774664626SKris Kennaway 		BIO_printf(bio_err,"                have been reported as requiring\n");
538f579bf8eSKris Kennaway 		BIO_printf(bio_err," -extensions .. specify certificate extension section (override value in config file)\n");
539f579bf8eSKris Kennaway 		BIO_printf(bio_err," -reqexts ..    specify request extension section (override value in config file)\n");
5405c87c606SMark Murray 		BIO_printf(bio_err," -utf8          input characters are UTF8 (default ASCII)\n");
5415c87c606SMark Murray 		BIO_printf(bio_err," -nameopt arg    - various certificate name options\n");
5425c87c606SMark Murray 		BIO_printf(bio_err," -reqopt arg    - various request text options\n\n");
54374664626SKris Kennaway 		goto end;
54474664626SKris Kennaway 		}
54574664626SKris Kennaway 
54674664626SKris Kennaway 	ERR_load_crypto_strings();
547f579bf8eSKris Kennaway 	if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
548f579bf8eSKris Kennaway 		BIO_printf(bio_err, "Error getting passwords\n");
549f579bf8eSKris Kennaway 		goto end;
550f579bf8eSKris Kennaway 	}
55174664626SKris Kennaway 
552f579bf8eSKris Kennaway #ifndef MONOLITH /* else this has happened in openssl.c (global `config') */
55374664626SKris Kennaway 	/* Lets load up our environment a little */
55474664626SKris Kennaway 	p=getenv("OPENSSL_CONF");
55574664626SKris Kennaway 	if (p == NULL)
55674664626SKris Kennaway 		p=getenv("SSLEAY_CONF");
55774664626SKris Kennaway 	if (p == NULL)
5585c87c606SMark Murray 		p=to_free=make_config_name();
55974664626SKris Kennaway 	default_config_file=p;
5605c87c606SMark Murray 	config=NCONF_new(NULL);
5615c87c606SMark Murray 	i=NCONF_load(config, p, &errline);
56274664626SKris Kennaway #endif
56374664626SKris Kennaway 
56474664626SKris Kennaway 	if (template != NULL)
56574664626SKris Kennaway 		{
5665c87c606SMark Murray 		long errline = -1;
56774664626SKris Kennaway 
5685c87c606SMark Murray 		if( verbose )
56974664626SKris Kennaway 			BIO_printf(bio_err,"Using configuration from %s\n",template);
5705c87c606SMark Murray 		req_conf=NCONF_new(NULL);
5715c87c606SMark Murray 		i=NCONF_load(req_conf,template,&errline);
5725c87c606SMark Murray 		if (i == 0)
57374664626SKris Kennaway 			{
57474664626SKris Kennaway 			BIO_printf(bio_err,"error on line %ld of %s\n",errline,template);
57574664626SKris Kennaway 			goto end;
57674664626SKris Kennaway 			}
57774664626SKris Kennaway 		}
57874664626SKris Kennaway 	else
57974664626SKris Kennaway 		{
58074664626SKris Kennaway 		req_conf=config;
5813b4e3dcbSSimon L. B. Nielsen 
58274664626SKris Kennaway 		if (req_conf == NULL)
58374664626SKris Kennaway 			{
5843b4e3dcbSSimon L. B. Nielsen 			BIO_printf(bio_err,"Unable to load config info from %s\n", default_config_file);
5853b4e3dcbSSimon L. B. Nielsen 			if (newreq)
5863b4e3dcbSSimon L. B. Nielsen 				goto end;
58774664626SKris Kennaway 			}
5883b4e3dcbSSimon L. B. Nielsen 		else if( verbose )
5893b4e3dcbSSimon L. B. Nielsen 			BIO_printf(bio_err,"Using configuration from %s\n",
5903b4e3dcbSSimon L. B. Nielsen 			default_config_file);
59174664626SKris Kennaway 		}
59274664626SKris Kennaway 
59374664626SKris Kennaway 	if (req_conf != NULL)
59474664626SKris Kennaway 		{
5955c87c606SMark Murray 		if (!load_config(bio_err, req_conf))
5965c87c606SMark Murray 			goto end;
5975c87c606SMark Murray 		p=NCONF_get_string(req_conf,NULL,"oid_file");
5985c87c606SMark Murray 		if (p == NULL)
5995c87c606SMark Murray 			ERR_clear_error();
60074664626SKris Kennaway 		if (p != NULL)
60174664626SKris Kennaway 			{
60274664626SKris Kennaway 			BIO *oid_bio;
60374664626SKris Kennaway 
60474664626SKris Kennaway 			oid_bio=BIO_new_file(p,"r");
60574664626SKris Kennaway 			if (oid_bio == NULL)
60674664626SKris Kennaway 				{
60774664626SKris Kennaway 				/*
60874664626SKris Kennaway 				BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
60974664626SKris Kennaway 				ERR_print_errors(bio_err);
61074664626SKris Kennaway 				*/
61174664626SKris Kennaway 				}
61274664626SKris Kennaway 			else
61374664626SKris Kennaway 				{
61474664626SKris Kennaway 				OBJ_create_objects(oid_bio);
61574664626SKris Kennaway 				BIO_free(oid_bio);
61674664626SKris Kennaway 				}
61774664626SKris Kennaway 			}
61874664626SKris Kennaway 		}
619ddd58736SKris Kennaway 	if(!add_oid_section(bio_err, req_conf)) goto end;
62074664626SKris Kennaway 
6215c87c606SMark Murray 	if (md_alg == NULL)
6225c87c606SMark Murray 		{
6235c87c606SMark Murray 		p=NCONF_get_string(req_conf,SECTION,"default_md");
6245c87c606SMark Murray 		if (p == NULL)
6255c87c606SMark Murray 			ERR_clear_error();
6265c87c606SMark Murray 		if (p != NULL)
62774664626SKris Kennaway 			{
62874664626SKris Kennaway 			if ((md_alg=EVP_get_digestbyname(p)) != NULL)
62974664626SKris Kennaway 				digest=md_alg;
63074664626SKris Kennaway 			}
6315c87c606SMark Murray 		}
63274664626SKris Kennaway 
633f579bf8eSKris Kennaway 	if (!extensions)
6345c87c606SMark Murray 		{
6355c87c606SMark Murray 		extensions = NCONF_get_string(req_conf, SECTION, V3_EXTENSIONS);
6365c87c606SMark Murray 		if (!extensions)
6375c87c606SMark Murray 			ERR_clear_error();
6385c87c606SMark Murray 		}
63974664626SKris Kennaway 	if (extensions) {
64074664626SKris Kennaway 		/* Check syntax of file */
64174664626SKris Kennaway 		X509V3_CTX ctx;
64274664626SKris Kennaway 		X509V3_set_ctx_test(&ctx);
6435c87c606SMark Murray 		X509V3_set_nconf(&ctx, req_conf);
6445c87c606SMark Murray 		if(!X509V3_EXT_add_nconf(req_conf, &ctx, extensions, NULL)) {
64574664626SKris Kennaway 			BIO_printf(bio_err,
64674664626SKris Kennaway 			 "Error Loading extension section %s\n", extensions);
64774664626SKris Kennaway 			goto end;
64874664626SKris Kennaway 		}
64974664626SKris Kennaway 	}
65074664626SKris Kennaway 
651f579bf8eSKris Kennaway 	if(!passin)
6525c87c606SMark Murray 		{
6535c87c606SMark Murray 		passin = NCONF_get_string(req_conf, SECTION, "input_password");
6545c87c606SMark Murray 		if (!passin)
6555c87c606SMark Murray 			ERR_clear_error();
6565c87c606SMark Murray 		}
657f579bf8eSKris Kennaway 
658f579bf8eSKris Kennaway 	if(!passout)
6595c87c606SMark Murray 		{
6605c87c606SMark Murray 		passout = NCONF_get_string(req_conf, SECTION, "output_password");
6615c87c606SMark Murray 		if (!passout)
6625c87c606SMark Murray 			ERR_clear_error();
6635c87c606SMark Murray 		}
664f579bf8eSKris Kennaway 
6655c87c606SMark Murray 	p = NCONF_get_string(req_conf, SECTION, STRING_MASK);
6665c87c606SMark Murray 	if (!p)
6675c87c606SMark Murray 		ERR_clear_error();
668f579bf8eSKris Kennaway 
669f579bf8eSKris Kennaway 	if(p && !ASN1_STRING_set_default_mask_asc(p)) {
670f579bf8eSKris Kennaway 		BIO_printf(bio_err, "Invalid global string mask setting %s\n", p);
671f579bf8eSKris Kennaway 		goto end;
672f579bf8eSKris Kennaway 	}
673f579bf8eSKris Kennaway 
6745c87c606SMark Murray 	if (chtype != MBSTRING_UTF8)
6755c87c606SMark Murray 		{
6765c87c606SMark Murray 		p = NCONF_get_string(req_conf, SECTION, UTF8_IN);
6775c87c606SMark Murray 		if (!p)
6785c87c606SMark Murray 			ERR_clear_error();
6795c87c606SMark Murray 		else if (!strcmp(p, "yes"))
6805c87c606SMark Murray 			chtype = MBSTRING_UTF8;
6815c87c606SMark Murray 		}
6825c87c606SMark Murray 
6835c87c606SMark Murray 
684f579bf8eSKris Kennaway 	if(!req_exts)
6855c87c606SMark Murray 		{
6865c87c606SMark Murray 		req_exts = NCONF_get_string(req_conf, SECTION, REQ_EXTENSIONS);
6875c87c606SMark Murray 		if (!req_exts)
6885c87c606SMark Murray 			ERR_clear_error();
6895c87c606SMark Murray 		}
690f579bf8eSKris Kennaway 	if(req_exts) {
691f579bf8eSKris Kennaway 		/* Check syntax of file */
692f579bf8eSKris Kennaway 		X509V3_CTX ctx;
693f579bf8eSKris Kennaway 		X509V3_set_ctx_test(&ctx);
6945c87c606SMark Murray 		X509V3_set_nconf(&ctx, req_conf);
6955c87c606SMark Murray 		if(!X509V3_EXT_add_nconf(req_conf, &ctx, req_exts, NULL)) {
696f579bf8eSKris Kennaway 			BIO_printf(bio_err,
697f579bf8eSKris Kennaway 			 "Error Loading request extension section %s\n",
698f579bf8eSKris Kennaway 								req_exts);
699f579bf8eSKris Kennaway 			goto end;
700f579bf8eSKris Kennaway 		}
701f579bf8eSKris Kennaway 	}
702f579bf8eSKris Kennaway 
70374664626SKris Kennaway 	in=BIO_new(BIO_s_file());
70474664626SKris Kennaway 	out=BIO_new(BIO_s_file());
70574664626SKris Kennaway 	if ((in == NULL) || (out == NULL))
70674664626SKris Kennaway 		goto end;
70774664626SKris Kennaway 
708fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE
7095c87c606SMark Murray         e = setup_engine(bio_err, engine, 0);
710fceca8a3SJacques Vidrine #endif
7115c87c606SMark Murray 
71274664626SKris Kennaway 	if (keyfile != NULL)
71374664626SKris Kennaway 		{
7145c87c606SMark Murray 		pkey = load_key(bio_err, keyfile, keyform, 0, passin, e,
7155c87c606SMark Murray 			"Private Key");
7165c87c606SMark Murray 		if (!pkey)
71774664626SKris Kennaway 			{
7185c87c606SMark Murray 			/* load_key() has already printed an appropriate
7195c87c606SMark Murray 			   message */
72074664626SKris Kennaway 			goto end;
72174664626SKris Kennaway 			}
722db522d3aSSimon L. B. Nielsen 		else
723ddd58736SKris Kennaway 			{
7245c87c606SMark Murray 			char *randfile = NCONF_get_string(req_conf,SECTION,"RANDFILE");
7255c87c606SMark Murray 			if (randfile == NULL)
7265c87c606SMark Murray 				ERR_clear_error();
727ddd58736SKris Kennaway 			app_RAND_load_file(randfile, bio_err, 0);
728ddd58736SKris Kennaway 			}
72974664626SKris Kennaway 		}
73074664626SKris Kennaway 
73174664626SKris Kennaway 	if (newreq && (pkey == NULL))
73274664626SKris Kennaway 		{
7333b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_RSA
7343b4e3dcbSSimon L. B. Nielsen 		BN_GENCB cb;
7353b4e3dcbSSimon L. B. Nielsen #endif
7365c87c606SMark Murray 		char *randfile = NCONF_get_string(req_conf,SECTION,"RANDFILE");
7375c87c606SMark Murray 		if (randfile == NULL)
7385c87c606SMark Murray 			ERR_clear_error();
739f579bf8eSKris Kennaway 		app_RAND_load_file(randfile, bio_err, 0);
740ddd58736SKris Kennaway 		if (inrand)
741ddd58736SKris Kennaway 			app_RAND_load_files(inrand);
74274664626SKris Kennaway 
74374664626SKris Kennaway 		if (newkey <= 0)
74474664626SKris Kennaway 			{
7455c87c606SMark Murray 			if (!NCONF_get_number(req_conf,SECTION,BITS, &newkey))
74674664626SKris Kennaway 				newkey=DEFAULT_KEY_LENGTH;
74774664626SKris Kennaway 			}
74874664626SKris Kennaway 
7493b4e3dcbSSimon L. B. Nielsen 		if (newkey < MIN_KEY_LENGTH && (pkey_type == TYPE_RSA || pkey_type == TYPE_DSA))
75074664626SKris Kennaway 			{
75174664626SKris Kennaway 			BIO_printf(bio_err,"private key length is too short,\n");
7523b4e3dcbSSimon L. B. Nielsen 			BIO_printf(bio_err,"it needs to be at least %d bits, not %ld\n",MIN_KEY_LENGTH,newkey);
75374664626SKris Kennaway 			goto end;
75474664626SKris Kennaway 			}
7553b4e3dcbSSimon L. B. Nielsen 		BIO_printf(bio_err,"Generating a %ld bit %s private key\n",
7563b4e3dcbSSimon L. B. Nielsen 			newkey,(pkey_type == TYPE_RSA)?"RSA":
7573b4e3dcbSSimon L. B. Nielsen 			(pkey_type == TYPE_DSA)?"DSA":"EC");
75874664626SKris Kennaway 
75974664626SKris Kennaway 		if ((pkey=EVP_PKEY_new()) == NULL) goto end;
76074664626SKris Kennaway 
7615c87c606SMark Murray #ifndef OPENSSL_NO_RSA
7623b4e3dcbSSimon L. B. Nielsen 		BN_GENCB_set(&cb, req_cb, bio_err);
76374664626SKris Kennaway 		if (pkey_type == TYPE_RSA)
76474664626SKris Kennaway 			{
7653b4e3dcbSSimon L. B. Nielsen 			RSA *rsa = RSA_new();
7663b4e3dcbSSimon L. B. Nielsen 			BIGNUM *bn = BN_new();
7673b4e3dcbSSimon L. B. Nielsen 			if(!bn || !rsa || !BN_set_word(bn, 0x10001) ||
7683b4e3dcbSSimon L. B. Nielsen 					!RSA_generate_key_ex(rsa, newkey, bn, &cb) ||
7693b4e3dcbSSimon L. B. Nielsen 					!EVP_PKEY_assign_RSA(pkey, rsa))
7703b4e3dcbSSimon L. B. Nielsen 				{
7713b4e3dcbSSimon L. B. Nielsen 				if(bn) BN_free(bn);
7723b4e3dcbSSimon L. B. Nielsen 				if(rsa) RSA_free(rsa);
77374664626SKris Kennaway 				goto end;
77474664626SKris Kennaway 				}
7753b4e3dcbSSimon L. B. Nielsen 			BN_free(bn);
7763b4e3dcbSSimon L. B. Nielsen 			}
77774664626SKris Kennaway 		else
77874664626SKris Kennaway #endif
7795c87c606SMark Murray #ifndef OPENSSL_NO_DSA
78074664626SKris Kennaway 			if (pkey_type == TYPE_DSA)
78174664626SKris Kennaway 			{
78274664626SKris Kennaway 			if (!DSA_generate_key(dsa_params)) goto end;
78374664626SKris Kennaway 			if (!EVP_PKEY_assign_DSA(pkey,dsa_params)) goto end;
78474664626SKris Kennaway 			dsa_params=NULL;
78574664626SKris Kennaway 			}
78674664626SKris Kennaway #endif
7873b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_ECDSA
7883b4e3dcbSSimon L. B. Nielsen 			if (pkey_type == TYPE_EC)
7893b4e3dcbSSimon L. B. Nielsen 			{
7903b4e3dcbSSimon L. B. Nielsen 			if (!EC_KEY_generate_key(ec_params)) goto end;
7913b4e3dcbSSimon L. B. Nielsen 			if (!EVP_PKEY_assign_EC_KEY(pkey, ec_params))
7923b4e3dcbSSimon L. B. Nielsen 				goto end;
7933b4e3dcbSSimon L. B. Nielsen 			ec_params = NULL;
7943b4e3dcbSSimon L. B. Nielsen 			}
7953b4e3dcbSSimon L. B. Nielsen #endif
79674664626SKris Kennaway 
797f579bf8eSKris Kennaway 		app_RAND_write_file(randfile, bio_err);
79874664626SKris Kennaway 
79974664626SKris Kennaway 		if (pkey == NULL) goto end;
80074664626SKris Kennaway 
80174664626SKris Kennaway 		if (keyout == NULL)
8025c87c606SMark Murray 			{
8035c87c606SMark Murray 			keyout=NCONF_get_string(req_conf,SECTION,KEYFILE);
8045c87c606SMark Murray 			if (keyout == NULL)
8055c87c606SMark Murray 				ERR_clear_error();
8065c87c606SMark Murray 			}
80774664626SKris Kennaway 
80874664626SKris Kennaway 		if (keyout == NULL)
80974664626SKris Kennaway 			{
81074664626SKris Kennaway 			BIO_printf(bio_err,"writing new private key to stdout\n");
81174664626SKris Kennaway 			BIO_set_fp(out,stdout,BIO_NOCLOSE);
8125c87c606SMark Murray #ifdef OPENSSL_SYS_VMS
813ddd58736SKris Kennaway 			{
814ddd58736SKris Kennaway 			BIO *tmpbio = BIO_new(BIO_f_linebuffer());
815ddd58736SKris Kennaway 			out = BIO_push(tmpbio, out);
816ddd58736SKris Kennaway 			}
817ddd58736SKris Kennaway #endif
81874664626SKris Kennaway 			}
81974664626SKris Kennaway 		else
82074664626SKris Kennaway 			{
82174664626SKris Kennaway 			BIO_printf(bio_err,"writing new private key to '%s'\n",keyout);
82274664626SKris Kennaway 			if (BIO_write_filename(out,keyout) <= 0)
82374664626SKris Kennaway 				{
82474664626SKris Kennaway 				perror(keyout);
82574664626SKris Kennaway 				goto end;
82674664626SKris Kennaway 				}
82774664626SKris Kennaway 			}
82874664626SKris Kennaway 
8295c87c606SMark Murray 		p=NCONF_get_string(req_conf,SECTION,"encrypt_rsa_key");
83074664626SKris Kennaway 		if (p == NULL)
8315c87c606SMark Murray 			{
8325c87c606SMark Murray 			ERR_clear_error();
8335c87c606SMark Murray 			p=NCONF_get_string(req_conf,SECTION,"encrypt_key");
8345c87c606SMark Murray 			if (p == NULL)
8355c87c606SMark Murray 				ERR_clear_error();
8365c87c606SMark Murray 			}
83774664626SKris Kennaway 		if ((p != NULL) && (strcmp(p,"no") == 0))
83874664626SKris Kennaway 			cipher=NULL;
83974664626SKris Kennaway 		if (nodes) cipher=NULL;
84074664626SKris Kennaway 
84174664626SKris Kennaway 		i=0;
84274664626SKris Kennaway loop:
84374664626SKris Kennaway 		if (!PEM_write_bio_PrivateKey(out,pkey,cipher,
844f579bf8eSKris Kennaway 			NULL,0,NULL,passout))
84574664626SKris Kennaway 			{
84674664626SKris Kennaway 			if ((ERR_GET_REASON(ERR_peek_error()) ==
84774664626SKris Kennaway 				PEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3))
84874664626SKris Kennaway 				{
84974664626SKris Kennaway 				ERR_clear_error();
85074664626SKris Kennaway 				i++;
85174664626SKris Kennaway 				goto loop;
85274664626SKris Kennaway 				}
85374664626SKris Kennaway 			goto end;
85474664626SKris Kennaway 			}
85574664626SKris Kennaway 		BIO_printf(bio_err,"-----\n");
85674664626SKris Kennaway 		}
85774664626SKris Kennaway 
85874664626SKris Kennaway 	if (!newreq)
85974664626SKris Kennaway 		{
86074664626SKris Kennaway 		/* Since we are using a pre-existing certificate
86174664626SKris Kennaway 		 * request, the kludge 'format' info should not be
86274664626SKris Kennaway 		 * changed. */
86374664626SKris Kennaway 		kludge= -1;
86474664626SKris Kennaway 		if (infile == NULL)
86574664626SKris Kennaway 			BIO_set_fp(in,stdin,BIO_NOCLOSE);
86674664626SKris Kennaway 		else
86774664626SKris Kennaway 			{
86874664626SKris Kennaway 			if (BIO_read_filename(in,infile) <= 0)
86974664626SKris Kennaway 				{
87074664626SKris Kennaway 				perror(infile);
87174664626SKris Kennaway 				goto end;
87274664626SKris Kennaway 				}
87374664626SKris Kennaway 			}
87474664626SKris Kennaway 
87574664626SKris Kennaway 		if	(informat == FORMAT_ASN1)
87674664626SKris Kennaway 			req=d2i_X509_REQ_bio(in,NULL);
87774664626SKris Kennaway 		else if (informat == FORMAT_PEM)
87874664626SKris Kennaway 			req=PEM_read_bio_X509_REQ(in,NULL,NULL,NULL);
87974664626SKris Kennaway 		else
88074664626SKris Kennaway 			{
88174664626SKris Kennaway 			BIO_printf(bio_err,"bad input format specified for X509 request\n");
88274664626SKris Kennaway 			goto end;
88374664626SKris Kennaway 			}
88474664626SKris Kennaway 		if (req == NULL)
88574664626SKris Kennaway 			{
88674664626SKris Kennaway 			BIO_printf(bio_err,"unable to load X509 request\n");
88774664626SKris Kennaway 			goto end;
88874664626SKris Kennaway 			}
88974664626SKris Kennaway 		}
89074664626SKris Kennaway 
89174664626SKris Kennaway 	if (newreq || x509)
89274664626SKris Kennaway 		{
89374664626SKris Kennaway 		if (pkey == NULL)
89474664626SKris Kennaway 			{
89574664626SKris Kennaway 			BIO_printf(bio_err,"you need to specify a private key\n");
89674664626SKris Kennaway 			goto end;
89774664626SKris Kennaway 			}
8985c87c606SMark Murray #ifndef OPENSSL_NO_DSA
899de7cdddaSKris Kennaway 		if (pkey->type == EVP_PKEY_DSA)
900de7cdddaSKris Kennaway 			digest=EVP_dss1();
901de7cdddaSKris Kennaway #endif
9023b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_ECDSA
9033b4e3dcbSSimon L. B. Nielsen 		if (pkey->type == EVP_PKEY_EC)
9043b4e3dcbSSimon L. B. Nielsen 			digest=EVP_ecdsa();
9053b4e3dcbSSimon L. B. Nielsen #endif
90674664626SKris Kennaway 		if (req == NULL)
90774664626SKris Kennaway 			{
90874664626SKris Kennaway 			req=X509_REQ_new();
90974664626SKris Kennaway 			if (req == NULL)
91074664626SKris Kennaway 				{
91174664626SKris Kennaway 				goto end;
91274664626SKris Kennaway 				}
91374664626SKris Kennaway 
9143b4e3dcbSSimon L. B. Nielsen 			i=make_REQ(req,pkey,subj,multirdn,!x509, chtype);
9155c87c606SMark Murray 			subj=NULL; /* done processing '-subj' option */
9165c87c606SMark Murray 			if ((kludge > 0) && !sk_X509_ATTRIBUTE_num(req->req_info->attributes))
9175c87c606SMark Murray 				{
9185c87c606SMark Murray 				sk_X509_ATTRIBUTE_free(req->req_info->attributes);
9195c87c606SMark Murray 				req->req_info->attributes = NULL;
9205c87c606SMark Murray 				}
92174664626SKris Kennaway 			if (!i)
92274664626SKris Kennaway 				{
92374664626SKris Kennaway 				BIO_printf(bio_err,"problems making Certificate Request\n");
92474664626SKris Kennaway 				goto end;
92574664626SKris Kennaway 				}
92674664626SKris Kennaway 			}
92774664626SKris Kennaway 		if (x509)
92874664626SKris Kennaway 			{
92974664626SKris Kennaway 			EVP_PKEY *tmppkey;
93074664626SKris Kennaway 			X509V3_CTX ext_ctx;
93174664626SKris Kennaway 			if ((x509ss=X509_new()) == NULL) goto end;
93274664626SKris Kennaway 
93374664626SKris Kennaway 			/* Set version to V3 */
934ced566fdSJacques Vidrine 			if(extensions && !X509_set_version(x509ss, 2)) goto end;
9355c87c606SMark Murray 			if (serial)
9365c87c606SMark Murray 				{
9375c87c606SMark Murray 				if (!X509_set_serialNumber(x509ss, serial)) goto end;
9385c87c606SMark Murray 				}
9395c87c606SMark Murray 			else
9405c87c606SMark Murray 				{
9416be8ae07SJacques Vidrine 				if (!rand_serial(NULL,
9426be8ae07SJacques Vidrine 					X509_get_serialNumber(x509ss)))
9436be8ae07SJacques Vidrine 						goto end;
9445c87c606SMark Murray 				}
94574664626SKris Kennaway 
946c1803d78SJacques Vidrine 			if (!X509_set_issuer_name(x509ss, X509_REQ_get_subject_name(req))) goto end;
947c1803d78SJacques Vidrine 			if (!X509_gmtime_adj(X509_get_notBefore(x509ss),0)) goto end;
948c1803d78SJacques Vidrine 			if (!X509_gmtime_adj(X509_get_notAfter(x509ss), (long)60*60*24*days)) goto end;
949c1803d78SJacques Vidrine 			if (!X509_set_subject_name(x509ss, X509_REQ_get_subject_name(req))) goto end;
95074664626SKris Kennaway 			tmppkey = X509_REQ_get_pubkey(req);
951c1803d78SJacques Vidrine 			if (!tmppkey || !X509_set_pubkey(x509ss,tmppkey)) goto end;
95274664626SKris Kennaway 			EVP_PKEY_free(tmppkey);
95374664626SKris Kennaway 
95474664626SKris Kennaway 			/* Set up V3 context struct */
95574664626SKris Kennaway 
95674664626SKris Kennaway 			X509V3_set_ctx(&ext_ctx, x509ss, x509ss, NULL, NULL, 0);
9575c87c606SMark Murray 			X509V3_set_nconf(&ext_ctx, req_conf);
95874664626SKris Kennaway 
95974664626SKris Kennaway 			/* Add extensions */
9605c87c606SMark Murray 			if(extensions && !X509V3_EXT_add_nconf(req_conf,
96174664626SKris Kennaway 				 	&ext_ctx, extensions, x509ss))
96274664626SKris Kennaway 				{
96374664626SKris Kennaway 				BIO_printf(bio_err,
96474664626SKris Kennaway 					"Error Loading extension section %s\n",
96574664626SKris Kennaway 					extensions);
96674664626SKris Kennaway 				goto end;
96774664626SKris Kennaway 				}
96874664626SKris Kennaway 
96974664626SKris Kennaway 			if (!(i=X509_sign(x509ss,pkey,digest)))
97074664626SKris Kennaway 				goto end;
97174664626SKris Kennaway 			}
97274664626SKris Kennaway 		else
97374664626SKris Kennaway 			{
974f579bf8eSKris Kennaway 			X509V3_CTX ext_ctx;
975f579bf8eSKris Kennaway 
976f579bf8eSKris Kennaway 			/* Set up V3 context struct */
977f579bf8eSKris Kennaway 
978f579bf8eSKris Kennaway 			X509V3_set_ctx(&ext_ctx, NULL, NULL, req, NULL, 0);
9795c87c606SMark Murray 			X509V3_set_nconf(&ext_ctx, req_conf);
980f579bf8eSKris Kennaway 
981f579bf8eSKris Kennaway 			/* Add extensions */
9825c87c606SMark Murray 			if(req_exts && !X509V3_EXT_REQ_add_nconf(req_conf,
983f579bf8eSKris Kennaway 				 	&ext_ctx, req_exts, req))
984f579bf8eSKris Kennaway 				{
985f579bf8eSKris Kennaway 				BIO_printf(bio_err,
986f579bf8eSKris Kennaway 					"Error Loading extension section %s\n",
987f579bf8eSKris Kennaway 					req_exts);
988f579bf8eSKris Kennaway 				goto end;
989f579bf8eSKris Kennaway 				}
99074664626SKris Kennaway 			if (!(i=X509_REQ_sign(req,pkey,digest)))
99174664626SKris Kennaway 				goto end;
99274664626SKris Kennaway 			}
99374664626SKris Kennaway 		}
99474664626SKris Kennaway 
9955c87c606SMark Murray 	if (subj && x509)
9965c87c606SMark Murray 		{
9975c87c606SMark Murray 		BIO_printf(bio_err, "Cannot modifiy certificate subject\n");
9985c87c606SMark Murray 		goto end;
9995c87c606SMark Murray 		}
10005c87c606SMark Murray 
10015c87c606SMark Murray 	if (subj && !x509)
10025c87c606SMark Murray 		{
10035c87c606SMark Murray 		if (verbose)
10045c87c606SMark Murray 			{
10055c87c606SMark Murray 			BIO_printf(bio_err, "Modifying Request's Subject\n");
10065c87c606SMark Murray 			print_name(bio_err, "old subject=", X509_REQ_get_subject_name(req), nmflag);
10075c87c606SMark Murray 			}
10085c87c606SMark Murray 
10093b4e3dcbSSimon L. B. Nielsen 		if (build_subject(req, subj, chtype, multirdn) == 0)
10105c87c606SMark Murray 			{
10115c87c606SMark Murray 			BIO_printf(bio_err, "ERROR: cannot modify subject\n");
10125c87c606SMark Murray 			ex=1;
10135c87c606SMark Murray 			goto end;
10145c87c606SMark Murray 			}
10155c87c606SMark Murray 
10165c87c606SMark Murray 		req->req_info->enc.modified = 1;
10175c87c606SMark Murray 
10185c87c606SMark Murray 		if (verbose)
10195c87c606SMark Murray 			{
10205c87c606SMark Murray 			print_name(bio_err, "new subject=", X509_REQ_get_subject_name(req), nmflag);
10215c87c606SMark Murray 			}
10225c87c606SMark Murray 		}
10235c87c606SMark Murray 
102474664626SKris Kennaway 	if (verify && !x509)
102574664626SKris Kennaway 		{
102674664626SKris Kennaway 		int tmp=0;
102774664626SKris Kennaway 
102874664626SKris Kennaway 		if (pkey == NULL)
102974664626SKris Kennaway 			{
103074664626SKris Kennaway 			pkey=X509_REQ_get_pubkey(req);
103174664626SKris Kennaway 			tmp=1;
103274664626SKris Kennaway 			if (pkey == NULL) goto end;
103374664626SKris Kennaway 			}
103474664626SKris Kennaway 
103574664626SKris Kennaway 		i=X509_REQ_verify(req,pkey);
103674664626SKris Kennaway 		if (tmp) {
103774664626SKris Kennaway 			EVP_PKEY_free(pkey);
103874664626SKris Kennaway 			pkey=NULL;
103974664626SKris Kennaway 		}
104074664626SKris Kennaway 
104174664626SKris Kennaway 		if (i < 0)
104274664626SKris Kennaway 			{
104374664626SKris Kennaway 			goto end;
104474664626SKris Kennaway 			}
104574664626SKris Kennaway 		else if (i == 0)
104674664626SKris Kennaway 			{
104774664626SKris Kennaway 			BIO_printf(bio_err,"verify failure\n");
10485c87c606SMark Murray 			ERR_print_errors(bio_err);
104974664626SKris Kennaway 			}
105074664626SKris Kennaway 		else /* if (i > 0) */
105174664626SKris Kennaway 			BIO_printf(bio_err,"verify OK\n");
105274664626SKris Kennaway 		}
105374664626SKris Kennaway 
10545c87c606SMark Murray 	if (noout && !text && !modulus && !subject && !pubkey)
105574664626SKris Kennaway 		{
105674664626SKris Kennaway 		ex=0;
105774664626SKris Kennaway 		goto end;
105874664626SKris Kennaway 		}
105974664626SKris Kennaway 
106074664626SKris Kennaway 	if (outfile == NULL)
1061ddd58736SKris Kennaway 		{
106274664626SKris Kennaway 		BIO_set_fp(out,stdout,BIO_NOCLOSE);
10635c87c606SMark Murray #ifdef OPENSSL_SYS_VMS
1064ddd58736SKris Kennaway 		{
1065ddd58736SKris Kennaway 		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
1066ddd58736SKris Kennaway 		out = BIO_push(tmpbio, out);
1067ddd58736SKris Kennaway 		}
1068ddd58736SKris Kennaway #endif
1069ddd58736SKris Kennaway 		}
107074664626SKris Kennaway 	else
107174664626SKris Kennaway 		{
107274664626SKris Kennaway 		if ((keyout != NULL) && (strcmp(outfile,keyout) == 0))
107374664626SKris Kennaway 			i=(int)BIO_append_filename(out,outfile);
107474664626SKris Kennaway 		else
107574664626SKris Kennaway 			i=(int)BIO_write_filename(out,outfile);
107674664626SKris Kennaway 		if (!i)
107774664626SKris Kennaway 			{
107874664626SKris Kennaway 			perror(outfile);
107974664626SKris Kennaway 			goto end;
108074664626SKris Kennaway 			}
108174664626SKris Kennaway 		}
108274664626SKris Kennaway 
10835c87c606SMark Murray 	if (pubkey)
10845c87c606SMark Murray 		{
10855c87c606SMark Murray 		EVP_PKEY *tpubkey;
10865c87c606SMark Murray 		tpubkey=X509_REQ_get_pubkey(req);
10875c87c606SMark Murray 		if (tpubkey == NULL)
10885c87c606SMark Murray 			{
10895c87c606SMark Murray 			BIO_printf(bio_err,"Error getting public key\n");
10905c87c606SMark Murray 			ERR_print_errors(bio_err);
10915c87c606SMark Murray 			goto end;
10925c87c606SMark Murray 			}
10935c87c606SMark Murray 		PEM_write_bio_PUBKEY(out, tpubkey);
10945c87c606SMark Murray 		EVP_PKEY_free(tpubkey);
10955c87c606SMark Murray 		}
10965c87c606SMark Murray 
109774664626SKris Kennaway 	if (text)
109874664626SKris Kennaway 		{
109974664626SKris Kennaway 		if (x509)
11005c87c606SMark Murray 			X509_print_ex(out, x509ss, nmflag, reqflag);
110174664626SKris Kennaway 		else
11025c87c606SMark Murray 			X509_REQ_print_ex(out, req, nmflag, reqflag);
11035c87c606SMark Murray 		}
11045c87c606SMark Murray 
11055c87c606SMark Murray 	if(subject)
11065c87c606SMark Murray 		{
11075c87c606SMark Murray 		if(x509)
11085c87c606SMark Murray 			print_name(out, "subject=", X509_get_subject_name(x509ss), nmflag);
11095c87c606SMark Murray 		else
11105c87c606SMark Murray 			print_name(out, "subject=", X509_REQ_get_subject_name(req), nmflag);
111174664626SKris Kennaway 		}
111274664626SKris Kennaway 
111374664626SKris Kennaway 	if (modulus)
111474664626SKris Kennaway 		{
11155c87c606SMark Murray 		EVP_PKEY *tpubkey;
111674664626SKris Kennaway 
111774664626SKris Kennaway 		if (x509)
11185c87c606SMark Murray 			tpubkey=X509_get_pubkey(x509ss);
111974664626SKris Kennaway 		else
11205c87c606SMark Murray 			tpubkey=X509_REQ_get_pubkey(req);
11215c87c606SMark Murray 		if (tpubkey == NULL)
112274664626SKris Kennaway 			{
112374664626SKris Kennaway 			fprintf(stdout,"Modulus=unavailable\n");
112474664626SKris Kennaway 			goto end;
112574664626SKris Kennaway 			}
112674664626SKris Kennaway 		fprintf(stdout,"Modulus=");
11275c87c606SMark Murray #ifndef OPENSSL_NO_RSA
11285c87c606SMark Murray 		if (tpubkey->type == EVP_PKEY_RSA)
11295c87c606SMark Murray 			BN_print(out,tpubkey->pkey.rsa->n);
113074664626SKris Kennaway 		else
113174664626SKris Kennaway #endif
113274664626SKris Kennaway 			fprintf(stdout,"Wrong Algorithm type");
11335c87c606SMark Murray 		EVP_PKEY_free(tpubkey);
113474664626SKris Kennaway 		fprintf(stdout,"\n");
113574664626SKris Kennaway 		}
113674664626SKris Kennaway 
113774664626SKris Kennaway 	if (!noout && !x509)
113874664626SKris Kennaway 		{
113974664626SKris Kennaway 		if 	(outformat == FORMAT_ASN1)
114074664626SKris Kennaway 			i=i2d_X509_REQ_bio(out,req);
1141f579bf8eSKris Kennaway 		else if (outformat == FORMAT_PEM) {
1142f579bf8eSKris Kennaway 			if(newhdr) i=PEM_write_bio_X509_REQ_NEW(out,req);
1143f579bf8eSKris Kennaway 			else i=PEM_write_bio_X509_REQ(out,req);
1144f579bf8eSKris Kennaway 		} else {
114574664626SKris Kennaway 			BIO_printf(bio_err,"bad output format specified for outfile\n");
114674664626SKris Kennaway 			goto end;
114774664626SKris Kennaway 			}
114874664626SKris Kennaway 		if (!i)
114974664626SKris Kennaway 			{
115074664626SKris Kennaway 			BIO_printf(bio_err,"unable to write X509 request\n");
115174664626SKris Kennaway 			goto end;
115274664626SKris Kennaway 			}
115374664626SKris Kennaway 		}
115474664626SKris Kennaway 	if (!noout && x509 && (x509ss != NULL))
115574664626SKris Kennaway 		{
115674664626SKris Kennaway 		if 	(outformat == FORMAT_ASN1)
115774664626SKris Kennaway 			i=i2d_X509_bio(out,x509ss);
115874664626SKris Kennaway 		else if (outformat == FORMAT_PEM)
115974664626SKris Kennaway 			i=PEM_write_bio_X509(out,x509ss);
116074664626SKris Kennaway 		else	{
116174664626SKris Kennaway 			BIO_printf(bio_err,"bad output format specified for outfile\n");
116274664626SKris Kennaway 			goto end;
116374664626SKris Kennaway 			}
116474664626SKris Kennaway 		if (!i)
116574664626SKris Kennaway 			{
116674664626SKris Kennaway 			BIO_printf(bio_err,"unable to write X509 certificate\n");
116774664626SKris Kennaway 			goto end;
116874664626SKris Kennaway 			}
116974664626SKris Kennaway 		}
117074664626SKris Kennaway 	ex=0;
117174664626SKris Kennaway end:
11725c87c606SMark Murray #ifndef MONOLITH
11735c87c606SMark Murray 	if(to_free)
11745c87c606SMark Murray 		OPENSSL_free(to_free);
11755c87c606SMark Murray #endif
117674664626SKris Kennaway 	if (ex)
117774664626SKris Kennaway 		{
117874664626SKris Kennaway 		ERR_print_errors(bio_err);
117974664626SKris Kennaway 		}
11805c87c606SMark Murray 	if ((req_conf != NULL) && (req_conf != config)) NCONF_free(req_conf);
118174664626SKris Kennaway 	BIO_free(in);
1182ddd58736SKris Kennaway 	BIO_free_all(out);
118374664626SKris Kennaway 	EVP_PKEY_free(pkey);
118474664626SKris Kennaway 	X509_REQ_free(req);
118574664626SKris Kennaway 	X509_free(x509ss);
11865c87c606SMark Murray 	ASN1_INTEGER_free(serial);
1187ddd58736SKris Kennaway 	if(passargin && passin) OPENSSL_free(passin);
1188ddd58736SKris Kennaway 	if(passargout && passout) OPENSSL_free(passout);
118974664626SKris Kennaway 	OBJ_cleanup();
11905c87c606SMark Murray #ifndef OPENSSL_NO_DSA
119174664626SKris Kennaway 	if (dsa_params != NULL) DSA_free(dsa_params);
119274664626SKris Kennaway #endif
11933b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_ECDSA
11943b4e3dcbSSimon L. B. Nielsen 	if (ec_params != NULL) EC_KEY_free(ec_params);
11953b4e3dcbSSimon L. B. Nielsen #endif
11965c87c606SMark Murray 	apps_shutdown();
11975c87c606SMark Murray 	OPENSSL_EXIT(ex);
119874664626SKris Kennaway 	}
119974664626SKris Kennaway 
12003b4e3dcbSSimon L. B. Nielsen static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, char *subj, int multirdn,
12013b4e3dcbSSimon L. B. Nielsen 			int attribs, unsigned long chtype)
120274664626SKris Kennaway 	{
120374664626SKris Kennaway 	int ret=0,i;
1204f579bf8eSKris Kennaway 	char no_prompt = 0;
1205f579bf8eSKris Kennaway 	STACK_OF(CONF_VALUE) *dn_sk, *attr_sk = NULL;
1206f579bf8eSKris Kennaway 	char *tmp, *dn_sect,*attr_sect;
120774664626SKris Kennaway 
12085c87c606SMark Murray 	tmp=NCONF_get_string(req_conf,SECTION,PROMPT);
12095c87c606SMark Murray 	if (tmp == NULL)
12105c87c606SMark Murray 		ERR_clear_error();
1211f579bf8eSKris Kennaway 	if((tmp != NULL) && !strcmp(tmp, "no")) no_prompt = 1;
1212f579bf8eSKris Kennaway 
12135c87c606SMark Murray 	dn_sect=NCONF_get_string(req_conf,SECTION,DISTINGUISHED_NAME);
1214f579bf8eSKris Kennaway 	if (dn_sect == NULL)
121574664626SKris Kennaway 		{
121674664626SKris Kennaway 		BIO_printf(bio_err,"unable to find '%s' in config\n",
121774664626SKris Kennaway 			DISTINGUISHED_NAME);
121874664626SKris Kennaway 		goto err;
121974664626SKris Kennaway 		}
12205c87c606SMark Murray 	dn_sk=NCONF_get_section(req_conf,dn_sect);
1221f579bf8eSKris Kennaway 	if (dn_sk == NULL)
122274664626SKris Kennaway 		{
1223f579bf8eSKris Kennaway 		BIO_printf(bio_err,"unable to get '%s' section\n",dn_sect);
122474664626SKris Kennaway 		goto err;
122574664626SKris Kennaway 		}
122674664626SKris Kennaway 
12275c87c606SMark Murray 	attr_sect=NCONF_get_string(req_conf,SECTION,ATTRIBUTES);
1228f579bf8eSKris Kennaway 	if (attr_sect == NULL)
12295c87c606SMark Murray 		{
12305c87c606SMark Murray 		ERR_clear_error();
1231f579bf8eSKris Kennaway 		attr_sk=NULL;
12325c87c606SMark Murray 		}
123374664626SKris Kennaway 	else
123474664626SKris Kennaway 		{
12355c87c606SMark Murray 		attr_sk=NCONF_get_section(req_conf,attr_sect);
1236f579bf8eSKris Kennaway 		if (attr_sk == NULL)
123774664626SKris Kennaway 			{
1238f579bf8eSKris Kennaway 			BIO_printf(bio_err,"unable to get '%s' section\n",attr_sect);
123974664626SKris Kennaway 			goto err;
124074664626SKris Kennaway 			}
124174664626SKris Kennaway 		}
124274664626SKris Kennaway 
1243f579bf8eSKris Kennaway 	/* setup version number */
1244f579bf8eSKris Kennaway 	if (!X509_REQ_set_version(req,0L)) goto err; /* version 1 */
124574664626SKris Kennaway 
12465c87c606SMark Murray 	if (no_prompt)
12475c87c606SMark Murray 		i = auto_info(req, dn_sk, attr_sk, attribs, chtype);
12485c87c606SMark Murray 	else
12495c87c606SMark Murray 		{
12505c87c606SMark Murray 		if (subj)
12513b4e3dcbSSimon L. B. Nielsen 			i = build_subject(req, subj, chtype, multirdn);
12525c87c606SMark Murray 		else
12535c87c606SMark Murray 			i = prompt_info(req, dn_sk, dn_sect, attr_sk, attr_sect, attribs, chtype);
12545c87c606SMark Murray 		}
1255f579bf8eSKris Kennaway 	if(!i) goto err;
1256f579bf8eSKris Kennaway 
1257c1803d78SJacques Vidrine 	if (!X509_REQ_set_pubkey(req,pkey)) goto err;
1258f579bf8eSKris Kennaway 
1259f579bf8eSKris Kennaway 	ret=1;
1260f579bf8eSKris Kennaway err:
1261f579bf8eSKris Kennaway 	return(ret);
1262f579bf8eSKris Kennaway 	}
1263f579bf8eSKris Kennaway 
12645c87c606SMark Murray /*
12655c87c606SMark Murray  * subject is expected to be in the format /type0=value0/type1=value1/type2=...
12665c87c606SMark Murray  * where characters may be escaped by \
12675c87c606SMark Murray  */
12683b4e3dcbSSimon L. B. Nielsen static int build_subject(X509_REQ *req, char *subject, unsigned long chtype, int multirdn)
12695c87c606SMark Murray 	{
12705c87c606SMark Murray 	X509_NAME *n;
12715c87c606SMark Murray 
12723b4e3dcbSSimon L. B. Nielsen 	if (!(n = parse_name(subject, chtype, multirdn)))
12735c87c606SMark Murray 		return 0;
12745c87c606SMark Murray 
12755c87c606SMark Murray 	if (!X509_REQ_set_subject_name(req, n))
12765c87c606SMark Murray 		{
12775c87c606SMark Murray 		X509_NAME_free(n);
12785c87c606SMark Murray 		return 0;
12795c87c606SMark Murray 		}
12805c87c606SMark Murray 	X509_NAME_free(n);
12815c87c606SMark Murray 	return 1;
12825c87c606SMark Murray }
12835c87c606SMark Murray 
1284f579bf8eSKris Kennaway 
1285f579bf8eSKris Kennaway static int prompt_info(X509_REQ *req,
1286f579bf8eSKris Kennaway 		STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect,
12875c87c606SMark Murray 		STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect, int attribs,
12885c87c606SMark Murray 		unsigned long chtype)
1289f579bf8eSKris Kennaway 	{
1290f579bf8eSKris Kennaway 	int i;
1291f579bf8eSKris Kennaway 	char *p,*q;
1292f579bf8eSKris Kennaway 	char buf[100];
12933b4e3dcbSSimon L. B. Nielsen 	int nid, mval;
12945c87c606SMark Murray 	long n_min,n_max;
12953b4e3dcbSSimon L. B. Nielsen 	char *type, *value;
12963b4e3dcbSSimon L. B. Nielsen 	const char *def;
1297f579bf8eSKris Kennaway 	CONF_VALUE *v;
1298f579bf8eSKris Kennaway 	X509_NAME *subj;
1299f579bf8eSKris Kennaway 	subj = X509_REQ_get_subject_name(req);
13005c87c606SMark Murray 
13015c87c606SMark Murray 	if(!batch)
13025c87c606SMark Murray 		{
130374664626SKris Kennaway 		BIO_printf(bio_err,"You are about to be asked to enter information that will be incorporated\n");
130474664626SKris Kennaway 		BIO_printf(bio_err,"into your certificate request.\n");
130574664626SKris Kennaway 		BIO_printf(bio_err,"What you are about to enter is what is called a Distinguished Name or a DN.\n");
130674664626SKris Kennaway 		BIO_printf(bio_err,"There are quite a few fields but you can leave some blank\n");
130774664626SKris Kennaway 		BIO_printf(bio_err,"For some fields there will be a default value,\n");
130874664626SKris Kennaway 		BIO_printf(bio_err,"If you enter '.', the field will be left blank.\n");
130974664626SKris Kennaway 		BIO_printf(bio_err,"-----\n");
13105c87c606SMark Murray 		}
131174664626SKris Kennaway 
131274664626SKris Kennaway 
1313f579bf8eSKris Kennaway 	if (sk_CONF_VALUE_num(dn_sk))
131474664626SKris Kennaway 		{
131574664626SKris Kennaway 		i= -1;
131674664626SKris Kennaway start:		for (;;)
131774664626SKris Kennaway 			{
131874664626SKris Kennaway 			i++;
1319f579bf8eSKris Kennaway 			if (sk_CONF_VALUE_num(dn_sk) <= i) break;
132074664626SKris Kennaway 
1321f579bf8eSKris Kennaway 			v=sk_CONF_VALUE_value(dn_sk,i);
132274664626SKris Kennaway 			p=q=NULL;
132374664626SKris Kennaway 			type=v->name;
132474664626SKris Kennaway 			if(!check_end(type,"_min") || !check_end(type,"_max") ||
132574664626SKris Kennaway 				!check_end(type,"_default") ||
132674664626SKris Kennaway 					 !check_end(type,"_value")) continue;
132774664626SKris Kennaway 			/* Skip past any leading X. X: X, etc to allow for
132874664626SKris Kennaway 			 * multiple instances
132974664626SKris Kennaway 			 */
133074664626SKris Kennaway 			for(p = v->name; *p ; p++)
133174664626SKris Kennaway 				if ((*p == ':') || (*p == ',') ||
133274664626SKris Kennaway 							 (*p == '.')) {
133374664626SKris Kennaway 					p++;
133474664626SKris Kennaway 					if(*p) type = p;
133574664626SKris Kennaway 					break;
133674664626SKris Kennaway 				}
13373b4e3dcbSSimon L. B. Nielsen 			if (*type == '+')
13383b4e3dcbSSimon L. B. Nielsen 				{
13393b4e3dcbSSimon L. B. Nielsen 				mval = -1;
13403b4e3dcbSSimon L. B. Nielsen 				type++;
13413b4e3dcbSSimon L. B. Nielsen 				}
13423b4e3dcbSSimon L. B. Nielsen 			else
13433b4e3dcbSSimon L. B. Nielsen 				mval = 0;
134474664626SKris Kennaway 			/* If OBJ not recognised ignore it */
134574664626SKris Kennaway 			if ((nid=OBJ_txt2nid(type)) == NID_undef) goto start;
1346ced566fdSJacques Vidrine 			if (BIO_snprintf(buf,sizeof buf,"%s_default",v->name)
13473b4e3dcbSSimon L. B. Nielsen 				>= (int)sizeof(buf))
13485c87c606SMark Murray 			   {
13495c87c606SMark Murray 			   BIO_printf(bio_err,"Name '%s' too long\n",v->name);
13505c87c606SMark Murray 			   return 0;
13515c87c606SMark Murray 			   }
13525c87c606SMark Murray 
13535c87c606SMark Murray 			if ((def=NCONF_get_string(req_conf,dn_sect,buf)) == NULL)
13545c87c606SMark Murray 				{
13555c87c606SMark Murray 				ERR_clear_error();
13565c87c606SMark Murray 				def="";
13575c87c606SMark Murray 				}
1358ced566fdSJacques Vidrine 
1359ced566fdSJacques Vidrine 			BIO_snprintf(buf,sizeof buf,"%s_value",v->name);
13605c87c606SMark Murray 			if ((value=NCONF_get_string(req_conf,dn_sect,buf)) == NULL)
13615c87c606SMark Murray 				{
13625c87c606SMark Murray 				ERR_clear_error();
136374664626SKris Kennaway 				value=NULL;
13645c87c606SMark Murray 				}
136574664626SKris Kennaway 
1366ced566fdSJacques Vidrine 			BIO_snprintf(buf,sizeof buf,"%s_min",v->name);
13675c87c606SMark Murray 			if (!NCONF_get_number(req_conf,dn_sect,buf, &n_min))
1368fceca8a3SJacques Vidrine 				{
1369fceca8a3SJacques Vidrine 				ERR_clear_error();
13705c87c606SMark Murray 				n_min = -1;
1371fceca8a3SJacques Vidrine 				}
137274664626SKris Kennaway 
1373ced566fdSJacques Vidrine 			BIO_snprintf(buf,sizeof buf,"%s_max",v->name);
13745c87c606SMark Murray 			if (!NCONF_get_number(req_conf,dn_sect,buf, &n_max))
1375fceca8a3SJacques Vidrine 				{
1376fceca8a3SJacques Vidrine 				ERR_clear_error();
13775c87c606SMark Murray 				n_max = -1;
1378fceca8a3SJacques Vidrine 				}
137974664626SKris Kennaway 
1380f579bf8eSKris Kennaway 			if (!add_DN_object(subj,v->value,def,value,nid,
13813b4e3dcbSSimon L. B. Nielsen 				n_min,n_max, chtype, mval))
1382f579bf8eSKris Kennaway 				return 0;
138374664626SKris Kennaway 			}
1384f579bf8eSKris Kennaway 		if (X509_NAME_entry_count(subj) == 0)
138574664626SKris Kennaway 			{
138674664626SKris Kennaway 			BIO_printf(bio_err,"error, no objects specified in config file\n");
1387f579bf8eSKris Kennaway 			return 0;
138874664626SKris Kennaway 			}
138974664626SKris Kennaway 
139074664626SKris Kennaway 		if (attribs)
139174664626SKris Kennaway 			{
13925c87c606SMark Murray 			if ((attr_sk != NULL) && (sk_CONF_VALUE_num(attr_sk) > 0) && (!batch))
139374664626SKris Kennaway 				{
139474664626SKris Kennaway 				BIO_printf(bio_err,"\nPlease enter the following 'extra' attributes\n");
139574664626SKris Kennaway 				BIO_printf(bio_err,"to be sent with your certificate request\n");
139674664626SKris Kennaway 				}
139774664626SKris Kennaway 
139874664626SKris Kennaway 			i= -1;
139974664626SKris Kennaway start2:			for (;;)
140074664626SKris Kennaway 				{
140174664626SKris Kennaway 				i++;
1402f579bf8eSKris Kennaway 				if ((attr_sk == NULL) ||
1403f579bf8eSKris Kennaway 					    (sk_CONF_VALUE_num(attr_sk) <= i))
140474664626SKris Kennaway 					break;
140574664626SKris Kennaway 
1406f579bf8eSKris Kennaway 				v=sk_CONF_VALUE_value(attr_sk,i);
140774664626SKris Kennaway 				type=v->name;
140874664626SKris Kennaway 				if ((nid=OBJ_txt2nid(type)) == NID_undef)
140974664626SKris Kennaway 					goto start2;
141074664626SKris Kennaway 
1411ced566fdSJacques Vidrine 				if (BIO_snprintf(buf,sizeof buf,"%s_default",type)
14123b4e3dcbSSimon L. B. Nielsen 					>= (int)sizeof(buf))
14135c87c606SMark Murray 				   {
14145c87c606SMark Murray 				   BIO_printf(bio_err,"Name '%s' too long\n",v->name);
14155c87c606SMark Murray 				   return 0;
14165c87c606SMark Murray 				   }
14175c87c606SMark Murray 
14185c87c606SMark Murray 				if ((def=NCONF_get_string(req_conf,attr_sect,buf))
141974664626SKris Kennaway 					== NULL)
14205c87c606SMark Murray 					{
14215c87c606SMark Murray 					ERR_clear_error();
142274664626SKris Kennaway 					def="";
14235c87c606SMark Murray 					}
14245c87c606SMark Murray 
142574664626SKris Kennaway 
1426ced566fdSJacques Vidrine 				BIO_snprintf(buf,sizeof buf,"%s_value",type);
14275c87c606SMark Murray 				if ((value=NCONF_get_string(req_conf,attr_sect,buf))
142874664626SKris Kennaway 					== NULL)
14295c87c606SMark Murray 					{
14305c87c606SMark Murray 					ERR_clear_error();
143174664626SKris Kennaway 					value=NULL;
14325c87c606SMark Murray 					}
143374664626SKris Kennaway 
1434ced566fdSJacques Vidrine 				BIO_snprintf(buf,sizeof buf,"%s_min",type);
14355c87c606SMark Murray 				if (!NCONF_get_number(req_conf,attr_sect,buf, &n_min))
1436ab8565e2SSimon L. B. Nielsen 					{
1437ab8565e2SSimon L. B. Nielsen 					ERR_clear_error();
14385c87c606SMark Murray 					n_min = -1;
1439ab8565e2SSimon L. B. Nielsen 					}
144074664626SKris Kennaway 
1441ced566fdSJacques Vidrine 				BIO_snprintf(buf,sizeof buf,"%s_max",type);
14425c87c606SMark Murray 				if (!NCONF_get_number(req_conf,attr_sect,buf, &n_max))
1443ab8565e2SSimon L. B. Nielsen 					{
1444ab8565e2SSimon L. B. Nielsen 					ERR_clear_error();
14455c87c606SMark Murray 					n_max = -1;
1446ab8565e2SSimon L. B. Nielsen 					}
144774664626SKris Kennaway 
1448f579bf8eSKris Kennaway 				if (!add_attribute_object(req,
14495c87c606SMark Murray 					v->value,def,value,nid,n_min,n_max, chtype))
1450f579bf8eSKris Kennaway 					return 0;
145174664626SKris Kennaway 				}
145274664626SKris Kennaway 			}
145374664626SKris Kennaway 		}
145474664626SKris Kennaway 	else
145574664626SKris Kennaway 		{
145674664626SKris Kennaway 		BIO_printf(bio_err,"No template, please set one up.\n");
1457f579bf8eSKris Kennaway 		return 0;
145874664626SKris Kennaway 		}
145974664626SKris Kennaway 
1460f579bf8eSKris Kennaway 	return 1;
146174664626SKris Kennaway 
146274664626SKris Kennaway 	}
146374664626SKris Kennaway 
1464f579bf8eSKris Kennaway static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *dn_sk,
14655c87c606SMark Murray 			STACK_OF(CONF_VALUE) *attr_sk, int attribs, unsigned long chtype)
1466f579bf8eSKris Kennaway 	{
1467f579bf8eSKris Kennaway 	int i;
1468f579bf8eSKris Kennaway 	char *p,*q;
1469f579bf8eSKris Kennaway 	char *type;
1470f579bf8eSKris Kennaway 	CONF_VALUE *v;
1471f579bf8eSKris Kennaway 	X509_NAME *subj;
1472f579bf8eSKris Kennaway 
1473f579bf8eSKris Kennaway 	subj = X509_REQ_get_subject_name(req);
1474f579bf8eSKris Kennaway 
1475f579bf8eSKris Kennaway 	for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++)
1476f579bf8eSKris Kennaway 		{
14773b4e3dcbSSimon L. B. Nielsen 		int mval;
1478f579bf8eSKris Kennaway 		v=sk_CONF_VALUE_value(dn_sk,i);
1479f579bf8eSKris Kennaway 		p=q=NULL;
1480f579bf8eSKris Kennaway 		type=v->name;
1481f579bf8eSKris Kennaway 		/* Skip past any leading X. X: X, etc to allow for
1482f579bf8eSKris Kennaway 		 * multiple instances
1483f579bf8eSKris Kennaway 		 */
1484f579bf8eSKris Kennaway 		for(p = v->name; *p ; p++)
1485ddd58736SKris Kennaway #ifndef CHARSET_EBCDIC
1486f579bf8eSKris Kennaway 			if ((*p == ':') || (*p == ',') || (*p == '.')) {
1487ddd58736SKris Kennaway #else
1488ddd58736SKris Kennaway 			if ((*p == os_toascii[':']) || (*p == os_toascii[',']) || (*p == os_toascii['.'])) {
1489ddd58736SKris Kennaway #endif
1490f579bf8eSKris Kennaway 				p++;
1491f579bf8eSKris Kennaway 				if(*p) type = p;
1492f579bf8eSKris Kennaway 				break;
1493f579bf8eSKris Kennaway 			}
14943b4e3dcbSSimon L. B. Nielsen #ifndef CHARSET_EBCDIC
14953b4e3dcbSSimon L. B. Nielsen 		if (*p == '+')
14963b4e3dcbSSimon L. B. Nielsen #else
14973b4e3dcbSSimon L. B. Nielsen 		if (*p == os_toascii['+'])
14983b4e3dcbSSimon L. B. Nielsen #endif
14993b4e3dcbSSimon L. B. Nielsen 			{
15003b4e3dcbSSimon L. B. Nielsen 			p++;
15013b4e3dcbSSimon L. B. Nielsen 			mval = -1;
15023b4e3dcbSSimon L. B. Nielsen 			}
15033b4e3dcbSSimon L. B. Nielsen 		else
15043b4e3dcbSSimon L. B. Nielsen 			mval = 0;
15055c87c606SMark Murray 		if (!X509_NAME_add_entry_by_txt(subj,type, chtype,
15063b4e3dcbSSimon L. B. Nielsen 				(unsigned char *) v->value,-1,-1,mval)) return 0;
1507f579bf8eSKris Kennaway 
1508f579bf8eSKris Kennaway 		}
1509f579bf8eSKris Kennaway 
1510f579bf8eSKris Kennaway 		if (!X509_NAME_entry_count(subj))
1511f579bf8eSKris Kennaway 			{
1512f579bf8eSKris Kennaway 			BIO_printf(bio_err,"error, no objects specified in config file\n");
1513f579bf8eSKris Kennaway 			return 0;
1514f579bf8eSKris Kennaway 			}
1515f579bf8eSKris Kennaway 		if (attribs)
1516f579bf8eSKris Kennaway 			{
1517f579bf8eSKris Kennaway 			for (i = 0; i < sk_CONF_VALUE_num(attr_sk); i++)
1518f579bf8eSKris Kennaway 				{
1519f579bf8eSKris Kennaway 				v=sk_CONF_VALUE_value(attr_sk,i);
15205c87c606SMark Murray 				if(!X509_REQ_add1_attr_by_txt(req, v->name, chtype,
1521f579bf8eSKris Kennaway 					(unsigned char *)v->value, -1)) return 0;
1522f579bf8eSKris Kennaway 				}
1523f579bf8eSKris Kennaway 			}
1524f579bf8eSKris Kennaway 	return 1;
1525f579bf8eSKris Kennaway 	}
1526f579bf8eSKris Kennaway 
1527f579bf8eSKris Kennaway 
15283b4e3dcbSSimon L. B. Nielsen static int add_DN_object(X509_NAME *n, char *text, const char *def, char *value,
15293b4e3dcbSSimon L. B. Nielsen 	     int nid, int n_min, int n_max, unsigned long chtype, int mval)
153074664626SKris Kennaway 	{
1531f579bf8eSKris Kennaway 	int i,ret=0;
153274664626SKris Kennaway 	MS_STATIC char buf[1024];
153374664626SKris Kennaway start:
15345c87c606SMark Murray 	if (!batch) BIO_printf(bio_err,"%s [%s]:",text,def);
153574664626SKris Kennaway 	(void)BIO_flush(bio_err);
153674664626SKris Kennaway 	if(value != NULL)
153774664626SKris Kennaway 		{
1538ced566fdSJacques Vidrine 		BUF_strlcpy(buf,value,sizeof buf);
1539ced566fdSJacques Vidrine 		BUF_strlcat(buf,"\n",sizeof buf);
154074664626SKris Kennaway 		BIO_printf(bio_err,"%s\n",value);
154174664626SKris Kennaway 		}
154274664626SKris Kennaway 	else
154374664626SKris Kennaway 		{
154474664626SKris Kennaway 		buf[0]='\0';
15455c87c606SMark Murray 		if (!batch)
15465c87c606SMark Murray 			{
15476a599222SSimon L. B. Nielsen 			if (!fgets(buf,sizeof buf,stdin))
15486a599222SSimon L. B. Nielsen 				return 0;
15495c87c606SMark Murray 			}
15505c87c606SMark Murray 		else
15515c87c606SMark Murray 			{
15525c87c606SMark Murray 			buf[0] = '\n';
15535c87c606SMark Murray 			buf[1] = '\0';
15545c87c606SMark Murray 			}
155574664626SKris Kennaway 		}
155674664626SKris Kennaway 
155774664626SKris Kennaway 	if (buf[0] == '\0') return(0);
155874664626SKris Kennaway 	else if (buf[0] == '\n')
155974664626SKris Kennaway 		{
156074664626SKris Kennaway 		if ((def == NULL) || (def[0] == '\0'))
156174664626SKris Kennaway 			return(1);
1562ced566fdSJacques Vidrine 		BUF_strlcpy(buf,def,sizeof buf);
1563ced566fdSJacques Vidrine 		BUF_strlcat(buf,"\n",sizeof buf);
156474664626SKris Kennaway 		}
156574664626SKris Kennaway 	else if ((buf[0] == '.') && (buf[1] == '\n')) return(1);
156674664626SKris Kennaway 
156774664626SKris Kennaway 	i=strlen(buf);
156874664626SKris Kennaway 	if (buf[i-1] != '\n')
156974664626SKris Kennaway 		{
157074664626SKris Kennaway 		BIO_printf(bio_err,"weird input :-(\n");
157174664626SKris Kennaway 		return(0);
157274664626SKris Kennaway 		}
157374664626SKris Kennaway 	buf[--i]='\0';
1574f579bf8eSKris Kennaway #ifdef CHARSET_EBCDIC
1575f579bf8eSKris Kennaway 	ebcdic2ascii(buf, buf, i);
1576f579bf8eSKris Kennaway #endif
15775c87c606SMark Murray 	if(!req_check_len(i, n_min, n_max)) goto start;
15785c87c606SMark Murray 	if (!X509_NAME_add_entry_by_NID(n,nid, chtype,
15793b4e3dcbSSimon L. B. Nielsen 				(unsigned char *) buf, -1,-1,mval)) goto err;
1580f579bf8eSKris Kennaway 	ret=1;
1581f579bf8eSKris Kennaway err:
1582f579bf8eSKris Kennaway 	return(ret);
158374664626SKris Kennaway 	}
158474664626SKris Kennaway 
15853b4e3dcbSSimon L. B. Nielsen static int add_attribute_object(X509_REQ *req, char *text, const char *def,
15863b4e3dcbSSimon L. B. Nielsen 				char *value, int nid, int n_min,
15875c87c606SMark Murray 				int n_max, unsigned long chtype)
1588f579bf8eSKris Kennaway 	{
1589f579bf8eSKris Kennaway 	int i;
1590f579bf8eSKris Kennaway 	static char buf[1024];
159174664626SKris Kennaway 
1592f579bf8eSKris Kennaway start:
15935c87c606SMark Murray 	if (!batch) BIO_printf(bio_err,"%s [%s]:",text,def);
1594f579bf8eSKris Kennaway 	(void)BIO_flush(bio_err);
1595f579bf8eSKris Kennaway 	if (value != NULL)
1596f579bf8eSKris Kennaway 		{
1597ced566fdSJacques Vidrine 		BUF_strlcpy(buf,value,sizeof buf);
1598ced566fdSJacques Vidrine 		BUF_strlcat(buf,"\n",sizeof buf);
1599f579bf8eSKris Kennaway 		BIO_printf(bio_err,"%s\n",value);
1600f579bf8eSKris Kennaway 		}
1601f579bf8eSKris Kennaway 	else
1602f579bf8eSKris Kennaway 		{
1603f579bf8eSKris Kennaway 		buf[0]='\0';
16045c87c606SMark Murray 		if (!batch)
16055c87c606SMark Murray 			{
16066a599222SSimon L. B. Nielsen 			if (!fgets(buf,sizeof buf,stdin))
16076a599222SSimon L. B. Nielsen 				return 0;
16085c87c606SMark Murray 			}
16095c87c606SMark Murray 		else
16105c87c606SMark Murray 			{
16115c87c606SMark Murray 			buf[0] = '\n';
16125c87c606SMark Murray 			buf[1] = '\0';
16135c87c606SMark Murray 			}
1614f579bf8eSKris Kennaway 		}
161574664626SKris Kennaway 
1616f579bf8eSKris Kennaway 	if (buf[0] == '\0') return(0);
1617f579bf8eSKris Kennaway 	else if (buf[0] == '\n')
1618f579bf8eSKris Kennaway 		{
1619f579bf8eSKris Kennaway 		if ((def == NULL) || (def[0] == '\0'))
1620f579bf8eSKris Kennaway 			return(1);
1621ced566fdSJacques Vidrine 		BUF_strlcpy(buf,def,sizeof buf);
1622ced566fdSJacques Vidrine 		BUF_strlcat(buf,"\n",sizeof buf);
1623f579bf8eSKris Kennaway 		}
1624f579bf8eSKris Kennaway 	else if ((buf[0] == '.') && (buf[1] == '\n')) return(1);
162574664626SKris Kennaway 
1626f579bf8eSKris Kennaway 	i=strlen(buf);
1627f579bf8eSKris Kennaway 	if (buf[i-1] != '\n')
1628f579bf8eSKris Kennaway 		{
1629f579bf8eSKris Kennaway 		BIO_printf(bio_err,"weird input :-(\n");
1630f579bf8eSKris Kennaway 		return(0);
1631f579bf8eSKris Kennaway 		}
1632f579bf8eSKris Kennaway 	buf[--i]='\0';
1633ddd58736SKris Kennaway #ifdef CHARSET_EBCDIC
1634ddd58736SKris Kennaway 	ebcdic2ascii(buf, buf, i);
1635ddd58736SKris Kennaway #endif
16365c87c606SMark Murray 	if(!req_check_len(i, n_min, n_max)) goto start;
1637f579bf8eSKris Kennaway 
16385c87c606SMark Murray 	if(!X509_REQ_add1_attr_by_NID(req, nid, chtype,
1639f579bf8eSKris Kennaway 					(unsigned char *)buf, -1)) {
1640f579bf8eSKris Kennaway 		BIO_printf(bio_err, "Error adding attribute\n");
1641f579bf8eSKris Kennaway 		ERR_print_errors(bio_err);
1642f579bf8eSKris Kennaway 		goto err;
1643f579bf8eSKris Kennaway 	}
1644f579bf8eSKris Kennaway 
164574664626SKris Kennaway 	return(1);
164674664626SKris Kennaway err:
164774664626SKris Kennaway 	return(0);
164874664626SKris Kennaway 	}
164974664626SKris Kennaway 
16505c87c606SMark Murray #ifndef OPENSSL_NO_RSA
16513b4e3dcbSSimon L. B. Nielsen static int MS_CALLBACK req_cb(int p, int n, BN_GENCB *cb)
165274664626SKris Kennaway 	{
165374664626SKris Kennaway 	char c='*';
165474664626SKris Kennaway 
165574664626SKris Kennaway 	if (p == 0) c='.';
165674664626SKris Kennaway 	if (p == 1) c='+';
165774664626SKris Kennaway 	if (p == 2) c='*';
165874664626SKris Kennaway 	if (p == 3) c='\n';
16593b4e3dcbSSimon L. B. Nielsen 	BIO_write(cb->arg,&c,1);
16603b4e3dcbSSimon L. B. Nielsen 	(void)BIO_flush(cb->arg);
166174664626SKris Kennaway #ifdef LINT
166274664626SKris Kennaway 	p=n;
166374664626SKris Kennaway #endif
16643b4e3dcbSSimon L. B. Nielsen 	return 1;
166574664626SKris Kennaway 	}
1666f579bf8eSKris Kennaway #endif
166774664626SKris Kennaway 
16685c87c606SMark Murray static int req_check_len(int len, int n_min, int n_max)
166974664626SKris Kennaway 	{
16705c87c606SMark Murray 	if ((n_min > 0) && (len < n_min))
167174664626SKris Kennaway 		{
16725c87c606SMark Murray 		BIO_printf(bio_err,"string is too short, it needs to be at least %d bytes long\n",n_min);
167374664626SKris Kennaway 		return(0);
167474664626SKris Kennaway 		}
16755c87c606SMark Murray 	if ((n_max >= 0) && (len > n_max))
167674664626SKris Kennaway 		{
16775c87c606SMark Murray 		BIO_printf(bio_err,"string is too long, it needs to be less than  %d bytes long\n",n_max);
167874664626SKris Kennaway 		return(0);
167974664626SKris Kennaway 		}
168074664626SKris Kennaway 	return(1);
168174664626SKris Kennaway 	}
168274664626SKris Kennaway 
168374664626SKris Kennaway /* Check if the end of a string matches 'end' */
16843b4e3dcbSSimon L. B. Nielsen static int check_end(const char *str, const char *end)
168574664626SKris Kennaway {
168674664626SKris Kennaway 	int elen, slen;
16873b4e3dcbSSimon L. B. Nielsen 	const char *tmp;
168874664626SKris Kennaway 	elen = strlen(end);
168974664626SKris Kennaway 	slen = strlen(str);
169074664626SKris Kennaway 	if(elen > slen) return 1;
169174664626SKris Kennaway 	tmp = str + slen - elen;
169274664626SKris Kennaway 	return strcmp(tmp, end);
169374664626SKris Kennaway }
1694