xref: /titanic_44/usr/src/cmd/cmd-inet/usr.lib/wanboot/p12split/p12split.c (revision d7141854234c22ab8fe0547bf51a2f3a30781870)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2002-2003 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27*d7141854SRobert Mustacchi #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*d7141854SRobert Mustacchi 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <libintl.h>
317c478bd9Sstevel@tonic-gate #include <locale.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/stat.h>
347c478bd9Sstevel@tonic-gate #include <sys/wanboot_impl.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <libinetutil.h>
387c478bd9Sstevel@tonic-gate #include <wanbootutil.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <openssl/crypto.h>
417c478bd9Sstevel@tonic-gate #include <openssl/buffer.h>
427c478bd9Sstevel@tonic-gate #include <openssl/bio.h>
437c478bd9Sstevel@tonic-gate #include <openssl/err.h>
447c478bd9Sstevel@tonic-gate #include <openssl/x509.h>
457c478bd9Sstevel@tonic-gate #include <openssl/x509v3.h>
467c478bd9Sstevel@tonic-gate #include <openssl/pkcs12.h>
477c478bd9Sstevel@tonic-gate #include <openssl/evp.h>
487c478bd9Sstevel@tonic-gate #include <p12aux.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static boolean_t verbose = B_FALSE;	/* When nonzero, do in verbose mode */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /* The following match/cert values require PKCS12 */
537c478bd9Sstevel@tonic-gate static int  matchty;		/* Type of matching do to on input */
547c478bd9Sstevel@tonic-gate static char *k_matchval;	/* localkeyid value to match */
557c478bd9Sstevel@tonic-gate static uint_t k_len;		/* length of k_matchval */
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define	IO_KEYFILE	1	/* Have a separate key file or data */
587c478bd9Sstevel@tonic-gate #define	IO_CERTFILE	2	/* Have a separate cert file or data */
597c478bd9Sstevel@tonic-gate #define	IO_TRUSTFILE	4	/* Have a separate trustanchor file */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static char *input = NULL;	/* Consolidated input file */
627c478bd9Sstevel@tonic-gate static char *key_out = NULL;	/* Key file to be output */
637c478bd9Sstevel@tonic-gate static char *cert_out = NULL;	/* Cert file to be output */
647c478bd9Sstevel@tonic-gate static char *trust_out = NULL;	/* Trust anchor file to be output */
657c478bd9Sstevel@tonic-gate static uint_t outfiles;		/* What files are there for output */
667c478bd9Sstevel@tonic-gate static char *progname;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /* Returns from time_check */
697c478bd9Sstevel@tonic-gate typedef enum {
707c478bd9Sstevel@tonic-gate 	CHK_TIME_OK = 0,		/* Cert in effect and not expired */
717c478bd9Sstevel@tonic-gate 	CHK_TIME_BEFORE_BAD,		/* not_before field is invalid */
727c478bd9Sstevel@tonic-gate 	CHK_TIME_AFTER_BAD,		/* not_after field is invalid */
737c478bd9Sstevel@tonic-gate 	CHK_TIME_IS_BEFORE,		/* Cert not yet in force */
747c478bd9Sstevel@tonic-gate 	CHK_TIME_HAS_EXPIRED		/* Cert has expired */
757c478bd9Sstevel@tonic-gate } time_errs_t;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static int parse_keyid(const char *);
787c478bd9Sstevel@tonic-gate static int do_certs(void);
797c478bd9Sstevel@tonic-gate static int read_files(STACK_OF(X509) **, X509 **, EVP_PKEY **);
807c478bd9Sstevel@tonic-gate static void check_certs(STACK_OF(X509) *, X509 **);
817c478bd9Sstevel@tonic-gate static time_errs_t time_check_print(X509 *);
827c478bd9Sstevel@tonic-gate static time_errs_t time_check(X509 *);
837c478bd9Sstevel@tonic-gate static int write_files(STACK_OF(X509) *, X509 *, EVP_PKEY *);
847c478bd9Sstevel@tonic-gate static int get_ifile(char *, char *, EVP_PKEY **, X509 **, STACK_OF(X509) **);
857c478bd9Sstevel@tonic-gate static int do_ofile(char *, EVP_PKEY *, X509 *, STACK_OF(X509) *);
867c478bd9Sstevel@tonic-gate static void usage(void);
877c478bd9Sstevel@tonic-gate static const char *cryptoerr(void);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)907c478bd9Sstevel@tonic-gate main(int argc, char **argv)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	int	i;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	/*
957c478bd9Sstevel@tonic-gate 	 * Do the necessary magic for localization support.
967c478bd9Sstevel@tonic-gate 	 */
977c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
987c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
997c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1007c478bd9Sstevel@tonic-gate #endif
1017c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	progname = strrchr(argv[0], '/');
1047c478bd9Sstevel@tonic-gate 	if (progname != NULL)
1057c478bd9Sstevel@tonic-gate 		progname++;
1067c478bd9Sstevel@tonic-gate 	else
1077c478bd9Sstevel@tonic-gate 		progname = argv[0];
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	wbku_errinit(progname);
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	matchty = DO_FIRST_PAIR;
1127c478bd9Sstevel@tonic-gate 	while ((i = getopt(argc, argv, "vc:i:k:l:t:")) != -1) {
1137c478bd9Sstevel@tonic-gate 		switch (i) {
1147c478bd9Sstevel@tonic-gate 		case 'v':
1157c478bd9Sstevel@tonic-gate 			verbose = B_TRUE;
1167c478bd9Sstevel@tonic-gate 			break;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 		case 'l':
1197c478bd9Sstevel@tonic-gate 			if (parse_keyid(optarg) < 0)
1207c478bd9Sstevel@tonic-gate 				return (EXIT_FAILURE);
1217c478bd9Sstevel@tonic-gate 			matchty = DO_FIND_KEYID;
1227c478bd9Sstevel@tonic-gate 			break;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 		case 'c':
1257c478bd9Sstevel@tonic-gate 			cert_out = optarg;
1267c478bd9Sstevel@tonic-gate 			outfiles |= IO_CERTFILE;
1277c478bd9Sstevel@tonic-gate 			break;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 		case 'k':
1307c478bd9Sstevel@tonic-gate 			key_out = optarg;
1317c478bd9Sstevel@tonic-gate 			outfiles |= IO_KEYFILE;
1327c478bd9Sstevel@tonic-gate 			break;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 		case 't':
1357c478bd9Sstevel@tonic-gate 			trust_out = optarg;
1367c478bd9Sstevel@tonic-gate 			outfiles |= IO_TRUSTFILE;
1377c478bd9Sstevel@tonic-gate 			break;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		case 'i':
1407c478bd9Sstevel@tonic-gate 			input = optarg;
1417c478bd9Sstevel@tonic-gate 			break;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 		default:
1447c478bd9Sstevel@tonic-gate 			usage();
1457c478bd9Sstevel@tonic-gate 		}
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	if (input == NULL) {
1497c478bd9Sstevel@tonic-gate 		wbku_printerr("no input file specified\n");
1507c478bd9Sstevel@tonic-gate 		usage();
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	/*
1547c478bd9Sstevel@tonic-gate 	 * Need output files.
1557c478bd9Sstevel@tonic-gate 	 */
1567c478bd9Sstevel@tonic-gate 	if (outfiles == 0) {
1577c478bd9Sstevel@tonic-gate 		wbku_printerr("at least one output file must be specified\n");
1587c478bd9Sstevel@tonic-gate 		usage();
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	if (do_certs() < 0)
1627c478bd9Sstevel@tonic-gate 		return (EXIT_FAILURE);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	return (EXIT_SUCCESS);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate static int
parse_keyid(const char * keystr)1687c478bd9Sstevel@tonic-gate parse_keyid(const char *keystr)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	const char 	*rp;
1717c478bd9Sstevel@tonic-gate 	char		*wp;
1727c478bd9Sstevel@tonic-gate 	char		*nkeystr;
1737c478bd9Sstevel@tonic-gate 	uint_t 		nkeystrlen;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	/*
1767c478bd9Sstevel@tonic-gate 	 * In the worst case, we'll need one additional character in our
1777c478bd9Sstevel@tonic-gate 	 * output string -- e.g. "A\0" -> "0A\0"
1787c478bd9Sstevel@tonic-gate 	 */
1797c478bd9Sstevel@tonic-gate 	nkeystrlen = strlen(keystr) + 2;
1807c478bd9Sstevel@tonic-gate 	k_len = (nkeystrlen + 1) / 2;
1817c478bd9Sstevel@tonic-gate 	nkeystr = malloc(nkeystrlen);
1827c478bd9Sstevel@tonic-gate 	k_matchval = malloc(k_len);
1837c478bd9Sstevel@tonic-gate 	if (nkeystr == NULL || k_matchval == NULL) {
1847c478bd9Sstevel@tonic-gate 		free(nkeystr);
1857c478bd9Sstevel@tonic-gate 		free(k_matchval);
1867c478bd9Sstevel@tonic-gate 		wbku_printerr("cannot allocate keyid");
1877c478bd9Sstevel@tonic-gate 		return (-1);
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	/*
1917c478bd9Sstevel@tonic-gate 	 * For convenience, we allow the user to put spaces between each digit
1927c478bd9Sstevel@tonic-gate 	 * when entering it on the command line.  As a result, we need to
1937c478bd9Sstevel@tonic-gate 	 * process it into a format that hexascii_to_octet() can handle.  Note
1947c478bd9Sstevel@tonic-gate 	 * that we're careful to map strings like "AA B CC D" to "AA0BCC0D".
1957c478bd9Sstevel@tonic-gate 	 */
1967c478bd9Sstevel@tonic-gate 	for (rp = keystr, wp = nkeystr; *rp != '\0'; rp++) {
1977c478bd9Sstevel@tonic-gate 		if (*rp == ' ')
1987c478bd9Sstevel@tonic-gate 			continue;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 		if (rp[1] == ' ' || rp[1] == '\0') {
2017c478bd9Sstevel@tonic-gate 			*wp++ = '0';	/* one character sequence; prepend 0 */
2027c478bd9Sstevel@tonic-gate 			*wp++ = *rp;
2037c478bd9Sstevel@tonic-gate 		} else {
2047c478bd9Sstevel@tonic-gate 			*wp++ = *rp++;
2057c478bd9Sstevel@tonic-gate 			*wp++ = *rp;
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 	*wp = '\0';
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	if (hexascii_to_octet(nkeystr, wp - nkeystr, k_matchval, &k_len) != 0) {
2117c478bd9Sstevel@tonic-gate 		free(nkeystr);
2127c478bd9Sstevel@tonic-gate 		free(k_matchval);
2137c478bd9Sstevel@tonic-gate 		wbku_printerr("invalid keyid `%s'\n", keystr);
2147c478bd9Sstevel@tonic-gate 		return (-1);
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	free(nkeystr);
2187c478bd9Sstevel@tonic-gate 	return (0);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate static int
do_certs(void)2227c478bd9Sstevel@tonic-gate do_certs(void)
2237c478bd9Sstevel@tonic-gate {
2247c478bd9Sstevel@tonic-gate 	char *bufp;
2257c478bd9Sstevel@tonic-gate 	STACK_OF(X509) *ta_in = NULL;
2267c478bd9Sstevel@tonic-gate 	EVP_PKEY *pkey_in = NULL;
2277c478bd9Sstevel@tonic-gate 	X509 *xcert_in = NULL;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	sunw_crypto_init();
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	if (read_files(&ta_in, &xcert_in, &pkey_in) < 0)
2327c478bd9Sstevel@tonic-gate 		return (-1);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if (verbose) {
2357c478bd9Sstevel@tonic-gate 		if (xcert_in != NULL) {
2367c478bd9Sstevel@tonic-gate 			(void) printf(gettext("\nMain cert:\n"));
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 			/*
2397c478bd9Sstevel@tonic-gate 			 * sunw_subject_attrs() returns a pointer to
2407c478bd9Sstevel@tonic-gate 			 * memory allocated on our behalf. The same
2417c478bd9Sstevel@tonic-gate 			 * behavior is exhibited by sunw_issuer_attrs().
2427c478bd9Sstevel@tonic-gate 			 */
2437c478bd9Sstevel@tonic-gate 			bufp = sunw_subject_attrs(xcert_in, NULL, 0);
2447c478bd9Sstevel@tonic-gate 			if (bufp != NULL) {
2457c478bd9Sstevel@tonic-gate 				(void) printf(gettext("  Subject: %s\n"),
2467c478bd9Sstevel@tonic-gate 				    bufp);
2477c478bd9Sstevel@tonic-gate 				OPENSSL_free(bufp);
2487c478bd9Sstevel@tonic-gate 			}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 			bufp = sunw_issuer_attrs(xcert_in, NULL, 0);
2517c478bd9Sstevel@tonic-gate 			if (bufp != NULL) {
2527c478bd9Sstevel@tonic-gate 				(void) printf(gettext("  Issuer: %s\n"), bufp);
2537c478bd9Sstevel@tonic-gate 				OPENSSL_free(bufp);
2547c478bd9Sstevel@tonic-gate 			}
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 			(void) sunw_print_times(stdout, PRNT_BOTH, NULL,
2577c478bd9Sstevel@tonic-gate 			    xcert_in);
2587c478bd9Sstevel@tonic-gate 		}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 		if (ta_in != NULL) {
2617c478bd9Sstevel@tonic-gate 			X509 *x;
2627c478bd9Sstevel@tonic-gate 			int i;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 			for (i = 0; i < sk_X509_num(ta_in); i++) {
265*d7141854SRobert Mustacchi 				/* LINTED */
2667c478bd9Sstevel@tonic-gate 				x = sk_X509_value(ta_in, i);
2677c478bd9Sstevel@tonic-gate 				(void) printf(
2687c478bd9Sstevel@tonic-gate 				    gettext("\nTrust Anchor cert %d:\n"), i);
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 				/*
2717c478bd9Sstevel@tonic-gate 				 * sunw_subject_attrs() returns a pointer to
2727c478bd9Sstevel@tonic-gate 				 * memory allocated on our behalf. We get the
2737c478bd9Sstevel@tonic-gate 				 * same behavior from sunw_issuer_attrs().
2747c478bd9Sstevel@tonic-gate 				 */
2757c478bd9Sstevel@tonic-gate 				bufp = sunw_subject_attrs(x, NULL, 0);
2767c478bd9Sstevel@tonic-gate 				if (bufp != NULL) {
2777c478bd9Sstevel@tonic-gate 					(void) printf(
2787c478bd9Sstevel@tonic-gate 					    gettext("  Subject: %s\n"), bufp);
2797c478bd9Sstevel@tonic-gate 					OPENSSL_free(bufp);
2807c478bd9Sstevel@tonic-gate 				}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 				bufp = sunw_issuer_attrs(x, NULL, 0);
2837c478bd9Sstevel@tonic-gate 				if (bufp != NULL) {
2847c478bd9Sstevel@tonic-gate 					(void) printf(
2857c478bd9Sstevel@tonic-gate 					    gettext("  Issuer: %s\n"), bufp);
2867c478bd9Sstevel@tonic-gate 					OPENSSL_free(bufp);
2877c478bd9Sstevel@tonic-gate 				}
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 				(void) sunw_print_times(stdout, PRNT_BOTH,
2907c478bd9Sstevel@tonic-gate 					NULL, x);
2917c478bd9Sstevel@tonic-gate 			}
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	check_certs(ta_in, &xcert_in);
2967c478bd9Sstevel@tonic-gate 	if (xcert_in != NULL && pkey_in != NULL) {
2977c478bd9Sstevel@tonic-gate 		if (sunw_check_keys(xcert_in, pkey_in) == 0) {
2987c478bd9Sstevel@tonic-gate 			wbku_printerr("warning: key and certificate do "
2997c478bd9Sstevel@tonic-gate 			    "not match\n");
3007c478bd9Sstevel@tonic-gate 		}
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	return (write_files(ta_in, xcert_in, pkey_in));
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate static int
read_files(STACK_OF (X509)** t_in,X509 ** c_in,EVP_PKEY ** k_in)3077c478bd9Sstevel@tonic-gate read_files(STACK_OF(X509) **t_in, X509 **c_in, EVP_PKEY **k_in)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	char *i_pass;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	i_pass = getpassphrase(gettext("Enter key password: "));
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	if (get_ifile(input, i_pass, k_in, c_in, t_in) < 0)
3147c478bd9Sstevel@tonic-gate 		return (-1);
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	/*
3177c478bd9Sstevel@tonic-gate 	 * If we are only interested in getting a trust anchor, and if there
3187c478bd9Sstevel@tonic-gate 	 * is no trust anchor but is a regular cert, use it instead.  Do this
3197c478bd9Sstevel@tonic-gate 	 * to handle the insanity with openssl, which requires a matching cert
3207c478bd9Sstevel@tonic-gate 	 * and key in order to write a PKCS12 file.
3217c478bd9Sstevel@tonic-gate 	 */
3227c478bd9Sstevel@tonic-gate 	if (outfiles == IO_TRUSTFILE) {
3237c478bd9Sstevel@tonic-gate 		if (c_in != NULL && *c_in != NULL && t_in != NULL) {
3247c478bd9Sstevel@tonic-gate 			if (*t_in == NULL) {
3257c478bd9Sstevel@tonic-gate 				if ((*t_in = sk_X509_new_null()) == NULL) {
3267c478bd9Sstevel@tonic-gate 					wbku_printerr("out of memory\n");
3277c478bd9Sstevel@tonic-gate 					return (-1);
3287c478bd9Sstevel@tonic-gate 				}
3297c478bd9Sstevel@tonic-gate 			}
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 			if (sk_X509_num(*t_in) == 0) {
3327c478bd9Sstevel@tonic-gate 				if (sk_X509_push(*t_in, *c_in) == 0) {
3337c478bd9Sstevel@tonic-gate 					wbku_printerr("out of memory\n");
3347c478bd9Sstevel@tonic-gate 					return (-1);
3357c478bd9Sstevel@tonic-gate 				}
3367c478bd9Sstevel@tonic-gate 				*c_in = NULL;
3377c478bd9Sstevel@tonic-gate 			}
3387c478bd9Sstevel@tonic-gate 		}
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	if ((outfiles & IO_KEYFILE) && *k_in == NULL) {
3427c478bd9Sstevel@tonic-gate 		wbku_printerr("no matching key found\n");
3437c478bd9Sstevel@tonic-gate 		return (-1);
3447c478bd9Sstevel@tonic-gate 	}
3457c478bd9Sstevel@tonic-gate 	if ((outfiles & IO_CERTFILE) && *c_in == NULL) {
3467c478bd9Sstevel@tonic-gate 		wbku_printerr("no matching certificate found\n");
3477c478bd9Sstevel@tonic-gate 		return (-1);
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate 	if ((outfiles & IO_TRUSTFILE) && *t_in == NULL) {
3507c478bd9Sstevel@tonic-gate 		wbku_printerr("no matching trust anchor found\n");
3517c478bd9Sstevel@tonic-gate 		return (-1);
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	return (0);
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate static void
check_certs(STACK_OF (X509)* ta_in,X509 ** c_in)3587c478bd9Sstevel@tonic-gate check_certs(STACK_OF(X509) *ta_in, X509 **c_in)
3597c478bd9Sstevel@tonic-gate {
3607c478bd9Sstevel@tonic-gate 	X509 *curr;
3617c478bd9Sstevel@tonic-gate 	time_errs_t ret;
3627c478bd9Sstevel@tonic-gate 	int i;
3637c478bd9Sstevel@tonic-gate 	int del_expired = (outfiles != 0);
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	if (c_in != NULL && *c_in != NULL) {
3667c478bd9Sstevel@tonic-gate 		ret = time_check_print(*c_in);
3677c478bd9Sstevel@tonic-gate 		if ((ret != CHK_TIME_OK && ret != CHK_TIME_IS_BEFORE) &&
3687c478bd9Sstevel@tonic-gate 		    del_expired) {
3697c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("  Removing cert\n"));
3707c478bd9Sstevel@tonic-gate 			X509_free(*c_in);
3717c478bd9Sstevel@tonic-gate 			*c_in = NULL;
3727c478bd9Sstevel@tonic-gate 		}
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	if (ta_in == NULL)
3767c478bd9Sstevel@tonic-gate 		return;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	for (i = 0; i < sk_X509_num(ta_in); ) {
379*d7141854SRobert Mustacchi 		/* LINTED */
3807c478bd9Sstevel@tonic-gate 		curr = sk_X509_value(ta_in, i);
3817c478bd9Sstevel@tonic-gate 		ret = time_check_print(curr);
3827c478bd9Sstevel@tonic-gate 		if ((ret != CHK_TIME_OK && ret != CHK_TIME_IS_BEFORE) &&
3837c478bd9Sstevel@tonic-gate 		    del_expired) {
3847c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("  Removing cert\n"));
385*d7141854SRobert Mustacchi 			/* LINTED */
3867c478bd9Sstevel@tonic-gate 			curr = sk_X509_delete(ta_in, i);
3877c478bd9Sstevel@tonic-gate 			X509_free(curr);
3887c478bd9Sstevel@tonic-gate 			continue;
3897c478bd9Sstevel@tonic-gate 		}
3907c478bd9Sstevel@tonic-gate 		i++;
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate }
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate static time_errs_t
time_check_print(X509 * cert)3957c478bd9Sstevel@tonic-gate time_check_print(X509 *cert)
3967c478bd9Sstevel@tonic-gate {
3977c478bd9Sstevel@tonic-gate 	char buf[256];
3987c478bd9Sstevel@tonic-gate 	int ret;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	ret = time_check(cert);
4017c478bd9Sstevel@tonic-gate 	if (ret == CHK_TIME_OK)
4027c478bd9Sstevel@tonic-gate 		return (CHK_TIME_OK);
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("  Subject: %s"),
4057c478bd9Sstevel@tonic-gate 	    sunw_subject_attrs(cert, buf, sizeof (buf)));
4067c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("  Issuer:  %s"),
4077c478bd9Sstevel@tonic-gate 	    sunw_issuer_attrs(cert, buf, sizeof (buf)));
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	switch (ret) {
4107c478bd9Sstevel@tonic-gate 	case CHK_TIME_BEFORE_BAD:
4117c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4127c478bd9Sstevel@tonic-gate 		    gettext("\n  Invalid cert 'not before' field\n"));
4137c478bd9Sstevel@tonic-gate 		break;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	case CHK_TIME_AFTER_BAD:
4167c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4177c478bd9Sstevel@tonic-gate 		    gettext("\n  Invalid cert 'not after' field\n"));
4187c478bd9Sstevel@tonic-gate 		break;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	case CHK_TIME_HAS_EXPIRED:
4217c478bd9Sstevel@tonic-gate 		(void) sunw_print_times(stderr, PRNT_NOT_AFTER,
4227c478bd9Sstevel@tonic-gate 		    gettext("\n  Cert has expired\n"), cert);
4237c478bd9Sstevel@tonic-gate 		break;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	case CHK_TIME_IS_BEFORE:
4267c478bd9Sstevel@tonic-gate 		(void) sunw_print_times(stderr, PRNT_NOT_BEFORE,
4277c478bd9Sstevel@tonic-gate 		    gettext("\n  Warning: cert not yet valid\n"), cert);
4287c478bd9Sstevel@tonic-gate 		break;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	default:
4317c478bd9Sstevel@tonic-gate 		break;
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	return (ret);
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate static time_errs_t
time_check(X509 * cert)4387c478bd9Sstevel@tonic-gate time_check(X509 *cert)
4397c478bd9Sstevel@tonic-gate {
4407c478bd9Sstevel@tonic-gate 	int i;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	i = X509_cmp_time(X509_get_notBefore(cert), NULL);
4437c478bd9Sstevel@tonic-gate 	if (i == 0)
4447c478bd9Sstevel@tonic-gate 		return (CHK_TIME_BEFORE_BAD);
4457c478bd9Sstevel@tonic-gate 	if (i > 0)
4467c478bd9Sstevel@tonic-gate 		return (CHK_TIME_IS_BEFORE);
4477c478bd9Sstevel@tonic-gate 	/* After 'not before' time */
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	i = X509_cmp_time(X509_get_notAfter(cert), NULL);
4507c478bd9Sstevel@tonic-gate 	if (i == 0)
4517c478bd9Sstevel@tonic-gate 		return (CHK_TIME_AFTER_BAD);
4527c478bd9Sstevel@tonic-gate 	if (i < 0)
4537c478bd9Sstevel@tonic-gate 		return (CHK_TIME_HAS_EXPIRED);
4547c478bd9Sstevel@tonic-gate 	return (CHK_TIME_OK);
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate static int
write_files(STACK_OF (X509)* t_out,X509 * c_out,EVP_PKEY * k_out)4587c478bd9Sstevel@tonic-gate write_files(STACK_OF(X509) *t_out, X509 *c_out, EVP_PKEY *k_out)
4597c478bd9Sstevel@tonic-gate {
4607c478bd9Sstevel@tonic-gate 	if (key_out != NULL) {
4617c478bd9Sstevel@tonic-gate 		if (verbose)
4627c478bd9Sstevel@tonic-gate 			(void) printf(gettext("%s: writing key\n"), progname);
4637c478bd9Sstevel@tonic-gate 		if (do_ofile(key_out, k_out, NULL, NULL) < 0)
4647c478bd9Sstevel@tonic-gate 			return (-1);
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	if (cert_out != NULL) {
4687c478bd9Sstevel@tonic-gate 		if (verbose)
4697c478bd9Sstevel@tonic-gate 			(void) printf(gettext("%s: writing cert\n"), progname);
4707c478bd9Sstevel@tonic-gate 		if (do_ofile(cert_out, NULL, c_out, NULL) < 0)
4717c478bd9Sstevel@tonic-gate 			return (-1);
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	if (trust_out != NULL) {
4757c478bd9Sstevel@tonic-gate 		if (verbose)
4767c478bd9Sstevel@tonic-gate 			(void) printf(gettext("%s: writing trust\n"),
4777c478bd9Sstevel@tonic-gate 			    progname);
4787c478bd9Sstevel@tonic-gate 		if (do_ofile(trust_out, NULL, NULL, t_out) < 0)
4797c478bd9Sstevel@tonic-gate 			return (-1);
4807c478bd9Sstevel@tonic-gate 	}
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	return (0);
4837c478bd9Sstevel@tonic-gate }
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate static int
get_ifile(char * name,char * pass,EVP_PKEY ** tmp_k,X509 ** tmp_c,STACK_OF (X509)** tmp_t)4867c478bd9Sstevel@tonic-gate get_ifile(char *name, char *pass, EVP_PKEY **tmp_k, X509 **tmp_c,
4877c478bd9Sstevel@tonic-gate     STACK_OF(X509) **tmp_t)
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	PKCS12		*p12;
4907c478bd9Sstevel@tonic-gate 	FILE		*fp;
4917c478bd9Sstevel@tonic-gate 	int		ret;
4927c478bd9Sstevel@tonic-gate 	struct stat	sbuf;
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	if (stat(name, &sbuf) == 0 && !S_ISREG(sbuf.st_mode)) {
4957c478bd9Sstevel@tonic-gate 		wbku_printerr("%s is not a regular file\n", name);
4967c478bd9Sstevel@tonic-gate 		return (-1);
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	if ((fp = fopen(name, "r")) == NULL) {
5007c478bd9Sstevel@tonic-gate 		wbku_printerr("cannot open input file %s", name);
5017c478bd9Sstevel@tonic-gate 		return (-1);
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	p12 = d2i_PKCS12_fp(fp, NULL);
5057c478bd9Sstevel@tonic-gate 	if (p12 == NULL) {
5067c478bd9Sstevel@tonic-gate 		wbku_printerr("cannot read file %s: %s\n", name, cryptoerr());
5077c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
5087c478bd9Sstevel@tonic-gate 		return (-1);
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	ret = sunw_PKCS12_parse(p12, pass, matchty, k_matchval, k_len,
5137c478bd9Sstevel@tonic-gate 	    NULL, tmp_k, tmp_c, tmp_t);
5147c478bd9Sstevel@tonic-gate 	if (ret <= 0) {
5157c478bd9Sstevel@tonic-gate 		if (ret == 0)
5167c478bd9Sstevel@tonic-gate 			wbku_printerr("cannot find matching cert and key\n");
5177c478bd9Sstevel@tonic-gate 		else
5187c478bd9Sstevel@tonic-gate 			wbku_printerr("cannot parse %s: %s\n", name,
5197c478bd9Sstevel@tonic-gate 			    cryptoerr());
5207c478bd9Sstevel@tonic-gate 		PKCS12_free(p12);
5217c478bd9Sstevel@tonic-gate 		return (-1);
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 	return (0);
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate static int
do_ofile(char * name,EVP_PKEY * pkey,X509 * cert,STACK_OF (X509)* ta)5277c478bd9Sstevel@tonic-gate do_ofile(char *name, EVP_PKEY *pkey, X509 *cert, STACK_OF(X509) *ta)
5287c478bd9Sstevel@tonic-gate {
5297c478bd9Sstevel@tonic-gate 	STACK_OF(EVP_PKEY) *klist = NULL;
5307c478bd9Sstevel@tonic-gate 	STACK_OF(X509)	*clist = NULL;
5317c478bd9Sstevel@tonic-gate 	PKCS12		*p12 = NULL;
5327c478bd9Sstevel@tonic-gate 	int		ret = 0;
5337c478bd9Sstevel@tonic-gate 	FILE		*fp;
5347c478bd9Sstevel@tonic-gate 	struct stat	sbuf;
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	if (stat(name, &sbuf) == 0 && !S_ISREG(sbuf.st_mode)) {
5377c478bd9Sstevel@tonic-gate 		wbku_printerr("%s is not a regular file\n", name);
5387c478bd9Sstevel@tonic-gate 		return (-1);
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	if ((fp = fopen(name, "w")) == NULL) {
5427c478bd9Sstevel@tonic-gate 		wbku_printerr("cannot open output file %s", name);
5437c478bd9Sstevel@tonic-gate 		return (-1);
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	if ((clist = sk_X509_new_null()) == NULL ||
5477c478bd9Sstevel@tonic-gate 	    (klist = sk_EVP_PKEY_new_null()) == NULL) {
5487c478bd9Sstevel@tonic-gate 		wbku_printerr("out of memory\n");
5497c478bd9Sstevel@tonic-gate 		ret = -1;
5507c478bd9Sstevel@tonic-gate 		goto cleanup;
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	if (cert != NULL && sk_X509_push(clist, cert) == 0) {
5547c478bd9Sstevel@tonic-gate 		wbku_printerr("out of memory\n");
5557c478bd9Sstevel@tonic-gate 		ret = -1;
5567c478bd9Sstevel@tonic-gate 		goto cleanup;
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	if (pkey != NULL && sk_EVP_PKEY_push(klist, pkey) == 0) {
5607c478bd9Sstevel@tonic-gate 		wbku_printerr("out of memory\n");
5617c478bd9Sstevel@tonic-gate 		ret = -1;
5627c478bd9Sstevel@tonic-gate 		goto cleanup;
5637c478bd9Sstevel@tonic-gate 	}
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	p12 = sunw_PKCS12_create(WANBOOT_PASSPHRASE, klist, clist, ta);
5667c478bd9Sstevel@tonic-gate 	if (p12 == NULL) {
5677c478bd9Sstevel@tonic-gate 		wbku_printerr("cannot create %s: %s\n", name, cryptoerr());
5687c478bd9Sstevel@tonic-gate 		ret = -1;
5697c478bd9Sstevel@tonic-gate 		goto cleanup;
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	if (i2d_PKCS12_fp(fp, p12) == 0) {
5737c478bd9Sstevel@tonic-gate 		wbku_printerr("cannot write %s: %s\n", name, cryptoerr());
5747c478bd9Sstevel@tonic-gate 		ret = -1;
5757c478bd9Sstevel@tonic-gate 		goto cleanup;
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate cleanup:
5797c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
5807c478bd9Sstevel@tonic-gate 	if (p12 != NULL)
5817c478bd9Sstevel@tonic-gate 		PKCS12_free(p12);
5827c478bd9Sstevel@tonic-gate 	/*
5837c478bd9Sstevel@tonic-gate 	 * Put the cert and pkey off of the stack so that they won't
5847c478bd9Sstevel@tonic-gate 	 * be freed two times.  (If they get left in the stack then
5857c478bd9Sstevel@tonic-gate 	 * they will be freed with the stack.)
5867c478bd9Sstevel@tonic-gate 	 */
5877c478bd9Sstevel@tonic-gate 	if (clist != NULL) {
5887c478bd9Sstevel@tonic-gate 		if (cert != NULL && sk_X509_num(clist) == 1) {
589*d7141854SRobert Mustacchi 			/* LINTED */
5907c478bd9Sstevel@tonic-gate 			(void) sk_X509_delete(clist, 0);
5917c478bd9Sstevel@tonic-gate 		}
5927c478bd9Sstevel@tonic-gate 		sk_X509_pop_free(clist, X509_free);
5937c478bd9Sstevel@tonic-gate 	}
5947c478bd9Sstevel@tonic-gate 	if (klist != NULL) {
5957c478bd9Sstevel@tonic-gate 		if (pkey != NULL && sk_EVP_PKEY_num(klist) == 1) {
596*d7141854SRobert Mustacchi 			/* LINTED */
5977c478bd9Sstevel@tonic-gate 			(void) sk_EVP_PKEY_delete(klist, 0);
5987c478bd9Sstevel@tonic-gate 		}
5997c478bd9Sstevel@tonic-gate 		sk_EVP_PKEY_pop_free(klist, sunw_evp_pkey_free);
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	return (ret);
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate static void
usage(void)6067c478bd9Sstevel@tonic-gate usage(void)
6077c478bd9Sstevel@tonic-gate {
6087c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
6097c478bd9Sstevel@tonic-gate 	    gettext("usage:\n"
6107c478bd9Sstevel@tonic-gate 	    "     %s -i <file> -c <file> -k <file> -t <file> [-l <keyid> -v]\n"
6117c478bd9Sstevel@tonic-gate 	    "\n"),
6127c478bd9Sstevel@tonic-gate 	    progname);
6137c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
6147c478bd9Sstevel@tonic-gate 	    gettext(" where:\n"
6157c478bd9Sstevel@tonic-gate 	    "  -i - input file to be split into component parts and put in\n"
6167c478bd9Sstevel@tonic-gate 	    "       files given by -c, -k and -t\n"
6177c478bd9Sstevel@tonic-gate 	    "  -c - output file for the client certificate\n"
6187c478bd9Sstevel@tonic-gate 	    "  -k - output file for the client private key\n"
6197c478bd9Sstevel@tonic-gate 	    "  -t - output file for the remaining certificates (assumed\n"
6207c478bd9Sstevel@tonic-gate 	    "       to be trust anchors)\n"
6217c478bd9Sstevel@tonic-gate 	    "\n Files are assumed to be pkcs12-format files.\n\n"
6227c478bd9Sstevel@tonic-gate 	    "  -v - verbose\n"
6237c478bd9Sstevel@tonic-gate 	    "  -l - value of 'localkeyid' attribute in client cert and\n"
6247c478bd9Sstevel@tonic-gate 	    "       private key to be selected from the input file.\n\n"));
6257c478bd9Sstevel@tonic-gate 	exit(EXIT_FAILURE);
6267c478bd9Sstevel@tonic-gate }
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate /*
6297c478bd9Sstevel@tonic-gate  * Return a pointer to a static buffer that contains a listing of crypto
6307c478bd9Sstevel@tonic-gate  * errors.  We presume that the user doesn't want more than 8KB of error
6317c478bd9Sstevel@tonic-gate  * messages :-)
6327c478bd9Sstevel@tonic-gate  */
6337c478bd9Sstevel@tonic-gate static const char *
cryptoerr(void)6347c478bd9Sstevel@tonic-gate cryptoerr(void)
6357c478bd9Sstevel@tonic-gate {
6367c478bd9Sstevel@tonic-gate 	static char	errbuf[8192];
6377c478bd9Sstevel@tonic-gate 	ulong_t		err;
6387c478bd9Sstevel@tonic-gate 	const char	*pfile;
6397c478bd9Sstevel@tonic-gate 	int		line;
6407c478bd9Sstevel@tonic-gate 	unsigned int	nerr = 0;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	errbuf[0] = '\0';
6437c478bd9Sstevel@tonic-gate 	while ((err = ERR_get_error_line(&pfile, &line)) != 0) {
6447c478bd9Sstevel@tonic-gate 		if (++nerr > 1)
6457c478bd9Sstevel@tonic-gate 			(void) strlcat(errbuf, "\n\t", sizeof (errbuf));
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 		if (err == (ulong_t)-1) {
6487c478bd9Sstevel@tonic-gate 			(void) strlcat(errbuf, strerror(errno),
6497c478bd9Sstevel@tonic-gate 			    sizeof (errbuf));
6507c478bd9Sstevel@tonic-gate 			break;
6517c478bd9Sstevel@tonic-gate 		}
6527c478bd9Sstevel@tonic-gate 		(void) strlcat(errbuf, ERR_reason_error_string(err),
6537c478bd9Sstevel@tonic-gate 		    sizeof (errbuf));
6547c478bd9Sstevel@tonic-gate 	}
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	return (errbuf);
6577c478bd9Sstevel@tonic-gate }
658