xref: /onnv-gate/usr/src/common/openssl/apps/ca.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* apps/ca.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
59*2139Sjp161948 /*
60*2139Sjp161948  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
61*2139Sjp161948  * Use is subject to license terms.
62*2139Sjp161948  */
63*2139Sjp161948 
64*2139Sjp161948 #pragma ident	"%Z%%M%	%I%	%E% SMI"
65*2139Sjp161948 
660Sstevel@tonic-gate /* The PPKI stuff has been donated by Jeff Barber <jeffb@issl.atl.hp.com> */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #include <stdio.h>
690Sstevel@tonic-gate #include <stdlib.h>
700Sstevel@tonic-gate #include <string.h>
710Sstevel@tonic-gate #include <ctype.h>
720Sstevel@tonic-gate #include <sys/types.h>
730Sstevel@tonic-gate #include <sys/stat.h>
740Sstevel@tonic-gate #include <openssl/conf.h>
750Sstevel@tonic-gate #include <openssl/bio.h>
760Sstevel@tonic-gate #include <openssl/err.h>
770Sstevel@tonic-gate #include <openssl/bn.h>
780Sstevel@tonic-gate #include <openssl/txt_db.h>
790Sstevel@tonic-gate #include <openssl/evp.h>
800Sstevel@tonic-gate #include <openssl/x509.h>
810Sstevel@tonic-gate #include <openssl/x509v3.h>
820Sstevel@tonic-gate #include <openssl/objects.h>
830Sstevel@tonic-gate #include <openssl/ocsp.h>
840Sstevel@tonic-gate #include <openssl/pem.h>
850Sstevel@tonic-gate 
860Sstevel@tonic-gate #ifndef W_OK
870Sstevel@tonic-gate #  ifdef OPENSSL_SYS_VMS
880Sstevel@tonic-gate #    if defined(__DECC)
890Sstevel@tonic-gate #      include <unistd.h>
900Sstevel@tonic-gate #    else
910Sstevel@tonic-gate #      include <unixlib.h>
920Sstevel@tonic-gate #    endif
93*2139Sjp161948 #  elif !defined(OPENSSL_SYS_VXWORKS) && !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_NETWARE)
940Sstevel@tonic-gate #    include <sys/file.h>
950Sstevel@tonic-gate #  endif
960Sstevel@tonic-gate #endif
970Sstevel@tonic-gate 
980Sstevel@tonic-gate #include "apps.h"
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate #ifndef W_OK
1010Sstevel@tonic-gate #  define F_OK 0
1020Sstevel@tonic-gate #  define X_OK 1
1030Sstevel@tonic-gate #  define W_OK 2
1040Sstevel@tonic-gate #  define R_OK 4
1050Sstevel@tonic-gate #endif
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate #undef PROG
1080Sstevel@tonic-gate #define PROG ca_main
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate #define BASE_SECTION	"ca"
1110Sstevel@tonic-gate #define CONFIG_FILE "openssl.cnf"
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate #define ENV_DEFAULT_CA		"default_ca"
1140Sstevel@tonic-gate 
115*2139Sjp161948 #define STRING_MASK	"string_mask"
116*2139Sjp161948 #define UTF8_IN			"utf8"
117*2139Sjp161948 
1180Sstevel@tonic-gate #define ENV_DIR			"dir"
1190Sstevel@tonic-gate #define ENV_CERTS		"certs"
1200Sstevel@tonic-gate #define ENV_CRL_DIR		"crl_dir"
1210Sstevel@tonic-gate #define ENV_CA_DB		"CA_DB"
1220Sstevel@tonic-gate #define ENV_NEW_CERTS_DIR	"new_certs_dir"
1230Sstevel@tonic-gate #define ENV_CERTIFICATE 	"certificate"
1240Sstevel@tonic-gate #define ENV_SERIAL		"serial"
1250Sstevel@tonic-gate #define ENV_CRLNUMBER		"crlnumber"
1260Sstevel@tonic-gate #define ENV_CRL			"crl"
1270Sstevel@tonic-gate #define ENV_PRIVATE_KEY		"private_key"
1280Sstevel@tonic-gate #define ENV_RANDFILE		"RANDFILE"
1290Sstevel@tonic-gate #define ENV_DEFAULT_DAYS 	"default_days"
1300Sstevel@tonic-gate #define ENV_DEFAULT_STARTDATE 	"default_startdate"
1310Sstevel@tonic-gate #define ENV_DEFAULT_ENDDATE 	"default_enddate"
1320Sstevel@tonic-gate #define ENV_DEFAULT_CRL_DAYS 	"default_crl_days"
1330Sstevel@tonic-gate #define ENV_DEFAULT_CRL_HOURS 	"default_crl_hours"
1340Sstevel@tonic-gate #define ENV_DEFAULT_MD		"default_md"
1350Sstevel@tonic-gate #define ENV_DEFAULT_EMAIL_DN	"email_in_dn"
1360Sstevel@tonic-gate #define ENV_PRESERVE		"preserve"
1370Sstevel@tonic-gate #define ENV_POLICY      	"policy"
1380Sstevel@tonic-gate #define ENV_EXTENSIONS      	"x509_extensions"
1390Sstevel@tonic-gate #define ENV_CRLEXT      	"crl_extensions"
1400Sstevel@tonic-gate #define ENV_MSIE_HACK		"msie_hack"
1410Sstevel@tonic-gate #define ENV_NAMEOPT		"name_opt"
1420Sstevel@tonic-gate #define ENV_CERTOPT		"cert_opt"
1430Sstevel@tonic-gate #define ENV_EXTCOPY		"copy_extensions"
144*2139Sjp161948 #define ENV_UNIQUE_SUBJECT	"unique_subject"
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate #define ENV_DATABASE		"database"
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate /* Additional revocation information types */
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate #define REV_NONE		0	/* No addditional information */
1510Sstevel@tonic-gate #define REV_CRL_REASON		1	/* Value is CRL reason code */
1520Sstevel@tonic-gate #define REV_HOLD		2	/* Value is hold instruction */
1530Sstevel@tonic-gate #define REV_KEY_COMPROMISE	3	/* Value is cert key compromise time */
1540Sstevel@tonic-gate #define REV_CA_COMPROMISE	4	/* Value is CA key compromise time */
1550Sstevel@tonic-gate 
156*2139Sjp161948 static const char *ca_usage[]={
1570Sstevel@tonic-gate "usage: ca args\n",
1580Sstevel@tonic-gate "\n",
1590Sstevel@tonic-gate " -verbose        - Talk alot while doing things\n",
1600Sstevel@tonic-gate " -config file    - A config file\n",
1610Sstevel@tonic-gate " -name arg       - The particular CA definition to use\n",
1620Sstevel@tonic-gate " -gencrl         - Generate a new CRL\n",
1630Sstevel@tonic-gate " -crldays days   - Days is when the next CRL is due\n",
1640Sstevel@tonic-gate " -crlhours hours - Hours is when the next CRL is due\n",
1650Sstevel@tonic-gate " -startdate YYMMDDHHMMSSZ  - certificate validity notBefore\n",
1660Sstevel@tonic-gate " -enddate YYMMDDHHMMSSZ    - certificate validity notAfter (overrides -days)\n",
1670Sstevel@tonic-gate " -days arg       - number of days to certify the certificate for\n",
1680Sstevel@tonic-gate " -md arg         - md to use, one of md2, md5, sha or sha1\n",
1690Sstevel@tonic-gate " -policy arg     - The CA 'policy' to support\n",
1700Sstevel@tonic-gate " -keyfile arg    - private key file\n",
1710Sstevel@tonic-gate " -keyform arg    - private key file format (PEM or ENGINE)\n",
1720Sstevel@tonic-gate " -key arg        - key to decode the private key if it is encrypted\n",
1730Sstevel@tonic-gate " -cert file      - The CA certificate\n",
174*2139Sjp161948 " -selfsign       - sign a certificate with the key associated with it\n",
1750Sstevel@tonic-gate " -in file        - The input PEM encoded certificate request(s)\n",
1760Sstevel@tonic-gate " -out file       - Where to put the output file(s)\n",
1770Sstevel@tonic-gate " -outdir dir     - Where to put output certificates\n",
1780Sstevel@tonic-gate " -infiles ....   - The last argument, requests to process\n",
1790Sstevel@tonic-gate " -spkac file     - File contains DN and signed public key and challenge\n",
1800Sstevel@tonic-gate " -ss_cert file   - File contains a self signed cert to sign\n",
1810Sstevel@tonic-gate " -preserveDN     - Don't re-order the DN\n",
1820Sstevel@tonic-gate " -noemailDN      - Don't add the EMAIL field into certificate' subject\n",
1830Sstevel@tonic-gate " -batch          - Don't ask questions\n",
1840Sstevel@tonic-gate " -msie_hack      - msie modifications to handle all those universal strings\n",
1850Sstevel@tonic-gate " -revoke file    - Revoke a certificate (given in file)\n",
1860Sstevel@tonic-gate " -subj arg       - Use arg instead of request's subject\n",
187*2139Sjp161948 " -utf8           - input characters are UTF8 (default ASCII)\n",
188*2139Sjp161948 " -multivalue-rdn - enable support for multivalued RDNs\n",
1890Sstevel@tonic-gate " -extensions ..  - Extension section (override value in config file)\n",
1900Sstevel@tonic-gate " -extfile file   - Configuration file with X509v3 extentions to add\n",
1910Sstevel@tonic-gate " -crlexts ..     - CRL extension section (override value in config file)\n",
1920Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1930Sstevel@tonic-gate " -engine e       - use engine e, possibly a hardware device.\n",
1940Sstevel@tonic-gate #endif
1950Sstevel@tonic-gate " -status serial  - Shows certificate status given the serial number\n",
1960Sstevel@tonic-gate " -updatedb       - Updates db for expired certificates\n",
1970Sstevel@tonic-gate NULL
1980Sstevel@tonic-gate };
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate #ifdef EFENCE
2010Sstevel@tonic-gate extern int EF_PROTECT_FREE;
2020Sstevel@tonic-gate extern int EF_PROTECT_BELOW;
2030Sstevel@tonic-gate extern int EF_ALIGNMENT;
2040Sstevel@tonic-gate #endif
2050Sstevel@tonic-gate 
206*2139Sjp161948 static void lookup_fail(const char *name, const char *tag);
2070Sstevel@tonic-gate static int certify(X509 **xret, char *infile,EVP_PKEY *pkey,X509 *x509,
2080Sstevel@tonic-gate 		   const EVP_MD *dgst,STACK_OF(CONF_VALUE) *policy,CA_DB *db,
209*2139Sjp161948 		   BIGNUM *serial, char *subj,unsigned long chtype, int multirdn, int email_dn, char *startdate,
2100Sstevel@tonic-gate 		   char *enddate, long days, int batch, char *ext_sect, CONF *conf,
2110Sstevel@tonic-gate 		   int verbose, unsigned long certopt, unsigned long nameopt,
212*2139Sjp161948 		   int default_op, int ext_copy, int selfsign);
2130Sstevel@tonic-gate static int certify_cert(X509 **xret, char *infile,EVP_PKEY *pkey,X509 *x509,
2140Sstevel@tonic-gate 			const EVP_MD *dgst,STACK_OF(CONF_VALUE) *policy,
215*2139Sjp161948 			CA_DB *db, BIGNUM *serial, char *subj,unsigned long chtype, int multirdn, int email_dn,
2160Sstevel@tonic-gate 			char *startdate, char *enddate, long days, int batch,
2170Sstevel@tonic-gate 			char *ext_sect, CONF *conf,int verbose, unsigned long certopt,
2180Sstevel@tonic-gate 			unsigned long nameopt, int default_op, int ext_copy,
2190Sstevel@tonic-gate 			ENGINE *e);
2200Sstevel@tonic-gate static int certify_spkac(X509 **xret, char *infile,EVP_PKEY *pkey,X509 *x509,
2210Sstevel@tonic-gate 			 const EVP_MD *dgst,STACK_OF(CONF_VALUE) *policy,
222*2139Sjp161948 			 CA_DB *db, BIGNUM *serial,char *subj,unsigned long chtype, int multirdn, int email_dn,
2230Sstevel@tonic-gate 			 char *startdate, char *enddate, long days, char *ext_sect,
2240Sstevel@tonic-gate 			 CONF *conf, int verbose, unsigned long certopt,
2250Sstevel@tonic-gate 			 unsigned long nameopt, int default_op, int ext_copy);
2260Sstevel@tonic-gate static int fix_data(int nid, int *type);
2270Sstevel@tonic-gate static void write_new_certificate(BIO *bp, X509 *x, int output_der, int notext);
2280Sstevel@tonic-gate static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509, const EVP_MD *dgst,
229*2139Sjp161948 	STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial,char *subj,unsigned long chtype, int multirdn,
2300Sstevel@tonic-gate 	int email_dn, char *startdate, char *enddate, long days, int batch,
2310Sstevel@tonic-gate        	int verbose, X509_REQ *req, char *ext_sect, CONF *conf,
2320Sstevel@tonic-gate 	unsigned long certopt, unsigned long nameopt, int default_op,
233*2139Sjp161948 	int ext_copy, int selfsign);
2340Sstevel@tonic-gate static int do_revoke(X509 *x509, CA_DB *db, int ext, char *extval);
2350Sstevel@tonic-gate static int get_certificate_status(const char *ser_status, CA_DB *db);
2360Sstevel@tonic-gate static int do_updatedb(CA_DB *db);
2370Sstevel@tonic-gate static int check_time_format(char *str);
2380Sstevel@tonic-gate char *make_revocation_str(int rev_type, char *rev_arg);
239*2139Sjp161948 int make_revoked(X509_REVOKED *rev, const char *str);
2400Sstevel@tonic-gate int old_entry_print(BIO *bp, ASN1_OBJECT *obj, ASN1_STRING *str);
2410Sstevel@tonic-gate static CONF *conf=NULL;
2420Sstevel@tonic-gate static CONF *extconf=NULL;
2430Sstevel@tonic-gate static char *section=NULL;
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate static int preserve=0;
2460Sstevel@tonic-gate static int msie_hack=0;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate int MAIN(int, char **);
2500Sstevel@tonic-gate 
MAIN(int argc,char ** argv)2510Sstevel@tonic-gate int MAIN(int argc, char **argv)
2520Sstevel@tonic-gate 	{
2530Sstevel@tonic-gate 	ENGINE *e = NULL;
2540Sstevel@tonic-gate 	char *key=NULL,*passargin=NULL;
255*2139Sjp161948 	int create_ser = 0;
2560Sstevel@tonic-gate 	int free_key = 0;
2570Sstevel@tonic-gate 	int total=0;
2580Sstevel@tonic-gate 	int total_done=0;
2590Sstevel@tonic-gate 	int badops=0;
2600Sstevel@tonic-gate 	int ret=1;
2610Sstevel@tonic-gate 	int email_dn=1;
2620Sstevel@tonic-gate 	int req=0;
2630Sstevel@tonic-gate 	int verbose=0;
2640Sstevel@tonic-gate 	int gencrl=0;
2650Sstevel@tonic-gate 	int dorevoke=0;
2660Sstevel@tonic-gate 	int doupdatedb=0;
2670Sstevel@tonic-gate 	long crldays=0;
2680Sstevel@tonic-gate 	long crlhours=0;
2690Sstevel@tonic-gate 	long errorline= -1;
2700Sstevel@tonic-gate 	char *configfile=NULL;
2710Sstevel@tonic-gate 	char *md=NULL;
2720Sstevel@tonic-gate 	char *policy=NULL;
2730Sstevel@tonic-gate 	char *keyfile=NULL;
2740Sstevel@tonic-gate 	char *certfile=NULL;
2750Sstevel@tonic-gate 	int keyform=FORMAT_PEM;
2760Sstevel@tonic-gate 	char *infile=NULL;
2770Sstevel@tonic-gate 	char *spkac_file=NULL;
2780Sstevel@tonic-gate 	char *ss_cert_file=NULL;
2790Sstevel@tonic-gate 	char *ser_status=NULL;
2800Sstevel@tonic-gate 	EVP_PKEY *pkey=NULL;
2810Sstevel@tonic-gate 	int output_der = 0;
2820Sstevel@tonic-gate 	char *outfile=NULL;
2830Sstevel@tonic-gate 	char *outdir=NULL;
2840Sstevel@tonic-gate 	char *serialfile=NULL;
2850Sstevel@tonic-gate 	char *crlnumberfile=NULL;
2860Sstevel@tonic-gate 	char *extensions=NULL;
2870Sstevel@tonic-gate 	char *extfile=NULL;
2880Sstevel@tonic-gate 	char *subj=NULL;
289*2139Sjp161948 	unsigned long chtype = MBSTRING_ASC;
290*2139Sjp161948 	int multirdn = 0;
2910Sstevel@tonic-gate 	char *tmp_email_dn=NULL;
2920Sstevel@tonic-gate 	char *crl_ext=NULL;
2930Sstevel@tonic-gate 	int rev_type = REV_NONE;
2940Sstevel@tonic-gate 	char *rev_arg = NULL;
2950Sstevel@tonic-gate 	BIGNUM *serial=NULL;
2960Sstevel@tonic-gate 	BIGNUM *crlnumber=NULL;
2970Sstevel@tonic-gate 	char *startdate=NULL;
2980Sstevel@tonic-gate 	char *enddate=NULL;
2990Sstevel@tonic-gate 	long days=0;
3000Sstevel@tonic-gate 	int batch=0;
3010Sstevel@tonic-gate 	int notext=0;
3020Sstevel@tonic-gate 	unsigned long nameopt = 0, certopt = 0;
3030Sstevel@tonic-gate 	int default_op = 1;
3040Sstevel@tonic-gate 	int ext_copy = EXT_COPY_NONE;
305*2139Sjp161948 	int selfsign = 0;
306*2139Sjp161948 	X509 *x509=NULL, *x509p = NULL;
3070Sstevel@tonic-gate 	X509 *x=NULL;
3080Sstevel@tonic-gate 	BIO *in=NULL,*out=NULL,*Sout=NULL,*Cout=NULL;
3090Sstevel@tonic-gate 	char *dbfile=NULL;
3100Sstevel@tonic-gate 	CA_DB *db=NULL;
3110Sstevel@tonic-gate 	X509_CRL *crl=NULL;
3120Sstevel@tonic-gate 	X509_REVOKED *r=NULL;
3130Sstevel@tonic-gate 	ASN1_TIME *tmptm;
3140Sstevel@tonic-gate 	ASN1_INTEGER *tmpser;
315*2139Sjp161948 	char *f;
316*2139Sjp161948 	const char *p, **pp;
3170Sstevel@tonic-gate 	int i,j;
3180Sstevel@tonic-gate 	const EVP_MD *dgst=NULL;
3190Sstevel@tonic-gate 	STACK_OF(CONF_VALUE) *attribs=NULL;
3200Sstevel@tonic-gate 	STACK_OF(X509) *cert_sk=NULL;
3210Sstevel@tonic-gate #undef BSIZE
3220Sstevel@tonic-gate #define BSIZE 256
3230Sstevel@tonic-gate 	MS_STATIC char buf[3][BSIZE];
3240Sstevel@tonic-gate 	char *randfile=NULL;
3250Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
3260Sstevel@tonic-gate 	char *engine = NULL;
3270Sstevel@tonic-gate #endif
3280Sstevel@tonic-gate 	char *tofree=NULL;
3290Sstevel@tonic-gate 	DB_ATTR db_attr;
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate #ifdef EFENCE
3320Sstevel@tonic-gate EF_PROTECT_FREE=1;
3330Sstevel@tonic-gate EF_PROTECT_BELOW=1;
3340Sstevel@tonic-gate EF_ALIGNMENT=0;
3350Sstevel@tonic-gate #endif
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	apps_startup();
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	conf = NULL;
3400Sstevel@tonic-gate 	key = NULL;
3410Sstevel@tonic-gate 	section = NULL;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	preserve=0;
3440Sstevel@tonic-gate 	msie_hack=0;
3450Sstevel@tonic-gate 	if (bio_err == NULL)
3460Sstevel@tonic-gate 		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
3470Sstevel@tonic-gate 			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	argc--;
3500Sstevel@tonic-gate 	argv++;
3510Sstevel@tonic-gate 	while (argc >= 1)
3520Sstevel@tonic-gate 		{
3530Sstevel@tonic-gate 		if	(strcmp(*argv,"-verbose") == 0)
3540Sstevel@tonic-gate 			verbose=1;
3550Sstevel@tonic-gate 		else if	(strcmp(*argv,"-config") == 0)
3560Sstevel@tonic-gate 			{
3570Sstevel@tonic-gate 			if (--argc < 1) goto bad;
3580Sstevel@tonic-gate 			configfile= *(++argv);
3590Sstevel@tonic-gate 			}
3600Sstevel@tonic-gate 		else if (strcmp(*argv,"-name") == 0)
3610Sstevel@tonic-gate 			{
3620Sstevel@tonic-gate 			if (--argc < 1) goto bad;
3630Sstevel@tonic-gate 			section= *(++argv);
3640Sstevel@tonic-gate 			}
3650Sstevel@tonic-gate 		else if (strcmp(*argv,"-subj") == 0)
3660Sstevel@tonic-gate 			{
3670Sstevel@tonic-gate 			if (--argc < 1) goto bad;
3680Sstevel@tonic-gate 			subj= *(++argv);
3690Sstevel@tonic-gate 			/* preserve=1; */
3700Sstevel@tonic-gate 			}
371*2139Sjp161948 		else if (strcmp(*argv,"-utf8") == 0)
372*2139Sjp161948 			chtype = MBSTRING_UTF8;
373*2139Sjp161948 		else if (strcmp(*argv,"-create_serial") == 0)
374*2139Sjp161948 			create_ser = 1;
375*2139Sjp161948 		else if (strcmp(*argv,"-multivalue-rdn") == 0)
376*2139Sjp161948 			multirdn=1;
3770Sstevel@tonic-gate 		else if (strcmp(*argv,"-startdate") == 0)
3780Sstevel@tonic-gate 			{
3790Sstevel@tonic-gate 			if (--argc < 1) goto bad;
3800Sstevel@tonic-gate 			startdate= *(++argv);
3810Sstevel@tonic-gate 			}
3820Sstevel@tonic-gate 		else if (strcmp(*argv,"-enddate") == 0)
3830Sstevel@tonic-gate 			{
3840Sstevel@tonic-gate 			if (--argc < 1) goto bad;
3850Sstevel@tonic-gate 			enddate= *(++argv);
3860Sstevel@tonic-gate 			}
3870Sstevel@tonic-gate 		else if (strcmp(*argv,"-days") == 0)
3880Sstevel@tonic-gate 			{
3890Sstevel@tonic-gate 			if (--argc < 1) goto bad;
3900Sstevel@tonic-gate 			days=atoi(*(++argv));
3910Sstevel@tonic-gate 			}
3920Sstevel@tonic-gate 		else if (strcmp(*argv,"-md") == 0)
3930Sstevel@tonic-gate 			{
3940Sstevel@tonic-gate 			if (--argc < 1) goto bad;
3950Sstevel@tonic-gate 			md= *(++argv);
3960Sstevel@tonic-gate 			}
3970Sstevel@tonic-gate 		else if (strcmp(*argv,"-policy") == 0)
3980Sstevel@tonic-gate 			{
3990Sstevel@tonic-gate 			if (--argc < 1) goto bad;
4000Sstevel@tonic-gate 			policy= *(++argv);
4010Sstevel@tonic-gate 			}
4020Sstevel@tonic-gate 		else if (strcmp(*argv,"-keyfile") == 0)
4030Sstevel@tonic-gate 			{
4040Sstevel@tonic-gate 			if (--argc < 1) goto bad;
4050Sstevel@tonic-gate 			keyfile= *(++argv);
4060Sstevel@tonic-gate 			}
4070Sstevel@tonic-gate 		else if (strcmp(*argv,"-keyform") == 0)
4080Sstevel@tonic-gate 			{
4090Sstevel@tonic-gate 			if (--argc < 1) goto bad;
4100Sstevel@tonic-gate 			keyform=str2fmt(*(++argv));
4110Sstevel@tonic-gate 			}
4120Sstevel@tonic-gate 		else if (strcmp(*argv,"-passin") == 0)
4130Sstevel@tonic-gate 			{
4140Sstevel@tonic-gate 			if (--argc < 1) goto bad;
4150Sstevel@tonic-gate 			passargin= *(++argv);
4160Sstevel@tonic-gate 			}
4170Sstevel@tonic-gate 		else if (strcmp(*argv,"-key") == 0)
4180Sstevel@tonic-gate 			{
4190Sstevel@tonic-gate 			if (--argc < 1) goto bad;
4200Sstevel@tonic-gate 			key= *(++argv);
4210Sstevel@tonic-gate 			}
4220Sstevel@tonic-gate 		else if (strcmp(*argv,"-cert") == 0)
4230Sstevel@tonic-gate 			{
4240Sstevel@tonic-gate 			if (--argc < 1) goto bad;
4250Sstevel@tonic-gate 			certfile= *(++argv);
4260Sstevel@tonic-gate 			}
427*2139Sjp161948 		else if (strcmp(*argv,"-selfsign") == 0)
428*2139Sjp161948 			selfsign=1;
4290Sstevel@tonic-gate 		else if (strcmp(*argv,"-in") == 0)
4300Sstevel@tonic-gate 			{
4310Sstevel@tonic-gate 			if (--argc < 1) goto bad;
4320Sstevel@tonic-gate 			infile= *(++argv);
4330Sstevel@tonic-gate 			req=1;
4340Sstevel@tonic-gate 			}
4350Sstevel@tonic-gate 		else if (strcmp(*argv,"-out") == 0)
4360Sstevel@tonic-gate 			{
4370Sstevel@tonic-gate 			if (--argc < 1) goto bad;
4380Sstevel@tonic-gate 			outfile= *(++argv);
4390Sstevel@tonic-gate 			}
4400Sstevel@tonic-gate 		else if (strcmp(*argv,"-outdir") == 0)
4410Sstevel@tonic-gate 			{
4420Sstevel@tonic-gate 			if (--argc < 1) goto bad;
4430Sstevel@tonic-gate 			outdir= *(++argv);
4440Sstevel@tonic-gate 			}
4450Sstevel@tonic-gate 		else if (strcmp(*argv,"-notext") == 0)
4460Sstevel@tonic-gate 			notext=1;
4470Sstevel@tonic-gate 		else if (strcmp(*argv,"-batch") == 0)
4480Sstevel@tonic-gate 			batch=1;
4490Sstevel@tonic-gate 		else if (strcmp(*argv,"-preserveDN") == 0)
4500Sstevel@tonic-gate 			preserve=1;
4510Sstevel@tonic-gate 		else if (strcmp(*argv,"-noemailDN") == 0)
4520Sstevel@tonic-gate 			email_dn=0;
4530Sstevel@tonic-gate 		else if (strcmp(*argv,"-gencrl") == 0)
4540Sstevel@tonic-gate 			gencrl=1;
4550Sstevel@tonic-gate 		else if (strcmp(*argv,"-msie_hack") == 0)
4560Sstevel@tonic-gate 			msie_hack=1;
4570Sstevel@tonic-gate 		else if (strcmp(*argv,"-crldays") == 0)
4580Sstevel@tonic-gate 			{
4590Sstevel@tonic-gate 			if (--argc < 1) goto bad;
4600Sstevel@tonic-gate 			crldays= atol(*(++argv));
4610Sstevel@tonic-gate 			}
4620Sstevel@tonic-gate 		else if (strcmp(*argv,"-crlhours") == 0)
4630Sstevel@tonic-gate 			{
4640Sstevel@tonic-gate 			if (--argc < 1) goto bad;
4650Sstevel@tonic-gate 			crlhours= atol(*(++argv));
4660Sstevel@tonic-gate 			}
4670Sstevel@tonic-gate 		else if (strcmp(*argv,"-infiles") == 0)
4680Sstevel@tonic-gate 			{
4690Sstevel@tonic-gate 			argc--;
4700Sstevel@tonic-gate 			argv++;
4710Sstevel@tonic-gate 			req=1;
4720Sstevel@tonic-gate 			break;
4730Sstevel@tonic-gate 			}
4740Sstevel@tonic-gate 		else if (strcmp(*argv, "-ss_cert") == 0)
4750Sstevel@tonic-gate 			{
4760Sstevel@tonic-gate 			if (--argc < 1) goto bad;
4770Sstevel@tonic-gate 			ss_cert_file = *(++argv);
4780Sstevel@tonic-gate 			req=1;
4790Sstevel@tonic-gate 			}
4800Sstevel@tonic-gate 		else if (strcmp(*argv, "-spkac") == 0)
4810Sstevel@tonic-gate 			{
4820Sstevel@tonic-gate 			if (--argc < 1) goto bad;
4830Sstevel@tonic-gate 			spkac_file = *(++argv);
4840Sstevel@tonic-gate 			req=1;
4850Sstevel@tonic-gate 			}
4860Sstevel@tonic-gate 		else if (strcmp(*argv,"-revoke") == 0)
4870Sstevel@tonic-gate 			{
4880Sstevel@tonic-gate 			if (--argc < 1) goto bad;
4890Sstevel@tonic-gate 			infile= *(++argv);
4900Sstevel@tonic-gate 			dorevoke=1;
4910Sstevel@tonic-gate 			}
4920Sstevel@tonic-gate 		else if (strcmp(*argv,"-extensions") == 0)
4930Sstevel@tonic-gate 			{
4940Sstevel@tonic-gate 			if (--argc < 1) goto bad;
4950Sstevel@tonic-gate 			extensions= *(++argv);
4960Sstevel@tonic-gate 			}
4970Sstevel@tonic-gate 		else if (strcmp(*argv,"-extfile") == 0)
4980Sstevel@tonic-gate 			{
4990Sstevel@tonic-gate 			if (--argc < 1) goto bad;
5000Sstevel@tonic-gate 			extfile= *(++argv);
5010Sstevel@tonic-gate 			}
5020Sstevel@tonic-gate 		else if (strcmp(*argv,"-status") == 0)
5030Sstevel@tonic-gate 			{
5040Sstevel@tonic-gate 			if (--argc < 1) goto bad;
5050Sstevel@tonic-gate 			ser_status= *(++argv);
5060Sstevel@tonic-gate 			}
5070Sstevel@tonic-gate 		else if (strcmp(*argv,"-updatedb") == 0)
5080Sstevel@tonic-gate 			{
5090Sstevel@tonic-gate 			doupdatedb=1;
5100Sstevel@tonic-gate 			}
5110Sstevel@tonic-gate 		else if (strcmp(*argv,"-crlexts") == 0)
5120Sstevel@tonic-gate 			{
5130Sstevel@tonic-gate 			if (--argc < 1) goto bad;
5140Sstevel@tonic-gate 			crl_ext= *(++argv);
5150Sstevel@tonic-gate 			}
5160Sstevel@tonic-gate 		else if (strcmp(*argv,"-crl_reason") == 0)
5170Sstevel@tonic-gate 			{
5180Sstevel@tonic-gate 			if (--argc < 1) goto bad;
5190Sstevel@tonic-gate 			rev_arg = *(++argv);
5200Sstevel@tonic-gate 			rev_type = REV_CRL_REASON;
5210Sstevel@tonic-gate 			}
5220Sstevel@tonic-gate 		else if (strcmp(*argv,"-crl_hold") == 0)
5230Sstevel@tonic-gate 			{
5240Sstevel@tonic-gate 			if (--argc < 1) goto bad;
5250Sstevel@tonic-gate 			rev_arg = *(++argv);
5260Sstevel@tonic-gate 			rev_type = REV_HOLD;
5270Sstevel@tonic-gate 			}
5280Sstevel@tonic-gate 		else if (strcmp(*argv,"-crl_compromise") == 0)
5290Sstevel@tonic-gate 			{
5300Sstevel@tonic-gate 			if (--argc < 1) goto bad;
5310Sstevel@tonic-gate 			rev_arg = *(++argv);
5320Sstevel@tonic-gate 			rev_type = REV_KEY_COMPROMISE;
5330Sstevel@tonic-gate 			}
5340Sstevel@tonic-gate 		else if (strcmp(*argv,"-crl_CA_compromise") == 0)
5350Sstevel@tonic-gate 			{
5360Sstevel@tonic-gate 			if (--argc < 1) goto bad;
5370Sstevel@tonic-gate 			rev_arg = *(++argv);
5380Sstevel@tonic-gate 			rev_type = REV_CA_COMPROMISE;
5390Sstevel@tonic-gate 			}
5400Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
5410Sstevel@tonic-gate 		else if (strcmp(*argv,"-engine") == 0)
5420Sstevel@tonic-gate 			{
5430Sstevel@tonic-gate 			if (--argc < 1) goto bad;
5440Sstevel@tonic-gate 			engine= *(++argv);
5450Sstevel@tonic-gate 			}
5460Sstevel@tonic-gate #endif
5470Sstevel@tonic-gate 		else
5480Sstevel@tonic-gate 			{
5490Sstevel@tonic-gate bad:
5500Sstevel@tonic-gate 			BIO_printf(bio_err,"unknown option %s\n",*argv);
5510Sstevel@tonic-gate 			badops=1;
5520Sstevel@tonic-gate 			break;
5530Sstevel@tonic-gate 			}
5540Sstevel@tonic-gate 		argc--;
5550Sstevel@tonic-gate 		argv++;
5560Sstevel@tonic-gate 		}
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	if (badops)
5590Sstevel@tonic-gate 		{
5600Sstevel@tonic-gate 		for (pp=ca_usage; (*pp != NULL); pp++)
5610Sstevel@tonic-gate 			BIO_printf(bio_err,"%s",*pp);
5620Sstevel@tonic-gate 		goto err;
5630Sstevel@tonic-gate 		}
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	ERR_load_crypto_strings();
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 	/*****************************************************************/
5680Sstevel@tonic-gate 	tofree=NULL;
5690Sstevel@tonic-gate 	if (configfile == NULL) configfile = getenv("OPENSSL_CONF");
5700Sstevel@tonic-gate 	if (configfile == NULL) configfile = getenv("SSLEAY_CONF");
5710Sstevel@tonic-gate 	if (configfile == NULL)
5720Sstevel@tonic-gate 		{
5730Sstevel@tonic-gate 		const char *s=X509_get_default_cert_area();
5740Sstevel@tonic-gate 		size_t len;
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
5770Sstevel@tonic-gate 		len = strlen(s)+sizeof(CONFIG_FILE);
5780Sstevel@tonic-gate 		tofree=OPENSSL_malloc(len);
5790Sstevel@tonic-gate 		strcpy(tofree,s);
5800Sstevel@tonic-gate #else
5810Sstevel@tonic-gate 		len = strlen(s)+sizeof(CONFIG_FILE)+1;
5820Sstevel@tonic-gate 		tofree=OPENSSL_malloc(len);
5830Sstevel@tonic-gate 		BUF_strlcpy(tofree,s,len);
5840Sstevel@tonic-gate 		BUF_strlcat(tofree,"/",len);
5850Sstevel@tonic-gate #endif
5860Sstevel@tonic-gate 		BUF_strlcat(tofree,CONFIG_FILE,len);
5870Sstevel@tonic-gate 		configfile=tofree;
5880Sstevel@tonic-gate 		}
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	BIO_printf(bio_err,"Using configuration from %s\n",configfile);
5910Sstevel@tonic-gate 	conf = NCONF_new(NULL);
5920Sstevel@tonic-gate 	if (NCONF_load(conf,configfile,&errorline) <= 0)
5930Sstevel@tonic-gate 		{
5940Sstevel@tonic-gate 		if (errorline <= 0)
5950Sstevel@tonic-gate 			BIO_printf(bio_err,"error loading the config file '%s'\n",
5960Sstevel@tonic-gate 				configfile);
5970Sstevel@tonic-gate 		else
5980Sstevel@tonic-gate 			BIO_printf(bio_err,"error on line %ld of config file '%s'\n"
5990Sstevel@tonic-gate 				,errorline,configfile);
6000Sstevel@tonic-gate 		goto err;
6010Sstevel@tonic-gate 		}
6020Sstevel@tonic-gate 	if(tofree)
6030Sstevel@tonic-gate 		{
6040Sstevel@tonic-gate 		OPENSSL_free(tofree);
6050Sstevel@tonic-gate 		tofree = NULL;
6060Sstevel@tonic-gate 		}
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	if (!load_config(bio_err, conf))
6090Sstevel@tonic-gate 		goto err;
6100Sstevel@tonic-gate 
611*2139Sjp161948 #ifndef OPENSSL_NO_ENGINE
612*2139Sjp161948 	e = setup_engine(bio_err, engine, 0);
613*2139Sjp161948 #endif
614*2139Sjp161948 
6150Sstevel@tonic-gate 	/* Lets get the config section we are using */
6160Sstevel@tonic-gate 	if (section == NULL)
6170Sstevel@tonic-gate 		{
6180Sstevel@tonic-gate 		section=NCONF_get_string(conf,BASE_SECTION,ENV_DEFAULT_CA);
6190Sstevel@tonic-gate 		if (section == NULL)
6200Sstevel@tonic-gate 			{
6210Sstevel@tonic-gate 			lookup_fail(BASE_SECTION,ENV_DEFAULT_CA);
6220Sstevel@tonic-gate 			goto err;
6230Sstevel@tonic-gate 			}
6240Sstevel@tonic-gate 		}
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	if (conf != NULL)
6270Sstevel@tonic-gate 		{
6280Sstevel@tonic-gate 		p=NCONF_get_string(conf,NULL,"oid_file");
6290Sstevel@tonic-gate 		if (p == NULL)
6300Sstevel@tonic-gate 			ERR_clear_error();
6310Sstevel@tonic-gate 		if (p != NULL)
6320Sstevel@tonic-gate 			{
6330Sstevel@tonic-gate 			BIO *oid_bio;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 			oid_bio=BIO_new_file(p,"r");
6360Sstevel@tonic-gate 			if (oid_bio == NULL)
6370Sstevel@tonic-gate 				{
6380Sstevel@tonic-gate 				/*
6390Sstevel@tonic-gate 				BIO_printf(bio_err,"problems opening %s for extra oid's\n",p);
6400Sstevel@tonic-gate 				ERR_print_errors(bio_err);
6410Sstevel@tonic-gate 				*/
6420Sstevel@tonic-gate 				ERR_clear_error();
6430Sstevel@tonic-gate 				}
6440Sstevel@tonic-gate 			else
6450Sstevel@tonic-gate 				{
6460Sstevel@tonic-gate 				OBJ_create_objects(oid_bio);
6470Sstevel@tonic-gate 				BIO_free(oid_bio);
6480Sstevel@tonic-gate 				}
6490Sstevel@tonic-gate 			}
6500Sstevel@tonic-gate 		if (!add_oid_section(bio_err,conf))
6510Sstevel@tonic-gate 			{
6520Sstevel@tonic-gate 			ERR_print_errors(bio_err);
6530Sstevel@tonic-gate 			goto err;
6540Sstevel@tonic-gate 			}
6550Sstevel@tonic-gate 		}
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	randfile = NCONF_get_string(conf, BASE_SECTION, "RANDFILE");
6580Sstevel@tonic-gate 	if (randfile == NULL)
6590Sstevel@tonic-gate 		ERR_clear_error();
6600Sstevel@tonic-gate 	app_RAND_load_file(randfile, bio_err, 0);
6610Sstevel@tonic-gate 
662*2139Sjp161948 	f = NCONF_get_string(conf, section, STRING_MASK);
663*2139Sjp161948 	if (!f)
664*2139Sjp161948 		ERR_clear_error();
665*2139Sjp161948 
666*2139Sjp161948 	if(f && !ASN1_STRING_set_default_mask_asc(f)) {
667*2139Sjp161948 		BIO_printf(bio_err, "Invalid global string mask setting %s\n", f);
668*2139Sjp161948 		goto err;
669*2139Sjp161948 	}
670*2139Sjp161948 
671*2139Sjp161948 	if (chtype != MBSTRING_UTF8){
672*2139Sjp161948 		f = NCONF_get_string(conf, section, UTF8_IN);
673*2139Sjp161948 		if (!f)
674*2139Sjp161948 			ERR_clear_error();
675*2139Sjp161948 		else if (!strcmp(f, "yes"))
676*2139Sjp161948 			chtype = MBSTRING_UTF8;
677*2139Sjp161948 	}
678*2139Sjp161948 
6790Sstevel@tonic-gate 	db_attr.unique_subject = 1;
680*2139Sjp161948 	p = NCONF_get_string(conf, section, ENV_UNIQUE_SUBJECT);
6810Sstevel@tonic-gate 	if (p)
6820Sstevel@tonic-gate 		{
6830Sstevel@tonic-gate #ifdef RL_DEBUG
6840Sstevel@tonic-gate 		BIO_printf(bio_err, "DEBUG: unique_subject = \"%s\"\n", p);
6850Sstevel@tonic-gate #endif
686*2139Sjp161948 		db_attr.unique_subject = parse_yesno(p,1);
6870Sstevel@tonic-gate 		}
688*2139Sjp161948 	else
689*2139Sjp161948 		ERR_clear_error();
6900Sstevel@tonic-gate #ifdef RL_DEBUG
691*2139Sjp161948 	if (!p)
6920Sstevel@tonic-gate 		BIO_printf(bio_err, "DEBUG: unique_subject undefined\n", p);
6930Sstevel@tonic-gate #endif
6940Sstevel@tonic-gate #ifdef RL_DEBUG
6950Sstevel@tonic-gate 	BIO_printf(bio_err, "DEBUG: configured unique_subject is %d\n",
6960Sstevel@tonic-gate 		db_attr.unique_subject);
6970Sstevel@tonic-gate #endif
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	in=BIO_new(BIO_s_file());
7000Sstevel@tonic-gate 	out=BIO_new(BIO_s_file());
7010Sstevel@tonic-gate 	Sout=BIO_new(BIO_s_file());
7020Sstevel@tonic-gate 	Cout=BIO_new(BIO_s_file());
7030Sstevel@tonic-gate 	if ((in == NULL) || (out == NULL) || (Sout == NULL) || (Cout == NULL))
7040Sstevel@tonic-gate 		{
7050Sstevel@tonic-gate 		ERR_print_errors(bio_err);
7060Sstevel@tonic-gate 		goto err;
7070Sstevel@tonic-gate 		}
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	/*****************************************************************/
7100Sstevel@tonic-gate 	/* report status of cert with serial number given on command line */
7110Sstevel@tonic-gate 	if (ser_status)
7120Sstevel@tonic-gate 	{
7130Sstevel@tonic-gate 		if ((dbfile=NCONF_get_string(conf,section,ENV_DATABASE)) == NULL)
7140Sstevel@tonic-gate 			{
7150Sstevel@tonic-gate 			lookup_fail(section,ENV_DATABASE);
7160Sstevel@tonic-gate 			goto err;
7170Sstevel@tonic-gate 			}
7180Sstevel@tonic-gate 		db = load_index(dbfile,&db_attr);
7190Sstevel@tonic-gate 		if (db == NULL) goto err;
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 		if (!index_index(db)) goto err;
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 		if (get_certificate_status(ser_status,db) != 1)
7240Sstevel@tonic-gate 			BIO_printf(bio_err,"Error verifying serial %s!\n",
7250Sstevel@tonic-gate 				 ser_status);
7260Sstevel@tonic-gate 		goto err;
7270Sstevel@tonic-gate 	}
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	/*****************************************************************/
730*2139Sjp161948 	/* we definitely need a private key, so let's get it */
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	if ((keyfile == NULL) && ((keyfile=NCONF_get_string(conf,
7330Sstevel@tonic-gate 		section,ENV_PRIVATE_KEY)) == NULL))
7340Sstevel@tonic-gate 		{
7350Sstevel@tonic-gate 		lookup_fail(section,ENV_PRIVATE_KEY);
7360Sstevel@tonic-gate 		goto err;
7370Sstevel@tonic-gate 		}
7380Sstevel@tonic-gate 	if (!key)
7390Sstevel@tonic-gate 		{
7400Sstevel@tonic-gate 		free_key = 1;
7410Sstevel@tonic-gate 		if (!app_passwd(bio_err, passargin, NULL, &key, NULL))
7420Sstevel@tonic-gate 			{
7430Sstevel@tonic-gate 			BIO_printf(bio_err,"Error getting password\n");
7440Sstevel@tonic-gate 			goto err;
7450Sstevel@tonic-gate 			}
7460Sstevel@tonic-gate 		}
7470Sstevel@tonic-gate 	pkey = load_key(bio_err, keyfile, keyform, 0, key, e,
7480Sstevel@tonic-gate 		"CA private key");
7490Sstevel@tonic-gate 	if (key) OPENSSL_cleanse(key,strlen(key));
7500Sstevel@tonic-gate 	if (pkey == NULL)
7510Sstevel@tonic-gate 		{
7520Sstevel@tonic-gate 		/* load_key() has already printed an appropriate message */
7530Sstevel@tonic-gate 		goto err;
7540Sstevel@tonic-gate 		}
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 	/*****************************************************************/
7570Sstevel@tonic-gate 	/* we need a certificate */
758*2139Sjp161948 	if (!selfsign || spkac_file || ss_cert_file || gencrl)
7590Sstevel@tonic-gate 		{
760*2139Sjp161948 		if ((certfile == NULL)
761*2139Sjp161948 			&& ((certfile=NCONF_get_string(conf,
762*2139Sjp161948 				     section,ENV_CERTIFICATE)) == NULL))
763*2139Sjp161948 			{
764*2139Sjp161948 			lookup_fail(section,ENV_CERTIFICATE);
765*2139Sjp161948 			goto err;
766*2139Sjp161948 			}
767*2139Sjp161948 		x509=load_cert(bio_err, certfile, FORMAT_PEM, NULL, e,
768*2139Sjp161948 			"CA certificate");
769*2139Sjp161948 		if (x509 == NULL)
770*2139Sjp161948 			goto err;
771*2139Sjp161948 
772*2139Sjp161948 		if (!X509_check_private_key(x509,pkey))
773*2139Sjp161948 			{
774*2139Sjp161948 			BIO_printf(bio_err,"CA certificate and CA private key do not match\n");
775*2139Sjp161948 			goto err;
776*2139Sjp161948 			}
7770Sstevel@tonic-gate 		}
778*2139Sjp161948 	if (!selfsign) x509p = x509;
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	f=NCONF_get_string(conf,BASE_SECTION,ENV_PRESERVE);
7810Sstevel@tonic-gate 	if (f == NULL)
7820Sstevel@tonic-gate 		ERR_clear_error();
7830Sstevel@tonic-gate 	if ((f != NULL) && ((*f == 'y') || (*f == 'Y')))
7840Sstevel@tonic-gate 		preserve=1;
7850Sstevel@tonic-gate 	f=NCONF_get_string(conf,BASE_SECTION,ENV_MSIE_HACK);
7860Sstevel@tonic-gate 	if (f == NULL)
7870Sstevel@tonic-gate 		ERR_clear_error();
7880Sstevel@tonic-gate 	if ((f != NULL) && ((*f == 'y') || (*f == 'Y')))
7890Sstevel@tonic-gate 		msie_hack=1;
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	f=NCONF_get_string(conf,section,ENV_NAMEOPT);
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	if (f)
7940Sstevel@tonic-gate 		{
7950Sstevel@tonic-gate 		if (!set_name_ex(&nameopt, f))
7960Sstevel@tonic-gate 			{
7970Sstevel@tonic-gate 			BIO_printf(bio_err, "Invalid name options: \"%s\"\n", f);
7980Sstevel@tonic-gate 			goto err;
7990Sstevel@tonic-gate 			}
8000Sstevel@tonic-gate 		default_op = 0;
8010Sstevel@tonic-gate 		}
8020Sstevel@tonic-gate 	else
8030Sstevel@tonic-gate 		ERR_clear_error();
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	f=NCONF_get_string(conf,section,ENV_CERTOPT);
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	if (f)
8080Sstevel@tonic-gate 		{
8090Sstevel@tonic-gate 		if (!set_cert_ex(&certopt, f))
8100Sstevel@tonic-gate 			{
8110Sstevel@tonic-gate 			BIO_printf(bio_err, "Invalid certificate options: \"%s\"\n", f);
8120Sstevel@tonic-gate 			goto err;
8130Sstevel@tonic-gate 			}
8140Sstevel@tonic-gate 		default_op = 0;
8150Sstevel@tonic-gate 		}
8160Sstevel@tonic-gate 	else
8170Sstevel@tonic-gate 		ERR_clear_error();
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	f=NCONF_get_string(conf,section,ENV_EXTCOPY);
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	if (f)
8220Sstevel@tonic-gate 		{
8230Sstevel@tonic-gate 		if (!set_ext_copy(&ext_copy, f))
8240Sstevel@tonic-gate 			{
8250Sstevel@tonic-gate 			BIO_printf(bio_err, "Invalid extension copy option: \"%s\"\n", f);
8260Sstevel@tonic-gate 			goto err;
8270Sstevel@tonic-gate 			}
8280Sstevel@tonic-gate 		}
8290Sstevel@tonic-gate 	else
8300Sstevel@tonic-gate 		ERR_clear_error();
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	/*****************************************************************/
8330Sstevel@tonic-gate 	/* lookup where to write new certificates */
8340Sstevel@tonic-gate 	if ((outdir == NULL) && (req))
8350Sstevel@tonic-gate 		{
8360Sstevel@tonic-gate 		struct stat sb;
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 		if ((outdir=NCONF_get_string(conf,section,ENV_NEW_CERTS_DIR))
8390Sstevel@tonic-gate 			== NULL)
8400Sstevel@tonic-gate 			{
8410Sstevel@tonic-gate 			BIO_printf(bio_err,"there needs to be defined a directory for new certificate to be placed in\n");
8420Sstevel@tonic-gate 			goto err;
8430Sstevel@tonic-gate 			}
8440Sstevel@tonic-gate #ifndef OPENSSL_SYS_VMS
8450Sstevel@tonic-gate 	    /* outdir is a directory spec, but access() for VMS demands a
8460Sstevel@tonic-gate 	       filename.  In any case, stat(), below, will catch the problem
8470Sstevel@tonic-gate 	       if outdir is not a directory spec, and the fopen() or open()
8480Sstevel@tonic-gate 	       will catch an error if there is no write access.
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	       Presumably, this problem could also be solved by using the DEC
8510Sstevel@tonic-gate 	       C routines to convert the directory syntax to Unixly, and give
8520Sstevel@tonic-gate 	       that to access().  However, time's too short to do that just
8530Sstevel@tonic-gate 	       now.
8540Sstevel@tonic-gate 	    */
8550Sstevel@tonic-gate 		if (access(outdir,R_OK|W_OK|X_OK) != 0)
8560Sstevel@tonic-gate 			{
8570Sstevel@tonic-gate 			BIO_printf(bio_err,"I am unable to access the %s directory\n",outdir);
8580Sstevel@tonic-gate 			perror(outdir);
8590Sstevel@tonic-gate 			goto err;
8600Sstevel@tonic-gate 			}
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 		if (stat(outdir,&sb) != 0)
8630Sstevel@tonic-gate 			{
8640Sstevel@tonic-gate 			BIO_printf(bio_err,"unable to stat(%s)\n",outdir);
8650Sstevel@tonic-gate 			perror(outdir);
8660Sstevel@tonic-gate 			goto err;
8670Sstevel@tonic-gate 			}
868871Scasper #ifdef S_ISDIR
869871Scasper 		if (!S_ISDIR(sb.st_mode))
8700Sstevel@tonic-gate 			{
8710Sstevel@tonic-gate 			BIO_printf(bio_err,"%s need to be a directory\n",outdir);
8720Sstevel@tonic-gate 			perror(outdir);
8730Sstevel@tonic-gate 			goto err;
8740Sstevel@tonic-gate 			}
8750Sstevel@tonic-gate #endif
8760Sstevel@tonic-gate #endif
8770Sstevel@tonic-gate 		}
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 	/*****************************************************************/
8800Sstevel@tonic-gate 	/* we need to load the database file */
8810Sstevel@tonic-gate 	if ((dbfile=NCONF_get_string(conf,section,ENV_DATABASE)) == NULL)
8820Sstevel@tonic-gate 		{
8830Sstevel@tonic-gate 		lookup_fail(section,ENV_DATABASE);
8840Sstevel@tonic-gate 		goto err;
8850Sstevel@tonic-gate 		}
8860Sstevel@tonic-gate 	db = load_index(dbfile, &db_attr);
8870Sstevel@tonic-gate 	if (db == NULL) goto err;
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	/* Lets check some fields */
8900Sstevel@tonic-gate 	for (i=0; i<sk_num(db->db->data); i++)
8910Sstevel@tonic-gate 		{
892*2139Sjp161948 		pp=(const char **)sk_value(db->db->data,i);
8930Sstevel@tonic-gate 		if ((pp[DB_type][0] != DB_TYPE_REV) &&
8940Sstevel@tonic-gate 			(pp[DB_rev_date][0] != '\0'))
8950Sstevel@tonic-gate 			{
8960Sstevel@tonic-gate 			BIO_printf(bio_err,"entry %d: not revoked yet, but has a revocation date\n",i+1);
8970Sstevel@tonic-gate 			goto err;
8980Sstevel@tonic-gate 			}
8990Sstevel@tonic-gate 		if ((pp[DB_type][0] == DB_TYPE_REV) &&
9000Sstevel@tonic-gate 			!make_revoked(NULL, pp[DB_rev_date]))
9010Sstevel@tonic-gate 			{
9020Sstevel@tonic-gate 			BIO_printf(bio_err," in entry %d\n", i+1);
9030Sstevel@tonic-gate 			goto err;
9040Sstevel@tonic-gate 			}
905*2139Sjp161948 		if (!check_time_format((char *)pp[DB_exp_date]))
9060Sstevel@tonic-gate 			{
9070Sstevel@tonic-gate 			BIO_printf(bio_err,"entry %d: invalid expiry date\n",i+1);
9080Sstevel@tonic-gate 			goto err;
9090Sstevel@tonic-gate 			}
9100Sstevel@tonic-gate 		p=pp[DB_serial];
9110Sstevel@tonic-gate 		j=strlen(p);
9120Sstevel@tonic-gate 		if (*p == '-')
9130Sstevel@tonic-gate 			{
9140Sstevel@tonic-gate 			p++;
9150Sstevel@tonic-gate 			j--;
9160Sstevel@tonic-gate 			}
9170Sstevel@tonic-gate 		if ((j&1) || (j < 2))
9180Sstevel@tonic-gate 			{
9190Sstevel@tonic-gate 			BIO_printf(bio_err,"entry %d: bad serial number length (%d)\n",i+1,j);
9200Sstevel@tonic-gate 			goto err;
9210Sstevel@tonic-gate 			}
9220Sstevel@tonic-gate 		while (*p)
9230Sstevel@tonic-gate 			{
9240Sstevel@tonic-gate 			if (!(	((*p >= '0') && (*p <= '9')) ||
9250Sstevel@tonic-gate 				((*p >= 'A') && (*p <= 'F')) ||
9260Sstevel@tonic-gate 				((*p >= 'a') && (*p <= 'f')))  )
9270Sstevel@tonic-gate 				{
9280Sstevel@tonic-gate 				BIO_printf(bio_err,"entry %d: bad serial number characters, char pos %ld, char is '%c'\n",i+1,(long)(p-pp[DB_serial]),*p);
9290Sstevel@tonic-gate 				goto err;
9300Sstevel@tonic-gate 				}
9310Sstevel@tonic-gate 			p++;
9320Sstevel@tonic-gate 			}
9330Sstevel@tonic-gate 		}
9340Sstevel@tonic-gate 	if (verbose)
9350Sstevel@tonic-gate 		{
9360Sstevel@tonic-gate 		BIO_set_fp(out,stdout,BIO_NOCLOSE|BIO_FP_TEXT); /* cannot fail */
9370Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
9380Sstevel@tonic-gate 		{
9390Sstevel@tonic-gate 		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
9400Sstevel@tonic-gate 		out = BIO_push(tmpbio, out);
9410Sstevel@tonic-gate 		}
9420Sstevel@tonic-gate #endif
9430Sstevel@tonic-gate 		TXT_DB_write(out,db->db);
9440Sstevel@tonic-gate 		BIO_printf(bio_err,"%d entries loaded from the database\n",
9450Sstevel@tonic-gate 			db->db->data->num);
9460Sstevel@tonic-gate 		BIO_printf(bio_err,"generating index\n");
9470Sstevel@tonic-gate 		}
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	if (!index_index(db)) goto err;
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	/*****************************************************************/
9520Sstevel@tonic-gate 	/* Update the db file for expired certificates */
9530Sstevel@tonic-gate 	if (doupdatedb)
9540Sstevel@tonic-gate 		{
9550Sstevel@tonic-gate 		if (verbose)
9560Sstevel@tonic-gate 			BIO_printf(bio_err, "Updating %s ...\n",
9570Sstevel@tonic-gate 							dbfile);
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 		i = do_updatedb(db);
9600Sstevel@tonic-gate 		if (i == -1)
9610Sstevel@tonic-gate 			{
9620Sstevel@tonic-gate 			BIO_printf(bio_err,"Malloc failure\n");
9630Sstevel@tonic-gate 			goto err;
9640Sstevel@tonic-gate 			}
9650Sstevel@tonic-gate 		else if (i == 0)
9660Sstevel@tonic-gate 			{
9670Sstevel@tonic-gate 			if (verbose) BIO_printf(bio_err,
9680Sstevel@tonic-gate 					"No entries found to mark expired\n");
9690Sstevel@tonic-gate 			}
9700Sstevel@tonic-gate 	    	else
9710Sstevel@tonic-gate 			{
9720Sstevel@tonic-gate 			if (!save_index(dbfile,"new",db)) goto err;
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 			if (!rotate_index(dbfile,"new","old")) goto err;
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 			if (verbose) BIO_printf(bio_err,
9770Sstevel@tonic-gate 				"Done. %d entries marked as expired\n",i);
9780Sstevel@tonic-gate 	      		}
9790Sstevel@tonic-gate 	  	}
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate  	/*****************************************************************/
9820Sstevel@tonic-gate 	/* Read extentions config file                                   */
9830Sstevel@tonic-gate 	if (extfile)
9840Sstevel@tonic-gate 		{
9850Sstevel@tonic-gate 		extconf = NCONF_new(NULL);
9860Sstevel@tonic-gate 		if (NCONF_load(extconf,extfile,&errorline) <= 0)
9870Sstevel@tonic-gate 			{
9880Sstevel@tonic-gate 			if (errorline <= 0)
9890Sstevel@tonic-gate 				BIO_printf(bio_err, "ERROR: loading the config file '%s'\n",
9900Sstevel@tonic-gate 					extfile);
9910Sstevel@tonic-gate 			else
9920Sstevel@tonic-gate 				BIO_printf(bio_err, "ERROR: on line %ld of config file '%s'\n",
9930Sstevel@tonic-gate 					errorline,extfile);
9940Sstevel@tonic-gate 			ret = 1;
9950Sstevel@tonic-gate 			goto err;
9960Sstevel@tonic-gate 			}
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 		if (verbose)
9990Sstevel@tonic-gate 			BIO_printf(bio_err, "Successfully loaded extensions file %s\n", extfile);
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 		/* We can have sections in the ext file */
10020Sstevel@tonic-gate 		if (!extensions && !(extensions = NCONF_get_string(extconf, "default", "extensions")))
10030Sstevel@tonic-gate 			extensions = "default";
10040Sstevel@tonic-gate 		}
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	/*****************************************************************/
10070Sstevel@tonic-gate 	if (req || gencrl)
10080Sstevel@tonic-gate 		{
10090Sstevel@tonic-gate 		if (outfile != NULL)
10100Sstevel@tonic-gate 			{
10110Sstevel@tonic-gate 			if (BIO_write_filename(Sout,outfile) <= 0)
10120Sstevel@tonic-gate 				{
10130Sstevel@tonic-gate 				perror(outfile);
10140Sstevel@tonic-gate 				goto err;
10150Sstevel@tonic-gate 				}
10160Sstevel@tonic-gate 			}
10170Sstevel@tonic-gate 		else
10180Sstevel@tonic-gate 			{
10190Sstevel@tonic-gate 			BIO_set_fp(Sout,stdout,BIO_NOCLOSE|BIO_FP_TEXT);
10200Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
10210Sstevel@tonic-gate 			{
10220Sstevel@tonic-gate 			BIO *tmpbio = BIO_new(BIO_f_linebuffer());
10230Sstevel@tonic-gate 			Sout = BIO_push(tmpbio, Sout);
10240Sstevel@tonic-gate 			}
10250Sstevel@tonic-gate #endif
10260Sstevel@tonic-gate 			}
10270Sstevel@tonic-gate 		}
10280Sstevel@tonic-gate 
1029*2139Sjp161948 	if ((md == NULL) && ((md=NCONF_get_string(conf,
1030*2139Sjp161948 		section,ENV_DEFAULT_MD)) == NULL))
1031*2139Sjp161948 		{
1032*2139Sjp161948 		lookup_fail(section,ENV_DEFAULT_MD);
1033*2139Sjp161948 		goto err;
1034*2139Sjp161948 		}
1035*2139Sjp161948 
1036*2139Sjp161948 	if ((dgst=EVP_get_digestbyname(md)) == NULL)
1037*2139Sjp161948 		{
1038*2139Sjp161948 		BIO_printf(bio_err,"%s is an unsupported message digest type\n",md);
1039*2139Sjp161948 		goto err;
1040*2139Sjp161948 		}
1041*2139Sjp161948 
10420Sstevel@tonic-gate 	if (req)
10430Sstevel@tonic-gate 		{
10440Sstevel@tonic-gate 		if ((email_dn == 1) && ((tmp_email_dn=NCONF_get_string(conf,
10450Sstevel@tonic-gate 			section,ENV_DEFAULT_EMAIL_DN)) != NULL ))
10460Sstevel@tonic-gate 			{
10470Sstevel@tonic-gate 			if(strcmp(tmp_email_dn,"no") == 0)
10480Sstevel@tonic-gate 				email_dn=0;
10490Sstevel@tonic-gate 			}
10500Sstevel@tonic-gate 		if (verbose)
10510Sstevel@tonic-gate 			BIO_printf(bio_err,"message digest is %s\n",
10520Sstevel@tonic-gate 				OBJ_nid2ln(dgst->type));
10530Sstevel@tonic-gate 		if ((policy == NULL) && ((policy=NCONF_get_string(conf,
10540Sstevel@tonic-gate 			section,ENV_POLICY)) == NULL))
10550Sstevel@tonic-gate 			{
10560Sstevel@tonic-gate 			lookup_fail(section,ENV_POLICY);
10570Sstevel@tonic-gate 			goto err;
10580Sstevel@tonic-gate 			}
10590Sstevel@tonic-gate 		if (verbose)
10600Sstevel@tonic-gate 			BIO_printf(bio_err,"policy is %s\n",policy);
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 		if ((serialfile=NCONF_get_string(conf,section,ENV_SERIAL))
10630Sstevel@tonic-gate 			== NULL)
10640Sstevel@tonic-gate 			{
10650Sstevel@tonic-gate 			lookup_fail(section,ENV_SERIAL);
10660Sstevel@tonic-gate 			goto err;
10670Sstevel@tonic-gate 			}
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 		if (!extconf)
10700Sstevel@tonic-gate 			{
10710Sstevel@tonic-gate 			/* no '-extfile' option, so we look for extensions
10720Sstevel@tonic-gate 			 * in the main configuration file */
10730Sstevel@tonic-gate 			if (!extensions)
10740Sstevel@tonic-gate 				{
10750Sstevel@tonic-gate 				extensions=NCONF_get_string(conf,section,
10760Sstevel@tonic-gate 								ENV_EXTENSIONS);
10770Sstevel@tonic-gate 				if (!extensions)
10780Sstevel@tonic-gate 					ERR_clear_error();
10790Sstevel@tonic-gate 				}
10800Sstevel@tonic-gate 			if (extensions)
10810Sstevel@tonic-gate 				{
10820Sstevel@tonic-gate 				/* Check syntax of file */
10830Sstevel@tonic-gate 				X509V3_CTX ctx;
10840Sstevel@tonic-gate 				X509V3_set_ctx_test(&ctx);
10850Sstevel@tonic-gate 				X509V3_set_nconf(&ctx, conf);
10860Sstevel@tonic-gate 				if (!X509V3_EXT_add_nconf(conf, &ctx, extensions,
10870Sstevel@tonic-gate 								NULL))
10880Sstevel@tonic-gate 					{
10890Sstevel@tonic-gate 					BIO_printf(bio_err,
10900Sstevel@tonic-gate 				 	"Error Loading extension section %s\n",
10910Sstevel@tonic-gate 								 extensions);
10920Sstevel@tonic-gate 					ret = 1;
10930Sstevel@tonic-gate 					goto err;
10940Sstevel@tonic-gate 					}
10950Sstevel@tonic-gate 				}
10960Sstevel@tonic-gate 			}
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 		if (startdate == NULL)
10990Sstevel@tonic-gate 			{
11000Sstevel@tonic-gate 			startdate=NCONF_get_string(conf,section,
11010Sstevel@tonic-gate 				ENV_DEFAULT_STARTDATE);
11020Sstevel@tonic-gate 			if (startdate == NULL)
11030Sstevel@tonic-gate 				ERR_clear_error();
11040Sstevel@tonic-gate 			}
11050Sstevel@tonic-gate 		if (startdate && !ASN1_UTCTIME_set_string(NULL,startdate))
11060Sstevel@tonic-gate 			{
11070Sstevel@tonic-gate 			BIO_printf(bio_err,"start date is invalid, it should be YYMMDDHHMMSSZ\n");
11080Sstevel@tonic-gate 			goto err;
11090Sstevel@tonic-gate 			}
11100Sstevel@tonic-gate 		if (startdate == NULL) startdate="today";
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 		if (enddate == NULL)
11130Sstevel@tonic-gate 			{
11140Sstevel@tonic-gate 			enddate=NCONF_get_string(conf,section,
11150Sstevel@tonic-gate 				ENV_DEFAULT_ENDDATE);
11160Sstevel@tonic-gate 			if (enddate == NULL)
11170Sstevel@tonic-gate 				ERR_clear_error();
11180Sstevel@tonic-gate 			}
11190Sstevel@tonic-gate 		if (enddate && !ASN1_UTCTIME_set_string(NULL,enddate))
11200Sstevel@tonic-gate 			{
11210Sstevel@tonic-gate 			BIO_printf(bio_err,"end date is invalid, it should be YYMMDDHHMMSSZ\n");
11220Sstevel@tonic-gate 			goto err;
11230Sstevel@tonic-gate 			}
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 		if (days == 0)
11260Sstevel@tonic-gate 			{
11270Sstevel@tonic-gate 			if(!NCONF_get_number(conf,section, ENV_DEFAULT_DAYS, &days))
11280Sstevel@tonic-gate 				days = 0;
11290Sstevel@tonic-gate 			}
11300Sstevel@tonic-gate 		if (!enddate && (days == 0))
11310Sstevel@tonic-gate 			{
11320Sstevel@tonic-gate 			BIO_printf(bio_err,"cannot lookup how many days to certify for\n");
11330Sstevel@tonic-gate 			goto err;
11340Sstevel@tonic-gate 			}
11350Sstevel@tonic-gate 
1136*2139Sjp161948 		if ((serial=load_serial(serialfile, create_ser, NULL)) == NULL)
11370Sstevel@tonic-gate 			{
11380Sstevel@tonic-gate 			BIO_printf(bio_err,"error while loading serial number\n");
11390Sstevel@tonic-gate 			goto err;
11400Sstevel@tonic-gate 			}
11410Sstevel@tonic-gate 		if (verbose)
11420Sstevel@tonic-gate 			{
11430Sstevel@tonic-gate 			if (BN_is_zero(serial))
11440Sstevel@tonic-gate 				BIO_printf(bio_err,"next serial number is 00\n");
11450Sstevel@tonic-gate 			else
11460Sstevel@tonic-gate 				{
11470Sstevel@tonic-gate 				if ((f=BN_bn2hex(serial)) == NULL) goto err;
11480Sstevel@tonic-gate 				BIO_printf(bio_err,"next serial number is %s\n",f);
11490Sstevel@tonic-gate 				OPENSSL_free(f);
11500Sstevel@tonic-gate 				}
11510Sstevel@tonic-gate 			}
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 		if ((attribs=NCONF_get_section(conf,policy)) == NULL)
11540Sstevel@tonic-gate 			{
11550Sstevel@tonic-gate 			BIO_printf(bio_err,"unable to find 'section' for %s\n",policy);
11560Sstevel@tonic-gate 			goto err;
11570Sstevel@tonic-gate 			}
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 		if ((cert_sk=sk_X509_new_null()) == NULL)
11600Sstevel@tonic-gate 			{
11610Sstevel@tonic-gate 			BIO_printf(bio_err,"Memory allocation failure\n");
11620Sstevel@tonic-gate 			goto err;
11630Sstevel@tonic-gate 			}
11640Sstevel@tonic-gate 		if (spkac_file != NULL)
11650Sstevel@tonic-gate 			{
11660Sstevel@tonic-gate 			total++;
11670Sstevel@tonic-gate 			j=certify_spkac(&x,spkac_file,pkey,x509,dgst,attribs,db,
1168*2139Sjp161948 				serial,subj,chtype,multirdn,email_dn,startdate,enddate,days,extensions,
11690Sstevel@tonic-gate 				conf,verbose,certopt,nameopt,default_op,ext_copy);
11700Sstevel@tonic-gate 			if (j < 0) goto err;
11710Sstevel@tonic-gate 			if (j > 0)
11720Sstevel@tonic-gate 				{
11730Sstevel@tonic-gate 				total_done++;
11740Sstevel@tonic-gate 				BIO_printf(bio_err,"\n");
11750Sstevel@tonic-gate 				if (!BN_add_word(serial,1)) goto err;
11760Sstevel@tonic-gate 				if (!sk_X509_push(cert_sk,x))
11770Sstevel@tonic-gate 					{
11780Sstevel@tonic-gate 					BIO_printf(bio_err,"Memory allocation failure\n");
11790Sstevel@tonic-gate 					goto err;
11800Sstevel@tonic-gate 					}
11810Sstevel@tonic-gate 				if (outfile)
11820Sstevel@tonic-gate 					{
11830Sstevel@tonic-gate 					output_der = 1;
11840Sstevel@tonic-gate 					batch = 1;
11850Sstevel@tonic-gate 					}
11860Sstevel@tonic-gate 				}
11870Sstevel@tonic-gate 			}
11880Sstevel@tonic-gate 		if (ss_cert_file != NULL)
11890Sstevel@tonic-gate 			{
11900Sstevel@tonic-gate 			total++;
11910Sstevel@tonic-gate 			j=certify_cert(&x,ss_cert_file,pkey,x509,dgst,attribs,
1192*2139Sjp161948 				db,serial,subj,chtype,multirdn,email_dn,startdate,enddate,days,batch,
11930Sstevel@tonic-gate 				extensions,conf,verbose, certopt, nameopt,
11940Sstevel@tonic-gate 				default_op, ext_copy, e);
11950Sstevel@tonic-gate 			if (j < 0) goto err;
11960Sstevel@tonic-gate 			if (j > 0)
11970Sstevel@tonic-gate 				{
11980Sstevel@tonic-gate 				total_done++;
11990Sstevel@tonic-gate 				BIO_printf(bio_err,"\n");
12000Sstevel@tonic-gate 				if (!BN_add_word(serial,1)) goto err;
12010Sstevel@tonic-gate 				if (!sk_X509_push(cert_sk,x))
12020Sstevel@tonic-gate 					{
12030Sstevel@tonic-gate 					BIO_printf(bio_err,"Memory allocation failure\n");
12040Sstevel@tonic-gate 					goto err;
12050Sstevel@tonic-gate 					}
12060Sstevel@tonic-gate 				}
12070Sstevel@tonic-gate 			}
12080Sstevel@tonic-gate 		if (infile != NULL)
12090Sstevel@tonic-gate 			{
12100Sstevel@tonic-gate 			total++;
1211*2139Sjp161948 			j=certify(&x,infile,pkey,x509p,dgst,attribs,db,
1212*2139Sjp161948 				serial,subj,chtype,multirdn,email_dn,startdate,enddate,days,batch,
12130Sstevel@tonic-gate 				extensions,conf,verbose, certopt, nameopt,
1214*2139Sjp161948 				default_op, ext_copy, selfsign);
12150Sstevel@tonic-gate 			if (j < 0) goto err;
12160Sstevel@tonic-gate 			if (j > 0)
12170Sstevel@tonic-gate 				{
12180Sstevel@tonic-gate 				total_done++;
12190Sstevel@tonic-gate 				BIO_printf(bio_err,"\n");
12200Sstevel@tonic-gate 				if (!BN_add_word(serial,1)) goto err;
12210Sstevel@tonic-gate 				if (!sk_X509_push(cert_sk,x))
12220Sstevel@tonic-gate 					{
12230Sstevel@tonic-gate 					BIO_printf(bio_err,"Memory allocation failure\n");
12240Sstevel@tonic-gate 					goto err;
12250Sstevel@tonic-gate 					}
12260Sstevel@tonic-gate 				}
12270Sstevel@tonic-gate 			}
12280Sstevel@tonic-gate 		for (i=0; i<argc; i++)
12290Sstevel@tonic-gate 			{
12300Sstevel@tonic-gate 			total++;
1231*2139Sjp161948 			j=certify(&x,argv[i],pkey,x509p,dgst,attribs,db,
1232*2139Sjp161948 				serial,subj,chtype,multirdn,email_dn,startdate,enddate,days,batch,
12330Sstevel@tonic-gate 				extensions,conf,verbose, certopt, nameopt,
1234*2139Sjp161948 				default_op, ext_copy, selfsign);
12350Sstevel@tonic-gate 			if (j < 0) goto err;
12360Sstevel@tonic-gate 			if (j > 0)
12370Sstevel@tonic-gate 				{
12380Sstevel@tonic-gate 				total_done++;
12390Sstevel@tonic-gate 				BIO_printf(bio_err,"\n");
12400Sstevel@tonic-gate 				if (!BN_add_word(serial,1)) goto err;
12410Sstevel@tonic-gate 				if (!sk_X509_push(cert_sk,x))
12420Sstevel@tonic-gate 					{
12430Sstevel@tonic-gate 					BIO_printf(bio_err,"Memory allocation failure\n");
12440Sstevel@tonic-gate 					goto err;
12450Sstevel@tonic-gate 					}
12460Sstevel@tonic-gate 				}
12470Sstevel@tonic-gate 			}
12480Sstevel@tonic-gate 		/* we have a stack of newly certified certificates
12490Sstevel@tonic-gate 		 * and a data base and serial number that need
12500Sstevel@tonic-gate 		 * updating */
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 		if (sk_X509_num(cert_sk) > 0)
12530Sstevel@tonic-gate 			{
12540Sstevel@tonic-gate 			if (!batch)
12550Sstevel@tonic-gate 				{
12560Sstevel@tonic-gate 				BIO_printf(bio_err,"\n%d out of %d certificate requests certified, commit? [y/n]",total_done,total);
12570Sstevel@tonic-gate 				(void)BIO_flush(bio_err);
12580Sstevel@tonic-gate 				buf[0][0]='\0';
12590Sstevel@tonic-gate 				fgets(buf[0],10,stdin);
12600Sstevel@tonic-gate 				if ((buf[0][0] != 'y') && (buf[0][0] != 'Y'))
12610Sstevel@tonic-gate 					{
12620Sstevel@tonic-gate 					BIO_printf(bio_err,"CERTIFICATION CANCELED\n");
12630Sstevel@tonic-gate 					ret=0;
12640Sstevel@tonic-gate 					goto err;
12650Sstevel@tonic-gate 					}
12660Sstevel@tonic-gate 				}
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate 			BIO_printf(bio_err,"Write out database with %d new entries\n",sk_X509_num(cert_sk));
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 			if (!save_serial(serialfile,"new",serial,NULL)) goto err;
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate 			if (!save_index(dbfile, "new", db)) goto err;
12730Sstevel@tonic-gate 			}
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 		if (verbose)
12760Sstevel@tonic-gate 			BIO_printf(bio_err,"writing new certificates\n");
12770Sstevel@tonic-gate 		for (i=0; i<sk_X509_num(cert_sk); i++)
12780Sstevel@tonic-gate 			{
12790Sstevel@tonic-gate 			int k;
12800Sstevel@tonic-gate 			char *n;
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 			x=sk_X509_value(cert_sk,i);
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 			j=x->cert_info->serialNumber->length;
1285*2139Sjp161948 			p=(const char *)x->cert_info->serialNumber->data;
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate 			if(strlen(outdir) >= (size_t)(j ? BSIZE-j*2-6 : BSIZE-8))
12880Sstevel@tonic-gate 				{
12890Sstevel@tonic-gate 				BIO_printf(bio_err,"certificate file name too long\n");
12900Sstevel@tonic-gate 				goto err;
12910Sstevel@tonic-gate 				}
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 			strcpy(buf[2],outdir);
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate #ifndef OPENSSL_SYS_VMS
12960Sstevel@tonic-gate 			BUF_strlcat(buf[2],"/",sizeof(buf[2]));
12970Sstevel@tonic-gate #endif
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 			n=(char *)&(buf[2][strlen(buf[2])]);
13000Sstevel@tonic-gate 			if (j > 0)
13010Sstevel@tonic-gate 				{
13020Sstevel@tonic-gate 				for (k=0; k<j; k++)
13030Sstevel@tonic-gate 					{
13040Sstevel@tonic-gate 					if (n >= &(buf[2][sizeof(buf[2])]))
13050Sstevel@tonic-gate 						break;
13060Sstevel@tonic-gate 					BIO_snprintf(n,
13070Sstevel@tonic-gate 						     &buf[2][0] + sizeof(buf[2]) - n,
13080Sstevel@tonic-gate 						     "%02X",(unsigned char)*(p++));
13090Sstevel@tonic-gate 					n+=2;
13100Sstevel@tonic-gate 					}
13110Sstevel@tonic-gate 				}
13120Sstevel@tonic-gate 			else
13130Sstevel@tonic-gate 				{
13140Sstevel@tonic-gate 				*(n++)='0';
13150Sstevel@tonic-gate 				*(n++)='0';
13160Sstevel@tonic-gate 				}
13170Sstevel@tonic-gate 			*(n++)='.'; *(n++)='p'; *(n++)='e'; *(n++)='m';
13180Sstevel@tonic-gate 			*n='\0';
13190Sstevel@tonic-gate 			if (verbose)
13200Sstevel@tonic-gate 				BIO_printf(bio_err,"writing %s\n",buf[2]);
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 			if (BIO_write_filename(Cout,buf[2]) <= 0)
13230Sstevel@tonic-gate 				{
13240Sstevel@tonic-gate 				perror(buf[2]);
13250Sstevel@tonic-gate 				goto err;
13260Sstevel@tonic-gate 				}
13270Sstevel@tonic-gate 			write_new_certificate(Cout,x, 0, notext);
13280Sstevel@tonic-gate 			write_new_certificate(Sout,x, output_der, notext);
13290Sstevel@tonic-gate 			}
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 		if (sk_X509_num(cert_sk))
13320Sstevel@tonic-gate 			{
13330Sstevel@tonic-gate 			/* Rename the database and the serial file */
13340Sstevel@tonic-gate 			if (!rotate_serial(serialfile,"new","old")) goto err;
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 			if (!rotate_index(dbfile,"new","old")) goto err;
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate 			BIO_printf(bio_err,"Data Base Updated\n");
13390Sstevel@tonic-gate 			}
13400Sstevel@tonic-gate 		}
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 	/*****************************************************************/
13430Sstevel@tonic-gate 	if (gencrl)
13440Sstevel@tonic-gate 		{
13450Sstevel@tonic-gate 		int crl_v2 = 0;
13460Sstevel@tonic-gate 		if (!crl_ext)
13470Sstevel@tonic-gate 			{
13480Sstevel@tonic-gate 			crl_ext=NCONF_get_string(conf,section,ENV_CRLEXT);
13490Sstevel@tonic-gate 			if (!crl_ext)
13500Sstevel@tonic-gate 				ERR_clear_error();
13510Sstevel@tonic-gate 			}
13520Sstevel@tonic-gate 		if (crl_ext)
13530Sstevel@tonic-gate 			{
13540Sstevel@tonic-gate 			/* Check syntax of file */
13550Sstevel@tonic-gate 			X509V3_CTX ctx;
13560Sstevel@tonic-gate 			X509V3_set_ctx_test(&ctx);
13570Sstevel@tonic-gate 			X509V3_set_nconf(&ctx, conf);
13580Sstevel@tonic-gate 			if (!X509V3_EXT_add_nconf(conf, &ctx, crl_ext, NULL))
13590Sstevel@tonic-gate 				{
13600Sstevel@tonic-gate 				BIO_printf(bio_err,
13610Sstevel@tonic-gate 				 "Error Loading CRL extension section %s\n",
13620Sstevel@tonic-gate 								 crl_ext);
13630Sstevel@tonic-gate 				ret = 1;
13640Sstevel@tonic-gate 				goto err;
13650Sstevel@tonic-gate 				}
13660Sstevel@tonic-gate 			}
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 		if ((crlnumberfile=NCONF_get_string(conf,section,ENV_CRLNUMBER))
13690Sstevel@tonic-gate 			!= NULL)
13700Sstevel@tonic-gate 			if ((crlnumber=load_serial(crlnumberfile,0,NULL)) == NULL)
13710Sstevel@tonic-gate 				{
13720Sstevel@tonic-gate 				BIO_printf(bio_err,"error while loading CRL number\n");
13730Sstevel@tonic-gate 				goto err;
13740Sstevel@tonic-gate 				}
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate 		if (!crldays && !crlhours)
13770Sstevel@tonic-gate 			{
13780Sstevel@tonic-gate 			if (!NCONF_get_number(conf,section,
13790Sstevel@tonic-gate 				ENV_DEFAULT_CRL_DAYS, &crldays))
13800Sstevel@tonic-gate 				crldays = 0;
13810Sstevel@tonic-gate 			if (!NCONF_get_number(conf,section,
13820Sstevel@tonic-gate 				ENV_DEFAULT_CRL_HOURS, &crlhours))
13830Sstevel@tonic-gate 				crlhours = 0;
13840Sstevel@tonic-gate 			}
13850Sstevel@tonic-gate 		if ((crldays == 0) && (crlhours == 0))
13860Sstevel@tonic-gate 			{
13870Sstevel@tonic-gate 			BIO_printf(bio_err,"cannot lookup how long until the next CRL is issued\n");
13880Sstevel@tonic-gate 			goto err;
13890Sstevel@tonic-gate 			}
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate 		if (verbose) BIO_printf(bio_err,"making CRL\n");
13920Sstevel@tonic-gate 		if ((crl=X509_CRL_new()) == NULL) goto err;
13930Sstevel@tonic-gate 		if (!X509_CRL_set_issuer_name(crl, X509_get_subject_name(x509))) goto err;
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 		tmptm = ASN1_TIME_new();
13960Sstevel@tonic-gate 		if (!tmptm) goto err;
13970Sstevel@tonic-gate 		X509_gmtime_adj(tmptm,0);
13980Sstevel@tonic-gate 		X509_CRL_set_lastUpdate(crl, tmptm);
13990Sstevel@tonic-gate 		X509_gmtime_adj(tmptm,(crldays*24+crlhours)*60*60);
14000Sstevel@tonic-gate 		X509_CRL_set_nextUpdate(crl, tmptm);
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 		ASN1_TIME_free(tmptm);
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate 		for (i=0; i<sk_num(db->db->data); i++)
14050Sstevel@tonic-gate 			{
1406*2139Sjp161948 			pp=(const char **)sk_value(db->db->data,i);
14070Sstevel@tonic-gate 			if (pp[DB_type][0] == DB_TYPE_REV)
14080Sstevel@tonic-gate 				{
14090Sstevel@tonic-gate 				if ((r=X509_REVOKED_new()) == NULL) goto err;
14100Sstevel@tonic-gate 				j = make_revoked(r, pp[DB_rev_date]);
14110Sstevel@tonic-gate 				if (!j) goto err;
14120Sstevel@tonic-gate 				if (j == 2) crl_v2 = 1;
14130Sstevel@tonic-gate 				if (!BN_hex2bn(&serial, pp[DB_serial]))
14140Sstevel@tonic-gate 					goto err;
14150Sstevel@tonic-gate 				tmpser = BN_to_ASN1_INTEGER(serial, NULL);
14160Sstevel@tonic-gate 				BN_free(serial);
14170Sstevel@tonic-gate 				serial = NULL;
14180Sstevel@tonic-gate 				if (!tmpser)
14190Sstevel@tonic-gate 					goto err;
14200Sstevel@tonic-gate 				X509_REVOKED_set_serialNumber(r, tmpser);
14210Sstevel@tonic-gate 				ASN1_INTEGER_free(tmpser);
14220Sstevel@tonic-gate 				X509_CRL_add0_revoked(crl,r);
14230Sstevel@tonic-gate 				}
14240Sstevel@tonic-gate 			}
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 		/* sort the data so it will be written in serial
14270Sstevel@tonic-gate 		 * number order */
14280Sstevel@tonic-gate 		X509_CRL_sort(crl);
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 		/* we now have a CRL */
14310Sstevel@tonic-gate 		if (verbose) BIO_printf(bio_err,"signing CRL\n");
1432*2139Sjp161948 #ifndef OPENSSL_NO_DSA
1433*2139Sjp161948 		if (pkey->type == EVP_PKEY_DSA)
1434*2139Sjp161948 			dgst=EVP_dss1();
14350Sstevel@tonic-gate 		else
14360Sstevel@tonic-gate #endif
1437*2139Sjp161948 #ifndef OPENSSL_NO_ECDSA
1438*2139Sjp161948 		if (pkey->type == EVP_PKEY_EC)
1439*2139Sjp161948 			dgst=EVP_ecdsa();
1440*2139Sjp161948 #endif
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate 		/* Add any extensions asked for */
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate 		if (crl_ext || crlnumberfile != NULL)
14450Sstevel@tonic-gate 			{
14460Sstevel@tonic-gate 			X509V3_CTX crlctx;
14470Sstevel@tonic-gate 			X509V3_set_ctx(&crlctx, x509, NULL, NULL, crl, 0);
14480Sstevel@tonic-gate 			X509V3_set_nconf(&crlctx, conf);
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate 			if (crl_ext)
14510Sstevel@tonic-gate 				if (!X509V3_EXT_CRL_add_nconf(conf, &crlctx,
14520Sstevel@tonic-gate 					crl_ext, crl)) goto err;
14530Sstevel@tonic-gate 			if (crlnumberfile != NULL)
14540Sstevel@tonic-gate 				{
14550Sstevel@tonic-gate 				tmpser = BN_to_ASN1_INTEGER(crlnumber, NULL);
14560Sstevel@tonic-gate 				if (!tmpser) goto err;
14570Sstevel@tonic-gate 				X509_CRL_add1_ext_i2d(crl,NID_crl_number,tmpser,0,0);
14580Sstevel@tonic-gate 				ASN1_INTEGER_free(tmpser);
14590Sstevel@tonic-gate 				crl_v2 = 1;
14600Sstevel@tonic-gate 				if (!BN_add_word(crlnumber,1)) goto err;
14610Sstevel@tonic-gate 				}
14620Sstevel@tonic-gate 			}
14630Sstevel@tonic-gate 		if (crl_ext || crl_v2)
14640Sstevel@tonic-gate 			{
14650Sstevel@tonic-gate 			if (!X509_CRL_set_version(crl, 1))
14660Sstevel@tonic-gate 				goto err; /* version 2 CRL */
14670Sstevel@tonic-gate 			}
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate 		if (crlnumberfile != NULL)	/* we have a CRL number that need updating */
14710Sstevel@tonic-gate 			if (!save_serial(crlnumberfile,"new",crlnumber,NULL)) goto err;
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 		if (!X509_CRL_sign(crl,pkey,dgst)) goto err;
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 		PEM_write_bio_X509_CRL(Sout,crl);
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate 		if (crlnumberfile != NULL)	/* Rename the crlnumber file */
14780Sstevel@tonic-gate 			if (!rotate_serial(crlnumberfile,"new","old")) goto err;
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 		}
14810Sstevel@tonic-gate 	/*****************************************************************/
14820Sstevel@tonic-gate 	if (dorevoke)
14830Sstevel@tonic-gate 		{
14840Sstevel@tonic-gate 		if (infile == NULL)
14850Sstevel@tonic-gate 			{
14860Sstevel@tonic-gate 			BIO_printf(bio_err,"no input files\n");
14870Sstevel@tonic-gate 			goto err;
14880Sstevel@tonic-gate 			}
14890Sstevel@tonic-gate 		else
14900Sstevel@tonic-gate 			{
14910Sstevel@tonic-gate 			X509 *revcert;
14920Sstevel@tonic-gate 			revcert=load_cert(bio_err, infile, FORMAT_PEM,
14930Sstevel@tonic-gate 				NULL, e, infile);
14940Sstevel@tonic-gate 			if (revcert == NULL)
14950Sstevel@tonic-gate 				goto err;
14960Sstevel@tonic-gate 			j=do_revoke(revcert,db, rev_type, rev_arg);
14970Sstevel@tonic-gate 			if (j <= 0) goto err;
14980Sstevel@tonic-gate 			X509_free(revcert);
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate 			if (!save_index(dbfile, "new", db)) goto err;
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 			if (!rotate_index(dbfile, "new", "old")) goto err;
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate 			BIO_printf(bio_err,"Data Base Updated\n");
15050Sstevel@tonic-gate 			}
15060Sstevel@tonic-gate 		}
15070Sstevel@tonic-gate 	/*****************************************************************/
15080Sstevel@tonic-gate 	ret=0;
15090Sstevel@tonic-gate err:
15100Sstevel@tonic-gate 	if(tofree)
15110Sstevel@tonic-gate 		OPENSSL_free(tofree);
15120Sstevel@tonic-gate 	BIO_free_all(Cout);
15130Sstevel@tonic-gate 	BIO_free_all(Sout);
15140Sstevel@tonic-gate 	BIO_free_all(out);
15150Sstevel@tonic-gate 	BIO_free_all(in);
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 	if (cert_sk)
15180Sstevel@tonic-gate 		sk_X509_pop_free(cert_sk,X509_free);
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	if (ret) ERR_print_errors(bio_err);
15210Sstevel@tonic-gate 	app_RAND_write_file(randfile, bio_err);
15220Sstevel@tonic-gate 	if (free_key && key)
15230Sstevel@tonic-gate 		OPENSSL_free(key);
15240Sstevel@tonic-gate 	BN_free(serial);
15250Sstevel@tonic-gate 	if (db)
15260Sstevel@tonic-gate 		free_index(db);
15270Sstevel@tonic-gate 	EVP_PKEY_free(pkey);
1528*2139Sjp161948 	if (x509) X509_free(x509);
15290Sstevel@tonic-gate 	X509_CRL_free(crl);
15300Sstevel@tonic-gate 	NCONF_free(conf);
15310Sstevel@tonic-gate 	OBJ_cleanup();
15320Sstevel@tonic-gate 	apps_shutdown();
15330Sstevel@tonic-gate 	OPENSSL_EXIT(ret);
15340Sstevel@tonic-gate 	}
15350Sstevel@tonic-gate 
lookup_fail(const char * name,const char * tag)1536*2139Sjp161948 static void lookup_fail(const char *name, const char *tag)
15370Sstevel@tonic-gate 	{
15380Sstevel@tonic-gate 	BIO_printf(bio_err,"variable lookup failed for %s::%s\n",name,tag);
15390Sstevel@tonic-gate 	}
15400Sstevel@tonic-gate 
certify(X509 ** xret,char * infile,EVP_PKEY * pkey,X509 * x509,const EVP_MD * dgst,STACK_OF (CONF_VALUE)* policy,CA_DB * db,BIGNUM * serial,char * subj,unsigned long chtype,int multirdn,int email_dn,char * startdate,char * enddate,long days,int batch,char * ext_sect,CONF * lconf,int verbose,unsigned long certopt,unsigned long nameopt,int default_op,int ext_copy,int selfsign)15410Sstevel@tonic-gate static int certify(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
15420Sstevel@tonic-gate 	     const EVP_MD *dgst, STACK_OF(CONF_VALUE) *policy, CA_DB *db,
1543*2139Sjp161948 	     BIGNUM *serial, char *subj,unsigned long chtype, int multirdn, int email_dn, char *startdate, char *enddate,
15440Sstevel@tonic-gate 	     long days, int batch, char *ext_sect, CONF *lconf, int verbose,
15450Sstevel@tonic-gate 	     unsigned long certopt, unsigned long nameopt, int default_op,
1546*2139Sjp161948 	     int ext_copy, int selfsign)
15470Sstevel@tonic-gate 	{
15480Sstevel@tonic-gate 	X509_REQ *req=NULL;
15490Sstevel@tonic-gate 	BIO *in=NULL;
15500Sstevel@tonic-gate 	EVP_PKEY *pktmp=NULL;
15510Sstevel@tonic-gate 	int ok= -1,i;
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 	in=BIO_new(BIO_s_file());
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 	if (BIO_read_filename(in,infile) <= 0)
15560Sstevel@tonic-gate 		{
15570Sstevel@tonic-gate 		perror(infile);
15580Sstevel@tonic-gate 		goto err;
15590Sstevel@tonic-gate 		}
15600Sstevel@tonic-gate 	if ((req=PEM_read_bio_X509_REQ(in,NULL,NULL,NULL)) == NULL)
15610Sstevel@tonic-gate 		{
15620Sstevel@tonic-gate 		BIO_printf(bio_err,"Error reading certificate request in %s\n",
15630Sstevel@tonic-gate 			infile);
15640Sstevel@tonic-gate 		goto err;
15650Sstevel@tonic-gate 		}
15660Sstevel@tonic-gate 	if (verbose)
15670Sstevel@tonic-gate 		X509_REQ_print(bio_err,req);
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate 	BIO_printf(bio_err,"Check that the request matches the signature\n");
15700Sstevel@tonic-gate 
1571*2139Sjp161948 	if (selfsign && !X509_REQ_check_private_key(req,pkey))
1572*2139Sjp161948 		{
1573*2139Sjp161948 		BIO_printf(bio_err,"Certificate request and CA private key do not match\n");
1574*2139Sjp161948 		ok=0;
1575*2139Sjp161948 		goto err;
1576*2139Sjp161948 		}
15770Sstevel@tonic-gate 	if ((pktmp=X509_REQ_get_pubkey(req)) == NULL)
15780Sstevel@tonic-gate 		{
15790Sstevel@tonic-gate 		BIO_printf(bio_err,"error unpacking public key\n");
15800Sstevel@tonic-gate 		goto err;
15810Sstevel@tonic-gate 		}
15820Sstevel@tonic-gate 	i=X509_REQ_verify(req,pktmp);
15830Sstevel@tonic-gate 	EVP_PKEY_free(pktmp);
15840Sstevel@tonic-gate 	if (i < 0)
15850Sstevel@tonic-gate 		{
15860Sstevel@tonic-gate 		ok=0;
15870Sstevel@tonic-gate 		BIO_printf(bio_err,"Signature verification problems....\n");
15880Sstevel@tonic-gate 		goto err;
15890Sstevel@tonic-gate 		}
15900Sstevel@tonic-gate 	if (i == 0)
15910Sstevel@tonic-gate 		{
15920Sstevel@tonic-gate 		ok=0;
15930Sstevel@tonic-gate 		BIO_printf(bio_err,"Signature did not match the certificate request\n");
15940Sstevel@tonic-gate 		goto err;
15950Sstevel@tonic-gate 		}
15960Sstevel@tonic-gate 	else
15970Sstevel@tonic-gate 		BIO_printf(bio_err,"Signature ok\n");
15980Sstevel@tonic-gate 
1599*2139Sjp161948 	ok=do_body(xret,pkey,x509,dgst,policy,db,serial,subj,chtype,multirdn, email_dn,
16000Sstevel@tonic-gate 		startdate,enddate,days,batch,verbose,req,ext_sect,lconf,
1601*2139Sjp161948 		certopt, nameopt, default_op, ext_copy, selfsign);
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate err:
16040Sstevel@tonic-gate 	if (req != NULL) X509_REQ_free(req);
16050Sstevel@tonic-gate 	if (in != NULL) BIO_free(in);
16060Sstevel@tonic-gate 	return(ok);
16070Sstevel@tonic-gate 	}
16080Sstevel@tonic-gate 
certify_cert(X509 ** xret,char * infile,EVP_PKEY * pkey,X509 * x509,const EVP_MD * dgst,STACK_OF (CONF_VALUE)* policy,CA_DB * db,BIGNUM * serial,char * subj,unsigned long chtype,int multirdn,int email_dn,char * startdate,char * enddate,long days,int batch,char * ext_sect,CONF * lconf,int verbose,unsigned long certopt,unsigned long nameopt,int default_op,int ext_copy,ENGINE * e)16090Sstevel@tonic-gate static int certify_cert(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
16100Sstevel@tonic-gate 	     const EVP_MD *dgst, STACK_OF(CONF_VALUE) *policy, CA_DB *db,
1611*2139Sjp161948 	     BIGNUM *serial, char *subj, unsigned long chtype, int multirdn, int email_dn, char *startdate, char *enddate,
16120Sstevel@tonic-gate 	     long days, int batch, char *ext_sect, CONF *lconf, int verbose,
16130Sstevel@tonic-gate 	     unsigned long certopt, unsigned long nameopt, int default_op,
16140Sstevel@tonic-gate 	     int ext_copy, ENGINE *e)
16150Sstevel@tonic-gate 	{
16160Sstevel@tonic-gate 	X509 *req=NULL;
16170Sstevel@tonic-gate 	X509_REQ *rreq=NULL;
16180Sstevel@tonic-gate 	EVP_PKEY *pktmp=NULL;
16190Sstevel@tonic-gate 	int ok= -1,i;
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 	if ((req=load_cert(bio_err, infile, FORMAT_PEM, NULL, e, infile)) == NULL)
16220Sstevel@tonic-gate 		goto err;
16230Sstevel@tonic-gate 	if (verbose)
16240Sstevel@tonic-gate 		X509_print(bio_err,req);
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate 	BIO_printf(bio_err,"Check that the request matches the signature\n");
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 	if ((pktmp=X509_get_pubkey(req)) == NULL)
16290Sstevel@tonic-gate 		{
16300Sstevel@tonic-gate 		BIO_printf(bio_err,"error unpacking public key\n");
16310Sstevel@tonic-gate 		goto err;
16320Sstevel@tonic-gate 		}
16330Sstevel@tonic-gate 	i=X509_verify(req,pktmp);
16340Sstevel@tonic-gate 	EVP_PKEY_free(pktmp);
16350Sstevel@tonic-gate 	if (i < 0)
16360Sstevel@tonic-gate 		{
16370Sstevel@tonic-gate 		ok=0;
16380Sstevel@tonic-gate 		BIO_printf(bio_err,"Signature verification problems....\n");
16390Sstevel@tonic-gate 		goto err;
16400Sstevel@tonic-gate 		}
16410Sstevel@tonic-gate 	if (i == 0)
16420Sstevel@tonic-gate 		{
16430Sstevel@tonic-gate 		ok=0;
16440Sstevel@tonic-gate 		BIO_printf(bio_err,"Signature did not match the certificate\n");
16450Sstevel@tonic-gate 		goto err;
16460Sstevel@tonic-gate 		}
16470Sstevel@tonic-gate 	else
16480Sstevel@tonic-gate 		BIO_printf(bio_err,"Signature ok\n");
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate 	if ((rreq=X509_to_X509_REQ(req,NULL,EVP_md5())) == NULL)
16510Sstevel@tonic-gate 		goto err;
16520Sstevel@tonic-gate 
1653*2139Sjp161948 	ok=do_body(xret,pkey,x509,dgst,policy,db,serial,subj,chtype,multirdn,email_dn,startdate,enddate,
16540Sstevel@tonic-gate 		days,batch,verbose,rreq,ext_sect,lconf, certopt, nameopt, default_op,
1655*2139Sjp161948 		ext_copy, 0);
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate err:
16580Sstevel@tonic-gate 	if (rreq != NULL) X509_REQ_free(rreq);
16590Sstevel@tonic-gate 	if (req != NULL) X509_free(req);
16600Sstevel@tonic-gate 	return(ok);
16610Sstevel@tonic-gate 	}
16620Sstevel@tonic-gate 
do_body(X509 ** xret,EVP_PKEY * pkey,X509 * x509,const EVP_MD * dgst,STACK_OF (CONF_VALUE)* policy,CA_DB * db,BIGNUM * serial,char * subj,unsigned long chtype,int multirdn,int email_dn,char * startdate,char * enddate,long days,int batch,int verbose,X509_REQ * req,char * ext_sect,CONF * lconf,unsigned long certopt,unsigned long nameopt,int default_op,int ext_copy,int selfsign)16630Sstevel@tonic-gate static int do_body(X509 **xret, EVP_PKEY *pkey, X509 *x509, const EVP_MD *dgst,
16640Sstevel@tonic-gate 	     STACK_OF(CONF_VALUE) *policy, CA_DB *db, BIGNUM *serial, char *subj,
1665*2139Sjp161948 	     unsigned long chtype, int multirdn,
16660Sstevel@tonic-gate 	     int email_dn, char *startdate, char *enddate, long days, int batch,
16670Sstevel@tonic-gate 	     int verbose, X509_REQ *req, char *ext_sect, CONF *lconf,
16680Sstevel@tonic-gate 	     unsigned long certopt, unsigned long nameopt, int default_op,
1669*2139Sjp161948 	     int ext_copy, int selfsign)
16700Sstevel@tonic-gate 	{
16710Sstevel@tonic-gate 	X509_NAME *name=NULL,*CAname=NULL,*subject=NULL, *dn_subject=NULL;
16720Sstevel@tonic-gate 	ASN1_UTCTIME *tm,*tmptm;
16730Sstevel@tonic-gate 	ASN1_STRING *str,*str2;
16740Sstevel@tonic-gate 	ASN1_OBJECT *obj;
16750Sstevel@tonic-gate 	X509 *ret=NULL;
16760Sstevel@tonic-gate 	X509_CINF *ci;
16770Sstevel@tonic-gate 	X509_NAME_ENTRY *ne;
16780Sstevel@tonic-gate 	X509_NAME_ENTRY *tne,*push;
16790Sstevel@tonic-gate 	EVP_PKEY *pktmp;
16800Sstevel@tonic-gate 	int ok= -1,i,j,last,nid;
1681*2139Sjp161948 	const char *p;
16820Sstevel@tonic-gate 	CONF_VALUE *cv;
16830Sstevel@tonic-gate 	char *row[DB_NUMBER],**rrow=NULL,**irow=NULL;
16840Sstevel@tonic-gate 	char buf[25];
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 	tmptm=ASN1_UTCTIME_new();
16870Sstevel@tonic-gate 	if (tmptm == NULL)
16880Sstevel@tonic-gate 		{
16890Sstevel@tonic-gate 		BIO_printf(bio_err,"malloc error\n");
16900Sstevel@tonic-gate 		return(0);
16910Sstevel@tonic-gate 		}
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate 	for (i=0; i<DB_NUMBER; i++)
16940Sstevel@tonic-gate 		row[i]=NULL;
16950Sstevel@tonic-gate 
16960Sstevel@tonic-gate 	if (subj)
16970Sstevel@tonic-gate 		{
1698*2139Sjp161948 		X509_NAME *n = parse_name(subj, chtype, multirdn);
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 		if (!n)
17010Sstevel@tonic-gate 			{
17020Sstevel@tonic-gate 			ERR_print_errors(bio_err);
17030Sstevel@tonic-gate 			goto err;
17040Sstevel@tonic-gate 			}
17050Sstevel@tonic-gate 		X509_REQ_set_subject_name(req,n);
17060Sstevel@tonic-gate 		req->req_info->enc.modified = 1;
17070Sstevel@tonic-gate 		X509_NAME_free(n);
17080Sstevel@tonic-gate 		}
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate 	if (default_op)
17110Sstevel@tonic-gate 		BIO_printf(bio_err,"The Subject's Distinguished Name is as follows\n");
17120Sstevel@tonic-gate 
17130Sstevel@tonic-gate 	name=X509_REQ_get_subject_name(req);
17140Sstevel@tonic-gate 	for (i=0; i<X509_NAME_entry_count(name); i++)
17150Sstevel@tonic-gate 		{
17160Sstevel@tonic-gate 		ne= X509_NAME_get_entry(name,i);
17170Sstevel@tonic-gate 		str=X509_NAME_ENTRY_get_data(ne);
17180Sstevel@tonic-gate 		obj=X509_NAME_ENTRY_get_object(ne);
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate 		if (msie_hack)
17210Sstevel@tonic-gate 			{
17220Sstevel@tonic-gate 			/* assume all type should be strings */
17230Sstevel@tonic-gate 			nid=OBJ_obj2nid(ne->object);
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate 			if (str->type == V_ASN1_UNIVERSALSTRING)
17260Sstevel@tonic-gate 				ASN1_UNIVERSALSTRING_to_string(str);
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate 			if ((str->type == V_ASN1_IA5STRING) &&
17290Sstevel@tonic-gate 				(nid != NID_pkcs9_emailAddress))
17300Sstevel@tonic-gate 				str->type=V_ASN1_T61STRING;
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate 			if ((nid == NID_pkcs9_emailAddress) &&
17330Sstevel@tonic-gate 				(str->type == V_ASN1_PRINTABLESTRING))
17340Sstevel@tonic-gate 				str->type=V_ASN1_IA5STRING;
17350Sstevel@tonic-gate 			}
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 		/* If no EMAIL is wanted in the subject */
17380Sstevel@tonic-gate 		if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) && (!email_dn))
17390Sstevel@tonic-gate 			continue;
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate 		/* check some things */
17420Sstevel@tonic-gate 		if ((OBJ_obj2nid(obj) == NID_pkcs9_emailAddress) &&
17430Sstevel@tonic-gate 			(str->type != V_ASN1_IA5STRING))
17440Sstevel@tonic-gate 			{
17450Sstevel@tonic-gate 			BIO_printf(bio_err,"\nemailAddress type needs to be of type IA5STRING\n");
17460Sstevel@tonic-gate 			goto err;
17470Sstevel@tonic-gate 			}
17480Sstevel@tonic-gate 		if ((str->type != V_ASN1_BMPSTRING) && (str->type != V_ASN1_UTF8STRING))
17490Sstevel@tonic-gate 			{
17500Sstevel@tonic-gate 			j=ASN1_PRINTABLE_type(str->data,str->length);
17510Sstevel@tonic-gate 			if (	((j == V_ASN1_T61STRING) &&
17520Sstevel@tonic-gate 				 (str->type != V_ASN1_T61STRING)) ||
17530Sstevel@tonic-gate 				((j == V_ASN1_IA5STRING) &&
17540Sstevel@tonic-gate 				 (str->type == V_ASN1_PRINTABLESTRING)))
17550Sstevel@tonic-gate 				{
17560Sstevel@tonic-gate 				BIO_printf(bio_err,"\nThe string contains characters that are illegal for the ASN.1 type\n");
17570Sstevel@tonic-gate 				goto err;
17580Sstevel@tonic-gate 				}
17590Sstevel@tonic-gate 			}
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate 		if (default_op)
17620Sstevel@tonic-gate 			old_entry_print(bio_err, obj, str);
17630Sstevel@tonic-gate 		}
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate 	/* Ok, now we check the 'policy' stuff. */
17660Sstevel@tonic-gate 	if ((subject=X509_NAME_new()) == NULL)
17670Sstevel@tonic-gate 		{
17680Sstevel@tonic-gate 		BIO_printf(bio_err,"Memory allocation failure\n");
17690Sstevel@tonic-gate 		goto err;
17700Sstevel@tonic-gate 		}
17710Sstevel@tonic-gate 
17720Sstevel@tonic-gate 	/* take a copy of the issuer name before we mess with it. */
1773*2139Sjp161948 	if (selfsign)
1774*2139Sjp161948 		CAname=X509_NAME_dup(name);
1775*2139Sjp161948 	else
1776*2139Sjp161948 		CAname=X509_NAME_dup(x509->cert_info->subject);
17770Sstevel@tonic-gate 	if (CAname == NULL) goto err;
17780Sstevel@tonic-gate 	str=str2=NULL;
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate 	for (i=0; i<sk_CONF_VALUE_num(policy); i++)
17810Sstevel@tonic-gate 		{
17820Sstevel@tonic-gate 		cv=sk_CONF_VALUE_value(policy,i); /* get the object id */
17830Sstevel@tonic-gate 		if ((j=OBJ_txt2nid(cv->name)) == NID_undef)
17840Sstevel@tonic-gate 			{
17850Sstevel@tonic-gate 			BIO_printf(bio_err,"%s:unknown object type in 'policy' configuration\n",cv->name);
17860Sstevel@tonic-gate 			goto err;
17870Sstevel@tonic-gate 			}
17880Sstevel@tonic-gate 		obj=OBJ_nid2obj(j);
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate 		last= -1;
17910Sstevel@tonic-gate 		for (;;)
17920Sstevel@tonic-gate 			{
17930Sstevel@tonic-gate 			/* lookup the object in the supplied name list */
17940Sstevel@tonic-gate 			j=X509_NAME_get_index_by_OBJ(name,obj,last);
17950Sstevel@tonic-gate 			if (j < 0)
17960Sstevel@tonic-gate 				{
17970Sstevel@tonic-gate 				if (last != -1) break;
17980Sstevel@tonic-gate 				tne=NULL;
17990Sstevel@tonic-gate 				}
18000Sstevel@tonic-gate 			else
18010Sstevel@tonic-gate 				{
18020Sstevel@tonic-gate 				tne=X509_NAME_get_entry(name,j);
18030Sstevel@tonic-gate 				}
18040Sstevel@tonic-gate 			last=j;
18050Sstevel@tonic-gate 
18060Sstevel@tonic-gate 			/* depending on the 'policy', decide what to do. */
18070Sstevel@tonic-gate 			push=NULL;
18080Sstevel@tonic-gate 			if (strcmp(cv->value,"optional") == 0)
18090Sstevel@tonic-gate 				{
18100Sstevel@tonic-gate 				if (tne != NULL)
18110Sstevel@tonic-gate 					push=tne;
18120Sstevel@tonic-gate 				}
18130Sstevel@tonic-gate 			else if (strcmp(cv->value,"supplied") == 0)
18140Sstevel@tonic-gate 				{
18150Sstevel@tonic-gate 				if (tne == NULL)
18160Sstevel@tonic-gate 					{
18170Sstevel@tonic-gate 					BIO_printf(bio_err,"The %s field needed to be supplied and was missing\n",cv->name);
18180Sstevel@tonic-gate 					goto err;
18190Sstevel@tonic-gate 					}
18200Sstevel@tonic-gate 				else
18210Sstevel@tonic-gate 					push=tne;
18220Sstevel@tonic-gate 				}
18230Sstevel@tonic-gate 			else if (strcmp(cv->value,"match") == 0)
18240Sstevel@tonic-gate 				{
18250Sstevel@tonic-gate 				int last2;
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate 				if (tne == NULL)
18280Sstevel@tonic-gate 					{
18290Sstevel@tonic-gate 					BIO_printf(bio_err,"The mandatory %s field was missing\n",cv->name);
18300Sstevel@tonic-gate 					goto err;
18310Sstevel@tonic-gate 					}
18320Sstevel@tonic-gate 
18330Sstevel@tonic-gate 				last2= -1;
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate again2:
18360Sstevel@tonic-gate 				j=X509_NAME_get_index_by_OBJ(CAname,obj,last2);
18370Sstevel@tonic-gate 				if ((j < 0) && (last2 == -1))
18380Sstevel@tonic-gate 					{
18390Sstevel@tonic-gate 					BIO_printf(bio_err,"The %s field does not exist in the CA certificate,\nthe 'policy' is misconfigured\n",cv->name);
18400Sstevel@tonic-gate 					goto err;
18410Sstevel@tonic-gate 					}
18420Sstevel@tonic-gate 				if (j >= 0)
18430Sstevel@tonic-gate 					{
18440Sstevel@tonic-gate 					push=X509_NAME_get_entry(CAname,j);
18450Sstevel@tonic-gate 					str=X509_NAME_ENTRY_get_data(tne);
18460Sstevel@tonic-gate 					str2=X509_NAME_ENTRY_get_data(push);
18470Sstevel@tonic-gate 					last2=j;
18480Sstevel@tonic-gate 					if (ASN1_STRING_cmp(str,str2) != 0)
18490Sstevel@tonic-gate 						goto again2;
18500Sstevel@tonic-gate 					}
18510Sstevel@tonic-gate 				if (j < 0)
18520Sstevel@tonic-gate 					{
18530Sstevel@tonic-gate 					BIO_printf(bio_err,"The %s field needed to be the same in the\nCA certificate (%s) and the request (%s)\n",cv->name,((str2 == NULL)?"NULL":(char *)str2->data),((str == NULL)?"NULL":(char *)str->data));
18540Sstevel@tonic-gate 					goto err;
18550Sstevel@tonic-gate 					}
18560Sstevel@tonic-gate 				}
18570Sstevel@tonic-gate 			else
18580Sstevel@tonic-gate 				{
18590Sstevel@tonic-gate 				BIO_printf(bio_err,"%s:invalid type in 'policy' configuration\n",cv->value);
18600Sstevel@tonic-gate 				goto err;
18610Sstevel@tonic-gate 				}
18620Sstevel@tonic-gate 
18630Sstevel@tonic-gate 			if (push != NULL)
18640Sstevel@tonic-gate 				{
18650Sstevel@tonic-gate 				if (!X509_NAME_add_entry(subject,push, -1, 0))
18660Sstevel@tonic-gate 					{
18670Sstevel@tonic-gate 					if (push != NULL)
18680Sstevel@tonic-gate 						X509_NAME_ENTRY_free(push);
18690Sstevel@tonic-gate 					BIO_printf(bio_err,"Memory allocation failure\n");
18700Sstevel@tonic-gate 					goto err;
18710Sstevel@tonic-gate 					}
18720Sstevel@tonic-gate 				}
18730Sstevel@tonic-gate 			if (j < 0) break;
18740Sstevel@tonic-gate 			}
18750Sstevel@tonic-gate 		}
18760Sstevel@tonic-gate 
18770Sstevel@tonic-gate 	if (preserve)
18780Sstevel@tonic-gate 		{
18790Sstevel@tonic-gate 		X509_NAME_free(subject);
18800Sstevel@tonic-gate 		/* subject=X509_NAME_dup(X509_REQ_get_subject_name(req)); */
18810Sstevel@tonic-gate 		subject=X509_NAME_dup(name);
18820Sstevel@tonic-gate 		if (subject == NULL) goto err;
18830Sstevel@tonic-gate 		}
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate 	if (verbose)
18860Sstevel@tonic-gate 		BIO_printf(bio_err,"The subject name appears to be ok, checking data base for clashes\n");
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate 	/* Build the correct Subject if no e-mail is wanted in the subject */
18890Sstevel@tonic-gate 	/* and add it later on because of the method extensions are added (altName) */
18900Sstevel@tonic-gate 
18910Sstevel@tonic-gate 	if (email_dn)
18920Sstevel@tonic-gate 		dn_subject = subject;
18930Sstevel@tonic-gate 	else
18940Sstevel@tonic-gate 		{
18950Sstevel@tonic-gate 		X509_NAME_ENTRY *tmpne;
18960Sstevel@tonic-gate 		/* Its best to dup the subject DN and then delete any email
18970Sstevel@tonic-gate 		 * addresses because this retains its structure.
18980Sstevel@tonic-gate 		 */
18990Sstevel@tonic-gate 		if (!(dn_subject = X509_NAME_dup(subject)))
19000Sstevel@tonic-gate 			{
19010Sstevel@tonic-gate 			BIO_printf(bio_err,"Memory allocation failure\n");
19020Sstevel@tonic-gate 			goto err;
19030Sstevel@tonic-gate 			}
19040Sstevel@tonic-gate 		while((i = X509_NAME_get_index_by_NID(dn_subject,
19050Sstevel@tonic-gate 					NID_pkcs9_emailAddress, -1)) >= 0)
19060Sstevel@tonic-gate 			{
19070Sstevel@tonic-gate 			tmpne = X509_NAME_get_entry(dn_subject, i);
19080Sstevel@tonic-gate 			X509_NAME_delete_entry(dn_subject, i);
19090Sstevel@tonic-gate 			X509_NAME_ENTRY_free(tmpne);
19100Sstevel@tonic-gate 			}
19110Sstevel@tonic-gate 		}
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate 	if (BN_is_zero(serial))
19140Sstevel@tonic-gate 		row[DB_serial]=BUF_strdup("00");
19150Sstevel@tonic-gate 	else
19160Sstevel@tonic-gate 		row[DB_serial]=BN_bn2hex(serial);
19170Sstevel@tonic-gate 	if (row[DB_serial] == NULL)
19180Sstevel@tonic-gate 		{
19190Sstevel@tonic-gate 		BIO_printf(bio_err,"Memory allocation failure\n");
19200Sstevel@tonic-gate 		goto err;
19210Sstevel@tonic-gate 		}
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate 	if (db->attributes.unique_subject)
19240Sstevel@tonic-gate 		{
19250Sstevel@tonic-gate 		rrow=TXT_DB_get_by_index(db->db,DB_name,row);
19260Sstevel@tonic-gate 		if (rrow != NULL)
19270Sstevel@tonic-gate 			{
19280Sstevel@tonic-gate 			BIO_printf(bio_err,
19290Sstevel@tonic-gate 				"ERROR:There is already a certificate for %s\n",
19300Sstevel@tonic-gate 				row[DB_name]);
19310Sstevel@tonic-gate 			}
19320Sstevel@tonic-gate 		}
19330Sstevel@tonic-gate 	if (rrow == NULL)
19340Sstevel@tonic-gate 		{
19350Sstevel@tonic-gate 		rrow=TXT_DB_get_by_index(db->db,DB_serial,row);
19360Sstevel@tonic-gate 		if (rrow != NULL)
19370Sstevel@tonic-gate 			{
19380Sstevel@tonic-gate 			BIO_printf(bio_err,"ERROR:Serial number %s has already been issued,\n",
19390Sstevel@tonic-gate 				row[DB_serial]);
19400Sstevel@tonic-gate 			BIO_printf(bio_err,"      check the database/serial_file for corruption\n");
19410Sstevel@tonic-gate 			}
19420Sstevel@tonic-gate 		}
19430Sstevel@tonic-gate 
19440Sstevel@tonic-gate 	if (rrow != NULL)
19450Sstevel@tonic-gate 		{
19460Sstevel@tonic-gate 		BIO_printf(bio_err,
19470Sstevel@tonic-gate 			"The matching entry has the following details\n");
19480Sstevel@tonic-gate 		if (rrow[DB_type][0] == 'E')
19490Sstevel@tonic-gate 			p="Expired";
19500Sstevel@tonic-gate 		else if (rrow[DB_type][0] == 'R')
19510Sstevel@tonic-gate 			p="Revoked";
19520Sstevel@tonic-gate 		else if (rrow[DB_type][0] == 'V')
19530Sstevel@tonic-gate 			p="Valid";
19540Sstevel@tonic-gate 		else
19550Sstevel@tonic-gate 			p="\ninvalid type, Data base error\n";
19560Sstevel@tonic-gate 		BIO_printf(bio_err,"Type	  :%s\n",p);;
19570Sstevel@tonic-gate 		if (rrow[DB_type][0] == 'R')
19580Sstevel@tonic-gate 			{
19590Sstevel@tonic-gate 			p=rrow[DB_exp_date]; if (p == NULL) p="undef";
19600Sstevel@tonic-gate 			BIO_printf(bio_err,"Was revoked on:%s\n",p);
19610Sstevel@tonic-gate 			}
19620Sstevel@tonic-gate 		p=rrow[DB_exp_date]; if (p == NULL) p="undef";
19630Sstevel@tonic-gate 		BIO_printf(bio_err,"Expires on    :%s\n",p);
19640Sstevel@tonic-gate 		p=rrow[DB_serial]; if (p == NULL) p="undef";
19650Sstevel@tonic-gate 		BIO_printf(bio_err,"Serial Number :%s\n",p);
19660Sstevel@tonic-gate 		p=rrow[DB_file]; if (p == NULL) p="undef";
19670Sstevel@tonic-gate 		BIO_printf(bio_err,"File name     :%s\n",p);
19680Sstevel@tonic-gate 		p=rrow[DB_name]; if (p == NULL) p="undef";
19690Sstevel@tonic-gate 		BIO_printf(bio_err,"Subject Name  :%s\n",p);
19700Sstevel@tonic-gate 		ok= -1; /* This is now a 'bad' error. */
19710Sstevel@tonic-gate 		goto err;
19720Sstevel@tonic-gate 		}
19730Sstevel@tonic-gate 
19740Sstevel@tonic-gate 	/* We are now totally happy, lets make and sign the certificate */
19750Sstevel@tonic-gate 	if (verbose)
19760Sstevel@tonic-gate 		BIO_printf(bio_err,"Everything appears to be ok, creating and signing the certificate\n");
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate 	if ((ret=X509_new()) == NULL) goto err;
19790Sstevel@tonic-gate 	ci=ret->cert_info;
19800Sstevel@tonic-gate 
19810Sstevel@tonic-gate #ifdef X509_V3
19820Sstevel@tonic-gate 	/* Make it an X509 v3 certificate. */
19830Sstevel@tonic-gate 	if (!X509_set_version(ret,2)) goto err;
19840Sstevel@tonic-gate #endif
19850Sstevel@tonic-gate 
19860Sstevel@tonic-gate 	if (BN_to_ASN1_INTEGER(serial,ci->serialNumber) == NULL)
19870Sstevel@tonic-gate 		goto err;
1988*2139Sjp161948 	if (selfsign)
1989*2139Sjp161948 		{
1990*2139Sjp161948 		if (!X509_set_issuer_name(ret,subject))
1991*2139Sjp161948 			goto err;
1992*2139Sjp161948 		}
1993*2139Sjp161948 	else
1994*2139Sjp161948 		{
1995*2139Sjp161948 		if (!X509_set_issuer_name(ret,X509_get_subject_name(x509)))
1996*2139Sjp161948 			goto err;
1997*2139Sjp161948 		}
19980Sstevel@tonic-gate 
19990Sstevel@tonic-gate 	if (strcmp(startdate,"today") == 0)
20000Sstevel@tonic-gate 		X509_gmtime_adj(X509_get_notBefore(ret),0);
20010Sstevel@tonic-gate 	else ASN1_UTCTIME_set_string(X509_get_notBefore(ret),startdate);
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate 	if (enddate == NULL)
20040Sstevel@tonic-gate 		X509_gmtime_adj(X509_get_notAfter(ret),(long)60*60*24*days);
20050Sstevel@tonic-gate 	else ASN1_UTCTIME_set_string(X509_get_notAfter(ret),enddate);
20060Sstevel@tonic-gate 
20070Sstevel@tonic-gate 	if (!X509_set_subject_name(ret,subject)) goto err;
20080Sstevel@tonic-gate 
20090Sstevel@tonic-gate 	pktmp=X509_REQ_get_pubkey(req);
20100Sstevel@tonic-gate 	i = X509_set_pubkey(ret,pktmp);
20110Sstevel@tonic-gate 	EVP_PKEY_free(pktmp);
20120Sstevel@tonic-gate 	if (!i) goto err;
20130Sstevel@tonic-gate 
20140Sstevel@tonic-gate 	/* Lets add the extensions, if there are any */
20150Sstevel@tonic-gate 	if (ext_sect)
20160Sstevel@tonic-gate 		{
20170Sstevel@tonic-gate 		X509V3_CTX ctx;
20180Sstevel@tonic-gate 		if (ci->version == NULL)
20190Sstevel@tonic-gate 			if ((ci->version=ASN1_INTEGER_new()) == NULL)
20200Sstevel@tonic-gate 				goto err;
20210Sstevel@tonic-gate 		ASN1_INTEGER_set(ci->version,2); /* version 3 certificate */
20220Sstevel@tonic-gate 
20230Sstevel@tonic-gate 		/* Free the current entries if any, there should not
20240Sstevel@tonic-gate 		 * be any I believe */
20250Sstevel@tonic-gate 		if (ci->extensions != NULL)
20260Sstevel@tonic-gate 			sk_X509_EXTENSION_pop_free(ci->extensions,
20270Sstevel@tonic-gate 						   X509_EXTENSION_free);
20280Sstevel@tonic-gate 
20290Sstevel@tonic-gate 		ci->extensions = NULL;
20300Sstevel@tonic-gate 
20310Sstevel@tonic-gate 		/* Initialize the context structure */
2032*2139Sjp161948 		if (selfsign)
2033*2139Sjp161948 			X509V3_set_ctx(&ctx, ret, ret, req, NULL, 0);
2034*2139Sjp161948 		else
2035*2139Sjp161948 			X509V3_set_ctx(&ctx, x509, ret, req, NULL, 0);
20360Sstevel@tonic-gate 
20370Sstevel@tonic-gate 		if (extconf)
20380Sstevel@tonic-gate 			{
20390Sstevel@tonic-gate 			if (verbose)
20400Sstevel@tonic-gate 				BIO_printf(bio_err, "Extra configuration file found\n");
20410Sstevel@tonic-gate 
20420Sstevel@tonic-gate 			/* Use the extconf configuration db LHASH */
20430Sstevel@tonic-gate 			X509V3_set_nconf(&ctx, extconf);
20440Sstevel@tonic-gate 
20450Sstevel@tonic-gate 			/* Test the structure (needed?) */
20460Sstevel@tonic-gate 			/* X509V3_set_ctx_test(&ctx); */
20470Sstevel@tonic-gate 
20480Sstevel@tonic-gate 			/* Adds exts contained in the configuration file */
20490Sstevel@tonic-gate 			if (!X509V3_EXT_add_nconf(extconf, &ctx, ext_sect,ret))
20500Sstevel@tonic-gate 				{
20510Sstevel@tonic-gate 				BIO_printf(bio_err,
20520Sstevel@tonic-gate 				    "ERROR: adding extensions in section %s\n",
20530Sstevel@tonic-gate 								ext_sect);
20540Sstevel@tonic-gate 				ERR_print_errors(bio_err);
20550Sstevel@tonic-gate 				goto err;
20560Sstevel@tonic-gate 				}
20570Sstevel@tonic-gate 			if (verbose)
20580Sstevel@tonic-gate 				BIO_printf(bio_err, "Successfully added extensions from file.\n");
20590Sstevel@tonic-gate 			}
20600Sstevel@tonic-gate 		else if (ext_sect)
20610Sstevel@tonic-gate 			{
20620Sstevel@tonic-gate 			/* We found extensions to be set from config file */
20630Sstevel@tonic-gate 			X509V3_set_nconf(&ctx, lconf);
20640Sstevel@tonic-gate 
20650Sstevel@tonic-gate 			if(!X509V3_EXT_add_nconf(lconf, &ctx, ext_sect, ret))
20660Sstevel@tonic-gate 				{
20670Sstevel@tonic-gate 				BIO_printf(bio_err, "ERROR: adding extensions in section %s\n", ext_sect);
20680Sstevel@tonic-gate 				ERR_print_errors(bio_err);
20690Sstevel@tonic-gate 				goto err;
20700Sstevel@tonic-gate 				}
20710Sstevel@tonic-gate 
20720Sstevel@tonic-gate 			if (verbose)
20730Sstevel@tonic-gate 				BIO_printf(bio_err, "Successfully added extensions from config\n");
20740Sstevel@tonic-gate 			}
20750Sstevel@tonic-gate 		}
20760Sstevel@tonic-gate 
20770Sstevel@tonic-gate 	/* Copy extensions from request (if any) */
20780Sstevel@tonic-gate 
20790Sstevel@tonic-gate 	if (!copy_extensions(ret, req, ext_copy))
20800Sstevel@tonic-gate 		{
20810Sstevel@tonic-gate 		BIO_printf(bio_err, "ERROR: adding extensions from request\n");
20820Sstevel@tonic-gate 		ERR_print_errors(bio_err);
20830Sstevel@tonic-gate 		goto err;
20840Sstevel@tonic-gate 		}
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 	/* Set the right value for the noemailDN option */
20870Sstevel@tonic-gate 	if( email_dn == 0 )
20880Sstevel@tonic-gate 		{
20890Sstevel@tonic-gate 		if (!X509_set_subject_name(ret,dn_subject)) goto err;
20900Sstevel@tonic-gate 		}
20910Sstevel@tonic-gate 
20920Sstevel@tonic-gate 	if (!default_op)
20930Sstevel@tonic-gate 		{
20940Sstevel@tonic-gate 		BIO_printf(bio_err, "Certificate Details:\n");
20950Sstevel@tonic-gate 		/* Never print signature details because signature not present */
20960Sstevel@tonic-gate 		certopt |= X509_FLAG_NO_SIGDUMP | X509_FLAG_NO_SIGNAME;
20970Sstevel@tonic-gate 		X509_print_ex(bio_err, ret, nameopt, certopt);
20980Sstevel@tonic-gate 		}
20990Sstevel@tonic-gate 
21000Sstevel@tonic-gate 	BIO_printf(bio_err,"Certificate is to be certified until ");
21010Sstevel@tonic-gate 	ASN1_UTCTIME_print(bio_err,X509_get_notAfter(ret));
2102*2139Sjp161948 	if (days) BIO_printf(bio_err," (%ld days)",days);
21030Sstevel@tonic-gate 	BIO_printf(bio_err, "\n");
21040Sstevel@tonic-gate 
21050Sstevel@tonic-gate 	if (!batch)
21060Sstevel@tonic-gate 		{
21070Sstevel@tonic-gate 
21080Sstevel@tonic-gate 		BIO_printf(bio_err,"Sign the certificate? [y/n]:");
21090Sstevel@tonic-gate 		(void)BIO_flush(bio_err);
21100Sstevel@tonic-gate 		buf[0]='\0';
21110Sstevel@tonic-gate 		fgets(buf,sizeof(buf)-1,stdin);
21120Sstevel@tonic-gate 		if (!((buf[0] == 'y') || (buf[0] == 'Y')))
21130Sstevel@tonic-gate 			{
21140Sstevel@tonic-gate 			BIO_printf(bio_err,"CERTIFICATE WILL NOT BE CERTIFIED\n");
21150Sstevel@tonic-gate 			ok=0;
21160Sstevel@tonic-gate 			goto err;
21170Sstevel@tonic-gate 			}
21180Sstevel@tonic-gate 		}
21190Sstevel@tonic-gate 
21200Sstevel@tonic-gate 
21210Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
21220Sstevel@tonic-gate 	if (pkey->type == EVP_PKEY_DSA) dgst=EVP_dss1();
21230Sstevel@tonic-gate 	pktmp=X509_get_pubkey(ret);
21240Sstevel@tonic-gate 	if (EVP_PKEY_missing_parameters(pktmp) &&
21250Sstevel@tonic-gate 		!EVP_PKEY_missing_parameters(pkey))
21260Sstevel@tonic-gate 		EVP_PKEY_copy_parameters(pktmp,pkey);
21270Sstevel@tonic-gate 	EVP_PKEY_free(pktmp);
21280Sstevel@tonic-gate #endif
2129*2139Sjp161948 #ifndef OPENSSL_NO_ECDSA
2130*2139Sjp161948 	if (pkey->type == EVP_PKEY_EC)
2131*2139Sjp161948 		dgst = EVP_ecdsa();
2132*2139Sjp161948 	pktmp = X509_get_pubkey(ret);
2133*2139Sjp161948 	if (EVP_PKEY_missing_parameters(pktmp) &&
2134*2139Sjp161948 		!EVP_PKEY_missing_parameters(pkey))
2135*2139Sjp161948 		EVP_PKEY_copy_parameters(pktmp, pkey);
2136*2139Sjp161948 	EVP_PKEY_free(pktmp);
2137*2139Sjp161948 #endif
2138*2139Sjp161948 
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate 	if (!X509_sign(ret,pkey,dgst))
21410Sstevel@tonic-gate 		goto err;
21420Sstevel@tonic-gate 
21430Sstevel@tonic-gate 	/* We now just add it to the database */
21440Sstevel@tonic-gate 	row[DB_type]=(char *)OPENSSL_malloc(2);
21450Sstevel@tonic-gate 
21460Sstevel@tonic-gate 	tm=X509_get_notAfter(ret);
21470Sstevel@tonic-gate 	row[DB_exp_date]=(char *)OPENSSL_malloc(tm->length+1);
21480Sstevel@tonic-gate 	memcpy(row[DB_exp_date],tm->data,tm->length);
21490Sstevel@tonic-gate 	row[DB_exp_date][tm->length]='\0';
21500Sstevel@tonic-gate 
21510Sstevel@tonic-gate 	row[DB_rev_date]=NULL;
21520Sstevel@tonic-gate 
21530Sstevel@tonic-gate 	/* row[DB_serial] done already */
21540Sstevel@tonic-gate 	row[DB_file]=(char *)OPENSSL_malloc(8);
21550Sstevel@tonic-gate 	row[DB_name]=X509_NAME_oneline(X509_get_subject_name(ret),NULL,0);
21560Sstevel@tonic-gate 
21570Sstevel@tonic-gate 	if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) ||
21580Sstevel@tonic-gate 		(row[DB_file] == NULL) || (row[DB_name] == NULL))
21590Sstevel@tonic-gate 		{
21600Sstevel@tonic-gate 		BIO_printf(bio_err,"Memory allocation failure\n");
21610Sstevel@tonic-gate 		goto err;
21620Sstevel@tonic-gate 		}
21630Sstevel@tonic-gate 	BUF_strlcpy(row[DB_file],"unknown",8);
21640Sstevel@tonic-gate 	row[DB_type][0]='V';
21650Sstevel@tonic-gate 	row[DB_type][1]='\0';
21660Sstevel@tonic-gate 
21670Sstevel@tonic-gate 	if ((irow=(char **)OPENSSL_malloc(sizeof(char *)*(DB_NUMBER+1))) == NULL)
21680Sstevel@tonic-gate 		{
21690Sstevel@tonic-gate 		BIO_printf(bio_err,"Memory allocation failure\n");
21700Sstevel@tonic-gate 		goto err;
21710Sstevel@tonic-gate 		}
21720Sstevel@tonic-gate 
21730Sstevel@tonic-gate 	for (i=0; i<DB_NUMBER; i++)
21740Sstevel@tonic-gate 		{
21750Sstevel@tonic-gate 		irow[i]=row[i];
21760Sstevel@tonic-gate 		row[i]=NULL;
21770Sstevel@tonic-gate 		}
21780Sstevel@tonic-gate 	irow[DB_NUMBER]=NULL;
21790Sstevel@tonic-gate 
21800Sstevel@tonic-gate 	if (!TXT_DB_insert(db->db,irow))
21810Sstevel@tonic-gate 		{
21820Sstevel@tonic-gate 		BIO_printf(bio_err,"failed to update database\n");
21830Sstevel@tonic-gate 		BIO_printf(bio_err,"TXT_DB error number %ld\n",db->db->error);
21840Sstevel@tonic-gate 		goto err;
21850Sstevel@tonic-gate 		}
21860Sstevel@tonic-gate 	ok=1;
21870Sstevel@tonic-gate err:
21880Sstevel@tonic-gate 	for (i=0; i<DB_NUMBER; i++)
21890Sstevel@tonic-gate 		if (row[i] != NULL) OPENSSL_free(row[i]);
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 	if (CAname != NULL)
21920Sstevel@tonic-gate 		X509_NAME_free(CAname);
21930Sstevel@tonic-gate 	if (subject != NULL)
21940Sstevel@tonic-gate 		X509_NAME_free(subject);
21950Sstevel@tonic-gate 	if ((dn_subject != NULL) && !email_dn)
21960Sstevel@tonic-gate 		X509_NAME_free(dn_subject);
21970Sstevel@tonic-gate 	if (tmptm != NULL)
21980Sstevel@tonic-gate 		ASN1_UTCTIME_free(tmptm);
21990Sstevel@tonic-gate 	if (ok <= 0)
22000Sstevel@tonic-gate 		{
22010Sstevel@tonic-gate 		if (ret != NULL) X509_free(ret);
22020Sstevel@tonic-gate 		ret=NULL;
22030Sstevel@tonic-gate 		}
22040Sstevel@tonic-gate 	else
22050Sstevel@tonic-gate 		*xret=ret;
22060Sstevel@tonic-gate 	return(ok);
22070Sstevel@tonic-gate 	}
22080Sstevel@tonic-gate 
write_new_certificate(BIO * bp,X509 * x,int output_der,int notext)22090Sstevel@tonic-gate static void write_new_certificate(BIO *bp, X509 *x, int output_der, int notext)
22100Sstevel@tonic-gate 	{
22110Sstevel@tonic-gate 
22120Sstevel@tonic-gate 	if (output_der)
22130Sstevel@tonic-gate 		{
22140Sstevel@tonic-gate 		(void)i2d_X509_bio(bp,x);
22150Sstevel@tonic-gate 		return;
22160Sstevel@tonic-gate 		}
22170Sstevel@tonic-gate #if 0
22180Sstevel@tonic-gate 	/* ??? Not needed since X509_print prints all this stuff anyway */
22190Sstevel@tonic-gate 	f=X509_NAME_oneline(X509_get_issuer_name(x),buf,256);
22200Sstevel@tonic-gate 	BIO_printf(bp,"issuer :%s\n",f);
22210Sstevel@tonic-gate 
22220Sstevel@tonic-gate 	f=X509_NAME_oneline(X509_get_subject_name(x),buf,256);
22230Sstevel@tonic-gate 	BIO_printf(bp,"subject:%s\n",f);
22240Sstevel@tonic-gate 
22250Sstevel@tonic-gate 	BIO_puts(bp,"serial :");
22260Sstevel@tonic-gate 	i2a_ASN1_INTEGER(bp,x->cert_info->serialNumber);
22270Sstevel@tonic-gate 	BIO_puts(bp,"\n\n");
22280Sstevel@tonic-gate #endif
22290Sstevel@tonic-gate 	if (!notext)X509_print(bp,x);
22300Sstevel@tonic-gate 	PEM_write_bio_X509(bp,x);
22310Sstevel@tonic-gate 	}
22320Sstevel@tonic-gate 
certify_spkac(X509 ** xret,char * infile,EVP_PKEY * pkey,X509 * x509,const EVP_MD * dgst,STACK_OF (CONF_VALUE)* policy,CA_DB * db,BIGNUM * serial,char * subj,unsigned long chtype,int multirdn,int email_dn,char * startdate,char * enddate,long days,char * ext_sect,CONF * lconf,int verbose,unsigned long certopt,unsigned long nameopt,int default_op,int ext_copy)22330Sstevel@tonic-gate static int certify_spkac(X509 **xret, char *infile, EVP_PKEY *pkey, X509 *x509,
22340Sstevel@tonic-gate 	     const EVP_MD *dgst, STACK_OF(CONF_VALUE) *policy, CA_DB *db,
2235*2139Sjp161948 	     BIGNUM *serial, char *subj,unsigned long chtype, int multirdn, int email_dn, char *startdate, char *enddate,
22360Sstevel@tonic-gate 	     long days, char *ext_sect, CONF *lconf, int verbose, unsigned long certopt,
22370Sstevel@tonic-gate 	     unsigned long nameopt, int default_op, int ext_copy)
22380Sstevel@tonic-gate 	{
22390Sstevel@tonic-gate 	STACK_OF(CONF_VALUE) *sk=NULL;
22400Sstevel@tonic-gate 	LHASH *parms=NULL;
22410Sstevel@tonic-gate 	X509_REQ *req=NULL;
22420Sstevel@tonic-gate 	CONF_VALUE *cv=NULL;
22430Sstevel@tonic-gate 	NETSCAPE_SPKI *spki = NULL;
22440Sstevel@tonic-gate 	X509_REQ_INFO *ri;
22450Sstevel@tonic-gate 	char *type,*buf;
22460Sstevel@tonic-gate 	EVP_PKEY *pktmp=NULL;
22470Sstevel@tonic-gate 	X509_NAME *n=NULL;
22480Sstevel@tonic-gate 	X509_NAME_ENTRY *ne=NULL;
22490Sstevel@tonic-gate 	int ok= -1,i,j;
22500Sstevel@tonic-gate 	long errline;
22510Sstevel@tonic-gate 	int nid;
22520Sstevel@tonic-gate 
22530Sstevel@tonic-gate 	/*
22540Sstevel@tonic-gate 	 * Load input file into a hash table.  (This is just an easy
22550Sstevel@tonic-gate 	 * way to read and parse the file, then put it into a convenient
22560Sstevel@tonic-gate 	 * STACK format).
22570Sstevel@tonic-gate 	 */
22580Sstevel@tonic-gate 	parms=CONF_load(NULL,infile,&errline);
22590Sstevel@tonic-gate 	if (parms == NULL)
22600Sstevel@tonic-gate 		{
22610Sstevel@tonic-gate 		BIO_printf(bio_err,"error on line %ld of %s\n",errline,infile);
22620Sstevel@tonic-gate 		ERR_print_errors(bio_err);
22630Sstevel@tonic-gate 		goto err;
22640Sstevel@tonic-gate 		}
22650Sstevel@tonic-gate 
22660Sstevel@tonic-gate 	sk=CONF_get_section(parms, "default");
22670Sstevel@tonic-gate 	if (sk_CONF_VALUE_num(sk) == 0)
22680Sstevel@tonic-gate 		{
22690Sstevel@tonic-gate 		BIO_printf(bio_err, "no name/value pairs found in %s\n", infile);
22700Sstevel@tonic-gate 		CONF_free(parms);
22710Sstevel@tonic-gate 		goto err;
22720Sstevel@tonic-gate 		}
22730Sstevel@tonic-gate 
22740Sstevel@tonic-gate 	/*
22750Sstevel@tonic-gate 	 * Now create a dummy X509 request structure.  We don't actually
22760Sstevel@tonic-gate 	 * have an X509 request, but we have many of the components
22770Sstevel@tonic-gate 	 * (a public key, various DN components).  The idea is that we
22780Sstevel@tonic-gate 	 * put these components into the right X509 request structure
22790Sstevel@tonic-gate 	 * and we can use the same code as if you had a real X509 request.
22800Sstevel@tonic-gate 	 */
22810Sstevel@tonic-gate 	req=X509_REQ_new();
22820Sstevel@tonic-gate 	if (req == NULL)
22830Sstevel@tonic-gate 		{
22840Sstevel@tonic-gate 		ERR_print_errors(bio_err);
22850Sstevel@tonic-gate 		goto err;
22860Sstevel@tonic-gate 		}
22870Sstevel@tonic-gate 
22880Sstevel@tonic-gate 	/*
22890Sstevel@tonic-gate 	 * Build up the subject name set.
22900Sstevel@tonic-gate 	 */
22910Sstevel@tonic-gate 	ri=req->req_info;
22920Sstevel@tonic-gate 	n = ri->subject;
22930Sstevel@tonic-gate 
22940Sstevel@tonic-gate 	for (i = 0; ; i++)
22950Sstevel@tonic-gate 		{
22960Sstevel@tonic-gate 		if (sk_CONF_VALUE_num(sk) <= i) break;
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate 		cv=sk_CONF_VALUE_value(sk,i);
22990Sstevel@tonic-gate 		type=cv->name;
23000Sstevel@tonic-gate 		/* Skip past any leading X. X: X, etc to allow for
23010Sstevel@tonic-gate 		 * multiple instances
23020Sstevel@tonic-gate 		 */
23030Sstevel@tonic-gate 		for (buf = cv->name; *buf ; buf++)
23040Sstevel@tonic-gate 			if ((*buf == ':') || (*buf == ',') || (*buf == '.'))
23050Sstevel@tonic-gate 				{
23060Sstevel@tonic-gate 				buf++;
23070Sstevel@tonic-gate 				if (*buf) type = buf;
23080Sstevel@tonic-gate 				break;
23090Sstevel@tonic-gate 				}
23100Sstevel@tonic-gate 
23110Sstevel@tonic-gate 		buf=cv->value;
23120Sstevel@tonic-gate 		if ((nid=OBJ_txt2nid(type)) == NID_undef)
23130Sstevel@tonic-gate 			{
23140Sstevel@tonic-gate 			if (strcmp(type, "SPKAC") == 0)
23150Sstevel@tonic-gate 				{
23160Sstevel@tonic-gate 				spki = NETSCAPE_SPKI_b64_decode(cv->value, -1);
23170Sstevel@tonic-gate 				if (spki == NULL)
23180Sstevel@tonic-gate 					{
23190Sstevel@tonic-gate 					BIO_printf(bio_err,"unable to load Netscape SPKAC structure\n");
23200Sstevel@tonic-gate 					ERR_print_errors(bio_err);
23210Sstevel@tonic-gate 					goto err;
23220Sstevel@tonic-gate 					}
23230Sstevel@tonic-gate 				}
23240Sstevel@tonic-gate 			continue;
23250Sstevel@tonic-gate 			}
23260Sstevel@tonic-gate 
23270Sstevel@tonic-gate 		/*
23280Sstevel@tonic-gate 		if ((nid == NID_pkcs9_emailAddress) && (email_dn == 0))
23290Sstevel@tonic-gate 			continue;
23300Sstevel@tonic-gate 		*/
23310Sstevel@tonic-gate 
23320Sstevel@tonic-gate 		j=ASN1_PRINTABLE_type((unsigned char *)buf,-1);
23330Sstevel@tonic-gate 		if (fix_data(nid, &j) == 0)
23340Sstevel@tonic-gate 			{
23350Sstevel@tonic-gate 			BIO_printf(bio_err,
23360Sstevel@tonic-gate 				"invalid characters in string %s\n",buf);
23370Sstevel@tonic-gate 			goto err;
23380Sstevel@tonic-gate 			}
23390Sstevel@tonic-gate 
23400Sstevel@tonic-gate 		if ((ne=X509_NAME_ENTRY_create_by_NID(&ne,nid,j,
23410Sstevel@tonic-gate 			(unsigned char *)buf,
23420Sstevel@tonic-gate 			strlen(buf))) == NULL)
23430Sstevel@tonic-gate 			goto err;
23440Sstevel@tonic-gate 
23450Sstevel@tonic-gate 		if (!X509_NAME_add_entry(n,ne,-1, 0)) goto err;
23460Sstevel@tonic-gate 		}
23470Sstevel@tonic-gate 	if (spki == NULL)
23480Sstevel@tonic-gate 		{
23490Sstevel@tonic-gate 		BIO_printf(bio_err,"Netscape SPKAC structure not found in %s\n",
23500Sstevel@tonic-gate 			infile);
23510Sstevel@tonic-gate 		goto err;
23520Sstevel@tonic-gate 		}
23530Sstevel@tonic-gate 
23540Sstevel@tonic-gate 	/*
23550Sstevel@tonic-gate 	 * Now extract the key from the SPKI structure.
23560Sstevel@tonic-gate 	 */
23570Sstevel@tonic-gate 
23580Sstevel@tonic-gate 	BIO_printf(bio_err,"Check that the SPKAC request matches the signature\n");
23590Sstevel@tonic-gate 
23600Sstevel@tonic-gate 	if ((pktmp=NETSCAPE_SPKI_get_pubkey(spki)) == NULL)
23610Sstevel@tonic-gate 		{
23620Sstevel@tonic-gate 		BIO_printf(bio_err,"error unpacking SPKAC public key\n");
23630Sstevel@tonic-gate 		goto err;
23640Sstevel@tonic-gate 		}
23650Sstevel@tonic-gate 
23660Sstevel@tonic-gate 	j = NETSCAPE_SPKI_verify(spki, pktmp);
23670Sstevel@tonic-gate 	if (j <= 0)
23680Sstevel@tonic-gate 		{
23690Sstevel@tonic-gate 		BIO_printf(bio_err,"signature verification failed on SPKAC public key\n");
23700Sstevel@tonic-gate 		goto err;
23710Sstevel@tonic-gate 		}
23720Sstevel@tonic-gate 	BIO_printf(bio_err,"Signature ok\n");
23730Sstevel@tonic-gate 
23740Sstevel@tonic-gate 	X509_REQ_set_pubkey(req,pktmp);
23750Sstevel@tonic-gate 	EVP_PKEY_free(pktmp);
2376*2139Sjp161948 	ok=do_body(xret,pkey,x509,dgst,policy,db,serial,subj,chtype,multirdn,email_dn,startdate,enddate,
23770Sstevel@tonic-gate 		   days,1,verbose,req,ext_sect,lconf, certopt, nameopt, default_op,
2378*2139Sjp161948 			ext_copy, 0);
23790Sstevel@tonic-gate err:
23800Sstevel@tonic-gate 	if (req != NULL) X509_REQ_free(req);
23810Sstevel@tonic-gate 	if (parms != NULL) CONF_free(parms);
23820Sstevel@tonic-gate 	if (spki != NULL) NETSCAPE_SPKI_free(spki);
23830Sstevel@tonic-gate 	if (ne != NULL) X509_NAME_ENTRY_free(ne);
23840Sstevel@tonic-gate 
23850Sstevel@tonic-gate 	return(ok);
23860Sstevel@tonic-gate 	}
23870Sstevel@tonic-gate 
fix_data(int nid,int * type)23880Sstevel@tonic-gate static int fix_data(int nid, int *type)
23890Sstevel@tonic-gate 	{
23900Sstevel@tonic-gate 	if (nid == NID_pkcs9_emailAddress)
23910Sstevel@tonic-gate 		*type=V_ASN1_IA5STRING;
23920Sstevel@tonic-gate 	if ((nid == NID_commonName) && (*type == V_ASN1_IA5STRING))
23930Sstevel@tonic-gate 		*type=V_ASN1_T61STRING;
23940Sstevel@tonic-gate 	if ((nid == NID_pkcs9_challengePassword) && (*type == V_ASN1_IA5STRING))
23950Sstevel@tonic-gate 		*type=V_ASN1_T61STRING;
23960Sstevel@tonic-gate 	if ((nid == NID_pkcs9_unstructuredName) && (*type == V_ASN1_T61STRING))
23970Sstevel@tonic-gate 		return(0);
23980Sstevel@tonic-gate 	if (nid == NID_pkcs9_unstructuredName)
23990Sstevel@tonic-gate 		*type=V_ASN1_IA5STRING;
24000Sstevel@tonic-gate 	return(1);
24010Sstevel@tonic-gate 	}
24020Sstevel@tonic-gate 
check_time_format(char * str)24030Sstevel@tonic-gate static int check_time_format(char *str)
24040Sstevel@tonic-gate 	{
24050Sstevel@tonic-gate 	ASN1_UTCTIME tm;
24060Sstevel@tonic-gate 
24070Sstevel@tonic-gate 	tm.data=(unsigned char *)str;
24080Sstevel@tonic-gate 	tm.length=strlen(str);
24090Sstevel@tonic-gate 	tm.type=V_ASN1_UTCTIME;
24100Sstevel@tonic-gate 	return(ASN1_UTCTIME_check(&tm));
24110Sstevel@tonic-gate 	}
24120Sstevel@tonic-gate 
do_revoke(X509 * x509,CA_DB * db,int type,char * value)24130Sstevel@tonic-gate static int do_revoke(X509 *x509, CA_DB *db, int type, char *value)
24140Sstevel@tonic-gate 	{
24150Sstevel@tonic-gate 	ASN1_UTCTIME *tm=NULL;
24160Sstevel@tonic-gate 	char *row[DB_NUMBER],**rrow,**irow;
24170Sstevel@tonic-gate 	char *rev_str = NULL;
24180Sstevel@tonic-gate 	BIGNUM *bn = NULL;
24190Sstevel@tonic-gate 	int ok=-1,i;
24200Sstevel@tonic-gate 
24210Sstevel@tonic-gate 	for (i=0; i<DB_NUMBER; i++)
24220Sstevel@tonic-gate 		row[i]=NULL;
24230Sstevel@tonic-gate 	row[DB_name]=X509_NAME_oneline(X509_get_subject_name(x509),NULL,0);
24240Sstevel@tonic-gate 	bn = ASN1_INTEGER_to_BN(X509_get_serialNumber(x509),NULL);
24250Sstevel@tonic-gate 	if (BN_is_zero(bn))
24260Sstevel@tonic-gate 		row[DB_serial]=BUF_strdup("00");
24270Sstevel@tonic-gate 	else
24280Sstevel@tonic-gate 		row[DB_serial]=BN_bn2hex(bn);
24290Sstevel@tonic-gate 	BN_free(bn);
24300Sstevel@tonic-gate 	if ((row[DB_name] == NULL) || (row[DB_serial] == NULL))
24310Sstevel@tonic-gate 		{
24320Sstevel@tonic-gate 		BIO_printf(bio_err,"Memory allocation failure\n");
24330Sstevel@tonic-gate 		goto err;
24340Sstevel@tonic-gate 		}
24350Sstevel@tonic-gate 	/* We have to lookup by serial number because name lookup
24360Sstevel@tonic-gate 	 * skips revoked certs
24370Sstevel@tonic-gate  	 */
24380Sstevel@tonic-gate 	rrow=TXT_DB_get_by_index(db->db,DB_serial,row);
24390Sstevel@tonic-gate 	if (rrow == NULL)
24400Sstevel@tonic-gate 		{
24410Sstevel@tonic-gate 		BIO_printf(bio_err,"Adding Entry with serial number %s to DB for %s\n", row[DB_serial], row[DB_name]);
24420Sstevel@tonic-gate 
24430Sstevel@tonic-gate 		/* We now just add it to the database */
24440Sstevel@tonic-gate 		row[DB_type]=(char *)OPENSSL_malloc(2);
24450Sstevel@tonic-gate 
24460Sstevel@tonic-gate 		tm=X509_get_notAfter(x509);
24470Sstevel@tonic-gate 		row[DB_exp_date]=(char *)OPENSSL_malloc(tm->length+1);
24480Sstevel@tonic-gate 		memcpy(row[DB_exp_date],tm->data,tm->length);
24490Sstevel@tonic-gate 		row[DB_exp_date][tm->length]='\0';
24500Sstevel@tonic-gate 
24510Sstevel@tonic-gate 		row[DB_rev_date]=NULL;
24520Sstevel@tonic-gate 
24530Sstevel@tonic-gate 		/* row[DB_serial] done already */
24540Sstevel@tonic-gate 		row[DB_file]=(char *)OPENSSL_malloc(8);
24550Sstevel@tonic-gate 
24560Sstevel@tonic-gate 		/* row[DB_name] done already */
24570Sstevel@tonic-gate 
24580Sstevel@tonic-gate 		if ((row[DB_type] == NULL) || (row[DB_exp_date] == NULL) ||
24590Sstevel@tonic-gate 			(row[DB_file] == NULL))
24600Sstevel@tonic-gate 			{
24610Sstevel@tonic-gate 			BIO_printf(bio_err,"Memory allocation failure\n");
24620Sstevel@tonic-gate 			goto err;
24630Sstevel@tonic-gate 			}
24640Sstevel@tonic-gate 		BUF_strlcpy(row[DB_file],"unknown",8);
24650Sstevel@tonic-gate 		row[DB_type][0]='V';
24660Sstevel@tonic-gate 		row[DB_type][1]='\0';
24670Sstevel@tonic-gate 
24680Sstevel@tonic-gate 		if ((irow=(char **)OPENSSL_malloc(sizeof(char *)*(DB_NUMBER+1))) == NULL)
24690Sstevel@tonic-gate 			{
24700Sstevel@tonic-gate 			BIO_printf(bio_err,"Memory allocation failure\n");
24710Sstevel@tonic-gate 			goto err;
24720Sstevel@tonic-gate 			}
24730Sstevel@tonic-gate 
24740Sstevel@tonic-gate 		for (i=0; i<DB_NUMBER; i++)
24750Sstevel@tonic-gate 			{
24760Sstevel@tonic-gate 			irow[i]=row[i];
24770Sstevel@tonic-gate 			row[i]=NULL;
24780Sstevel@tonic-gate 			}
24790Sstevel@tonic-gate 		irow[DB_NUMBER]=NULL;
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate 		if (!TXT_DB_insert(db->db,irow))
24820Sstevel@tonic-gate 			{
24830Sstevel@tonic-gate 			BIO_printf(bio_err,"failed to update database\n");
24840Sstevel@tonic-gate 			BIO_printf(bio_err,"TXT_DB error number %ld\n",db->db->error);
24850Sstevel@tonic-gate 			goto err;
24860Sstevel@tonic-gate 			}
24870Sstevel@tonic-gate 
24880Sstevel@tonic-gate 		/* Revoke Certificate */
24890Sstevel@tonic-gate 		ok = do_revoke(x509,db, type, value);
24900Sstevel@tonic-gate 
24910Sstevel@tonic-gate 		goto err;
24920Sstevel@tonic-gate 
24930Sstevel@tonic-gate 		}
24940Sstevel@tonic-gate 	else if (index_name_cmp((const char **)row,(const char **)rrow))
24950Sstevel@tonic-gate 		{
24960Sstevel@tonic-gate 		BIO_printf(bio_err,"ERROR:name does not match %s\n",
24970Sstevel@tonic-gate 			   row[DB_name]);
24980Sstevel@tonic-gate 		goto err;
24990Sstevel@tonic-gate 		}
25000Sstevel@tonic-gate 	else if (rrow[DB_type][0]=='R')
25010Sstevel@tonic-gate 		{
25020Sstevel@tonic-gate 		BIO_printf(bio_err,"ERROR:Already revoked, serial number %s\n",
25030Sstevel@tonic-gate 			   row[DB_serial]);
25040Sstevel@tonic-gate 		goto err;
25050Sstevel@tonic-gate 		}
25060Sstevel@tonic-gate 	else
25070Sstevel@tonic-gate 		{
25080Sstevel@tonic-gate 		BIO_printf(bio_err,"Revoking Certificate %s.\n", rrow[DB_serial]);
25090Sstevel@tonic-gate 		rev_str = make_revocation_str(type, value);
25100Sstevel@tonic-gate 		if (!rev_str)
25110Sstevel@tonic-gate 			{
25120Sstevel@tonic-gate 			BIO_printf(bio_err, "Error in revocation arguments\n");
25130Sstevel@tonic-gate 			goto err;
25140Sstevel@tonic-gate 			}
25150Sstevel@tonic-gate 		rrow[DB_type][0]='R';
25160Sstevel@tonic-gate 		rrow[DB_type][1]='\0';
25170Sstevel@tonic-gate 		rrow[DB_rev_date] = rev_str;
25180Sstevel@tonic-gate 		}
25190Sstevel@tonic-gate 	ok=1;
25200Sstevel@tonic-gate err:
25210Sstevel@tonic-gate 	for (i=0; i<DB_NUMBER; i++)
25220Sstevel@tonic-gate 		{
25230Sstevel@tonic-gate 		if (row[i] != NULL)
25240Sstevel@tonic-gate 			OPENSSL_free(row[i]);
25250Sstevel@tonic-gate 		}
25260Sstevel@tonic-gate 	return(ok);
25270Sstevel@tonic-gate 	}
25280Sstevel@tonic-gate 
get_certificate_status(const char * serial,CA_DB * db)25290Sstevel@tonic-gate static int get_certificate_status(const char *serial, CA_DB *db)
25300Sstevel@tonic-gate 	{
25310Sstevel@tonic-gate 	char *row[DB_NUMBER],**rrow;
25320Sstevel@tonic-gate 	int ok=-1,i;
25330Sstevel@tonic-gate 
25340Sstevel@tonic-gate 	/* Free Resources */
25350Sstevel@tonic-gate 	for (i=0; i<DB_NUMBER; i++)
25360Sstevel@tonic-gate 		row[i]=NULL;
25370Sstevel@tonic-gate 
25380Sstevel@tonic-gate 	/* Malloc needed char spaces */
25390Sstevel@tonic-gate 	row[DB_serial] = OPENSSL_malloc(strlen(serial) + 2);
25400Sstevel@tonic-gate 	if (row[DB_serial] == NULL)
25410Sstevel@tonic-gate 		{
25420Sstevel@tonic-gate 		BIO_printf(bio_err,"Malloc failure\n");
25430Sstevel@tonic-gate 		goto err;
25440Sstevel@tonic-gate 		}
25450Sstevel@tonic-gate 
25460Sstevel@tonic-gate 	if (strlen(serial) % 2)
25470Sstevel@tonic-gate 		{
25480Sstevel@tonic-gate 		/* Set the first char to 0 */;
25490Sstevel@tonic-gate 		row[DB_serial][0]='0';
25500Sstevel@tonic-gate 
25510Sstevel@tonic-gate 		/* Copy String from serial to row[DB_serial] */
25520Sstevel@tonic-gate 		memcpy(row[DB_serial]+1, serial, strlen(serial));
25530Sstevel@tonic-gate 		row[DB_serial][strlen(serial)+1]='\0';
25540Sstevel@tonic-gate 		}
25550Sstevel@tonic-gate 	else
25560Sstevel@tonic-gate 		{
25570Sstevel@tonic-gate 		/* Copy String from serial to row[DB_serial] */
25580Sstevel@tonic-gate 		memcpy(row[DB_serial], serial, strlen(serial));
25590Sstevel@tonic-gate 		row[DB_serial][strlen(serial)]='\0';
25600Sstevel@tonic-gate 		}
25610Sstevel@tonic-gate 
25620Sstevel@tonic-gate 	/* Make it Upper Case */
25630Sstevel@tonic-gate 	for (i=0; row[DB_serial][i] != '\0'; i++)
25640Sstevel@tonic-gate 		row[DB_serial][i] = toupper(row[DB_serial][i]);
25650Sstevel@tonic-gate 
25660Sstevel@tonic-gate 
25670Sstevel@tonic-gate 	ok=1;
25680Sstevel@tonic-gate 
25690Sstevel@tonic-gate 	/* Search for the certificate */
25700Sstevel@tonic-gate 	rrow=TXT_DB_get_by_index(db->db,DB_serial,row);
25710Sstevel@tonic-gate 	if (rrow == NULL)
25720Sstevel@tonic-gate 		{
25730Sstevel@tonic-gate 		BIO_printf(bio_err,"Serial %s not present in db.\n",
25740Sstevel@tonic-gate 				 row[DB_serial]);
25750Sstevel@tonic-gate 		ok=-1;
25760Sstevel@tonic-gate 		goto err;
25770Sstevel@tonic-gate 		}
25780Sstevel@tonic-gate 	else if (rrow[DB_type][0]=='V')
25790Sstevel@tonic-gate 		{
25800Sstevel@tonic-gate 		BIO_printf(bio_err,"%s=Valid (%c)\n",
25810Sstevel@tonic-gate 			row[DB_serial], rrow[DB_type][0]);
25820Sstevel@tonic-gate 		goto err;
25830Sstevel@tonic-gate 		}
25840Sstevel@tonic-gate 	else if (rrow[DB_type][0]=='R')
25850Sstevel@tonic-gate 		{
25860Sstevel@tonic-gate 		BIO_printf(bio_err,"%s=Revoked (%c)\n",
25870Sstevel@tonic-gate 			row[DB_serial], rrow[DB_type][0]);
25880Sstevel@tonic-gate 		goto err;
25890Sstevel@tonic-gate 		}
25900Sstevel@tonic-gate 	else if (rrow[DB_type][0]=='E')
25910Sstevel@tonic-gate 		{
25920Sstevel@tonic-gate 		BIO_printf(bio_err,"%s=Expired (%c)\n",
25930Sstevel@tonic-gate 			row[DB_serial], rrow[DB_type][0]);
25940Sstevel@tonic-gate 		goto err;
25950Sstevel@tonic-gate 		}
25960Sstevel@tonic-gate 	else if (rrow[DB_type][0]=='S')
25970Sstevel@tonic-gate 		{
25980Sstevel@tonic-gate 		BIO_printf(bio_err,"%s=Suspended (%c)\n",
25990Sstevel@tonic-gate 			row[DB_serial], rrow[DB_type][0]);
26000Sstevel@tonic-gate 		goto err;
26010Sstevel@tonic-gate 		}
26020Sstevel@tonic-gate 	else
26030Sstevel@tonic-gate 		{
26040Sstevel@tonic-gate 		BIO_printf(bio_err,"%s=Unknown (%c).\n",
26050Sstevel@tonic-gate 			row[DB_serial], rrow[DB_type][0]);
26060Sstevel@tonic-gate 		ok=-1;
26070Sstevel@tonic-gate 		}
26080Sstevel@tonic-gate err:
26090Sstevel@tonic-gate 	for (i=0; i<DB_NUMBER; i++)
26100Sstevel@tonic-gate 		{
26110Sstevel@tonic-gate 		if (row[i] != NULL)
26120Sstevel@tonic-gate 			OPENSSL_free(row[i]);
26130Sstevel@tonic-gate 		}
26140Sstevel@tonic-gate 	return(ok);
26150Sstevel@tonic-gate 	}
26160Sstevel@tonic-gate 
do_updatedb(CA_DB * db)26170Sstevel@tonic-gate static int do_updatedb (CA_DB *db)
26180Sstevel@tonic-gate 	{
26190Sstevel@tonic-gate 	ASN1_UTCTIME	*a_tm = NULL;
26200Sstevel@tonic-gate 	int i, cnt = 0;
26210Sstevel@tonic-gate 	int db_y2k, a_y2k;  /* flags = 1 if y >= 2000 */
26220Sstevel@tonic-gate 	char **rrow, *a_tm_s;
26230Sstevel@tonic-gate 
26240Sstevel@tonic-gate 	a_tm = ASN1_UTCTIME_new();
26250Sstevel@tonic-gate 
26260Sstevel@tonic-gate 	/* get actual time and make a string */
26270Sstevel@tonic-gate 	a_tm = X509_gmtime_adj(a_tm, 0);
26280Sstevel@tonic-gate 	a_tm_s = (char *) OPENSSL_malloc(a_tm->length+1);
26290Sstevel@tonic-gate 	if (a_tm_s == NULL)
26300Sstevel@tonic-gate 		{
26310Sstevel@tonic-gate 		cnt = -1;
26320Sstevel@tonic-gate 		goto err;
26330Sstevel@tonic-gate 		}
26340Sstevel@tonic-gate 
26350Sstevel@tonic-gate 	memcpy(a_tm_s, a_tm->data, a_tm->length);
26360Sstevel@tonic-gate 	a_tm_s[a_tm->length] = '\0';
26370Sstevel@tonic-gate 
26380Sstevel@tonic-gate 	if (strncmp(a_tm_s, "49", 2) <= 0)
26390Sstevel@tonic-gate 		a_y2k = 1;
26400Sstevel@tonic-gate 	else
26410Sstevel@tonic-gate 		a_y2k = 0;
26420Sstevel@tonic-gate 
26430Sstevel@tonic-gate 	for (i = 0; i < sk_num(db->db->data); i++)
26440Sstevel@tonic-gate 		{
26450Sstevel@tonic-gate 		rrow = (char **) sk_value(db->db->data, i);
26460Sstevel@tonic-gate 
26470Sstevel@tonic-gate 		if (rrow[DB_type][0] == 'V')
26480Sstevel@tonic-gate 		 	{
26490Sstevel@tonic-gate 			/* ignore entries that are not valid */
26500Sstevel@tonic-gate 			if (strncmp(rrow[DB_exp_date], "49", 2) <= 0)
26510Sstevel@tonic-gate 				db_y2k = 1;
26520Sstevel@tonic-gate 			else
26530Sstevel@tonic-gate 				db_y2k = 0;
26540Sstevel@tonic-gate 
26550Sstevel@tonic-gate 			if (db_y2k == a_y2k)
26560Sstevel@tonic-gate 				{
26570Sstevel@tonic-gate 				/* all on the same y2k side */
26580Sstevel@tonic-gate 				if (strcmp(rrow[DB_exp_date], a_tm_s) <= 0)
26590Sstevel@tonic-gate 				       	{
26600Sstevel@tonic-gate 				       	rrow[DB_type][0]  = 'E';
26610Sstevel@tonic-gate 				       	rrow[DB_type][1]  = '\0';
26620Sstevel@tonic-gate 	  				cnt++;
26630Sstevel@tonic-gate 
26640Sstevel@tonic-gate 					BIO_printf(bio_err, "%s=Expired\n",
26650Sstevel@tonic-gate 							rrow[DB_serial]);
26660Sstevel@tonic-gate 					}
26670Sstevel@tonic-gate 				}
26680Sstevel@tonic-gate 			else if (db_y2k < a_y2k)
26690Sstevel@tonic-gate 				{
26700Sstevel@tonic-gate 		  		rrow[DB_type][0]  = 'E';
26710Sstevel@tonic-gate 		  		rrow[DB_type][1]  = '\0';
26720Sstevel@tonic-gate 	  			cnt++;
26730Sstevel@tonic-gate 
26740Sstevel@tonic-gate 				BIO_printf(bio_err, "%s=Expired\n",
26750Sstevel@tonic-gate 							rrow[DB_serial]);
26760Sstevel@tonic-gate 				}
26770Sstevel@tonic-gate 
26780Sstevel@tonic-gate 			}
26790Sstevel@tonic-gate     		}
26800Sstevel@tonic-gate 
26810Sstevel@tonic-gate err:
26820Sstevel@tonic-gate 
26830Sstevel@tonic-gate 	ASN1_UTCTIME_free(a_tm);
26840Sstevel@tonic-gate 	OPENSSL_free(a_tm_s);
26850Sstevel@tonic-gate 
26860Sstevel@tonic-gate 	return (cnt);
26870Sstevel@tonic-gate 	}
26880Sstevel@tonic-gate 
2689*2139Sjp161948 static const char *crl_reasons[] = {
26900Sstevel@tonic-gate 	/* CRL reason strings */
26910Sstevel@tonic-gate 	"unspecified",
26920Sstevel@tonic-gate 	"keyCompromise",
26930Sstevel@tonic-gate 	"CACompromise",
26940Sstevel@tonic-gate 	"affiliationChanged",
26950Sstevel@tonic-gate 	"superseded",
26960Sstevel@tonic-gate 	"cessationOfOperation",
26970Sstevel@tonic-gate 	"certificateHold",
26980Sstevel@tonic-gate 	"removeFromCRL",
26990Sstevel@tonic-gate 	/* Additional pseudo reasons */
27000Sstevel@tonic-gate 	"holdInstruction",
27010Sstevel@tonic-gate 	"keyTime",
27020Sstevel@tonic-gate 	"CAkeyTime"
27030Sstevel@tonic-gate };
27040Sstevel@tonic-gate 
27050Sstevel@tonic-gate #define NUM_REASONS (sizeof(crl_reasons) / sizeof(char *))
27060Sstevel@tonic-gate 
27070Sstevel@tonic-gate /* Given revocation information convert to a DB string.
27080Sstevel@tonic-gate  * The format of the string is:
27090Sstevel@tonic-gate  * revtime[,reason,extra]. Where 'revtime' is the
27100Sstevel@tonic-gate  * revocation time (the current time). 'reason' is the
27110Sstevel@tonic-gate  * optional CRL reason and 'extra' is any additional
27120Sstevel@tonic-gate  * argument
27130Sstevel@tonic-gate  */
27140Sstevel@tonic-gate 
make_revocation_str(int rev_type,char * rev_arg)27150Sstevel@tonic-gate char *make_revocation_str(int rev_type, char *rev_arg)
27160Sstevel@tonic-gate 	{
2717*2139Sjp161948 	char *other = NULL, *str;
2718*2139Sjp161948 	const char *reason = NULL;
27190Sstevel@tonic-gate 	ASN1_OBJECT *otmp;
27200Sstevel@tonic-gate 	ASN1_UTCTIME *revtm = NULL;
27210Sstevel@tonic-gate 	int i;
27220Sstevel@tonic-gate 	switch (rev_type)
27230Sstevel@tonic-gate 		{
27240Sstevel@tonic-gate 	case REV_NONE:
27250Sstevel@tonic-gate 		break;
27260Sstevel@tonic-gate 
27270Sstevel@tonic-gate 	case REV_CRL_REASON:
27280Sstevel@tonic-gate 		for (i = 0; i < 8; i++)
27290Sstevel@tonic-gate 			{
27300Sstevel@tonic-gate 			if (!strcasecmp(rev_arg, crl_reasons[i]))
27310Sstevel@tonic-gate 				{
27320Sstevel@tonic-gate 				reason = crl_reasons[i];
27330Sstevel@tonic-gate 				break;
27340Sstevel@tonic-gate 				}
27350Sstevel@tonic-gate 			}
27360Sstevel@tonic-gate 		if (reason == NULL)
27370Sstevel@tonic-gate 			{
27380Sstevel@tonic-gate 			BIO_printf(bio_err, "Unknown CRL reason %s\n", rev_arg);
27390Sstevel@tonic-gate 			return NULL;
27400Sstevel@tonic-gate 			}
27410Sstevel@tonic-gate 		break;
27420Sstevel@tonic-gate 
27430Sstevel@tonic-gate 	case REV_HOLD:
27440Sstevel@tonic-gate 		/* Argument is an OID */
27450Sstevel@tonic-gate 
27460Sstevel@tonic-gate 		otmp = OBJ_txt2obj(rev_arg, 0);
27470Sstevel@tonic-gate 		ASN1_OBJECT_free(otmp);
27480Sstevel@tonic-gate 
27490Sstevel@tonic-gate 		if (otmp == NULL)
27500Sstevel@tonic-gate 			{
27510Sstevel@tonic-gate 			BIO_printf(bio_err, "Invalid object identifier %s\n", rev_arg);
27520Sstevel@tonic-gate 			return NULL;
27530Sstevel@tonic-gate 			}
27540Sstevel@tonic-gate 
27550Sstevel@tonic-gate 		reason = "holdInstruction";
27560Sstevel@tonic-gate 		other = rev_arg;
27570Sstevel@tonic-gate 		break;
27580Sstevel@tonic-gate 
27590Sstevel@tonic-gate 	case REV_KEY_COMPROMISE:
27600Sstevel@tonic-gate 	case REV_CA_COMPROMISE:
27610Sstevel@tonic-gate 
27620Sstevel@tonic-gate 		/* Argument is the key compromise time  */
27630Sstevel@tonic-gate 		if (!ASN1_GENERALIZEDTIME_set_string(NULL, rev_arg))
27640Sstevel@tonic-gate 			{
27650Sstevel@tonic-gate 			BIO_printf(bio_err, "Invalid time format %s. Need YYYYMMDDHHMMSSZ\n", rev_arg);
27660Sstevel@tonic-gate 			return NULL;
27670Sstevel@tonic-gate 			}
27680Sstevel@tonic-gate 		other = rev_arg;
27690Sstevel@tonic-gate 		if (rev_type == REV_KEY_COMPROMISE)
27700Sstevel@tonic-gate 			reason = "keyTime";
27710Sstevel@tonic-gate 		else
27720Sstevel@tonic-gate 			reason = "CAkeyTime";
27730Sstevel@tonic-gate 
27740Sstevel@tonic-gate 		break;
27750Sstevel@tonic-gate 
27760Sstevel@tonic-gate 		}
27770Sstevel@tonic-gate 
27780Sstevel@tonic-gate 	revtm = X509_gmtime_adj(NULL, 0);
27790Sstevel@tonic-gate 
27800Sstevel@tonic-gate 	i = revtm->length + 1;
27810Sstevel@tonic-gate 
27820Sstevel@tonic-gate 	if (reason) i += strlen(reason) + 1;
27830Sstevel@tonic-gate 	if (other) i += strlen(other) + 1;
27840Sstevel@tonic-gate 
27850Sstevel@tonic-gate 	str = OPENSSL_malloc(i);
27860Sstevel@tonic-gate 
27870Sstevel@tonic-gate 	if (!str) return NULL;
27880Sstevel@tonic-gate 
27890Sstevel@tonic-gate 	BUF_strlcpy(str, (char *)revtm->data, i);
27900Sstevel@tonic-gate 	if (reason)
27910Sstevel@tonic-gate 		{
27920Sstevel@tonic-gate 		BUF_strlcat(str, ",", i);
27930Sstevel@tonic-gate 		BUF_strlcat(str, reason, i);
27940Sstevel@tonic-gate 		}
27950Sstevel@tonic-gate 	if (other)
27960Sstevel@tonic-gate 		{
27970Sstevel@tonic-gate 		BUF_strlcat(str, ",", i);
27980Sstevel@tonic-gate 		BUF_strlcat(str, other, i);
27990Sstevel@tonic-gate 		}
28000Sstevel@tonic-gate 	ASN1_UTCTIME_free(revtm);
28010Sstevel@tonic-gate 	return str;
28020Sstevel@tonic-gate 	}
28030Sstevel@tonic-gate 
28040Sstevel@tonic-gate /* Convert revocation field to X509_REVOKED entry
28050Sstevel@tonic-gate  * return code:
28060Sstevel@tonic-gate  * 0 error
28070Sstevel@tonic-gate  * 1 OK
28080Sstevel@tonic-gate  * 2 OK and some extensions added (i.e. V2 CRL)
28090Sstevel@tonic-gate  */
28100Sstevel@tonic-gate 
28110Sstevel@tonic-gate 
make_revoked(X509_REVOKED * rev,const char * str)2812*2139Sjp161948 int make_revoked(X509_REVOKED *rev, const char *str)
28130Sstevel@tonic-gate 	{
28140Sstevel@tonic-gate 	char *tmp = NULL;
28150Sstevel@tonic-gate 	int reason_code = -1;
28160Sstevel@tonic-gate 	int i, ret = 0;
28170Sstevel@tonic-gate 	ASN1_OBJECT *hold = NULL;
28180Sstevel@tonic-gate 	ASN1_GENERALIZEDTIME *comp_time = NULL;
28190Sstevel@tonic-gate 	ASN1_ENUMERATED *rtmp = NULL;
28200Sstevel@tonic-gate 
28210Sstevel@tonic-gate 	ASN1_TIME *revDate = NULL;
28220Sstevel@tonic-gate 
28230Sstevel@tonic-gate 	i = unpack_revinfo(&revDate, &reason_code, &hold, &comp_time, str);
28240Sstevel@tonic-gate 
28250Sstevel@tonic-gate 	if (i == 0)
28260Sstevel@tonic-gate 		goto err;
28270Sstevel@tonic-gate 
28280Sstevel@tonic-gate 	if (rev && !X509_REVOKED_set_revocationDate(rev, revDate))
28290Sstevel@tonic-gate 		goto err;
28300Sstevel@tonic-gate 
28310Sstevel@tonic-gate 	if (rev && (reason_code != OCSP_REVOKED_STATUS_NOSTATUS))
28320Sstevel@tonic-gate 		{
28330Sstevel@tonic-gate 		rtmp = ASN1_ENUMERATED_new();
28340Sstevel@tonic-gate 		if (!rtmp || !ASN1_ENUMERATED_set(rtmp, reason_code))
28350Sstevel@tonic-gate 			goto err;
28360Sstevel@tonic-gate 		if (!X509_REVOKED_add1_ext_i2d(rev, NID_crl_reason, rtmp, 0, 0))
28370Sstevel@tonic-gate 			goto err;
28380Sstevel@tonic-gate 		}
28390Sstevel@tonic-gate 
28400Sstevel@tonic-gate 	if (rev && comp_time)
28410Sstevel@tonic-gate 		{
28420Sstevel@tonic-gate 		if (!X509_REVOKED_add1_ext_i2d(rev, NID_invalidity_date, comp_time, 0, 0))
28430Sstevel@tonic-gate 			goto err;
28440Sstevel@tonic-gate 		}
28450Sstevel@tonic-gate 	if (rev && hold)
28460Sstevel@tonic-gate 		{
28470Sstevel@tonic-gate 		if (!X509_REVOKED_add1_ext_i2d(rev, NID_hold_instruction_code, hold, 0, 0))
28480Sstevel@tonic-gate 			goto err;
28490Sstevel@tonic-gate 		}
28500Sstevel@tonic-gate 
28510Sstevel@tonic-gate 	if (reason_code != OCSP_REVOKED_STATUS_NOSTATUS)
28520Sstevel@tonic-gate 		ret = 2;
28530Sstevel@tonic-gate 	else ret = 1;
28540Sstevel@tonic-gate 
28550Sstevel@tonic-gate 	err:
28560Sstevel@tonic-gate 
28570Sstevel@tonic-gate 	if (tmp) OPENSSL_free(tmp);
28580Sstevel@tonic-gate 	ASN1_OBJECT_free(hold);
28590Sstevel@tonic-gate 	ASN1_GENERALIZEDTIME_free(comp_time);
28600Sstevel@tonic-gate 	ASN1_ENUMERATED_free(rtmp);
28610Sstevel@tonic-gate 	ASN1_TIME_free(revDate);
28620Sstevel@tonic-gate 
28630Sstevel@tonic-gate 	return ret;
28640Sstevel@tonic-gate 	}
28650Sstevel@tonic-gate 
old_entry_print(BIO * bp,ASN1_OBJECT * obj,ASN1_STRING * str)28660Sstevel@tonic-gate int old_entry_print(BIO *bp, ASN1_OBJECT *obj, ASN1_STRING *str)
28670Sstevel@tonic-gate 	{
28680Sstevel@tonic-gate 	char buf[25],*pbuf, *p;
28690Sstevel@tonic-gate 	int j;
28700Sstevel@tonic-gate 	j=i2a_ASN1_OBJECT(bp,obj);
28710Sstevel@tonic-gate 	pbuf=buf;
28720Sstevel@tonic-gate 	for (j=22-j; j>0; j--)
28730Sstevel@tonic-gate 		*(pbuf++)=' ';
28740Sstevel@tonic-gate 	*(pbuf++)=':';
28750Sstevel@tonic-gate 	*(pbuf++)='\0';
28760Sstevel@tonic-gate 	BIO_puts(bp,buf);
28770Sstevel@tonic-gate 
28780Sstevel@tonic-gate 	if (str->type == V_ASN1_PRINTABLESTRING)
28790Sstevel@tonic-gate 		BIO_printf(bp,"PRINTABLE:'");
28800Sstevel@tonic-gate 	else if (str->type == V_ASN1_T61STRING)
28810Sstevel@tonic-gate 		BIO_printf(bp,"T61STRING:'");
28820Sstevel@tonic-gate 	else if (str->type == V_ASN1_IA5STRING)
28830Sstevel@tonic-gate 		BIO_printf(bp,"IA5STRING:'");
28840Sstevel@tonic-gate 	else if (str->type == V_ASN1_UNIVERSALSTRING)
28850Sstevel@tonic-gate 		BIO_printf(bp,"UNIVERSALSTRING:'");
28860Sstevel@tonic-gate 	else
28870Sstevel@tonic-gate 		BIO_printf(bp,"ASN.1 %2d:'",str->type);
28880Sstevel@tonic-gate 
28890Sstevel@tonic-gate 	p=(char *)str->data;
28900Sstevel@tonic-gate 	for (j=str->length; j>0; j--)
28910Sstevel@tonic-gate 		{
28920Sstevel@tonic-gate 		if ((*p >= ' ') && (*p <= '~'))
28930Sstevel@tonic-gate 			BIO_printf(bp,"%c",*p);
28940Sstevel@tonic-gate 		else if (*p & 0x80)
28950Sstevel@tonic-gate 			BIO_printf(bp,"\\0x%02X",*p);
28960Sstevel@tonic-gate 		else if ((unsigned char)*p == 0xf7)
28970Sstevel@tonic-gate 			BIO_printf(bp,"^?");
28980Sstevel@tonic-gate 		else	BIO_printf(bp,"^%c",*p+'@');
28990Sstevel@tonic-gate 		p++;
29000Sstevel@tonic-gate 		}
29010Sstevel@tonic-gate 	BIO_printf(bp,"'\n");
29020Sstevel@tonic-gate 	return 1;
29030Sstevel@tonic-gate 	}
29040Sstevel@tonic-gate 
unpack_revinfo(ASN1_TIME ** prevtm,int * preason,ASN1_OBJECT ** phold,ASN1_GENERALIZEDTIME ** pinvtm,const char * str)2905*2139Sjp161948 int unpack_revinfo(ASN1_TIME **prevtm, int *preason, ASN1_OBJECT **phold, ASN1_GENERALIZEDTIME **pinvtm, const char *str)
29060Sstevel@tonic-gate 	{
29070Sstevel@tonic-gate 	char *tmp = NULL;
29080Sstevel@tonic-gate 	char *rtime_str, *reason_str = NULL, *arg_str = NULL, *p;
29090Sstevel@tonic-gate 	int reason_code = -1;
2910*2139Sjp161948 	int ret = 0;
2911*2139Sjp161948 	unsigned int i;
29120Sstevel@tonic-gate 	ASN1_OBJECT *hold = NULL;
29130Sstevel@tonic-gate 	ASN1_GENERALIZEDTIME *comp_time = NULL;
29140Sstevel@tonic-gate 	tmp = BUF_strdup(str);
29150Sstevel@tonic-gate 
29160Sstevel@tonic-gate 	p = strchr(tmp, ',');
29170Sstevel@tonic-gate 
29180Sstevel@tonic-gate 	rtime_str = tmp;
29190Sstevel@tonic-gate 
29200Sstevel@tonic-gate 	if (p)
29210Sstevel@tonic-gate 		{
29220Sstevel@tonic-gate 		*p = '\0';
29230Sstevel@tonic-gate 		p++;
29240Sstevel@tonic-gate 		reason_str = p;
29250Sstevel@tonic-gate 		p = strchr(p, ',');
29260Sstevel@tonic-gate 		if (p)
29270Sstevel@tonic-gate 			{
29280Sstevel@tonic-gate 			*p = '\0';
29290Sstevel@tonic-gate 			arg_str = p + 1;
29300Sstevel@tonic-gate 			}
29310Sstevel@tonic-gate 		}
29320Sstevel@tonic-gate 
29330Sstevel@tonic-gate 	if (prevtm)
29340Sstevel@tonic-gate 		{
29350Sstevel@tonic-gate 		*prevtm = ASN1_UTCTIME_new();
29360Sstevel@tonic-gate 		if (!ASN1_UTCTIME_set_string(*prevtm, rtime_str))
29370Sstevel@tonic-gate 			{
29380Sstevel@tonic-gate 			BIO_printf(bio_err, "invalid revocation date %s\n", rtime_str);
29390Sstevel@tonic-gate 			goto err;
29400Sstevel@tonic-gate 			}
29410Sstevel@tonic-gate 		}
29420Sstevel@tonic-gate 	if (reason_str)
29430Sstevel@tonic-gate 		{
29440Sstevel@tonic-gate 		for (i = 0; i < NUM_REASONS; i++)
29450Sstevel@tonic-gate 			{
29460Sstevel@tonic-gate 			if(!strcasecmp(reason_str, crl_reasons[i]))
29470Sstevel@tonic-gate 				{
29480Sstevel@tonic-gate 				reason_code = i;
29490Sstevel@tonic-gate 				break;
29500Sstevel@tonic-gate 				}
29510Sstevel@tonic-gate 			}
29520Sstevel@tonic-gate 		if (reason_code == OCSP_REVOKED_STATUS_NOSTATUS)
29530Sstevel@tonic-gate 			{
29540Sstevel@tonic-gate 			BIO_printf(bio_err, "invalid reason code %s\n", reason_str);
29550Sstevel@tonic-gate 			goto err;
29560Sstevel@tonic-gate 			}
29570Sstevel@tonic-gate 
29580Sstevel@tonic-gate 		if (reason_code == 7)
29590Sstevel@tonic-gate 			reason_code = OCSP_REVOKED_STATUS_REMOVEFROMCRL;
29600Sstevel@tonic-gate 		else if (reason_code == 8)		/* Hold instruction */
29610Sstevel@tonic-gate 			{
29620Sstevel@tonic-gate 			if (!arg_str)
29630Sstevel@tonic-gate 				{
29640Sstevel@tonic-gate 				BIO_printf(bio_err, "missing hold instruction\n");
29650Sstevel@tonic-gate 				goto err;
29660Sstevel@tonic-gate 				}
29670Sstevel@tonic-gate 			reason_code = OCSP_REVOKED_STATUS_CERTIFICATEHOLD;
29680Sstevel@tonic-gate 			hold = OBJ_txt2obj(arg_str, 0);
29690Sstevel@tonic-gate 
29700Sstevel@tonic-gate 			if (!hold)
29710Sstevel@tonic-gate 				{
29720Sstevel@tonic-gate 				BIO_printf(bio_err, "invalid object identifier %s\n", arg_str);
29730Sstevel@tonic-gate 				goto err;
29740Sstevel@tonic-gate 				}
29750Sstevel@tonic-gate 			if (phold) *phold = hold;
29760Sstevel@tonic-gate 			}
29770Sstevel@tonic-gate 		else if ((reason_code == 9) || (reason_code == 10))
29780Sstevel@tonic-gate 			{
29790Sstevel@tonic-gate 			if (!arg_str)
29800Sstevel@tonic-gate 				{
29810Sstevel@tonic-gate 				BIO_printf(bio_err, "missing compromised time\n");
29820Sstevel@tonic-gate 				goto err;
29830Sstevel@tonic-gate 				}
29840Sstevel@tonic-gate 			comp_time = ASN1_GENERALIZEDTIME_new();
29850Sstevel@tonic-gate 			if (!ASN1_GENERALIZEDTIME_set_string(comp_time, arg_str))
29860Sstevel@tonic-gate 				{
29870Sstevel@tonic-gate 				BIO_printf(bio_err, "invalid compromised time %s\n", arg_str);
29880Sstevel@tonic-gate 				goto err;
29890Sstevel@tonic-gate 				}
29900Sstevel@tonic-gate 			if (reason_code == 9)
29910Sstevel@tonic-gate 				reason_code = OCSP_REVOKED_STATUS_KEYCOMPROMISE;
29920Sstevel@tonic-gate 			else
29930Sstevel@tonic-gate 				reason_code = OCSP_REVOKED_STATUS_CACOMPROMISE;
29940Sstevel@tonic-gate 			}
29950Sstevel@tonic-gate 		}
29960Sstevel@tonic-gate 
29970Sstevel@tonic-gate 	if (preason) *preason = reason_code;
29980Sstevel@tonic-gate 	if (pinvtm) *pinvtm = comp_time;
29990Sstevel@tonic-gate 	else ASN1_GENERALIZEDTIME_free(comp_time);
30000Sstevel@tonic-gate 
30010Sstevel@tonic-gate 	ret = 1;
30020Sstevel@tonic-gate 
30030Sstevel@tonic-gate 	err:
30040Sstevel@tonic-gate 
30050Sstevel@tonic-gate 	if (tmp) OPENSSL_free(tmp);
30060Sstevel@tonic-gate 	if (!phold) ASN1_OBJECT_free(hold);
30070Sstevel@tonic-gate 	if (!pinvtm) ASN1_GENERALIZEDTIME_free(comp_time);
30080Sstevel@tonic-gate 
30090Sstevel@tonic-gate 	return ret;
30100Sstevel@tonic-gate 	}
3011