Update pkcs8 defaults.
[openssl.git] / apps / pkcs8.c
1 /*
2  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
3  * 1999-2004.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include "apps.h"
62 #include <openssl/pem.h>
63 #include <openssl/err.h>
64 #include <openssl/evp.h>
65 #include <openssl/pkcs12.h>
66
67 typedef enum OPTION_choice {
68     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
69     OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
70     OPT_TOPK8, OPT_NOITER, OPT_NOCRYPT,
71 #ifndef OPENSSL_NO_SCRYPT
72     OPT_SCRYPT, OPT_SCRYPT_N, OPT_SCRYPT_R, OPT_SCRYPT_P,
73 #endif
74     OPT_V2, OPT_V1, OPT_V2PRF, OPT_ITER, OPT_PASSIN, OPT_PASSOUT
75 } OPTION_CHOICE;
76
77 OPTIONS pkcs8_options[] = {
78     {"help", OPT_HELP, '-', "Display this summary"},
79     {"inform", OPT_INFORM, 'F', "Input format (DER or PEM)"},
80     {"outform", OPT_OUTFORM, 'F', "Output format (DER or PEM)"},
81     {"in", OPT_IN, '<', "Input file"},
82     {"out", OPT_OUT, '>', "Output file"},
83     {"topk8", OPT_TOPK8, '-', "Output PKCS8 file"},
84     {"noiter", OPT_NOITER, '-', "Use 1 as iteration count"},
85     {"nocrypt", OPT_NOCRYPT, '-', "Use or expect unencrypted private key"},
86     {"v2", OPT_V2, 's', "Use PKCS#5 v2.0 and cipher"},
87     {"v1", OPT_V1, 's', "Use PKCS#5 v1.5 and cipher"},
88     {"v2prf", OPT_V2PRF, 's'},
89     {"iter", OPT_ITER, 'p', "Specify the iteration count"},
90     {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
91     {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
92 #ifndef OPENSSL_NO_ENGINE
93     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
94 #endif
95 #ifndef OPENSSL_NO_SCRYPT
96     {"scrypt", OPT_SCRYPT, '-', "Use scrypt algorithm"},
97     {"scrypt_N", OPT_SCRYPT_N, 's', "Set scrypt N parameter"},
98     {"scrypt_r", OPT_SCRYPT_R, 's', "Set scrypt r parameter"},
99     {"scrypt_p", OPT_SCRYPT_P, 's', "Set scrypt p parameter"},
100 #endif
101     {NULL}
102 };
103
104 int pkcs8_main(int argc, char **argv)
105 {
106     BIO *in = NULL, *out = NULL;
107     ENGINE *e = NULL;
108     EVP_PKEY *pkey = NULL;
109     PKCS8_PRIV_KEY_INFO *p8inf = NULL;
110     X509_SIG *p8 = NULL;
111     const EVP_CIPHER *cipher = NULL;
112     char *infile = NULL, *outfile = NULL;
113     char *passinarg = NULL, *passoutarg = NULL, *prog;
114 #ifndef OPENSSL_NO_UI
115     char pass[50];
116 #endif
117     char *passin = NULL, *passout = NULL, *p8pass = NULL;
118     OPTION_CHOICE o;
119     int nocrypt = 0, ret = 1, iter = PKCS12_DEFAULT_ITER;
120     int informat = FORMAT_PEM, outformat = FORMAT_PEM, topk8 = 0, pbe_nid = -1;
121     int private = 0;
122 #ifndef OPENSSL_NO_SCRYPT
123     long scrypt_N = 0, scrypt_r = 0, scrypt_p = 0;
124 #endif
125
126     prog = opt_init(argc, argv, pkcs8_options);
127     while ((o = opt_next()) != OPT_EOF) {
128         switch (o) {
129         case OPT_EOF:
130         case OPT_ERR:
131  opthelp:
132             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
133             goto end;
134         case OPT_HELP:
135             opt_help(pkcs8_options);
136             ret = 0;
137             goto end;
138         case OPT_INFORM:
139             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
140                 goto opthelp;
141             break;
142         case OPT_IN:
143             infile = opt_arg();
144             break;
145         case OPT_OUTFORM:
146             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
147                 goto opthelp;
148             break;
149         case OPT_OUT:
150             outfile = opt_arg();
151             break;
152         case OPT_TOPK8:
153             topk8 = 1;
154             break;
155         case OPT_NOITER:
156             iter = 1;
157             break;
158         case OPT_NOCRYPT:
159             nocrypt = 1;
160             break;
161         case OPT_V2:
162             if (!opt_cipher(opt_arg(), &cipher))
163                 goto opthelp;
164             break;
165         case OPT_V1:
166             pbe_nid = OBJ_txt2nid(opt_arg());
167             if (pbe_nid == NID_undef) {
168                 BIO_printf(bio_err,
169                            "%s: Unknown PBE algorithm %s\n", prog, opt_arg());
170                 goto opthelp;
171             }
172             break;
173         case OPT_V2PRF:
174             pbe_nid = OBJ_txt2nid(opt_arg());
175             if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, pbe_nid, NULL, NULL, 0)) {
176                 BIO_printf(bio_err,
177                            "%s: Unknown PRF algorithm %s\n", prog, opt_arg());
178                 goto opthelp;
179             }
180             if (cipher == NULL)
181                 cipher = EVP_aes_256_cbc();
182             break;
183         case OPT_ITER:
184             if (!opt_int(opt_arg(), &iter))
185                 goto opthelp;
186             break;
187         case OPT_PASSIN:
188             passinarg = opt_arg();
189             break;
190         case OPT_PASSOUT:
191             passoutarg = opt_arg();
192             break;
193         case OPT_ENGINE:
194             e = setup_engine(opt_arg(), 0);
195             break;
196 #ifndef OPENSSL_NO_SCRYPT
197         case OPT_SCRYPT:
198             scrypt_N = 16384;
199             scrypt_r = 8;
200             scrypt_p = 1;
201             if (cipher == NULL)
202                 cipher = EVP_aes_256_cbc();
203             break;
204         case OPT_SCRYPT_N:
205             if (!opt_long(opt_arg(), &scrypt_N) || scrypt_N <= 0)
206                 goto opthelp;
207             break;
208         case OPT_SCRYPT_R:
209             if (!opt_long(opt_arg(), &scrypt_r) || scrypt_r <= 0)
210                 goto opthelp;
211             break;
212         case OPT_SCRYPT_P:
213             if (!opt_long(opt_arg(), &scrypt_p) || scrypt_p <= 0)
214                 goto opthelp;
215             break;
216 #endif
217         }
218     }
219     argc = opt_num_rest();
220     if (argc != 0)
221         goto opthelp;
222
223     private = 1;
224
225     if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
226         BIO_printf(bio_err, "Error getting passwords\n");
227         goto end;
228     }
229
230     if ((pbe_nid == -1) && cipher == NULL)
231         cipher = EVP_aes_256_cbc();
232
233     in = bio_open_default(infile, 'r', informat);
234     if (in == NULL)
235         goto end;
236     out = bio_open_owner(outfile, outformat, private);
237     if (out == NULL)
238         goto end;
239
240     if (topk8) {
241         pkey = load_key(infile, informat, 1, passin, e, "key");
242         if (!pkey)
243             goto end;
244         if ((p8inf = EVP_PKEY2PKCS8(pkey)) == NULL) {
245             BIO_printf(bio_err, "Error converting key\n");
246             ERR_print_errors(bio_err);
247             goto end;
248         }
249         if (nocrypt) {
250             assert(private);
251             if (outformat == FORMAT_PEM)
252                 PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf);
253             else if (outformat == FORMAT_ASN1)
254                 i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf);
255             else {
256                 BIO_printf(bio_err, "Bad format specified for key\n");
257                 goto end;
258             }
259         } else {
260             X509_ALGOR *pbe;
261             if (cipher) {
262 #ifndef OPENSSL_NO_SCRYPT
263                 if (scrypt_N && scrypt_r && scrypt_p)
264                     pbe = PKCS5_pbe2_set_scrypt(cipher, NULL, 0, NULL,
265                                                 scrypt_N, scrypt_r, scrypt_p);
266                 else
267 #endif
268                     pbe = PKCS5_pbe2_set_iv(cipher, iter, NULL, 0, NULL,
269                                             pbe_nid);
270             } else {
271                 pbe = PKCS5_pbe_set(pbe_nid, iter, NULL, 0);
272             }
273             if (pbe == NULL) {
274                 BIO_printf(bio_err, "Error setting PBE algorithm\n");
275                 ERR_print_errors(bio_err);
276                 goto end;
277             }
278             if (passout)
279                 p8pass = passout;
280             else if (1) {
281 #ifndef OPENSSL_NO_UI
282                 p8pass = pass;
283                 if (EVP_read_pw_string
284                     (pass, sizeof pass, "Enter Encryption Password:", 1)) {
285                     X509_ALGOR_free(pbe);
286                     goto end;
287                 }
288             } else {
289 #endif
290                 BIO_printf(bio_err, "Password required\n");
291                 goto end;
292             }
293             app_RAND_load_file(NULL, 0);
294             p8 = PKCS8_set0_pbe(p8pass, strlen(p8pass), p8inf, pbe);
295             if (p8 == NULL) {
296                 X509_ALGOR_free(pbe);
297                 BIO_printf(bio_err, "Error encrypting key\n");
298                 ERR_print_errors(bio_err);
299                 goto end;
300             }
301             app_RAND_write_file(NULL);
302             assert(private);
303             if (outformat == FORMAT_PEM)
304                 PEM_write_bio_PKCS8(out, p8);
305             else if (outformat == FORMAT_ASN1)
306                 i2d_PKCS8_bio(out, p8);
307             else {
308                 BIO_printf(bio_err, "Bad format specified for key\n");
309                 goto end;
310             }
311         }
312
313         ret = 0;
314         goto end;
315     }
316
317     if (nocrypt) {
318         if (informat == FORMAT_PEM)
319             p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in, NULL, NULL, NULL);
320         else if (informat == FORMAT_ASN1)
321             p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL);
322         else {
323             BIO_printf(bio_err, "Bad format specified for key\n");
324             goto end;
325         }
326     } else {
327         if (informat == FORMAT_PEM)
328             p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL);
329         else if (informat == FORMAT_ASN1)
330             p8 = d2i_PKCS8_bio(in, NULL);
331         else {
332             BIO_printf(bio_err, "Bad format specified for key\n");
333             goto end;
334         }
335
336         if (!p8) {
337             BIO_printf(bio_err, "Error reading key\n");
338             ERR_print_errors(bio_err);
339             goto end;
340         }
341         if (passin)
342             p8pass = passin;
343         else if (1) {
344 #ifndef OPENSSL_NO_UI
345             p8pass = pass;
346             if (EVP_read_pw_string(pass, sizeof pass, "Enter Password:", 0)) {
347                 BIO_printf(bio_err, "Can't read Password\n");
348                 goto end;
349             }
350         } else {
351 #endif
352             BIO_printf(bio_err, "Password required\n");
353             goto end;
354         }
355         p8inf = PKCS8_decrypt(p8, p8pass, strlen(p8pass));
356     }
357
358     if (!p8inf) {
359         BIO_printf(bio_err, "Error decrypting key\n");
360         ERR_print_errors(bio_err);
361         goto end;
362     }
363
364     if ((pkey = EVP_PKCS82PKEY(p8inf)) == NULL) {
365         BIO_printf(bio_err, "Error converting key\n");
366         ERR_print_errors(bio_err);
367         goto end;
368     }
369
370     assert(private);
371     if (outformat == FORMAT_PEM)
372         PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, passout);
373     else if (outformat == FORMAT_ASN1)
374         i2d_PrivateKey_bio(out, pkey);
375     else {
376         BIO_printf(bio_err, "Bad format specified for key\n");
377         goto end;
378     }
379     ret = 0;
380
381  end:
382     X509_SIG_free(p8);
383     PKCS8_PRIV_KEY_INFO_free(p8inf);
384     EVP_PKEY_free(pkey);
385     BIO_free_all(out);
386     BIO_free(in);
387     OPENSSL_free(passin);
388     OPENSSL_free(passout);
389
390     return ret;
391 }