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