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