xref: /freebsd/crypto/openssl/apps/req.c (revision 74664626283603849f4acbd49e8c8288c08cff4d)
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>
6374664626SKris Kennaway #ifdef 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/rand.h>
7074664626SKris Kennaway #include <openssl/conf.h>
7174664626SKris Kennaway #include <openssl/err.h>
7274664626SKris Kennaway #include <openssl/asn1.h>
7374664626SKris Kennaway #include <openssl/x509.h>
7474664626SKris Kennaway #include <openssl/x509v3.h>
7574664626SKris Kennaway #include <openssl/objects.h>
7674664626SKris Kennaway #include <openssl/pem.h>
7774664626SKris Kennaway 
7874664626SKris Kennaway #define SECTION		"req"
7974664626SKris Kennaway 
8074664626SKris Kennaway #define BITS		"default_bits"
8174664626SKris Kennaway #define KEYFILE		"default_keyfile"
8274664626SKris Kennaway #define DISTINGUISHED_NAME	"distinguished_name"
8374664626SKris Kennaway #define ATTRIBUTES	"attributes"
8474664626SKris Kennaway #define V3_EXTENSIONS	"x509_extensions"
8574664626SKris Kennaway 
8674664626SKris Kennaway #define DEFAULT_KEY_LENGTH	512
8774664626SKris Kennaway #define MIN_KEY_LENGTH		384
8874664626SKris Kennaway 
8974664626SKris Kennaway #undef PROG
9074664626SKris Kennaway #define PROG	req_main
9174664626SKris Kennaway 
9274664626SKris Kennaway /* -inform arg	- input format - default PEM (one of DER, TXT or PEM)
9374664626SKris Kennaway  * -outform arg - output format - default PEM
9474664626SKris Kennaway  * -in arg	- input file - default stdin
9574664626SKris Kennaway  * -out arg	- output file - default stdout
9674664626SKris Kennaway  * -verify	- check request signature
9774664626SKris Kennaway  * -noout	- don't print stuff out.
9874664626SKris Kennaway  * -text	- print out human readable text.
9974664626SKris Kennaway  * -nodes	- no des encryption
10074664626SKris Kennaway  * -config file	- Load configuration file.
10174664626SKris Kennaway  * -key file	- make a request using key in file (or use it for verification).
10274664626SKris Kennaway  * -keyform	- key file format.
10374664626SKris Kennaway  * -newkey	- make a key and a request.
10474664626SKris Kennaway  * -modulus	- print RSA modulus.
10574664626SKris Kennaway  * -x509	- output a self signed X509 structure instead.
10674664626SKris Kennaway  * -asn1-kludge	- output new certificate request in a format that some CA's
10774664626SKris Kennaway  *		  require.  This format is wrong
10874664626SKris Kennaway  */
10974664626SKris Kennaway 
11074664626SKris Kennaway static int make_REQ(X509_REQ *req,EVP_PKEY *pkey,int attribs);
11174664626SKris Kennaway static int add_attribute_object(STACK_OF(X509_ATTRIBUTE) *n, char *text,
11274664626SKris Kennaway 				char *def, char *value, int nid, int min,
11374664626SKris Kennaway 				int max);
11474664626SKris Kennaway static int add_DN_object(X509_NAME *n, char *text, char *def, char *value,
11574664626SKris Kennaway 	int nid,int min,int max);
11674664626SKris Kennaway static void MS_CALLBACK req_cb(int p,int n,void *arg);
11774664626SKris Kennaway static int req_fix_data(int nid,int *type,int len,int min,int max);
11874664626SKris Kennaway static int check_end(char *str, char *end);
11974664626SKris Kennaway static int add_oid_section(LHASH *conf);
12074664626SKris Kennaway #ifndef MONOLITH
12174664626SKris Kennaway static char *default_config_file=NULL;
12274664626SKris Kennaway static LHASH *config=NULL;
12374664626SKris Kennaway #endif
12474664626SKris Kennaway static LHASH *req_conf=NULL;
12574664626SKris Kennaway 
12674664626SKris Kennaway #define TYPE_RSA	1
12774664626SKris Kennaway #define TYPE_DSA	2
12874664626SKris Kennaway #define TYPE_DH		3
12974664626SKris Kennaway 
13074664626SKris Kennaway int MAIN(int argc, char **argv)
13174664626SKris Kennaway 	{
13274664626SKris Kennaway #ifndef NO_DSA
13374664626SKris Kennaway 	DSA *dsa_params=NULL;
13474664626SKris Kennaway #endif
13574664626SKris Kennaway 	int ex=1,x509=0,days=30;
13674664626SKris Kennaway 	X509 *x509ss=NULL;
13774664626SKris Kennaway 	X509_REQ *req=NULL;
13874664626SKris Kennaway 	EVP_PKEY *pkey=NULL;
13974664626SKris Kennaway 	int i,badops=0,newreq=0,newkey= -1,pkey_type=0;
14074664626SKris Kennaway 	BIO *in=NULL,*out=NULL;
14174664626SKris Kennaway 	int informat,outformat,verify=0,noout=0,text=0,keyform=FORMAT_PEM;
14274664626SKris Kennaway 	int nodes=0,kludge=0;
14374664626SKris Kennaway 	char *infile,*outfile,*prog,*keyfile=NULL,*template=NULL,*keyout=NULL;
14474664626SKris Kennaway 	char *extensions = NULL;
14574664626SKris Kennaway 	EVP_CIPHER *cipher=NULL;
14674664626SKris Kennaway 	int modulus=0;
14774664626SKris Kennaway 	char *p;
14874664626SKris Kennaway 	const EVP_MD *md_alg=NULL,*digest=EVP_md5();
14974664626SKris Kennaway #ifndef MONOLITH
15074664626SKris Kennaway 	MS_STATIC char config_name[256];
15174664626SKris Kennaway #endif
15274664626SKris Kennaway 
15374664626SKris Kennaway #ifndef NO_DES
15474664626SKris Kennaway 	cipher=EVP_des_ede3_cbc();
15574664626SKris Kennaway #endif
15674664626SKris Kennaway 	apps_startup();
15774664626SKris Kennaway 
15874664626SKris Kennaway 	if (bio_err == NULL)
15974664626SKris Kennaway 		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
16074664626SKris Kennaway 			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
16174664626SKris Kennaway 
16274664626SKris Kennaway 	infile=NULL;
16374664626SKris Kennaway 	outfile=NULL;
16474664626SKris Kennaway 	informat=FORMAT_PEM;
16574664626SKris Kennaway 	outformat=FORMAT_PEM;
16674664626SKris Kennaway 
16774664626SKris Kennaway 	prog=argv[0];
16874664626SKris Kennaway 	argc--;
16974664626SKris Kennaway 	argv++;
17074664626SKris Kennaway 	while (argc >= 1)
17174664626SKris Kennaway 		{
17274664626SKris Kennaway 		if 	(strcmp(*argv,"-inform") == 0)
17374664626SKris Kennaway 			{
17474664626SKris Kennaway 			if (--argc < 1) goto bad;
17574664626SKris Kennaway 			informat=str2fmt(*(++argv));
17674664626SKris Kennaway 			}
17774664626SKris Kennaway 		else if (strcmp(*argv,"-outform") == 0)
17874664626SKris Kennaway 			{
17974664626SKris Kennaway 			if (--argc < 1) goto bad;
18074664626SKris Kennaway 			outformat=str2fmt(*(++argv));
18174664626SKris Kennaway 			}
18274664626SKris Kennaway 		else if (strcmp(*argv,"-key") == 0)
18374664626SKris Kennaway 			{
18474664626SKris Kennaway 			if (--argc < 1) goto bad;
18574664626SKris Kennaway 			keyfile= *(++argv);
18674664626SKris Kennaway 			}
18774664626SKris Kennaway 		else if (strcmp(*argv,"-new") == 0)
18874664626SKris Kennaway 			{
18974664626SKris Kennaway 			pkey_type=TYPE_RSA;
19074664626SKris Kennaway 			newreq=1;
19174664626SKris Kennaway 			}
19274664626SKris Kennaway 		else if (strcmp(*argv,"-config") == 0)
19374664626SKris Kennaway 			{
19474664626SKris Kennaway 			if (--argc < 1) goto bad;
19574664626SKris Kennaway 			template= *(++argv);
19674664626SKris Kennaway 			}
19774664626SKris Kennaway 		else if (strcmp(*argv,"-keyform") == 0)
19874664626SKris Kennaway 			{
19974664626SKris Kennaway 			if (--argc < 1) goto bad;
20074664626SKris Kennaway 			keyform=str2fmt(*(++argv));
20174664626SKris Kennaway 			}
20274664626SKris Kennaway 		else if (strcmp(*argv,"-in") == 0)
20374664626SKris Kennaway 			{
20474664626SKris Kennaway 			if (--argc < 1) goto bad;
20574664626SKris Kennaway 			infile= *(++argv);
20674664626SKris Kennaway 			}
20774664626SKris Kennaway 		else if (strcmp(*argv,"-out") == 0)
20874664626SKris Kennaway 			{
20974664626SKris Kennaway 			if (--argc < 1) goto bad;
21074664626SKris Kennaway 			outfile= *(++argv);
21174664626SKris Kennaway 			}
21274664626SKris Kennaway 		else if (strcmp(*argv,"-keyout") == 0)
21374664626SKris Kennaway 			{
21474664626SKris Kennaway 			if (--argc < 1) goto bad;
21574664626SKris Kennaway 			keyout= *(++argv);
21674664626SKris Kennaway 			}
21774664626SKris Kennaway 		else if (strcmp(*argv,"-newkey") == 0)
21874664626SKris Kennaway 			{
21974664626SKris Kennaway 			int is_numeric;
22074664626SKris Kennaway 
22174664626SKris Kennaway 			if (--argc < 1) goto bad;
22274664626SKris Kennaway 			p= *(++argv);
22374664626SKris Kennaway 			is_numeric = p[0] >= '0' && p[0] <= '9';
22474664626SKris Kennaway 			if (strncmp("rsa:",p,4) == 0 || is_numeric)
22574664626SKris Kennaway 				{
22674664626SKris Kennaway 				pkey_type=TYPE_RSA;
22774664626SKris Kennaway 				if(!is_numeric)
22874664626SKris Kennaway 				    p+=4;
22974664626SKris Kennaway 				newkey= atoi(p);
23074664626SKris Kennaway 				}
23174664626SKris Kennaway 			else
23274664626SKris Kennaway #ifndef NO_DSA
23374664626SKris Kennaway 				if (strncmp("dsa:",p,4) == 0)
23474664626SKris Kennaway 				{
23574664626SKris Kennaway 				X509 *xtmp=NULL;
23674664626SKris Kennaway 				EVP_PKEY *dtmp;
23774664626SKris Kennaway 
23874664626SKris Kennaway 				pkey_type=TYPE_DSA;
23974664626SKris Kennaway 				p+=4;
24074664626SKris Kennaway 				if ((in=BIO_new_file(p,"r")) == NULL)
24174664626SKris Kennaway 					{
24274664626SKris Kennaway 					perror(p);
24374664626SKris Kennaway 					goto end;
24474664626SKris Kennaway 					}
24574664626SKris Kennaway 				if ((dsa_params=PEM_read_bio_DSAparams(in,NULL,NULL,NULL)) == NULL)
24674664626SKris Kennaway 					{
24774664626SKris Kennaway 					ERR_clear_error();
24874664626SKris Kennaway 					(void)BIO_reset(in);
24974664626SKris Kennaway 					if ((xtmp=PEM_read_bio_X509(in,NULL,NULL,NULL)) == NULL)
25074664626SKris Kennaway 						{
25174664626SKris Kennaway 						BIO_printf(bio_err,"unable to load DSA parameters from file\n");
25274664626SKris Kennaway 						goto end;
25374664626SKris Kennaway 						}
25474664626SKris Kennaway 
25574664626SKris Kennaway 					dtmp=X509_get_pubkey(xtmp);
25674664626SKris Kennaway 					if (dtmp->type == EVP_PKEY_DSA)
25774664626SKris Kennaway 						dsa_params=DSAparams_dup(dtmp->pkey.dsa);
25874664626SKris Kennaway 					EVP_PKEY_free(dtmp);
25974664626SKris Kennaway 					X509_free(xtmp);
26074664626SKris Kennaway 					if (dsa_params == NULL)
26174664626SKris Kennaway 						{
26274664626SKris Kennaway 						BIO_printf(bio_err,"Certificate does not contain DSA parameters\n");
26374664626SKris Kennaway 						goto end;
26474664626SKris Kennaway 						}
26574664626SKris Kennaway 					}
26674664626SKris Kennaway 				BIO_free(in);
26774664626SKris Kennaway 				newkey=BN_num_bits(dsa_params->p);
26874664626SKris Kennaway 				in=NULL;
26974664626SKris Kennaway 				}
27074664626SKris Kennaway 			else
27174664626SKris Kennaway #endif
27274664626SKris Kennaway #ifndef NO_DH
27374664626SKris Kennaway 				if (strncmp("dh:",p,4) == 0)
27474664626SKris Kennaway 				{
27574664626SKris Kennaway 				pkey_type=TYPE_DH;
27674664626SKris Kennaway 				p+=3;
27774664626SKris Kennaway 				}
27874664626SKris Kennaway 			else
27974664626SKris Kennaway #endif
28074664626SKris Kennaway 				pkey_type=TYPE_RSA;
28174664626SKris Kennaway 
28274664626SKris Kennaway 			newreq=1;
28374664626SKris Kennaway 			}
28474664626SKris Kennaway 		else if (strcmp(*argv,"-modulus") == 0)
28574664626SKris Kennaway 			modulus=1;
28674664626SKris Kennaway 		else if (strcmp(*argv,"-verify") == 0)
28774664626SKris Kennaway 			verify=1;
28874664626SKris Kennaway 		else if (strcmp(*argv,"-nodes") == 0)
28974664626SKris Kennaway 			nodes=1;
29074664626SKris Kennaway 		else if (strcmp(*argv,"-noout") == 0)
29174664626SKris Kennaway 			noout=1;
29274664626SKris Kennaway 		else if (strcmp(*argv,"-text") == 0)
29374664626SKris Kennaway 			text=1;
29474664626SKris Kennaway 		else if (strcmp(*argv,"-x509") == 0)
29574664626SKris Kennaway 			x509=1;
29674664626SKris Kennaway 		else if (strcmp(*argv,"-asn1-kludge") == 0)
29774664626SKris Kennaway 			kludge=1;
29874664626SKris Kennaway 		else if (strcmp(*argv,"-no-asn1-kludge") == 0)
29974664626SKris Kennaway 			kludge=0;
30074664626SKris Kennaway 		else if (strcmp(*argv,"-days") == 0)
30174664626SKris Kennaway 			{
30274664626SKris Kennaway 			if (--argc < 1) goto bad;
30374664626SKris Kennaway 			days= atoi(*(++argv));
30474664626SKris Kennaway 			if (days == 0) days=30;
30574664626SKris Kennaway 			}
30674664626SKris Kennaway 		else if ((md_alg=EVP_get_digestbyname(&((*argv)[1]))) != NULL)
30774664626SKris Kennaway 			{
30874664626SKris Kennaway 			/* ok */
30974664626SKris Kennaway 			digest=md_alg;
31074664626SKris Kennaway 			}
31174664626SKris Kennaway 		else
31274664626SKris Kennaway 
31374664626SKris Kennaway 			{
31474664626SKris Kennaway 			BIO_printf(bio_err,"unknown option %s\n",*argv);
31574664626SKris Kennaway 			badops=1;
31674664626SKris Kennaway 			break;
31774664626SKris Kennaway 			}
31874664626SKris Kennaway 		argc--;
31974664626SKris Kennaway 		argv++;
32074664626SKris Kennaway 		}
32174664626SKris Kennaway 
32274664626SKris Kennaway 	if (badops)
32374664626SKris Kennaway 		{
32474664626SKris Kennaway bad:
32574664626SKris Kennaway 		BIO_printf(bio_err,"%s [options] <infile >outfile\n",prog);
32674664626SKris Kennaway 		BIO_printf(bio_err,"where options  are\n");
32774664626SKris Kennaway 		BIO_printf(bio_err," -inform arg    input format - one of DER TXT PEM\n");
32874664626SKris Kennaway 		BIO_printf(bio_err," -outform arg   output format - one of DER TXT PEM\n");
32974664626SKris Kennaway 		BIO_printf(bio_err," -in arg        input file\n");
33074664626SKris Kennaway 		BIO_printf(bio_err," -out arg       output file\n");
33174664626SKris Kennaway 		BIO_printf(bio_err," -text          text form of request\n");
33274664626SKris Kennaway 		BIO_printf(bio_err," -noout         do not output REQ\n");
33374664626SKris Kennaway 		BIO_printf(bio_err," -verify        verify signature on REQ\n");
33474664626SKris Kennaway 		BIO_printf(bio_err," -modulus       RSA modulus\n");
33574664626SKris Kennaway 		BIO_printf(bio_err," -nodes         don't encrypt the output key\n");
33674664626SKris Kennaway 		BIO_printf(bio_err," -key file	use the private key contained in file\n");
33774664626SKris Kennaway 		BIO_printf(bio_err," -keyform arg   key file format\n");
33874664626SKris Kennaway 		BIO_printf(bio_err," -keyout arg    file to send the key to\n");
33974664626SKris Kennaway 		BIO_printf(bio_err," -newkey rsa:bits generate a new RSA key of 'bits' in size\n");
34074664626SKris Kennaway 		BIO_printf(bio_err," -newkey dsa:file generate a new DSA key, parameters taken from CA in 'file'\n");
34174664626SKris Kennaway 
34274664626SKris Kennaway 		BIO_printf(bio_err," -[digest]      Digest to sign with (md5, sha1, md2, mdc2)\n");
34374664626SKris Kennaway 		BIO_printf(bio_err," -config file   request template file.\n");
34474664626SKris Kennaway 		BIO_printf(bio_err," -new           new request.\n");
34574664626SKris Kennaway 		BIO_printf(bio_err," -x509          output a x509 structure instead of a cert. req.\n");
34674664626SKris Kennaway 		BIO_printf(bio_err," -days          number of days a x509 generated by -x509 is valid for.\n");
34774664626SKris Kennaway 		BIO_printf(bio_err," -asn1-kludge   Output the 'request' in a format that is wrong but some CA's\n");
34874664626SKris Kennaway 		BIO_printf(bio_err,"                have been reported as requiring\n");
34974664626SKris Kennaway 		BIO_printf(bio_err,"                [ It is now always turned on but can be turned off with -no-asn1-kludge ]\n");
35074664626SKris Kennaway 		goto end;
35174664626SKris Kennaway 		}
35274664626SKris Kennaway 
35374664626SKris Kennaway 	ERR_load_crypto_strings();
35474664626SKris Kennaway 	X509V3_add_standard_extensions();
35574664626SKris Kennaway 
35674664626SKris Kennaway #ifndef MONOLITH
35774664626SKris Kennaway 	/* Lets load up our environment a little */
35874664626SKris Kennaway 	p=getenv("OPENSSL_CONF");
35974664626SKris Kennaway 	if (p == NULL)
36074664626SKris Kennaway 		p=getenv("SSLEAY_CONF");
36174664626SKris Kennaway 	if (p == NULL)
36274664626SKris Kennaway 		{
36374664626SKris Kennaway 		strcpy(config_name,X509_get_default_cert_area());
36474664626SKris Kennaway #ifndef VMS
36574664626SKris Kennaway 		strcat(config_name,"/");
36674664626SKris Kennaway #endif
36774664626SKris Kennaway 		strcat(config_name,OPENSSL_CONF);
36874664626SKris Kennaway 		p=config_name;
36974664626SKris Kennaway 		}
37074664626SKris Kennaway         default_config_file=p;
37174664626SKris Kennaway 	config=CONF_load(config,p,NULL);
37274664626SKris Kennaway #endif
37374664626SKris Kennaway 
37474664626SKris Kennaway 	if (template != NULL)
37574664626SKris Kennaway 		{
37674664626SKris Kennaway 		long errline;
37774664626SKris Kennaway 
37874664626SKris Kennaway 		BIO_printf(bio_err,"Using configuration from %s\n",template);
37974664626SKris Kennaway 		req_conf=CONF_load(NULL,template,&errline);
38074664626SKris Kennaway 		if (req_conf == NULL)
38174664626SKris Kennaway 			{
38274664626SKris Kennaway 			BIO_printf(bio_err,"error on line %ld of %s\n",errline,template);
38374664626SKris Kennaway 			goto end;
38474664626SKris Kennaway 			}
38574664626SKris Kennaway 		}
38674664626SKris Kennaway 	else
38774664626SKris Kennaway 		{
38874664626SKris Kennaway 		req_conf=config;
38974664626SKris Kennaway 		BIO_printf(bio_err,"Using configuration from %s\n",
39074664626SKris Kennaway 			default_config_file);
39174664626SKris Kennaway 		if (req_conf == NULL)
39274664626SKris Kennaway 			{
39374664626SKris Kennaway 			BIO_printf(bio_err,"Unable to load config info\n");
39474664626SKris Kennaway 			}
39574664626SKris Kennaway 		}
39674664626SKris Kennaway 
39774664626SKris Kennaway 	if (req_conf != NULL)
39874664626SKris Kennaway 		{
39974664626SKris Kennaway 		p=CONF_get_string(req_conf,NULL,"oid_file");
40074664626SKris Kennaway 		if (p != NULL)
40174664626SKris Kennaway 			{
40274664626SKris Kennaway 			BIO *oid_bio;
40374664626SKris Kennaway 
40474664626SKris Kennaway 			oid_bio=BIO_new_file(p,"r");
40574664626SKris Kennaway 			if (oid_bio == NULL)
40674664626SKris Kennaway 				{
40774664626SKris Kennaway 				/*
40874664626SKris Kennaway 				BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
40974664626SKris Kennaway 				ERR_print_errors(bio_err);
41074664626SKris Kennaway 				*/
41174664626SKris Kennaway 				}
41274664626SKris Kennaway 			else
41374664626SKris Kennaway 				{
41474664626SKris Kennaway 				OBJ_create_objects(oid_bio);
41574664626SKris Kennaway 				BIO_free(oid_bio);
41674664626SKris Kennaway 				}
41774664626SKris Kennaway 			}
41874664626SKris Kennaway 		}
41974664626SKris Kennaway 		if(!add_oid_section(req_conf)) goto end;
42074664626SKris Kennaway 
42174664626SKris Kennaway 	if ((md_alg == NULL) &&
42274664626SKris Kennaway 		((p=CONF_get_string(req_conf,SECTION,"default_md")) != NULL))
42374664626SKris Kennaway 		{
42474664626SKris Kennaway 		if ((md_alg=EVP_get_digestbyname(p)) != NULL)
42574664626SKris Kennaway 			digest=md_alg;
42674664626SKris Kennaway 		}
42774664626SKris Kennaway 
42874664626SKris Kennaway 	extensions = CONF_get_string(req_conf, SECTION, V3_EXTENSIONS);
42974664626SKris Kennaway 	if(extensions) {
43074664626SKris Kennaway 		/* Check syntax of file */
43174664626SKris Kennaway 		X509V3_CTX ctx;
43274664626SKris Kennaway 		X509V3_set_ctx_test(&ctx);
43374664626SKris Kennaway 		X509V3_set_conf_lhash(&ctx, req_conf);
43474664626SKris Kennaway 		if(!X509V3_EXT_add_conf(req_conf, &ctx, extensions, NULL)) {
43574664626SKris Kennaway 			BIO_printf(bio_err,
43674664626SKris Kennaway 			 "Error Loading extension section %s\n", extensions);
43774664626SKris Kennaway 			goto end;
43874664626SKris Kennaway 		}
43974664626SKris Kennaway 	}
44074664626SKris Kennaway 
44174664626SKris Kennaway 	in=BIO_new(BIO_s_file());
44274664626SKris Kennaway 	out=BIO_new(BIO_s_file());
44374664626SKris Kennaway 	if ((in == NULL) || (out == NULL))
44474664626SKris Kennaway 		goto end;
44574664626SKris Kennaway 
44674664626SKris Kennaway 	if (keyfile != NULL)
44774664626SKris Kennaway 		{
44874664626SKris Kennaway 		if (BIO_read_filename(in,keyfile) <= 0)
44974664626SKris Kennaway 			{
45074664626SKris Kennaway 			perror(keyfile);
45174664626SKris Kennaway 			goto end;
45274664626SKris Kennaway 			}
45374664626SKris Kennaway 
45474664626SKris Kennaway /*		if (keyform == FORMAT_ASN1)
45574664626SKris Kennaway 			rsa=d2i_RSAPrivateKey_bio(in,NULL);
45674664626SKris Kennaway 		else */
45774664626SKris Kennaway 		if (keyform == FORMAT_PEM)
45874664626SKris Kennaway 			pkey=PEM_read_bio_PrivateKey(in,NULL,NULL,NULL);
45974664626SKris Kennaway 		else
46074664626SKris Kennaway 			{
46174664626SKris Kennaway 			BIO_printf(bio_err,"bad input format specified for X509 request\n");
46274664626SKris Kennaway 			goto end;
46374664626SKris Kennaway 			}
46474664626SKris Kennaway 
46574664626SKris Kennaway 		if (pkey == NULL)
46674664626SKris Kennaway 			{
46774664626SKris Kennaway 			BIO_printf(bio_err,"unable to load Private key\n");
46874664626SKris Kennaway 			goto end;
46974664626SKris Kennaway 			}
47074664626SKris Kennaway 		}
47174664626SKris Kennaway 
47274664626SKris Kennaway 	if (newreq && (pkey == NULL))
47374664626SKris Kennaway 		{
47474664626SKris Kennaway 		char *randfile;
47574664626SKris Kennaway 		char buffer[200];
47674664626SKris Kennaway 
47774664626SKris Kennaway 		if ((randfile=CONF_get_string(req_conf,SECTION,"RANDFILE")) == NULL)
47874664626SKris Kennaway 			randfile=RAND_file_name(buffer,200);
47974664626SKris Kennaway #ifdef WINDOWS
48074664626SKris Kennaway 		BIO_printf(bio_err,"Loading 'screen' into random state -");
48174664626SKris Kennaway 		BIO_flush(bio_err);
48274664626SKris Kennaway 		RAND_screen();
48374664626SKris Kennaway 		BIO_printf(bio_err," done\n");
48474664626SKris Kennaway #endif
48574664626SKris Kennaway 		if ((randfile == NULL) || !RAND_load_file(randfile,1024L*1024L))
48674664626SKris Kennaway 			{
48774664626SKris Kennaway 			BIO_printf(bio_err,"unable to load 'random state'\n");
48874664626SKris Kennaway 			BIO_printf(bio_err,"What this means is that the random number generator has not been seeded\n");
48974664626SKris Kennaway 			BIO_printf(bio_err,"with much random data.\n");
49074664626SKris Kennaway 			BIO_printf(bio_err,"Consider setting the RANDFILE environment variable to point at a file that\n");
49174664626SKris Kennaway 			BIO_printf(bio_err,"'random' data can be kept in.\n");
49274664626SKris Kennaway 			}
49374664626SKris Kennaway 		if (newkey <= 0)
49474664626SKris Kennaway 			{
49574664626SKris Kennaway 			newkey=(int)CONF_get_number(req_conf,SECTION,BITS);
49674664626SKris Kennaway 			if (newkey <= 0)
49774664626SKris Kennaway 				newkey=DEFAULT_KEY_LENGTH;
49874664626SKris Kennaway 			}
49974664626SKris Kennaway 
50074664626SKris Kennaway 		if (newkey < MIN_KEY_LENGTH)
50174664626SKris Kennaway 			{
50274664626SKris Kennaway 			BIO_printf(bio_err,"private key length is too short,\n");
50374664626SKris Kennaway 			BIO_printf(bio_err,"it needs to be at least %d bits, not %d\n",MIN_KEY_LENGTH,newkey);
50474664626SKris Kennaway 			goto end;
50574664626SKris Kennaway 			}
50674664626SKris Kennaway 		BIO_printf(bio_err,"Generating a %d bit %s private key\n",
50774664626SKris Kennaway 			newkey,(pkey_type == TYPE_RSA)?"RSA":"DSA");
50874664626SKris Kennaway 
50974664626SKris Kennaway 		if ((pkey=EVP_PKEY_new()) == NULL) goto end;
51074664626SKris Kennaway 
51174664626SKris Kennaway #ifndef NO_RSA
51274664626SKris Kennaway 		if (pkey_type == TYPE_RSA)
51374664626SKris Kennaway 			{
51474664626SKris Kennaway 			if (!EVP_PKEY_assign_RSA(pkey,
51574664626SKris Kennaway 				RSA_generate_key(newkey,0x10001,
51674664626SKris Kennaway 					req_cb,bio_err)))
51774664626SKris Kennaway 				goto end;
51874664626SKris Kennaway 			}
51974664626SKris Kennaway 		else
52074664626SKris Kennaway #endif
52174664626SKris Kennaway #ifndef NO_DSA
52274664626SKris Kennaway 			if (pkey_type == TYPE_DSA)
52374664626SKris Kennaway 			{
52474664626SKris Kennaway 			if (!DSA_generate_key(dsa_params)) goto end;
52574664626SKris Kennaway 			if (!EVP_PKEY_assign_DSA(pkey,dsa_params)) goto end;
52674664626SKris Kennaway 			dsa_params=NULL;
52774664626SKris Kennaway 			}
52874664626SKris Kennaway #endif
52974664626SKris Kennaway 
53074664626SKris Kennaway 		if ((randfile == NULL) || (RAND_write_file(randfile) == 0))
53174664626SKris Kennaway 			BIO_printf(bio_err,"unable to write 'random state'\n");
53274664626SKris Kennaway 
53374664626SKris Kennaway 		if (pkey == NULL) goto end;
53474664626SKris Kennaway 
53574664626SKris Kennaway 		if (keyout == NULL)
53674664626SKris Kennaway 			keyout=CONF_get_string(req_conf,SECTION,KEYFILE);
53774664626SKris Kennaway 
53874664626SKris Kennaway 		if (keyout == NULL)
53974664626SKris Kennaway 			{
54074664626SKris Kennaway 			BIO_printf(bio_err,"writing new private key to stdout\n");
54174664626SKris Kennaway 			BIO_set_fp(out,stdout,BIO_NOCLOSE);
54274664626SKris Kennaway 			}
54374664626SKris Kennaway 		else
54474664626SKris Kennaway 			{
54574664626SKris Kennaway 			BIO_printf(bio_err,"writing new private key to '%s'\n",keyout);
54674664626SKris Kennaway 			if (BIO_write_filename(out,keyout) <= 0)
54774664626SKris Kennaway 				{
54874664626SKris Kennaway 				perror(keyout);
54974664626SKris Kennaway 				goto end;
55074664626SKris Kennaway 				}
55174664626SKris Kennaway 			}
55274664626SKris Kennaway 
55374664626SKris Kennaway 		p=CONF_get_string(req_conf,SECTION,"encrypt_rsa_key");
55474664626SKris Kennaway 		if (p == NULL)
55574664626SKris Kennaway 			p=CONF_get_string(req_conf,SECTION,"encrypt_key");
55674664626SKris Kennaway 		if ((p != NULL) && (strcmp(p,"no") == 0))
55774664626SKris Kennaway 			cipher=NULL;
55874664626SKris Kennaway 		if (nodes) cipher=NULL;
55974664626SKris Kennaway 
56074664626SKris Kennaway 		i=0;
56174664626SKris Kennaway loop:
56274664626SKris Kennaway 		if (!PEM_write_bio_PrivateKey(out,pkey,cipher,
56374664626SKris Kennaway 			NULL,0,NULL,NULL))
56474664626SKris Kennaway 			{
56574664626SKris Kennaway 			if ((ERR_GET_REASON(ERR_peek_error()) ==
56674664626SKris Kennaway 				PEM_R_PROBLEMS_GETTING_PASSWORD) && (i < 3))
56774664626SKris Kennaway 				{
56874664626SKris Kennaway 				ERR_clear_error();
56974664626SKris Kennaway 				i++;
57074664626SKris Kennaway 				goto loop;
57174664626SKris Kennaway 				}
57274664626SKris Kennaway 			goto end;
57374664626SKris Kennaway 			}
57474664626SKris Kennaway 		BIO_printf(bio_err,"-----\n");
57574664626SKris Kennaway 		}
57674664626SKris Kennaway 
57774664626SKris Kennaway 	if (!newreq)
57874664626SKris Kennaway 		{
57974664626SKris Kennaway 		/* Since we are using a pre-existing certificate
58074664626SKris Kennaway 		 * request, the kludge 'format' info should not be
58174664626SKris Kennaway 		 * changed. */
58274664626SKris Kennaway 		kludge= -1;
58374664626SKris Kennaway 		if (infile == NULL)
58474664626SKris Kennaway 			BIO_set_fp(in,stdin,BIO_NOCLOSE);
58574664626SKris Kennaway 		else
58674664626SKris Kennaway 			{
58774664626SKris Kennaway 			if (BIO_read_filename(in,infile) <= 0)
58874664626SKris Kennaway 				{
58974664626SKris Kennaway 				perror(infile);
59074664626SKris Kennaway 				goto end;
59174664626SKris Kennaway 				}
59274664626SKris Kennaway 			}
59374664626SKris Kennaway 
59474664626SKris Kennaway 		if	(informat == FORMAT_ASN1)
59574664626SKris Kennaway 			req=d2i_X509_REQ_bio(in,NULL);
59674664626SKris Kennaway 		else if (informat == FORMAT_PEM)
59774664626SKris Kennaway 			req=PEM_read_bio_X509_REQ(in,NULL,NULL,NULL);
59874664626SKris Kennaway 		else
59974664626SKris Kennaway 			{
60074664626SKris Kennaway 			BIO_printf(bio_err,"bad input format specified for X509 request\n");
60174664626SKris Kennaway 			goto end;
60274664626SKris Kennaway 			}
60374664626SKris Kennaway 		if (req == NULL)
60474664626SKris Kennaway 			{
60574664626SKris Kennaway 			BIO_printf(bio_err,"unable to load X509 request\n");
60674664626SKris Kennaway 			goto end;
60774664626SKris Kennaway 			}
60874664626SKris Kennaway 		}
60974664626SKris Kennaway 
61074664626SKris Kennaway 	if (newreq || x509)
61174664626SKris Kennaway 		{
61274664626SKris Kennaway #ifndef NO_DSA
61374664626SKris Kennaway 		if (pkey->type == EVP_PKEY_DSA)
61474664626SKris Kennaway 			digest=EVP_dss1();
61574664626SKris Kennaway #endif
61674664626SKris Kennaway 
61774664626SKris Kennaway 		if (pkey == NULL)
61874664626SKris Kennaway 			{
61974664626SKris Kennaway 			BIO_printf(bio_err,"you need to specify a private key\n");
62074664626SKris Kennaway 			goto end;
62174664626SKris Kennaway 			}
62274664626SKris Kennaway 		if (req == NULL)
62374664626SKris Kennaway 			{
62474664626SKris Kennaway 			req=X509_REQ_new();
62574664626SKris Kennaway 			if (req == NULL)
62674664626SKris Kennaway 				{
62774664626SKris Kennaway 				goto end;
62874664626SKris Kennaway 				}
62974664626SKris Kennaway 
63074664626SKris Kennaway 			i=make_REQ(req,pkey,!x509);
63174664626SKris Kennaway 			if (kludge >= 0)
63274664626SKris Kennaway 				req->req_info->req_kludge=kludge;
63374664626SKris Kennaway 			if (!i)
63474664626SKris Kennaway 				{
63574664626SKris Kennaway 				BIO_printf(bio_err,"problems making Certificate Request\n");
63674664626SKris Kennaway 				goto end;
63774664626SKris Kennaway 				}
63874664626SKris Kennaway 			}
63974664626SKris Kennaway 		if (x509)
64074664626SKris Kennaway 			{
64174664626SKris Kennaway 			EVP_PKEY *tmppkey;
64274664626SKris Kennaway 			X509V3_CTX ext_ctx;
64374664626SKris Kennaway 			if ((x509ss=X509_new()) == NULL) goto end;
64474664626SKris Kennaway 
64574664626SKris Kennaway 			/* Set version to V3 */
64674664626SKris Kennaway 			if(!X509_set_version(x509ss, 2)) goto end;
64774664626SKris Kennaway 			ASN1_INTEGER_set(X509_get_serialNumber(x509ss),0L);
64874664626SKris Kennaway 
64974664626SKris Kennaway 			X509_set_issuer_name(x509ss,
65074664626SKris Kennaway 				X509_REQ_get_subject_name(req));
65174664626SKris Kennaway 			X509_gmtime_adj(X509_get_notBefore(x509ss),0);
65274664626SKris Kennaway 			X509_gmtime_adj(X509_get_notAfter(x509ss),
65374664626SKris Kennaway 				(long)60*60*24*days);
65474664626SKris Kennaway 			X509_set_subject_name(x509ss,
65574664626SKris Kennaway 				X509_REQ_get_subject_name(req));
65674664626SKris Kennaway 			tmppkey = X509_REQ_get_pubkey(req);
65774664626SKris Kennaway 			X509_set_pubkey(x509ss,tmppkey);
65874664626SKris Kennaway 			EVP_PKEY_free(tmppkey);
65974664626SKris Kennaway 
66074664626SKris Kennaway 			/* Set up V3 context struct */
66174664626SKris Kennaway 
66274664626SKris Kennaway 			X509V3_set_ctx(&ext_ctx, x509ss, x509ss, NULL, NULL, 0);
66374664626SKris Kennaway 			X509V3_set_conf_lhash(&ext_ctx, req_conf);
66474664626SKris Kennaway 
66574664626SKris Kennaway 			/* Add extensions */
66674664626SKris Kennaway 			if(extensions && !X509V3_EXT_add_conf(req_conf,
66774664626SKris Kennaway 				 	&ext_ctx, extensions, x509ss))
66874664626SKris Kennaway 			    {
66974664626SKris Kennaway 			    BIO_printf(bio_err,
67074664626SKris Kennaway 				       "Error Loading extension section %s\n",
67174664626SKris Kennaway 				       extensions);
67274664626SKris Kennaway 			    goto end;
67374664626SKris Kennaway 			    }
67474664626SKris Kennaway 
67574664626SKris Kennaway 			if (!(i=X509_sign(x509ss,pkey,digest)))
67674664626SKris Kennaway 				goto end;
67774664626SKris Kennaway 			}
67874664626SKris Kennaway 		else
67974664626SKris Kennaway 			{
68074664626SKris Kennaway 			if (!(i=X509_REQ_sign(req,pkey,digest)))
68174664626SKris Kennaway 				goto end;
68274664626SKris Kennaway 			}
68374664626SKris Kennaway 		}
68474664626SKris Kennaway 
68574664626SKris Kennaway 	if (verify && !x509)
68674664626SKris Kennaway 		{
68774664626SKris Kennaway 		int tmp=0;
68874664626SKris Kennaway 
68974664626SKris Kennaway 		if (pkey == NULL)
69074664626SKris Kennaway 			{
69174664626SKris Kennaway 			pkey=X509_REQ_get_pubkey(req);
69274664626SKris Kennaway 			tmp=1;
69374664626SKris Kennaway 			if (pkey == NULL) goto end;
69474664626SKris Kennaway 			}
69574664626SKris Kennaway 
69674664626SKris Kennaway 		i=X509_REQ_verify(req,pkey);
69774664626SKris Kennaway 		if (tmp) {
69874664626SKris Kennaway 			EVP_PKEY_free(pkey);
69974664626SKris Kennaway 			pkey=NULL;
70074664626SKris Kennaway 		}
70174664626SKris Kennaway 
70274664626SKris Kennaway 		if (i < 0)
70374664626SKris Kennaway 			{
70474664626SKris Kennaway 			goto end;
70574664626SKris Kennaway 			}
70674664626SKris Kennaway 		else if (i == 0)
70774664626SKris Kennaway 			{
70874664626SKris Kennaway 			BIO_printf(bio_err,"verify failure\n");
70974664626SKris Kennaway 			}
71074664626SKris Kennaway 		else /* if (i > 0) */
71174664626SKris Kennaway 			BIO_printf(bio_err,"verify OK\n");
71274664626SKris Kennaway 		}
71374664626SKris Kennaway 
71474664626SKris Kennaway 	if (noout && !text && !modulus)
71574664626SKris Kennaway 		{
71674664626SKris Kennaway 		ex=0;
71774664626SKris Kennaway 		goto end;
71874664626SKris Kennaway 		}
71974664626SKris Kennaway 
72074664626SKris Kennaway 	if (outfile == NULL)
72174664626SKris Kennaway 		BIO_set_fp(out,stdout,BIO_NOCLOSE);
72274664626SKris Kennaway 	else
72374664626SKris Kennaway 		{
72474664626SKris Kennaway 		if ((keyout != NULL) && (strcmp(outfile,keyout) == 0))
72574664626SKris Kennaway 			i=(int)BIO_append_filename(out,outfile);
72674664626SKris Kennaway 		else
72774664626SKris Kennaway 			i=(int)BIO_write_filename(out,outfile);
72874664626SKris Kennaway 		if (!i)
72974664626SKris Kennaway 			{
73074664626SKris Kennaway 			perror(outfile);
73174664626SKris Kennaway 			goto end;
73274664626SKris Kennaway 			}
73374664626SKris Kennaway 		}
73474664626SKris Kennaway 
73574664626SKris Kennaway 	if (text)
73674664626SKris Kennaway 		{
73774664626SKris Kennaway 		if (x509)
73874664626SKris Kennaway 			X509_print(out,x509ss);
73974664626SKris Kennaway 		else
74074664626SKris Kennaway 			X509_REQ_print(out,req);
74174664626SKris Kennaway 		}
74274664626SKris Kennaway 
74374664626SKris Kennaway 	if (modulus)
74474664626SKris Kennaway 		{
74574664626SKris Kennaway 		EVP_PKEY *pubkey;
74674664626SKris Kennaway 
74774664626SKris Kennaway 		if (x509)
74874664626SKris Kennaway 			pubkey=X509_get_pubkey(x509ss);
74974664626SKris Kennaway 		else
75074664626SKris Kennaway 			pubkey=X509_REQ_get_pubkey(req);
75174664626SKris Kennaway 		if (pubkey == NULL)
75274664626SKris Kennaway 			{
75374664626SKris Kennaway 			fprintf(stdout,"Modulus=unavailable\n");
75474664626SKris Kennaway 			goto end;
75574664626SKris Kennaway 			}
75674664626SKris Kennaway 		fprintf(stdout,"Modulus=");
75774664626SKris Kennaway #ifndef NO_RSA
75874664626SKris Kennaway 		if (pubkey->type == EVP_PKEY_RSA)
75974664626SKris Kennaway 			BN_print(out,pubkey->pkey.rsa->n);
76074664626SKris Kennaway 		else
76174664626SKris Kennaway #endif
76274664626SKris Kennaway 			fprintf(stdout,"Wrong Algorithm type");
76374664626SKris Kennaway 		fprintf(stdout,"\n");
76474664626SKris Kennaway 		}
76574664626SKris Kennaway 
76674664626SKris Kennaway 	if (!noout && !x509)
76774664626SKris Kennaway 		{
76874664626SKris Kennaway 		if 	(outformat == FORMAT_ASN1)
76974664626SKris Kennaway 			i=i2d_X509_REQ_bio(out,req);
77074664626SKris Kennaway 		else if (outformat == FORMAT_PEM)
77174664626SKris Kennaway 			i=PEM_write_bio_X509_REQ(out,req);
77274664626SKris Kennaway 		else	{
77374664626SKris Kennaway 			BIO_printf(bio_err,"bad output format specified for outfile\n");
77474664626SKris Kennaway 			goto end;
77574664626SKris Kennaway 			}
77674664626SKris Kennaway 		if (!i)
77774664626SKris Kennaway 			{
77874664626SKris Kennaway 			BIO_printf(bio_err,"unable to write X509 request\n");
77974664626SKris Kennaway 			goto end;
78074664626SKris Kennaway 			}
78174664626SKris Kennaway 		}
78274664626SKris Kennaway 	if (!noout && x509 && (x509ss != NULL))
78374664626SKris Kennaway 		{
78474664626SKris Kennaway 		if 	(outformat == FORMAT_ASN1)
78574664626SKris Kennaway 			i=i2d_X509_bio(out,x509ss);
78674664626SKris Kennaway 		else if (outformat == FORMAT_PEM)
78774664626SKris Kennaway 			i=PEM_write_bio_X509(out,x509ss);
78874664626SKris Kennaway 		else	{
78974664626SKris Kennaway 			BIO_printf(bio_err,"bad output format specified for outfile\n");
79074664626SKris Kennaway 			goto end;
79174664626SKris Kennaway 			}
79274664626SKris Kennaway 		if (!i)
79374664626SKris Kennaway 			{
79474664626SKris Kennaway 			BIO_printf(bio_err,"unable to write X509 certificate\n");
79574664626SKris Kennaway 			goto end;
79674664626SKris Kennaway 			}
79774664626SKris Kennaway 		}
79874664626SKris Kennaway 	ex=0;
79974664626SKris Kennaway end:
80074664626SKris Kennaway 	if (ex)
80174664626SKris Kennaway 		{
80274664626SKris Kennaway 		ERR_print_errors(bio_err);
80374664626SKris Kennaway 		}
80474664626SKris Kennaway 	if ((req_conf != NULL) && (req_conf != config)) CONF_free(req_conf);
80574664626SKris Kennaway 	BIO_free(in);
80674664626SKris Kennaway 	BIO_free(out);
80774664626SKris Kennaway 	EVP_PKEY_free(pkey);
80874664626SKris Kennaway 	X509_REQ_free(req);
80974664626SKris Kennaway 	X509_free(x509ss);
81074664626SKris Kennaway 	X509V3_EXT_cleanup();
81174664626SKris Kennaway 	OBJ_cleanup();
81274664626SKris Kennaway #ifndef NO_DSA
81374664626SKris Kennaway 	if (dsa_params != NULL) DSA_free(dsa_params);
81474664626SKris Kennaway #endif
81574664626SKris Kennaway 	EXIT(ex);
81674664626SKris Kennaway 	}
81774664626SKris Kennaway 
81874664626SKris Kennaway static int make_REQ(X509_REQ *req, EVP_PKEY *pkey, int attribs)
81974664626SKris Kennaway 	{
82074664626SKris Kennaway 	int ret=0,i;
82174664626SKris Kennaway 	char *p,*q;
82274664626SKris Kennaway 	X509_REQ_INFO *ri;
82374664626SKris Kennaway 	char buf[100];
82474664626SKris Kennaway 	int nid,min,max;
82574664626SKris Kennaway 	char *type,*def,*tmp,*value,*tmp_attr;
82674664626SKris Kennaway 	STACK_OF(CONF_VALUE) *sk, *attr=NULL;
82774664626SKris Kennaway 	CONF_VALUE *v;
82874664626SKris Kennaway 
82974664626SKris Kennaway 	tmp=CONF_get_string(req_conf,SECTION,DISTINGUISHED_NAME);
83074664626SKris Kennaway 	if (tmp == NULL)
83174664626SKris Kennaway 		{
83274664626SKris Kennaway 		BIO_printf(bio_err,"unable to find '%s' in config\n",
83374664626SKris Kennaway 			DISTINGUISHED_NAME);
83474664626SKris Kennaway 		goto err;
83574664626SKris Kennaway 		}
83674664626SKris Kennaway 	sk=CONF_get_section(req_conf,tmp);
83774664626SKris Kennaway 	if (sk == NULL)
83874664626SKris Kennaway 		{
83974664626SKris Kennaway 		BIO_printf(bio_err,"unable to get '%s' section\n",tmp);
84074664626SKris Kennaway 		goto err;
84174664626SKris Kennaway 		}
84274664626SKris Kennaway 
84374664626SKris Kennaway 	tmp_attr=CONF_get_string(req_conf,SECTION,ATTRIBUTES);
84474664626SKris Kennaway 	if (tmp_attr == NULL)
84574664626SKris Kennaway 		attr=NULL;
84674664626SKris Kennaway 	else
84774664626SKris Kennaway 		{
84874664626SKris Kennaway 		attr=CONF_get_section(req_conf,tmp_attr);
84974664626SKris Kennaway 		if (attr == NULL)
85074664626SKris Kennaway 			{
85174664626SKris Kennaway 			BIO_printf(bio_err,"unable to get '%s' section\n",tmp_attr);
85274664626SKris Kennaway 			goto err;
85374664626SKris Kennaway 			}
85474664626SKris Kennaway 		}
85574664626SKris Kennaway 
85674664626SKris Kennaway 	ri=req->req_info;
85774664626SKris Kennaway 
85874664626SKris Kennaway 	BIO_printf(bio_err,"You are about to be asked to enter information that will be incorporated\n");
85974664626SKris Kennaway 	BIO_printf(bio_err,"into your certificate request.\n");
86074664626SKris Kennaway 	BIO_printf(bio_err,"What you are about to enter is what is called a Distinguished Name or a DN.\n");
86174664626SKris Kennaway 	BIO_printf(bio_err,"There are quite a few fields but you can leave some blank\n");
86274664626SKris Kennaway 	BIO_printf(bio_err,"For some fields there will be a default value,\n");
86374664626SKris Kennaway 	BIO_printf(bio_err,"If you enter '.', the field will be left blank.\n");
86474664626SKris Kennaway 	BIO_printf(bio_err,"-----\n");
86574664626SKris Kennaway 
86674664626SKris Kennaway 	/* setup version number */
86774664626SKris Kennaway 	if (!ASN1_INTEGER_set(ri->version,0L)) goto err; /* version 1 */
86874664626SKris Kennaway 
86974664626SKris Kennaway 	if (sk_CONF_VALUE_num(sk))
87074664626SKris Kennaway 		{
87174664626SKris Kennaway 		i= -1;
87274664626SKris Kennaway start:		for (;;)
87374664626SKris Kennaway 			{
87474664626SKris Kennaway 			i++;
87574664626SKris Kennaway 			if (sk_CONF_VALUE_num(sk) <= i) break;
87674664626SKris Kennaway 
87774664626SKris Kennaway 			v=sk_CONF_VALUE_value(sk,i);
87874664626SKris Kennaway 			p=q=NULL;
87974664626SKris Kennaway 			type=v->name;
88074664626SKris Kennaway 			if(!check_end(type,"_min") || !check_end(type,"_max") ||
88174664626SKris Kennaway 				!check_end(type,"_default") ||
88274664626SKris Kennaway 					 !check_end(type,"_value")) continue;
88374664626SKris Kennaway 			/* Skip past any leading X. X: X, etc to allow for
88474664626SKris Kennaway 			 * multiple instances
88574664626SKris Kennaway 			 */
88674664626SKris Kennaway 			for(p = v->name; *p ; p++)
88774664626SKris Kennaway 				if ((*p == ':') || (*p == ',') ||
88874664626SKris Kennaway 							 (*p == '.')) {
88974664626SKris Kennaway 					p++;
89074664626SKris Kennaway 					if(*p) type = p;
89174664626SKris Kennaway 					break;
89274664626SKris Kennaway 				}
89374664626SKris Kennaway 			/* If OBJ not recognised ignore it */
89474664626SKris Kennaway 			if ((nid=OBJ_txt2nid(type)) == NID_undef) goto start;
89574664626SKris Kennaway 			sprintf(buf,"%s_default",v->name);
89674664626SKris Kennaway 			if ((def=CONF_get_string(req_conf,tmp,buf)) == NULL)
89774664626SKris Kennaway 				def="";
89874664626SKris Kennaway 
89974664626SKris Kennaway 			sprintf(buf,"%s_value",v->name);
90074664626SKris Kennaway 			if ((value=CONF_get_string(req_conf,tmp,buf)) == NULL)
90174664626SKris Kennaway 				value=NULL;
90274664626SKris Kennaway 
90374664626SKris Kennaway 			sprintf(buf,"%s_min",v->name);
90474664626SKris Kennaway 			min=(int)CONF_get_number(req_conf,tmp,buf);
90574664626SKris Kennaway 
90674664626SKris Kennaway 			sprintf(buf,"%s_max",v->name);
90774664626SKris Kennaway 			max=(int)CONF_get_number(req_conf,tmp,buf);
90874664626SKris Kennaway 
90974664626SKris Kennaway 			if (!add_DN_object(ri->subject,v->value,def,value,nid,
91074664626SKris Kennaway 				min,max))
91174664626SKris Kennaway 				goto err;
91274664626SKris Kennaway 			}
91374664626SKris Kennaway 		if (sk_X509_NAME_ENTRY_num(ri->subject->entries) == 0)
91474664626SKris Kennaway 			{
91574664626SKris Kennaway 			BIO_printf(bio_err,"error, no objects specified in config file\n");
91674664626SKris Kennaway 			goto err;
91774664626SKris Kennaway 			}
91874664626SKris Kennaway 
91974664626SKris Kennaway 		if (attribs)
92074664626SKris Kennaway 			{
92174664626SKris Kennaway 			if ((attr != NULL) && (sk_CONF_VALUE_num(attr) > 0))
92274664626SKris Kennaway 				{
92374664626SKris Kennaway 				BIO_printf(bio_err,"\nPlease enter the following 'extra' attributes\n");
92474664626SKris Kennaway 				BIO_printf(bio_err,"to be sent with your certificate request\n");
92574664626SKris Kennaway 				}
92674664626SKris Kennaway 
92774664626SKris Kennaway 			i= -1;
92874664626SKris Kennaway start2:			for (;;)
92974664626SKris Kennaway 				{
93074664626SKris Kennaway 				i++;
93174664626SKris Kennaway 				if ((attr == NULL) ||
93274664626SKris Kennaway 					    (sk_CONF_VALUE_num(attr) <= i))
93374664626SKris Kennaway 					break;
93474664626SKris Kennaway 
93574664626SKris Kennaway 				v=sk_CONF_VALUE_value(attr,i);
93674664626SKris Kennaway 				type=v->name;
93774664626SKris Kennaway 				if ((nid=OBJ_txt2nid(type)) == NID_undef)
93874664626SKris Kennaway 					goto start2;
93974664626SKris Kennaway 
94074664626SKris Kennaway 				sprintf(buf,"%s_default",type);
94174664626SKris Kennaway 				if ((def=CONF_get_string(req_conf,tmp_attr,buf))
94274664626SKris Kennaway 					== NULL)
94374664626SKris Kennaway 					def="";
94474664626SKris Kennaway 
94574664626SKris Kennaway 				sprintf(buf,"%s_value",type);
94674664626SKris Kennaway 				if ((value=CONF_get_string(req_conf,tmp_attr,buf))
94774664626SKris Kennaway 					== NULL)
94874664626SKris Kennaway 					value=NULL;
94974664626SKris Kennaway 
95074664626SKris Kennaway 				sprintf(buf,"%s_min",type);
95174664626SKris Kennaway 				min=(int)CONF_get_number(req_conf,tmp_attr,buf);
95274664626SKris Kennaway 
95374664626SKris Kennaway 				sprintf(buf,"%s_max",type);
95474664626SKris Kennaway 				max=(int)CONF_get_number(req_conf,tmp_attr,buf);
95574664626SKris Kennaway 
95674664626SKris Kennaway 				if (!add_attribute_object(ri->attributes,
95774664626SKris Kennaway 					v->value,def,value,nid,min,max))
95874664626SKris Kennaway 					goto err;
95974664626SKris Kennaway 				}
96074664626SKris Kennaway 			}
96174664626SKris Kennaway 		}
96274664626SKris Kennaway 	else
96374664626SKris Kennaway 		{
96474664626SKris Kennaway 		BIO_printf(bio_err,"No template, please set one up.\n");
96574664626SKris Kennaway 		goto err;
96674664626SKris Kennaway 		}
96774664626SKris Kennaway 
96874664626SKris Kennaway 	X509_REQ_set_pubkey(req,pkey);
96974664626SKris Kennaway 
97074664626SKris Kennaway 	ret=1;
97174664626SKris Kennaway err:
97274664626SKris Kennaway 	return(ret);
97374664626SKris Kennaway 	}
97474664626SKris Kennaway 
97574664626SKris Kennaway static int add_DN_object(X509_NAME *n, char *text, char *def, char *value,
97674664626SKris Kennaway 	     int nid, int min, int max)
97774664626SKris Kennaway 	{
97874664626SKris Kennaway 	int i,j,ret=0;
97974664626SKris Kennaway 	X509_NAME_ENTRY *ne=NULL;
98074664626SKris Kennaway 	MS_STATIC char buf[1024];
98174664626SKris Kennaway 
98274664626SKris Kennaway 	BIO_printf(bio_err,"%s [%s]:",text,def);
98374664626SKris Kennaway 	(void)BIO_flush(bio_err);
98474664626SKris Kennaway 	if (value != NULL)
98574664626SKris Kennaway 		{
98674664626SKris Kennaway 		strcpy(buf,value);
98774664626SKris Kennaway 		strcat(buf,"\n");
98874664626SKris Kennaway 		BIO_printf(bio_err,"%s\n",value);
98974664626SKris Kennaway 		}
99074664626SKris Kennaway 	else
99174664626SKris Kennaway 		{
99274664626SKris Kennaway 		buf[0]='\0';
99374664626SKris Kennaway 		fgets(buf,1024,stdin);
99474664626SKris Kennaway 		}
99574664626SKris Kennaway 
99674664626SKris Kennaway 	if (buf[0] == '\0') return(0);
99774664626SKris Kennaway 	else if (buf[0] == '\n')
99874664626SKris Kennaway 		{
99974664626SKris Kennaway 		if ((def == NULL) || (def[0] == '\0'))
100074664626SKris Kennaway 			return(1);
100174664626SKris Kennaway 		strcpy(buf,def);
100274664626SKris Kennaway 		strcat(buf,"\n");
100374664626SKris Kennaway 		}
100474664626SKris Kennaway 	else if ((buf[0] == '.') && (buf[1] == '\n')) return(1);
100574664626SKris Kennaway 
100674664626SKris Kennaway 	i=strlen(buf);
100774664626SKris Kennaway 	if (buf[i-1] != '\n')
100874664626SKris Kennaway 		{
100974664626SKris Kennaway 		BIO_printf(bio_err,"weird input :-(\n");
101074664626SKris Kennaway 		return(0);
101174664626SKris Kennaway 		}
101274664626SKris Kennaway 	buf[--i]='\0';
101374664626SKris Kennaway 
101474664626SKris Kennaway 	j=ASN1_PRINTABLE_type((unsigned char *)buf,-1);
101574664626SKris Kennaway 	if (req_fix_data(nid,&j,i,min,max) == 0)
101674664626SKris Kennaway 		goto err;
101774664626SKris Kennaway #ifdef CHARSET_EBCDIC
101874664626SKris Kennaway 	ebcdic2ascii(buf, buf, i);
101974664626SKris Kennaway #endif
102074664626SKris Kennaway 	if ((ne=X509_NAME_ENTRY_create_by_NID(NULL,nid,j,(unsigned char *)buf,
102174664626SKris Kennaway 		strlen(buf)))
102274664626SKris Kennaway 		== NULL) goto err;
102374664626SKris Kennaway 	if (!X509_NAME_add_entry(n,ne,X509_NAME_entry_count(n),0))
102474664626SKris Kennaway 		goto err;
102574664626SKris Kennaway 
102674664626SKris Kennaway 	ret=1;
102774664626SKris Kennaway err:
102874664626SKris Kennaway 	if (ne != NULL) X509_NAME_ENTRY_free(ne);
102974664626SKris Kennaway 	return(ret);
103074664626SKris Kennaway 	}
103174664626SKris Kennaway 
103274664626SKris Kennaway static int add_attribute_object(STACK_OF(X509_ATTRIBUTE) *n, char *text,
103374664626SKris Kennaway 				char *def, char *value, int nid, int min,
103474664626SKris Kennaway 				int max)
103574664626SKris Kennaway 	{
103674664626SKris Kennaway 	int i,z;
103774664626SKris Kennaway 	X509_ATTRIBUTE *xa=NULL;
103874664626SKris Kennaway 	static char buf[1024];
103974664626SKris Kennaway 	ASN1_BIT_STRING *bs=NULL;
104074664626SKris Kennaway 	ASN1_TYPE *at=NULL;
104174664626SKris Kennaway 
104274664626SKris Kennaway start:
104374664626SKris Kennaway 	BIO_printf(bio_err,"%s [%s]:",text,def);
104474664626SKris Kennaway 	(void)BIO_flush(bio_err);
104574664626SKris Kennaway 	if (value != NULL)
104674664626SKris Kennaway 		{
104774664626SKris Kennaway 		strcpy(buf,value);
104874664626SKris Kennaway 		strcat(buf,"\n");
104974664626SKris Kennaway 		BIO_printf(bio_err,"%s\n",value);
105074664626SKris Kennaway 		}
105174664626SKris Kennaway 	else
105274664626SKris Kennaway 		{
105374664626SKris Kennaway 		buf[0]='\0';
105474664626SKris Kennaway 		fgets(buf,1024,stdin);
105574664626SKris Kennaway 		}
105674664626SKris Kennaway 
105774664626SKris Kennaway 	if (buf[0] == '\0') return(0);
105874664626SKris Kennaway 	else if (buf[0] == '\n')
105974664626SKris Kennaway 		{
106074664626SKris Kennaway 		if ((def == NULL) || (def[0] == '\0'))
106174664626SKris Kennaway 			return(1);
106274664626SKris Kennaway 		strcpy(buf,def);
106374664626SKris Kennaway 		strcat(buf,"\n");
106474664626SKris Kennaway 		}
106574664626SKris Kennaway 	else if ((buf[0] == '.') && (buf[1] == '\n')) return(1);
106674664626SKris Kennaway 
106774664626SKris Kennaway 	i=strlen(buf);
106874664626SKris Kennaway 	if (buf[i-1] != '\n')
106974664626SKris Kennaway 		{
107074664626SKris Kennaway 		BIO_printf(bio_err,"weird input :-(\n");
107174664626SKris Kennaway 		return(0);
107274664626SKris Kennaway 		}
107374664626SKris Kennaway 	buf[--i]='\0';
107474664626SKris Kennaway 
107574664626SKris Kennaway 	/* add object plus value */
107674664626SKris Kennaway 	if ((xa=X509_ATTRIBUTE_new()) == NULL)
107774664626SKris Kennaway 		goto err;
107874664626SKris Kennaway 	if ((xa->value.set=sk_ASN1_TYPE_new_null()) == NULL)
107974664626SKris Kennaway 		goto err;
108074664626SKris Kennaway 	xa->set=1;
108174664626SKris Kennaway 
108274664626SKris Kennaway 	if (xa->object != NULL) ASN1_OBJECT_free(xa->object);
108374664626SKris Kennaway 	xa->object=OBJ_nid2obj(nid);
108474664626SKris Kennaway 
108574664626SKris Kennaway 	if ((bs=ASN1_BIT_STRING_new()) == NULL) goto err;
108674664626SKris Kennaway 
108774664626SKris Kennaway 	bs->type=ASN1_PRINTABLE_type((unsigned char *)buf,-1);
108874664626SKris Kennaway 
108974664626SKris Kennaway 	z=req_fix_data(nid,&bs->type,i,min,max);
109074664626SKris Kennaway 	if (z == 0)
109174664626SKris Kennaway 		{
109274664626SKris Kennaway 		if (value == NULL)
109374664626SKris Kennaway 			goto start;
109474664626SKris Kennaway 		else	goto err;
109574664626SKris Kennaway 		}
109674664626SKris Kennaway 
109774664626SKris Kennaway 	if (!ASN1_STRING_set(bs,(unsigned char *)buf,i+1))
109874664626SKris Kennaway 		{ BIO_printf(bio_err,"Malloc failure\n"); goto err; }
109974664626SKris Kennaway 
110074664626SKris Kennaway 	if ((at=ASN1_TYPE_new()) == NULL)
110174664626SKris Kennaway 		{ BIO_printf(bio_err,"Malloc failure\n"); goto err; }
110274664626SKris Kennaway 
110374664626SKris Kennaway 	ASN1_TYPE_set(at,bs->type,(char *)bs);
110474664626SKris Kennaway 	sk_ASN1_TYPE_push(xa->value.set,at);
110574664626SKris Kennaway 	bs=NULL;
110674664626SKris Kennaway 	at=NULL;
110774664626SKris Kennaway 	/* only one item per attribute */
110874664626SKris Kennaway 
110974664626SKris Kennaway 	if (!sk_X509_ATTRIBUTE_push(n,xa)) goto err;
111074664626SKris Kennaway 	return(1);
111174664626SKris Kennaway err:
111274664626SKris Kennaway 	if (xa != NULL) X509_ATTRIBUTE_free(xa);
111374664626SKris Kennaway 	if (at != NULL) ASN1_TYPE_free(at);
111474664626SKris Kennaway 	if (bs != NULL) ASN1_BIT_STRING_free(bs);
111574664626SKris Kennaway 	return(0);
111674664626SKris Kennaway 	}
111774664626SKris Kennaway 
111874664626SKris Kennaway static void MS_CALLBACK req_cb(int p, int n, void *arg)
111974664626SKris Kennaway 	{
112074664626SKris Kennaway 	char c='*';
112174664626SKris Kennaway 
112274664626SKris Kennaway 	if (p == 0) c='.';
112374664626SKris Kennaway 	if (p == 1) c='+';
112474664626SKris Kennaway 	if (p == 2) c='*';
112574664626SKris Kennaway 	if (p == 3) c='\n';
112674664626SKris Kennaway 	BIO_write((BIO *)arg,&c,1);
112774664626SKris Kennaway 	(void)BIO_flush((BIO *)arg);
112874664626SKris Kennaway #ifdef LINT
112974664626SKris Kennaway 	p=n;
113074664626SKris Kennaway #endif
113174664626SKris Kennaway 	}
113274664626SKris Kennaway 
113374664626SKris Kennaway static int req_fix_data(int nid, int *type, int len, int min, int max)
113474664626SKris Kennaway 	{
113574664626SKris Kennaway 	if (nid == NID_pkcs9_emailAddress)
113674664626SKris Kennaway 		*type=V_ASN1_IA5STRING;
113774664626SKris Kennaway 	if ((nid == NID_commonName) && (*type == V_ASN1_IA5STRING))
113874664626SKris Kennaway 		*type=V_ASN1_T61STRING;
113974664626SKris Kennaway 	if ((nid == NID_pkcs9_challengePassword) &&
114074664626SKris Kennaway 		(*type == V_ASN1_IA5STRING))
114174664626SKris Kennaway 		*type=V_ASN1_T61STRING;
114274664626SKris Kennaway 
114374664626SKris Kennaway 	if ((nid == NID_pkcs9_unstructuredName) &&
114474664626SKris Kennaway 		(*type == V_ASN1_T61STRING))
114574664626SKris Kennaway 		{
114674664626SKris Kennaway 		BIO_printf(bio_err,"invalid characters in string, please re-enter the string\n");
114774664626SKris Kennaway 		return(0);
114874664626SKris Kennaway 		}
114974664626SKris Kennaway 	if (nid == NID_pkcs9_unstructuredName)
115074664626SKris Kennaway 		*type=V_ASN1_IA5STRING;
115174664626SKris Kennaway 
115274664626SKris Kennaway 	if (len < min)
115374664626SKris Kennaway 		{
115474664626SKris Kennaway 		BIO_printf(bio_err,"string is too short, it needs to be at least %d bytes long\n",min);
115574664626SKris Kennaway 		return(0);
115674664626SKris Kennaway 		}
115774664626SKris Kennaway 	if ((max != 0) && (len > max))
115874664626SKris Kennaway 		{
115974664626SKris Kennaway 		BIO_printf(bio_err,"string is too long, it needs to be less than  %d bytes long\n",max);
116074664626SKris Kennaway 		return(0);
116174664626SKris Kennaway 		}
116274664626SKris Kennaway 	return(1);
116374664626SKris Kennaway 	}
116474664626SKris Kennaway 
116574664626SKris Kennaway /* Check if the end of a string matches 'end' */
116674664626SKris Kennaway static int check_end(char *str, char *end)
116774664626SKris Kennaway {
116874664626SKris Kennaway 	int elen, slen;
116974664626SKris Kennaway 	char *tmp;
117074664626SKris Kennaway 	elen = strlen(end);
117174664626SKris Kennaway 	slen = strlen(str);
117274664626SKris Kennaway 	if(elen > slen) return 1;
117374664626SKris Kennaway 	tmp = str + slen - elen;
117474664626SKris Kennaway 	return strcmp(tmp, end);
117574664626SKris Kennaway }
117674664626SKris Kennaway 
117774664626SKris Kennaway static int add_oid_section(LHASH *conf)
117874664626SKris Kennaway {
117974664626SKris Kennaway 	char *p;
118074664626SKris Kennaway 	STACK_OF(CONF_VALUE) *sktmp;
118174664626SKris Kennaway 	CONF_VALUE *cnf;
118274664626SKris Kennaway 	int i;
118374664626SKris Kennaway 	if(!(p=CONF_get_string(conf,NULL,"oid_section"))) return 1;
118474664626SKris Kennaway 	if(!(sktmp = CONF_get_section(conf, p))) {
118574664626SKris Kennaway 		BIO_printf(bio_err, "problem loading oid section %s\n", p);
118674664626SKris Kennaway 		return 0;
118774664626SKris Kennaway 	}
118874664626SKris Kennaway 	for(i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
118974664626SKris Kennaway 		cnf = sk_CONF_VALUE_value(sktmp, i);
119074664626SKris Kennaway 		if(OBJ_create(cnf->value, cnf->name, cnf->name) == NID_undef) {
119174664626SKris Kennaway 			BIO_printf(bio_err, "problem creating object %s=%s\n",
119274664626SKris Kennaway 							 cnf->name, cnf->value);
119374664626SKris Kennaway 			return 0;
119474664626SKris Kennaway 		}
119574664626SKris Kennaway 	}
119674664626SKris Kennaway 	return 1;
119774664626SKris Kennaway }
1198