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