Don't try and parse boolean type.
[openssl.git] / apps / ec.c
1 /*
2  * Written by Nils Larsch for the OpenSSL project.
3  */
4 /* ====================================================================
5  * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. All advertising materials mentioning features or use of this
20  *    software must display the following acknowledgment:
21  *    "This product includes software developed by the OpenSSL Project
22  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
23  *
24  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25  *    endorse or promote products derived from this software without
26  *    prior written permission. For written permission, please contact
27  *    openssl-core@openssl.org.
28  *
29  * 5. Products derived from this software may not be called "OpenSSL"
30  *    nor may "OpenSSL" appear in their names without prior written
31  *    permission of the OpenSSL Project.
32  *
33  * 6. Redistributions of any form whatsoever must retain the following
34  *    acknowledgment:
35  *    "This product includes software developed by the OpenSSL Project
36  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49  * OF THE POSSIBILITY OF SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This product includes cryptographic software written by Eric Young
53  * (eay@cryptsoft.com).  This product includes software written by Tim
54  * Hudson (tjh@cryptsoft.com).
55  *
56  */
57
58 #include <openssl/opensslconf.h>
59 #ifndef OPENSSL_NO_EC
60 # include <stdio.h>
61 # include <stdlib.h>
62 # include <string.h>
63 # include "apps.h"
64 # include <openssl/bio.h>
65 # include <openssl/err.h>
66 # include <openssl/evp.h>
67 # include <openssl/pem.h>
68
69 static OPT_PAIR conv_forms[] = {
70     {"compressed", POINT_CONVERSION_COMPRESSED},
71     {"uncompressed", POINT_CONVERSION_UNCOMPRESSED},
72     {"hybrid", POINT_CONVERSION_HYBRID},
73     {NULL}
74 };
75
76 static OPT_PAIR param_enc[] = {
77     {"named_curve", OPENSSL_EC_NAMED_CURVE},
78     {"explicit", 0},
79     {NULL}
80 };
81
82 typedef enum OPTION_choice {
83     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
84     OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
85     OPT_NOOUT, OPT_TEXT, OPT_PARAM_OUT, OPT_PUBIN, OPT_PUBOUT,
86     OPT_PASSIN, OPT_PASSOUT, OPT_PARAM_ENC, OPT_CONV_FORM, OPT_CIPHER
87 } OPTION_CHOICE;
88
89 OPTIONS ec_options[] = {
90     {"help", OPT_HELP, '-', "Display this summary"},
91     {"in", OPT_IN, '<', "Input file"},
92     {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
93     {"out", OPT_OUT, '>', "Output file"},
94     {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
95     {"noout", OPT_NOOUT, '-', "Don't print key out"},
96     {"text", OPT_TEXT, '-', "Print the key"},
97     {"param_out", OPT_PARAM_OUT, '-', "Print the elliptic curve parameters"},
98     {"pubin", OPT_PUBIN, '-'},
99     {"pubout", OPT_PUBOUT, '-'},
100     {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
101     {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
102     {"param_enc", OPT_PARAM_ENC, 's',
103      "Specifies the way the ec parameters are encoded"},
104     {"conv_form", OPT_CONV_FORM, 's', "Specifies the point conversion form "},
105     {"", OPT_CIPHER, '-', "Any supported cipher"},
106 # ifndef OPENSSL_NO_ENGINE
107     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
108 # endif
109     {NULL}
110 };
111
112 int ec_main(int argc, char **argv)
113 {
114     BIO *in = NULL, *out = NULL;
115     EC_KEY *eckey = NULL;
116     const EC_GROUP *group;
117     const EVP_CIPHER *enc = NULL;
118     point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
119     char *infile = NULL, *outfile = NULL, *prog;
120     char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
121     OPTION_CHOICE o;
122     int asn1_flag = OPENSSL_EC_NAMED_CURVE, new_form = 0, new_asn1_flag = 0;
123     int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, noout = 0;
124     int pubin = 0, pubout = 0, param_out = 0, i, ret = 1, private = 0;
125
126     prog = opt_init(argc, argv, ec_options);
127     while ((o = opt_next()) != OPT_EOF) {
128         switch (o) {
129         case OPT_EOF:
130         case OPT_ERR:
131  opthelp:
132             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
133             goto end;
134         case OPT_HELP:
135             opt_help(ec_options);
136             ret = 0;
137             goto end;
138         case OPT_INFORM:
139             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
140                 goto opthelp;
141             break;
142         case OPT_IN:
143             infile = opt_arg();
144             break;
145         case OPT_OUTFORM:
146             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
147                 goto opthelp;
148             break;
149         case OPT_OUT:
150             outfile = opt_arg();
151             break;
152         case OPT_NOOUT:
153             noout = 1;
154             break;
155         case OPT_TEXT:
156             text = 1;
157             break;
158         case OPT_PARAM_OUT:
159             param_out = 1;
160             break;
161         case OPT_PUBIN:
162             pubin = 1;
163             break;
164         case OPT_PUBOUT:
165             pubout = 1;
166             break;
167         case OPT_PASSIN:
168             passinarg = opt_arg();
169             break;
170         case OPT_PASSOUT:
171             passoutarg = opt_arg();
172             break;
173         case OPT_ENGINE:
174             (void)setup_engine(opt_arg(), 0);
175             break;
176         case OPT_CIPHER:
177             if (!opt_cipher(opt_unknown(), &enc))
178                 goto opthelp;
179             break;
180         case OPT_CONV_FORM:
181             if (!opt_pair(opt_arg(), conv_forms, &i))
182                 goto opthelp;
183             new_form = 1;
184             form = i;
185             break;
186         case OPT_PARAM_ENC:
187             if (!opt_pair(opt_arg(), param_enc, &i))
188                 goto opthelp;
189             new_asn1_flag = 1;
190             asn1_flag = i;
191             break;
192         }
193     }
194     argc = opt_num_rest();
195     argv = opt_rest();
196     private = param_out || pubin || pubout ? 0 : 1;
197     if (text)
198         private = 1;
199
200     if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
201         BIO_printf(bio_err, "Error getting passwords\n");
202         goto end;
203     }
204
205     if (!app_load_modules(NULL))
206         goto end;
207
208     in = bio_open_default(infile, 'r', informat);
209     if (in == NULL)
210         goto end;
211
212     BIO_printf(bio_err, "read EC key\n");
213     if (informat == FORMAT_ASN1) {
214         if (pubin)
215             eckey = d2i_EC_PUBKEY_bio(in, NULL);
216         else
217             eckey = d2i_ECPrivateKey_bio(in, NULL);
218     } else {
219         if (pubin)
220             eckey = PEM_read_bio_EC_PUBKEY(in, NULL, NULL, NULL);
221         else
222             eckey = PEM_read_bio_ECPrivateKey(in, NULL, NULL, passin);
223     }
224     if (eckey == NULL) {
225         BIO_printf(bio_err, "unable to load Key\n");
226         ERR_print_errors(bio_err);
227         goto end;
228     }
229
230     out = bio_open_owner(outfile, outformat, private);
231     if (out == NULL)
232         goto end;
233
234     group = EC_KEY_get0_group(eckey);
235
236     if (new_form)
237         EC_KEY_set_conv_form(eckey, form);
238
239     if (new_asn1_flag)
240         EC_KEY_set_asn1_flag(eckey, asn1_flag);
241
242     if (text) {
243         assert(private);
244         if (!EC_KEY_print(out, eckey, 0)) {
245             perror(outfile);
246             ERR_print_errors(bio_err);
247             goto end;
248         }
249     }
250
251     if (noout) {
252         ret = 0;
253         goto end;
254     }
255
256     BIO_printf(bio_err, "writing EC key\n");
257     if (outformat == FORMAT_ASN1) {
258         if (param_out)
259             i = i2d_ECPKParameters_bio(out, group);
260         else if (pubin || pubout)
261             i = i2d_EC_PUBKEY_bio(out, eckey);
262         else {
263             assert(private);
264             i = i2d_ECPrivateKey_bio(out, eckey);
265         }
266     } else {
267         if (param_out)
268             i = PEM_write_bio_ECPKParameters(out, group);
269         else if (pubin || pubout)
270             i = PEM_write_bio_EC_PUBKEY(out, eckey);
271         else {
272             assert(private);
273             i = PEM_write_bio_ECPrivateKey(out, eckey, enc,
274                                            NULL, 0, NULL, passout);
275         }
276     }
277
278     if (!i) {
279         BIO_printf(bio_err, "unable to write private key\n");
280         ERR_print_errors(bio_err);
281     } else
282         ret = 0;
283  end:
284     BIO_free(in);
285     BIO_free_all(out);
286     EC_KEY_free(eckey);
287     OPENSSL_free(passin);
288     OPENSSL_free(passout);
289     return (ret);
290 }
291 #else                           /* !OPENSSL_NO_EC */
292
293 # if PEDANTIC
294 static void *dummy = &dummy;
295 # endif
296
297 #endif