1 /*
2 * Copyright 1999-2023 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include "apps.h"
14 #include "progs.h"
15 #include <openssl/pem.h>
16 #include <openssl/err.h>
17 #include <openssl/evp.h>
18 #include <openssl/pkcs12.h>
19
20 #define STR(a) XSTR(a)
21 #define XSTR(a) #a
22
23 typedef enum OPTION_choice {
24 OPT_COMMON,
25 OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
26 OPT_TOPK8, OPT_NOITER, OPT_NOCRYPT,
27 #ifndef OPENSSL_NO_SCRYPT
28 OPT_SCRYPT, OPT_SCRYPT_N, OPT_SCRYPT_R, OPT_SCRYPT_P,
29 #endif
30 OPT_V2, OPT_V1, OPT_V2PRF, OPT_ITER, OPT_PASSIN, OPT_PASSOUT,
31 OPT_TRADITIONAL,
32 OPT_SALTLEN,
33 OPT_R_ENUM, OPT_PROV_ENUM
34 } OPTION_CHOICE;
35
36 const OPTIONS pkcs8_options[] = {
37 OPT_SECTION("General"),
38 {"help", OPT_HELP, '-', "Display this summary"},
39 #ifndef OPENSSL_NO_ENGINE
40 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
41 #endif
42 {"v1", OPT_V1, 's', "Use PKCS#5 v1.5 and cipher"},
43 {"v2", OPT_V2, 's', "Use PKCS#5 v2.0 and cipher"},
44 {"v2prf", OPT_V2PRF, 's', "Set the PRF algorithm to use with PKCS#5 v2.0"},
45
46 OPT_SECTION("Input"),
47 {"in", OPT_IN, '<', "Input file"},
48 {"inform", OPT_INFORM, 'F', "Input format (DER or PEM)"},
49 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
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"},
56 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
57 {"traditional", OPT_TRADITIONAL, '-', "use traditional format private key"},
58 {"iter", OPT_ITER, 'p', "Specify the iteration count"},
59 {"noiter", OPT_NOITER, '-', "Use 1 as iteration count"},
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)"},
62 #ifndef OPENSSL_NO_SCRYPT
63 OPT_SECTION("Scrypt"),
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"},
68 #endif
69
70 OPT_R_OPTIONS,
71 OPT_PROV_OPTIONS,
72 {NULL}
73 };
74
pkcs8_main(int argc,char ** argv)75 int pkcs8_main(int argc, char **argv)
76 {
77 BIO *in = NULL, *out = NULL;
78 ENGINE *e = NULL;
79 EVP_PKEY *pkey = NULL;
80 PKCS8_PRIV_KEY_INFO *p8inf = NULL;
81 X509_SIG *p8 = NULL;
82 EVP_CIPHER *cipher = NULL;
83 char *infile = NULL, *outfile = NULL, *ciphername = NULL;
84 char *passinarg = NULL, *passoutarg = NULL, *prog;
85 #ifndef OPENSSL_NO_UI_CONSOLE
86 char pass[APP_PASS_LEN];
87 #endif
88 char *passin = NULL, *passout = NULL, *p8pass = NULL;
89 OPTION_CHOICE o;
90 int nocrypt = 0, ret = 1, iter = PKCS12_DEFAULT_ITER;
91 int informat = FORMAT_UNDEF, outformat = FORMAT_PEM, topk8 = 0, pbe_nid = -1;
92 int private = 0, traditional = 0;
93 #ifndef OPENSSL_NO_SCRYPT
94 long scrypt_N = 0, scrypt_r = 0, scrypt_p = 0;
95 #endif
96 int saltlen = 0; /* A value of zero chooses the default */
97
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:
125 topk8 = 1;
126 break;
127 case OPT_NOITER:
128 iter = 1;
129 break;
130 case OPT_NOCRYPT:
131 nocrypt = 1;
132 break;
133 case OPT_R_CASES:
134 if (!opt_rand(o))
135 goto end;
136 break;
137 case OPT_PROV_CASES:
138 if (!opt_provider(o))
139 goto end;
140 break;
141 case OPT_TRADITIONAL:
142 traditional = 1;
143 break;
144 case OPT_V2:
145 ciphername = opt_arg();
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 }
162 if (cipher == NULL)
163 cipher = (EVP_CIPHER *)EVP_aes_256_cbc();
164 break;
165 case OPT_ITER:
166 iter = opt_int_arg();
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:
175 e = setup_engine(opt_arg(), 0);
176 break;
177 #ifndef OPENSSL_NO_SCRYPT
178 case OPT_SCRYPT:
179 scrypt_N = 16384;
180 scrypt_r = 8;
181 scrypt_p = 1;
182 if (cipher == NULL)
183 cipher = (EVP_CIPHER *)EVP_aes_256_cbc();
184 break;
185 case OPT_SCRYPT_N:
186 if (!opt_long(opt_arg(), &scrypt_N) || scrypt_N <= 0)
187 goto opthelp;
188 break;
189 case OPT_SCRYPT_R:
190 if (!opt_long(opt_arg(), &scrypt_r) || scrypt_r <= 0)
191 goto opthelp;
192 break;
193 case OPT_SCRYPT_P:
194 if (!opt_long(opt_arg(), &scrypt_p) || scrypt_p <= 0)
195 goto opthelp;
196 break;
197 #endif
198 case OPT_SALTLEN:
199 if (!opt_int(opt_arg(), &saltlen))
200 goto opthelp;
201 break;
202 }
203 }
204
205 /* No extra arguments. */
206 if (!opt_check_rest_arg(NULL))
207 goto opthelp;
208
209 private = 1;
210 if (!app_RAND_load())
211 goto end;
212
213 if (ciphername != NULL) {
214 if (!opt_cipher(ciphername, &cipher))
215 goto opthelp;
216 }
217
218 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
219 BIO_printf(bio_err, "Error getting passwords\n");
220 goto end;
221 }
222
223 if ((pbe_nid == -1) && cipher == NULL)
224 cipher = (EVP_CIPHER *)EVP_aes_256_cbc();
225
226 in = bio_open_default(infile, 'r',
227 informat == FORMAT_UNDEF ? FORMAT_PEM : informat);
228 if (in == NULL)
229 goto end;
230
231 if (topk8) {
232 pkey = load_key(infile, informat, 1, passin, e, "key");
233 if (pkey == NULL)
234 goto end;
235 if ((p8inf = EVP_PKEY2PKCS8(pkey)) == NULL) {
236 BIO_printf(bio_err, "Error converting key\n");
237 ERR_print_errors(bio_err);
238 goto end;
239 }
240 if ((out = bio_open_owner(outfile, outformat, private)) == NULL)
241 goto end;
242 if (nocrypt) {
243 assert(private);
244 if (outformat == FORMAT_PEM) {
245 PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf);
246 } else if (outformat == FORMAT_ASN1) {
247 i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf);
248 } else {
249 BIO_printf(bio_err, "Bad format specified for key\n");
250 goto end;
251 }
252 } else {
253 X509_ALGOR *pbe;
254 if (cipher) {
255 #ifndef OPENSSL_NO_SCRYPT
256 if (scrypt_N && scrypt_r && scrypt_p)
257 pbe = PKCS5_pbe2_set_scrypt(cipher, NULL, saltlen, NULL,
258 scrypt_N, scrypt_r, scrypt_p);
259 else
260 #endif
261 pbe = PKCS5_pbe2_set_iv(cipher, iter, NULL, saltlen, NULL,
262 pbe_nid);
263 } else {
264 pbe = PKCS5_pbe_set(pbe_nid, iter, NULL, saltlen);
265 }
266 if (pbe == NULL) {
267 BIO_printf(bio_err, "Error setting PBE algorithm\n");
268 ERR_print_errors(bio_err);
269 goto end;
270 }
271 if (passout != NULL) {
272 p8pass = passout;
273 } else if (1) {
274 /* To avoid bit rot */
275 #ifndef OPENSSL_NO_UI_CONSOLE
276 p8pass = pass;
277 if (EVP_read_pw_string
278 (pass, sizeof(pass), "Enter Encryption Password:", 1)) {
279 X509_ALGOR_free(pbe);
280 goto end;
281 }
282 } else {
283 #endif
284 BIO_printf(bio_err, "Password required\n");
285 goto end;
286 }
287 p8 = PKCS8_set0_pbe(p8pass, strlen(p8pass), p8inf, pbe);
288 if (p8 == NULL) {
289 X509_ALGOR_free(pbe);
290 BIO_printf(bio_err, "Error encrypting key\n");
291 ERR_print_errors(bio_err);
292 goto end;
293 }
294 assert(private);
295 if (outformat == FORMAT_PEM)
296 PEM_write_bio_PKCS8(out, p8);
297 else if (outformat == FORMAT_ASN1)
298 i2d_PKCS8_bio(out, p8);
299 else {
300 BIO_printf(bio_err, "Bad format specified for key\n");
301 goto end;
302 }
303 }
304
305 ret = 0;
306 goto end;
307 }
308
309 if (nocrypt) {
310 if (informat == FORMAT_PEM || informat == FORMAT_UNDEF) {
311 p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in, NULL, NULL, NULL);
312 } else if (informat == FORMAT_ASN1) {
313 p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL);
314 } else {
315 BIO_printf(bio_err, "Bad format specified for key\n");
316 goto end;
317 }
318 } else {
319 if (informat == FORMAT_PEM || informat == FORMAT_UNDEF) {
320 p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL);
321 } else if (informat == FORMAT_ASN1) {
322 p8 = d2i_PKCS8_bio(in, NULL);
323 } else {
324 BIO_printf(bio_err, "Bad format specified for key\n");
325 goto end;
326 }
327
328 if (p8 == NULL) {
329 BIO_printf(bio_err, "Error reading key\n");
330 ERR_print_errors(bio_err);
331 goto end;
332 }
333 if (passin != NULL) {
334 p8pass = passin;
335 } else if (1) {
336 #ifndef OPENSSL_NO_UI_CONSOLE
337 p8pass = pass;
338 if (EVP_read_pw_string(pass, sizeof(pass), "Enter Password:", 0)) {
339 BIO_printf(bio_err, "Can't read Password\n");
340 goto end;
341 }
342 } else {
343 #endif
344 BIO_printf(bio_err, "Password required\n");
345 goto end;
346 }
347 p8inf = PKCS8_decrypt(p8, p8pass, strlen(p8pass));
348 }
349
350 if (p8inf == NULL) {
351 BIO_printf(bio_err, "Error decrypting key\n");
352 ERR_print_errors(bio_err);
353 goto end;
354 }
355
356 if ((pkey = EVP_PKCS82PKEY(p8inf)) == NULL) {
357 BIO_printf(bio_err, "Error converting key\n");
358 ERR_print_errors(bio_err);
359 goto end;
360 }
361
362 assert(private);
363 out = bio_open_owner(outfile, outformat, private);
364 if (out == NULL)
365 goto end;
366 if (outformat == FORMAT_PEM) {
367 if (traditional)
368 PEM_write_bio_PrivateKey_traditional(out, pkey, NULL, NULL, 0,
369 NULL, passout);
370 else
371 PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, passout);
372 } else if (outformat == FORMAT_ASN1) {
373 i2d_PrivateKey_bio(out, pkey);
374 } else {
375 BIO_printf(bio_err, "Bad format specified for key\n");
376 goto end;
377 }
378 ret = 0;
379
380 end:
381 X509_SIG_free(p8);
382 PKCS8_PRIV_KEY_INFO_free(p8inf);
383 EVP_PKEY_free(pkey);
384 EVP_CIPHER_free(cipher);
385 release_engine(e);
386 BIO_free_all(out);
387 BIO_free(in);
388 OPENSSL_free(passin);
389 OPENSSL_free(passout);
390
391 return ret;
392 }
393