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