xref: /onnv-gate/usr/src/common/openssl/apps/pkcs8.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* pkcs8.c */
2*0Sstevel@tonic-gate /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3*0Sstevel@tonic-gate  * project 1999.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate /* ====================================================================
6*0Sstevel@tonic-gate  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7*0Sstevel@tonic-gate  *
8*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
9*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
10*0Sstevel@tonic-gate  * are met:
11*0Sstevel@tonic-gate  *
12*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
13*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
14*0Sstevel@tonic-gate  *
15*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
16*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
17*0Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
18*0Sstevel@tonic-gate  *    distribution.
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
21*0Sstevel@tonic-gate  *    software must display the following acknowledgment:
22*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
23*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26*0Sstevel@tonic-gate  *    endorse or promote products derived from this software without
27*0Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
28*0Sstevel@tonic-gate  *    licensing@OpenSSL.org.
29*0Sstevel@tonic-gate  *
30*0Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
31*0Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
32*0Sstevel@tonic-gate  *    permission of the OpenSSL Project.
33*0Sstevel@tonic-gate  *
34*0Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
35*0Sstevel@tonic-gate  *    acknowledgment:
36*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
37*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38*0Sstevel@tonic-gate  *
39*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40*0Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42*0Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43*0Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44*0Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45*0Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46*0Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48*0Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49*0Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50*0Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
51*0Sstevel@tonic-gate  * ====================================================================
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
54*0Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
55*0Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
56*0Sstevel@tonic-gate  *
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate #include <stdio.h>
59*0Sstevel@tonic-gate #include <string.h>
60*0Sstevel@tonic-gate #include "apps.h"
61*0Sstevel@tonic-gate #include <openssl/pem.h>
62*0Sstevel@tonic-gate #include <openssl/err.h>
63*0Sstevel@tonic-gate #include <openssl/evp.h>
64*0Sstevel@tonic-gate #include <openssl/pkcs12.h>
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate #define PROG pkcs8_main
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate int MAIN(int, char **);
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate int MAIN(int argc, char **argv)
71*0Sstevel@tonic-gate {
72*0Sstevel@tonic-gate 	ENGINE *e = NULL;
73*0Sstevel@tonic-gate 	char **args, *infile = NULL, *outfile = NULL;
74*0Sstevel@tonic-gate 	char *passargin = NULL, *passargout = NULL;
75*0Sstevel@tonic-gate 	BIO *in = NULL, *out = NULL;
76*0Sstevel@tonic-gate 	int topk8 = 0;
77*0Sstevel@tonic-gate 	int pbe_nid = -1;
78*0Sstevel@tonic-gate 	const EVP_CIPHER *cipher = NULL;
79*0Sstevel@tonic-gate 	int iter = PKCS12_DEFAULT_ITER;
80*0Sstevel@tonic-gate 	int informat, outformat;
81*0Sstevel@tonic-gate 	int p8_broken = PKCS8_OK;
82*0Sstevel@tonic-gate 	int nocrypt = 0;
83*0Sstevel@tonic-gate 	X509_SIG *p8;
84*0Sstevel@tonic-gate 	PKCS8_PRIV_KEY_INFO *p8inf;
85*0Sstevel@tonic-gate 	EVP_PKEY *pkey=NULL;
86*0Sstevel@tonic-gate 	char pass[50], *passin = NULL, *passout = NULL, *p8pass = NULL;
87*0Sstevel@tonic-gate 	int badarg = 0;
88*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
89*0Sstevel@tonic-gate 	char *engine=NULL;
90*0Sstevel@tonic-gate #endif
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	if (bio_err == NULL) bio_err = BIO_new_fp (stderr, BIO_NOCLOSE);
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	if (!load_config(bio_err, NULL))
95*0Sstevel@tonic-gate 		goto end;
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	informat=FORMAT_PEM;
98*0Sstevel@tonic-gate 	outformat=FORMAT_PEM;
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	ERR_load_crypto_strings();
101*0Sstevel@tonic-gate 	OpenSSL_add_all_algorithms();
102*0Sstevel@tonic-gate 	args = argv + 1;
103*0Sstevel@tonic-gate 	while (!badarg && *args && *args[0] == '-') {
104*0Sstevel@tonic-gate 		if (!strcmp(*args,"-v2")) {
105*0Sstevel@tonic-gate 			if (args[1]) {
106*0Sstevel@tonic-gate 				args++;
107*0Sstevel@tonic-gate 				cipher=EVP_get_cipherbyname(*args);
108*0Sstevel@tonic-gate 				if(!cipher) {
109*0Sstevel@tonic-gate 					BIO_printf(bio_err,
110*0Sstevel@tonic-gate 						 "Unknown cipher %s\n", *args);
111*0Sstevel@tonic-gate 					badarg = 1;
112*0Sstevel@tonic-gate 				}
113*0Sstevel@tonic-gate 			} else badarg = 1;
114*0Sstevel@tonic-gate 		} else if (!strcmp(*args,"-v1")) {
115*0Sstevel@tonic-gate 			if (args[1]) {
116*0Sstevel@tonic-gate 				args++;
117*0Sstevel@tonic-gate 				pbe_nid=OBJ_txt2nid(*args);
118*0Sstevel@tonic-gate 				if(pbe_nid == NID_undef) {
119*0Sstevel@tonic-gate 					BIO_printf(bio_err,
120*0Sstevel@tonic-gate 						 "Unknown PBE algorithm %s\n", *args);
121*0Sstevel@tonic-gate 					badarg = 1;
122*0Sstevel@tonic-gate 				}
123*0Sstevel@tonic-gate 			} else badarg = 1;
124*0Sstevel@tonic-gate 		} else if (!strcmp(*args,"-inform")) {
125*0Sstevel@tonic-gate 			if (args[1]) {
126*0Sstevel@tonic-gate 				args++;
127*0Sstevel@tonic-gate 				informat=str2fmt(*args);
128*0Sstevel@tonic-gate 			} else badarg = 1;
129*0Sstevel@tonic-gate 		} else if (!strcmp(*args,"-outform")) {
130*0Sstevel@tonic-gate 			if (args[1]) {
131*0Sstevel@tonic-gate 				args++;
132*0Sstevel@tonic-gate 				outformat=str2fmt(*args);
133*0Sstevel@tonic-gate 			} else badarg = 1;
134*0Sstevel@tonic-gate 		} else if (!strcmp (*args, "-topk8")) topk8 = 1;
135*0Sstevel@tonic-gate 		else if (!strcmp (*args, "-noiter")) iter = 1;
136*0Sstevel@tonic-gate 		else if (!strcmp (*args, "-nocrypt")) nocrypt = 1;
137*0Sstevel@tonic-gate 		else if (!strcmp (*args, "-nooct")) p8_broken = PKCS8_NO_OCTET;
138*0Sstevel@tonic-gate 		else if (!strcmp (*args, "-nsdb")) p8_broken = PKCS8_NS_DB;
139*0Sstevel@tonic-gate 		else if (!strcmp (*args, "-embed")) p8_broken = PKCS8_EMBEDDED_PARAM;
140*0Sstevel@tonic-gate 		else if (!strcmp(*args,"-passin"))
141*0Sstevel@tonic-gate 			{
142*0Sstevel@tonic-gate 			if (!args[1]) goto bad;
143*0Sstevel@tonic-gate 			passargin= *(++args);
144*0Sstevel@tonic-gate 			}
145*0Sstevel@tonic-gate 		else if (!strcmp(*args,"-passout"))
146*0Sstevel@tonic-gate 			{
147*0Sstevel@tonic-gate 			if (!args[1]) goto bad;
148*0Sstevel@tonic-gate 			passargout= *(++args);
149*0Sstevel@tonic-gate 			}
150*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
151*0Sstevel@tonic-gate 		else if (strcmp(*args,"-engine") == 0)
152*0Sstevel@tonic-gate 			{
153*0Sstevel@tonic-gate 			if (!args[1]) goto bad;
154*0Sstevel@tonic-gate 			engine= *(++args);
155*0Sstevel@tonic-gate 			}
156*0Sstevel@tonic-gate #endif
157*0Sstevel@tonic-gate 		else if (!strcmp (*args, "-in")) {
158*0Sstevel@tonic-gate 			if (args[1]) {
159*0Sstevel@tonic-gate 				args++;
160*0Sstevel@tonic-gate 				infile = *args;
161*0Sstevel@tonic-gate 			} else badarg = 1;
162*0Sstevel@tonic-gate 		} else if (!strcmp (*args, "-out")) {
163*0Sstevel@tonic-gate 			if (args[1]) {
164*0Sstevel@tonic-gate 				args++;
165*0Sstevel@tonic-gate 				outfile = *args;
166*0Sstevel@tonic-gate 			} else badarg = 1;
167*0Sstevel@tonic-gate 		} else badarg = 1;
168*0Sstevel@tonic-gate 		args++;
169*0Sstevel@tonic-gate 	}
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 	if (badarg) {
172*0Sstevel@tonic-gate 		bad:
173*0Sstevel@tonic-gate 		BIO_printf(bio_err, "Usage pkcs8 [options]\n");
174*0Sstevel@tonic-gate 		BIO_printf(bio_err, "where options are\n");
175*0Sstevel@tonic-gate 		BIO_printf(bio_err, "-in file        input file\n");
176*0Sstevel@tonic-gate 		BIO_printf(bio_err, "-inform X       input format (DER or PEM)\n");
177*0Sstevel@tonic-gate 		BIO_printf(bio_err, "-passin arg     input file pass phrase source\n");
178*0Sstevel@tonic-gate 		BIO_printf(bio_err, "-outform X      output format (DER or PEM)\n");
179*0Sstevel@tonic-gate 		BIO_printf(bio_err, "-out file       output file\n");
180*0Sstevel@tonic-gate 		BIO_printf(bio_err, "-passout arg    output file pass phrase source\n");
181*0Sstevel@tonic-gate 		BIO_printf(bio_err, "-topk8          output PKCS8 file\n");
182*0Sstevel@tonic-gate 		BIO_printf(bio_err, "-nooct          use (nonstandard) no octet format\n");
183*0Sstevel@tonic-gate 		BIO_printf(bio_err, "-embed          use (nonstandard) embedded DSA parameters format\n");
184*0Sstevel@tonic-gate 		BIO_printf(bio_err, "-nsdb           use (nonstandard) DSA Netscape DB format\n");
185*0Sstevel@tonic-gate 		BIO_printf(bio_err, "-noiter         use 1 as iteration count\n");
186*0Sstevel@tonic-gate 		BIO_printf(bio_err, "-nocrypt        use or expect unencrypted private key\n");
187*0Sstevel@tonic-gate 		BIO_printf(bio_err, "-v2 alg         use PKCS#5 v2.0 and cipher \"alg\"\n");
188*0Sstevel@tonic-gate 		BIO_printf(bio_err, "-v1 obj         use PKCS#5 v1.5 and cipher \"alg\"\n");
189*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
190*0Sstevel@tonic-gate 		BIO_printf(bio_err," -engine e       use engine e, possibly a hardware device.\n");
191*0Sstevel@tonic-gate #endif
192*0Sstevel@tonic-gate 		return (1);
193*0Sstevel@tonic-gate 	}
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
196*0Sstevel@tonic-gate         e = setup_engine(bio_err, engine, 0);
197*0Sstevel@tonic-gate #endif
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 	if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
200*0Sstevel@tonic-gate 		BIO_printf(bio_err, "Error getting passwords\n");
201*0Sstevel@tonic-gate 		return (1);
202*0Sstevel@tonic-gate 	}
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 	if ((pbe_nid == -1) && !cipher) pbe_nid = NID_pbeWithMD5AndDES_CBC;
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	if (infile) {
207*0Sstevel@tonic-gate 		if (!(in = BIO_new_file(infile, "rb"))) {
208*0Sstevel@tonic-gate 			BIO_printf(bio_err,
209*0Sstevel@tonic-gate 				 "Can't open input file %s\n", infile);
210*0Sstevel@tonic-gate 			return (1);
211*0Sstevel@tonic-gate 		}
212*0Sstevel@tonic-gate 	} else in = BIO_new_fp (stdin, BIO_NOCLOSE);
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 	if (outfile) {
215*0Sstevel@tonic-gate 		if (!(out = BIO_new_file (outfile, "wb"))) {
216*0Sstevel@tonic-gate 			BIO_printf(bio_err,
217*0Sstevel@tonic-gate 				 "Can't open output file %s\n", outfile);
218*0Sstevel@tonic-gate 			return (1);
219*0Sstevel@tonic-gate 		}
220*0Sstevel@tonic-gate 	} else {
221*0Sstevel@tonic-gate 		out = BIO_new_fp (stdout, BIO_NOCLOSE);
222*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
223*0Sstevel@tonic-gate 		{
224*0Sstevel@tonic-gate 			BIO *tmpbio = BIO_new(BIO_f_linebuffer());
225*0Sstevel@tonic-gate 			out = BIO_push(tmpbio, out);
226*0Sstevel@tonic-gate 		}
227*0Sstevel@tonic-gate #endif
228*0Sstevel@tonic-gate 	}
229*0Sstevel@tonic-gate 	if (topk8)
230*0Sstevel@tonic-gate 		{
231*0Sstevel@tonic-gate 		BIO_free(in); /* Not needed in this section */
232*0Sstevel@tonic-gate 		pkey = load_key(bio_err, infile, informat, 1,
233*0Sstevel@tonic-gate 			passin, e, "key");
234*0Sstevel@tonic-gate 		if (!pkey) {
235*0Sstevel@tonic-gate 			return (1);
236*0Sstevel@tonic-gate 		}
237*0Sstevel@tonic-gate 		if (!(p8inf = EVP_PKEY2PKCS8_broken(pkey, p8_broken))) {
238*0Sstevel@tonic-gate 			BIO_printf(bio_err, "Error converting key\n");
239*0Sstevel@tonic-gate 			ERR_print_errors(bio_err);
240*0Sstevel@tonic-gate 			return (1);
241*0Sstevel@tonic-gate 		}
242*0Sstevel@tonic-gate 		if(nocrypt) {
243*0Sstevel@tonic-gate 			if(outformat == FORMAT_PEM)
244*0Sstevel@tonic-gate 				PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf);
245*0Sstevel@tonic-gate 			else if(outformat == FORMAT_ASN1)
246*0Sstevel@tonic-gate 				i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf);
247*0Sstevel@tonic-gate 			else {
248*0Sstevel@tonic-gate 				BIO_printf(bio_err, "Bad format specified for key\n");
249*0Sstevel@tonic-gate 				return (1);
250*0Sstevel@tonic-gate 			}
251*0Sstevel@tonic-gate 		} else {
252*0Sstevel@tonic-gate 			if(passout) p8pass = passout;
253*0Sstevel@tonic-gate 			else {
254*0Sstevel@tonic-gate 				p8pass = pass;
255*0Sstevel@tonic-gate 				if (EVP_read_pw_string(pass, sizeof pass, "Enter Encryption Password:", 1))
256*0Sstevel@tonic-gate 					return (1);
257*0Sstevel@tonic-gate 			}
258*0Sstevel@tonic-gate 			app_RAND_load_file(NULL, bio_err, 0);
259*0Sstevel@tonic-gate 			if (!(p8 = PKCS8_encrypt(pbe_nid, cipher,
260*0Sstevel@tonic-gate 					p8pass, strlen(p8pass),
261*0Sstevel@tonic-gate 					NULL, 0, iter, p8inf))) {
262*0Sstevel@tonic-gate 				BIO_printf(bio_err, "Error encrypting key\n");
263*0Sstevel@tonic-gate 				ERR_print_errors(bio_err);
264*0Sstevel@tonic-gate 				return (1);
265*0Sstevel@tonic-gate 			}
266*0Sstevel@tonic-gate 			app_RAND_write_file(NULL, bio_err);
267*0Sstevel@tonic-gate 			if(outformat == FORMAT_PEM)
268*0Sstevel@tonic-gate 				PEM_write_bio_PKCS8(out, p8);
269*0Sstevel@tonic-gate 			else if(outformat == FORMAT_ASN1)
270*0Sstevel@tonic-gate 				i2d_PKCS8_bio(out, p8);
271*0Sstevel@tonic-gate 			else {
272*0Sstevel@tonic-gate 				BIO_printf(bio_err, "Bad format specified for key\n");
273*0Sstevel@tonic-gate 				return (1);
274*0Sstevel@tonic-gate 			}
275*0Sstevel@tonic-gate 			X509_SIG_free(p8);
276*0Sstevel@tonic-gate 		}
277*0Sstevel@tonic-gate 		PKCS8_PRIV_KEY_INFO_free (p8inf);
278*0Sstevel@tonic-gate 		EVP_PKEY_free(pkey);
279*0Sstevel@tonic-gate 		BIO_free_all(out);
280*0Sstevel@tonic-gate 		if(passin) OPENSSL_free(passin);
281*0Sstevel@tonic-gate 		if(passout) OPENSSL_free(passout);
282*0Sstevel@tonic-gate 		return (0);
283*0Sstevel@tonic-gate 	}
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 	if(nocrypt) {
286*0Sstevel@tonic-gate 		if(informat == FORMAT_PEM)
287*0Sstevel@tonic-gate 			p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in,NULL,NULL, NULL);
288*0Sstevel@tonic-gate 		else if(informat == FORMAT_ASN1)
289*0Sstevel@tonic-gate 			p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL);
290*0Sstevel@tonic-gate 		else {
291*0Sstevel@tonic-gate 			BIO_printf(bio_err, "Bad format specified for key\n");
292*0Sstevel@tonic-gate 			return (1);
293*0Sstevel@tonic-gate 		}
294*0Sstevel@tonic-gate 	} else {
295*0Sstevel@tonic-gate 		if(informat == FORMAT_PEM)
296*0Sstevel@tonic-gate 			p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL);
297*0Sstevel@tonic-gate 		else if(informat == FORMAT_ASN1)
298*0Sstevel@tonic-gate 			p8 = d2i_PKCS8_bio(in, NULL);
299*0Sstevel@tonic-gate 		else {
300*0Sstevel@tonic-gate 			BIO_printf(bio_err, "Bad format specified for key\n");
301*0Sstevel@tonic-gate 			return (1);
302*0Sstevel@tonic-gate 		}
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate 		if (!p8) {
305*0Sstevel@tonic-gate 			BIO_printf (bio_err, "Error reading key\n");
306*0Sstevel@tonic-gate 			ERR_print_errors(bio_err);
307*0Sstevel@tonic-gate 			return (1);
308*0Sstevel@tonic-gate 		}
309*0Sstevel@tonic-gate 		if(passin) p8pass = passin;
310*0Sstevel@tonic-gate 		else {
311*0Sstevel@tonic-gate 			p8pass = pass;
312*0Sstevel@tonic-gate 			EVP_read_pw_string(pass, sizeof pass, "Enter Password:", 0);
313*0Sstevel@tonic-gate 		}
314*0Sstevel@tonic-gate 		p8inf = PKCS8_decrypt(p8, p8pass, strlen(p8pass));
315*0Sstevel@tonic-gate 		X509_SIG_free(p8);
316*0Sstevel@tonic-gate 	}
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate 	if (!p8inf) {
319*0Sstevel@tonic-gate 		BIO_printf(bio_err, "Error decrypting key\n");
320*0Sstevel@tonic-gate 		ERR_print_errors(bio_err);
321*0Sstevel@tonic-gate 		return (1);
322*0Sstevel@tonic-gate 	}
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate 	if (!(pkey = EVP_PKCS82PKEY(p8inf))) {
325*0Sstevel@tonic-gate 		BIO_printf(bio_err, "Error converting key\n");
326*0Sstevel@tonic-gate 		ERR_print_errors(bio_err);
327*0Sstevel@tonic-gate 		return (1);
328*0Sstevel@tonic-gate 	}
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 	if (p8inf->broken) {
331*0Sstevel@tonic-gate 		BIO_printf(bio_err, "Warning: broken key encoding: ");
332*0Sstevel@tonic-gate 		switch (p8inf->broken) {
333*0Sstevel@tonic-gate 			case PKCS8_NO_OCTET:
334*0Sstevel@tonic-gate 			BIO_printf(bio_err, "No Octet String in PrivateKey\n");
335*0Sstevel@tonic-gate 			break;
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 			case PKCS8_EMBEDDED_PARAM:
338*0Sstevel@tonic-gate 			BIO_printf(bio_err, "DSA parameters included in PrivateKey\n");
339*0Sstevel@tonic-gate 			break;
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 			case PKCS8_NS_DB:
342*0Sstevel@tonic-gate 			BIO_printf(bio_err, "DSA public key include in PrivateKey\n");
343*0Sstevel@tonic-gate 			break;
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 			default:
346*0Sstevel@tonic-gate 			BIO_printf(bio_err, "Unknown broken type\n");
347*0Sstevel@tonic-gate 			break;
348*0Sstevel@tonic-gate 		}
349*0Sstevel@tonic-gate 	}
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 	PKCS8_PRIV_KEY_INFO_free(p8inf);
352*0Sstevel@tonic-gate 	if(outformat == FORMAT_PEM)
353*0Sstevel@tonic-gate 		PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, passout);
354*0Sstevel@tonic-gate 	else if(outformat == FORMAT_ASN1)
355*0Sstevel@tonic-gate 		i2d_PrivateKey_bio(out, pkey);
356*0Sstevel@tonic-gate 	else {
357*0Sstevel@tonic-gate 		BIO_printf(bio_err, "Bad format specified for key\n");
358*0Sstevel@tonic-gate 			return (1);
359*0Sstevel@tonic-gate 	}
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate 	end:
362*0Sstevel@tonic-gate 	EVP_PKEY_free(pkey);
363*0Sstevel@tonic-gate 	BIO_free_all(out);
364*0Sstevel@tonic-gate 	BIO_free(in);
365*0Sstevel@tonic-gate 	if(passin) OPENSSL_free(passin);
366*0Sstevel@tonic-gate 	if(passout) OPENSSL_free(passout);
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate 	return (0);
369*0Sstevel@tonic-gate }
370