]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/pkcs8.c
apps/speed.c: add missing checks for RAND_bytes()
[thirdparty/openssl.git] / apps / pkcs8.c
CommitLineData
0f113f3e 1/*
6738bf14 2 * Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
600dec15 3 *
846e33c7
RS
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
600dec15 8 */
846e33c7 9
600dec15 10#include <stdio.h>
2d7153e8 11#include <stdlib.h>
8c197cc5 12#include <string.h>
20432eae 13#include "apps.h"
dab2cd68 14#include "progs.h"
600dec15
DSH
15#include <openssl/pem.h>
16#include <openssl/err.h>
17#include <openssl/evp.h>
18#include <openssl/pkcs12.h>
19
7e1b7485
RS
20typedef enum OPTION_choice {
21 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
22 OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
54dbf423 23 OPT_TOPK8, OPT_NOITER, OPT_NOCRYPT,
b0809bc8
RS
24#ifndef OPENSSL_NO_SCRYPT
25 OPT_SCRYPT, OPT_SCRYPT_N, OPT_SCRYPT_R, OPT_SCRYPT_P,
26#endif
05dba815 27 OPT_V2, OPT_V1, OPT_V2PRF, OPT_ITER, OPT_PASSIN, OPT_PASSOUT,
3ee1eac2
RS
28 OPT_TRADITIONAL,
29 OPT_R_ENUM
7e1b7485 30} OPTION_CHOICE;
600dec15 31
44c83ebd 32const OPTIONS pkcs8_options[] = {
7e1b7485
RS
33 {"help", OPT_HELP, '-', "Display this summary"},
34 {"inform", OPT_INFORM, 'F', "Input format (DER or PEM)"},
35 {"outform", OPT_OUTFORM, 'F', "Output format (DER or PEM)"},
36 {"in", OPT_IN, '<', "Input file"},
37 {"out", OPT_OUT, '>', "Output file"},
38 {"topk8", OPT_TOPK8, '-', "Output PKCS8 file"},
39 {"noiter", OPT_NOITER, '-', "Use 1 as iteration count"},
40 {"nocrypt", OPT_NOCRYPT, '-', "Use or expect unencrypted private key"},
3ee1eac2 41 OPT_R_OPTIONS,
7e1b7485
RS
42 {"v2", OPT_V2, 's', "Use PKCS#5 v2.0 and cipher"},
43 {"v1", OPT_V1, 's', "Use PKCS#5 v1.5 and cipher"},
12d56b29 44 {"v2prf", OPT_V2PRF, 's', "Set the PRF algorithm to use with PKCS#5 v2.0"},
7e1b7485
RS
45 {"iter", OPT_ITER, 'p', "Specify the iteration count"},
46 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
47 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
05dba815 48 {"traditional", OPT_TRADITIONAL, '-', "use traditional format private key"},
7e1b7485
RS
49#ifndef OPENSSL_NO_ENGINE
50 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
51#endif
b0809bc8 52#ifndef OPENSSL_NO_SCRYPT
0ceb8b74
DSH
53 {"scrypt", OPT_SCRYPT, '-', "Use scrypt algorithm"},
54 {"scrypt_N", OPT_SCRYPT_N, 's', "Set scrypt N parameter"},
55 {"scrypt_r", OPT_SCRYPT_R, 's', "Set scrypt r parameter"},
56 {"scrypt_p", OPT_SCRYPT_P, 's', "Set scrypt p parameter"},
b0809bc8 57#endif
7e1b7485
RS
58 {NULL}
59};
600dec15 60
7e1b7485 61int pkcs8_main(int argc, char **argv)
0f113f3e 62{
0f113f3e 63 BIO *in = NULL, *out = NULL;
7e1b7485 64 ENGINE *e = NULL;
0f113f3e 65 EVP_PKEY *pkey = NULL;
7e1b7485
RS
66 PKCS8_PRIV_KEY_INFO *p8inf = NULL;
67 X509_SIG *p8 = NULL;
68 const EVP_CIPHER *cipher = NULL;
333b070e 69 char *infile = NULL, *outfile = NULL;
7e1b7485 70 char *passinarg = NULL, *passoutarg = NULL, *prog;
48feaceb 71#ifndef OPENSSL_NO_UI_CONSOLE
bf580d5f 72 char pass[APP_PASS_LEN];
923b1857
RL
73#endif
74 char *passin = NULL, *passout = NULL, *p8pass = NULL;
7e1b7485 75 OPTION_CHOICE o;
54dbf423 76 int nocrypt = 0, ret = 1, iter = PKCS12_DEFAULT_ITER;
333b070e 77 int informat = FORMAT_PEM, outformat = FORMAT_PEM, topk8 = 0, pbe_nid = -1;
05dba815 78 int private = 0, traditional = 0;
b0809bc8 79#ifndef OPENSSL_NO_SCRYPT
bd4850df 80 long scrypt_N = 0, scrypt_r = 0, scrypt_p = 0;
b0809bc8 81#endif
3647bee2 82
7e1b7485
RS
83 prog = opt_init(argc, argv, pkcs8_options);
84 while ((o = opt_next()) != OPT_EOF) {
85 switch (o) {
86 case OPT_EOF:
87 case OPT_ERR:
88 opthelp:
89 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
90 goto end;
91 case OPT_HELP:
92 opt_help(pkcs8_options);
93 ret = 0;
94 goto end;
95 case OPT_INFORM:
96 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
97 goto opthelp;
98 break;
99 case OPT_IN:
100 infile = opt_arg();
101 break;
102 case OPT_OUTFORM:
103 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
104 goto opthelp;
105 break;
106 case OPT_OUT:
107 outfile = opt_arg();
108 break;
109 case OPT_TOPK8:
0f113f3e 110 topk8 = 1;
7e1b7485
RS
111 break;
112 case OPT_NOITER:
0f113f3e 113 iter = 1;
7e1b7485
RS
114 break;
115 case OPT_NOCRYPT:
0f113f3e 116 nocrypt = 1;
7e1b7485 117 break;
3ee1eac2
RS
118 case OPT_R_CASES:
119 if (!opt_rand(o))
120 goto end;
121 break;
05dba815
DSH
122 case OPT_TRADITIONAL:
123 traditional = 1;
124 break;
7e1b7485
RS
125 case OPT_V2:
126 if (!opt_cipher(opt_arg(), &cipher))
127 goto opthelp;
128 break;
129 case OPT_V1:
130 pbe_nid = OBJ_txt2nid(opt_arg());
131 if (pbe_nid == NID_undef) {
132 BIO_printf(bio_err,
133 "%s: Unknown PBE algorithm %s\n", prog, opt_arg());
134 goto opthelp;
135 }
136 break;
137 case OPT_V2PRF:
138 pbe_nid = OBJ_txt2nid(opt_arg());
139 if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, pbe_nid, NULL, NULL, 0)) {
140 BIO_printf(bio_err,
141 "%s: Unknown PRF algorithm %s\n", prog, opt_arg());
142 goto opthelp;
143 }
8fc06e88
DSH
144 if (cipher == NULL)
145 cipher = EVP_aes_256_cbc();
7e1b7485
RS
146 break;
147 case OPT_ITER:
148 if (!opt_int(opt_arg(), &iter))
149 goto opthelp;
150 break;
151 case OPT_PASSIN:
152 passinarg = opt_arg();
153 break;
154 case OPT_PASSOUT:
155 passoutarg = opt_arg();
156 break;
157 case OPT_ENGINE:
333b070e 158 e = setup_engine(opt_arg(), 0);
7e1b7485 159 break;
b0809bc8 160#ifndef OPENSSL_NO_SCRYPT
0ceb8b74 161 case OPT_SCRYPT:
5fc3ee4b 162 scrypt_N = 16384;
0ceb8b74 163 scrypt_r = 8;
5fc3ee4b 164 scrypt_p = 1;
0ceb8b74
DSH
165 if (cipher == NULL)
166 cipher = EVP_aes_256_cbc();
167 break;
168 case OPT_SCRYPT_N:
bd4850df 169 if (!opt_long(opt_arg(), &scrypt_N) || scrypt_N <= 0)
0ceb8b74
DSH
170 goto opthelp;
171 break;
172 case OPT_SCRYPT_R:
bd4850df 173 if (!opt_long(opt_arg(), &scrypt_r) || scrypt_r <= 0)
0ceb8b74
DSH
174 goto opthelp;
175 break;
176 case OPT_SCRYPT_P:
bd4850df 177 if (!opt_long(opt_arg(), &scrypt_p) || scrypt_p <= 0)
0ceb8b74
DSH
178 goto opthelp;
179 break;
b0809bc8 180#endif
0f113f3e 181 }
0f113f3e 182 }
7e1b7485 183 argc = opt_num_rest();
03358517
KR
184 if (argc != 0)
185 goto opthelp;
186
3b061a00 187 private = 1;
600dec15 188
7e1b7485 189 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
0f113f3e
MC
190 BIO_printf(bio_err, "Error getting passwords\n");
191 goto end;
192 }
a3fe382e 193
8fc06e88
DSH
194 if ((pbe_nid == -1) && cipher == NULL)
195 cipher = EVP_aes_256_cbc();
600dec15 196
bdd58d98 197 in = bio_open_default(infile, 'r', informat);
7e1b7485
RS
198 if (in == NULL)
199 goto end;
bdd58d98 200 out = bio_open_owner(outfile, outformat, private);
7e1b7485
RS
201 if (out == NULL)
202 goto end;
3b061a00 203
0f113f3e 204 if (topk8) {
7e1b7485 205 pkey = load_key(infile, informat, 1, passin, e, "key");
2234212c 206 if (pkey == NULL)
0f113f3e 207 goto end;
54dbf423 208 if ((p8inf = EVP_PKEY2PKCS8(pkey)) == NULL) {
0f113f3e
MC
209 BIO_printf(bio_err, "Error converting key\n");
210 ERR_print_errors(bio_err);
211 goto end;
212 }
213 if (nocrypt) {
3b061a00 214 assert(private);
2234212c 215 if (outformat == FORMAT_PEM) {
0f113f3e 216 PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf);
2234212c 217 } else if (outformat == FORMAT_ASN1) {
0f113f3e 218 i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf);
2234212c 219 } else {
0f113f3e
MC
220 BIO_printf(bio_err, "Bad format specified for key\n");
221 goto end;
222 }
223 } else {
6355d315 224 X509_ALGOR *pbe;
0ceb8b74 225 if (cipher) {
b0809bc8 226#ifndef OPENSSL_NO_SCRYPT
0ceb8b74
DSH
227 if (scrypt_N && scrypt_r && scrypt_p)
228 pbe = PKCS5_pbe2_set_scrypt(cipher, NULL, 0, NULL,
229 scrypt_N, scrypt_r, scrypt_p);
230 else
b0809bc8 231#endif
0ceb8b74
DSH
232 pbe = PKCS5_pbe2_set_iv(cipher, iter, NULL, 0, NULL,
233 pbe_nid);
234 } else {
6355d315 235 pbe = PKCS5_pbe_set(pbe_nid, iter, NULL, 0);
0ceb8b74 236 }
6355d315
DSH
237 if (pbe == NULL) {
238 BIO_printf(bio_err, "Error setting PBE algorithm\n");
239 ERR_print_errors(bio_err);
240 goto end;
241 }
2234212c 242 if (passout != NULL) {
0f113f3e 243 p8pass = passout;
2234212c
PY
244 } else if (1) {
245 /* To avoid bit rot */
48feaceb 246#ifndef OPENSSL_NO_UI_CONSOLE
0f113f3e
MC
247 p8pass = pass;
248 if (EVP_read_pw_string
cbe29648 249 (pass, sizeof(pass), "Enter Encryption Password:", 1)) {
6355d315 250 X509_ALGOR_free(pbe);
0f113f3e 251 goto end;
6355d315 252 }
923b1857
RL
253 } else {
254#endif
255 BIO_printf(bio_err, "Password required\n");
256 goto end;
0f113f3e 257 }
6355d315
DSH
258 p8 = PKCS8_set0_pbe(p8pass, strlen(p8pass), p8inf, pbe);
259 if (p8 == NULL) {
260 X509_ALGOR_free(pbe);
0f113f3e
MC
261 BIO_printf(bio_err, "Error encrypting key\n");
262 ERR_print_errors(bio_err);
263 goto end;
264 }
3b061a00 265 assert(private);
0f113f3e
MC
266 if (outformat == FORMAT_PEM)
267 PEM_write_bio_PKCS8(out, p8);
268 else if (outformat == FORMAT_ASN1)
269 i2d_PKCS8_bio(out, p8);
270 else {
271 BIO_printf(bio_err, "Bad format specified for key\n");
272 goto end;
273 }
274 }
275
276 ret = 0;
277 goto end;
278 }
279
280 if (nocrypt) {
2234212c 281 if (informat == FORMAT_PEM) {
0f113f3e 282 p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in, NULL, NULL, NULL);
2234212c 283 } else if (informat == FORMAT_ASN1) {
0f113f3e 284 p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL);
2234212c 285 } else {
0f113f3e
MC
286 BIO_printf(bio_err, "Bad format specified for key\n");
287 goto end;
288 }
289 } else {
2234212c 290 if (informat == FORMAT_PEM) {
0f113f3e 291 p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL);
2234212c 292 } else if (informat == FORMAT_ASN1) {
0f113f3e 293 p8 = d2i_PKCS8_bio(in, NULL);
2234212c 294 } else {
0f113f3e
MC
295 BIO_printf(bio_err, "Bad format specified for key\n");
296 goto end;
297 }
8a605478 298
2234212c 299 if (p8 == NULL) {
0f113f3e
MC
300 BIO_printf(bio_err, "Error reading key\n");
301 ERR_print_errors(bio_err);
302 goto end;
303 }
2234212c 304 if (passin != NULL) {
0f113f3e 305 p8pass = passin;
2234212c 306 } else if (1) {
48feaceb 307#ifndef OPENSSL_NO_UI_CONSOLE
0f113f3e 308 p8pass = pass;
cbe29648 309 if (EVP_read_pw_string(pass, sizeof(pass), "Enter Password:", 0)) {
bcc31778
MC
310 BIO_printf(bio_err, "Can't read Password\n");
311 goto end;
312 }
923b1857
RL
313 } else {
314#endif
315 BIO_printf(bio_err, "Password required\n");
316 goto end;
0f113f3e
MC
317 }
318 p8inf = PKCS8_decrypt(p8, p8pass, strlen(p8pass));
319 }
600dec15 320
2234212c 321 if (p8inf == NULL) {
0f113f3e
MC
322 BIO_printf(bio_err, "Error decrypting key\n");
323 ERR_print_errors(bio_err);
324 goto end;
325 }
3cbb7937 326
75ebbd9a 327 if ((pkey = EVP_PKCS82PKEY(p8inf)) == NULL) {
0f113f3e
MC
328 BIO_printf(bio_err, "Error converting key\n");
329 ERR_print_errors(bio_err);
330 goto end;
331 }
e7871ffa 332
3b061a00 333 assert(private);
05dba815
DSH
334 if (outformat == FORMAT_PEM) {
335 if (traditional)
336 PEM_write_bio_PrivateKey_traditional(out, pkey, NULL, NULL, 0,
337 NULL, passout);
338 else
339 PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, passout);
340 } else if (outformat == FORMAT_ASN1) {
0f113f3e 341 i2d_PrivateKey_bio(out, pkey);
05dba815 342 } else {
0f113f3e
MC
343 BIO_printf(bio_err, "Bad format specified for key\n");
344 goto end;
345 }
346 ret = 0;
600dec15 347
0f113f3e
MC
348 end:
349 X509_SIG_free(p8);
350 PKCS8_PRIV_KEY_INFO_free(p8inf);
351 EVP_PKEY_free(pkey);
dd1abd44 352 release_engine(e);
0f113f3e
MC
353 BIO_free_all(out);
354 BIO_free(in);
b548a1f1
RS
355 OPENSSL_free(passin);
356 OPENSSL_free(passout);
8eb57af5 357
0f113f3e
MC
358 return ret;
359}