2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
5 /* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * 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 typedef enum OPTION_choice {
68 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
69 OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
70 OPT_TOPK8, OPT_NOITER, OPT_NOCRYPT,
71 #ifndef OPENSSL_NO_SCRYPT
72 OPT_SCRYPT, OPT_SCRYPT_N, OPT_SCRYPT_R, OPT_SCRYPT_P,
74 OPT_V2, OPT_V1, OPT_V2PRF, OPT_ITER, OPT_PASSIN, OPT_PASSOUT
77 OPTIONS pkcs8_options[] = {
78 {"help", OPT_HELP, '-', "Display this summary"},
79 {"inform", OPT_INFORM, 'F', "Input format (DER or PEM)"},
80 {"outform", OPT_OUTFORM, 'F', "Output format (DER or PEM)"},
81 {"in", OPT_IN, '<', "Input file"},
82 {"out", OPT_OUT, '>', "Output file"},
83 {"topk8", OPT_TOPK8, '-', "Output PKCS8 file"},
84 {"noiter", OPT_NOITER, '-', "Use 1 as iteration count"},
85 {"nocrypt", OPT_NOCRYPT, '-', "Use or expect unencrypted private key"},
86 {"v2", OPT_V2, 's', "Use PKCS#5 v2.0 and cipher"},
87 {"v1", OPT_V1, 's', "Use PKCS#5 v1.5 and cipher"},
88 {"v2prf", OPT_V2PRF, 's'},
89 {"iter", OPT_ITER, 'p', "Specify the iteration count"},
90 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
91 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
92 #ifndef OPENSSL_NO_ENGINE
93 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
95 #ifndef OPENSSL_NO_SCRYPT
96 {"scrypt", OPT_SCRYPT, '-', "Use scrypt algorithm"},
97 {"scrypt_N", OPT_SCRYPT_N, 's', "Set scrypt N parameter"},
98 {"scrypt_r", OPT_SCRYPT_R, 's', "Set scrypt r parameter"},
99 {"scrypt_p", OPT_SCRYPT_P, 's', "Set scrypt p parameter"},
104 int pkcs8_main(int argc, char **argv)
106 BIO *in = NULL, *out = NULL;
108 EVP_PKEY *pkey = NULL;
109 PKCS8_PRIV_KEY_INFO *p8inf = NULL;
111 const EVP_CIPHER *cipher = NULL;
112 char *infile = NULL, *outfile = NULL;
113 char *passinarg = NULL, *passoutarg = NULL, *prog;
114 #ifndef OPENSSL_NO_UI
117 char *passin = NULL, *passout = NULL, *p8pass = NULL;
119 int nocrypt = 0, ret = 1, iter = PKCS12_DEFAULT_ITER;
120 int informat = FORMAT_PEM, outformat = FORMAT_PEM, topk8 = 0, pbe_nid = -1;
122 #ifndef OPENSSL_NO_SCRYPT
123 long scrypt_N = 0, scrypt_r = 0, scrypt_p = 0;
126 prog = opt_init(argc, argv, pkcs8_options);
127 while ((o = opt_next()) != OPT_EOF) {
132 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
135 opt_help(pkcs8_options);
139 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
146 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
162 if (!opt_cipher(opt_arg(), &cipher))
166 pbe_nid = OBJ_txt2nid(opt_arg());
167 if (pbe_nid == NID_undef) {
169 "%s: Unknown PBE algorithm %s\n", prog, opt_arg());
174 pbe_nid = OBJ_txt2nid(opt_arg());
175 if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, pbe_nid, NULL, NULL, 0)) {
177 "%s: Unknown PRF algorithm %s\n", prog, opt_arg());
182 if (!opt_int(opt_arg(), &iter))
186 passinarg = opt_arg();
189 passoutarg = opt_arg();
192 e = setup_engine(opt_arg(), 0);
194 #ifndef OPENSSL_NO_SCRYPT
200 cipher = EVP_aes_256_cbc();
203 if (!opt_long(opt_arg(), &scrypt_N) || scrypt_N <= 0)
207 if (!opt_long(opt_arg(), &scrypt_r) || scrypt_r <= 0)
211 if (!opt_long(opt_arg(), &scrypt_p) || scrypt_p <= 0)
217 argc = opt_num_rest();
223 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
224 BIO_printf(bio_err, "Error getting passwords\n");
228 if ((pbe_nid == -1) && !cipher)
229 pbe_nid = NID_pbeWithMD5AndDES_CBC;
231 in = bio_open_default(infile, 'r', informat);
234 out = bio_open_owner(outfile, outformat, private);
239 pkey = load_key(infile, informat, 1, passin, e, "key");
242 if ((p8inf = EVP_PKEY2PKCS8(pkey)) == NULL) {
243 BIO_printf(bio_err, "Error converting key\n");
244 ERR_print_errors(bio_err);
249 if (outformat == FORMAT_PEM)
250 PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf);
251 else if (outformat == FORMAT_ASN1)
252 i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf);
254 BIO_printf(bio_err, "Bad format specified for key\n");
260 #ifndef OPENSSL_NO_SCRYPT
261 if (scrypt_N && scrypt_r && scrypt_p)
262 pbe = PKCS5_pbe2_set_scrypt(cipher, NULL, 0, NULL,
263 scrypt_N, scrypt_r, scrypt_p);
266 pbe = PKCS5_pbe2_set_iv(cipher, iter, NULL, 0, NULL,
269 pbe = PKCS5_pbe_set(pbe_nid, iter, NULL, 0);
272 BIO_printf(bio_err, "Error setting PBE algorithm\n");
273 ERR_print_errors(bio_err);
279 #ifndef OPENSSL_NO_UI
281 if (EVP_read_pw_string
282 (pass, sizeof pass, "Enter Encryption Password:", 1)) {
283 X509_ALGOR_free(pbe);
288 BIO_printf(bio_err, "Password required\n");
291 app_RAND_load_file(NULL, 0);
292 p8 = PKCS8_set0_pbe(p8pass, strlen(p8pass), p8inf, pbe);
294 X509_ALGOR_free(pbe);
295 BIO_printf(bio_err, "Error encrypting key\n");
296 ERR_print_errors(bio_err);
299 app_RAND_write_file(NULL);
301 if (outformat == FORMAT_PEM)
302 PEM_write_bio_PKCS8(out, p8);
303 else if (outformat == FORMAT_ASN1)
304 i2d_PKCS8_bio(out, p8);
306 BIO_printf(bio_err, "Bad format specified for key\n");
316 if (informat == FORMAT_PEM)
317 p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in, NULL, NULL, NULL);
318 else if (informat == FORMAT_ASN1)
319 p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL);
321 BIO_printf(bio_err, "Bad format specified for key\n");
325 if (informat == FORMAT_PEM)
326 p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL);
327 else if (informat == FORMAT_ASN1)
328 p8 = d2i_PKCS8_bio(in, NULL);
330 BIO_printf(bio_err, "Bad format specified for key\n");
335 BIO_printf(bio_err, "Error reading key\n");
336 ERR_print_errors(bio_err);
342 #ifndef OPENSSL_NO_UI
344 EVP_read_pw_string(pass, sizeof pass, "Enter Password:", 0);
347 BIO_printf(bio_err, "Password required\n");
350 p8inf = PKCS8_decrypt(p8, p8pass, strlen(p8pass));
354 BIO_printf(bio_err, "Error decrypting key\n");
355 ERR_print_errors(bio_err);
359 if ((pkey = EVP_PKCS82PKEY(p8inf)) == NULL) {
360 BIO_printf(bio_err, "Error converting key\n");
361 ERR_print_errors(bio_err);
366 if (outformat == FORMAT_PEM)
367 PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, passout);
368 else if (outformat == FORMAT_ASN1)
369 i2d_PrivateKey_bio(out, pkey);
371 BIO_printf(bio_err, "Bad format specified for key\n");
378 PKCS8_PRIV_KEY_INFO_free(p8inf);
382 OPENSSL_free(passin);
383 OPENSSL_free(passout);