enc: "bad decrypt" only in decryption
[openssl.git] / apps / pkcs8.c
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
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     out = bio_open_owner(outfile, outformat, private);
231     if (out == NULL)
232         goto end;
233
234     if (topk8) {
235         pkey = load_key(infile, informat, 1, passin, e, "key");
236         if (pkey == NULL)
237             goto end;
238         if ((p8inf = EVP_PKEY2PKCS8(pkey)) == NULL) {
239             BIO_printf(bio_err, "Error converting key\n");
240             ERR_print_errors(bio_err);
241             goto end;
242         }
243         if (nocrypt) {
244             assert(private);
245             if (outformat == FORMAT_PEM) {
246                 PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf);
247             } else if (outformat == FORMAT_ASN1) {
248                 i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf);
249             } else {
250                 BIO_printf(bio_err, "Bad format specified for key\n");
251                 goto end;
252             }
253         } else {
254             X509_ALGOR *pbe;
255             if (cipher) {
256 #ifndef OPENSSL_NO_SCRYPT
257                 if (scrypt_N && scrypt_r && scrypt_p)
258                     pbe = PKCS5_pbe2_set_scrypt(cipher, NULL, saltlen, NULL,
259                                                 scrypt_N, scrypt_r, scrypt_p);
260                 else
261 #endif
262                     pbe = PKCS5_pbe2_set_iv(cipher, iter, NULL, saltlen, NULL,
263                                             pbe_nid);
264             } else {
265                 pbe = PKCS5_pbe_set(pbe_nid, iter, NULL, saltlen);
266             }
267             if (pbe == NULL) {
268                 BIO_printf(bio_err, "Error setting PBE algorithm\n");
269                 ERR_print_errors(bio_err);
270                 goto end;
271             }
272             if (passout != NULL) {
273                 p8pass = passout;
274             } else if (1) {
275                 /* To avoid bit rot */
276 #ifndef OPENSSL_NO_UI_CONSOLE
277                 p8pass = pass;
278                 if (EVP_read_pw_string
279                     (pass, sizeof(pass), "Enter Encryption Password:", 1)) {
280                     X509_ALGOR_free(pbe);
281                     goto end;
282                 }
283             } else {
284 #endif
285                 BIO_printf(bio_err, "Password required\n");
286                 goto end;
287             }
288             p8 = PKCS8_set0_pbe(p8pass, strlen(p8pass), p8inf, pbe);
289             if (p8 == NULL) {
290                 X509_ALGOR_free(pbe);
291                 BIO_printf(bio_err, "Error encrypting key\n");
292                 ERR_print_errors(bio_err);
293                 goto end;
294             }
295             assert(private);
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) {
311         if (informat == FORMAT_PEM || informat == FORMAT_UNDEF) {
312             p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in, NULL, NULL, NULL);
313         } else if (informat == FORMAT_ASN1) {
314             p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL);
315         } else {
316             BIO_printf(bio_err, "Bad format specified for key\n");
317             goto end;
318         }
319     } else {
320         if (informat == FORMAT_PEM || informat == FORMAT_UNDEF) {
321             p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL);
322         } else if (informat == FORMAT_ASN1) {
323             p8 = d2i_PKCS8_bio(in, NULL);
324         } else {
325             BIO_printf(bio_err, "Bad format specified for key\n");
326             goto end;
327         }
328
329         if (p8 == NULL) {
330             BIO_printf(bio_err, "Error reading key\n");
331             ERR_print_errors(bio_err);
332             goto end;
333         }
334         if (passin != NULL) {
335             p8pass = passin;
336         } else if (1) {
337 #ifndef OPENSSL_NO_UI_CONSOLE
338             p8pass = pass;
339             if (EVP_read_pw_string(pass, sizeof(pass), "Enter Password:", 0)) {
340                 BIO_printf(bio_err, "Can't read Password\n");
341                 goto end;
342             }
343         } else {
344 #endif
345             BIO_printf(bio_err, "Password required\n");
346             goto end;
347         }
348         p8inf = PKCS8_decrypt(p8, p8pass, strlen(p8pass));
349     }
350
351     if (p8inf == NULL) {
352         BIO_printf(bio_err, "Error decrypting key\n");
353         ERR_print_errors(bio_err);
354         goto end;
355     }
356
357     if ((pkey = EVP_PKCS82PKEY(p8inf)) == NULL) {
358         BIO_printf(bio_err, "Error converting key\n");
359         ERR_print_errors(bio_err);
360         goto end;
361     }
362
363     assert(private);
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) {
371         i2d_PrivateKey_bio(out, pkey);
372     } else {
373         BIO_printf(bio_err, "Bad format specified for key\n");
374         goto end;
375     }
376     ret = 0;
377
378  end:
379     X509_SIG_free(p8);
380     PKCS8_PRIV_KEY_INFO_free(p8inf);
381     EVP_PKEY_free(pkey);
382     EVP_CIPHER_free(cipher);
383     release_engine(e);
384     BIO_free_all(out);
385     BIO_free(in);
386     OPENSSL_free(passin);
387     OPENSSL_free(passout);
388
389     return ret;
390 }