3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
62 #include <openssl/pem.h>
63 #include <openssl/err.h>
64 #include <openssl/evp.h>
65 #include <openssl/pkcs12.h>
67 #define PROG pkcs8_main
69 int MAIN(int, char **);
71 int MAIN(int argc
, char **argv
)
74 char **args
, *infile
= NULL
, *outfile
= NULL
;
75 char *passargin
= NULL
, *passargout
= NULL
;
76 BIO
*in
= NULL
, *out
= NULL
;
79 const EVP_CIPHER
*cipher
= NULL
;
80 int iter
= PKCS12_DEFAULT_ITER
;
81 int informat
, outformat
;
82 int p8_broken
= PKCS8_OK
;
85 PKCS8_PRIV_KEY_INFO
*p8inf
= NULL
;
86 EVP_PKEY
*pkey
= NULL
;
87 char pass
[50], *passin
= NULL
, *passout
= NULL
, *p8pass
= NULL
;
90 #ifndef OPENSSL_NO_ENGINE
95 bio_err
= BIO_new_fp(stderr
, BIO_NOCLOSE
);
97 if (!load_config(bio_err
, NULL
))
100 informat
= FORMAT_PEM
;
101 outformat
= FORMAT_PEM
;
103 ERR_load_crypto_strings();
104 OpenSSL_add_all_algorithms();
106 while (!badarg
&& *args
&& *args
[0] == '-') {
107 if (!strcmp(*args
, "-v2")) {
110 cipher
= EVP_get_cipherbyname(*args
);
112 BIO_printf(bio_err
, "Unknown cipher %s\n", *args
);
117 } else if (!strcmp(*args
, "-v1")) {
120 pbe_nid
= OBJ_txt2nid(*args
);
121 if (pbe_nid
== NID_undef
) {
122 BIO_printf(bio_err
, "Unknown PBE algorithm %s\n", *args
);
127 } else if (!strcmp(*args
, "-v2prf")) {
130 pbe_nid
= OBJ_txt2nid(*args
);
131 if (!EVP_PBE_find(EVP_PBE_TYPE_PRF
, pbe_nid
, NULL
, NULL
, 0)) {
132 BIO_printf(bio_err
, "Unknown PRF algorithm %s\n", *args
);
137 } else if (!strcmp(*args
, "-inform")) {
140 informat
= str2fmt(*args
);
143 } else if (!strcmp(*args
, "-outform")) {
146 outformat
= str2fmt(*args
);
149 } else if (!strcmp(*args
, "-topk8"))
151 else if (!strcmp(*args
, "-noiter"))
153 else if (!strcmp(*args
, "-nocrypt"))
155 else if (!strcmp(*args
, "-nooct"))
156 p8_broken
= PKCS8_NO_OCTET
;
157 else if (!strcmp(*args
, "-nsdb"))
158 p8_broken
= PKCS8_NS_DB
;
159 else if (!strcmp(*args
, "-embed"))
160 p8_broken
= PKCS8_EMBEDDED_PARAM
;
161 else if (!strcmp(*args
, "-passin")) {
164 passargin
= *(++args
);
165 } else if (!strcmp(*args
, "-passout")) {
168 passargout
= *(++args
);
170 #ifndef OPENSSL_NO_ENGINE
171 else if (strcmp(*args
, "-engine") == 0) {
177 else if (!strcmp(*args
, "-in")) {
183 } else if (!strcmp(*args
, "-out")) {
196 BIO_printf(bio_err
, "Usage pkcs8 [options]\n");
197 BIO_printf(bio_err
, "where options are\n");
198 BIO_printf(bio_err
, "-in file input file\n");
199 BIO_printf(bio_err
, "-inform X input format (DER or PEM)\n");
201 "-passin arg input file pass phrase source\n");
202 BIO_printf(bio_err
, "-outform X output format (DER or PEM)\n");
203 BIO_printf(bio_err
, "-out file output file\n");
205 "-passout arg output file pass phrase source\n");
206 BIO_printf(bio_err
, "-topk8 output PKCS8 file\n");
208 "-nooct use (nonstandard) no octet format\n");
210 "-embed use (nonstandard) embedded DSA parameters format\n");
212 "-nsdb use (nonstandard) DSA Netscape DB format\n");
213 BIO_printf(bio_err
, "-noiter use 1 as iteration count\n");
215 "-nocrypt use or expect unencrypted private key\n");
217 "-v2 alg use PKCS#5 v2.0 and cipher \"alg\"\n");
219 "-v1 obj use PKCS#5 v1.5 and cipher \"alg\"\n");
220 #ifndef OPENSSL_NO_ENGINE
222 " -engine e use engine e, possibly a hardware device.\n");
226 #ifndef OPENSSL_NO_ENGINE
227 e
= setup_engine(bio_err
, engine
, 0);
230 if (!app_passwd(bio_err
, passargin
, passargout
, &passin
, &passout
)) {
231 BIO_printf(bio_err
, "Error getting passwords\n");
235 if ((pbe_nid
== -1) && !cipher
)
236 pbe_nid
= NID_pbeWithMD5AndDES_CBC
;
239 if (!(in
= BIO_new_file(infile
, "rb"))) {
240 BIO_printf(bio_err
, "Can't open input file %s\n", infile
);
244 in
= BIO_new_fp(stdin
, BIO_NOCLOSE
);
247 if (!(out
= BIO_new_file(outfile
, "wb"))) {
248 BIO_printf(bio_err
, "Can't open output file %s\n", outfile
);
252 out
= BIO_new_fp(stdout
, BIO_NOCLOSE
);
253 #ifdef OPENSSL_SYS_VMS
255 BIO
*tmpbio
= BIO_new(BIO_f_linebuffer());
256 out
= BIO_push(tmpbio
, out
);
261 pkey
= load_key(bio_err
, infile
, informat
, 1, passin
, e
, "key");
264 if (!(p8inf
= EVP_PKEY2PKCS8_broken(pkey
, p8_broken
))) {
265 BIO_printf(bio_err
, "Error converting key\n");
266 ERR_print_errors(bio_err
);
270 if (outformat
== FORMAT_PEM
)
271 PEM_write_bio_PKCS8_PRIV_KEY_INFO(out
, p8inf
);
272 else if (outformat
== FORMAT_ASN1
)
273 i2d_PKCS8_PRIV_KEY_INFO_bio(out
, p8inf
);
275 BIO_printf(bio_err
, "Bad format specified for key\n");
283 if (EVP_read_pw_string
284 (pass
, sizeof pass
, "Enter Encryption Password:", 1))
287 app_RAND_load_file(NULL
, bio_err
, 0);
288 if (!(p8
= PKCS8_encrypt(pbe_nid
, cipher
,
289 p8pass
, strlen(p8pass
),
290 NULL
, 0, iter
, p8inf
))) {
291 BIO_printf(bio_err
, "Error encrypting key\n");
292 ERR_print_errors(bio_err
);
295 app_RAND_write_file(NULL
, bio_err
);
296 if (outformat
== FORMAT_PEM
)
297 PEM_write_bio_PKCS8(out
, p8
);
298 else if (outformat
== FORMAT_ASN1
)
299 i2d_PKCS8_bio(out
, p8
);
301 BIO_printf(bio_err
, "Bad format specified for key\n");
311 if (informat
== FORMAT_PEM
)
312 p8inf
= PEM_read_bio_PKCS8_PRIV_KEY_INFO(in
, NULL
, NULL
, NULL
);
313 else if (informat
== FORMAT_ASN1
)
314 p8inf
= d2i_PKCS8_PRIV_KEY_INFO_bio(in
, NULL
);
316 BIO_printf(bio_err
, "Bad format specified for key\n");
320 if (informat
== FORMAT_PEM
)
321 p8
= PEM_read_bio_PKCS8(in
, NULL
, NULL
, NULL
);
322 else if (informat
== FORMAT_ASN1
)
323 p8
= d2i_PKCS8_bio(in
, NULL
);
325 BIO_printf(bio_err
, "Bad format specified for key\n");
330 BIO_printf(bio_err
, "Error reading key\n");
331 ERR_print_errors(bio_err
);
338 EVP_read_pw_string(pass
, sizeof pass
, "Enter Password:", 0);
340 p8inf
= PKCS8_decrypt(p8
, p8pass
, strlen(p8pass
));
344 BIO_printf(bio_err
, "Error decrypting key\n");
345 ERR_print_errors(bio_err
);
349 if (!(pkey
= EVP_PKCS82PKEY(p8inf
))) {
350 BIO_printf(bio_err
, "Error converting key\n");
351 ERR_print_errors(bio_err
);
356 BIO_printf(bio_err
, "Warning: broken key encoding: ");
357 switch (p8inf
->broken
) {
359 BIO_printf(bio_err
, "No Octet String in PrivateKey\n");
362 case PKCS8_EMBEDDED_PARAM
:
363 BIO_printf(bio_err
, "DSA parameters included in PrivateKey\n");
367 BIO_printf(bio_err
, "DSA public key include in PrivateKey\n");
370 case PKCS8_NEG_PRIVKEY
:
371 BIO_printf(bio_err
, "DSA private key value is negative\n");
375 BIO_printf(bio_err
, "Unknown broken type\n");
380 if (outformat
== FORMAT_PEM
)
381 PEM_write_bio_PrivateKey(out
, pkey
, NULL
, NULL
, 0, NULL
, passout
);
382 else if (outformat
== FORMAT_ASN1
)
383 i2d_PrivateKey_bio(out
, pkey
);
385 BIO_printf(bio_err
, "Bad format specified for key\n");
392 PKCS8_PRIV_KEY_INFO_free(p8inf
);
397 OPENSSL_free(passin
);
399 OPENSSL_free(passout
);