Rename SSLeay_add_all_algorithms() et al to
[openssl.git] / apps / pkcs8.c
1 /* pkcs8.c */
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3  * project 1999.
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 <string.h>
60 #include "apps.h"
61 #include <openssl/pem.h>
62 #include <openssl/err.h>
63 #include <openssl/evp.h>
64 #include <openssl/pkcs12.h>
65
66 #include "apps.h"
67 #define PROG pkcs8_main
68
69
70 int MAIN(int argc, char **argv)
71 {
72         char **args, *infile = NULL, *outfile = NULL;
73         BIO *in = NULL, *out = NULL;
74         int topk8 = 0;
75         int pbe_nid = -1;
76         const EVP_CIPHER *cipher = NULL;
77         int iter = PKCS12_DEFAULT_ITER;
78         int informat, outformat;
79         int p8_broken = PKCS8_OK;
80         int nocrypt = 0;
81         X509_SIG *p8;
82         PKCS8_PRIV_KEY_INFO *p8inf;
83         EVP_PKEY *pkey;
84         char pass[50], *passin = NULL, *passout = NULL;
85         int badarg = 0;
86         if (bio_err == NULL) bio_err = BIO_new_fp (stderr, BIO_NOCLOSE);
87         informat=FORMAT_PEM;
88         outformat=FORMAT_PEM;
89         ERR_load_crypto_strings();
90         OpenSSL_add_all_algorithms();
91         args = argv + 1;
92         while (!badarg && *args && *args[0] == '-') {
93                 if (!strcmp(*args,"-v2")) {
94                         if (args[1]) {
95                                 args++;
96                                 cipher=EVP_get_cipherbyname(*args);
97                                 if(!cipher) {
98                                         BIO_printf(bio_err,
99                                                  "Unknown cipher %s\n", *args);
100                                         badarg = 1;
101                                 }
102                         } else badarg = 1;
103                 } else if (!strcmp(*args,"-v1")) {
104                         if (args[1]) {
105                                 args++;
106                                 pbe_nid=OBJ_txt2nid(*args);
107                                 if(pbe_nid == NID_undef) {
108                                         BIO_printf(bio_err,
109                                                  "Unknown PBE algorithm %s\n", *args);
110                                         badarg = 1;
111                                 }
112                         } else badarg = 1;
113                 } else if (!strcmp(*args,"-inform")) {
114                         if (args[1]) {
115                                 args++;
116                                 informat=str2fmt(*args);
117                         } else badarg = 1;
118                 } else if (!strcmp(*args,"-outform")) {
119                         if (args[1]) {
120                                 args++;
121                                 outformat=str2fmt(*args);
122                         } else badarg = 1;
123                 } else if (!strcmp (*args, "-topk8")) topk8 = 1;
124                 else if (!strcmp (*args, "-noiter")) iter = 1;
125                 else if (!strcmp (*args, "-nocrypt")) nocrypt = 1;
126                 else if (!strcmp (*args, "-nooct")) p8_broken = PKCS8_NO_OCTET;
127                 else if (!strcmp(*args,"-passin"))
128                         {
129                         if (!args[1]) goto bad;
130                         passin= *(++args);
131                         }
132                 else if (!strcmp(*args,"-envpassin"))
133                         {
134                         if (!args[1]) goto bad;
135                         if(!(passin= getenv(*(++args))))
136                                 {
137                                 BIO_printf(bio_err,
138                                  "Can't read environment variable %s\n",
139                                                                 *args);
140                                 badarg = 1;
141                                 }
142                         }
143                 else if (strcmp(*args,"-envpassout") == 0)
144                         {
145                         if (!args[1]) goto bad;
146                         if(!(passout= getenv(*(++args))))
147                                 {
148                                 BIO_printf(bio_err,
149                                  "Can't read environment variable %s\n",
150                                                                 *args);
151                                 badarg = 1;
152                                 }
153                         }
154                 else if (!strcmp(*args,"-passout"))
155                         {
156                         if (!args[1]) goto bad;
157                         passout= *(++args);
158                         }
159                 else if (!strcmp (*args, "-in")) {
160                         if (args[1]) {
161                                 args++;
162                                 infile = *args;
163                         } else badarg = 1;
164                 } else if (!strcmp (*args, "-out")) {
165                         if (args[1]) {
166                                 args++;
167                                 outfile = *args;
168                         } else badarg = 1;
169                 } else badarg = 1;
170                 args++;
171         }
172
173         if (badarg) {
174                 bad:
175                 BIO_printf(bio_err, "Usage pkcs8 [options]\n");
176                 BIO_printf(bio_err, "where options are\n");
177                 BIO_printf(bio_err, "-in file   input file\n");
178                 BIO_printf(bio_err, "-inform X  input format (DER or PEM)\n");
179                 BIO_printf(bio_err, "-passin arg     input file pass phrase\n");
180                 BIO_printf(bio_err, "-envpassin arg  environment variable containing input file pass phrase\n");
181                 BIO_printf(bio_err, "-outform X output format (DER or PEM)\n");
182                 BIO_printf(bio_err, "-out file  output file\n");
183                 BIO_printf(bio_err, "-passout arg    input file pass phrase\n");
184                 BIO_printf(bio_err, "-envpassout arg environment variable containing input file pass phrase\n");
185                 BIO_printf(bio_err, "-topk8     output PKCS8 file\n");
186                 BIO_printf(bio_err, "-nooct     use (broken) no octet form\n");
187                 BIO_printf(bio_err, "-noiter    use 1 as iteration count\n");
188                 BIO_printf(bio_err, "-nocrypt   use or expect unencrypted private key\n");
189                 BIO_printf(bio_err, "-v2 alg    use PKCS#5 v2.0 and cipher \"alg\"\n");
190                 BIO_printf(bio_err, "-v1 obj    use PKCS#5 v1.5 and cipher \"alg\"\n");
191                 return (1);
192         }
193
194         if ((pbe_nid == -1) && !cipher) pbe_nid = NID_pbeWithMD5AndDES_CBC;
195
196         if (infile) {
197                 if (!(in = BIO_new_file(infile, "rb"))) {
198                         BIO_printf(bio_err,
199                                  "Can't open input file %s\n", infile);
200                         return (1);
201                 }
202         } else in = BIO_new_fp (stdin, BIO_NOCLOSE);
203
204         if (outfile) {
205                 if (!(out = BIO_new_file (outfile, "wb"))) {
206                         BIO_printf(bio_err,
207                                  "Can't open output file %s\n", outfile);
208                         return (1);
209                 }
210         } else out = BIO_new_fp (stdout, BIO_NOCLOSE);
211
212         if (topk8) {
213                 if(informat == FORMAT_PEM)
214                         pkey = PEM_read_bio_PrivateKey(in, NULL, PEM_cb, passin);
215                 else if(informat == FORMAT_ASN1)
216                         pkey = d2i_PrivateKey_bio(in, NULL);
217                 else {
218                         BIO_printf(bio_err, "Bad format specified for key\n");
219                         return (1);
220                 }
221                 if (!pkey) {
222                         BIO_printf(bio_err, "Error reading key\n", outfile);
223                         ERR_print_errors(bio_err);
224                         return (1);
225                 }
226                 BIO_free(in);
227                 if (!(p8inf = EVP_PKEY2PKCS8(pkey))) {
228                         BIO_printf(bio_err, "Error converting key\n", outfile);
229                         ERR_print_errors(bio_err);
230                         return (1);
231                 }
232                 PKCS8_set_broken(p8inf, p8_broken);
233                 if(nocrypt) {
234                         if(outformat == FORMAT_PEM) 
235                                 PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8inf);
236                         else if(outformat == FORMAT_ASN1)
237                                 i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8inf);
238                         else {
239                                 BIO_printf(bio_err, "Bad format specified for key\n");
240                                 return (1);
241                         }
242                 } else {
243                         if(!passout) {
244                                 passout = pass;
245                                 EVP_read_pw_string(pass, 50, "Enter Encryption Password:", 1);
246                         }
247                         if (!(p8 = PKCS8_encrypt(pbe_nid, cipher,
248                                         passout, strlen(passout),
249                                         NULL, 0, iter, p8inf))) {
250                                 BIO_printf(bio_err, "Error encrypting key\n",
251                                                                  outfile);
252                                 ERR_print_errors(bio_err);
253                                 return (1);
254                         }
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                                 return (1);
262                         }
263                         X509_SIG_free(p8);
264                 }
265                 PKCS8_PRIV_KEY_INFO_free (p8inf);
266                 EVP_PKEY_free(pkey);
267                 BIO_free(out);
268                 return (0);
269         }
270
271         if(nocrypt) {
272                 if(informat == FORMAT_PEM) 
273                         p8inf = PEM_read_bio_PKCS8_PRIV_KEY_INFO(in,NULL,NULL, NULL);
274                 else if(informat == FORMAT_ASN1)
275                         p8inf = d2i_PKCS8_PRIV_KEY_INFO_bio(in, NULL);
276                 else {
277                         BIO_printf(bio_err, "Bad format specified for key\n");
278                         return (1);
279                 }
280         } else {
281                 if(informat == FORMAT_PEM) 
282                         p8 = PEM_read_bio_PKCS8(in, NULL, NULL, NULL);
283                 else if(informat == FORMAT_ASN1)
284                         p8 = d2i_PKCS8_bio(in, NULL);
285                 else {
286                         BIO_printf(bio_err, "Bad format specified for key\n");
287                         return (1);
288                 }
289
290                 if (!p8) {
291                         BIO_printf (bio_err, "Error reading key\n", outfile);
292                         ERR_print_errors(bio_err);
293                         return (1);
294                 }
295                 if(!passin) {
296                         passin = pass;
297                         EVP_read_pw_string(pass, 50, "Enter Password:", 0);
298                 }
299                 p8inf = M_PKCS8_decrypt(p8, passin, strlen(passin));
300                 X509_SIG_free(p8);
301         }
302
303         if (!p8inf) {
304                 BIO_printf(bio_err, "Error decrypting key\n", outfile);
305                 ERR_print_errors(bio_err);
306                 return (1);
307         }
308
309         if (!(pkey = EVP_PKCS82PKEY(p8inf))) {
310                 BIO_printf(bio_err, "Error converting key\n", outfile);
311                 ERR_print_errors(bio_err);
312                 return (1);
313         }
314         
315         if (p8inf->broken) {
316                 BIO_printf(bio_err, "Warning: broken key encoding: ");
317                 switch (p8inf->broken) {
318                         case PKCS8_NO_OCTET:
319                         BIO_printf(bio_err, "No Octet String\n");
320                         break;
321
322                         default:
323                         BIO_printf(bio_err, "Unknown broken type\n");
324                         break;
325                 }
326         }
327         
328         PKCS8_PRIV_KEY_INFO_free(p8inf);
329         if(outformat == FORMAT_PEM) 
330                 PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, PEM_cb, passout);
331         else if(outformat == FORMAT_ASN1)
332                 i2d_PrivateKey_bio(out, pkey);
333         else {
334                 BIO_printf(bio_err, "Bad format specified for key\n");
335                         return (1);
336         }
337
338         EVP_PKEY_free(pkey);
339         BIO_free(out);
340         BIO_free(in);
341
342         return (0);
343 }