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