2 * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
14 #include <openssl/pem.h>
15 #include <openssl/err.h>
16 #include <openssl/evp.h>
17 #include <openssl/pkcs12.h>
19 typedef enum OPTION_choice {
20 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
21 OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
22 OPT_TOPK8, OPT_NOITER, OPT_NOCRYPT,
23 #ifndef OPENSSL_NO_SCRYPT
24 OPT_SCRYPT, OPT_SCRYPT_N, OPT_SCRYPT_R, OPT_SCRYPT_P,
26 OPT_V2, OPT_V1, OPT_V2PRF, OPT_ITER, OPT_PASSIN, OPT_PASSOUT
29 OPTIONS pkcs8_options[] = {
30 {"help", OPT_HELP, '-', "Display this summary"},
31 {"inform", OPT_INFORM, 'F', "Input format (DER or PEM)"},
32 {"outform", OPT_OUTFORM, 'F', "Output format (DER or PEM)"},
33 {"in", OPT_IN, '<', "Input file"},
34 {"out", OPT_OUT, '>', "Output file"},
35 {"topk8", OPT_TOPK8, '-', "Output PKCS8 file"},
36 {"noiter", OPT_NOITER, '-', "Use 1 as iteration count"},
37 {"nocrypt", OPT_NOCRYPT, '-', "Use or expect unencrypted private key"},
38 {"v2", OPT_V2, 's', "Use PKCS#5 v2.0 and cipher"},
39 {"v1", OPT_V1, 's', "Use PKCS#5 v1.5 and cipher"},
40 {"v2prf", OPT_V2PRF, 's'},
41 {"iter", OPT_ITER, 'p', "Specify the iteration count"},
42 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
43 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
44 #ifndef OPENSSL_NO_ENGINE
45 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
47 #ifndef OPENSSL_NO_SCRYPT
48 {"scrypt", OPT_SCRYPT, '-', "Use scrypt algorithm"},
49 {"scrypt_N", OPT_SCRYPT_N, 's', "Set scrypt N parameter"},
50 {"scrypt_r", OPT_SCRYPT_R, 's', "Set scrypt r parameter"},
51 {"scrypt_p", OPT_SCRYPT_P, 's', "Set scrypt p parameter"},
56 int pkcs8_main(int argc, char **argv)
58 BIO *in = NULL, *out = NULL;
60 EVP_PKEY *pkey = NULL;
61 PKCS8_PRIV_KEY_INFO *p8inf = NULL;
63 const EVP_CIPHER *cipher = NULL;
64 char *infile = NULL, *outfile = NULL;
65 char *passinarg = NULL, *passoutarg = NULL, *prog;
69 char *passin = NULL, *passout = NULL, *p8pass = NULL;
71 int nocrypt = 0, ret = 1, iter = PKCS12_DEFAULT_ITER;
72 int informat = FORMAT_PEM, outformat = FORMAT_PEM, topk8 = 0, pbe_nid = -1;
74 #ifndef OPENSSL_NO_SCRYPT
75 long scrypt_N = 0, scrypt_r = 0, scrypt_p = 0;
78 prog = opt_init(argc, argv, pkcs8_options);
79 while ((o = opt_next()) != OPT_EOF) {
84 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
87 opt_help(pkcs8_options);
91 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
98 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
114 if (!opt_cipher(opt_arg(), &cipher))
118 pbe_nid = OBJ_txt2nid(opt_arg());
119 if (pbe_nid == NID_undef) {
121 "%s: Unknown PBE algorithm %s\n", prog, opt_arg());
126 pbe_nid = OBJ_txt2nid(opt_arg());
127 if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, pbe_nid, NULL, NULL, 0)) {
129 "%s: Unknown PRF algorithm %s\n", prog, opt_arg());
133 cipher = EVP_aes_256_cbc();
136 if (!opt_int(opt_arg(), &iter))
140 passinarg = opt_arg();
143 passoutarg = opt_arg();
146 e = setup_engine(opt_arg(), 0);
148 #ifndef OPENSSL_NO_SCRYPT
154 cipher = EVP_aes_256_cbc();
157 if (!opt_long(opt_arg(), &scrypt_N) || scrypt_N <= 0)
161 if (!opt_long(opt_arg(), &scrypt_r) || scrypt_r <= 0)
165 if (!opt_long(opt_arg(), &scrypt_p) || scrypt_p <= 0)
171 argc = opt_num_rest();
177 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
178 BIO_printf(bio_err, "Error getting passwords\n");
182 if ((pbe_nid == -1) && cipher == NULL)
183 cipher = EVP_aes_256_cbc();
185 in = bio_open_default(infile, 'r', informat);
188 out = bio_open_owner(outfile, outformat, private);
193 pkey = load_key(infile, informat, 1, passin, e, "key");
196 if ((p8inf = EVP_PKEY2PKCS8(pkey)) == NULL) {
197 BIO_printf(bio_err, "Error converting key\n");
198 ERR_print_errors(bio_err);
203 if (outformat == FORMAT_PEM)
204 PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf);
205 else if (outformat == FORMAT_ASN1)
206 i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf);
208 BIO_printf(bio_err, "Bad format specified for key\n");
214 #ifndef OPENSSL_NO_SCRYPT
215 if (scrypt_N && scrypt_r && scrypt_p)
216 pbe = PKCS5_pbe2_set_scrypt(cipher, NULL, 0, NULL,
217 scrypt_N, scrypt_r, scrypt_p);
220 pbe = PKCS5_pbe2_set_iv(cipher, iter, NULL, 0, NULL,
223 pbe = PKCS5_pbe_set(pbe_nid, iter, NULL, 0);
226 BIO_printf(bio_err, "Error setting PBE algorithm\n");
227 ERR_print_errors(bio_err);
233 #ifndef OPENSSL_NO_UI
235 if (EVP_read_pw_string
236 (pass, sizeof pass, "Enter Encryption Password:", 1)) {
237 X509_ALGOR_free(pbe);
242 BIO_printf(bio_err, "Password required\n");
245 app_RAND_load_file(NULL, 0);
246 p8 = PKCS8_set0_pbe(p8pass, strlen(p8pass), p8inf, pbe);
248 X509_ALGOR_free(pbe);
249 BIO_printf(bio_err, "Error encrypting key\n");
250 ERR_print_errors(bio_err);
253 app_RAND_write_file(NULL);
255 if (outformat == FORMAT_PEM)
256 PEM_write_bio_PKCS8(out, p8);
257 else if (outformat == FORMAT_ASN1)
258 i2d_PKCS8_bio(out, p8);
260 BIO_printf(bio_err, "Bad format specified for key\n");
270 if (informat == FORMAT_PEM)
271 p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in, NULL, NULL, NULL);
272 else if (informat == FORMAT_ASN1)
273 p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL);
275 BIO_printf(bio_err, "Bad format specified for key\n");
279 if (informat == FORMAT_PEM)
280 p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL);
281 else if (informat == FORMAT_ASN1)
282 p8 = d2i_PKCS8_bio(in, NULL);
284 BIO_printf(bio_err, "Bad format specified for key\n");
289 BIO_printf(bio_err, "Error reading key\n");
290 ERR_print_errors(bio_err);
296 #ifndef OPENSSL_NO_UI
298 if (EVP_read_pw_string(pass, sizeof pass, "Enter Password:", 0)) {
299 BIO_printf(bio_err, "Can't read Password\n");
304 BIO_printf(bio_err, "Password required\n");
307 p8inf = PKCS8_decrypt(p8, p8pass, strlen(p8pass));
311 BIO_printf(bio_err, "Error decrypting key\n");
312 ERR_print_errors(bio_err);
316 if ((pkey = EVP_PKCS82PKEY(p8inf)) == NULL) {
317 BIO_printf(bio_err, "Error converting key\n");
318 ERR_print_errors(bio_err);
323 if (outformat == FORMAT_PEM)
324 PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, passout);
325 else if (outformat == FORMAT_ASN1)
326 i2d_PrivateKey_bio(out, pkey);
328 BIO_printf(bio_err, "Bad format specified for key\n");
335 PKCS8_PRIV_KEY_INFO_free(p8inf);
339 OPENSSL_free(passin);
340 OPENSSL_free(passout);