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