xref: /onnv-gate/usr/src/common/openssl/apps/apps.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* apps/apps.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 /* ====================================================================
590Sstevel@tonic-gate  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
620Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
630Sstevel@tonic-gate  * are met:
640Sstevel@tonic-gate  *
650Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
660Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
690Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
700Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
710Sstevel@tonic-gate  *    distribution.
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
740Sstevel@tonic-gate  *    software must display the following acknowledgment:
750Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
760Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
790Sstevel@tonic-gate  *    endorse or promote products derived from this software without
800Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
810Sstevel@tonic-gate  *    openssl-core@openssl.org.
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
840Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
850Sstevel@tonic-gate  *    permission of the OpenSSL Project.
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
880Sstevel@tonic-gate  *    acknowledgment:
890Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
900Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
930Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
940Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
950Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
960Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
970Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
980Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
990Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1000Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1010Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1020Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1030Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
1040Sstevel@tonic-gate  * ====================================================================
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
1070Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
1080Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
1090Sstevel@tonic-gate  *
1100Sstevel@tonic-gate  */
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate #include <stdio.h>
1130Sstevel@tonic-gate #include <stdlib.h>
1140Sstevel@tonic-gate #include <string.h>
1150Sstevel@tonic-gate #include <sys/types.h>
1160Sstevel@tonic-gate #include <sys/stat.h>
1170Sstevel@tonic-gate #include <ctype.h>
1180Sstevel@tonic-gate #include <openssl/err.h>
1190Sstevel@tonic-gate #include <openssl/x509.h>
1200Sstevel@tonic-gate #include <openssl/x509v3.h>
1210Sstevel@tonic-gate #include <openssl/pem.h>
1220Sstevel@tonic-gate #include <openssl/pkcs12.h>
1230Sstevel@tonic-gate #include <openssl/ui.h>
1240Sstevel@tonic-gate #include <openssl/safestack.h>
1250Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1260Sstevel@tonic-gate #include <openssl/engine.h>
1270Sstevel@tonic-gate #endif
128*2139Sjp161948 #ifndef OPENSSL_NO_RSA
129*2139Sjp161948 #include <openssl/rsa.h>
1300Sstevel@tonic-gate #endif
131*2139Sjp161948 #include <openssl/bn.h>
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate #define NON_MAIN
1340Sstevel@tonic-gate #include "apps.h"
1350Sstevel@tonic-gate #undef NON_MAIN
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate typedef struct {
138*2139Sjp161948 	const char *name;
1390Sstevel@tonic-gate 	unsigned long flag;
1400Sstevel@tonic-gate 	unsigned long mask;
1410Sstevel@tonic-gate } NAME_EX_TBL;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate static UI_METHOD *ui_method = NULL;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate static int set_table_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl);
1460Sstevel@tonic-gate static int set_multi_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
1490Sstevel@tonic-gate /* Looks like this stuff is worth moving into separate function */
1500Sstevel@tonic-gate static EVP_PKEY *
1510Sstevel@tonic-gate load_netscape_key(BIO *err, BIO *key, const char *file,
1520Sstevel@tonic-gate 		const char *key_descrip, int format);
1530Sstevel@tonic-gate #endif
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate int app_init(long mesgwin);
1560Sstevel@tonic-gate #ifdef undef /* never finished - probably never will be :-) */
args_from_file(char * file,int * argc,char ** argv[])1570Sstevel@tonic-gate int args_from_file(char *file, int *argc, char **argv[])
1580Sstevel@tonic-gate 	{
1590Sstevel@tonic-gate 	FILE *fp;
1600Sstevel@tonic-gate 	int num,i;
1610Sstevel@tonic-gate 	unsigned int len;
1620Sstevel@tonic-gate 	static char *buf=NULL;
1630Sstevel@tonic-gate 	static char **arg=NULL;
1640Sstevel@tonic-gate 	char *p;
1650Sstevel@tonic-gate 	struct stat stbuf;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if (stat(file,&stbuf) < 0) return(0);
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	fp=fopen(file,"r");
1700Sstevel@tonic-gate 	if (fp == NULL)
1710Sstevel@tonic-gate 		return(0);
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	*argc=0;
1740Sstevel@tonic-gate 	*argv=NULL;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	len=(unsigned int)stbuf.st_size;
1770Sstevel@tonic-gate 	if (buf != NULL) OPENSSL_free(buf);
1780Sstevel@tonic-gate 	buf=(char *)OPENSSL_malloc(len+1);
1790Sstevel@tonic-gate 	if (buf == NULL) return(0);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	len=fread(buf,1,len,fp);
1820Sstevel@tonic-gate 	if (len <= 1) return(0);
1830Sstevel@tonic-gate 	buf[len]='\0';
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	i=0;
1860Sstevel@tonic-gate 	for (p=buf; *p; p++)
1870Sstevel@tonic-gate 		if (*p == '\n') i++;
1880Sstevel@tonic-gate 	if (arg != NULL) OPENSSL_free(arg);
1890Sstevel@tonic-gate 	arg=(char **)OPENSSL_malloc(sizeof(char *)*(i*2));
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	*argv=arg;
1920Sstevel@tonic-gate 	num=0;
1930Sstevel@tonic-gate 	p=buf;
1940Sstevel@tonic-gate 	for (;;)
1950Sstevel@tonic-gate 		{
1960Sstevel@tonic-gate 		if (!*p) break;
1970Sstevel@tonic-gate 		if (*p == '#') /* comment line */
1980Sstevel@tonic-gate 			{
1990Sstevel@tonic-gate 			while (*p && (*p != '\n')) p++;
2000Sstevel@tonic-gate 			continue;
2010Sstevel@tonic-gate 			}
2020Sstevel@tonic-gate 		/* else we have a line */
2030Sstevel@tonic-gate 		*(arg++)=p;
2040Sstevel@tonic-gate 		num++;
2050Sstevel@tonic-gate 		while (*p && ((*p != ' ') && (*p != '\t') && (*p != '\n')))
2060Sstevel@tonic-gate 			p++;
2070Sstevel@tonic-gate 		if (!*p) break;
2080Sstevel@tonic-gate 		if (*p == '\n')
2090Sstevel@tonic-gate 			{
2100Sstevel@tonic-gate 			*(p++)='\0';
2110Sstevel@tonic-gate 			continue;
2120Sstevel@tonic-gate 			}
2130Sstevel@tonic-gate 		/* else it is a tab or space */
2140Sstevel@tonic-gate 		p++;
2150Sstevel@tonic-gate 		while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n')))
2160Sstevel@tonic-gate 			p++;
2170Sstevel@tonic-gate 		if (!*p) break;
2180Sstevel@tonic-gate 		if (*p == '\n')
2190Sstevel@tonic-gate 			{
2200Sstevel@tonic-gate 			p++;
2210Sstevel@tonic-gate 			continue;
2220Sstevel@tonic-gate 			}
2230Sstevel@tonic-gate 		*(arg++)=p++;
2240Sstevel@tonic-gate 		num++;
2250Sstevel@tonic-gate 		while (*p && (*p != '\n')) p++;
2260Sstevel@tonic-gate 		if (!*p) break;
2270Sstevel@tonic-gate 		/* else *p == '\n' */
2280Sstevel@tonic-gate 		*(p++)='\0';
2290Sstevel@tonic-gate 		}
2300Sstevel@tonic-gate 	*argc=num;
2310Sstevel@tonic-gate 	return(1);
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate #endif
2340Sstevel@tonic-gate 
str2fmt(char * s)2350Sstevel@tonic-gate int str2fmt(char *s)
2360Sstevel@tonic-gate 	{
2370Sstevel@tonic-gate 	if 	((*s == 'D') || (*s == 'd'))
2380Sstevel@tonic-gate 		return(FORMAT_ASN1);
2390Sstevel@tonic-gate 	else if ((*s == 'T') || (*s == 't'))
2400Sstevel@tonic-gate 		return(FORMAT_TEXT);
2410Sstevel@tonic-gate 	else if ((*s == 'P') || (*s == 'p'))
2420Sstevel@tonic-gate 		return(FORMAT_PEM);
2430Sstevel@tonic-gate 	else if ((*s == 'N') || (*s == 'n'))
2440Sstevel@tonic-gate 		return(FORMAT_NETSCAPE);
2450Sstevel@tonic-gate 	else if ((*s == 'S') || (*s == 's'))
2460Sstevel@tonic-gate 		return(FORMAT_SMIME);
2470Sstevel@tonic-gate 	else if ((*s == '1')
2480Sstevel@tonic-gate 		|| (strcmp(s,"PKCS12") == 0) || (strcmp(s,"pkcs12") == 0)
2490Sstevel@tonic-gate 		|| (strcmp(s,"P12") == 0) || (strcmp(s,"p12") == 0))
2500Sstevel@tonic-gate 		return(FORMAT_PKCS12);
2510Sstevel@tonic-gate 	else if ((*s == 'E') || (*s == 'e'))
2520Sstevel@tonic-gate 		return(FORMAT_ENGINE);
2530Sstevel@tonic-gate 	else
2540Sstevel@tonic-gate 		return(FORMAT_UNDEF);
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate 
257*2139Sjp161948 #if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_WIN16) || defined(OPENSSL_SYS_NETWARE)
program_name(char * in,char * out,int size)2580Sstevel@tonic-gate void program_name(char *in, char *out, int size)
2590Sstevel@tonic-gate 	{
2600Sstevel@tonic-gate 	int i,n;
2610Sstevel@tonic-gate 	char *p=NULL;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	n=strlen(in);
2640Sstevel@tonic-gate 	/* find the last '/', '\' or ':' */
2650Sstevel@tonic-gate 	for (i=n-1; i>0; i--)
2660Sstevel@tonic-gate 		{
2670Sstevel@tonic-gate 		if ((in[i] == '/') || (in[i] == '\\') || (in[i] == ':'))
2680Sstevel@tonic-gate 			{
2690Sstevel@tonic-gate 			p= &(in[i+1]);
2700Sstevel@tonic-gate 			break;
2710Sstevel@tonic-gate 			}
2720Sstevel@tonic-gate 		}
2730Sstevel@tonic-gate 	if (p == NULL)
2740Sstevel@tonic-gate 		p=in;
2750Sstevel@tonic-gate 	n=strlen(p);
276*2139Sjp161948 
277*2139Sjp161948 #if defined(OPENSSL_SYS_NETWARE)
278*2139Sjp161948    /* strip off trailing .nlm if present. */
279*2139Sjp161948    if ((n > 4) && (p[n-4] == '.') &&
280*2139Sjp161948       ((p[n-3] == 'n') || (p[n-3] == 'N')) &&
281*2139Sjp161948       ((p[n-2] == 'l') || (p[n-2] == 'L')) &&
282*2139Sjp161948       ((p[n-1] == 'm') || (p[n-1] == 'M')))
283*2139Sjp161948       n-=4;
284*2139Sjp161948 #else
2850Sstevel@tonic-gate 	/* strip off trailing .exe if present. */
2860Sstevel@tonic-gate 	if ((n > 4) && (p[n-4] == '.') &&
2870Sstevel@tonic-gate 		((p[n-3] == 'e') || (p[n-3] == 'E')) &&
2880Sstevel@tonic-gate 		((p[n-2] == 'x') || (p[n-2] == 'X')) &&
2890Sstevel@tonic-gate 		((p[n-1] == 'e') || (p[n-1] == 'E')))
2900Sstevel@tonic-gate 		n-=4;
291*2139Sjp161948 #endif
292*2139Sjp161948 
2930Sstevel@tonic-gate 	if (n > size-1)
2940Sstevel@tonic-gate 		n=size-1;
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	for (i=0; i<n; i++)
2970Sstevel@tonic-gate 		{
2980Sstevel@tonic-gate 		if ((p[i] >= 'A') && (p[i] <= 'Z'))
2990Sstevel@tonic-gate 			out[i]=p[i]-'A'+'a';
3000Sstevel@tonic-gate 		else
3010Sstevel@tonic-gate 			out[i]=p[i];
3020Sstevel@tonic-gate 		}
3030Sstevel@tonic-gate 	out[n]='\0';
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate #else
3060Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
program_name(char * in,char * out,int size)3070Sstevel@tonic-gate void program_name(char *in, char *out, int size)
3080Sstevel@tonic-gate 	{
3090Sstevel@tonic-gate 	char *p=in, *q;
3100Sstevel@tonic-gate 	char *chars=":]>";
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	while(*chars != '\0')
3130Sstevel@tonic-gate 		{
3140Sstevel@tonic-gate 		q=strrchr(p,*chars);
3150Sstevel@tonic-gate 		if (q > p)
3160Sstevel@tonic-gate 			p = q + 1;
3170Sstevel@tonic-gate 		chars++;
3180Sstevel@tonic-gate 		}
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	q=strrchr(p,'.');
3210Sstevel@tonic-gate 	if (q == NULL)
3220Sstevel@tonic-gate 		q = p + strlen(p);
3230Sstevel@tonic-gate 	strncpy(out,p,size-1);
3240Sstevel@tonic-gate 	if (q-p >= size)
3250Sstevel@tonic-gate 		{
3260Sstevel@tonic-gate 		out[size-1]='\0';
3270Sstevel@tonic-gate 		}
3280Sstevel@tonic-gate 	else
3290Sstevel@tonic-gate 		{
3300Sstevel@tonic-gate 		out[q-p]='\0';
3310Sstevel@tonic-gate 		}
3320Sstevel@tonic-gate 	}
3330Sstevel@tonic-gate #else
program_name(char * in,char * out,int size)3340Sstevel@tonic-gate void program_name(char *in, char *out, int size)
3350Sstevel@tonic-gate 	{
3360Sstevel@tonic-gate 	char *p;
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	p=strrchr(in,'/');
3390Sstevel@tonic-gate 	if (p != NULL)
3400Sstevel@tonic-gate 		p++;
3410Sstevel@tonic-gate 	else
3420Sstevel@tonic-gate 		p=in;
3430Sstevel@tonic-gate 	BUF_strlcpy(out,p,size);
3440Sstevel@tonic-gate 	}
3450Sstevel@tonic-gate #endif
3460Sstevel@tonic-gate #endif
3470Sstevel@tonic-gate 
chopup_args(ARGS * arg,char * buf,int * argc,char ** argv[])3480Sstevel@tonic-gate int chopup_args(ARGS *arg, char *buf, int *argc, char **argv[])
3490Sstevel@tonic-gate 	{
3500Sstevel@tonic-gate 	int num,len,i;
3510Sstevel@tonic-gate 	char *p;
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 	*argc=0;
3540Sstevel@tonic-gate 	*argv=NULL;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	len=strlen(buf);
3570Sstevel@tonic-gate 	i=0;
3580Sstevel@tonic-gate 	if (arg->count == 0)
3590Sstevel@tonic-gate 		{
3600Sstevel@tonic-gate 		arg->count=20;
3610Sstevel@tonic-gate 		arg->data=(char **)OPENSSL_malloc(sizeof(char *)*arg->count);
3620Sstevel@tonic-gate 		}
3630Sstevel@tonic-gate 	for (i=0; i<arg->count; i++)
3640Sstevel@tonic-gate 		arg->data[i]=NULL;
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	num=0;
3670Sstevel@tonic-gate 	p=buf;
3680Sstevel@tonic-gate 	for (;;)
3690Sstevel@tonic-gate 		{
3700Sstevel@tonic-gate 		/* first scan over white space */
3710Sstevel@tonic-gate 		if (!*p) break;
3720Sstevel@tonic-gate 		while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n')))
3730Sstevel@tonic-gate 			p++;
3740Sstevel@tonic-gate 		if (!*p) break;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 		/* The start of something good :-) */
3770Sstevel@tonic-gate 		if (num >= arg->count)
3780Sstevel@tonic-gate 			{
379*2139Sjp161948 			char **tmp_p;
380*2139Sjp161948 			int tlen = arg->count + 20;
381*2139Sjp161948 			tmp_p = (char **)OPENSSL_realloc(arg->data,
382*2139Sjp161948 				sizeof(char *)*tlen);
383*2139Sjp161948 			if (tmp_p == NULL)
384*2139Sjp161948 				return 0;
385*2139Sjp161948 			arg->data  = tmp_p;
386*2139Sjp161948 			arg->count = tlen;
387*2139Sjp161948 			/* initialize newly allocated data */
388*2139Sjp161948 			for (i = num; i < arg->count; i++)
389*2139Sjp161948 				arg->data[i] = NULL;
3900Sstevel@tonic-gate 			}
3910Sstevel@tonic-gate 		arg->data[num++]=p;
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 		/* now look for the end of this */
3940Sstevel@tonic-gate 		if ((*p == '\'') || (*p == '\"')) /* scan for closing quote */
3950Sstevel@tonic-gate 			{
3960Sstevel@tonic-gate 			i= *(p++);
3970Sstevel@tonic-gate 			arg->data[num-1]++; /* jump over quote */
3980Sstevel@tonic-gate 			while (*p && (*p != i))
3990Sstevel@tonic-gate 				p++;
4000Sstevel@tonic-gate 			*p='\0';
4010Sstevel@tonic-gate 			}
4020Sstevel@tonic-gate 		else
4030Sstevel@tonic-gate 			{
4040Sstevel@tonic-gate 			while (*p && ((*p != ' ') &&
4050Sstevel@tonic-gate 				(*p != '\t') && (*p != '\n')))
4060Sstevel@tonic-gate 				p++;
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 			if (*p == '\0')
4090Sstevel@tonic-gate 				p--;
4100Sstevel@tonic-gate 			else
4110Sstevel@tonic-gate 				*p='\0';
4120Sstevel@tonic-gate 			}
4130Sstevel@tonic-gate 		p++;
4140Sstevel@tonic-gate 		}
4150Sstevel@tonic-gate 	*argc=num;
4160Sstevel@tonic-gate 	*argv=arg->data;
4170Sstevel@tonic-gate 	return(1);
4180Sstevel@tonic-gate 	}
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate #ifndef APP_INIT
app_init(long mesgwin)4210Sstevel@tonic-gate int app_init(long mesgwin)
4220Sstevel@tonic-gate 	{
4230Sstevel@tonic-gate 	return(1);
4240Sstevel@tonic-gate 	}
4250Sstevel@tonic-gate #endif
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 
dump_cert_text(BIO * out,X509 * x)4280Sstevel@tonic-gate int dump_cert_text (BIO *out, X509 *x)
4290Sstevel@tonic-gate {
4300Sstevel@tonic-gate 	char *p;
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	p=X509_NAME_oneline(X509_get_subject_name(x),NULL,0);
4330Sstevel@tonic-gate 	BIO_puts(out,"subject=");
4340Sstevel@tonic-gate 	BIO_puts(out,p);
4350Sstevel@tonic-gate 	OPENSSL_free(p);
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	p=X509_NAME_oneline(X509_get_issuer_name(x),NULL,0);
4380Sstevel@tonic-gate 	BIO_puts(out,"\nissuer=");
4390Sstevel@tonic-gate 	BIO_puts(out,p);
4400Sstevel@tonic-gate 	BIO_puts(out,"\n");
4410Sstevel@tonic-gate 	OPENSSL_free(p);
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	return 0;
4440Sstevel@tonic-gate }
4450Sstevel@tonic-gate 
ui_open(UI * ui)4460Sstevel@tonic-gate static int ui_open(UI *ui)
4470Sstevel@tonic-gate 	{
4480Sstevel@tonic-gate 	return UI_method_get_opener(UI_OpenSSL())(ui);
4490Sstevel@tonic-gate 	}
ui_read(UI * ui,UI_STRING * uis)4500Sstevel@tonic-gate static int ui_read(UI *ui, UI_STRING *uis)
4510Sstevel@tonic-gate 	{
4520Sstevel@tonic-gate 	if (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD
4530Sstevel@tonic-gate 		&& UI_get0_user_data(ui))
4540Sstevel@tonic-gate 		{
4550Sstevel@tonic-gate 		switch(UI_get_string_type(uis))
4560Sstevel@tonic-gate 			{
4570Sstevel@tonic-gate 		case UIT_PROMPT:
4580Sstevel@tonic-gate 		case UIT_VERIFY:
4590Sstevel@tonic-gate 			{
4600Sstevel@tonic-gate 			const char *password =
4610Sstevel@tonic-gate 				((PW_CB_DATA *)UI_get0_user_data(ui))->password;
4620Sstevel@tonic-gate 			if (password && password[0] != '\0')
4630Sstevel@tonic-gate 				{
4640Sstevel@tonic-gate 				UI_set_result(ui, uis, password);
4650Sstevel@tonic-gate 				return 1;
4660Sstevel@tonic-gate 				}
4670Sstevel@tonic-gate 			}
4680Sstevel@tonic-gate 		default:
4690Sstevel@tonic-gate 			break;
4700Sstevel@tonic-gate 			}
4710Sstevel@tonic-gate 		}
4720Sstevel@tonic-gate 	return UI_method_get_reader(UI_OpenSSL())(ui, uis);
4730Sstevel@tonic-gate 	}
ui_write(UI * ui,UI_STRING * uis)4740Sstevel@tonic-gate static int ui_write(UI *ui, UI_STRING *uis)
4750Sstevel@tonic-gate 	{
4760Sstevel@tonic-gate 	if (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD
4770Sstevel@tonic-gate 		&& UI_get0_user_data(ui))
4780Sstevel@tonic-gate 		{
4790Sstevel@tonic-gate 		switch(UI_get_string_type(uis))
4800Sstevel@tonic-gate 			{
4810Sstevel@tonic-gate 		case UIT_PROMPT:
4820Sstevel@tonic-gate 		case UIT_VERIFY:
4830Sstevel@tonic-gate 			{
4840Sstevel@tonic-gate 			const char *password =
4850Sstevel@tonic-gate 				((PW_CB_DATA *)UI_get0_user_data(ui))->password;
4860Sstevel@tonic-gate 			if (password && password[0] != '\0')
4870Sstevel@tonic-gate 				return 1;
4880Sstevel@tonic-gate 			}
4890Sstevel@tonic-gate 		default:
4900Sstevel@tonic-gate 			break;
4910Sstevel@tonic-gate 			}
4920Sstevel@tonic-gate 		}
4930Sstevel@tonic-gate 	return UI_method_get_writer(UI_OpenSSL())(ui, uis);
4940Sstevel@tonic-gate 	}
ui_close(UI * ui)4950Sstevel@tonic-gate static int ui_close(UI *ui)
4960Sstevel@tonic-gate 	{
4970Sstevel@tonic-gate 	return UI_method_get_closer(UI_OpenSSL())(ui);
4980Sstevel@tonic-gate 	}
setup_ui_method(void)4990Sstevel@tonic-gate int setup_ui_method(void)
5000Sstevel@tonic-gate 	{
5010Sstevel@tonic-gate 	ui_method = UI_create_method("OpenSSL application user interface");
5020Sstevel@tonic-gate 	UI_method_set_opener(ui_method, ui_open);
5030Sstevel@tonic-gate 	UI_method_set_reader(ui_method, ui_read);
5040Sstevel@tonic-gate 	UI_method_set_writer(ui_method, ui_write);
5050Sstevel@tonic-gate 	UI_method_set_closer(ui_method, ui_close);
5060Sstevel@tonic-gate 	return 0;
5070Sstevel@tonic-gate 	}
destroy_ui_method(void)5080Sstevel@tonic-gate void destroy_ui_method(void)
5090Sstevel@tonic-gate 	{
5100Sstevel@tonic-gate 	if(ui_method)
5110Sstevel@tonic-gate 		{
5120Sstevel@tonic-gate 		UI_destroy_method(ui_method);
5130Sstevel@tonic-gate 		ui_method = NULL;
5140Sstevel@tonic-gate 		}
5150Sstevel@tonic-gate 	}
password_callback(char * buf,int bufsiz,int verify,PW_CB_DATA * cb_tmp)5160Sstevel@tonic-gate int password_callback(char *buf, int bufsiz, int verify,
5170Sstevel@tonic-gate 	PW_CB_DATA *cb_tmp)
5180Sstevel@tonic-gate 	{
5190Sstevel@tonic-gate 	UI *ui = NULL;
5200Sstevel@tonic-gate 	int res = 0;
5210Sstevel@tonic-gate 	const char *prompt_info = NULL;
5220Sstevel@tonic-gate 	const char *password = NULL;
5230Sstevel@tonic-gate 	PW_CB_DATA *cb_data = (PW_CB_DATA *)cb_tmp;
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	if (cb_data)
5260Sstevel@tonic-gate 		{
5270Sstevel@tonic-gate 		if (cb_data->password)
5280Sstevel@tonic-gate 			password = cb_data->password;
5290Sstevel@tonic-gate 		if (cb_data->prompt_info)
5300Sstevel@tonic-gate 			prompt_info = cb_data->prompt_info;
5310Sstevel@tonic-gate 		}
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	if (password)
5340Sstevel@tonic-gate 		{
5350Sstevel@tonic-gate 		res = strlen(password);
5360Sstevel@tonic-gate 		if (res > bufsiz)
5370Sstevel@tonic-gate 			res = bufsiz;
5380Sstevel@tonic-gate 		memcpy(buf, password, res);
5390Sstevel@tonic-gate 		return res;
5400Sstevel@tonic-gate 		}
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	ui = UI_new_method(ui_method);
5430Sstevel@tonic-gate 	if (ui)
5440Sstevel@tonic-gate 		{
5450Sstevel@tonic-gate 		int ok = 0;
5460Sstevel@tonic-gate 		char *buff = NULL;
5470Sstevel@tonic-gate 		int ui_flags = 0;
5480Sstevel@tonic-gate 		char *prompt = NULL;
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 		prompt = UI_construct_prompt(ui, "pass phrase",
551*2139Sjp161948 			prompt_info);
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 		ui_flags |= UI_INPUT_FLAG_DEFAULT_PWD;
5540Sstevel@tonic-gate 		UI_ctrl(ui, UI_CTRL_PRINT_ERRORS, 1, 0, 0);
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 		if (ok >= 0)
5570Sstevel@tonic-gate 			ok = UI_add_input_string(ui,prompt,ui_flags,buf,
5580Sstevel@tonic-gate 				PW_MIN_LENGTH,BUFSIZ-1);
5590Sstevel@tonic-gate 		if (ok >= 0 && verify)
5600Sstevel@tonic-gate 			{
5610Sstevel@tonic-gate 			buff = (char *)OPENSSL_malloc(bufsiz);
5620Sstevel@tonic-gate 			ok = UI_add_verify_string(ui,prompt,ui_flags,buff,
5630Sstevel@tonic-gate 				PW_MIN_LENGTH,BUFSIZ-1, buf);
5640Sstevel@tonic-gate 			}
5650Sstevel@tonic-gate 		if (ok >= 0)
5660Sstevel@tonic-gate 			do
5670Sstevel@tonic-gate 				{
5680Sstevel@tonic-gate 				ok = UI_process(ui);
5690Sstevel@tonic-gate 				}
5700Sstevel@tonic-gate 			while (ok < 0 && UI_ctrl(ui, UI_CTRL_IS_REDOABLE, 0, 0, 0));
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 		if (buff)
5730Sstevel@tonic-gate 			{
5740Sstevel@tonic-gate 			OPENSSL_cleanse(buff,(unsigned int)bufsiz);
5750Sstevel@tonic-gate 			OPENSSL_free(buff);
5760Sstevel@tonic-gate 			}
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 		if (ok >= 0)
5790Sstevel@tonic-gate 			res = strlen(buf);
5800Sstevel@tonic-gate 		if (ok == -1)
5810Sstevel@tonic-gate 			{
5820Sstevel@tonic-gate 			BIO_printf(bio_err, "User interface error\n");
5830Sstevel@tonic-gate 			ERR_print_errors(bio_err);
5840Sstevel@tonic-gate 			OPENSSL_cleanse(buf,(unsigned int)bufsiz);
5850Sstevel@tonic-gate 			res = 0;
5860Sstevel@tonic-gate 			}
5870Sstevel@tonic-gate 		if (ok == -2)
5880Sstevel@tonic-gate 			{
5890Sstevel@tonic-gate 			BIO_printf(bio_err,"aborted!\n");
5900Sstevel@tonic-gate 			OPENSSL_cleanse(buf,(unsigned int)bufsiz);
5910Sstevel@tonic-gate 			res = 0;
5920Sstevel@tonic-gate 			}
5930Sstevel@tonic-gate 		UI_free(ui);
5940Sstevel@tonic-gate 		OPENSSL_free(prompt);
5950Sstevel@tonic-gate 		}
5960Sstevel@tonic-gate 	return res;
5970Sstevel@tonic-gate 	}
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate static char *app_get_pass(BIO *err, char *arg, int keepbio);
6000Sstevel@tonic-gate 
app_passwd(BIO * err,char * arg1,char * arg2,char ** pass1,char ** pass2)6010Sstevel@tonic-gate int app_passwd(BIO *err, char *arg1, char *arg2, char **pass1, char **pass2)
6020Sstevel@tonic-gate {
6030Sstevel@tonic-gate 	int same;
6040Sstevel@tonic-gate 	if(!arg2 || !arg1 || strcmp(arg1, arg2)) same = 0;
6050Sstevel@tonic-gate 	else same = 1;
6060Sstevel@tonic-gate 	if(arg1) {
6070Sstevel@tonic-gate 		*pass1 = app_get_pass(err, arg1, same);
6080Sstevel@tonic-gate 		if(!*pass1) return 0;
6090Sstevel@tonic-gate 	} else if(pass1) *pass1 = NULL;
6100Sstevel@tonic-gate 	if(arg2) {
6110Sstevel@tonic-gate 		*pass2 = app_get_pass(err, arg2, same ? 2 : 0);
6120Sstevel@tonic-gate 		if(!*pass2) return 0;
6130Sstevel@tonic-gate 	} else if(pass2) *pass2 = NULL;
6140Sstevel@tonic-gate 	return 1;
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate 
app_get_pass(BIO * err,char * arg,int keepbio)6170Sstevel@tonic-gate static char *app_get_pass(BIO *err, char *arg, int keepbio)
6180Sstevel@tonic-gate {
6190Sstevel@tonic-gate 	char *tmp, tpass[APP_PASS_LEN];
6200Sstevel@tonic-gate 	static BIO *pwdbio = NULL;
6210Sstevel@tonic-gate 	int i;
6220Sstevel@tonic-gate 	if(!strncmp(arg, "pass:", 5)) return BUF_strdup(arg + 5);
6230Sstevel@tonic-gate 	if(!strncmp(arg, "env:", 4)) {
6240Sstevel@tonic-gate 		tmp = getenv(arg + 4);
6250Sstevel@tonic-gate 		if(!tmp) {
6260Sstevel@tonic-gate 			BIO_printf(err, "Can't read environment variable %s\n", arg + 4);
6270Sstevel@tonic-gate 			return NULL;
6280Sstevel@tonic-gate 		}
6290Sstevel@tonic-gate 		return BUF_strdup(tmp);
6300Sstevel@tonic-gate 	}
6310Sstevel@tonic-gate 	if(!keepbio || !pwdbio) {
6320Sstevel@tonic-gate 		if(!strncmp(arg, "file:", 5)) {
6330Sstevel@tonic-gate 			pwdbio = BIO_new_file(arg + 5, "r");
6340Sstevel@tonic-gate 			if(!pwdbio) {
6350Sstevel@tonic-gate 				BIO_printf(err, "Can't open file %s\n", arg + 5);
6360Sstevel@tonic-gate 				return NULL;
6370Sstevel@tonic-gate 			}
6380Sstevel@tonic-gate 		} else if(!strncmp(arg, "fd:", 3)) {
6390Sstevel@tonic-gate 			BIO *btmp;
6400Sstevel@tonic-gate 			i = atoi(arg + 3);
6410Sstevel@tonic-gate 			if(i >= 0) pwdbio = BIO_new_fd(i, BIO_NOCLOSE);
6420Sstevel@tonic-gate 			if((i < 0) || !pwdbio) {
6430Sstevel@tonic-gate 				BIO_printf(err, "Can't access file descriptor %s\n", arg + 3);
6440Sstevel@tonic-gate 				return NULL;
6450Sstevel@tonic-gate 			}
6460Sstevel@tonic-gate 			/* Can't do BIO_gets on an fd BIO so add a buffering BIO */
6470Sstevel@tonic-gate 			btmp = BIO_new(BIO_f_buffer());
6480Sstevel@tonic-gate 			pwdbio = BIO_push(btmp, pwdbio);
6490Sstevel@tonic-gate 		} else if(!strcmp(arg, "stdin")) {
6500Sstevel@tonic-gate 			pwdbio = BIO_new_fp(stdin, BIO_NOCLOSE);
6510Sstevel@tonic-gate 			if(!pwdbio) {
6520Sstevel@tonic-gate 				BIO_printf(err, "Can't open BIO for stdin\n");
6530Sstevel@tonic-gate 				return NULL;
6540Sstevel@tonic-gate 			}
6550Sstevel@tonic-gate 		} else {
6560Sstevel@tonic-gate 			BIO_printf(err, "Invalid password argument \"%s\"\n", arg);
6570Sstevel@tonic-gate 			return NULL;
6580Sstevel@tonic-gate 		}
6590Sstevel@tonic-gate 	}
6600Sstevel@tonic-gate 	i = BIO_gets(pwdbio, tpass, APP_PASS_LEN);
6610Sstevel@tonic-gate 	if(keepbio != 1) {
6620Sstevel@tonic-gate 		BIO_free_all(pwdbio);
6630Sstevel@tonic-gate 		pwdbio = NULL;
6640Sstevel@tonic-gate 	}
6650Sstevel@tonic-gate 	if(i <= 0) {
6660Sstevel@tonic-gate 		BIO_printf(err, "Error reading password from BIO\n");
6670Sstevel@tonic-gate 		return NULL;
6680Sstevel@tonic-gate 	}
6690Sstevel@tonic-gate 	tmp = strchr(tpass, '\n');
6700Sstevel@tonic-gate 	if(tmp) *tmp = 0;
6710Sstevel@tonic-gate 	return BUF_strdup(tpass);
6720Sstevel@tonic-gate }
6730Sstevel@tonic-gate 
add_oid_section(BIO * err,CONF * conf)6740Sstevel@tonic-gate int add_oid_section(BIO *err, CONF *conf)
6750Sstevel@tonic-gate {
6760Sstevel@tonic-gate 	char *p;
6770Sstevel@tonic-gate 	STACK_OF(CONF_VALUE) *sktmp;
6780Sstevel@tonic-gate 	CONF_VALUE *cnf;
6790Sstevel@tonic-gate 	int i;
6800Sstevel@tonic-gate 	if(!(p=NCONF_get_string(conf,NULL,"oid_section")))
6810Sstevel@tonic-gate 		{
6820Sstevel@tonic-gate 		ERR_clear_error();
6830Sstevel@tonic-gate 		return 1;
6840Sstevel@tonic-gate 		}
6850Sstevel@tonic-gate 	if(!(sktmp = NCONF_get_section(conf, p))) {
6860Sstevel@tonic-gate 		BIO_printf(err, "problem loading oid section %s\n", p);
6870Sstevel@tonic-gate 		return 0;
6880Sstevel@tonic-gate 	}
6890Sstevel@tonic-gate 	for(i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
6900Sstevel@tonic-gate 		cnf = sk_CONF_VALUE_value(sktmp, i);
6910Sstevel@tonic-gate 		if(OBJ_create(cnf->value, cnf->name, cnf->name) == NID_undef) {
6920Sstevel@tonic-gate 			BIO_printf(err, "problem creating object %s=%s\n",
6930Sstevel@tonic-gate 							 cnf->name, cnf->value);
6940Sstevel@tonic-gate 			return 0;
6950Sstevel@tonic-gate 		}
6960Sstevel@tonic-gate 	}
6970Sstevel@tonic-gate 	return 1;
6980Sstevel@tonic-gate }
6990Sstevel@tonic-gate 
load_pkcs12(BIO * err,BIO * in,const char * desc,pem_password_cb * pem_cb,void * cb_data,EVP_PKEY ** pkey,X509 ** cert,STACK_OF (X509)** ca)700*2139Sjp161948 static int load_pkcs12(BIO *err, BIO *in, const char *desc,
701*2139Sjp161948 		pem_password_cb *pem_cb,  void *cb_data,
702*2139Sjp161948 		EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca)
703*2139Sjp161948 	{
704*2139Sjp161948  	const char *pass;
705*2139Sjp161948 	char tpass[PEM_BUFSIZE];
706*2139Sjp161948 	int len, ret = 0;
707*2139Sjp161948 	PKCS12 *p12;
708*2139Sjp161948 	p12 = d2i_PKCS12_bio(in, NULL);
709*2139Sjp161948 	if (p12 == NULL)
710*2139Sjp161948 		{
711*2139Sjp161948 		BIO_printf(err, "Error loading PKCS12 file for %s\n", desc);
712*2139Sjp161948 		goto die;
713*2139Sjp161948 		}
714*2139Sjp161948 	/* See if an empty password will do */
715*2139Sjp161948 	if (PKCS12_verify_mac(p12, "", 0) || PKCS12_verify_mac(p12, NULL, 0))
716*2139Sjp161948 		pass = "";
717*2139Sjp161948 	else
718*2139Sjp161948 		{
719*2139Sjp161948 		if (!pem_cb)
720*2139Sjp161948 			pem_cb = (pem_password_cb *)password_callback;
721*2139Sjp161948 		len = pem_cb(tpass, PEM_BUFSIZE, 0, cb_data);
722*2139Sjp161948 		if (len < 0)
723*2139Sjp161948 			{
724*2139Sjp161948 			BIO_printf(err, "Passpharse callback error for %s\n",
725*2139Sjp161948 					desc);
726*2139Sjp161948 			goto die;
727*2139Sjp161948 			}
728*2139Sjp161948 		if (len < PEM_BUFSIZE)
729*2139Sjp161948 			tpass[len] = 0;
730*2139Sjp161948 		if (!PKCS12_verify_mac(p12, tpass, len))
731*2139Sjp161948 			{
732*2139Sjp161948 			BIO_printf(err,
733*2139Sjp161948 	"Mac verify error (wrong password?) in PKCS12 file for %s\n", desc);
734*2139Sjp161948 			goto die;
735*2139Sjp161948 			}
736*2139Sjp161948 		pass = tpass;
737*2139Sjp161948 		}
738*2139Sjp161948 	ret = PKCS12_parse(p12, pass, pkey, cert, ca);
739*2139Sjp161948 	die:
740*2139Sjp161948 	if (p12)
741*2139Sjp161948 		PKCS12_free(p12);
742*2139Sjp161948 	return ret;
743*2139Sjp161948 	}
744*2139Sjp161948 
load_cert(BIO * err,const char * file,int format,const char * pass,ENGINE * e,const char * cert_descrip)7450Sstevel@tonic-gate X509 *load_cert(BIO *err, const char *file, int format,
7460Sstevel@tonic-gate 	const char *pass, ENGINE *e, const char *cert_descrip)
7470Sstevel@tonic-gate 	{
7480Sstevel@tonic-gate 	ASN1_HEADER *ah=NULL;
7490Sstevel@tonic-gate 	BUF_MEM *buf=NULL;
7500Sstevel@tonic-gate 	X509 *x=NULL;
7510Sstevel@tonic-gate 	BIO *cert;
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	if ((cert=BIO_new(BIO_s_file())) == NULL)
7540Sstevel@tonic-gate 		{
7550Sstevel@tonic-gate 		ERR_print_errors(err);
7560Sstevel@tonic-gate 		goto end;
7570Sstevel@tonic-gate 		}
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	if (file == NULL)
7600Sstevel@tonic-gate 		{
7610Sstevel@tonic-gate 		setvbuf(stdin, NULL, _IONBF, 0);
7620Sstevel@tonic-gate 		BIO_set_fp(cert,stdin,BIO_NOCLOSE);
7630Sstevel@tonic-gate 		}
7640Sstevel@tonic-gate 	else
7650Sstevel@tonic-gate 		{
7660Sstevel@tonic-gate 		if (BIO_read_filename(cert,file) <= 0)
7670Sstevel@tonic-gate 			{
7680Sstevel@tonic-gate 			BIO_printf(err, "Error opening %s %s\n",
7690Sstevel@tonic-gate 				cert_descrip, file);
7700Sstevel@tonic-gate 			ERR_print_errors(err);
7710Sstevel@tonic-gate 			goto end;
7720Sstevel@tonic-gate 			}
7730Sstevel@tonic-gate 		}
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	if 	(format == FORMAT_ASN1)
7760Sstevel@tonic-gate 		x=d2i_X509_bio(cert,NULL);
7770Sstevel@tonic-gate 	else if (format == FORMAT_NETSCAPE)
7780Sstevel@tonic-gate 		{
779*2139Sjp161948 		const unsigned char *p,*op;
7800Sstevel@tonic-gate 		int size=0,i;
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 		/* We sort of have to do it this way because it is sort of nice
7830Sstevel@tonic-gate 		 * to read the header first and check it, then
7840Sstevel@tonic-gate 		 * try to read the certificate */
7850Sstevel@tonic-gate 		buf=BUF_MEM_new();
7860Sstevel@tonic-gate 		for (;;)
7870Sstevel@tonic-gate 			{
7880Sstevel@tonic-gate 			if ((buf == NULL) || (!BUF_MEM_grow(buf,size+1024*10)))
7890Sstevel@tonic-gate 				goto end;
7900Sstevel@tonic-gate 			i=BIO_read(cert,&(buf->data[size]),1024*10);
7910Sstevel@tonic-gate 			size+=i;
7920Sstevel@tonic-gate 			if (i == 0) break;
7930Sstevel@tonic-gate 			if (i < 0)
7940Sstevel@tonic-gate 				{
7950Sstevel@tonic-gate 				perror("reading certificate");
7960Sstevel@tonic-gate 				goto end;
7970Sstevel@tonic-gate 				}
7980Sstevel@tonic-gate 			}
7990Sstevel@tonic-gate 		p=(unsigned char *)buf->data;
8000Sstevel@tonic-gate 		op=p;
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 		/* First load the header */
8030Sstevel@tonic-gate 		if ((ah=d2i_ASN1_HEADER(NULL,&p,(long)size)) == NULL)
8040Sstevel@tonic-gate 			goto end;
8050Sstevel@tonic-gate 		if ((ah->header == NULL) || (ah->header->data == NULL) ||
8060Sstevel@tonic-gate 			(strncmp(NETSCAPE_CERT_HDR,(char *)ah->header->data,
8070Sstevel@tonic-gate 			ah->header->length) != 0))
8080Sstevel@tonic-gate 			{
8090Sstevel@tonic-gate 			BIO_printf(err,"Error reading header on certificate\n");
8100Sstevel@tonic-gate 			goto end;
8110Sstevel@tonic-gate 			}
8120Sstevel@tonic-gate 		/* header is ok, so now read the object */
8130Sstevel@tonic-gate 		p=op;
8140Sstevel@tonic-gate 		ah->meth=X509_asn1_meth();
8150Sstevel@tonic-gate 		if ((ah=d2i_ASN1_HEADER(&ah,&p,(long)size)) == NULL)
8160Sstevel@tonic-gate 			goto end;
8170Sstevel@tonic-gate 		x=(X509 *)ah->data;
8180Sstevel@tonic-gate 		ah->data=NULL;
8190Sstevel@tonic-gate 		}
8200Sstevel@tonic-gate 	else if (format == FORMAT_PEM)
8210Sstevel@tonic-gate 		x=PEM_read_bio_X509_AUX(cert,NULL,
8220Sstevel@tonic-gate 			(pem_password_cb *)password_callback, NULL);
8230Sstevel@tonic-gate 	else if (format == FORMAT_PKCS12)
8240Sstevel@tonic-gate 		{
825*2139Sjp161948 		if (!load_pkcs12(err, cert,cert_descrip, NULL, NULL,
826*2139Sjp161948 					NULL, &x, NULL))
827*2139Sjp161948 			goto end;
8280Sstevel@tonic-gate 		}
8290Sstevel@tonic-gate 	else	{
8300Sstevel@tonic-gate 		BIO_printf(err,"bad input format specified for %s\n",
8310Sstevel@tonic-gate 			cert_descrip);
8320Sstevel@tonic-gate 		goto end;
8330Sstevel@tonic-gate 		}
8340Sstevel@tonic-gate end:
8350Sstevel@tonic-gate 	if (x == NULL)
8360Sstevel@tonic-gate 		{
8370Sstevel@tonic-gate 		BIO_printf(err,"unable to load certificate\n");
8380Sstevel@tonic-gate 		ERR_print_errors(err);
8390Sstevel@tonic-gate 		}
8400Sstevel@tonic-gate 	if (ah != NULL) ASN1_HEADER_free(ah);
8410Sstevel@tonic-gate 	if (cert != NULL) BIO_free(cert);
8420Sstevel@tonic-gate 	if (buf != NULL) BUF_MEM_free(buf);
8430Sstevel@tonic-gate 	return(x);
8440Sstevel@tonic-gate 	}
8450Sstevel@tonic-gate 
load_key(BIO * err,const char * file,int format,int maybe_stdin,const char * pass,ENGINE * e,const char * key_descrip)8460Sstevel@tonic-gate EVP_PKEY *load_key(BIO *err, const char *file, int format, int maybe_stdin,
8470Sstevel@tonic-gate 	const char *pass, ENGINE *e, const char *key_descrip)
8480Sstevel@tonic-gate 	{
8490Sstevel@tonic-gate 	BIO *key=NULL;
8500Sstevel@tonic-gate 	EVP_PKEY *pkey=NULL;
8510Sstevel@tonic-gate 	PW_CB_DATA cb_data;
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	cb_data.password = pass;
8540Sstevel@tonic-gate 	cb_data.prompt_info = file;
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 	if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE))
8570Sstevel@tonic-gate 		{
8580Sstevel@tonic-gate 		BIO_printf(err,"no keyfile specified\n");
8590Sstevel@tonic-gate 		goto end;
8600Sstevel@tonic-gate 		}
8610Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
8620Sstevel@tonic-gate 	if (format == FORMAT_ENGINE)
8630Sstevel@tonic-gate 		{
8640Sstevel@tonic-gate 		if (!e)
8650Sstevel@tonic-gate 			BIO_printf(bio_err,"no engine specified\n");
8660Sstevel@tonic-gate 		else
8670Sstevel@tonic-gate 			pkey = ENGINE_load_private_key(e, file,
8680Sstevel@tonic-gate 				ui_method, &cb_data);
8690Sstevel@tonic-gate 		goto end;
8700Sstevel@tonic-gate 		}
8710Sstevel@tonic-gate #endif
8720Sstevel@tonic-gate 	key=BIO_new(BIO_s_file());
8730Sstevel@tonic-gate 	if (key == NULL)
8740Sstevel@tonic-gate 		{
8750Sstevel@tonic-gate 		ERR_print_errors(err);
8760Sstevel@tonic-gate 		goto end;
8770Sstevel@tonic-gate 		}
8780Sstevel@tonic-gate 	if (file == NULL && maybe_stdin)
8790Sstevel@tonic-gate 		{
8800Sstevel@tonic-gate 		setvbuf(stdin, NULL, _IONBF, 0);
8810Sstevel@tonic-gate 		BIO_set_fp(key,stdin,BIO_NOCLOSE);
8820Sstevel@tonic-gate 		}
8830Sstevel@tonic-gate 	else
8840Sstevel@tonic-gate 		if (BIO_read_filename(key,file) <= 0)
8850Sstevel@tonic-gate 			{
8860Sstevel@tonic-gate 			BIO_printf(err, "Error opening %s %s\n",
8870Sstevel@tonic-gate 				key_descrip, file);
8880Sstevel@tonic-gate 			ERR_print_errors(err);
8890Sstevel@tonic-gate 			goto end;
8900Sstevel@tonic-gate 			}
8910Sstevel@tonic-gate 	if (format == FORMAT_ASN1)
8920Sstevel@tonic-gate 		{
8930Sstevel@tonic-gate 		pkey=d2i_PrivateKey_bio(key, NULL);
8940Sstevel@tonic-gate 		}
8950Sstevel@tonic-gate 	else if (format == FORMAT_PEM)
8960Sstevel@tonic-gate 		{
8970Sstevel@tonic-gate 		pkey=PEM_read_bio_PrivateKey(key,NULL,
8980Sstevel@tonic-gate 			(pem_password_cb *)password_callback, &cb_data);
8990Sstevel@tonic-gate 		}
9000Sstevel@tonic-gate #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
9010Sstevel@tonic-gate 	else if (format == FORMAT_NETSCAPE || format == FORMAT_IISSGC)
9020Sstevel@tonic-gate 		pkey = load_netscape_key(err, key, file, key_descrip, format);
9030Sstevel@tonic-gate #endif
9040Sstevel@tonic-gate 	else if (format == FORMAT_PKCS12)
9050Sstevel@tonic-gate 		{
906*2139Sjp161948 		if (!load_pkcs12(err, key, key_descrip,
907*2139Sjp161948 				(pem_password_cb *)password_callback, &cb_data,
908*2139Sjp161948 				&pkey, NULL, NULL))
909*2139Sjp161948 			goto end;
9100Sstevel@tonic-gate 		}
9110Sstevel@tonic-gate 	else
9120Sstevel@tonic-gate 		{
9130Sstevel@tonic-gate 		BIO_printf(err,"bad input format specified for key file\n");
9140Sstevel@tonic-gate 		goto end;
9150Sstevel@tonic-gate 		}
9160Sstevel@tonic-gate  end:
9170Sstevel@tonic-gate 	if (key != NULL) BIO_free(key);
9180Sstevel@tonic-gate 	if (pkey == NULL)
9190Sstevel@tonic-gate 		BIO_printf(err,"unable to load %s\n", key_descrip);
9200Sstevel@tonic-gate 	return(pkey);
9210Sstevel@tonic-gate 	}
9220Sstevel@tonic-gate 
load_pubkey(BIO * err,const char * file,int format,int maybe_stdin,const char * pass,ENGINE * e,const char * key_descrip)9230Sstevel@tonic-gate EVP_PKEY *load_pubkey(BIO *err, const char *file, int format, int maybe_stdin,
9240Sstevel@tonic-gate 	const char *pass, ENGINE *e, const char *key_descrip)
9250Sstevel@tonic-gate 	{
9260Sstevel@tonic-gate 	BIO *key=NULL;
9270Sstevel@tonic-gate 	EVP_PKEY *pkey=NULL;
9280Sstevel@tonic-gate 	PW_CB_DATA cb_data;
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	cb_data.password = pass;
9310Sstevel@tonic-gate 	cb_data.prompt_info = file;
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 	if (file == NULL && (!maybe_stdin || format == FORMAT_ENGINE))
9340Sstevel@tonic-gate 		{
9350Sstevel@tonic-gate 		BIO_printf(err,"no keyfile specified\n");
9360Sstevel@tonic-gate 		goto end;
9370Sstevel@tonic-gate 		}
9380Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
9390Sstevel@tonic-gate 	if (format == FORMAT_ENGINE)
9400Sstevel@tonic-gate 		{
9410Sstevel@tonic-gate 		if (!e)
9420Sstevel@tonic-gate 			BIO_printf(bio_err,"no engine specified\n");
9430Sstevel@tonic-gate 		else
9440Sstevel@tonic-gate 			pkey = ENGINE_load_public_key(e, file,
9450Sstevel@tonic-gate 				ui_method, &cb_data);
9460Sstevel@tonic-gate 		goto end;
9470Sstevel@tonic-gate 		}
9480Sstevel@tonic-gate #endif
9490Sstevel@tonic-gate 	key=BIO_new(BIO_s_file());
9500Sstevel@tonic-gate 	if (key == NULL)
9510Sstevel@tonic-gate 		{
9520Sstevel@tonic-gate 		ERR_print_errors(err);
9530Sstevel@tonic-gate 		goto end;
9540Sstevel@tonic-gate 		}
9550Sstevel@tonic-gate 	if (file == NULL && maybe_stdin)
9560Sstevel@tonic-gate 		{
9570Sstevel@tonic-gate 		setvbuf(stdin, NULL, _IONBF, 0);
9580Sstevel@tonic-gate 		BIO_set_fp(key,stdin,BIO_NOCLOSE);
9590Sstevel@tonic-gate 		}
9600Sstevel@tonic-gate 	else
9610Sstevel@tonic-gate 		if (BIO_read_filename(key,file) <= 0)
9620Sstevel@tonic-gate 			{
9630Sstevel@tonic-gate 			BIO_printf(err, "Error opening %s %s\n",
9640Sstevel@tonic-gate 				key_descrip, file);
9650Sstevel@tonic-gate 			ERR_print_errors(err);
9660Sstevel@tonic-gate 			goto end;
9670Sstevel@tonic-gate 		}
9680Sstevel@tonic-gate 	if (format == FORMAT_ASN1)
9690Sstevel@tonic-gate 		{
9700Sstevel@tonic-gate 		pkey=d2i_PUBKEY_bio(key, NULL);
9710Sstevel@tonic-gate 		}
9720Sstevel@tonic-gate 	else if (format == FORMAT_PEM)
9730Sstevel@tonic-gate 		{
9740Sstevel@tonic-gate 		pkey=PEM_read_bio_PUBKEY(key,NULL,
9750Sstevel@tonic-gate 			(pem_password_cb *)password_callback, &cb_data);
9760Sstevel@tonic-gate 		}
9770Sstevel@tonic-gate #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
9780Sstevel@tonic-gate 	else if (format == FORMAT_NETSCAPE || format == FORMAT_IISSGC)
9790Sstevel@tonic-gate 		pkey = load_netscape_key(err, key, file, key_descrip, format);
9800Sstevel@tonic-gate #endif
9810Sstevel@tonic-gate 	else
9820Sstevel@tonic-gate 		{
9830Sstevel@tonic-gate 		BIO_printf(err,"bad input format specified for key file\n");
9840Sstevel@tonic-gate 		goto end;
9850Sstevel@tonic-gate 		}
9860Sstevel@tonic-gate  end:
9870Sstevel@tonic-gate 	if (key != NULL) BIO_free(key);
9880Sstevel@tonic-gate 	if (pkey == NULL)
9890Sstevel@tonic-gate 		BIO_printf(err,"unable to load %s\n", key_descrip);
9900Sstevel@tonic-gate 	return(pkey);
9910Sstevel@tonic-gate 	}
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate #if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_RSA)
9940Sstevel@tonic-gate static EVP_PKEY *
load_netscape_key(BIO * err,BIO * key,const char * file,const char * key_descrip,int format)9950Sstevel@tonic-gate load_netscape_key(BIO *err, BIO *key, const char *file,
9960Sstevel@tonic-gate 		const char *key_descrip, int format)
9970Sstevel@tonic-gate 	{
9980Sstevel@tonic-gate 	EVP_PKEY *pkey;
9990Sstevel@tonic-gate 	BUF_MEM *buf;
10000Sstevel@tonic-gate 	RSA	*rsa;
10010Sstevel@tonic-gate 	const unsigned char *p;
10020Sstevel@tonic-gate 	int size, i;
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 	buf=BUF_MEM_new();
10050Sstevel@tonic-gate 	pkey = EVP_PKEY_new();
10060Sstevel@tonic-gate 	size = 0;
10070Sstevel@tonic-gate 	if (buf == NULL || pkey == NULL)
10080Sstevel@tonic-gate 		goto error;
10090Sstevel@tonic-gate 	for (;;)
10100Sstevel@tonic-gate 		{
10110Sstevel@tonic-gate 		if (!BUF_MEM_grow_clean(buf,size+1024*10))
10120Sstevel@tonic-gate 			goto error;
10130Sstevel@tonic-gate 		i = BIO_read(key, &(buf->data[size]), 1024*10);
10140Sstevel@tonic-gate 		size += i;
10150Sstevel@tonic-gate 		if (i == 0)
10160Sstevel@tonic-gate 			break;
10170Sstevel@tonic-gate 		if (i < 0)
10180Sstevel@tonic-gate 			{
10190Sstevel@tonic-gate 				BIO_printf(err, "Error reading %s %s",
10200Sstevel@tonic-gate 					key_descrip, file);
10210Sstevel@tonic-gate 				goto error;
10220Sstevel@tonic-gate 			}
10230Sstevel@tonic-gate 		}
10240Sstevel@tonic-gate 	p=(unsigned char *)buf->data;
10250Sstevel@tonic-gate 	rsa = d2i_RSA_NET(NULL,&p,(long)size,NULL,
10260Sstevel@tonic-gate 		(format == FORMAT_IISSGC ? 1 : 0));
10270Sstevel@tonic-gate 	if (rsa == NULL)
10280Sstevel@tonic-gate 		goto error;
10290Sstevel@tonic-gate 	BUF_MEM_free(buf);
10300Sstevel@tonic-gate 	EVP_PKEY_set1_RSA(pkey, rsa);
10310Sstevel@tonic-gate 	return pkey;
10320Sstevel@tonic-gate error:
10330Sstevel@tonic-gate 	BUF_MEM_free(buf);
10340Sstevel@tonic-gate 	EVP_PKEY_free(pkey);
10350Sstevel@tonic-gate 	return NULL;
10360Sstevel@tonic-gate 	}
10370Sstevel@tonic-gate #endif /* ndef OPENSSL_NO_RC4 */
10380Sstevel@tonic-gate 
STACK_OF(X509)10390Sstevel@tonic-gate STACK_OF(X509) *load_certs(BIO *err, const char *file, int format,
10400Sstevel@tonic-gate 	const char *pass, ENGINE *e, const char *cert_descrip)
10410Sstevel@tonic-gate 	{
10420Sstevel@tonic-gate 	BIO *certs;
10430Sstevel@tonic-gate 	int i;
10440Sstevel@tonic-gate 	STACK_OF(X509) *othercerts = NULL;
10450Sstevel@tonic-gate 	STACK_OF(X509_INFO) *allcerts = NULL;
10460Sstevel@tonic-gate 	X509_INFO *xi;
10470Sstevel@tonic-gate 	PW_CB_DATA cb_data;
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 	cb_data.password = pass;
10500Sstevel@tonic-gate 	cb_data.prompt_info = file;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	if((certs = BIO_new(BIO_s_file())) == NULL)
10530Sstevel@tonic-gate 		{
10540Sstevel@tonic-gate 		ERR_print_errors(err);
10550Sstevel@tonic-gate 		goto end;
10560Sstevel@tonic-gate 		}
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate 	if (file == NULL)
10590Sstevel@tonic-gate 		BIO_set_fp(certs,stdin,BIO_NOCLOSE);
10600Sstevel@tonic-gate 	else
10610Sstevel@tonic-gate 		{
10620Sstevel@tonic-gate 		if (BIO_read_filename(certs,file) <= 0)
10630Sstevel@tonic-gate 			{
10640Sstevel@tonic-gate 			BIO_printf(err, "Error opening %s %s\n",
10650Sstevel@tonic-gate 				cert_descrip, file);
10660Sstevel@tonic-gate 			ERR_print_errors(err);
10670Sstevel@tonic-gate 			goto end;
10680Sstevel@tonic-gate 			}
10690Sstevel@tonic-gate 		}
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 	if      (format == FORMAT_PEM)
10720Sstevel@tonic-gate 		{
10730Sstevel@tonic-gate 		othercerts = sk_X509_new_null();
10740Sstevel@tonic-gate 		if(!othercerts)
10750Sstevel@tonic-gate 			{
10760Sstevel@tonic-gate 			sk_X509_free(othercerts);
10770Sstevel@tonic-gate 			othercerts = NULL;
10780Sstevel@tonic-gate 			goto end;
10790Sstevel@tonic-gate 			}
10800Sstevel@tonic-gate 		allcerts = PEM_X509_INFO_read_bio(certs, NULL,
10810Sstevel@tonic-gate 				(pem_password_cb *)password_callback, &cb_data);
10820Sstevel@tonic-gate 		for(i = 0; i < sk_X509_INFO_num(allcerts); i++)
10830Sstevel@tonic-gate 			{
10840Sstevel@tonic-gate 			xi = sk_X509_INFO_value (allcerts, i);
10850Sstevel@tonic-gate 			if (xi->x509)
10860Sstevel@tonic-gate 				{
10870Sstevel@tonic-gate 				sk_X509_push(othercerts, xi->x509);
10880Sstevel@tonic-gate 				xi->x509 = NULL;
10890Sstevel@tonic-gate 				}
10900Sstevel@tonic-gate 			}
10910Sstevel@tonic-gate 		goto end;
10920Sstevel@tonic-gate 		}
10930Sstevel@tonic-gate 	else	{
10940Sstevel@tonic-gate 		BIO_printf(err,"bad input format specified for %s\n",
10950Sstevel@tonic-gate 			cert_descrip);
10960Sstevel@tonic-gate 		goto end;
10970Sstevel@tonic-gate 		}
10980Sstevel@tonic-gate end:
10990Sstevel@tonic-gate 	if (othercerts == NULL)
11000Sstevel@tonic-gate 		{
11010Sstevel@tonic-gate 		BIO_printf(err,"unable to load certificates\n");
11020Sstevel@tonic-gate 		ERR_print_errors(err);
11030Sstevel@tonic-gate 		}
11040Sstevel@tonic-gate 	if (allcerts) sk_X509_INFO_pop_free(allcerts, X509_INFO_free);
11050Sstevel@tonic-gate 	if (certs != NULL) BIO_free(certs);
11060Sstevel@tonic-gate 	return(othercerts);
11070Sstevel@tonic-gate 	}
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate #define X509V3_EXT_UNKNOWN_MASK		(0xfL << 16)
11110Sstevel@tonic-gate /* Return error for unknown extensions */
11120Sstevel@tonic-gate #define X509V3_EXT_DEFAULT		0
11130Sstevel@tonic-gate /* Print error for unknown extensions */
11140Sstevel@tonic-gate #define X509V3_EXT_ERROR_UNKNOWN	(1L << 16)
11150Sstevel@tonic-gate /* ASN1 parse unknown extensions */
11160Sstevel@tonic-gate #define X509V3_EXT_PARSE_UNKNOWN	(2L << 16)
11170Sstevel@tonic-gate /* BIO_dump unknown extensions */
11180Sstevel@tonic-gate #define X509V3_EXT_DUMP_UNKNOWN		(3L << 16)
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate #define X509_FLAG_CA (X509_FLAG_NO_ISSUER | X509_FLAG_NO_PUBKEY | \
11210Sstevel@tonic-gate 			 X509_FLAG_NO_HEADER | X509_FLAG_NO_VERSION)
11220Sstevel@tonic-gate 
set_cert_ex(unsigned long * flags,const char * arg)11230Sstevel@tonic-gate int set_cert_ex(unsigned long *flags, const char *arg)
11240Sstevel@tonic-gate {
11250Sstevel@tonic-gate 	static const NAME_EX_TBL cert_tbl[] = {
11260Sstevel@tonic-gate 		{ "compatible", X509_FLAG_COMPAT, 0xffffffffl},
11270Sstevel@tonic-gate 		{ "ca_default", X509_FLAG_CA, 0xffffffffl},
11280Sstevel@tonic-gate 		{ "no_header", X509_FLAG_NO_HEADER, 0},
11290Sstevel@tonic-gate 		{ "no_version", X509_FLAG_NO_VERSION, 0},
11300Sstevel@tonic-gate 		{ "no_serial", X509_FLAG_NO_SERIAL, 0},
11310Sstevel@tonic-gate 		{ "no_signame", X509_FLAG_NO_SIGNAME, 0},
11320Sstevel@tonic-gate 		{ "no_validity", X509_FLAG_NO_VALIDITY, 0},
11330Sstevel@tonic-gate 		{ "no_subject", X509_FLAG_NO_SUBJECT, 0},
11340Sstevel@tonic-gate 		{ "no_issuer", X509_FLAG_NO_ISSUER, 0},
11350Sstevel@tonic-gate 		{ "no_pubkey", X509_FLAG_NO_PUBKEY, 0},
11360Sstevel@tonic-gate 		{ "no_extensions", X509_FLAG_NO_EXTENSIONS, 0},
11370Sstevel@tonic-gate 		{ "no_sigdump", X509_FLAG_NO_SIGDUMP, 0},
11380Sstevel@tonic-gate 		{ "no_aux", X509_FLAG_NO_AUX, 0},
11390Sstevel@tonic-gate 		{ "no_attributes", X509_FLAG_NO_ATTRIBUTES, 0},
11400Sstevel@tonic-gate 		{ "ext_default", X509V3_EXT_DEFAULT, X509V3_EXT_UNKNOWN_MASK},
11410Sstevel@tonic-gate 		{ "ext_error", X509V3_EXT_ERROR_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
11420Sstevel@tonic-gate 		{ "ext_parse", X509V3_EXT_PARSE_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
11430Sstevel@tonic-gate 		{ "ext_dump", X509V3_EXT_DUMP_UNKNOWN, X509V3_EXT_UNKNOWN_MASK},
11440Sstevel@tonic-gate 		{ NULL, 0, 0}
11450Sstevel@tonic-gate 	};
11460Sstevel@tonic-gate 	return set_multi_opts(flags, arg, cert_tbl);
11470Sstevel@tonic-gate }
11480Sstevel@tonic-gate 
set_name_ex(unsigned long * flags,const char * arg)11490Sstevel@tonic-gate int set_name_ex(unsigned long *flags, const char *arg)
11500Sstevel@tonic-gate {
11510Sstevel@tonic-gate 	static const NAME_EX_TBL ex_tbl[] = {
11520Sstevel@tonic-gate 		{ "esc_2253", ASN1_STRFLGS_ESC_2253, 0},
11530Sstevel@tonic-gate 		{ "esc_ctrl", ASN1_STRFLGS_ESC_CTRL, 0},
11540Sstevel@tonic-gate 		{ "esc_msb", ASN1_STRFLGS_ESC_MSB, 0},
11550Sstevel@tonic-gate 		{ "use_quote", ASN1_STRFLGS_ESC_QUOTE, 0},
11560Sstevel@tonic-gate 		{ "utf8", ASN1_STRFLGS_UTF8_CONVERT, 0},
11570Sstevel@tonic-gate 		{ "ignore_type", ASN1_STRFLGS_IGNORE_TYPE, 0},
11580Sstevel@tonic-gate 		{ "show_type", ASN1_STRFLGS_SHOW_TYPE, 0},
11590Sstevel@tonic-gate 		{ "dump_all", ASN1_STRFLGS_DUMP_ALL, 0},
11600Sstevel@tonic-gate 		{ "dump_nostr", ASN1_STRFLGS_DUMP_UNKNOWN, 0},
11610Sstevel@tonic-gate 		{ "dump_der", ASN1_STRFLGS_DUMP_DER, 0},
11620Sstevel@tonic-gate 		{ "compat", XN_FLAG_COMPAT, 0xffffffffL},
11630Sstevel@tonic-gate 		{ "sep_comma_plus", XN_FLAG_SEP_COMMA_PLUS, XN_FLAG_SEP_MASK},
11640Sstevel@tonic-gate 		{ "sep_comma_plus_space", XN_FLAG_SEP_CPLUS_SPC, XN_FLAG_SEP_MASK},
11650Sstevel@tonic-gate 		{ "sep_semi_plus_space", XN_FLAG_SEP_SPLUS_SPC, XN_FLAG_SEP_MASK},
11660Sstevel@tonic-gate 		{ "sep_multiline", XN_FLAG_SEP_MULTILINE, XN_FLAG_SEP_MASK},
11670Sstevel@tonic-gate 		{ "dn_rev", XN_FLAG_DN_REV, 0},
11680Sstevel@tonic-gate 		{ "nofname", XN_FLAG_FN_NONE, XN_FLAG_FN_MASK},
11690Sstevel@tonic-gate 		{ "sname", XN_FLAG_FN_SN, XN_FLAG_FN_MASK},
11700Sstevel@tonic-gate 		{ "lname", XN_FLAG_FN_LN, XN_FLAG_FN_MASK},
11710Sstevel@tonic-gate 		{ "align", XN_FLAG_FN_ALIGN, 0},
11720Sstevel@tonic-gate 		{ "oid", XN_FLAG_FN_OID, XN_FLAG_FN_MASK},
11730Sstevel@tonic-gate 		{ "space_eq", XN_FLAG_SPC_EQ, 0},
11740Sstevel@tonic-gate 		{ "dump_unknown", XN_FLAG_DUMP_UNKNOWN_FIELDS, 0},
11750Sstevel@tonic-gate 		{ "RFC2253", XN_FLAG_RFC2253, 0xffffffffL},
11760Sstevel@tonic-gate 		{ "oneline", XN_FLAG_ONELINE, 0xffffffffL},
11770Sstevel@tonic-gate 		{ "multiline", XN_FLAG_MULTILINE, 0xffffffffL},
11780Sstevel@tonic-gate 		{ "ca_default", XN_FLAG_MULTILINE, 0xffffffffL},
11790Sstevel@tonic-gate 		{ NULL, 0, 0}
11800Sstevel@tonic-gate 	};
11810Sstevel@tonic-gate 	return set_multi_opts(flags, arg, ex_tbl);
11820Sstevel@tonic-gate }
11830Sstevel@tonic-gate 
set_ext_copy(int * copy_type,const char * arg)11840Sstevel@tonic-gate int set_ext_copy(int *copy_type, const char *arg)
11850Sstevel@tonic-gate {
11860Sstevel@tonic-gate 	if (!strcasecmp(arg, "none"))
11870Sstevel@tonic-gate 		*copy_type = EXT_COPY_NONE;
11880Sstevel@tonic-gate 	else if (!strcasecmp(arg, "copy"))
11890Sstevel@tonic-gate 		*copy_type = EXT_COPY_ADD;
11900Sstevel@tonic-gate 	else if (!strcasecmp(arg, "copyall"))
11910Sstevel@tonic-gate 		*copy_type = EXT_COPY_ALL;
11920Sstevel@tonic-gate 	else
11930Sstevel@tonic-gate 		return 0;
11940Sstevel@tonic-gate 	return 1;
11950Sstevel@tonic-gate }
11960Sstevel@tonic-gate 
copy_extensions(X509 * x,X509_REQ * req,int copy_type)11970Sstevel@tonic-gate int copy_extensions(X509 *x, X509_REQ *req, int copy_type)
11980Sstevel@tonic-gate {
11990Sstevel@tonic-gate 	STACK_OF(X509_EXTENSION) *exts = NULL;
12000Sstevel@tonic-gate 	X509_EXTENSION *ext, *tmpext;
12010Sstevel@tonic-gate 	ASN1_OBJECT *obj;
12020Sstevel@tonic-gate 	int i, idx, ret = 0;
12030Sstevel@tonic-gate 	if (!x || !req || (copy_type == EXT_COPY_NONE))
12040Sstevel@tonic-gate 		return 1;
12050Sstevel@tonic-gate 	exts = X509_REQ_get_extensions(req);
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 	for(i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
12080Sstevel@tonic-gate 		ext = sk_X509_EXTENSION_value(exts, i);
12090Sstevel@tonic-gate 		obj = X509_EXTENSION_get_object(ext);
12100Sstevel@tonic-gate 		idx = X509_get_ext_by_OBJ(x, obj, -1);
12110Sstevel@tonic-gate 		/* Does extension exist? */
12120Sstevel@tonic-gate 		if (idx != -1) {
12130Sstevel@tonic-gate 			/* If normal copy don't override existing extension */
12140Sstevel@tonic-gate 			if (copy_type == EXT_COPY_ADD)
12150Sstevel@tonic-gate 				continue;
12160Sstevel@tonic-gate 			/* Delete all extensions of same type */
12170Sstevel@tonic-gate 			do {
12180Sstevel@tonic-gate 				tmpext = X509_get_ext(x, idx);
12190Sstevel@tonic-gate 				X509_delete_ext(x, idx);
12200Sstevel@tonic-gate 				X509_EXTENSION_free(tmpext);
12210Sstevel@tonic-gate 				idx = X509_get_ext_by_OBJ(x, obj, -1);
12220Sstevel@tonic-gate 			} while (idx != -1);
12230Sstevel@tonic-gate 		}
12240Sstevel@tonic-gate 		if (!X509_add_ext(x, ext, -1))
12250Sstevel@tonic-gate 			goto end;
12260Sstevel@tonic-gate 	}
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 	ret = 1;
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 	end:
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 	sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 	return ret;
12350Sstevel@tonic-gate }
12360Sstevel@tonic-gate 
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 
set_multi_opts(unsigned long * flags,const char * arg,const NAME_EX_TBL * in_tbl)12400Sstevel@tonic-gate static int set_multi_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl)
12410Sstevel@tonic-gate {
12420Sstevel@tonic-gate 	STACK_OF(CONF_VALUE) *vals;
12430Sstevel@tonic-gate 	CONF_VALUE *val;
12440Sstevel@tonic-gate 	int i, ret = 1;
12450Sstevel@tonic-gate 	if(!arg) return 0;
12460Sstevel@tonic-gate 	vals = X509V3_parse_list(arg);
12470Sstevel@tonic-gate 	for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
12480Sstevel@tonic-gate 		val = sk_CONF_VALUE_value(vals, i);
12490Sstevel@tonic-gate 		if (!set_table_opts(flags, val->name, in_tbl))
12500Sstevel@tonic-gate 			ret = 0;
12510Sstevel@tonic-gate 	}
12520Sstevel@tonic-gate 	sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
12530Sstevel@tonic-gate 	return ret;
12540Sstevel@tonic-gate }
12550Sstevel@tonic-gate 
set_table_opts(unsigned long * flags,const char * arg,const NAME_EX_TBL * in_tbl)12560Sstevel@tonic-gate static int set_table_opts(unsigned long *flags, const char *arg, const NAME_EX_TBL *in_tbl)
12570Sstevel@tonic-gate {
12580Sstevel@tonic-gate 	char c;
12590Sstevel@tonic-gate 	const NAME_EX_TBL *ptbl;
12600Sstevel@tonic-gate 	c = arg[0];
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	if(c == '-') {
12630Sstevel@tonic-gate 		c = 0;
12640Sstevel@tonic-gate 		arg++;
12650Sstevel@tonic-gate 	} else if (c == '+') {
12660Sstevel@tonic-gate 		c = 1;
12670Sstevel@tonic-gate 		arg++;
12680Sstevel@tonic-gate 	} else c = 1;
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 	for(ptbl = in_tbl; ptbl->name; ptbl++) {
12710Sstevel@tonic-gate 		if(!strcasecmp(arg, ptbl->name)) {
12720Sstevel@tonic-gate 			*flags &= ~ptbl->mask;
12730Sstevel@tonic-gate 			if(c) *flags |= ptbl->flag;
12740Sstevel@tonic-gate 			else *flags &= ~ptbl->flag;
12750Sstevel@tonic-gate 			return 1;
12760Sstevel@tonic-gate 		}
12770Sstevel@tonic-gate 	}
12780Sstevel@tonic-gate 	return 0;
12790Sstevel@tonic-gate }
12800Sstevel@tonic-gate 
print_name(BIO * out,const char * title,X509_NAME * nm,unsigned long lflags)1281*2139Sjp161948 void print_name(BIO *out, const char *title, X509_NAME *nm, unsigned long lflags)
12820Sstevel@tonic-gate {
12830Sstevel@tonic-gate 	char *buf;
12840Sstevel@tonic-gate 	char mline = 0;
12850Sstevel@tonic-gate 	int indent = 0;
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate 	if(title) BIO_puts(out, title);
12880Sstevel@tonic-gate 	if((lflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
12890Sstevel@tonic-gate 		mline = 1;
12900Sstevel@tonic-gate 		indent = 4;
12910Sstevel@tonic-gate 	}
12920Sstevel@tonic-gate 	if(lflags == XN_FLAG_COMPAT) {
12930Sstevel@tonic-gate 		buf = X509_NAME_oneline(nm, 0, 0);
12940Sstevel@tonic-gate 		BIO_puts(out, buf);
12950Sstevel@tonic-gate 		BIO_puts(out, "\n");
12960Sstevel@tonic-gate 		OPENSSL_free(buf);
12970Sstevel@tonic-gate 	} else {
12980Sstevel@tonic-gate 		if(mline) BIO_puts(out, "\n");
12990Sstevel@tonic-gate 		X509_NAME_print_ex(out, nm, indent, lflags);
13000Sstevel@tonic-gate 		BIO_puts(out, "\n");
13010Sstevel@tonic-gate 	}
13020Sstevel@tonic-gate }
13030Sstevel@tonic-gate 
setup_verify(BIO * bp,char * CAfile,char * CApath)13040Sstevel@tonic-gate X509_STORE *setup_verify(BIO *bp, char *CAfile, char *CApath)
13050Sstevel@tonic-gate {
13060Sstevel@tonic-gate 	X509_STORE *store;
13070Sstevel@tonic-gate 	X509_LOOKUP *lookup;
13080Sstevel@tonic-gate 	if(!(store = X509_STORE_new())) goto end;
13090Sstevel@tonic-gate 	lookup=X509_STORE_add_lookup(store,X509_LOOKUP_file());
13100Sstevel@tonic-gate 	if (lookup == NULL) goto end;
13110Sstevel@tonic-gate 	if (CAfile) {
13120Sstevel@tonic-gate 		if(!X509_LOOKUP_load_file(lookup,CAfile,X509_FILETYPE_PEM)) {
13130Sstevel@tonic-gate 			BIO_printf(bp, "Error loading file %s\n", CAfile);
13140Sstevel@tonic-gate 			goto end;
13150Sstevel@tonic-gate 		}
13160Sstevel@tonic-gate 	} else X509_LOOKUP_load_file(lookup,NULL,X509_FILETYPE_DEFAULT);
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 	lookup=X509_STORE_add_lookup(store,X509_LOOKUP_hash_dir());
13190Sstevel@tonic-gate 	if (lookup == NULL) goto end;
13200Sstevel@tonic-gate 	if (CApath) {
13210Sstevel@tonic-gate 		if(!X509_LOOKUP_add_dir(lookup,CApath,X509_FILETYPE_PEM)) {
13220Sstevel@tonic-gate 			BIO_printf(bp, "Error loading directory %s\n", CApath);
13230Sstevel@tonic-gate 			goto end;
13240Sstevel@tonic-gate 		}
13250Sstevel@tonic-gate 	} else X509_LOOKUP_add_dir(lookup,NULL,X509_FILETYPE_DEFAULT);
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 	ERR_clear_error();
13280Sstevel@tonic-gate 	return store;
13290Sstevel@tonic-gate 	end:
13300Sstevel@tonic-gate 	X509_STORE_free(store);
13310Sstevel@tonic-gate 	return NULL;
13320Sstevel@tonic-gate }
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
13350Sstevel@tonic-gate /* Try to load an engine in a shareable library */
try_load_engine(BIO * err,const char * engine,int debug)13360Sstevel@tonic-gate static ENGINE *try_load_engine(BIO *err, const char *engine, int debug)
13370Sstevel@tonic-gate 	{
13380Sstevel@tonic-gate 	ENGINE *e = ENGINE_by_id("dynamic");
13390Sstevel@tonic-gate 	if (e)
13400Sstevel@tonic-gate 		{
13410Sstevel@tonic-gate 		if (!ENGINE_ctrl_cmd_string(e, "SO_PATH", engine, 0)
13420Sstevel@tonic-gate 			|| !ENGINE_ctrl_cmd_string(e, "LOAD", NULL, 0))
13430Sstevel@tonic-gate 			{
13440Sstevel@tonic-gate 			ENGINE_free(e);
13450Sstevel@tonic-gate 			e = NULL;
13460Sstevel@tonic-gate 			}
13470Sstevel@tonic-gate 		}
13480Sstevel@tonic-gate 	return e;
13490Sstevel@tonic-gate 	}
13500Sstevel@tonic-gate 
setup_engine(BIO * err,const char * engine,int debug)13510Sstevel@tonic-gate ENGINE *setup_engine(BIO *err, const char *engine, int debug)
13520Sstevel@tonic-gate         {
13530Sstevel@tonic-gate         ENGINE *e = NULL;
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate         if (engine)
13560Sstevel@tonic-gate                 {
13570Sstevel@tonic-gate 		if(strcmp(engine, "auto") == 0)
13580Sstevel@tonic-gate 			{
13590Sstevel@tonic-gate 			BIO_printf(err,"enabling auto ENGINE support\n");
13600Sstevel@tonic-gate 			ENGINE_register_all_complete();
13610Sstevel@tonic-gate 			return NULL;
13620Sstevel@tonic-gate 			}
13630Sstevel@tonic-gate 		if((e = ENGINE_by_id(engine)) == NULL
13640Sstevel@tonic-gate 			&& (e = try_load_engine(err, engine, debug)) == NULL)
13650Sstevel@tonic-gate 			{
13660Sstevel@tonic-gate 			BIO_printf(err,"invalid engine \"%s\"\n", engine);
13670Sstevel@tonic-gate 			ERR_print_errors(err);
13680Sstevel@tonic-gate 			return NULL;
13690Sstevel@tonic-gate 			}
13700Sstevel@tonic-gate 		if (debug)
13710Sstevel@tonic-gate 			{
13720Sstevel@tonic-gate 			ENGINE_ctrl(e, ENGINE_CTRL_SET_LOGSTREAM,
13730Sstevel@tonic-gate 				0, err, 0);
13740Sstevel@tonic-gate 			}
13750Sstevel@tonic-gate                 ENGINE_ctrl_cmd(e, "SET_USER_INTERFACE", 0, ui_method, 0, 1);
13760Sstevel@tonic-gate 		if(!ENGINE_set_default(e, ENGINE_METHOD_ALL))
13770Sstevel@tonic-gate 			{
13780Sstevel@tonic-gate 			BIO_printf(err,"can't use that engine\n");
13790Sstevel@tonic-gate 			ERR_print_errors(err);
13800Sstevel@tonic-gate 			ENGINE_free(e);
13810Sstevel@tonic-gate 			return NULL;
13820Sstevel@tonic-gate 			}
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 		BIO_printf(err,"engine \"%s\" set.\n", ENGINE_get_id(e));
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 		/* Free our "structural" reference. */
13870Sstevel@tonic-gate 		ENGINE_free(e);
13880Sstevel@tonic-gate 		}
13890Sstevel@tonic-gate         return e;
13900Sstevel@tonic-gate         }
13910Sstevel@tonic-gate #endif
13920Sstevel@tonic-gate 
load_config(BIO * err,CONF * cnf)13930Sstevel@tonic-gate int load_config(BIO *err, CONF *cnf)
13940Sstevel@tonic-gate 	{
13950Sstevel@tonic-gate 	if (!cnf)
13960Sstevel@tonic-gate 		cnf = config;
13970Sstevel@tonic-gate 	if (!cnf)
13980Sstevel@tonic-gate 		return 1;
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate 	OPENSSL_load_builtin_modules();
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 	if (CONF_modules_load(cnf, NULL, 0) <= 0)
14030Sstevel@tonic-gate 		{
14040Sstevel@tonic-gate 		BIO_printf(err, "Error configuring OpenSSL\n");
14050Sstevel@tonic-gate 		ERR_print_errors(err);
14060Sstevel@tonic-gate 		return 0;
14070Sstevel@tonic-gate 		}
14080Sstevel@tonic-gate 	return 1;
14090Sstevel@tonic-gate 	}
14100Sstevel@tonic-gate 
make_config_name()14110Sstevel@tonic-gate char *make_config_name()
14120Sstevel@tonic-gate 	{
14130Sstevel@tonic-gate 	const char *t=X509_get_default_cert_area();
14140Sstevel@tonic-gate 	size_t len;
14150Sstevel@tonic-gate 	char *p;
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 	len=strlen(t)+strlen(OPENSSL_CONF)+2;
14180Sstevel@tonic-gate 	p=OPENSSL_malloc(len);
14190Sstevel@tonic-gate 	BUF_strlcpy(p,t,len);
14200Sstevel@tonic-gate #ifndef OPENSSL_SYS_VMS
14210Sstevel@tonic-gate 	BUF_strlcat(p,"/",len);
14220Sstevel@tonic-gate #endif
14230Sstevel@tonic-gate 	BUF_strlcat(p,OPENSSL_CONF,len);
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate 	return p;
14260Sstevel@tonic-gate 	}
14270Sstevel@tonic-gate 
index_serial_hash(const char ** a)14280Sstevel@tonic-gate static unsigned long index_serial_hash(const char **a)
14290Sstevel@tonic-gate 	{
14300Sstevel@tonic-gate 	const char *n;
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 	n=a[DB_serial];
14330Sstevel@tonic-gate 	while (*n == '0') n++;
14340Sstevel@tonic-gate 	return(lh_strhash(n));
14350Sstevel@tonic-gate 	}
14360Sstevel@tonic-gate 
index_serial_cmp(const char ** a,const char ** b)14370Sstevel@tonic-gate static int index_serial_cmp(const char **a, const char **b)
14380Sstevel@tonic-gate 	{
14390Sstevel@tonic-gate 	const char *aa,*bb;
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate 	for (aa=a[DB_serial]; *aa == '0'; aa++);
14420Sstevel@tonic-gate 	for (bb=b[DB_serial]; *bb == '0'; bb++);
14430Sstevel@tonic-gate 	return(strcmp(aa,bb));
14440Sstevel@tonic-gate 	}
14450Sstevel@tonic-gate 
index_name_qual(char ** a)14460Sstevel@tonic-gate static int index_name_qual(char **a)
14470Sstevel@tonic-gate 	{ return(a[0][0] == 'V'); }
14480Sstevel@tonic-gate 
index_name_hash(const char ** a)14490Sstevel@tonic-gate static unsigned long index_name_hash(const char **a)
14500Sstevel@tonic-gate 	{ return(lh_strhash(a[DB_name])); }
14510Sstevel@tonic-gate 
index_name_cmp(const char ** a,const char ** b)14520Sstevel@tonic-gate int index_name_cmp(const char **a, const char **b)
14530Sstevel@tonic-gate 	{ return(strcmp(a[DB_name],
14540Sstevel@tonic-gate 	     b[DB_name])); }
14550Sstevel@tonic-gate 
IMPLEMENT_LHASH_HASH_FN(index_serial_hash,const char **)14560Sstevel@tonic-gate static IMPLEMENT_LHASH_HASH_FN(index_serial_hash,const char **)
14570Sstevel@tonic-gate static IMPLEMENT_LHASH_COMP_FN(index_serial_cmp,const char **)
14580Sstevel@tonic-gate static IMPLEMENT_LHASH_HASH_FN(index_name_hash,const char **)
14590Sstevel@tonic-gate static IMPLEMENT_LHASH_COMP_FN(index_name_cmp,const char **)
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate #undef BSIZE
14620Sstevel@tonic-gate #define BSIZE 256
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate BIGNUM *load_serial(char *serialfile, int create, ASN1_INTEGER **retai)
14650Sstevel@tonic-gate 	{
14660Sstevel@tonic-gate 	BIO *in=NULL;
14670Sstevel@tonic-gate 	BIGNUM *ret=NULL;
14680Sstevel@tonic-gate 	MS_STATIC char buf[1024];
14690Sstevel@tonic-gate 	ASN1_INTEGER *ai=NULL;
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 	ai=ASN1_INTEGER_new();
14720Sstevel@tonic-gate 	if (ai == NULL) goto err;
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	if ((in=BIO_new(BIO_s_file())) == NULL)
14750Sstevel@tonic-gate 		{
14760Sstevel@tonic-gate 		ERR_print_errors(bio_err);
14770Sstevel@tonic-gate 		goto err;
14780Sstevel@tonic-gate 		}
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 	if (BIO_read_filename(in,serialfile) <= 0)
14810Sstevel@tonic-gate 		{
14820Sstevel@tonic-gate 		if (!create)
14830Sstevel@tonic-gate 			{
14840Sstevel@tonic-gate 			perror(serialfile);
14850Sstevel@tonic-gate 			goto err;
14860Sstevel@tonic-gate 			}
14870Sstevel@tonic-gate 		else
14880Sstevel@tonic-gate 			{
14890Sstevel@tonic-gate 			ret=BN_new();
1490*2139Sjp161948 			if (ret == NULL || !rand_serial(ret, ai))
14910Sstevel@tonic-gate 				BIO_printf(bio_err, "Out of memory\n");
14920Sstevel@tonic-gate 			}
14930Sstevel@tonic-gate 		}
14940Sstevel@tonic-gate 	else
14950Sstevel@tonic-gate 		{
14960Sstevel@tonic-gate 		if (!a2i_ASN1_INTEGER(in,ai,buf,1024))
14970Sstevel@tonic-gate 			{
14980Sstevel@tonic-gate 			BIO_printf(bio_err,"unable to load number from %s\n",
14990Sstevel@tonic-gate 				serialfile);
15000Sstevel@tonic-gate 			goto err;
15010Sstevel@tonic-gate 			}
15020Sstevel@tonic-gate 		ret=ASN1_INTEGER_to_BN(ai,NULL);
15030Sstevel@tonic-gate 		if (ret == NULL)
15040Sstevel@tonic-gate 			{
15050Sstevel@tonic-gate 			BIO_printf(bio_err,"error converting number from bin to BIGNUM\n");
15060Sstevel@tonic-gate 			goto err;
15070Sstevel@tonic-gate 			}
15080Sstevel@tonic-gate 		}
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 	if (ret && retai)
15110Sstevel@tonic-gate 		{
15120Sstevel@tonic-gate 		*retai = ai;
15130Sstevel@tonic-gate 		ai = NULL;
15140Sstevel@tonic-gate 		}
15150Sstevel@tonic-gate  err:
15160Sstevel@tonic-gate 	if (in != NULL) BIO_free(in);
15170Sstevel@tonic-gate 	if (ai != NULL) ASN1_INTEGER_free(ai);
15180Sstevel@tonic-gate 	return(ret);
15190Sstevel@tonic-gate 	}
15200Sstevel@tonic-gate 
save_serial(char * serialfile,char * suffix,BIGNUM * serial,ASN1_INTEGER ** retai)15210Sstevel@tonic-gate int save_serial(char *serialfile, char *suffix, BIGNUM *serial, ASN1_INTEGER **retai)
15220Sstevel@tonic-gate 	{
15230Sstevel@tonic-gate 	char buf[1][BSIZE];
15240Sstevel@tonic-gate 	BIO *out = NULL;
15250Sstevel@tonic-gate 	int ret=0;
15260Sstevel@tonic-gate 	ASN1_INTEGER *ai=NULL;
15270Sstevel@tonic-gate 	int j;
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	if (suffix == NULL)
15300Sstevel@tonic-gate 		j = strlen(serialfile);
15310Sstevel@tonic-gate 	else
15320Sstevel@tonic-gate 		j = strlen(serialfile) + strlen(suffix) + 1;
15330Sstevel@tonic-gate 	if (j >= BSIZE)
15340Sstevel@tonic-gate 		{
15350Sstevel@tonic-gate 		BIO_printf(bio_err,"file name too long\n");
15360Sstevel@tonic-gate 		goto err;
15370Sstevel@tonic-gate 		}
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate 	if (suffix == NULL)
15400Sstevel@tonic-gate 		BUF_strlcpy(buf[0], serialfile, BSIZE);
15410Sstevel@tonic-gate 	else
15420Sstevel@tonic-gate 		{
15430Sstevel@tonic-gate #ifndef OPENSSL_SYS_VMS
15440Sstevel@tonic-gate 		j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s", serialfile, suffix);
15450Sstevel@tonic-gate #else
15460Sstevel@tonic-gate 		j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", serialfile, suffix);
15470Sstevel@tonic-gate #endif
15480Sstevel@tonic-gate 		}
15490Sstevel@tonic-gate #ifdef RL_DEBUG
15500Sstevel@tonic-gate 	BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[0]);
15510Sstevel@tonic-gate #endif
15520Sstevel@tonic-gate 	out=BIO_new(BIO_s_file());
15530Sstevel@tonic-gate 	if (out == NULL)
15540Sstevel@tonic-gate 		{
15550Sstevel@tonic-gate 		ERR_print_errors(bio_err);
15560Sstevel@tonic-gate 		goto err;
15570Sstevel@tonic-gate 		}
15580Sstevel@tonic-gate 	if (BIO_write_filename(out,buf[0]) <= 0)
15590Sstevel@tonic-gate 		{
15600Sstevel@tonic-gate 		perror(serialfile);
15610Sstevel@tonic-gate 		goto err;
15620Sstevel@tonic-gate 		}
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 	if ((ai=BN_to_ASN1_INTEGER(serial,NULL)) == NULL)
15650Sstevel@tonic-gate 		{
15660Sstevel@tonic-gate 		BIO_printf(bio_err,"error converting serial to ASN.1 format\n");
15670Sstevel@tonic-gate 		goto err;
15680Sstevel@tonic-gate 		}
15690Sstevel@tonic-gate 	i2a_ASN1_INTEGER(out,ai);
15700Sstevel@tonic-gate 	BIO_puts(out,"\n");
15710Sstevel@tonic-gate 	ret=1;
15720Sstevel@tonic-gate 	if (retai)
15730Sstevel@tonic-gate 		{
15740Sstevel@tonic-gate 		*retai = ai;
15750Sstevel@tonic-gate 		ai = NULL;
15760Sstevel@tonic-gate 		}
15770Sstevel@tonic-gate err:
15780Sstevel@tonic-gate 	if (out != NULL) BIO_free_all(out);
15790Sstevel@tonic-gate 	if (ai != NULL) ASN1_INTEGER_free(ai);
15800Sstevel@tonic-gate 	return(ret);
15810Sstevel@tonic-gate 	}
15820Sstevel@tonic-gate 
rotate_serial(char * serialfile,char * new_suffix,char * old_suffix)15830Sstevel@tonic-gate int rotate_serial(char *serialfile, char *new_suffix, char *old_suffix)
15840Sstevel@tonic-gate 	{
15850Sstevel@tonic-gate 	char buf[5][BSIZE];
15860Sstevel@tonic-gate 	int i,j;
15870Sstevel@tonic-gate 	struct stat sb;
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate 	i = strlen(serialfile) + strlen(old_suffix);
15900Sstevel@tonic-gate 	j = strlen(serialfile) + strlen(new_suffix);
15910Sstevel@tonic-gate 	if (i > j) j = i;
15920Sstevel@tonic-gate 	if (j + 1 >= BSIZE)
15930Sstevel@tonic-gate 		{
15940Sstevel@tonic-gate 		BIO_printf(bio_err,"file name too long\n");
15950Sstevel@tonic-gate 		goto err;
15960Sstevel@tonic-gate 		}
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate #ifndef OPENSSL_SYS_VMS
15990Sstevel@tonic-gate 	j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s",
16000Sstevel@tonic-gate 		serialfile, new_suffix);
16010Sstevel@tonic-gate #else
16020Sstevel@tonic-gate 	j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s",
16030Sstevel@tonic-gate 		serialfile, new_suffix);
16040Sstevel@tonic-gate #endif
16050Sstevel@tonic-gate #ifndef OPENSSL_SYS_VMS
16060Sstevel@tonic-gate 	j = BIO_snprintf(buf[1], sizeof buf[1], "%s.%s",
16070Sstevel@tonic-gate 		serialfile, old_suffix);
16080Sstevel@tonic-gate #else
16090Sstevel@tonic-gate 	j = BIO_snprintf(buf[1], sizeof buf[1], "%s-%s",
16100Sstevel@tonic-gate 		serialfile, old_suffix);
16110Sstevel@tonic-gate #endif
16120Sstevel@tonic-gate 	if (stat(serialfile,&sb) < 0)
16130Sstevel@tonic-gate 		{
16140Sstevel@tonic-gate 		if (errno != ENOENT
16150Sstevel@tonic-gate #ifdef ENOTDIR
1616*2139Sjp161948 			&& errno != ENOTDIR
16170Sstevel@tonic-gate #endif
1618*2139Sjp161948 		   )
16190Sstevel@tonic-gate 			goto err;
16200Sstevel@tonic-gate 		}
16210Sstevel@tonic-gate 	else
16220Sstevel@tonic-gate 		{
16230Sstevel@tonic-gate #ifdef RL_DEBUG
16240Sstevel@tonic-gate 		BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
16250Sstevel@tonic-gate 			serialfile, buf[1]);
16260Sstevel@tonic-gate #endif
16270Sstevel@tonic-gate 		if (rename(serialfile,buf[1]) < 0)
16280Sstevel@tonic-gate 			{
16290Sstevel@tonic-gate 			BIO_printf(bio_err,
16300Sstevel@tonic-gate 				"unable to rename %s to %s\n",
16310Sstevel@tonic-gate 				serialfile, buf[1]);
16320Sstevel@tonic-gate 			perror("reason");
16330Sstevel@tonic-gate 			goto err;
16340Sstevel@tonic-gate 			}
16350Sstevel@tonic-gate 		}
16360Sstevel@tonic-gate #ifdef RL_DEBUG
16370Sstevel@tonic-gate 	BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
16380Sstevel@tonic-gate 		buf[0],serialfile);
16390Sstevel@tonic-gate #endif
16400Sstevel@tonic-gate 	if (rename(buf[0],serialfile) < 0)
16410Sstevel@tonic-gate 		{
16420Sstevel@tonic-gate 		BIO_printf(bio_err,
16430Sstevel@tonic-gate 			"unable to rename %s to %s\n",
16440Sstevel@tonic-gate 			buf[0],serialfile);
16450Sstevel@tonic-gate 		perror("reason");
16460Sstevel@tonic-gate 		rename(buf[1],serialfile);
16470Sstevel@tonic-gate 		goto err;
16480Sstevel@tonic-gate 		}
16490Sstevel@tonic-gate 	return 1;
16500Sstevel@tonic-gate  err:
16510Sstevel@tonic-gate 	return 0;
16520Sstevel@tonic-gate 	}
16530Sstevel@tonic-gate 
rand_serial(BIGNUM * b,ASN1_INTEGER * ai)1654*2139Sjp161948 int rand_serial(BIGNUM *b, ASN1_INTEGER *ai)
1655*2139Sjp161948 	{
1656*2139Sjp161948 	BIGNUM *btmp;
1657*2139Sjp161948 	int ret = 0;
1658*2139Sjp161948 	if (b)
1659*2139Sjp161948 		btmp = b;
1660*2139Sjp161948 	else
1661*2139Sjp161948 		btmp = BN_new();
1662*2139Sjp161948 
1663*2139Sjp161948 	if (!btmp)
1664*2139Sjp161948 		return 0;
1665*2139Sjp161948 
1666*2139Sjp161948 	if (!BN_pseudo_rand(btmp, SERIAL_RAND_BITS, 0, 0))
1667*2139Sjp161948 		goto error;
1668*2139Sjp161948 	if (ai && !BN_to_ASN1_INTEGER(btmp, ai))
1669*2139Sjp161948 		goto error;
1670*2139Sjp161948 
1671*2139Sjp161948 	ret = 1;
1672*2139Sjp161948 
1673*2139Sjp161948 	error:
1674*2139Sjp161948 
1675*2139Sjp161948 	if (!b)
1676*2139Sjp161948 		BN_free(btmp);
1677*2139Sjp161948 
1678*2139Sjp161948 	return ret;
1679*2139Sjp161948 	}
1680*2139Sjp161948 
load_index(char * dbfile,DB_ATTR * db_attr)16810Sstevel@tonic-gate CA_DB *load_index(char *dbfile, DB_ATTR *db_attr)
16820Sstevel@tonic-gate 	{
16830Sstevel@tonic-gate 	CA_DB *retdb = NULL;
16840Sstevel@tonic-gate 	TXT_DB *tmpdb = NULL;
16850Sstevel@tonic-gate 	BIO *in = BIO_new(BIO_s_file());
16860Sstevel@tonic-gate 	CONF *dbattr_conf = NULL;
16870Sstevel@tonic-gate 	char buf[1][BSIZE];
16880Sstevel@tonic-gate 	long errorline= -1;
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate 	if (in == NULL)
16910Sstevel@tonic-gate 		{
16920Sstevel@tonic-gate 		ERR_print_errors(bio_err);
16930Sstevel@tonic-gate 		goto err;
16940Sstevel@tonic-gate 		}
16950Sstevel@tonic-gate 	if (BIO_read_filename(in,dbfile) <= 0)
16960Sstevel@tonic-gate 		{
16970Sstevel@tonic-gate 		perror(dbfile);
16980Sstevel@tonic-gate 		BIO_printf(bio_err,"unable to open '%s'\n",dbfile);
16990Sstevel@tonic-gate 		goto err;
17000Sstevel@tonic-gate 		}
17010Sstevel@tonic-gate 	if ((tmpdb = TXT_DB_read(in,DB_NUMBER)) == NULL)
17020Sstevel@tonic-gate 		{
17030Sstevel@tonic-gate 		if (tmpdb != NULL) TXT_DB_free(tmpdb);
17040Sstevel@tonic-gate 		goto err;
17050Sstevel@tonic-gate 		}
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate #ifndef OPENSSL_SYS_VMS
17080Sstevel@tonic-gate 	BIO_snprintf(buf[0], sizeof buf[0], "%s.attr", dbfile);
17090Sstevel@tonic-gate #else
17100Sstevel@tonic-gate 	BIO_snprintf(buf[0], sizeof buf[0], "%s-attr", dbfile);
17110Sstevel@tonic-gate #endif
17120Sstevel@tonic-gate 	dbattr_conf = NCONF_new(NULL);
17130Sstevel@tonic-gate 	if (NCONF_load(dbattr_conf,buf[0],&errorline) <= 0)
17140Sstevel@tonic-gate 		{
17150Sstevel@tonic-gate 		if (errorline > 0)
17160Sstevel@tonic-gate 			{
17170Sstevel@tonic-gate 			BIO_printf(bio_err,
17180Sstevel@tonic-gate 				"error on line %ld of db attribute file '%s'\n"
17190Sstevel@tonic-gate 				,errorline,buf[0]);
17200Sstevel@tonic-gate 			goto err;
17210Sstevel@tonic-gate 			}
17220Sstevel@tonic-gate 		else
17230Sstevel@tonic-gate 			{
17240Sstevel@tonic-gate 			NCONF_free(dbattr_conf);
17250Sstevel@tonic-gate 			dbattr_conf = NULL;
17260Sstevel@tonic-gate 			}
17270Sstevel@tonic-gate 		}
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 	if ((retdb = OPENSSL_malloc(sizeof(CA_DB))) == NULL)
17300Sstevel@tonic-gate 		{
17310Sstevel@tonic-gate 		fprintf(stderr, "Out of memory\n");
17320Sstevel@tonic-gate 		goto err;
17330Sstevel@tonic-gate 		}
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 	retdb->db = tmpdb;
17360Sstevel@tonic-gate 	tmpdb = NULL;
17370Sstevel@tonic-gate 	if (db_attr)
17380Sstevel@tonic-gate 		retdb->attributes = *db_attr;
17390Sstevel@tonic-gate 	else
17400Sstevel@tonic-gate 		{
17410Sstevel@tonic-gate 		retdb->attributes.unique_subject = 1;
17420Sstevel@tonic-gate 		}
17430Sstevel@tonic-gate 
17440Sstevel@tonic-gate 	if (dbattr_conf)
17450Sstevel@tonic-gate 		{
17460Sstevel@tonic-gate 		char *p = NCONF_get_string(dbattr_conf,NULL,"unique_subject");
17470Sstevel@tonic-gate 		if (p)
17480Sstevel@tonic-gate 			{
1749*2139Sjp161948 #ifdef RL_DEBUG
17500Sstevel@tonic-gate 			BIO_printf(bio_err, "DEBUG[load_index]: unique_subject = \"%s\"\n", p);
1751*2139Sjp161948 #endif
1752*2139Sjp161948 			retdb->attributes.unique_subject = parse_yesno(p,1);
17530Sstevel@tonic-gate 			}
17540Sstevel@tonic-gate 		}
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate  err:
17570Sstevel@tonic-gate 	if (dbattr_conf) NCONF_free(dbattr_conf);
17580Sstevel@tonic-gate 	if (tmpdb) TXT_DB_free(tmpdb);
17590Sstevel@tonic-gate 	if (in) BIO_free_all(in);
17600Sstevel@tonic-gate 	return retdb;
17610Sstevel@tonic-gate 	}
17620Sstevel@tonic-gate 
index_index(CA_DB * db)17630Sstevel@tonic-gate int index_index(CA_DB *db)
17640Sstevel@tonic-gate 	{
17650Sstevel@tonic-gate 	if (!TXT_DB_create_index(db->db, DB_serial, NULL,
17660Sstevel@tonic-gate 				LHASH_HASH_FN(index_serial_hash),
17670Sstevel@tonic-gate 				LHASH_COMP_FN(index_serial_cmp)))
17680Sstevel@tonic-gate 		{
17690Sstevel@tonic-gate 		BIO_printf(bio_err,
17700Sstevel@tonic-gate 		  "error creating serial number index:(%ld,%ld,%ld)\n",
17710Sstevel@tonic-gate 		  			db->db->error,db->db->arg1,db->db->arg2);
17720Sstevel@tonic-gate 			return 0;
17730Sstevel@tonic-gate 		}
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 	if (db->attributes.unique_subject
17760Sstevel@tonic-gate 		&& !TXT_DB_create_index(db->db, DB_name, index_name_qual,
17770Sstevel@tonic-gate 			LHASH_HASH_FN(index_name_hash),
17780Sstevel@tonic-gate 			LHASH_COMP_FN(index_name_cmp)))
17790Sstevel@tonic-gate 		{
17800Sstevel@tonic-gate 		BIO_printf(bio_err,"error creating name index:(%ld,%ld,%ld)\n",
17810Sstevel@tonic-gate 			db->db->error,db->db->arg1,db->db->arg2);
17820Sstevel@tonic-gate 		return 0;
17830Sstevel@tonic-gate 		}
17840Sstevel@tonic-gate 	return 1;
17850Sstevel@tonic-gate 	}
17860Sstevel@tonic-gate 
save_index(const char * dbfile,const char * suffix,CA_DB * db)1787*2139Sjp161948 int save_index(const char *dbfile, const char *suffix, CA_DB *db)
17880Sstevel@tonic-gate 	{
17890Sstevel@tonic-gate 	char buf[3][BSIZE];
17900Sstevel@tonic-gate 	BIO *out = BIO_new(BIO_s_file());
17910Sstevel@tonic-gate 	int j;
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 	if (out == NULL)
17940Sstevel@tonic-gate 		{
17950Sstevel@tonic-gate 		ERR_print_errors(bio_err);
17960Sstevel@tonic-gate 		goto err;
17970Sstevel@tonic-gate 		}
17980Sstevel@tonic-gate 
17990Sstevel@tonic-gate 	j = strlen(dbfile) + strlen(suffix);
18000Sstevel@tonic-gate 	if (j + 6 >= BSIZE)
18010Sstevel@tonic-gate 		{
18020Sstevel@tonic-gate 		BIO_printf(bio_err,"file name too long\n");
18030Sstevel@tonic-gate 		goto err;
18040Sstevel@tonic-gate 		}
18050Sstevel@tonic-gate 
18060Sstevel@tonic-gate #ifndef OPENSSL_SYS_VMS
18070Sstevel@tonic-gate 	j = BIO_snprintf(buf[2], sizeof buf[2], "%s.attr", dbfile);
18080Sstevel@tonic-gate #else
18090Sstevel@tonic-gate 	j = BIO_snprintf(buf[2], sizeof buf[2], "%s-attr", dbfile);
18100Sstevel@tonic-gate #endif
18110Sstevel@tonic-gate #ifndef OPENSSL_SYS_VMS
18120Sstevel@tonic-gate 	j = BIO_snprintf(buf[1], sizeof buf[1], "%s.attr.%s", dbfile, suffix);
18130Sstevel@tonic-gate #else
18140Sstevel@tonic-gate 	j = BIO_snprintf(buf[1], sizeof buf[1], "%s-attr-%s", dbfile, suffix);
18150Sstevel@tonic-gate #endif
18160Sstevel@tonic-gate #ifndef OPENSSL_SYS_VMS
18170Sstevel@tonic-gate 	j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s", dbfile, suffix);
18180Sstevel@tonic-gate #else
18190Sstevel@tonic-gate 	j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s", dbfile, suffix);
18200Sstevel@tonic-gate #endif
18210Sstevel@tonic-gate #ifdef RL_DEBUG
18220Sstevel@tonic-gate 	BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[0]);
18230Sstevel@tonic-gate #endif
18240Sstevel@tonic-gate 	if (BIO_write_filename(out,buf[0]) <= 0)
18250Sstevel@tonic-gate 		{
18260Sstevel@tonic-gate 		perror(dbfile);
18270Sstevel@tonic-gate 		BIO_printf(bio_err,"unable to open '%s'\n", dbfile);
18280Sstevel@tonic-gate 		goto err;
18290Sstevel@tonic-gate 		}
18300Sstevel@tonic-gate 	j=TXT_DB_write(out,db->db);
18310Sstevel@tonic-gate 	if (j <= 0) goto err;
18320Sstevel@tonic-gate 
18330Sstevel@tonic-gate 	BIO_free(out);
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate 	out = BIO_new(BIO_s_file());
18360Sstevel@tonic-gate #ifdef RL_DEBUG
18370Sstevel@tonic-gate 	BIO_printf(bio_err, "DEBUG: writing \"%s\"\n", buf[1]);
18380Sstevel@tonic-gate #endif
18390Sstevel@tonic-gate 	if (BIO_write_filename(out,buf[1]) <= 0)
18400Sstevel@tonic-gate 		{
18410Sstevel@tonic-gate 		perror(buf[2]);
18420Sstevel@tonic-gate 		BIO_printf(bio_err,"unable to open '%s'\n", buf[2]);
18430Sstevel@tonic-gate 		goto err;
18440Sstevel@tonic-gate 		}
18450Sstevel@tonic-gate 	BIO_printf(out,"unique_subject = %s\n",
18460Sstevel@tonic-gate 		db->attributes.unique_subject ? "yes" : "no");
18470Sstevel@tonic-gate 	BIO_free(out);
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate 	return 1;
18500Sstevel@tonic-gate  err:
18510Sstevel@tonic-gate 	return 0;
18520Sstevel@tonic-gate 	}
18530Sstevel@tonic-gate 
rotate_index(const char * dbfile,const char * new_suffix,const char * old_suffix)1854*2139Sjp161948 int rotate_index(const char *dbfile, const char *new_suffix, const char *old_suffix)
18550Sstevel@tonic-gate 	{
18560Sstevel@tonic-gate 	char buf[5][BSIZE];
18570Sstevel@tonic-gate 	int i,j;
18580Sstevel@tonic-gate 	struct stat sb;
18590Sstevel@tonic-gate 
18600Sstevel@tonic-gate 	i = strlen(dbfile) + strlen(old_suffix);
18610Sstevel@tonic-gate 	j = strlen(dbfile) + strlen(new_suffix);
18620Sstevel@tonic-gate 	if (i > j) j = i;
18630Sstevel@tonic-gate 	if (j + 6 >= BSIZE)
18640Sstevel@tonic-gate 		{
18650Sstevel@tonic-gate 		BIO_printf(bio_err,"file name too long\n");
18660Sstevel@tonic-gate 		goto err;
18670Sstevel@tonic-gate 		}
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate #ifndef OPENSSL_SYS_VMS
18700Sstevel@tonic-gate 	j = BIO_snprintf(buf[4], sizeof buf[4], "%s.attr", dbfile);
18710Sstevel@tonic-gate #else
18720Sstevel@tonic-gate 	j = BIO_snprintf(buf[4], sizeof buf[4], "%s-attr", dbfile);
18730Sstevel@tonic-gate #endif
18740Sstevel@tonic-gate #ifndef OPENSSL_SYS_VMS
18750Sstevel@tonic-gate 	j = BIO_snprintf(buf[2], sizeof buf[2], "%s.attr.%s",
18760Sstevel@tonic-gate 		dbfile, new_suffix);
18770Sstevel@tonic-gate #else
18780Sstevel@tonic-gate 	j = BIO_snprintf(buf[2], sizeof buf[2], "%s-attr-%s",
18790Sstevel@tonic-gate 		dbfile, new_suffix);
18800Sstevel@tonic-gate #endif
18810Sstevel@tonic-gate #ifndef OPENSSL_SYS_VMS
18820Sstevel@tonic-gate 	j = BIO_snprintf(buf[0], sizeof buf[0], "%s.%s",
18830Sstevel@tonic-gate 		dbfile, new_suffix);
18840Sstevel@tonic-gate #else
18850Sstevel@tonic-gate 	j = BIO_snprintf(buf[0], sizeof buf[0], "%s-%s",
18860Sstevel@tonic-gate 		dbfile, new_suffix);
18870Sstevel@tonic-gate #endif
18880Sstevel@tonic-gate #ifndef OPENSSL_SYS_VMS
18890Sstevel@tonic-gate 	j = BIO_snprintf(buf[1], sizeof buf[1], "%s.%s",
18900Sstevel@tonic-gate 		dbfile, old_suffix);
18910Sstevel@tonic-gate #else
18920Sstevel@tonic-gate 	j = BIO_snprintf(buf[1], sizeof buf[1], "%s-%s",
18930Sstevel@tonic-gate 		dbfile, old_suffix);
18940Sstevel@tonic-gate #endif
18950Sstevel@tonic-gate #ifndef OPENSSL_SYS_VMS
18960Sstevel@tonic-gate 	j = BIO_snprintf(buf[3], sizeof buf[3], "%s.attr.%s",
18970Sstevel@tonic-gate 		dbfile, old_suffix);
18980Sstevel@tonic-gate #else
18990Sstevel@tonic-gate 	j = BIO_snprintf(buf[3], sizeof buf[3], "%s-attr-%s",
19000Sstevel@tonic-gate 		dbfile, old_suffix);
19010Sstevel@tonic-gate #endif
19020Sstevel@tonic-gate 	if (stat(dbfile,&sb) < 0)
19030Sstevel@tonic-gate 		{
19040Sstevel@tonic-gate 		if (errno != ENOENT
19050Sstevel@tonic-gate #ifdef ENOTDIR
1906*2139Sjp161948 			&& errno != ENOTDIR
19070Sstevel@tonic-gate #endif
1908*2139Sjp161948 		   )
19090Sstevel@tonic-gate 			goto err;
19100Sstevel@tonic-gate 		}
19110Sstevel@tonic-gate 	else
19120Sstevel@tonic-gate 		{
19130Sstevel@tonic-gate #ifdef RL_DEBUG
19140Sstevel@tonic-gate 		BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
19150Sstevel@tonic-gate 			dbfile, buf[1]);
19160Sstevel@tonic-gate #endif
19170Sstevel@tonic-gate 		if (rename(dbfile,buf[1]) < 0)
19180Sstevel@tonic-gate 			{
19190Sstevel@tonic-gate 			BIO_printf(bio_err,
19200Sstevel@tonic-gate 				"unable to rename %s to %s\n",
19210Sstevel@tonic-gate 				dbfile, buf[1]);
19220Sstevel@tonic-gate 			perror("reason");
19230Sstevel@tonic-gate 			goto err;
19240Sstevel@tonic-gate 			}
19250Sstevel@tonic-gate 		}
19260Sstevel@tonic-gate #ifdef RL_DEBUG
19270Sstevel@tonic-gate 	BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
19280Sstevel@tonic-gate 		buf[0],dbfile);
19290Sstevel@tonic-gate #endif
19300Sstevel@tonic-gate 	if (rename(buf[0],dbfile) < 0)
19310Sstevel@tonic-gate 		{
19320Sstevel@tonic-gate 		BIO_printf(bio_err,
19330Sstevel@tonic-gate 			"unable to rename %s to %s\n",
19340Sstevel@tonic-gate 			buf[0],dbfile);
19350Sstevel@tonic-gate 		perror("reason");
19360Sstevel@tonic-gate 		rename(buf[1],dbfile);
19370Sstevel@tonic-gate 		goto err;
19380Sstevel@tonic-gate 		}
19390Sstevel@tonic-gate 	if (stat(buf[4],&sb) < 0)
19400Sstevel@tonic-gate 		{
19410Sstevel@tonic-gate 		if (errno != ENOENT
19420Sstevel@tonic-gate #ifdef ENOTDIR
1943*2139Sjp161948 			&& errno != ENOTDIR
19440Sstevel@tonic-gate #endif
1945*2139Sjp161948 		   )
19460Sstevel@tonic-gate 			goto err;
19470Sstevel@tonic-gate 		}
19480Sstevel@tonic-gate 	else
19490Sstevel@tonic-gate 		{
19500Sstevel@tonic-gate #ifdef RL_DEBUG
19510Sstevel@tonic-gate 		BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
19520Sstevel@tonic-gate 			buf[4],buf[3]);
19530Sstevel@tonic-gate #endif
19540Sstevel@tonic-gate 		if (rename(buf[4],buf[3]) < 0)
19550Sstevel@tonic-gate 			{
19560Sstevel@tonic-gate 			BIO_printf(bio_err,
19570Sstevel@tonic-gate 				"unable to rename %s to %s\n",
19580Sstevel@tonic-gate 				buf[4], buf[3]);
19590Sstevel@tonic-gate 			perror("reason");
19600Sstevel@tonic-gate 			rename(dbfile,buf[0]);
19610Sstevel@tonic-gate 			rename(buf[1],dbfile);
19620Sstevel@tonic-gate 			goto err;
19630Sstevel@tonic-gate 			}
19640Sstevel@tonic-gate 		}
19650Sstevel@tonic-gate #ifdef RL_DEBUG
19660Sstevel@tonic-gate 	BIO_printf(bio_err, "DEBUG: renaming \"%s\" to \"%s\"\n",
19670Sstevel@tonic-gate 		buf[2],buf[4]);
19680Sstevel@tonic-gate #endif
19690Sstevel@tonic-gate 	if (rename(buf[2],buf[4]) < 0)
19700Sstevel@tonic-gate 		{
19710Sstevel@tonic-gate 		BIO_printf(bio_err,
19720Sstevel@tonic-gate 			"unable to rename %s to %s\n",
19730Sstevel@tonic-gate 			buf[2],buf[4]);
19740Sstevel@tonic-gate 		perror("reason");
19750Sstevel@tonic-gate 		rename(buf[3],buf[4]);
19760Sstevel@tonic-gate 		rename(dbfile,buf[0]);
19770Sstevel@tonic-gate 		rename(buf[1],dbfile);
19780Sstevel@tonic-gate 		goto err;
19790Sstevel@tonic-gate 		}
19800Sstevel@tonic-gate 	return 1;
19810Sstevel@tonic-gate  err:
19820Sstevel@tonic-gate 	return 0;
19830Sstevel@tonic-gate 	}
19840Sstevel@tonic-gate 
free_index(CA_DB * db)19850Sstevel@tonic-gate void free_index(CA_DB *db)
19860Sstevel@tonic-gate 	{
1987*2139Sjp161948 	if (db)
1988*2139Sjp161948 		{
1989*2139Sjp161948 		if (db->db) TXT_DB_free(db->db);
1990*2139Sjp161948 		OPENSSL_free(db);
1991*2139Sjp161948 		}
1992*2139Sjp161948 	}
1993*2139Sjp161948 
parse_yesno(const char * str,int def)1994*2139Sjp161948 int parse_yesno(const char *str, int def)
1995*2139Sjp161948 	{
1996*2139Sjp161948 	int ret = def;
1997*2139Sjp161948 	if (str)
1998*2139Sjp161948 		{
1999*2139Sjp161948 		switch (*str)
2000*2139Sjp161948 			{
2001*2139Sjp161948 		case 'f': /* false */
2002*2139Sjp161948 		case 'F': /* FALSE */
2003*2139Sjp161948 		case 'n': /* no */
2004*2139Sjp161948 		case 'N': /* NO */
2005*2139Sjp161948 		case '0': /* 0 */
2006*2139Sjp161948 			ret = 0;
2007*2139Sjp161948 			break;
2008*2139Sjp161948 		case 't': /* true */
2009*2139Sjp161948 		case 'T': /* TRUE */
2010*2139Sjp161948 		case 'y': /* yes */
2011*2139Sjp161948 		case 'Y': /* YES */
2012*2139Sjp161948 		case '1': /* 1 */
2013*2139Sjp161948 			ret = 0;
2014*2139Sjp161948 			break;
2015*2139Sjp161948 		default:
2016*2139Sjp161948 			ret = def;
2017*2139Sjp161948 			break;
2018*2139Sjp161948 			}
2019*2139Sjp161948 		}
2020*2139Sjp161948 	return ret;
20210Sstevel@tonic-gate 	}
2022*2139Sjp161948 
2023*2139Sjp161948 /*
2024*2139Sjp161948  * subject is expected to be in the format /type0=value0/type1=value1/type2=...
2025*2139Sjp161948  * where characters may be escaped by \
2026*2139Sjp161948  */
parse_name(char * subject,long chtype,int multirdn)2027*2139Sjp161948 X509_NAME *parse_name(char *subject, long chtype, int multirdn)
2028*2139Sjp161948 	{
2029*2139Sjp161948 	size_t buflen = strlen(subject)+1; /* to copy the types and values into. due to escaping, the copy can only become shorter */
2030*2139Sjp161948 	char *buf = OPENSSL_malloc(buflen);
2031*2139Sjp161948 	size_t max_ne = buflen / 2 + 1; /* maximum number of name elements */
2032*2139Sjp161948 	char **ne_types = OPENSSL_malloc(max_ne * sizeof (char *));
2033*2139Sjp161948 	char **ne_values = OPENSSL_malloc(max_ne * sizeof (char *));
2034*2139Sjp161948 	int *mval = OPENSSL_malloc (max_ne * sizeof (int));
2035*2139Sjp161948 
2036*2139Sjp161948 	char *sp = subject, *bp = buf;
2037*2139Sjp161948 	int i, ne_num = 0;
2038*2139Sjp161948 
2039*2139Sjp161948 	X509_NAME *n = NULL;
2040*2139Sjp161948 	int nid;
2041*2139Sjp161948 
2042*2139Sjp161948 	if (!buf || !ne_types || !ne_values)
2043*2139Sjp161948 		{
2044*2139Sjp161948 		BIO_printf(bio_err, "malloc error\n");
2045*2139Sjp161948 		goto error;
2046*2139Sjp161948 		}
2047*2139Sjp161948 
2048*2139Sjp161948 	if (*subject != '/')
2049*2139Sjp161948 		{
2050*2139Sjp161948 		BIO_printf(bio_err, "Subject does not start with '/'.\n");
2051*2139Sjp161948 		goto error;
2052*2139Sjp161948 		}
2053*2139Sjp161948 	sp++; /* skip leading / */
2054*2139Sjp161948 
2055*2139Sjp161948 	/* no multivalued RDN by default */
2056*2139Sjp161948 	mval[ne_num] = 0;
2057*2139Sjp161948 
2058*2139Sjp161948 	while (*sp)
2059*2139Sjp161948 		{
2060*2139Sjp161948 		/* collect type */
2061*2139Sjp161948 		ne_types[ne_num] = bp;
2062*2139Sjp161948 		while (*sp)
2063*2139Sjp161948 			{
2064*2139Sjp161948 			if (*sp == '\\') /* is there anything to escape in the type...? */
2065*2139Sjp161948 				{
2066*2139Sjp161948 				if (*++sp)
2067*2139Sjp161948 					*bp++ = *sp++;
2068*2139Sjp161948 				else
2069*2139Sjp161948 					{
2070*2139Sjp161948 					BIO_printf(bio_err, "escape character at end of string\n");
2071*2139Sjp161948 					goto error;
2072*2139Sjp161948 					}
2073*2139Sjp161948 				}
2074*2139Sjp161948 			else if (*sp == '=')
2075*2139Sjp161948 				{
2076*2139Sjp161948 				sp++;
2077*2139Sjp161948 				*bp++ = '\0';
2078*2139Sjp161948 				break;
2079*2139Sjp161948 				}
2080*2139Sjp161948 			else
2081*2139Sjp161948 				*bp++ = *sp++;
2082*2139Sjp161948 			}
2083*2139Sjp161948 		if (!*sp)
2084*2139Sjp161948 			{
2085*2139Sjp161948 			BIO_printf(bio_err, "end of string encountered while processing type of subject name element #%d\n", ne_num);
2086*2139Sjp161948 			goto error;
2087*2139Sjp161948 			}
2088*2139Sjp161948 		ne_values[ne_num] = bp;
2089*2139Sjp161948 		while (*sp)
2090*2139Sjp161948 			{
2091*2139Sjp161948 			if (*sp == '\\')
2092*2139Sjp161948 				{
2093*2139Sjp161948 				if (*++sp)
2094*2139Sjp161948 					*bp++ = *sp++;
2095*2139Sjp161948 				else
2096*2139Sjp161948 					{
2097*2139Sjp161948 					BIO_printf(bio_err, "escape character at end of string\n");
2098*2139Sjp161948 					goto error;
2099*2139Sjp161948 					}
2100*2139Sjp161948 				}
2101*2139Sjp161948 			else if (*sp == '/')
2102*2139Sjp161948 				{
2103*2139Sjp161948 				sp++;
2104*2139Sjp161948 				/* no multivalued RDN by default */
2105*2139Sjp161948 				mval[ne_num+1] = 0;
2106*2139Sjp161948 				break;
2107*2139Sjp161948 				}
2108*2139Sjp161948 			else if (*sp == '+' && multirdn)
2109*2139Sjp161948 				{
2110*2139Sjp161948 				/* a not escaped + signals a mutlivalued RDN */
2111*2139Sjp161948 				sp++;
2112*2139Sjp161948 				mval[ne_num+1] = -1;
2113*2139Sjp161948 				break;
2114*2139Sjp161948 				}
2115*2139Sjp161948 			else
2116*2139Sjp161948 				*bp++ = *sp++;
2117*2139Sjp161948 			}
2118*2139Sjp161948 		*bp++ = '\0';
2119*2139Sjp161948 		ne_num++;
2120*2139Sjp161948 		}
2121*2139Sjp161948 
2122*2139Sjp161948 	if (!(n = X509_NAME_new()))
2123*2139Sjp161948 		goto error;
2124*2139Sjp161948 
2125*2139Sjp161948 	for (i = 0; i < ne_num; i++)
2126*2139Sjp161948 		{
2127*2139Sjp161948 		if ((nid=OBJ_txt2nid(ne_types[i])) == NID_undef)
2128*2139Sjp161948 			{
2129*2139Sjp161948 			BIO_printf(bio_err, "Subject Attribute %s has no known NID, skipped\n", ne_types[i]);
2130*2139Sjp161948 			continue;
2131*2139Sjp161948 			}
2132*2139Sjp161948 
2133*2139Sjp161948 		if (!*ne_values[i])
2134*2139Sjp161948 			{
2135*2139Sjp161948 			BIO_printf(bio_err, "No value provided for Subject Attribute %s, skipped\n", ne_types[i]);
2136*2139Sjp161948 			continue;
2137*2139Sjp161948 			}
2138*2139Sjp161948 
2139*2139Sjp161948 		if (!X509_NAME_add_entry_by_NID(n, nid, chtype, (unsigned char*)ne_values[i], -1,-1,mval[i]))
2140*2139Sjp161948 			goto error;
2141*2139Sjp161948 		}
2142*2139Sjp161948 
2143*2139Sjp161948 	OPENSSL_free(ne_values);
2144*2139Sjp161948 	OPENSSL_free(ne_types);
2145*2139Sjp161948 	OPENSSL_free(buf);
2146*2139Sjp161948 	return n;
2147*2139Sjp161948 
2148*2139Sjp161948 error:
2149*2139Sjp161948 	X509_NAME_free(n);
2150*2139Sjp161948 	if (ne_values)
2151*2139Sjp161948 		OPENSSL_free(ne_values);
2152*2139Sjp161948 	if (ne_types)
2153*2139Sjp161948 		OPENSSL_free(ne_types);
2154*2139Sjp161948 	if (buf)
2155*2139Sjp161948 		OPENSSL_free(buf);
2156*2139Sjp161948 	return NULL;
2157*2139Sjp161948 }
2158*2139Sjp161948 
2159*2139Sjp161948 /* This code MUST COME AFTER anything that uses rename() */
2160*2139Sjp161948 #ifdef OPENSSL_SYS_WIN32
WIN32_rename(const char * from,const char * to)2161*2139Sjp161948 int WIN32_rename(const char *from, const char *to)
2162*2139Sjp161948 	{
2163*2139Sjp161948 #ifndef OPENSSL_SYS_WINCE
2164*2139Sjp161948 	/* Windows rename gives an error if 'to' exists, so delete it
2165*2139Sjp161948 	 * first and ignore file not found errror
2166*2139Sjp161948 	 */
2167*2139Sjp161948 	if((remove(to) != 0) && (errno != ENOENT))
2168*2139Sjp161948 		return -1;
2169*2139Sjp161948 #undef rename
2170*2139Sjp161948 	return rename(from, to);
2171*2139Sjp161948 #else
2172*2139Sjp161948 	/* convert strings to UNICODE */
2173*2139Sjp161948 	{
2174*2139Sjp161948 	BOOL result = FALSE;
2175*2139Sjp161948 	WCHAR* wfrom;
2176*2139Sjp161948 	WCHAR* wto;
2177*2139Sjp161948 	int i;
2178*2139Sjp161948 	wfrom = malloc((strlen(from)+1)*2);
2179*2139Sjp161948 	wto = malloc((strlen(to)+1)*2);
2180*2139Sjp161948 	if (wfrom != NULL && wto != NULL)
2181*2139Sjp161948 		{
2182*2139Sjp161948 		for (i=0; i<(int)strlen(from)+1; i++)
2183*2139Sjp161948 			wfrom[i] = (short)from[i];
2184*2139Sjp161948 		for (i=0; i<(int)strlen(to)+1; i++)
2185*2139Sjp161948 			wto[i] = (short)to[i];
2186*2139Sjp161948 		result = MoveFile(wfrom, wto);
2187*2139Sjp161948 		}
2188*2139Sjp161948 	if (wfrom != NULL)
2189*2139Sjp161948 		free(wfrom);
2190*2139Sjp161948 	if (wto != NULL)
2191*2139Sjp161948 		free(wto);
2192*2139Sjp161948 	return result;
2193*2139Sjp161948 	}
2194*2139Sjp161948 #endif
2195*2139Sjp161948 	}
2196*2139Sjp161948 #endif
2197*2139Sjp161948 
args_verify(char *** pargs,int * pargc,int * badarg,BIO * err,X509_VERIFY_PARAM ** pm)2198*2139Sjp161948 int args_verify(char ***pargs, int *pargc,
2199*2139Sjp161948 			int *badarg, BIO *err, X509_VERIFY_PARAM **pm)
2200*2139Sjp161948 	{
2201*2139Sjp161948 	ASN1_OBJECT *otmp = NULL;
2202*2139Sjp161948 	unsigned long flags = 0;
2203*2139Sjp161948 	int i;
2204*2139Sjp161948 	int purpose = 0;
2205*2139Sjp161948 	char **oldargs = *pargs;
2206*2139Sjp161948 	char *arg = **pargs, *argn = (*pargs)[1];
2207*2139Sjp161948 	if (!strcmp(arg, "-policy"))
2208*2139Sjp161948 		{
2209*2139Sjp161948 		if (!argn)
2210*2139Sjp161948 			*badarg = 1;
2211*2139Sjp161948 		else
2212*2139Sjp161948 			{
2213*2139Sjp161948 			otmp = OBJ_txt2obj(argn, 0);
2214*2139Sjp161948 			if (!otmp)
2215*2139Sjp161948 				{
2216*2139Sjp161948 				BIO_printf(err, "Invalid Policy \"%s\"\n",
2217*2139Sjp161948 									argn);
2218*2139Sjp161948 				*badarg = 1;
2219*2139Sjp161948 				}
2220*2139Sjp161948 			}
2221*2139Sjp161948 		(*pargs)++;
2222*2139Sjp161948 		}
2223*2139Sjp161948 	else if (strcmp(arg,"-purpose") == 0)
2224*2139Sjp161948 		{
2225*2139Sjp161948 		X509_PURPOSE *xptmp;
2226*2139Sjp161948 		if (!argn)
2227*2139Sjp161948 			*badarg = 1;
2228*2139Sjp161948 		else
2229*2139Sjp161948 			{
2230*2139Sjp161948 			i = X509_PURPOSE_get_by_sname(argn);
2231*2139Sjp161948 			if(i < 0)
2232*2139Sjp161948 				{
2233*2139Sjp161948 				BIO_printf(err, "unrecognized purpose\n");
2234*2139Sjp161948 				*badarg = 1;
2235*2139Sjp161948 				}
2236*2139Sjp161948 			else
2237*2139Sjp161948 				{
2238*2139Sjp161948 				xptmp = X509_PURPOSE_get0(i);
2239*2139Sjp161948 				purpose = X509_PURPOSE_get_id(xptmp);
2240*2139Sjp161948 				}
2241*2139Sjp161948 			}
2242*2139Sjp161948 		(*pargs)++;
2243*2139Sjp161948 		}
2244*2139Sjp161948 	else if (!strcmp(arg, "-ignore_critical"))
2245*2139Sjp161948 		flags |= X509_V_FLAG_IGNORE_CRITICAL;
2246*2139Sjp161948 	else if (!strcmp(arg, "-issuer_checks"))
2247*2139Sjp161948 		flags |= X509_V_FLAG_CB_ISSUER_CHECK;
2248*2139Sjp161948 	else if (!strcmp(arg, "-crl_check"))
2249*2139Sjp161948 		flags |=  X509_V_FLAG_CRL_CHECK;
2250*2139Sjp161948 	else if (!strcmp(arg, "-crl_check_all"))
2251*2139Sjp161948 		flags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL;
2252*2139Sjp161948 	else if (!strcmp(arg, "-policy_check"))
2253*2139Sjp161948 		flags |= X509_V_FLAG_POLICY_CHECK;
2254*2139Sjp161948 	else if (!strcmp(arg, "-explicit_policy"))
2255*2139Sjp161948 		flags |= X509_V_FLAG_EXPLICIT_POLICY;
2256*2139Sjp161948 	else if (!strcmp(arg, "-x509_strict"))
2257*2139Sjp161948 		flags |= X509_V_FLAG_X509_STRICT;
2258*2139Sjp161948 	else if (!strcmp(arg, "-policy_print"))
2259*2139Sjp161948 		flags |= X509_V_FLAG_NOTIFY_POLICY;
2260*2139Sjp161948 	else
2261*2139Sjp161948 		return 0;
2262*2139Sjp161948 
2263*2139Sjp161948 	if (*badarg)
2264*2139Sjp161948 		{
2265*2139Sjp161948 		if (*pm)
2266*2139Sjp161948 			X509_VERIFY_PARAM_free(*pm);
2267*2139Sjp161948 		*pm = NULL;
2268*2139Sjp161948 		goto end;
2269*2139Sjp161948 		}
2270*2139Sjp161948 
2271*2139Sjp161948 	if (!*pm && !(*pm = X509_VERIFY_PARAM_new()))
2272*2139Sjp161948 		{
2273*2139Sjp161948 		*badarg = 1;
2274*2139Sjp161948 		goto end;
2275*2139Sjp161948 		}
2276*2139Sjp161948 
2277*2139Sjp161948 	if (otmp)
2278*2139Sjp161948 		X509_VERIFY_PARAM_add0_policy(*pm, otmp);
2279*2139Sjp161948 	if (flags)
2280*2139Sjp161948 		X509_VERIFY_PARAM_set_flags(*pm, flags);
2281*2139Sjp161948 
2282*2139Sjp161948 	if (purpose)
2283*2139Sjp161948 		X509_VERIFY_PARAM_set_purpose(*pm, purpose);
2284*2139Sjp161948 
2285*2139Sjp161948 	end:
2286*2139Sjp161948 
2287*2139Sjp161948 	(*pargs)++;
2288*2139Sjp161948 
2289*2139Sjp161948 	if (pargc)
2290*2139Sjp161948 		*pargc -= *pargs - oldargs;
2291*2139Sjp161948 
2292*2139Sjp161948 	return 1;
2293*2139Sjp161948 
2294*2139Sjp161948 	}
2295*2139Sjp161948 
nodes_print(BIO * out,const char * name,STACK_OF (X509_POLICY_NODE)* nodes)2296*2139Sjp161948 static void nodes_print(BIO *out, const char *name,
2297*2139Sjp161948 	STACK_OF(X509_POLICY_NODE) *nodes)
2298*2139Sjp161948 	{
2299*2139Sjp161948 	X509_POLICY_NODE *node;
2300*2139Sjp161948 	int i;
2301*2139Sjp161948 	BIO_printf(out, "%s Policies:", name);
2302*2139Sjp161948 	if (nodes)
2303*2139Sjp161948 		{
2304*2139Sjp161948 		BIO_puts(out, "\n");
2305*2139Sjp161948 		for (i = 0; i < sk_X509_POLICY_NODE_num(nodes); i++)
2306*2139Sjp161948 			{
2307*2139Sjp161948 			node = sk_X509_POLICY_NODE_value(nodes, i);
2308*2139Sjp161948 			X509_POLICY_NODE_print(out, node, 2);
2309*2139Sjp161948 			}
2310*2139Sjp161948 		}
2311*2139Sjp161948 	else
2312*2139Sjp161948 		BIO_puts(out, " <empty>\n");
2313*2139Sjp161948 	}
2314*2139Sjp161948 
policies_print(BIO * out,X509_STORE_CTX * ctx)2315*2139Sjp161948 void policies_print(BIO *out, X509_STORE_CTX *ctx)
2316*2139Sjp161948 	{
2317*2139Sjp161948 	X509_POLICY_TREE *tree;
2318*2139Sjp161948 	int explicit_policy;
2319*2139Sjp161948 	int free_out = 0;
2320*2139Sjp161948 	if (out == NULL)
2321*2139Sjp161948 		{
2322*2139Sjp161948 		out = BIO_new_fp(stderr, BIO_NOCLOSE);
2323*2139Sjp161948 		free_out = 1;
2324*2139Sjp161948 		}
2325*2139Sjp161948 	tree = X509_STORE_CTX_get0_policy_tree(ctx);
2326*2139Sjp161948 	explicit_policy = X509_STORE_CTX_get_explicit_policy(ctx);
2327*2139Sjp161948 
2328*2139Sjp161948 	BIO_printf(out, "Require explicit Policy: %s\n",
2329*2139Sjp161948 				explicit_policy ? "True" : "False");
2330*2139Sjp161948 
2331*2139Sjp161948 	nodes_print(out, "Authority", X509_policy_tree_get0_policies(tree));
2332*2139Sjp161948 	nodes_print(out, "User", X509_policy_tree_get0_user_policies(tree));
2333*2139Sjp161948 	if (free_out)
2334*2139Sjp161948 		BIO_free(out);
2335*2139Sjp161948 	}
2336