use saner default parameters for scrypt
[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, OPT_NOOCT, OPT_NSDB, OPT_EMBED,
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     {"nooct", OPT_NOOCT, '-', "Use (nonstandard) no octet format"},
87     {"nsdb", OPT_NSDB, '-', "Use (nonstandard) DSA Netscape DB format"},
88     {"embed", OPT_EMBED, '-',
89      "Use (nonstandard) embedded DSA parameters format"},
90     {"v2", OPT_V2, 's', "Use PKCS#5 v2.0 and cipher"},
91     {"v1", OPT_V1, 's', "Use PKCS#5 v1.5 and cipher"},
92     {"v2prf", OPT_V2PRF, 's'},
93     {"iter", OPT_ITER, 'p', "Specify the iteration count"},
94     {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
95     {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
96 #ifndef OPENSSL_NO_ENGINE
97     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
98 #endif
99 #ifndef OPENSSL_NO_SCRYPT
100     {"scrypt", OPT_SCRYPT, '-', "Use scrypt algorithm"},
101     {"scrypt_N", OPT_SCRYPT_N, 's', "Set scrypt N parameter"},
102     {"scrypt_r", OPT_SCRYPT_R, 's', "Set scrypt r parameter"},
103     {"scrypt_p", OPT_SCRYPT_P, 's', "Set scrypt p parameter"},
104 #endif
105     {NULL}
106 };
107
108 int pkcs8_main(int argc, char **argv)
109 {
110     BIO *in = NULL, *out = NULL;
111     ENGINE *e = NULL;
112     EVP_PKEY *pkey = NULL;
113     PKCS8_PRIV_KEY_INFO *p8inf = NULL;
114     X509_SIG *p8 = NULL;
115     const EVP_CIPHER *cipher = NULL;
116     char *infile = NULL, *outfile = NULL;
117     char *passinarg = NULL, *passoutarg = NULL, *prog;
118     char pass[50], *passin = NULL, *passout = NULL, *p8pass = NULL;
119     OPTION_CHOICE o;
120     int nocrypt = 0, ret = 1, iter = PKCS12_DEFAULT_ITER, p8_broken = PKCS8_OK;
121     int informat = FORMAT_PEM, outformat = FORMAT_PEM, topk8 = 0, pbe_nid = -1;
122     int private = 0;
123 #ifndef OPENSSL_NO_SCRYPT
124     long scrypt_N = 0, scrypt_r = 0, scrypt_p = 0;
125 #endif
126
127     prog = opt_init(argc, argv, pkcs8_options);
128     while ((o = opt_next()) != OPT_EOF) {
129         switch (o) {
130         case OPT_EOF:
131         case OPT_ERR:
132  opthelp:
133             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
134             goto end;
135         case OPT_HELP:
136             opt_help(pkcs8_options);
137             ret = 0;
138             goto end;
139         case OPT_INFORM:
140             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
141                 goto opthelp;
142             break;
143         case OPT_IN:
144             infile = opt_arg();
145             break;
146         case OPT_OUTFORM:
147             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
148                 goto opthelp;
149             break;
150         case OPT_OUT:
151             outfile = opt_arg();
152             break;
153         case OPT_TOPK8:
154             topk8 = 1;
155             break;
156         case OPT_NOITER:
157             iter = 1;
158             break;
159         case OPT_NOCRYPT:
160             nocrypt = 1;
161             break;
162         case OPT_NOOCT:
163             p8_broken = PKCS8_NO_OCTET;
164             break;
165         case OPT_NSDB:
166             p8_broken = PKCS8_NS_DB;
167             break;
168         case OPT_EMBED:
169             p8_broken = PKCS8_EMBEDDED_PARAM;
170             break;
171         case OPT_V2:
172             if (!opt_cipher(opt_arg(), &cipher))
173                 goto opthelp;
174             break;
175         case OPT_V1:
176             pbe_nid = OBJ_txt2nid(opt_arg());
177             if (pbe_nid == NID_undef) {
178                 BIO_printf(bio_err,
179                            "%s: Unknown PBE algorithm %s\n", prog, opt_arg());
180                 goto opthelp;
181             }
182             break;
183         case OPT_V2PRF:
184             pbe_nid = OBJ_txt2nid(opt_arg());
185             if (!EVP_PBE_find(EVP_PBE_TYPE_PRF, pbe_nid, NULL, NULL, 0)) {
186                 BIO_printf(bio_err,
187                            "%s: Unknown PRF algorithm %s\n", prog, opt_arg());
188                 goto opthelp;
189             }
190             break;
191         case OPT_ITER:
192             if (!opt_int(opt_arg(), &iter))
193                 goto opthelp;
194             break;
195         case OPT_PASSIN:
196             passinarg = opt_arg();
197             break;
198         case OPT_PASSOUT:
199             passoutarg = opt_arg();
200             break;
201         case OPT_ENGINE:
202             e = setup_engine(opt_arg(), 0);
203             break;
204 #ifndef OPENSSL_NO_SCRYPT
205         case OPT_SCRYPT:
206             scrypt_N = 16384;
207             scrypt_r = 8;
208             scrypt_p = 1;
209             if (cipher == NULL)
210                 cipher = EVP_aes_256_cbc();
211             break;
212         case OPT_SCRYPT_N:
213             if (!opt_long(opt_arg(), &scrypt_N) || scrypt_N <= 0)
214                 goto opthelp;
215             break;
216         case OPT_SCRYPT_R:
217             if (!opt_long(opt_arg(), &scrypt_r) || scrypt_r <= 0)
218                 goto opthelp;
219             break;
220         case OPT_SCRYPT_P:
221             if (!opt_long(opt_arg(), &scrypt_p) || scrypt_p <= 0)
222                 goto opthelp;
223             break;
224 #endif
225         }
226     }
227     argc = opt_num_rest();
228     if (argc != 0)
229         goto opthelp;
230
231     private = 1;
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)
239         pbe_nid = NID_pbeWithMD5AndDES_CBC;
240
241     in = bio_open_default(infile, 'r', informat);
242     if (in == NULL)
243         goto end;
244     out = bio_open_owner(outfile, outformat, private);
245     if (out == NULL)
246         goto end;
247
248     if (topk8) {
249         pkey = load_key(infile, informat, 1, passin, e, "key");
250         if (!pkey)
251             goto end;
252         if ((p8inf = EVP_PKEY2PKCS8_broken(pkey, p8_broken)) == NULL) {
253             BIO_printf(bio_err, "Error converting key\n");
254             ERR_print_errors(bio_err);
255             goto end;
256         }
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, 0, NULL,
273                                                 scrypt_N, scrypt_r, scrypt_p);
274                 else
275 #endif
276                     pbe = PKCS5_pbe2_set_iv(cipher, iter, NULL, 0, NULL,
277                                             pbe_nid);
278             } else {
279                 pbe = PKCS5_pbe_set(pbe_nid, iter, NULL, 0);
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)
287                 p8pass = passout;
288             else {
289                 p8pass = pass;
290                 if (EVP_read_pw_string
291                     (pass, sizeof pass, "Enter Encryption Password:", 1)) {
292                     X509_ALGOR_free(pbe);
293                     goto end;
294                 }
295             }
296             app_RAND_load_file(NULL, 0);
297             p8 = PKCS8_set0_pbe(p8pass, strlen(p8pass), p8inf, pbe);
298             if (p8 == NULL) {
299                 X509_ALGOR_free(pbe);
300                 BIO_printf(bio_err, "Error encrypting key\n");
301                 ERR_print_errors(bio_err);
302                 goto end;
303             }
304             app_RAND_write_file(NULL);
305             assert(private);
306             if (outformat == FORMAT_PEM)
307                 PEM_write_bio_PKCS8(out, p8);
308             else if (outformat == FORMAT_ASN1)
309                 i2d_PKCS8_bio(out, p8);
310             else {
311                 BIO_printf(bio_err, "Bad format specified for key\n");
312                 goto end;
313             }
314         }
315
316         ret = 0;
317         goto end;
318     }
319
320     if (nocrypt) {
321         if (informat == FORMAT_PEM)
322             p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in, NULL, NULL, NULL);
323         else if (informat == FORMAT_ASN1)
324             p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL);
325         else {
326             BIO_printf(bio_err, "Bad format specified for key\n");
327             goto end;
328         }
329     } else {
330         if (informat == FORMAT_PEM)
331             p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL);
332         else if (informat == FORMAT_ASN1)
333             p8 = d2i_PKCS8_bio(in, NULL);
334         else {
335             BIO_printf(bio_err, "Bad format specified for key\n");
336             goto end;
337         }
338
339         if (!p8) {
340             BIO_printf(bio_err, "Error reading key\n");
341             ERR_print_errors(bio_err);
342             goto end;
343         }
344         if (passin)
345             p8pass = passin;
346         else {
347             p8pass = pass;
348             EVP_read_pw_string(pass, sizeof pass, "Enter Password:", 0);
349         }
350         p8inf = PKCS8_decrypt(p8, p8pass, strlen(p8pass));
351     }
352
353     if (!p8inf) {
354         BIO_printf(bio_err, "Error decrypting key\n");
355         ERR_print_errors(bio_err);
356         goto end;
357     }
358
359     if ((pkey = EVP_PKCS82PKEY(p8inf)) == NULL) {
360         BIO_printf(bio_err, "Error converting key\n");
361         ERR_print_errors(bio_err);
362         goto end;
363     }
364
365     if (p8inf->broken) {
366         BIO_printf(bio_err, "Warning: broken key encoding: ");
367         switch (p8inf->broken) {
368         case PKCS8_NO_OCTET:
369             BIO_printf(bio_err, "No Octet String in PrivateKey\n");
370             break;
371
372         case PKCS8_EMBEDDED_PARAM:
373             BIO_printf(bio_err, "DSA parameters included in PrivateKey\n");
374             break;
375
376         case PKCS8_NS_DB:
377             BIO_printf(bio_err, "DSA public key include in PrivateKey\n");
378             break;
379
380         case PKCS8_NEG_PRIVKEY:
381             BIO_printf(bio_err, "DSA private key value is negative\n");
382             break;
383
384         default:
385             BIO_printf(bio_err, "Unknown broken type\n");
386             break;
387         }
388     }
389
390     assert(private);
391     if (outformat == FORMAT_PEM)
392         PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, passout);
393     else if (outformat == FORMAT_ASN1)
394         i2d_PrivateKey_bio(out, pkey);
395     else {
396         BIO_printf(bio_err, "Bad format specified for key\n");
397         goto end;
398     }
399     ret = 0;
400
401  end:
402     X509_SIG_free(p8);
403     PKCS8_PRIV_KEY_INFO_free(p8inf);
404     EVP_PKEY_free(pkey);
405     BIO_free_all(out);
406     BIO_free(in);
407     OPENSSL_free(passin);
408     OPENSSL_free(passout);
409
410     return ret;
411 }