a3fecd412a17a394c09ff76884f8c2b90ff2d33d
[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     OPT_NO_PUBLIC, OPT_CHECK
88 } OPTION_CHOICE;
89
90 OPTIONS ec_options[] = {
91     {"help", OPT_HELP, '-', "Display this summary"},
92     {"in", OPT_IN, '<', "Input file"},
93     {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
94     {"out", OPT_OUT, '>', "Output file"},
95     {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
96     {"noout", OPT_NOOUT, '-', "Don't print key out"},
97     {"text", OPT_TEXT, '-', "Print the key"},
98     {"param_out", OPT_PARAM_OUT, '-', "Print the elliptic curve parameters"},
99     {"pubin", OPT_PUBIN, '-'},
100     {"pubout", OPT_PUBOUT, '-'},
101     {"no_public", OPT_NO_PUBLIC, '-', "exclude public key from private key"},
102     {"check", OPT_CHECK, '-', "check key consistency"},
103     {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
104     {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
105     {"param_enc", OPT_PARAM_ENC, 's',
106      "Specifies the way the ec parameters are encoded"},
107     {"conv_form", OPT_CONV_FORM, 's', "Specifies the point conversion form "},
108     {"", OPT_CIPHER, '-', "Any supported cipher"},
109 # ifndef OPENSSL_NO_ENGINE
110     {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
111 # endif
112     {NULL}
113 };
114
115 int ec_main(int argc, char **argv)
116 {
117     BIO *in = NULL, *out = NULL;
118     EC_KEY *eckey = NULL;
119     const EC_GROUP *group;
120     const EVP_CIPHER *enc = NULL;
121     point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
122     char *infile = NULL, *outfile = NULL, *prog;
123     char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
124     OPTION_CHOICE o;
125     int asn1_flag = OPENSSL_EC_NAMED_CURVE, new_form = 0, new_asn1_flag = 0;
126     int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, noout = 0;
127     int pubin = 0, pubout = 0, param_out = 0, i, ret = 1, private = 0;
128     int no_public = 0, check = 0;
129
130     prog = opt_init(argc, argv, ec_options);
131     while ((o = opt_next()) != OPT_EOF) {
132         switch (o) {
133         case OPT_EOF:
134         case OPT_ERR:
135  opthelp:
136             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
137             goto end;
138         case OPT_HELP:
139             opt_help(ec_options);
140             ret = 0;
141             goto end;
142         case OPT_INFORM:
143             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
144                 goto opthelp;
145             break;
146         case OPT_IN:
147             infile = opt_arg();
148             break;
149         case OPT_OUTFORM:
150             if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
151                 goto opthelp;
152             break;
153         case OPT_OUT:
154             outfile = opt_arg();
155             break;
156         case OPT_NOOUT:
157             noout = 1;
158             break;
159         case OPT_TEXT:
160             text = 1;
161             break;
162         case OPT_PARAM_OUT:
163             param_out = 1;
164             break;
165         case OPT_PUBIN:
166             pubin = 1;
167             break;
168         case OPT_PUBOUT:
169             pubout = 1;
170             break;
171         case OPT_PASSIN:
172             passinarg = opt_arg();
173             break;
174         case OPT_PASSOUT:
175             passoutarg = opt_arg();
176             break;
177         case OPT_ENGINE:
178             (void)setup_engine(opt_arg(), 0);
179             break;
180         case OPT_CIPHER:
181             if (!opt_cipher(opt_unknown(), &enc))
182                 goto opthelp;
183             break;
184         case OPT_CONV_FORM:
185             if (!opt_pair(opt_arg(), conv_forms, &i))
186                 goto opthelp;
187             new_form = 1;
188             form = i;
189             break;
190         case OPT_PARAM_ENC:
191             if (!opt_pair(opt_arg(), param_enc, &i))
192                 goto opthelp;
193             new_asn1_flag = 1;
194             asn1_flag = i;
195             break;
196         case OPT_NO_PUBLIC:
197             no_public = 1;
198             break;
199         case OPT_CHECK:
200             check = 1;
201             break;
202         }
203     }
204     argc = opt_num_rest();
205     argv = opt_rest();
206     private = param_out || pubin || pubout ? 0 : 1;
207     if (text && !pubin)
208         private = 1;
209
210     if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
211         BIO_printf(bio_err, "Error getting passwords\n");
212         goto end;
213     }
214
215     in = bio_open_default(infile, 'r', informat);
216     if (in == NULL)
217         goto end;
218
219     BIO_printf(bio_err, "read EC key\n");
220     if (informat == FORMAT_ASN1) {
221         if (pubin)
222             eckey = d2i_EC_PUBKEY_bio(in, NULL);
223         else
224             eckey = d2i_ECPrivateKey_bio(in, NULL);
225     } else {
226         if (pubin)
227             eckey = PEM_read_bio_EC_PUBKEY(in, NULL, NULL, NULL);
228         else
229             eckey = PEM_read_bio_ECPrivateKey(in, NULL, NULL, passin);
230     }
231     if (eckey == NULL) {
232         BIO_printf(bio_err, "unable to load Key\n");
233         ERR_print_errors(bio_err);
234         goto end;
235     }
236
237     out = bio_open_owner(outfile, outformat, private);
238     if (out == NULL)
239         goto end;
240
241     group = EC_KEY_get0_group(eckey);
242
243     if (new_form)
244         EC_KEY_set_conv_form(eckey, form);
245
246     if (new_asn1_flag)
247         EC_KEY_set_asn1_flag(eckey, asn1_flag);
248
249     if (no_public)
250         EC_KEY_set_enc_flags(eckey, EC_PKEY_NO_PUBKEY);
251
252     if (text) {
253         assert(pubin || private);
254         if (!EC_KEY_print(out, eckey, 0)) {
255             perror(outfile);
256             ERR_print_errors(bio_err);
257             goto end;
258         }
259     }
260
261     if (check) {
262         if (EC_KEY_check_key(eckey) == 1) {
263             BIO_printf(bio_err, "EC Key valid.\n");
264         } else {
265             BIO_printf(bio_err, "EC Key Invalid!\n");
266             ERR_print_errors(bio_err);
267         }
268     }
269
270     if (noout) {
271         ret = 0;
272         goto end;
273     }
274
275     BIO_printf(bio_err, "writing EC key\n");
276     if (outformat == FORMAT_ASN1) {
277         if (param_out)
278             i = i2d_ECPKParameters_bio(out, group);
279         else if (pubin || pubout)
280             i = i2d_EC_PUBKEY_bio(out, eckey);
281         else {
282             assert(private);
283             i = i2d_ECPrivateKey_bio(out, eckey);
284         }
285     } else {
286         if (param_out)
287             i = PEM_write_bio_ECPKParameters(out, group);
288         else if (pubin || pubout)
289             i = PEM_write_bio_EC_PUBKEY(out, eckey);
290         else {
291             assert(private);
292             i = PEM_write_bio_ECPrivateKey(out, eckey, enc,
293                                            NULL, 0, NULL, passout);
294         }
295     }
296
297     if (!i) {
298         BIO_printf(bio_err, "unable to write private key\n");
299         ERR_print_errors(bio_err);
300     } else
301         ret = 0;
302  end:
303     BIO_free(in);
304     BIO_free_all(out);
305     EC_KEY_free(eckey);
306     OPENSSL_free(passin);
307     OPENSSL_free(passout);
308     return (ret);
309 }
310 #else                           /* !OPENSSL_NO_EC */
311
312 # if PEDANTIC
313 static void *dummy = &dummy;
314 # endif
315
316 #endif