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