If an engine comes up explicitely, it must also come down explicitely
[oweals/openssl.git] / apps / ec.c
1 /* apps/ec.c */
2 /*
3  * Written by Nils Larsch for the OpenSSL project.
4  */
5 /* ====================================================================
6  * Copyright (c) 1998-2005 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  *    openssl-core@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
59 #include <openssl/opensslconf.h>
60 #ifndef OPENSSL_NO_EC
61 # include <stdio.h>
62 # include <stdlib.h>
63 # include <string.h>
64 # include "apps.h"
65 # include <openssl/bio.h>
66 # include <openssl/err.h>
67 # include <openssl/evp.h>
68 # include <openssl/pem.h>
69
70 # undef PROG
71 # define PROG    ec_main
72
73 /*-
74  * -inform arg    - input format - default PEM (one of DER, NET or PEM)
75  * -outform arg   - output format - default PEM
76  * -in arg        - input file - default stdin
77  * -out arg       - output file - default stdout
78  * -des           - encrypt output if PEM format with DES in cbc mode
79  * -text          - print a text version
80  * -param_out     - print the elliptic curve parameters
81  * -conv_form arg - specifies the point encoding form
82  * -param_enc arg - specifies the parameter encoding
83  */
84
85 int MAIN(int, char **);
86
87 int MAIN(int argc, char **argv)
88 {
89     int ret = 1;
90     EC_KEY *eckey = NULL;
91     const EC_GROUP *group;
92     int i, badops = 0;
93     const EVP_CIPHER *enc = NULL;
94     BIO *in = NULL, *out = NULL;
95     int informat, outformat, text = 0, noout = 0;
96     int pubin = 0, pubout = 0, param_out = 0;
97     char *infile, *outfile, *prog, *engine;
98 # ifndef OPENSSL_NO_ENGINE
99     ENGINE *e = NULL;
100 # endif
101     char *passargin = NULL, *passargout = NULL;
102     char *passin = NULL, *passout = NULL;
103     point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
104     int new_form = 0;
105     int asn1_flag = OPENSSL_EC_NAMED_CURVE;
106     int new_asn1_flag = 0;
107
108     apps_startup();
109
110     if (bio_err == NULL)
111         if ((bio_err = BIO_new(BIO_s_file())) != NULL)
112             BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
113
114     if (!load_config(bio_err, NULL))
115         goto end;
116
117     engine = NULL;
118     infile = NULL;
119     outfile = NULL;
120     informat = FORMAT_PEM;
121     outformat = FORMAT_PEM;
122
123     prog = argv[0];
124     argc--;
125     argv++;
126     while (argc >= 1) {
127         if (strcmp(*argv, "-inform") == 0) {
128             if (--argc < 1)
129                 goto bad;
130             informat = str2fmt(*(++argv));
131         } else if (strcmp(*argv, "-outform") == 0) {
132             if (--argc < 1)
133                 goto bad;
134             outformat = str2fmt(*(++argv));
135         } else if (strcmp(*argv, "-in") == 0) {
136             if (--argc < 1)
137                 goto bad;
138             infile = *(++argv);
139         } else if (strcmp(*argv, "-out") == 0) {
140             if (--argc < 1)
141                 goto bad;
142             outfile = *(++argv);
143         } else if (strcmp(*argv, "-passin") == 0) {
144             if (--argc < 1)
145                 goto bad;
146             passargin = *(++argv);
147         } else if (strcmp(*argv, "-passout") == 0) {
148             if (--argc < 1)
149                 goto bad;
150             passargout = *(++argv);
151         } else if (strcmp(*argv, "-engine") == 0) {
152             if (--argc < 1)
153                 goto bad;
154             engine = *(++argv);
155         } else if (strcmp(*argv, "-noout") == 0)
156             noout = 1;
157         else if (strcmp(*argv, "-text") == 0)
158             text = 1;
159         else if (strcmp(*argv, "-conv_form") == 0) {
160             if (--argc < 1)
161                 goto bad;
162             ++argv;
163             new_form = 1;
164             if (strcmp(*argv, "compressed") == 0)
165                 form = POINT_CONVERSION_COMPRESSED;
166             else if (strcmp(*argv, "uncompressed") == 0)
167                 form = POINT_CONVERSION_UNCOMPRESSED;
168             else if (strcmp(*argv, "hybrid") == 0)
169                 form = POINT_CONVERSION_HYBRID;
170             else
171                 goto bad;
172         } else if (strcmp(*argv, "-param_enc") == 0) {
173             if (--argc < 1)
174                 goto bad;
175             ++argv;
176             new_asn1_flag = 1;
177             if (strcmp(*argv, "named_curve") == 0)
178                 asn1_flag = OPENSSL_EC_NAMED_CURVE;
179             else if (strcmp(*argv, "explicit") == 0)
180                 asn1_flag = 0;
181             else
182                 goto bad;
183         } else if (strcmp(*argv, "-param_out") == 0)
184             param_out = 1;
185         else if (strcmp(*argv, "-pubin") == 0)
186             pubin = 1;
187         else if (strcmp(*argv, "-pubout") == 0)
188             pubout = 1;
189         else if ((enc = EVP_get_cipherbyname(&(argv[0][1]))) == NULL) {
190             BIO_printf(bio_err, "unknown option %s\n", *argv);
191             badops = 1;
192             break;
193         }
194         argc--;
195         argv++;
196     }
197
198     if (badops) {
199  bad:
200         BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
201         BIO_printf(bio_err, "where options are\n");
202         BIO_printf(bio_err, " -inform arg     input format - "
203                    "DER or PEM\n");
204         BIO_printf(bio_err, " -outform arg    output format - "
205                    "DER or PEM\n");
206         BIO_printf(bio_err, " -in arg         input file\n");
207         BIO_printf(bio_err, " -passin arg     input file pass "
208                    "phrase source\n");
209         BIO_printf(bio_err, " -out arg        output file\n");
210         BIO_printf(bio_err, " -passout arg    output file pass "
211                    "phrase source\n");
212         BIO_printf(bio_err, " -engine e       use engine e, "
213                    "possibly a hardware device.\n");
214         BIO_printf(bio_err, " -des            encrypt PEM output, "
215                    "instead of 'des' every other \n"
216                    "                 cipher "
217                    "supported by OpenSSL can be used\n");
218         BIO_printf(bio_err, " -text           print the key\n");
219         BIO_printf(bio_err, " -noout          don't print key out\n");
220         BIO_printf(bio_err, " -param_out      print the elliptic "
221                    "curve parameters\n");
222         BIO_printf(bio_err, " -conv_form arg  specifies the "
223                    "point conversion form \n");
224         BIO_printf(bio_err, "                 possible values:"
225                    " compressed\n");
226         BIO_printf(bio_err, "                                 "
227                    " uncompressed (default)\n");
228         BIO_printf(bio_err, "                                  " " hybrid\n");
229         BIO_printf(bio_err, " -param_enc arg  specifies the way"
230                    " the ec parameters are encoded\n");
231         BIO_printf(bio_err, "                 in the asn1 der " "encoding\n");
232         BIO_printf(bio_err, "                 possible values:"
233                    " named_curve (default)\n");
234         BIO_printf(bio_err, "                                  "
235                    "explicit\n");
236         goto end;
237     }
238
239     ERR_load_crypto_strings();
240
241 # ifndef OPENSSL_NO_ENGINE
242     e = setup_engine(bio_err, engine, 0);
243 # endif
244
245     if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
246         BIO_printf(bio_err, "Error getting passwords\n");
247         goto end;
248     }
249
250     in = BIO_new(BIO_s_file());
251     out = BIO_new(BIO_s_file());
252     if ((in == NULL) || (out == NULL)) {
253         ERR_print_errors(bio_err);
254         goto end;
255     }
256
257     if (infile == NULL)
258         BIO_set_fp(in, stdin, BIO_NOCLOSE);
259     else {
260         if (BIO_read_filename(in, infile) <= 0) {
261             perror(infile);
262             goto end;
263         }
264     }
265
266     BIO_printf(bio_err, "read EC key\n");
267     if (informat == FORMAT_ASN1) {
268         if (pubin)
269             eckey = d2i_EC_PUBKEY_bio(in, NULL);
270         else
271             eckey = d2i_ECPrivateKey_bio(in, NULL);
272     } else if (informat == FORMAT_PEM) {
273         if (pubin)
274             eckey = PEM_read_bio_EC_PUBKEY(in, NULL, NULL, NULL);
275         else
276             eckey = PEM_read_bio_ECPrivateKey(in, NULL, NULL, passin);
277     } else {
278         BIO_printf(bio_err, "bad input format specified for key\n");
279         goto end;
280     }
281     if (eckey == NULL) {
282         BIO_printf(bio_err, "unable to load Key\n");
283         ERR_print_errors(bio_err);
284         goto end;
285     }
286
287     if (outfile == NULL) {
288         BIO_set_fp(out, stdout, BIO_NOCLOSE);
289 # ifdef OPENSSL_SYS_VMS
290         {
291             BIO *tmpbio = BIO_new(BIO_f_linebuffer());
292             out = BIO_push(tmpbio, out);
293         }
294 # endif
295     } else {
296         if (BIO_write_filename(out, outfile) <= 0) {
297             perror(outfile);
298             goto end;
299         }
300     }
301
302     group = EC_KEY_get0_group(eckey);
303
304     if (new_form)
305         EC_KEY_set_conv_form(eckey, form);
306
307     if (new_asn1_flag)
308         EC_KEY_set_asn1_flag(eckey, asn1_flag);
309
310     if (text)
311         if (!EC_KEY_print(out, eckey, 0)) {
312             perror(outfile);
313             ERR_print_errors(bio_err);
314             goto end;
315         }
316
317     if (noout) {
318         ret = 0;
319         goto end;
320     }
321
322     BIO_printf(bio_err, "writing EC key\n");
323     if (outformat == FORMAT_ASN1) {
324         if (param_out)
325             i = i2d_ECPKParameters_bio(out, group);
326         else if (pubin || pubout)
327             i = i2d_EC_PUBKEY_bio(out, eckey);
328         else
329             i = i2d_ECPrivateKey_bio(out, eckey);
330     } else if (outformat == FORMAT_PEM) {
331         if (param_out)
332             i = PEM_write_bio_ECPKParameters(out, group);
333         else if (pubin || pubout)
334             i = PEM_write_bio_EC_PUBKEY(out, eckey);
335         else
336             i = PEM_write_bio_ECPrivateKey(out, eckey, enc,
337                                            NULL, 0, NULL, passout);
338     } else {
339         BIO_printf(bio_err, "bad output format specified for " "outfile\n");
340         goto end;
341     }
342
343     if (!i) {
344         BIO_printf(bio_err, "unable to write private key\n");
345         ERR_print_errors(bio_err);
346     } else
347         ret = 0;
348  end:
349     if (in)
350         BIO_free(in);
351     if (out)
352         BIO_free_all(out);
353     if (eckey)
354         EC_KEY_free(eckey);
355 # ifndef OPENSSL_NO_ENGINE
356     if (e != NULL)
357         release_engine(e);
358 # endif
359     if (passin)
360         OPENSSL_free(passin);
361     if (passout)
362         OPENSSL_free(passout);
363     apps_shutdown();
364     OPENSSL_EXIT(ret);
365 }
366 #else                           /* !OPENSSL_NO_EC */
367
368 # if PEDANTIC
369 static void *dummy = &dummy;
370 # endif
371
372 #endif