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