xref: /freebsd/crypto/openssl/apps/req.c (revision ced566fd0b59417323f6b6f17b51b25e942cff52)
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 
5974664626SKris Kennaway #include <stdio.h>
6074664626SKris Kennaway #include <stdlib.h>
6174664626SKris Kennaway #include <time.h>
6274664626SKris Kennaway #include <string.h>
635c87c606SMark Murray #ifdef OPENSSL_NO_STDIO
6474664626SKris Kennaway #define APPS_WIN16
6574664626SKris Kennaway #endif
6674664626SKris Kennaway #include "apps.h"
6774664626SKris Kennaway #include <openssl/bio.h>
6874664626SKris Kennaway #include <openssl/evp.h>
6974664626SKris Kennaway #include <openssl/conf.h>
7074664626SKris Kennaway #include <openssl/err.h>
7174664626SKris Kennaway #include <openssl/asn1.h>
7274664626SKris Kennaway #include <openssl/x509.h>
7374664626SKris Kennaway #include <openssl/x509v3.h>
7474664626SKris Kennaway #include <openssl/objects.h>
7574664626SKris Kennaway #include <openssl/pem.h>
765c87c606SMark Murray #include "../crypto/cryptlib.h"
7774664626SKris Kennaway 
7874664626SKris Kennaway #define SECTION		"req"
7974664626SKris Kennaway 
8074664626SKris Kennaway #define BITS		"default_bits"
8174664626SKris Kennaway #define KEYFILE		"default_keyfile"
82f579bf8eSKris Kennaway #define PROMPT		"prompt"
8374664626SKris Kennaway #define DISTINGUISHED_NAME	"distinguished_name"
8474664626SKris Kennaway #define ATTRIBUTES	"attributes"
8574664626SKris Kennaway #define V3_EXTENSIONS	"x509_extensions"
86f579bf8eSKris Kennaway #define REQ_EXTENSIONS	"req_extensions"
87f579bf8eSKris Kennaway #define STRING_MASK	"string_mask"
885c87c606SMark Murray #define UTF8_IN		"utf8"
8974664626SKris Kennaway 
9074664626SKris Kennaway #define DEFAULT_KEY_LENGTH	512
9174664626SKris Kennaway #define MIN_KEY_LENGTH		384
9274664626SKris Kennaway 
9374664626SKris Kennaway #undef PROG
9474664626SKris Kennaway #define PROG	req_main
9574664626SKris Kennaway 
96f579bf8eSKris Kennaway /* -inform arg	- input format - default PEM (DER or PEM)
9774664626SKris Kennaway  * -outform arg - output format - default PEM
9874664626SKris Kennaway  * -in arg	- input file - default stdin
9974664626SKris Kennaway  * -out arg	- output file - default stdout
10074664626SKris Kennaway  * -verify	- check request signature
10174664626SKris Kennaway  * -noout	- don't print stuff out.
10274664626SKris Kennaway  * -text	- print out human readable text.
10374664626SKris Kennaway  * -nodes	- no des encryption
10474664626SKris Kennaway  * -config file	- Load configuration file.
10574664626SKris Kennaway  * -key file	- make a request using key in file (or use it for verification).
1065c87c606SMark Murray  * -keyform arg	- key file format.
107ddd58736SKris Kennaway  * -rand file(s) - load the file(s) into the PRNG.
10874664626SKris Kennaway  * -newkey	- make a key and a request.
10974664626SKris Kennaway  * -modulus	- print RSA modulus.
1105c87c606SMark Murray  * -pubkey	- output Public Key.
11174664626SKris Kennaway  * -x509	- output a self signed X509 structure instead.
11274664626SKris Kennaway  * -asn1-kludge	- output new certificate request in a format that some CA's
11374664626SKris Kennaway  *		  require.  This format is wrong
11474664626SKris Kennaway  */
11574664626SKris Kennaway 
1165c87c606SMark Murray static int make_REQ(X509_REQ *req,EVP_PKEY *pkey,char *dn,int attribs,
1175c87c606SMark Murray 		unsigned long chtype);
1185c87c606SMark Murray static int build_subject(X509_REQ *req, char *subj, unsigned long chtype);
119f579bf8eSKris Kennaway static int prompt_info(X509_REQ *req,
120f579bf8eSKris Kennaway 		STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect,
1215c87c606SMark Murray 		STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect, int attribs,
1225c87c606SMark Murray 		unsigned long chtype);
123f579bf8eSKris Kennaway static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *sk,
1245c87c606SMark Murray 				STACK_OF(CONF_VALUE) *attr, int attribs,
1255c87c606SMark Murray 				unsigned long chtype);
126f579bf8eSKris Kennaway static int add_attribute_object(X509_REQ *req, char *text,
1275c87c606SMark Murray 				char *def, char *value, int nid, int n_min,
1285c87c606SMark Murray 				int n_max, unsigned long chtype);
12974664626SKris Kennaway static int add_DN_object(X509_NAME *n, char *text, char *def, char *value,
1305c87c606SMark Murray 	int nid,int n_min,int n_max, unsigned long chtype);
1315c87c606SMark Murray #ifndef OPENSSL_NO_RSA
13274664626SKris Kennaway static void MS_CALLBACK req_cb(int p,int n,void *arg);
133f579bf8eSKris Kennaway #endif
1345c87c606SMark Murray static int req_check_len(int len,int n_min,int n_max);
13574664626SKris Kennaway static int check_end(char *str, char *end);
13674664626SKris Kennaway #ifndef MONOLITH
13774664626SKris Kennaway static char *default_config_file=NULL;
13874664626SKris Kennaway #endif
1395c87c606SMark Murray static CONF *req_conf=NULL;
1405c87c606SMark Murray static int batch=0;
14174664626SKris Kennaway 
14274664626SKris Kennaway #define TYPE_RSA	1
14374664626SKris Kennaway #define TYPE_DSA	2
14474664626SKris Kennaway #define TYPE_DH		3
14574664626SKris Kennaway 
146f579bf8eSKris Kennaway int MAIN(int, char **);
147f579bf8eSKris Kennaway 
14874664626SKris Kennaway int MAIN(int argc, char **argv)
14974664626SKris Kennaway 	{
1505c87c606SMark Murray 	ENGINE *e = NULL;
1515c87c606SMark Murray #ifndef OPENSSL_NO_DSA
15274664626SKris Kennaway 	DSA *dsa_params=NULL;
15374664626SKris Kennaway #endif
1545c87c606SMark Murray 	unsigned long nmflag = 0, reqflag = 0;
15574664626SKris Kennaway 	int ex=1,x509=0,days=30;
15674664626SKris Kennaway 	X509 *x509ss=NULL;
15774664626SKris Kennaway 	X509_REQ *req=NULL;
15874664626SKris Kennaway 	EVP_PKEY *pkey=NULL;
1595c87c606SMark Murray 	int i=0,badops=0,newreq=0,verbose=0,pkey_type=TYPE_RSA;
1605c87c606SMark Murray 	long newkey = -1;
16174664626SKris Kennaway 	BIO *in=NULL,*out=NULL;
16274664626SKris Kennaway 	int informat,outformat,verify=0,noout=0,text=0,keyform=FORMAT_PEM;
1635c87c606SMark Murray 	int nodes=0,kludge=0,newhdr=0,subject=0,pubkey=0;
16474664626SKris Kennaway 	char *infile,*outfile,*prog,*keyfile=NULL,*template=NULL,*keyout=NULL;
165fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE
1665c87c606SMark Murray 	char *engine=NULL;
167fceca8a3SJacques Vidrine #endif
16874664626SKris Kennaway 	char *extensions = NULL;
169f579bf8eSKris Kennaway 	char *req_exts = NULL;
1705c87c606SMark Murray 	const EVP_CIPHER *cipher=NULL;
1715c87c606SMark Murray 	ASN1_INTEGER *serial = NULL;
17274664626SKris Kennaway 	int modulus=0;
173ddd58736SKris Kennaway 	char *inrand=NULL;
174f579bf8eSKris Kennaway 	char *passargin = NULL, *passargout = NULL;
175f579bf8eSKris Kennaway 	char *passin = NULL, *passout = NULL;
17674664626SKris Kennaway 	char *p;
1775c87c606SMark Murray 	char *subj = NULL;
17874664626SKris Kennaway 	const EVP_MD *md_alg=NULL,*digest=EVP_md5();
1795c87c606SMark Murray 	unsigned long chtype = MBSTRING_ASC;
18074664626SKris Kennaway #ifndef MONOLITH
1815c87c606SMark Murray 	char *to_free;
1825c87c606SMark Murray 	long errline;
18374664626SKris Kennaway #endif
18474664626SKris Kennaway 
185f579bf8eSKris Kennaway 	req_conf = NULL;
1865c87c606SMark Murray #ifndef OPENSSL_NO_DES
18774664626SKris Kennaway 	cipher=EVP_des_ede3_cbc();
18874664626SKris Kennaway #endif
18974664626SKris Kennaway 	apps_startup();
19074664626SKris Kennaway 
19174664626SKris Kennaway 	if (bio_err == NULL)
19274664626SKris Kennaway 		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
19374664626SKris Kennaway 			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
19474664626SKris Kennaway 
19574664626SKris Kennaway 	infile=NULL;
19674664626SKris Kennaway 	outfile=NULL;
19774664626SKris Kennaway 	informat=FORMAT_PEM;
19874664626SKris Kennaway 	outformat=FORMAT_PEM;
19974664626SKris Kennaway 
20074664626SKris Kennaway 	prog=argv[0];
20174664626SKris Kennaway 	argc--;
20274664626SKris Kennaway 	argv++;
20374664626SKris Kennaway 	while (argc >= 1)
20474664626SKris Kennaway 		{
20574664626SKris Kennaway 		if 	(strcmp(*argv,"-inform") == 0)
20674664626SKris Kennaway 			{
20774664626SKris Kennaway 			if (--argc < 1) goto bad;
20874664626SKris Kennaway 			informat=str2fmt(*(++argv));
20974664626SKris Kennaway 			}
21074664626SKris Kennaway 		else if (strcmp(*argv,"-outform") == 0)
21174664626SKris Kennaway 			{
21274664626SKris Kennaway 			if (--argc < 1) goto bad;
21374664626SKris Kennaway 			outformat=str2fmt(*(++argv));
21474664626SKris Kennaway 			}
215fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE
2165c87c606SMark Murray 		else if (strcmp(*argv,"-engine") == 0)
2175c87c606SMark Murray 			{
2185c87c606SMark Murray 			if (--argc < 1) goto bad;
2195c87c606SMark Murray 			engine= *(++argv);
2205c87c606SMark Murray 			}
221fceca8a3SJacques Vidrine #endif
22274664626SKris Kennaway 		else if (strcmp(*argv,"-key") == 0)
22374664626SKris Kennaway 			{
22474664626SKris Kennaway 			if (--argc < 1) goto bad;
22574664626SKris Kennaway 			keyfile= *(++argv);
22674664626SKris Kennaway 			}
2275c87c606SMark Murray 		else if (strcmp(*argv,"-pubkey") == 0)
2285c87c606SMark Murray 			{
2295c87c606SMark Murray 			pubkey=1;
2305c87c606SMark Murray 			}
23174664626SKris Kennaway 		else if (strcmp(*argv,"-new") == 0)
23274664626SKris Kennaway 			{
23374664626SKris Kennaway 			newreq=1;
23474664626SKris Kennaway 			}
23574664626SKris Kennaway 		else if (strcmp(*argv,"-config") == 0)
23674664626SKris Kennaway 			{
23774664626SKris Kennaway 			if (--argc < 1) goto bad;
23874664626SKris Kennaway 			template= *(++argv);
23974664626SKris Kennaway 			}
24074664626SKris Kennaway 		else if (strcmp(*argv,"-keyform") == 0)
24174664626SKris Kennaway 			{
24274664626SKris Kennaway 			if (--argc < 1) goto bad;
24374664626SKris Kennaway 			keyform=str2fmt(*(++argv));
24474664626SKris Kennaway 			}
24574664626SKris Kennaway 		else if (strcmp(*argv,"-in") == 0)
24674664626SKris Kennaway 			{
24774664626SKris Kennaway 			if (--argc < 1) goto bad;
24874664626SKris Kennaway 			infile= *(++argv);
24974664626SKris Kennaway 			}
25074664626SKris Kennaway 		else if (strcmp(*argv,"-out") == 0)
25174664626SKris Kennaway 			{
25274664626SKris Kennaway 			if (--argc < 1) goto bad;
25374664626SKris Kennaway 			outfile= *(++argv);
25474664626SKris Kennaway 			}
25574664626SKris Kennaway 		else if (strcmp(*argv,"-keyout") == 0)
25674664626SKris Kennaway 			{
25774664626SKris Kennaway 			if (--argc < 1) goto bad;
25874664626SKris Kennaway 			keyout= *(++argv);
25974664626SKris Kennaway 			}
260f579bf8eSKris Kennaway 		else if (strcmp(*argv,"-passin") == 0)
261f579bf8eSKris Kennaway 			{
262f579bf8eSKris Kennaway 			if (--argc < 1) goto bad;
263f579bf8eSKris Kennaway 			passargin= *(++argv);
264f579bf8eSKris Kennaway 			}
265f579bf8eSKris Kennaway 		else if (strcmp(*argv,"-passout") == 0)
266f579bf8eSKris Kennaway 			{
267f579bf8eSKris Kennaway 			if (--argc < 1) goto bad;
268f579bf8eSKris Kennaway 			passargout= *(++argv);
269f579bf8eSKris Kennaway 			}
270ddd58736SKris Kennaway 		else if (strcmp(*argv,"-rand") == 0)
271ddd58736SKris Kennaway 			{
272ddd58736SKris Kennaway 			if (--argc < 1) goto bad;
273ddd58736SKris Kennaway 			inrand= *(++argv);
274ddd58736SKris Kennaway 			}
27574664626SKris Kennaway 		else if (strcmp(*argv,"-newkey") == 0)
27674664626SKris Kennaway 			{
27774664626SKris Kennaway 			int is_numeric;
27874664626SKris Kennaway 
27974664626SKris Kennaway 			if (--argc < 1) goto bad;
28074664626SKris Kennaway 			p= *(++argv);
28174664626SKris Kennaway 			is_numeric = p[0] >= '0' && p[0] <= '9';
28274664626SKris Kennaway 			if (strncmp("rsa:",p,4) == 0 || is_numeric)
28374664626SKris Kennaway 				{
28474664626SKris Kennaway 				pkey_type=TYPE_RSA;
28574664626SKris Kennaway 				if(!is_numeric)
28674664626SKris Kennaway 				    p+=4;
28774664626SKris Kennaway 				newkey= atoi(p);
28874664626SKris Kennaway 				}
28974664626SKris Kennaway 			else
2905c87c606SMark Murray #ifndef OPENSSL_NO_DSA
29174664626SKris Kennaway 				if (strncmp("dsa:",p,4) == 0)
29274664626SKris Kennaway 				{
29374664626SKris Kennaway 				X509 *xtmp=NULL;
29474664626SKris Kennaway 				EVP_PKEY *dtmp;
29574664626SKris Kennaway 
29674664626SKris Kennaway 				pkey_type=TYPE_DSA;
29774664626SKris Kennaway 				p+=4;
29874664626SKris Kennaway 				if ((in=BIO_new_file(p,"r")) == NULL)
29974664626SKris Kennaway 					{
30074664626SKris Kennaway 					perror(p);
30174664626SKris Kennaway 					goto end;
30274664626SKris Kennaway 					}
30374664626SKris Kennaway 				if ((dsa_params=PEM_read_bio_DSAparams(in,NULL,NULL,NULL)) == NULL)
30474664626SKris Kennaway 					{
30574664626SKris Kennaway 					ERR_clear_error();
30674664626SKris Kennaway 					(void)BIO_reset(in);
30774664626SKris Kennaway 					if ((xtmp=PEM_read_bio_X509(in,NULL,NULL,NULL)) == NULL)
30874664626SKris Kennaway 						{
30974664626SKris Kennaway 						BIO_printf(bio_err,"unable to load DSA parameters from file\n");
31074664626SKris Kennaway 						goto end;
31174664626SKris Kennaway 						}
31274664626SKris Kennaway 
313c1803d78SJacques Vidrine 					if ((dtmp=X509_get_pubkey(xtmp)) == NULL) goto end;
31474664626SKris Kennaway 					if (dtmp->type == EVP_PKEY_DSA)
31574664626SKris Kennaway 						dsa_params=DSAparams_dup(dtmp->pkey.dsa);
31674664626SKris Kennaway 					EVP_PKEY_free(dtmp);
31774664626SKris Kennaway 					X509_free(xtmp);
31874664626SKris Kennaway 					if (dsa_params == NULL)
31974664626SKris Kennaway 						{
32074664626SKris Kennaway 						BIO_printf(bio_err,"Certificate does not contain DSA parameters\n");
32174664626SKris Kennaway 						goto end;
32274664626SKris Kennaway 						}
32374664626SKris Kennaway 					}
32474664626SKris Kennaway 				BIO_free(in);
32574664626SKris Kennaway 				newkey=BN_num_bits(dsa_params->p);
32674664626SKris Kennaway 				in=NULL;
32774664626SKris Kennaway 				}
32874664626SKris Kennaway 			else
32974664626SKris Kennaway #endif
3305c87c606SMark Murray #ifndef OPENSSL_NO_DH
33174664626SKris Kennaway 				if (strncmp("dh:",p,4) == 0)
33274664626SKris Kennaway 				{
33374664626SKris Kennaway 				pkey_type=TYPE_DH;
33474664626SKris Kennaway 				p+=3;
33574664626SKris Kennaway 				}
33674664626SKris Kennaway 			else
33774664626SKris Kennaway #endif
33874664626SKris Kennaway 				pkey_type=TYPE_RSA;
33974664626SKris Kennaway 
34074664626SKris Kennaway 			newreq=1;
34174664626SKris Kennaway 			}
3425c87c606SMark Murray 		else if (strcmp(*argv,"-batch") == 0)
3435c87c606SMark Murray 			batch=1;
344f579bf8eSKris Kennaway 		else if (strcmp(*argv,"-newhdr") == 0)
345f579bf8eSKris Kennaway 			newhdr=1;
34674664626SKris Kennaway 		else if (strcmp(*argv,"-modulus") == 0)
34774664626SKris Kennaway 			modulus=1;
34874664626SKris Kennaway 		else if (strcmp(*argv,"-verify") == 0)
34974664626SKris Kennaway 			verify=1;
35074664626SKris Kennaway 		else if (strcmp(*argv,"-nodes") == 0)
35174664626SKris Kennaway 			nodes=1;
35274664626SKris Kennaway 		else if (strcmp(*argv,"-noout") == 0)
35374664626SKris Kennaway 			noout=1;
3545c87c606SMark Murray 		else if (strcmp(*argv,"-verbose") == 0)
3555c87c606SMark Murray 			verbose=1;
3565c87c606SMark Murray 		else if (strcmp(*argv,"-utf8") == 0)
3575c87c606SMark Murray 			chtype = MBSTRING_UTF8;
3585c87c606SMark Murray 		else if (strcmp(*argv,"-nameopt") == 0)
3595c87c606SMark Murray 			{
3605c87c606SMark Murray 			if (--argc < 1) goto bad;
3615c87c606SMark Murray 			if (!set_name_ex(&nmflag, *(++argv))) goto bad;
3625c87c606SMark Murray 			}
3635c87c606SMark Murray 		else if (strcmp(*argv,"-reqopt") == 0)
3645c87c606SMark Murray 			{
3655c87c606SMark Murray 			if (--argc < 1) goto bad;
3665c87c606SMark Murray 			if (!set_cert_ex(&reqflag, *(++argv))) goto bad;
3675c87c606SMark Murray 			}
3685c87c606SMark Murray 		else if (strcmp(*argv,"-subject") == 0)
3695c87c606SMark Murray 			subject=1;
37074664626SKris Kennaway 		else if (strcmp(*argv,"-text") == 0)
37174664626SKris Kennaway 			text=1;
37274664626SKris Kennaway 		else if (strcmp(*argv,"-x509") == 0)
37374664626SKris Kennaway 			x509=1;
37474664626SKris Kennaway 		else if (strcmp(*argv,"-asn1-kludge") == 0)
37574664626SKris Kennaway 			kludge=1;
37674664626SKris Kennaway 		else if (strcmp(*argv,"-no-asn1-kludge") == 0)
37774664626SKris Kennaway 			kludge=0;
3785c87c606SMark Murray 		else if (strcmp(*argv,"-subj") == 0)
3795c87c606SMark Murray 			{
3805c87c606SMark Murray 			if (--argc < 1) goto bad;
3815c87c606SMark Murray 			subj= *(++argv);
3825c87c606SMark Murray 			}
38374664626SKris Kennaway 		else if (strcmp(*argv,"-days") == 0)
38474664626SKris Kennaway 			{
38574664626SKris Kennaway 			if (--argc < 1) goto bad;
38674664626SKris Kennaway 			days= atoi(*(++argv));
38774664626SKris Kennaway 			if (days == 0) days=30;
38874664626SKris Kennaway 			}
3895c87c606SMark Murray 		else if (strcmp(*argv,"-set_serial") == 0)
3905c87c606SMark Murray 			{
3915c87c606SMark Murray 			if (--argc < 1) goto bad;
3925c87c606SMark Murray 			serial = s2i_ASN1_INTEGER(NULL, *(++argv));
3935c87c606SMark Murray 			if (!serial) goto bad;
3945c87c606SMark Murray 			}
39574664626SKris Kennaway 		else if ((md_alg=EVP_get_digestbyname(&((*argv)[1]))) != NULL)
39674664626SKris Kennaway 			{
39774664626SKris Kennaway 			/* ok */
39874664626SKris Kennaway 			digest=md_alg;
39974664626SKris Kennaway 			}
400f579bf8eSKris Kennaway 		else if (strcmp(*argv,"-extensions") == 0)
401f579bf8eSKris Kennaway 			{
402f579bf8eSKris Kennaway 			if (--argc < 1) goto bad;
403f579bf8eSKris Kennaway 			extensions = *(++argv);
404f579bf8eSKris Kennaway 			}
405f579bf8eSKris Kennaway 		else if (strcmp(*argv,"-reqexts") == 0)
406f579bf8eSKris Kennaway 			{
407f579bf8eSKris Kennaway 			if (--argc < 1) goto bad;
408f579bf8eSKris Kennaway 			req_exts = *(++argv);
409f579bf8eSKris Kennaway 			}
41074664626SKris Kennaway 		else
41174664626SKris Kennaway 			{
41274664626SKris Kennaway 			BIO_printf(bio_err,"unknown option %s\n",*argv);
41374664626SKris Kennaway 			badops=1;
41474664626SKris Kennaway 			break;
41574664626SKris Kennaway 			}
41674664626SKris Kennaway 		argc--;
41774664626SKris Kennaway 		argv++;
41874664626SKris Kennaway 		}
41974664626SKris Kennaway 
42074664626SKris Kennaway 	if (badops)
42174664626SKris Kennaway 		{
42274664626SKris Kennaway bad:
42374664626SKris Kennaway 		BIO_printf(bio_err,"%s [options] <infile >outfile\n",prog);
42474664626SKris Kennaway 		BIO_printf(bio_err,"where options  are\n");
425f579bf8eSKris Kennaway 		BIO_printf(bio_err," -inform arg    input format - DER or PEM\n");
426f579bf8eSKris Kennaway 		BIO_printf(bio_err," -outform arg   output format - DER or PEM\n");
42774664626SKris Kennaway 		BIO_printf(bio_err," -in arg        input file\n");
42874664626SKris Kennaway 		BIO_printf(bio_err," -out arg       output file\n");
42974664626SKris Kennaway 		BIO_printf(bio_err," -text          text form of request\n");
4305c87c606SMark Murray 		BIO_printf(bio_err," -pubkey        output public key\n");
43174664626SKris Kennaway 		BIO_printf(bio_err," -noout         do not output REQ\n");
43274664626SKris Kennaway 		BIO_printf(bio_err," -verify        verify signature on REQ\n");
43374664626SKris Kennaway 		BIO_printf(bio_err," -modulus       RSA modulus\n");
43474664626SKris Kennaway 		BIO_printf(bio_err," -nodes         don't encrypt the output key\n");
435fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE
4365c87c606SMark Murray 		BIO_printf(bio_err," -engine e      use engine e, possibly a hardware device\n");
437fceca8a3SJacques Vidrine #endif
4385c87c606SMark Murray 		BIO_printf(bio_err," -subject       output the request's subject\n");
4395c87c606SMark Murray 		BIO_printf(bio_err," -passin        private key password source\n");
44074664626SKris Kennaway 		BIO_printf(bio_err," -key file      use the private key contained in file\n");
44174664626SKris Kennaway 		BIO_printf(bio_err," -keyform arg   key file format\n");
44274664626SKris Kennaway 		BIO_printf(bio_err," -keyout arg    file to send the key to\n");
443ddd58736SKris Kennaway 		BIO_printf(bio_err," -rand file%cfile%c...\n", LIST_SEPARATOR_CHAR, LIST_SEPARATOR_CHAR);
444ddd58736SKris Kennaway 		BIO_printf(bio_err,"                load the file (or the files in the directory) into\n");
445ddd58736SKris Kennaway 		BIO_printf(bio_err,"                the random number generator\n");
44674664626SKris Kennaway 		BIO_printf(bio_err," -newkey rsa:bits generate a new RSA key of 'bits' in size\n");
44774664626SKris Kennaway 		BIO_printf(bio_err," -newkey dsa:file generate a new DSA key, parameters taken from CA in 'file'\n");
448a21b1b38SKris Kennaway 		BIO_printf(bio_err," -[digest]      Digest to sign with (md5, sha1, md2, mdc2, md4)\n");
44974664626SKris Kennaway 		BIO_printf(bio_err," -config file   request template file.\n");
4505c87c606SMark Murray 		BIO_printf(bio_err," -subj arg      set or modify request subject\n");
45174664626SKris Kennaway 		BIO_printf(bio_err," -new           new request.\n");
4525c87c606SMark Murray 		BIO_printf(bio_err," -batch         do not ask anything during request generation\n");
45374664626SKris Kennaway 		BIO_printf(bio_err," -x509          output a x509 structure instead of a cert. req.\n");
4545c87c606SMark Murray 		BIO_printf(bio_err," -days          number of days a certificate generated by -x509 is valid for.\n");
4555c87c606SMark Murray 		BIO_printf(bio_err," -set_serial    serial number to use for a certificate generated by -x509.\n");
456f579bf8eSKris Kennaway 		BIO_printf(bio_err," -newhdr        output \"NEW\" in the header lines\n");
45774664626SKris Kennaway 		BIO_printf(bio_err," -asn1-kludge   Output the 'request' in a format that is wrong but some CA's\n");
45874664626SKris Kennaway 		BIO_printf(bio_err,"                have been reported as requiring\n");
459f579bf8eSKris Kennaway 		BIO_printf(bio_err," -extensions .. specify certificate extension section (override value in config file)\n");
460f579bf8eSKris Kennaway 		BIO_printf(bio_err," -reqexts ..    specify request extension section (override value in config file)\n");
4615c87c606SMark Murray 		BIO_printf(bio_err," -utf8          input characters are UTF8 (default ASCII)\n");
4625c87c606SMark Murray 		BIO_printf(bio_err," -nameopt arg    - various certificate name options\n");
4635c87c606SMark Murray 		BIO_printf(bio_err," -reqopt arg    - various request text options\n\n");
46474664626SKris Kennaway 		goto end;
46574664626SKris Kennaway 		}
46674664626SKris Kennaway 
46774664626SKris Kennaway 	ERR_load_crypto_strings();
468f579bf8eSKris Kennaway 	if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
469f579bf8eSKris Kennaway 		BIO_printf(bio_err, "Error getting passwords\n");
470f579bf8eSKris Kennaway 		goto end;
471f579bf8eSKris Kennaway 	}
47274664626SKris Kennaway 
473f579bf8eSKris Kennaway #ifndef MONOLITH /* else this has happened in openssl.c (global `config') */
47474664626SKris Kennaway 	/* Lets load up our environment a little */
47574664626SKris Kennaway 	p=getenv("OPENSSL_CONF");
47674664626SKris Kennaway 	if (p == NULL)
47774664626SKris Kennaway 		p=getenv("SSLEAY_CONF");
47874664626SKris Kennaway 	if (p == NULL)
4795c87c606SMark Murray 		p=to_free=make_config_name();
48074664626SKris Kennaway 	default_config_file=p;
4815c87c606SMark Murray 	config=NCONF_new(NULL);
4825c87c606SMark Murray 	i=NCONF_load(config, p, &errline);
48374664626SKris Kennaway #endif
48474664626SKris Kennaway 
48574664626SKris Kennaway 	if (template != NULL)
48674664626SKris Kennaway 		{
4875c87c606SMark Murray 		long errline = -1;
48874664626SKris Kennaway 
4895c87c606SMark Murray 		if( verbose )
49074664626SKris Kennaway 			BIO_printf(bio_err,"Using configuration from %s\n",template);
4915c87c606SMark Murray 		req_conf=NCONF_new(NULL);
4925c87c606SMark Murray 		i=NCONF_load(req_conf,template,&errline);
4935c87c606SMark Murray 		if (i == 0)
49474664626SKris Kennaway 			{
49574664626SKris Kennaway 			BIO_printf(bio_err,"error on line %ld of %s\n",errline,template);
49674664626SKris Kennaway 			goto end;
49774664626SKris Kennaway 			}
49874664626SKris Kennaway 		}
49974664626SKris Kennaway 	else
50074664626SKris Kennaway 		{
50174664626SKris Kennaway 		req_conf=config;
5025c87c606SMark Murray 		if( verbose )
50374664626SKris Kennaway 			BIO_printf(bio_err,"Using configuration from %s\n",
50474664626SKris Kennaway 			default_config_file);
50574664626SKris Kennaway 		if (req_conf == NULL)
50674664626SKris Kennaway 			{
50774664626SKris Kennaway 			BIO_printf(bio_err,"Unable to load config info\n");
50874664626SKris Kennaway 			}
50974664626SKris Kennaway 		}
51074664626SKris Kennaway 
51174664626SKris Kennaway 	if (req_conf != NULL)
51274664626SKris Kennaway 		{
5135c87c606SMark Murray 		if (!load_config(bio_err, req_conf))
5145c87c606SMark Murray 			goto end;
5155c87c606SMark Murray 		p=NCONF_get_string(req_conf,NULL,"oid_file");
5165c87c606SMark Murray 		if (p == NULL)
5175c87c606SMark Murray 			ERR_clear_error();
51874664626SKris Kennaway 		if (p != NULL)
51974664626SKris Kennaway 			{
52074664626SKris Kennaway 			BIO *oid_bio;
52174664626SKris Kennaway 
52274664626SKris Kennaway 			oid_bio=BIO_new_file(p,"r");
52374664626SKris Kennaway 			if (oid_bio == NULL)
52474664626SKris Kennaway 				{
52574664626SKris Kennaway 				/*
52674664626SKris Kennaway 				BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
52774664626SKris Kennaway 				ERR_print_errors(bio_err);
52874664626SKris Kennaway 				*/
52974664626SKris Kennaway 				}
53074664626SKris Kennaway 			else
53174664626SKris Kennaway 				{
53274664626SKris Kennaway 				OBJ_create_objects(oid_bio);
53374664626SKris Kennaway 				BIO_free(oid_bio);
53474664626SKris Kennaway 				}
53574664626SKris Kennaway 			}
53674664626SKris Kennaway 		}
537ddd58736SKris Kennaway 	if(!add_oid_section(bio_err, req_conf)) goto end;
53874664626SKris Kennaway 
5395c87c606SMark Murray 	if (md_alg == NULL)
5405c87c606SMark Murray 		{
5415c87c606SMark Murray 		p=NCONF_get_string(req_conf,SECTION,"default_md");
5425c87c606SMark Murray 		if (p == NULL)
5435c87c606SMark Murray 			ERR_clear_error();
5445c87c606SMark Murray 		if (p != NULL)
54574664626SKris Kennaway 			{
54674664626SKris Kennaway 			if ((md_alg=EVP_get_digestbyname(p)) != NULL)
54774664626SKris Kennaway 				digest=md_alg;
54874664626SKris Kennaway 			}
5495c87c606SMark Murray 		}
55074664626SKris Kennaway 
551f579bf8eSKris Kennaway 	if (!extensions)
5525c87c606SMark Murray 		{
5535c87c606SMark Murray 		extensions = NCONF_get_string(req_conf, SECTION, V3_EXTENSIONS);
5545c87c606SMark Murray 		if (!extensions)
5555c87c606SMark Murray 			ERR_clear_error();
5565c87c606SMark Murray 		}
55774664626SKris Kennaway 	if (extensions) {
55874664626SKris Kennaway 		/* Check syntax of file */
55974664626SKris Kennaway 		X509V3_CTX ctx;
56074664626SKris Kennaway 		X509V3_set_ctx_test(&ctx);
5615c87c606SMark Murray 		X509V3_set_nconf(&ctx, req_conf);
5625c87c606SMark Murray 		if(!X509V3_EXT_add_nconf(req_conf, &ctx, extensions, NULL)) {
56374664626SKris Kennaway 			BIO_printf(bio_err,
56474664626SKris Kennaway 			 "Error Loading extension section %s\n", extensions);
56574664626SKris Kennaway 			goto end;
56674664626SKris Kennaway 		}
56774664626SKris Kennaway 	}
56874664626SKris Kennaway 
569f579bf8eSKris Kennaway 	if(!passin)
5705c87c606SMark Murray 		{
5715c87c606SMark Murray 		passin = NCONF_get_string(req_conf, SECTION, "input_password");
5725c87c606SMark Murray 		if (!passin)
5735c87c606SMark Murray 			ERR_clear_error();
5745c87c606SMark Murray 		}
575f579bf8eSKris Kennaway 
576f579bf8eSKris Kennaway 	if(!passout)
5775c87c606SMark Murray 		{
5785c87c606SMark Murray 		passout = NCONF_get_string(req_conf, SECTION, "output_password");
5795c87c606SMark Murray 		if (!passout)
5805c87c606SMark Murray 			ERR_clear_error();
5815c87c606SMark Murray 		}
582f579bf8eSKris Kennaway 
5835c87c606SMark Murray 	p = NCONF_get_string(req_conf, SECTION, STRING_MASK);
5845c87c606SMark Murray 	if (!p)
5855c87c606SMark Murray 		ERR_clear_error();
586f579bf8eSKris Kennaway 
587f579bf8eSKris Kennaway 	if(p && !ASN1_STRING_set_default_mask_asc(p)) {
588f579bf8eSKris Kennaway 		BIO_printf(bio_err, "Invalid global string mask setting %s\n", p);
589f579bf8eSKris Kennaway 		goto end;
590f579bf8eSKris Kennaway 	}
591f579bf8eSKris Kennaway 
5925c87c606SMark Murray 	if (chtype != MBSTRING_UTF8)
5935c87c606SMark Murray 		{
5945c87c606SMark Murray 		p = NCONF_get_string(req_conf, SECTION, UTF8_IN);
5955c87c606SMark Murray 		if (!p)
5965c87c606SMark Murray 			ERR_clear_error();
5975c87c606SMark Murray 		else if (!strcmp(p, "yes"))
5985c87c606SMark Murray 			chtype = MBSTRING_UTF8;
5995c87c606SMark Murray 		}
6005c87c606SMark Murray 
6015c87c606SMark Murray 
602f579bf8eSKris Kennaway 	if(!req_exts)
6035c87c606SMark Murray 		{
6045c87c606SMark Murray 		req_exts = NCONF_get_string(req_conf, SECTION, REQ_EXTENSIONS);
6055c87c606SMark Murray 		if (!req_exts)
6065c87c606SMark Murray 			ERR_clear_error();
6075c87c606SMark Murray 		}
608f579bf8eSKris Kennaway 	if(req_exts) {
609f579bf8eSKris Kennaway 		/* Check syntax of file */
610f579bf8eSKris Kennaway 		X509V3_CTX ctx;
611f579bf8eSKris Kennaway 		X509V3_set_ctx_test(&ctx);
6125c87c606SMark Murray 		X509V3_set_nconf(&ctx, req_conf);
6135c87c606SMark Murray 		if(!X509V3_EXT_add_nconf(req_conf, &ctx, req_exts, NULL)) {
614f579bf8eSKris Kennaway 			BIO_printf(bio_err,
615f579bf8eSKris Kennaway 			 "Error Loading request extension section %s\n",
616f579bf8eSKris Kennaway 								req_exts);
617f579bf8eSKris Kennaway 			goto end;
618f579bf8eSKris Kennaway 		}
619f579bf8eSKris Kennaway 	}
620f579bf8eSKris Kennaway 
62174664626SKris Kennaway 	in=BIO_new(BIO_s_file());
62274664626SKris Kennaway 	out=BIO_new(BIO_s_file());
62374664626SKris Kennaway 	if ((in == NULL) || (out == NULL))
62474664626SKris Kennaway 		goto end;
62574664626SKris Kennaway 
626fceca8a3SJacques Vidrine #ifndef OPENSSL_NO_ENGINE
6275c87c606SMark Murray         e = setup_engine(bio_err, engine, 0);
628fceca8a3SJacques Vidrine #endif
6295c87c606SMark Murray 
63074664626SKris Kennaway 	if (keyfile != NULL)
63174664626SKris Kennaway 		{
6325c87c606SMark Murray 		pkey = load_key(bio_err, keyfile, keyform, 0, passin, e,
6335c87c606SMark Murray 			"Private Key");
6345c87c606SMark Murray 		if (!pkey)
63574664626SKris Kennaway 			{
6365c87c606SMark Murray 			/* load_key() has already printed an appropriate
6375c87c606SMark Murray 			   message */
63874664626SKris Kennaway 			goto end;
63974664626SKris Kennaway 			}
640ddd58736SKris Kennaway 		if (EVP_PKEY_type(pkey->type) == EVP_PKEY_DSA)
641ddd58736SKris Kennaway 			{
6425c87c606SMark Murray 			char *randfile = NCONF_get_string(req_conf,SECTION,"RANDFILE");
6435c87c606SMark Murray 			if (randfile == NULL)
6445c87c606SMark Murray 				ERR_clear_error();
645ddd58736SKris Kennaway 			app_RAND_load_file(randfile, bio_err, 0);
646ddd58736SKris Kennaway 			}
64774664626SKris Kennaway 		}
64874664626SKris Kennaway 
64974664626SKris Kennaway 	if (newreq && (pkey == NULL))
65074664626SKris Kennaway 		{
6515c87c606SMark Murray 		char *randfile = NCONF_get_string(req_conf,SECTION,"RANDFILE");
6525c87c606SMark Murray 		if (randfile == NULL)
6535c87c606SMark Murray 			ERR_clear_error();
654f579bf8eSKris Kennaway 		app_RAND_load_file(randfile, bio_err, 0);
655ddd58736SKris Kennaway 		if (inrand)
656ddd58736SKris Kennaway 			app_RAND_load_files(inrand);
65774664626SKris Kennaway 
65874664626SKris Kennaway 		if (newkey <= 0)
65974664626SKris Kennaway 			{
6605c87c606SMark Murray 			if (!NCONF_get_number(req_conf,SECTION,BITS, &newkey))
66174664626SKris Kennaway 				newkey=DEFAULT_KEY_LENGTH;
66274664626SKris Kennaway 			}
66374664626SKris Kennaway 
66474664626SKris Kennaway 		if (newkey < MIN_KEY_LENGTH)
66574664626SKris Kennaway 			{
66674664626SKris Kennaway 			BIO_printf(bio_err,"private key length is too short,\n");
66774664626SKris Kennaway 			BIO_printf(bio_err,"it needs to be at least %d bits, not %d\n",MIN_KEY_LENGTH,newkey);
66874664626SKris Kennaway 			goto end;
66974664626SKris Kennaway 			}
67074664626SKris Kennaway 		BIO_printf(bio_err,"Generating a %d bit %s private key\n",
67174664626SKris Kennaway 			newkey,(pkey_type == TYPE_RSA)?"RSA":"DSA");
67274664626SKris Kennaway 
67374664626SKris Kennaway 		if ((pkey=EVP_PKEY_new()) == NULL) goto end;
67474664626SKris Kennaway 
6755c87c606SMark Murray #ifndef OPENSSL_NO_RSA
67674664626SKris Kennaway 		if (pkey_type == TYPE_RSA)
67774664626SKris Kennaway 			{
67874664626SKris Kennaway 			if (!EVP_PKEY_assign_RSA(pkey,
67974664626SKris Kennaway 				RSA_generate_key(newkey,0x10001,
68074664626SKris Kennaway 					req_cb,bio_err)))
68174664626SKris Kennaway 				goto end;
68274664626SKris Kennaway 			}
68374664626SKris Kennaway 		else
68474664626SKris Kennaway #endif
6855c87c606SMark Murray #ifndef OPENSSL_NO_DSA
68674664626SKris Kennaway 			if (pkey_type == TYPE_DSA)
68774664626SKris Kennaway 			{
68874664626SKris Kennaway 			if (!DSA_generate_key(dsa_params)) goto end;
68974664626SKris Kennaway 			if (!EVP_PKEY_assign_DSA(pkey,dsa_params)) goto end;
69074664626SKris Kennaway 			dsa_params=NULL;
69174664626SKris Kennaway 			}
69274664626SKris Kennaway #endif
69374664626SKris Kennaway 
694f579bf8eSKris Kennaway 		app_RAND_write_file(randfile, bio_err);
69574664626SKris Kennaway 
69674664626SKris Kennaway 		if (pkey == NULL) goto end;
69774664626SKris Kennaway 
69874664626SKris Kennaway 		if (keyout == NULL)
6995c87c606SMark Murray 			{
7005c87c606SMark Murray 			keyout=NCONF_get_string(req_conf,SECTION,KEYFILE);
7015c87c606SMark Murray 			if (keyout == NULL)
7025c87c606SMark Murray 				ERR_clear_error();
7035c87c606SMark Murray 			}
70474664626SKris Kennaway 
70574664626SKris Kennaway 		if (keyout == NULL)
70674664626SKris Kennaway 			{
70774664626SKris Kennaway 			BIO_printf(bio_err,"writing new private key to stdout\n");
70874664626SKris Kennaway 			BIO_set_fp(out,stdout,BIO_NOCLOSE);
7095c87c606SMark Murray #ifdef OPENSSL_SYS_VMS
710ddd58736SKris Kennaway 			{
711ddd58736SKris Kennaway 			BIO *tmpbio = BIO_new(BIO_f_linebuffer());
712ddd58736SKris Kennaway 			out = BIO_push(tmpbio, out);
713ddd58736SKris Kennaway 			}
714ddd58736SKris Kennaway #endif
71574664626SKris Kennaway 			}
71674664626SKris Kennaway 		else
71774664626SKris Kennaway 			{
71874664626SKris Kennaway 			BIO_printf(bio_err,"writing new private key to '%s'\n",keyout);
71974664626SKris Kennaway 			if (BIO_write_filename(out,keyout) <= 0)
72074664626SKris Kennaway 				{
72174664626SKris Kennaway 				perror(keyout);
72274664626SKris Kennaway 				goto end;
72374664626SKris Kennaway 				}
72474664626SKris Kennaway 			}
72574664626SKris Kennaway 
7265c87c606SMark Murray 		p=NCONF_get_string(req_conf,SECTION,"encrypt_rsa_key");
72774664626SKris Kennaway 		if (p == NULL)
7285c87c606SMark Murray 			{
7295c87c606SMark Murray 			ERR_clear_error();
7305c87c606SMark Murray 			p=NCONF_get_string(req_conf,SECTION,"encrypt_key");
7315c87c606SMark Murray 			if (p == NULL)
7325c87c606SMark Murray 				ERR_clear_error();
7335c87c606SMark Murray 			}
73474664626SKris Kennaway 		if ((p != NULL) && (strcmp(p,"no") == 0))
73574664626SKris Kennaway 			cipher=NULL;
73674664626SKris Kennaway 		if (nodes) cipher=NULL;
73774664626SKris Kennaway 
73874664626SKris Kennaway 		i=0;
73974664626SKris Kennaway loop:
74074664626SKris Kennaway 		if (!PEM_write_bio_PrivateKey(out,pkey,cipher,
741f579bf8eSKris Kennaway 			NULL,0,NULL,passout))
74274664626SKris Kennaway 			{
74374664626SKris Kennaway 			if ((ERR_GET_REASON(ERR_peek_error()) ==
74474664626SKris Kennaway 				PEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3))
74574664626SKris Kennaway 				{
74674664626SKris Kennaway 				ERR_clear_error();
74774664626SKris Kennaway 				i++;
74874664626SKris Kennaway 				goto loop;
74974664626SKris Kennaway 				}
75074664626SKris Kennaway 			goto end;
75174664626SKris Kennaway 			}
75274664626SKris Kennaway 		BIO_printf(bio_err,"-----\n");
75374664626SKris Kennaway 		}
75474664626SKris Kennaway 
75574664626SKris Kennaway 	if (!newreq)
75674664626SKris Kennaway 		{
75774664626SKris Kennaway 		/* Since we are using a pre-existing certificate
75874664626SKris Kennaway 		 * request, the kludge 'format' info should not be
75974664626SKris Kennaway 		 * changed. */
76074664626SKris Kennaway 		kludge= -1;
76174664626SKris Kennaway 		if (infile == NULL)
76274664626SKris Kennaway 			BIO_set_fp(in,stdin,BIO_NOCLOSE);
76374664626SKris Kennaway 		else
76474664626SKris Kennaway 			{
76574664626SKris Kennaway 			if (BIO_read_filename(in,infile) <= 0)
76674664626SKris Kennaway 				{
76774664626SKris Kennaway 				perror(infile);
76874664626SKris Kennaway 				goto end;
76974664626SKris Kennaway 				}
77074664626SKris Kennaway 			}
77174664626SKris Kennaway 
77274664626SKris Kennaway 		if	(informat == FORMAT_ASN1)
77374664626SKris Kennaway 			req=d2i_X509_REQ_bio(in,NULL);
77474664626SKris Kennaway 		else if (informat == FORMAT_PEM)
77574664626SKris Kennaway 			req=PEM_read_bio_X509_REQ(in,NULL,NULL,NULL);
77674664626SKris Kennaway 		else
77774664626SKris Kennaway 			{
77874664626SKris Kennaway 			BIO_printf(bio_err,"bad input format specified for X509 request\n");
77974664626SKris Kennaway 			goto end;
78074664626SKris Kennaway 			}
78174664626SKris Kennaway 		if (req == NULL)
78274664626SKris Kennaway 			{
78374664626SKris Kennaway 			BIO_printf(bio_err,"unable to load X509 request\n");
78474664626SKris Kennaway 			goto end;
78574664626SKris Kennaway 			}
78674664626SKris Kennaway 		}
78774664626SKris Kennaway 
78874664626SKris Kennaway 	if (newreq || x509)
78974664626SKris Kennaway 		{
79074664626SKris Kennaway 		if (pkey == NULL)
79174664626SKris Kennaway 			{
79274664626SKris Kennaway 			BIO_printf(bio_err,"you need to specify a private key\n");
79374664626SKris Kennaway 			goto end;
79474664626SKris Kennaway 			}
7955c87c606SMark Murray #ifndef OPENSSL_NO_DSA
796de7cdddaSKris Kennaway 		if (pkey->type == EVP_PKEY_DSA)
797de7cdddaSKris Kennaway 			digest=EVP_dss1();
798de7cdddaSKris Kennaway #endif
79974664626SKris Kennaway 		if (req == NULL)
80074664626SKris Kennaway 			{
80174664626SKris Kennaway 			req=X509_REQ_new();
80274664626SKris Kennaway 			if (req == NULL)
80374664626SKris Kennaway 				{
80474664626SKris Kennaway 				goto end;
80574664626SKris Kennaway 				}
80674664626SKris Kennaway 
8075c87c606SMark Murray 			i=make_REQ(req,pkey,subj,!x509, chtype);
8085c87c606SMark Murray 			subj=NULL; /* done processing '-subj' option */
8095c87c606SMark Murray 			if ((kludge > 0) && !sk_X509_ATTRIBUTE_num(req->req_info->attributes))
8105c87c606SMark Murray 				{
8115c87c606SMark Murray 				sk_X509_ATTRIBUTE_free(req->req_info->attributes);
8125c87c606SMark Murray 				req->req_info->attributes = NULL;
8135c87c606SMark Murray 				}
81474664626SKris Kennaway 			if (!i)
81574664626SKris Kennaway 				{
81674664626SKris Kennaway 				BIO_printf(bio_err,"problems making Certificate Request\n");
81774664626SKris Kennaway 				goto end;
81874664626SKris Kennaway 				}
81974664626SKris Kennaway 			}
82074664626SKris Kennaway 		if (x509)
82174664626SKris Kennaway 			{
82274664626SKris Kennaway 			EVP_PKEY *tmppkey;
82374664626SKris Kennaway 			X509V3_CTX ext_ctx;
82474664626SKris Kennaway 			if ((x509ss=X509_new()) == NULL) goto end;
82574664626SKris Kennaway 
82674664626SKris Kennaway 			/* Set version to V3 */
827ced566fdSJacques Vidrine 			if(extensions && !X509_set_version(x509ss, 2)) goto end;
8285c87c606SMark Murray 			if (serial)
8295c87c606SMark Murray 				{
8305c87c606SMark Murray 				if (!X509_set_serialNumber(x509ss, serial)) goto end;
8315c87c606SMark Murray 				}
8325c87c606SMark Murray 			else
8335c87c606SMark Murray 				{
834c1803d78SJacques Vidrine 				if (!ASN1_INTEGER_set(X509_get_serialNumber(x509ss),0L)) goto end;
8355c87c606SMark Murray 				}
83674664626SKris Kennaway 
837c1803d78SJacques Vidrine 			if (!X509_set_issuer_name(x509ss, X509_REQ_get_subject_name(req))) goto end;
838c1803d78SJacques Vidrine 			if (!X509_gmtime_adj(X509_get_notBefore(x509ss),0)) goto end;
839c1803d78SJacques Vidrine 			if (!X509_gmtime_adj(X509_get_notAfter(x509ss), (long)60*60*24*days)) goto end;
840c1803d78SJacques Vidrine 			if (!X509_set_subject_name(x509ss, X509_REQ_get_subject_name(req))) goto end;
84174664626SKris Kennaway 			tmppkey = X509_REQ_get_pubkey(req);
842c1803d78SJacques Vidrine 			if (!tmppkey || !X509_set_pubkey(x509ss,tmppkey)) goto end;
84374664626SKris Kennaway 			EVP_PKEY_free(tmppkey);
84474664626SKris Kennaway 
84574664626SKris Kennaway 			/* Set up V3 context struct */
84674664626SKris Kennaway 
84774664626SKris Kennaway 			X509V3_set_ctx(&ext_ctx, x509ss, x509ss, NULL, NULL, 0);
8485c87c606SMark Murray 			X509V3_set_nconf(&ext_ctx, req_conf);
84974664626SKris Kennaway 
85074664626SKris Kennaway 			/* Add extensions */
8515c87c606SMark Murray 			if(extensions && !X509V3_EXT_add_nconf(req_conf,
85274664626SKris Kennaway 				 	&ext_ctx, extensions, x509ss))
85374664626SKris Kennaway 				{
85474664626SKris Kennaway 				BIO_printf(bio_err,
85574664626SKris Kennaway 					"Error Loading extension section %s\n",
85674664626SKris Kennaway 					extensions);
85774664626SKris Kennaway 				goto end;
85874664626SKris Kennaway 				}
85974664626SKris Kennaway 
86074664626SKris Kennaway 			if (!(i=X509_sign(x509ss,pkey,digest)))
86174664626SKris Kennaway 				goto end;
86274664626SKris Kennaway 			}
86374664626SKris Kennaway 		else
86474664626SKris Kennaway 			{
865f579bf8eSKris Kennaway 			X509V3_CTX ext_ctx;
866f579bf8eSKris Kennaway 
867f579bf8eSKris Kennaway 			/* Set up V3 context struct */
868f579bf8eSKris Kennaway 
869f579bf8eSKris Kennaway 			X509V3_set_ctx(&ext_ctx, NULL, NULL, req, NULL, 0);
8705c87c606SMark Murray 			X509V3_set_nconf(&ext_ctx, req_conf);
871f579bf8eSKris Kennaway 
872f579bf8eSKris Kennaway 			/* Add extensions */
8735c87c606SMark Murray 			if(req_exts && !X509V3_EXT_REQ_add_nconf(req_conf,
874f579bf8eSKris Kennaway 				 	&ext_ctx, req_exts, req))
875f579bf8eSKris Kennaway 				{
876f579bf8eSKris Kennaway 				BIO_printf(bio_err,
877f579bf8eSKris Kennaway 					"Error Loading extension section %s\n",
878f579bf8eSKris Kennaway 					req_exts);
879f579bf8eSKris Kennaway 				goto end;
880f579bf8eSKris Kennaway 				}
88174664626SKris Kennaway 			if (!(i=X509_REQ_sign(req,pkey,digest)))
88274664626SKris Kennaway 				goto end;
88374664626SKris Kennaway 			}
88474664626SKris Kennaway 		}
88574664626SKris Kennaway 
8865c87c606SMark Murray 	if (subj && x509)
8875c87c606SMark Murray 		{
8885c87c606SMark Murray 		BIO_printf(bio_err, "Cannot modifiy certificate subject\n");
8895c87c606SMark Murray 		goto end;
8905c87c606SMark Murray 		}
8915c87c606SMark Murray 
8925c87c606SMark Murray 	if (subj && !x509)
8935c87c606SMark Murray 		{
8945c87c606SMark Murray 		if (verbose)
8955c87c606SMark Murray 			{
8965c87c606SMark Murray 			BIO_printf(bio_err, "Modifying Request's Subject\n");
8975c87c606SMark Murray 			print_name(bio_err, "old subject=", X509_REQ_get_subject_name(req), nmflag);
8985c87c606SMark Murray 			}
8995c87c606SMark Murray 
9005c87c606SMark Murray 		if (build_subject(req, subj, chtype) == 0)
9015c87c606SMark Murray 			{
9025c87c606SMark Murray 			BIO_printf(bio_err, "ERROR: cannot modify subject\n");
9035c87c606SMark Murray 			ex=1;
9045c87c606SMark Murray 			goto end;
9055c87c606SMark Murray 			}
9065c87c606SMark Murray 
9075c87c606SMark Murray 		req->req_info->enc.modified = 1;
9085c87c606SMark Murray 
9095c87c606SMark Murray 		if (verbose)
9105c87c606SMark Murray 			{
9115c87c606SMark Murray 			print_name(bio_err, "new subject=", X509_REQ_get_subject_name(req), nmflag);
9125c87c606SMark Murray 			}
9135c87c606SMark Murray 		}
9145c87c606SMark Murray 
91574664626SKris Kennaway 	if (verify && !x509)
91674664626SKris Kennaway 		{
91774664626SKris Kennaway 		int tmp=0;
91874664626SKris Kennaway 
91974664626SKris Kennaway 		if (pkey == NULL)
92074664626SKris Kennaway 			{
92174664626SKris Kennaway 			pkey=X509_REQ_get_pubkey(req);
92274664626SKris Kennaway 			tmp=1;
92374664626SKris Kennaway 			if (pkey == NULL) goto end;
92474664626SKris Kennaway 			}
92574664626SKris Kennaway 
92674664626SKris Kennaway 		i=X509_REQ_verify(req,pkey);
92774664626SKris Kennaway 		if (tmp) {
92874664626SKris Kennaway 			EVP_PKEY_free(pkey);
92974664626SKris Kennaway 			pkey=NULL;
93074664626SKris Kennaway 		}
93174664626SKris Kennaway 
93274664626SKris Kennaway 		if (i < 0)
93374664626SKris Kennaway 			{
93474664626SKris Kennaway 			goto end;
93574664626SKris Kennaway 			}
93674664626SKris Kennaway 		else if (i == 0)
93774664626SKris Kennaway 			{
93874664626SKris Kennaway 			BIO_printf(bio_err,"verify failure\n");
9395c87c606SMark Murray 			ERR_print_errors(bio_err);
94074664626SKris Kennaway 			}
94174664626SKris Kennaway 		else /* if (i > 0) */
94274664626SKris Kennaway 			BIO_printf(bio_err,"verify OK\n");
94374664626SKris Kennaway 		}
94474664626SKris Kennaway 
9455c87c606SMark Murray 	if (noout && !text && !modulus && !subject && !pubkey)
94674664626SKris Kennaway 		{
94774664626SKris Kennaway 		ex=0;
94874664626SKris Kennaway 		goto end;
94974664626SKris Kennaway 		}
95074664626SKris Kennaway 
95174664626SKris Kennaway 	if (outfile == NULL)
952ddd58736SKris Kennaway 		{
95374664626SKris Kennaway 		BIO_set_fp(out,stdout,BIO_NOCLOSE);
9545c87c606SMark Murray #ifdef OPENSSL_SYS_VMS
955ddd58736SKris Kennaway 		{
956ddd58736SKris Kennaway 		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
957ddd58736SKris Kennaway 		out = BIO_push(tmpbio, out);
958ddd58736SKris Kennaway 		}
959ddd58736SKris Kennaway #endif
960ddd58736SKris Kennaway 		}
96174664626SKris Kennaway 	else
96274664626SKris Kennaway 		{
96374664626SKris Kennaway 		if ((keyout != NULL) && (strcmp(outfile,keyout) == 0))
96474664626SKris Kennaway 			i=(int)BIO_append_filename(out,outfile);
96574664626SKris Kennaway 		else
96674664626SKris Kennaway 			i=(int)BIO_write_filename(out,outfile);
96774664626SKris Kennaway 		if (!i)
96874664626SKris Kennaway 			{
96974664626SKris Kennaway 			perror(outfile);
97074664626SKris Kennaway 			goto end;
97174664626SKris Kennaway 			}
97274664626SKris Kennaway 		}
97374664626SKris Kennaway 
9745c87c606SMark Murray 	if (pubkey)
9755c87c606SMark Murray 		{
9765c87c606SMark Murray 		EVP_PKEY *tpubkey;
9775c87c606SMark Murray 		tpubkey=X509_REQ_get_pubkey(req);
9785c87c606SMark Murray 		if (tpubkey == NULL)
9795c87c606SMark Murray 			{
9805c87c606SMark Murray 			BIO_printf(bio_err,"Error getting public key\n");
9815c87c606SMark Murray 			ERR_print_errors(bio_err);
9825c87c606SMark Murray 			goto end;
9835c87c606SMark Murray 			}
9845c87c606SMark Murray 		PEM_write_bio_PUBKEY(out, tpubkey);
9855c87c606SMark Murray 		EVP_PKEY_free(tpubkey);
9865c87c606SMark Murray 		}
9875c87c606SMark Murray 
98874664626SKris Kennaway 	if (text)
98974664626SKris Kennaway 		{
99074664626SKris Kennaway 		if (x509)
9915c87c606SMark Murray 			X509_print_ex(out, x509ss, nmflag, reqflag);
99274664626SKris Kennaway 		else
9935c87c606SMark Murray 			X509_REQ_print_ex(out, req, nmflag, reqflag);
9945c87c606SMark Murray 		}
9955c87c606SMark Murray 
9965c87c606SMark Murray 	if(subject)
9975c87c606SMark Murray 		{
9985c87c606SMark Murray 		if(x509)
9995c87c606SMark Murray 			print_name(out, "subject=", X509_get_subject_name(x509ss), nmflag);
10005c87c606SMark Murray 		else
10015c87c606SMark Murray 			print_name(out, "subject=", X509_REQ_get_subject_name(req), nmflag);
100274664626SKris Kennaway 		}
100374664626SKris Kennaway 
100474664626SKris Kennaway 	if (modulus)
100574664626SKris Kennaway 		{
10065c87c606SMark Murray 		EVP_PKEY *tpubkey;
100774664626SKris Kennaway 
100874664626SKris Kennaway 		if (x509)
10095c87c606SMark Murray 			tpubkey=X509_get_pubkey(x509ss);
101074664626SKris Kennaway 		else
10115c87c606SMark Murray 			tpubkey=X509_REQ_get_pubkey(req);
10125c87c606SMark Murray 		if (tpubkey == NULL)
101374664626SKris Kennaway 			{
101474664626SKris Kennaway 			fprintf(stdout,"Modulus=unavailable\n");
101574664626SKris Kennaway 			goto end;
101674664626SKris Kennaway 			}
101774664626SKris Kennaway 		fprintf(stdout,"Modulus=");
10185c87c606SMark Murray #ifndef OPENSSL_NO_RSA
10195c87c606SMark Murray 		if (tpubkey->type == EVP_PKEY_RSA)
10205c87c606SMark Murray 			BN_print(out,tpubkey->pkey.rsa->n);
102174664626SKris Kennaway 		else
102274664626SKris Kennaway #endif
102374664626SKris Kennaway 			fprintf(stdout,"Wrong Algorithm type");
10245c87c606SMark Murray 		EVP_PKEY_free(tpubkey);
102574664626SKris Kennaway 		fprintf(stdout,"\n");
102674664626SKris Kennaway 		}
102774664626SKris Kennaway 
102874664626SKris Kennaway 	if (!noout && !x509)
102974664626SKris Kennaway 		{
103074664626SKris Kennaway 		if 	(outformat == FORMAT_ASN1)
103174664626SKris Kennaway 			i=i2d_X509_REQ_bio(out,req);
1032f579bf8eSKris Kennaway 		else if (outformat == FORMAT_PEM) {
1033f579bf8eSKris Kennaway 			if(newhdr) i=PEM_write_bio_X509_REQ_NEW(out,req);
1034f579bf8eSKris Kennaway 			else i=PEM_write_bio_X509_REQ(out,req);
1035f579bf8eSKris Kennaway 		} else {
103674664626SKris Kennaway 			BIO_printf(bio_err,"bad output format specified for outfile\n");
103774664626SKris Kennaway 			goto end;
103874664626SKris Kennaway 			}
103974664626SKris Kennaway 		if (!i)
104074664626SKris Kennaway 			{
104174664626SKris Kennaway 			BIO_printf(bio_err,"unable to write X509 request\n");
104274664626SKris Kennaway 			goto end;
104374664626SKris Kennaway 			}
104474664626SKris Kennaway 		}
104574664626SKris Kennaway 	if (!noout && x509 && (x509ss != NULL))
104674664626SKris Kennaway 		{
104774664626SKris Kennaway 		if 	(outformat == FORMAT_ASN1)
104874664626SKris Kennaway 			i=i2d_X509_bio(out,x509ss);
104974664626SKris Kennaway 		else if (outformat == FORMAT_PEM)
105074664626SKris Kennaway 			i=PEM_write_bio_X509(out,x509ss);
105174664626SKris Kennaway 		else	{
105274664626SKris Kennaway 			BIO_printf(bio_err,"bad output format specified for outfile\n");
105374664626SKris Kennaway 			goto end;
105474664626SKris Kennaway 			}
105574664626SKris Kennaway 		if (!i)
105674664626SKris Kennaway 			{
105774664626SKris Kennaway 			BIO_printf(bio_err,"unable to write X509 certificate\n");
105874664626SKris Kennaway 			goto end;
105974664626SKris Kennaway 			}
106074664626SKris Kennaway 		}
106174664626SKris Kennaway 	ex=0;
106274664626SKris Kennaway end:
10635c87c606SMark Murray #ifndef MONOLITH
10645c87c606SMark Murray 	if(to_free)
10655c87c606SMark Murray 		OPENSSL_free(to_free);
10665c87c606SMark Murray #endif
106774664626SKris Kennaway 	if (ex)
106874664626SKris Kennaway 		{
106974664626SKris Kennaway 		ERR_print_errors(bio_err);
107074664626SKris Kennaway 		}
10715c87c606SMark Murray 	if ((req_conf != NULL) && (req_conf != config)) NCONF_free(req_conf);
107274664626SKris Kennaway 	BIO_free(in);
1073ddd58736SKris Kennaway 	BIO_free_all(out);
107474664626SKris Kennaway 	EVP_PKEY_free(pkey);
107574664626SKris Kennaway 	X509_REQ_free(req);
107674664626SKris Kennaway 	X509_free(x509ss);
10775c87c606SMark Murray 	ASN1_INTEGER_free(serial);
1078ddd58736SKris Kennaway 	if(passargin && passin) OPENSSL_free(passin);
1079ddd58736SKris Kennaway 	if(passargout && passout) OPENSSL_free(passout);
108074664626SKris Kennaway 	OBJ_cleanup();
10815c87c606SMark Murray #ifndef OPENSSL_NO_DSA
108274664626SKris Kennaway 	if (dsa_params != NULL) DSA_free(dsa_params);
108374664626SKris Kennaway #endif
10845c87c606SMark Murray 	apps_shutdown();
10855c87c606SMark Murray 	OPENSSL_EXIT(ex);
108674664626SKris Kennaway 	}
108774664626SKris Kennaway 
10885c87c606SMark Murray static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, char *subj, int attribs,
10895c87c606SMark Murray 			unsigned long chtype)
109074664626SKris Kennaway 	{
109174664626SKris Kennaway 	int ret=0,i;
1092f579bf8eSKris Kennaway 	char no_prompt = 0;
1093f579bf8eSKris Kennaway 	STACK_OF(CONF_VALUE) *dn_sk, *attr_sk = NULL;
1094f579bf8eSKris Kennaway 	char *tmp, *dn_sect,*attr_sect;
109574664626SKris Kennaway 
10965c87c606SMark Murray 	tmp=NCONF_get_string(req_conf,SECTION,PROMPT);
10975c87c606SMark Murray 	if (tmp == NULL)
10985c87c606SMark Murray 		ERR_clear_error();
1099f579bf8eSKris Kennaway 	if((tmp != NULL) && !strcmp(tmp, "no")) no_prompt = 1;
1100f579bf8eSKris Kennaway 
11015c87c606SMark Murray 	dn_sect=NCONF_get_string(req_conf,SECTION,DISTINGUISHED_NAME);
1102f579bf8eSKris Kennaway 	if (dn_sect == NULL)
110374664626SKris Kennaway 		{
110474664626SKris Kennaway 		BIO_printf(bio_err,"unable to find '%s' in config\n",
110574664626SKris Kennaway 			DISTINGUISHED_NAME);
110674664626SKris Kennaway 		goto err;
110774664626SKris Kennaway 		}
11085c87c606SMark Murray 	dn_sk=NCONF_get_section(req_conf,dn_sect);
1109f579bf8eSKris Kennaway 	if (dn_sk == NULL)
111074664626SKris Kennaway 		{
1111f579bf8eSKris Kennaway 		BIO_printf(bio_err,"unable to get '%s' section\n",dn_sect);
111274664626SKris Kennaway 		goto err;
111374664626SKris Kennaway 		}
111474664626SKris Kennaway 
11155c87c606SMark Murray 	attr_sect=NCONF_get_string(req_conf,SECTION,ATTRIBUTES);
1116f579bf8eSKris Kennaway 	if (attr_sect == NULL)
11175c87c606SMark Murray 		{
11185c87c606SMark Murray 		ERR_clear_error();
1119f579bf8eSKris Kennaway 		attr_sk=NULL;
11205c87c606SMark Murray 		}
112174664626SKris Kennaway 	else
112274664626SKris Kennaway 		{
11235c87c606SMark Murray 		attr_sk=NCONF_get_section(req_conf,attr_sect);
1124f579bf8eSKris Kennaway 		if (attr_sk == NULL)
112574664626SKris Kennaway 			{
1126f579bf8eSKris Kennaway 			BIO_printf(bio_err,"unable to get '%s' section\n",attr_sect);
112774664626SKris Kennaway 			goto err;
112874664626SKris Kennaway 			}
112974664626SKris Kennaway 		}
113074664626SKris Kennaway 
1131f579bf8eSKris Kennaway 	/* setup version number */
1132f579bf8eSKris Kennaway 	if (!X509_REQ_set_version(req,0L)) goto err; /* version 1 */
113374664626SKris Kennaway 
11345c87c606SMark Murray 	if (no_prompt)
11355c87c606SMark Murray 		i = auto_info(req, dn_sk, attr_sk, attribs, chtype);
11365c87c606SMark Murray 	else
11375c87c606SMark Murray 		{
11385c87c606SMark Murray 		if (subj)
11395c87c606SMark Murray 			i = build_subject(req, subj, chtype);
11405c87c606SMark Murray 		else
11415c87c606SMark Murray 			i = prompt_info(req, dn_sk, dn_sect, attr_sk, attr_sect, attribs, chtype);
11425c87c606SMark Murray 		}
1143f579bf8eSKris Kennaway 	if(!i) goto err;
1144f579bf8eSKris Kennaway 
1145c1803d78SJacques Vidrine 	if (!X509_REQ_set_pubkey(req,pkey)) goto err;
1146f579bf8eSKris Kennaway 
1147f579bf8eSKris Kennaway 	ret=1;
1148f579bf8eSKris Kennaway err:
1149f579bf8eSKris Kennaway 	return(ret);
1150f579bf8eSKris Kennaway 	}
1151f579bf8eSKris Kennaway 
11525c87c606SMark Murray /*
11535c87c606SMark Murray  * subject is expected to be in the format /type0=value0/type1=value1/type2=...
11545c87c606SMark Murray  * where characters may be escaped by \
11555c87c606SMark Murray  */
11565c87c606SMark Murray static int build_subject(X509_REQ *req, char *subject, unsigned long chtype)
11575c87c606SMark Murray 	{
11585c87c606SMark Murray 	X509_NAME *n;
11595c87c606SMark Murray 
11605c87c606SMark Murray 	if (!(n = do_subject(subject, chtype)))
11615c87c606SMark Murray 		return 0;
11625c87c606SMark Murray 
11635c87c606SMark Murray 	if (!X509_REQ_set_subject_name(req, n))
11645c87c606SMark Murray 		{
11655c87c606SMark Murray 		X509_NAME_free(n);
11665c87c606SMark Murray 		return 0;
11675c87c606SMark Murray 		}
11685c87c606SMark Murray 	X509_NAME_free(n);
11695c87c606SMark Murray 	return 1;
11705c87c606SMark Murray }
11715c87c606SMark Murray 
1172f579bf8eSKris Kennaway 
1173f579bf8eSKris Kennaway static int prompt_info(X509_REQ *req,
1174f579bf8eSKris Kennaway 		STACK_OF(CONF_VALUE) *dn_sk, char *dn_sect,
11755c87c606SMark Murray 		STACK_OF(CONF_VALUE) *attr_sk, char *attr_sect, int attribs,
11765c87c606SMark Murray 		unsigned long chtype)
1177f579bf8eSKris Kennaway 	{
1178f579bf8eSKris Kennaway 	int i;
1179f579bf8eSKris Kennaway 	char *p,*q;
1180f579bf8eSKris Kennaway 	char buf[100];
11815c87c606SMark Murray 	int nid;
11825c87c606SMark Murray 	long n_min,n_max;
1183f579bf8eSKris Kennaway 	char *type,*def,*value;
1184f579bf8eSKris Kennaway 	CONF_VALUE *v;
1185f579bf8eSKris Kennaway 	X509_NAME *subj;
1186f579bf8eSKris Kennaway 	subj = X509_REQ_get_subject_name(req);
11875c87c606SMark Murray 
11885c87c606SMark Murray 	if(!batch)
11895c87c606SMark Murray 		{
119074664626SKris Kennaway 		BIO_printf(bio_err,"You are about to be asked to enter information that will be incorporated\n");
119174664626SKris Kennaway 		BIO_printf(bio_err,"into your certificate request.\n");
119274664626SKris Kennaway 		BIO_printf(bio_err,"What you are about to enter is what is called a Distinguished Name or a DN.\n");
119374664626SKris Kennaway 		BIO_printf(bio_err,"There are quite a few fields but you can leave some blank\n");
119474664626SKris Kennaway 		BIO_printf(bio_err,"For some fields there will be a default value,\n");
119574664626SKris Kennaway 		BIO_printf(bio_err,"If you enter '.', the field will be left blank.\n");
119674664626SKris Kennaway 		BIO_printf(bio_err,"-----\n");
11975c87c606SMark Murray 		}
119874664626SKris Kennaway 
119974664626SKris Kennaway 
1200f579bf8eSKris Kennaway 	if (sk_CONF_VALUE_num(dn_sk))
120174664626SKris Kennaway 		{
120274664626SKris Kennaway 		i= -1;
120374664626SKris Kennaway start:		for (;;)
120474664626SKris Kennaway 			{
120574664626SKris Kennaway 			i++;
1206f579bf8eSKris Kennaway 			if (sk_CONF_VALUE_num(dn_sk) <= i) break;
120774664626SKris Kennaway 
1208f579bf8eSKris Kennaway 			v=sk_CONF_VALUE_value(dn_sk,i);
120974664626SKris Kennaway 			p=q=NULL;
121074664626SKris Kennaway 			type=v->name;
121174664626SKris Kennaway 			if(!check_end(type,"_min") || !check_end(type,"_max") ||
121274664626SKris Kennaway 				!check_end(type,"_default") ||
121374664626SKris Kennaway 					 !check_end(type,"_value")) continue;
121474664626SKris Kennaway 			/* Skip past any leading X. X: X, etc to allow for
121574664626SKris Kennaway 			 * multiple instances
121674664626SKris Kennaway 			 */
121774664626SKris Kennaway 			for(p = v->name; *p ; p++)
121874664626SKris Kennaway 				if ((*p == ':') || (*p == ',') ||
121974664626SKris Kennaway 							 (*p == '.')) {
122074664626SKris Kennaway 					p++;
122174664626SKris Kennaway 					if(*p) type = p;
122274664626SKris Kennaway 					break;
122374664626SKris Kennaway 				}
122474664626SKris Kennaway 			/* If OBJ not recognised ignore it */
122574664626SKris Kennaway 			if ((nid=OBJ_txt2nid(type)) == NID_undef) goto start;
1226ced566fdSJacques Vidrine 			if (BIO_snprintf(buf,sizeof buf,"%s_default",v->name)
1227ced566fdSJacques Vidrine 				>= sizeof buf)
12285c87c606SMark Murray 			   {
12295c87c606SMark Murray 			   BIO_printf(bio_err,"Name '%s' too long\n",v->name);
12305c87c606SMark Murray 			   return 0;
12315c87c606SMark Murray 			   }
12325c87c606SMark Murray 
12335c87c606SMark Murray 			if ((def=NCONF_get_string(req_conf,dn_sect,buf)) == NULL)
12345c87c606SMark Murray 				{
12355c87c606SMark Murray 				ERR_clear_error();
12365c87c606SMark Murray 				def="";
12375c87c606SMark Murray 				}
1238ced566fdSJacques Vidrine 
1239ced566fdSJacques Vidrine 			BIO_snprintf(buf,sizeof buf,"%s_value",v->name);
12405c87c606SMark Murray 			if ((value=NCONF_get_string(req_conf,dn_sect,buf)) == NULL)
12415c87c606SMark Murray 				{
12425c87c606SMark Murray 				ERR_clear_error();
124374664626SKris Kennaway 				value=NULL;
12445c87c606SMark Murray 				}
124574664626SKris Kennaway 
1246ced566fdSJacques Vidrine 			BIO_snprintf(buf,sizeof buf,"%s_min",v->name);
12475c87c606SMark Murray 			if (!NCONF_get_number(req_conf,dn_sect,buf, &n_min))
1248fceca8a3SJacques Vidrine 				{
1249fceca8a3SJacques Vidrine 				ERR_clear_error();
12505c87c606SMark Murray 				n_min = -1;
1251fceca8a3SJacques Vidrine 				}
125274664626SKris Kennaway 
1253ced566fdSJacques Vidrine 			BIO_snprintf(buf,sizeof buf,"%s_max",v->name);
12545c87c606SMark Murray 			if (!NCONF_get_number(req_conf,dn_sect,buf, &n_max))
1255fceca8a3SJacques Vidrine 				{
1256fceca8a3SJacques Vidrine 				ERR_clear_error();
12575c87c606SMark Murray 				n_max = -1;
1258fceca8a3SJacques Vidrine 				}
125974664626SKris Kennaway 
1260f579bf8eSKris Kennaway 			if (!add_DN_object(subj,v->value,def,value,nid,
12615c87c606SMark Murray 				n_min,n_max, chtype))
1262f579bf8eSKris Kennaway 				return 0;
126374664626SKris Kennaway 			}
1264f579bf8eSKris Kennaway 		if (X509_NAME_entry_count(subj) == 0)
126574664626SKris Kennaway 			{
126674664626SKris Kennaway 			BIO_printf(bio_err,"error, no objects specified in config file\n");
1267f579bf8eSKris Kennaway 			return 0;
126874664626SKris Kennaway 			}
126974664626SKris Kennaway 
127074664626SKris Kennaway 		if (attribs)
127174664626SKris Kennaway 			{
12725c87c606SMark Murray 			if ((attr_sk != NULL) && (sk_CONF_VALUE_num(attr_sk) > 0) && (!batch))
127374664626SKris Kennaway 				{
127474664626SKris Kennaway 				BIO_printf(bio_err,"\nPlease enter the following 'extra' attributes\n");
127574664626SKris Kennaway 				BIO_printf(bio_err,"to be sent with your certificate request\n");
127674664626SKris Kennaway 				}
127774664626SKris Kennaway 
127874664626SKris Kennaway 			i= -1;
127974664626SKris Kennaway start2:			for (;;)
128074664626SKris Kennaway 				{
128174664626SKris Kennaway 				i++;
1282f579bf8eSKris Kennaway 				if ((attr_sk == NULL) ||
1283f579bf8eSKris Kennaway 					    (sk_CONF_VALUE_num(attr_sk) <= i))
128474664626SKris Kennaway 					break;
128574664626SKris Kennaway 
1286f579bf8eSKris Kennaway 				v=sk_CONF_VALUE_value(attr_sk,i);
128774664626SKris Kennaway 				type=v->name;
128874664626SKris Kennaway 				if ((nid=OBJ_txt2nid(type)) == NID_undef)
128974664626SKris Kennaway 					goto start2;
129074664626SKris Kennaway 
1291ced566fdSJacques Vidrine 				if (BIO_snprintf(buf,sizeof buf,"%s_default",type)
1292ced566fdSJacques Vidrine 					>= sizeof buf)
12935c87c606SMark Murray 				   {
12945c87c606SMark Murray 				   BIO_printf(bio_err,"Name '%s' too long\n",v->name);
12955c87c606SMark Murray 				   return 0;
12965c87c606SMark Murray 				   }
12975c87c606SMark Murray 
12985c87c606SMark Murray 				if ((def=NCONF_get_string(req_conf,attr_sect,buf))
129974664626SKris Kennaway 					== NULL)
13005c87c606SMark Murray 					{
13015c87c606SMark Murray 					ERR_clear_error();
130274664626SKris Kennaway 					def="";
13035c87c606SMark Murray 					}
13045c87c606SMark Murray 
130574664626SKris Kennaway 
1306ced566fdSJacques Vidrine 				BIO_snprintf(buf,sizeof buf,"%s_value",type);
13075c87c606SMark Murray 				if ((value=NCONF_get_string(req_conf,attr_sect,buf))
130874664626SKris Kennaway 					== NULL)
13095c87c606SMark Murray 					{
13105c87c606SMark Murray 					ERR_clear_error();
131174664626SKris Kennaway 					value=NULL;
13125c87c606SMark Murray 					}
131374664626SKris Kennaway 
1314ced566fdSJacques Vidrine 				BIO_snprintf(buf,sizeof buf,"%s_min",type);
13155c87c606SMark Murray 				if (!NCONF_get_number(req_conf,attr_sect,buf, &n_min))
13165c87c606SMark Murray 					n_min = -1;
131774664626SKris Kennaway 
1318ced566fdSJacques Vidrine 				BIO_snprintf(buf,sizeof buf,"%s_max",type);
13195c87c606SMark Murray 				if (!NCONF_get_number(req_conf,attr_sect,buf, &n_max))
13205c87c606SMark Murray 					n_max = -1;
132174664626SKris Kennaway 
1322f579bf8eSKris Kennaway 				if (!add_attribute_object(req,
13235c87c606SMark Murray 					v->value,def,value,nid,n_min,n_max, chtype))
1324f579bf8eSKris Kennaway 					return 0;
132574664626SKris Kennaway 				}
132674664626SKris Kennaway 			}
132774664626SKris Kennaway 		}
132874664626SKris Kennaway 	else
132974664626SKris Kennaway 		{
133074664626SKris Kennaway 		BIO_printf(bio_err,"No template, please set one up.\n");
1331f579bf8eSKris Kennaway 		return 0;
133274664626SKris Kennaway 		}
133374664626SKris Kennaway 
1334f579bf8eSKris Kennaway 	return 1;
133574664626SKris Kennaway 
133674664626SKris Kennaway 	}
133774664626SKris Kennaway 
1338f579bf8eSKris Kennaway static int auto_info(X509_REQ *req, STACK_OF(CONF_VALUE) *dn_sk,
13395c87c606SMark Murray 			STACK_OF(CONF_VALUE) *attr_sk, int attribs, unsigned long chtype)
1340f579bf8eSKris Kennaway 	{
1341f579bf8eSKris Kennaway 	int i;
1342f579bf8eSKris Kennaway 	char *p,*q;
1343f579bf8eSKris Kennaway 	char *type;
1344f579bf8eSKris Kennaway 	CONF_VALUE *v;
1345f579bf8eSKris Kennaway 	X509_NAME *subj;
1346f579bf8eSKris Kennaway 
1347f579bf8eSKris Kennaway 	subj = X509_REQ_get_subject_name(req);
1348f579bf8eSKris Kennaway 
1349f579bf8eSKris Kennaway 	for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++)
1350f579bf8eSKris Kennaway 		{
1351f579bf8eSKris Kennaway 		v=sk_CONF_VALUE_value(dn_sk,i);
1352f579bf8eSKris Kennaway 		p=q=NULL;
1353f579bf8eSKris Kennaway 		type=v->name;
1354f579bf8eSKris Kennaway 		/* Skip past any leading X. X: X, etc to allow for
1355f579bf8eSKris Kennaway 		 * multiple instances
1356f579bf8eSKris Kennaway 		 */
1357f579bf8eSKris Kennaway 		for(p = v->name; *p ; p++)
1358ddd58736SKris Kennaway #ifndef CHARSET_EBCDIC
1359f579bf8eSKris Kennaway 			if ((*p == ':') || (*p == ',') || (*p == '.')) {
1360ddd58736SKris Kennaway #else
1361ddd58736SKris Kennaway 			if ((*p == os_toascii[':']) || (*p == os_toascii[',']) || (*p == os_toascii['.'])) {
1362ddd58736SKris Kennaway #endif
1363f579bf8eSKris Kennaway 				p++;
1364f579bf8eSKris Kennaway 				if(*p) type = p;
1365f579bf8eSKris Kennaway 				break;
1366f579bf8eSKris Kennaway 			}
13675c87c606SMark Murray 		if (!X509_NAME_add_entry_by_txt(subj,type, chtype,
1368f579bf8eSKris Kennaway 				(unsigned char *) v->value,-1,-1,0)) return 0;
1369f579bf8eSKris Kennaway 
1370f579bf8eSKris Kennaway 		}
1371f579bf8eSKris Kennaway 
1372f579bf8eSKris Kennaway 		if (!X509_NAME_entry_count(subj))
1373f579bf8eSKris Kennaway 			{
1374f579bf8eSKris Kennaway 			BIO_printf(bio_err,"error, no objects specified in config file\n");
1375f579bf8eSKris Kennaway 			return 0;
1376f579bf8eSKris Kennaway 			}
1377f579bf8eSKris Kennaway 		if (attribs)
1378f579bf8eSKris Kennaway 			{
1379f579bf8eSKris Kennaway 			for (i = 0; i < sk_CONF_VALUE_num(attr_sk); i++)
1380f579bf8eSKris Kennaway 				{
1381f579bf8eSKris Kennaway 				v=sk_CONF_VALUE_value(attr_sk,i);
13825c87c606SMark Murray 				if(!X509_REQ_add1_attr_by_txt(req, v->name, chtype,
1383f579bf8eSKris Kennaway 					(unsigned char *)v->value, -1)) return 0;
1384f579bf8eSKris Kennaway 				}
1385f579bf8eSKris Kennaway 			}
1386f579bf8eSKris Kennaway 	return 1;
1387f579bf8eSKris Kennaway 	}
1388f579bf8eSKris Kennaway 
1389f579bf8eSKris Kennaway 
139074664626SKris Kennaway static int add_DN_object(X509_NAME *n, char *text, char *def, char *value,
13915c87c606SMark Murray 	     int nid, int n_min, int n_max, unsigned long chtype)
139274664626SKris Kennaway 	{
1393f579bf8eSKris Kennaway 	int i,ret=0;
139474664626SKris Kennaway 	MS_STATIC char buf[1024];
139574664626SKris Kennaway start:
13965c87c606SMark Murray 	if (!batch) BIO_printf(bio_err,"%s [%s]:",text,def);
139774664626SKris Kennaway 	(void)BIO_flush(bio_err);
139874664626SKris Kennaway 	if(value != NULL)
139974664626SKris Kennaway 		{
1400ced566fdSJacques Vidrine 		BUF_strlcpy(buf,value,sizeof buf);
1401ced566fdSJacques Vidrine 		BUF_strlcat(buf,"\n",sizeof buf);
140274664626SKris Kennaway 		BIO_printf(bio_err,"%s\n",value);
140374664626SKris Kennaway 		}
140474664626SKris Kennaway 	else
140574664626SKris Kennaway 		{
140674664626SKris Kennaway 		buf[0]='\0';
14075c87c606SMark Murray 		if (!batch)
14085c87c606SMark Murray 			{
14095c87c606SMark Murray 			fgets(buf,sizeof buf,stdin);
14105c87c606SMark Murray 			}
14115c87c606SMark Murray 		else
14125c87c606SMark Murray 			{
14135c87c606SMark Murray 			buf[0] = '\n';
14145c87c606SMark Murray 			buf[1] = '\0';
14155c87c606SMark Murray 			}
141674664626SKris Kennaway 		}
141774664626SKris Kennaway 
141874664626SKris Kennaway 	if (buf[0] == '\0') return(0);
141974664626SKris Kennaway 	else if (buf[0] == '\n')
142074664626SKris Kennaway 		{
142174664626SKris Kennaway 		if ((def == NULL) || (def[0] == '\0'))
142274664626SKris Kennaway 			return(1);
1423ced566fdSJacques Vidrine 		BUF_strlcpy(buf,def,sizeof buf);
1424ced566fdSJacques Vidrine 		BUF_strlcat(buf,"\n",sizeof buf);
142574664626SKris Kennaway 		}
142674664626SKris Kennaway 	else if ((buf[0] == '.') && (buf[1] == '\n')) return(1);
142774664626SKris Kennaway 
142874664626SKris Kennaway 	i=strlen(buf);
142974664626SKris Kennaway 	if (buf[i-1] != '\n')
143074664626SKris Kennaway 		{
143174664626SKris Kennaway 		BIO_printf(bio_err,"weird input :-(\n");
143274664626SKris Kennaway 		return(0);
143374664626SKris Kennaway 		}
143474664626SKris Kennaway 	buf[--i]='\0';
1435f579bf8eSKris Kennaway #ifdef CHARSET_EBCDIC
1436f579bf8eSKris Kennaway 	ebcdic2ascii(buf, buf, i);
1437f579bf8eSKris Kennaway #endif
14385c87c606SMark Murray 	if(!req_check_len(i, n_min, n_max)) goto start;
14395c87c606SMark Murray 	if (!X509_NAME_add_entry_by_NID(n,nid, chtype,
1440f579bf8eSKris Kennaway 				(unsigned char *) buf, -1,-1,0)) goto err;
1441f579bf8eSKris Kennaway 	ret=1;
1442f579bf8eSKris Kennaway err:
1443f579bf8eSKris Kennaway 	return(ret);
144474664626SKris Kennaway 	}
144574664626SKris Kennaway 
1446f579bf8eSKris Kennaway static int add_attribute_object(X509_REQ *req, char *text,
14475c87c606SMark Murray 				char *def, char *value, int nid, int n_min,
14485c87c606SMark Murray 				int n_max, unsigned long chtype)
1449f579bf8eSKris Kennaway 	{
1450f579bf8eSKris Kennaway 	int i;
1451f579bf8eSKris Kennaway 	static char buf[1024];
145274664626SKris Kennaway 
1453f579bf8eSKris Kennaway start:
14545c87c606SMark Murray 	if (!batch) BIO_printf(bio_err,"%s [%s]:",text,def);
1455f579bf8eSKris Kennaway 	(void)BIO_flush(bio_err);
1456f579bf8eSKris Kennaway 	if (value != NULL)
1457f579bf8eSKris Kennaway 		{
1458ced566fdSJacques Vidrine 		BUF_strlcpy(buf,value,sizeof buf);
1459ced566fdSJacques Vidrine 		BUF_strlcat(buf,"\n",sizeof buf);
1460f579bf8eSKris Kennaway 		BIO_printf(bio_err,"%s\n",value);
1461f579bf8eSKris Kennaway 		}
1462f579bf8eSKris Kennaway 	else
1463f579bf8eSKris Kennaway 		{
1464f579bf8eSKris Kennaway 		buf[0]='\0';
14655c87c606SMark Murray 		if (!batch)
14665c87c606SMark Murray 			{
14675c87c606SMark Murray 			fgets(buf,sizeof buf,stdin);
14685c87c606SMark Murray 			}
14695c87c606SMark Murray 		else
14705c87c606SMark Murray 			{
14715c87c606SMark Murray 			buf[0] = '\n';
14725c87c606SMark Murray 			buf[1] = '\0';
14735c87c606SMark Murray 			}
1474f579bf8eSKris Kennaway 		}
147574664626SKris Kennaway 
1476f579bf8eSKris Kennaway 	if (buf[0] == '\0') return(0);
1477f579bf8eSKris Kennaway 	else if (buf[0] == '\n')
1478f579bf8eSKris Kennaway 		{
1479f579bf8eSKris Kennaway 		if ((def == NULL) || (def[0] == '\0'))
1480f579bf8eSKris Kennaway 			return(1);
1481ced566fdSJacques Vidrine 		BUF_strlcpy(buf,def,sizeof buf);
1482ced566fdSJacques Vidrine 		BUF_strlcat(buf,"\n",sizeof buf);
1483f579bf8eSKris Kennaway 		}
1484f579bf8eSKris Kennaway 	else if ((buf[0] == '.') && (buf[1] == '\n')) return(1);
148574664626SKris Kennaway 
1486f579bf8eSKris Kennaway 	i=strlen(buf);
1487f579bf8eSKris Kennaway 	if (buf[i-1] != '\n')
1488f579bf8eSKris Kennaway 		{
1489f579bf8eSKris Kennaway 		BIO_printf(bio_err,"weird input :-(\n");
1490f579bf8eSKris Kennaway 		return(0);
1491f579bf8eSKris Kennaway 		}
1492f579bf8eSKris Kennaway 	buf[--i]='\0';
1493ddd58736SKris Kennaway #ifdef CHARSET_EBCDIC
1494ddd58736SKris Kennaway 	ebcdic2ascii(buf, buf, i);
1495ddd58736SKris Kennaway #endif
14965c87c606SMark Murray 	if(!req_check_len(i, n_min, n_max)) goto start;
1497f579bf8eSKris Kennaway 
14985c87c606SMark Murray 	if(!X509_REQ_add1_attr_by_NID(req, nid, chtype,
1499f579bf8eSKris Kennaway 					(unsigned char *)buf, -1)) {
1500f579bf8eSKris Kennaway 		BIO_printf(bio_err, "Error adding attribute\n");
1501f579bf8eSKris Kennaway 		ERR_print_errors(bio_err);
1502f579bf8eSKris Kennaway 		goto err;
1503f579bf8eSKris Kennaway 	}
1504f579bf8eSKris Kennaway 
150574664626SKris Kennaway 	return(1);
150674664626SKris Kennaway err:
150774664626SKris Kennaway 	return(0);
150874664626SKris Kennaway 	}
150974664626SKris Kennaway 
15105c87c606SMark Murray #ifndef OPENSSL_NO_RSA
151174664626SKris Kennaway static void MS_CALLBACK req_cb(int p, int n, void *arg)
151274664626SKris Kennaway 	{
151374664626SKris Kennaway 	char c='*';
151474664626SKris Kennaway 
151574664626SKris Kennaway 	if (p == 0) c='.';
151674664626SKris Kennaway 	if (p == 1) c='+';
151774664626SKris Kennaway 	if (p == 2) c='*';
151874664626SKris Kennaway 	if (p == 3) c='\n';
151974664626SKris Kennaway 	BIO_write((BIO *)arg,&c,1);
152074664626SKris Kennaway 	(void)BIO_flush((BIO *)arg);
152174664626SKris Kennaway #ifdef LINT
152274664626SKris Kennaway 	p=n;
152374664626SKris Kennaway #endif
152474664626SKris Kennaway 	}
1525f579bf8eSKris Kennaway #endif
152674664626SKris Kennaway 
15275c87c606SMark Murray static int req_check_len(int len, int n_min, int n_max)
152874664626SKris Kennaway 	{
15295c87c606SMark Murray 	if ((n_min > 0) && (len < n_min))
153074664626SKris Kennaway 		{
15315c87c606SMark Murray 		BIO_printf(bio_err,"string is too short, it needs to be at least %d bytes long\n",n_min);
153274664626SKris Kennaway 		return(0);
153374664626SKris Kennaway 		}
15345c87c606SMark Murray 	if ((n_max >= 0) && (len > n_max))
153574664626SKris Kennaway 		{
15365c87c606SMark Murray 		BIO_printf(bio_err,"string is too long, it needs to be less than  %d bytes long\n",n_max);
153774664626SKris Kennaway 		return(0);
153874664626SKris Kennaway 		}
153974664626SKris Kennaway 	return(1);
154074664626SKris Kennaway 	}
154174664626SKris Kennaway 
154274664626SKris Kennaway /* Check if the end of a string matches 'end' */
154374664626SKris Kennaway static int check_end(char *str, char *end)
154474664626SKris Kennaway {
154574664626SKris Kennaway 	int elen, slen;
154674664626SKris Kennaway 	char *tmp;
154774664626SKris Kennaway 	elen = strlen(end);
154874664626SKris Kennaway 	slen = strlen(str);
154974664626SKris Kennaway 	if(elen > slen) return 1;
155074664626SKris Kennaway 	tmp = str + slen - elen;
155174664626SKris Kennaway 	return strcmp(tmp, end);
155274664626SKris Kennaway }
1553