xref: /minix3/crypto/external/bsd/openssl/dist/apps/ec.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* apps/ec.c */
2ebfedea0SLionel Sambuc /*
3ebfedea0SLionel Sambuc  * Written by Nils Larsch for the OpenSSL project.
4ebfedea0SLionel Sambuc  */
5ebfedea0SLionel Sambuc /* ====================================================================
6ebfedea0SLionel Sambuc  * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
17ebfedea0SLionel Sambuc  *    the documentation and/or other materials provided with the
18ebfedea0SLionel Sambuc  *    distribution.
19ebfedea0SLionel Sambuc  *
20ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this
21ebfedea0SLionel Sambuc  *    software must display the following acknowledgment:
22ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
23ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24ebfedea0SLionel Sambuc  *
25ebfedea0SLionel Sambuc  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26ebfedea0SLionel Sambuc  *    endorse or promote products derived from this software without
27ebfedea0SLionel Sambuc  *    prior written permission. For written permission, please contact
28ebfedea0SLionel Sambuc  *    openssl-core@openssl.org.
29ebfedea0SLionel Sambuc  *
30ebfedea0SLionel Sambuc  * 5. Products derived from this software may not be called "OpenSSL"
31ebfedea0SLionel Sambuc  *    nor may "OpenSSL" appear in their names without prior written
32ebfedea0SLionel Sambuc  *    permission of the OpenSSL Project.
33ebfedea0SLionel Sambuc  *
34ebfedea0SLionel Sambuc  * 6. Redistributions of any form whatsoever must retain the following
35ebfedea0SLionel Sambuc  *    acknowledgment:
36ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
37ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38ebfedea0SLionel Sambuc  *
39ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40ebfedea0SLionel Sambuc  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43ebfedea0SLionel Sambuc  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44ebfedea0SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45ebfedea0SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46ebfedea0SLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48ebfedea0SLionel Sambuc  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50ebfedea0SLionel Sambuc  * OF THE POSSIBILITY OF SUCH DAMAGE.
51ebfedea0SLionel Sambuc  * ====================================================================
52ebfedea0SLionel Sambuc  *
53ebfedea0SLionel Sambuc  * This product includes cryptographic software written by Eric Young
54ebfedea0SLionel Sambuc  * (eay@cryptsoft.com).  This product includes software written by Tim
55ebfedea0SLionel Sambuc  * Hudson (tjh@cryptsoft.com).
56ebfedea0SLionel Sambuc  *
57ebfedea0SLionel Sambuc  */
58ebfedea0SLionel Sambuc 
59ebfedea0SLionel Sambuc #include <openssl/opensslconf.h>
60ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_EC
61ebfedea0SLionel Sambuc # include <stdio.h>
62ebfedea0SLionel Sambuc # include <stdlib.h>
63ebfedea0SLionel Sambuc # include <string.h>
64ebfedea0SLionel Sambuc # include "apps.h"
65ebfedea0SLionel Sambuc # include <openssl/bio.h>
66ebfedea0SLionel Sambuc # include <openssl/err.h>
67ebfedea0SLionel Sambuc # include <openssl/evp.h>
68ebfedea0SLionel Sambuc # include <openssl/pem.h>
69ebfedea0SLionel Sambuc 
70ebfedea0SLionel Sambuc # undef PROG
71ebfedea0SLionel Sambuc # define PROG    ec_main
72ebfedea0SLionel Sambuc 
73*0a6a1f1dSLionel Sambuc /*-
74*0a6a1f1dSLionel Sambuc  * -inform arg    - input format - default PEM (one of DER, NET or PEM)
75ebfedea0SLionel Sambuc  * -outform arg   - output format - default PEM
76ebfedea0SLionel Sambuc  * -in arg        - input file - default stdin
77ebfedea0SLionel Sambuc  * -out arg       - output file - default stdout
78ebfedea0SLionel Sambuc  * -des           - encrypt output if PEM format with DES in cbc mode
79ebfedea0SLionel Sambuc  * -text          - print a text version
80ebfedea0SLionel Sambuc  * -param_out     - print the elliptic curve parameters
81ebfedea0SLionel Sambuc  * -conv_form arg - specifies the point encoding form
82ebfedea0SLionel Sambuc  * -param_enc arg - specifies the parameter encoding
83ebfedea0SLionel Sambuc  */
84ebfedea0SLionel Sambuc 
85ebfedea0SLionel Sambuc int MAIN(int, char **);
86ebfedea0SLionel Sambuc 
MAIN(int argc,char ** argv)87ebfedea0SLionel Sambuc int MAIN(int argc, char **argv)
88ebfedea0SLionel Sambuc {
89ebfedea0SLionel Sambuc     int ret = 1;
90ebfedea0SLionel Sambuc     EC_KEY *eckey = NULL;
91ebfedea0SLionel Sambuc     const EC_GROUP *group;
92ebfedea0SLionel Sambuc     int i, badops = 0;
93ebfedea0SLionel Sambuc     const EVP_CIPHER *enc = NULL;
94ebfedea0SLionel Sambuc     BIO *in = NULL, *out = NULL;
95ebfedea0SLionel Sambuc     int informat, outformat, text = 0, noout = 0;
96ebfedea0SLionel Sambuc     int pubin = 0, pubout = 0, param_out = 0;
97ebfedea0SLionel Sambuc     char *infile, *outfile, *prog, *engine;
98ebfedea0SLionel Sambuc     char *passargin = NULL, *passargout = NULL;
99ebfedea0SLionel Sambuc     char *passin = NULL, *passout = NULL;
100ebfedea0SLionel Sambuc     point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
101ebfedea0SLionel Sambuc     int new_form = 0;
102ebfedea0SLionel Sambuc     int asn1_flag = OPENSSL_EC_NAMED_CURVE;
103ebfedea0SLionel Sambuc     int new_asn1_flag = 0;
104ebfedea0SLionel Sambuc 
105ebfedea0SLionel Sambuc     apps_startup();
106ebfedea0SLionel Sambuc 
107ebfedea0SLionel Sambuc     if (bio_err == NULL)
108ebfedea0SLionel Sambuc         if ((bio_err = BIO_new(BIO_s_file())) != NULL)
109ebfedea0SLionel Sambuc             BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
110ebfedea0SLionel Sambuc 
111ebfedea0SLionel Sambuc     if (!load_config(bio_err, NULL))
112ebfedea0SLionel Sambuc         goto end;
113ebfedea0SLionel Sambuc 
114ebfedea0SLionel Sambuc     engine = NULL;
115ebfedea0SLionel Sambuc     infile = NULL;
116ebfedea0SLionel Sambuc     outfile = NULL;
117ebfedea0SLionel Sambuc     informat = FORMAT_PEM;
118ebfedea0SLionel Sambuc     outformat = FORMAT_PEM;
119ebfedea0SLionel Sambuc 
120ebfedea0SLionel Sambuc     prog = argv[0];
121ebfedea0SLionel Sambuc     argc--;
122ebfedea0SLionel Sambuc     argv++;
123*0a6a1f1dSLionel Sambuc     while (argc >= 1) {
124*0a6a1f1dSLionel Sambuc         if (strcmp(*argv, "-inform") == 0) {
125*0a6a1f1dSLionel Sambuc             if (--argc < 1)
126*0a6a1f1dSLionel Sambuc                 goto bad;
127ebfedea0SLionel Sambuc             informat = str2fmt(*(++argv));
128*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-outform") == 0) {
129*0a6a1f1dSLionel Sambuc             if (--argc < 1)
130*0a6a1f1dSLionel Sambuc                 goto bad;
131ebfedea0SLionel Sambuc             outformat = str2fmt(*(++argv));
132*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-in") == 0) {
133*0a6a1f1dSLionel Sambuc             if (--argc < 1)
134*0a6a1f1dSLionel Sambuc                 goto bad;
135ebfedea0SLionel Sambuc             infile = *(++argv);
136*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-out") == 0) {
137*0a6a1f1dSLionel Sambuc             if (--argc < 1)
138*0a6a1f1dSLionel Sambuc                 goto bad;
139ebfedea0SLionel Sambuc             outfile = *(++argv);
140*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-passin") == 0) {
141*0a6a1f1dSLionel Sambuc             if (--argc < 1)
142*0a6a1f1dSLionel Sambuc                 goto bad;
143ebfedea0SLionel Sambuc             passargin = *(++argv);
144*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-passout") == 0) {
145*0a6a1f1dSLionel Sambuc             if (--argc < 1)
146*0a6a1f1dSLionel Sambuc                 goto bad;
147ebfedea0SLionel Sambuc             passargout = *(++argv);
148*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-engine") == 0) {
149*0a6a1f1dSLionel Sambuc             if (--argc < 1)
150*0a6a1f1dSLionel Sambuc                 goto bad;
151ebfedea0SLionel Sambuc             engine = *(++argv);
152*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-noout") == 0)
153ebfedea0SLionel Sambuc             noout = 1;
154ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-text") == 0)
155ebfedea0SLionel Sambuc             text = 1;
156*0a6a1f1dSLionel Sambuc         else if (strcmp(*argv, "-conv_form") == 0) {
157ebfedea0SLionel Sambuc             if (--argc < 1)
158ebfedea0SLionel Sambuc                 goto bad;
159ebfedea0SLionel Sambuc             ++argv;
160ebfedea0SLionel Sambuc             new_form = 1;
161ebfedea0SLionel Sambuc             if (strcmp(*argv, "compressed") == 0)
162ebfedea0SLionel Sambuc                 form = POINT_CONVERSION_COMPRESSED;
163ebfedea0SLionel Sambuc             else if (strcmp(*argv, "uncompressed") == 0)
164ebfedea0SLionel Sambuc                 form = POINT_CONVERSION_UNCOMPRESSED;
165ebfedea0SLionel Sambuc             else if (strcmp(*argv, "hybrid") == 0)
166ebfedea0SLionel Sambuc                 form = POINT_CONVERSION_HYBRID;
167ebfedea0SLionel Sambuc             else
168ebfedea0SLionel Sambuc                 goto bad;
169*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-param_enc") == 0) {
170ebfedea0SLionel Sambuc             if (--argc < 1)
171ebfedea0SLionel Sambuc                 goto bad;
172ebfedea0SLionel Sambuc             ++argv;
173ebfedea0SLionel Sambuc             new_asn1_flag = 1;
174ebfedea0SLionel Sambuc             if (strcmp(*argv, "named_curve") == 0)
175ebfedea0SLionel Sambuc                 asn1_flag = OPENSSL_EC_NAMED_CURVE;
176ebfedea0SLionel Sambuc             else if (strcmp(*argv, "explicit") == 0)
177ebfedea0SLionel Sambuc                 asn1_flag = 0;
178ebfedea0SLionel Sambuc             else
179ebfedea0SLionel Sambuc                 goto bad;
180*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-param_out") == 0)
181ebfedea0SLionel Sambuc             param_out = 1;
182ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-pubin") == 0)
183ebfedea0SLionel Sambuc             pubin = 1;
184ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-pubout") == 0)
185ebfedea0SLionel Sambuc             pubout = 1;
186*0a6a1f1dSLionel Sambuc         else if ((enc = EVP_get_cipherbyname(&(argv[0][1]))) == NULL) {
187ebfedea0SLionel Sambuc             BIO_printf(bio_err, "unknown option %s\n", *argv);
188ebfedea0SLionel Sambuc             badops = 1;
189ebfedea0SLionel Sambuc             break;
190ebfedea0SLionel Sambuc         }
191ebfedea0SLionel Sambuc         argc--;
192ebfedea0SLionel Sambuc         argv++;
193ebfedea0SLionel Sambuc     }
194ebfedea0SLionel Sambuc 
195*0a6a1f1dSLionel Sambuc     if (badops) {
196ebfedea0SLionel Sambuc  bad:
197ebfedea0SLionel Sambuc         BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
198ebfedea0SLionel Sambuc         BIO_printf(bio_err, "where options are\n");
199ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -inform arg     input format - "
200ebfedea0SLionel Sambuc                    "DER or PEM\n");
201ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -outform arg    output format - "
202ebfedea0SLionel Sambuc                    "DER or PEM\n");
203ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -in arg         input file\n");
204ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -passin arg     input file pass "
205ebfedea0SLionel Sambuc                    "phrase source\n");
206ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -out arg        output file\n");
207ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -passout arg    output file pass "
208ebfedea0SLionel Sambuc                    "phrase source\n");
209ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -engine e       use engine e, "
210ebfedea0SLionel Sambuc                    "possibly a hardware device.\n");
211ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -des            encrypt PEM output, "
212ebfedea0SLionel Sambuc                    "instead of 'des' every other \n"
213ebfedea0SLionel Sambuc                    "                 cipher "
214ebfedea0SLionel Sambuc                    "supported by OpenSSL can be used\n");
215ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -text           print the key\n");
216ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -noout          don't print key out\n");
217ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -param_out      print the elliptic "
218ebfedea0SLionel Sambuc                    "curve parameters\n");
219ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -conv_form arg  specifies the "
220ebfedea0SLionel Sambuc                    "point conversion form \n");
221ebfedea0SLionel Sambuc         BIO_printf(bio_err, "                 possible values:"
222ebfedea0SLionel Sambuc                    " compressed\n");
223ebfedea0SLionel Sambuc         BIO_printf(bio_err, "                                 "
224ebfedea0SLionel Sambuc                    " uncompressed (default)\n");
225*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err, "                                  " " hybrid\n");
226ebfedea0SLionel Sambuc         BIO_printf(bio_err, " -param_enc arg  specifies the way"
227ebfedea0SLionel Sambuc                    " the ec parameters are encoded\n");
228*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err, "                 in the asn1 der " "encoding\n");
229ebfedea0SLionel Sambuc         BIO_printf(bio_err, "                 possible values:"
230ebfedea0SLionel Sambuc                    " named_curve (default)\n");
231ebfedea0SLionel Sambuc         BIO_printf(bio_err, "                                  "
232ebfedea0SLionel Sambuc                    "explicit\n");
233ebfedea0SLionel Sambuc         goto end;
234ebfedea0SLionel Sambuc     }
235ebfedea0SLionel Sambuc 
236ebfedea0SLionel Sambuc     ERR_load_crypto_strings();
237ebfedea0SLionel Sambuc 
238ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ENGINE
239ebfedea0SLionel Sambuc     setup_engine(bio_err, engine, 0);
240ebfedea0SLionel Sambuc # endif
241ebfedea0SLionel Sambuc 
242*0a6a1f1dSLionel Sambuc     if (!app_passwd(bio_err, passargin, passargout, &passin, &passout)) {
243ebfedea0SLionel Sambuc         BIO_printf(bio_err, "Error getting passwords\n");
244ebfedea0SLionel Sambuc         goto end;
245ebfedea0SLionel Sambuc     }
246ebfedea0SLionel Sambuc 
247ebfedea0SLionel Sambuc     in = BIO_new(BIO_s_file());
248ebfedea0SLionel Sambuc     out = BIO_new(BIO_s_file());
249*0a6a1f1dSLionel Sambuc     if ((in == NULL) || (out == NULL)) {
250ebfedea0SLionel Sambuc         ERR_print_errors(bio_err);
251ebfedea0SLionel Sambuc         goto end;
252ebfedea0SLionel Sambuc     }
253ebfedea0SLionel Sambuc 
254ebfedea0SLionel Sambuc     if (infile == NULL)
255ebfedea0SLionel Sambuc         BIO_set_fp(in, stdin, BIO_NOCLOSE);
256*0a6a1f1dSLionel Sambuc     else {
257*0a6a1f1dSLionel Sambuc         if (BIO_read_filename(in, infile) <= 0) {
258ebfedea0SLionel Sambuc             perror(infile);
259ebfedea0SLionel Sambuc             goto end;
260ebfedea0SLionel Sambuc         }
261ebfedea0SLionel Sambuc     }
262ebfedea0SLionel Sambuc 
263ebfedea0SLionel Sambuc     BIO_printf(bio_err, "read EC key\n");
264*0a6a1f1dSLionel Sambuc     if (informat == FORMAT_ASN1) {
265ebfedea0SLionel Sambuc         if (pubin)
266ebfedea0SLionel Sambuc             eckey = d2i_EC_PUBKEY_bio(in, NULL);
267ebfedea0SLionel Sambuc         else
268ebfedea0SLionel Sambuc             eckey = d2i_ECPrivateKey_bio(in, NULL);
269*0a6a1f1dSLionel Sambuc     } else if (informat == FORMAT_PEM) {
270ebfedea0SLionel Sambuc         if (pubin)
271*0a6a1f1dSLionel Sambuc             eckey = PEM_read_bio_EC_PUBKEY(in, NULL, NULL, NULL);
272ebfedea0SLionel Sambuc         else
273*0a6a1f1dSLionel Sambuc             eckey = PEM_read_bio_ECPrivateKey(in, NULL, NULL, passin);
274*0a6a1f1dSLionel Sambuc     } else {
275ebfedea0SLionel Sambuc         BIO_printf(bio_err, "bad input format specified for key\n");
276ebfedea0SLionel Sambuc         goto end;
277ebfedea0SLionel Sambuc     }
278*0a6a1f1dSLionel Sambuc     if (eckey == NULL) {
279ebfedea0SLionel Sambuc         BIO_printf(bio_err, "unable to load Key\n");
280ebfedea0SLionel Sambuc         ERR_print_errors(bio_err);
281ebfedea0SLionel Sambuc         goto end;
282ebfedea0SLionel Sambuc     }
283ebfedea0SLionel Sambuc 
284*0a6a1f1dSLionel Sambuc     if (outfile == NULL) {
285ebfedea0SLionel Sambuc         BIO_set_fp(out, stdout, BIO_NOCLOSE);
286ebfedea0SLionel Sambuc # ifdef OPENSSL_SYS_VMS
287ebfedea0SLionel Sambuc         {
288ebfedea0SLionel Sambuc             BIO *tmpbio = BIO_new(BIO_f_linebuffer());
289ebfedea0SLionel Sambuc             out = BIO_push(tmpbio, out);
290ebfedea0SLionel Sambuc         }
291ebfedea0SLionel Sambuc # endif
292*0a6a1f1dSLionel Sambuc     } else {
293*0a6a1f1dSLionel Sambuc         if (BIO_write_filename(out, outfile) <= 0) {
294ebfedea0SLionel Sambuc             perror(outfile);
295ebfedea0SLionel Sambuc             goto end;
296ebfedea0SLionel Sambuc         }
297ebfedea0SLionel Sambuc     }
298ebfedea0SLionel Sambuc 
299ebfedea0SLionel Sambuc     group = EC_KEY_get0_group(eckey);
300ebfedea0SLionel Sambuc 
301ebfedea0SLionel Sambuc     if (new_form)
302ebfedea0SLionel Sambuc         EC_KEY_set_conv_form(eckey, form);
303ebfedea0SLionel Sambuc 
304ebfedea0SLionel Sambuc     if (new_asn1_flag)
305ebfedea0SLionel Sambuc         EC_KEY_set_asn1_flag(eckey, asn1_flag);
306ebfedea0SLionel Sambuc 
307ebfedea0SLionel Sambuc     if (text)
308*0a6a1f1dSLionel Sambuc         if (!EC_KEY_print(out, eckey, 0)) {
309ebfedea0SLionel Sambuc             perror(outfile);
310ebfedea0SLionel Sambuc             ERR_print_errors(bio_err);
311ebfedea0SLionel Sambuc             goto end;
312ebfedea0SLionel Sambuc         }
313ebfedea0SLionel Sambuc 
314*0a6a1f1dSLionel Sambuc     if (noout) {
315ebfedea0SLionel Sambuc         ret = 0;
316ebfedea0SLionel Sambuc         goto end;
317ebfedea0SLionel Sambuc     }
318ebfedea0SLionel Sambuc 
319ebfedea0SLionel Sambuc     BIO_printf(bio_err, "writing EC key\n");
320*0a6a1f1dSLionel Sambuc     if (outformat == FORMAT_ASN1) {
321ebfedea0SLionel Sambuc         if (param_out)
322ebfedea0SLionel Sambuc             i = i2d_ECPKParameters_bio(out, group);
323ebfedea0SLionel Sambuc         else if (pubin || pubout)
324ebfedea0SLionel Sambuc             i = i2d_EC_PUBKEY_bio(out, eckey);
325ebfedea0SLionel Sambuc         else
326ebfedea0SLionel Sambuc             i = i2d_ECPrivateKey_bio(out, eckey);
327*0a6a1f1dSLionel Sambuc     } else if (outformat == FORMAT_PEM) {
328ebfedea0SLionel Sambuc         if (param_out)
329ebfedea0SLionel Sambuc             i = PEM_write_bio_ECPKParameters(out, group);
330ebfedea0SLionel Sambuc         else if (pubin || pubout)
331ebfedea0SLionel Sambuc             i = PEM_write_bio_EC_PUBKEY(out, eckey);
332ebfedea0SLionel Sambuc         else
333ebfedea0SLionel Sambuc             i = PEM_write_bio_ECPrivateKey(out, eckey, enc,
334ebfedea0SLionel Sambuc                                            NULL, 0, NULL, passout);
335*0a6a1f1dSLionel Sambuc     } else {
336*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err, "bad output format specified for " "outfile\n");
337ebfedea0SLionel Sambuc         goto end;
338ebfedea0SLionel Sambuc     }
339ebfedea0SLionel Sambuc 
340*0a6a1f1dSLionel Sambuc     if (!i) {
341ebfedea0SLionel Sambuc         BIO_printf(bio_err, "unable to write private key\n");
342ebfedea0SLionel Sambuc         ERR_print_errors(bio_err);
343*0a6a1f1dSLionel Sambuc     } else
344ebfedea0SLionel Sambuc         ret = 0;
345ebfedea0SLionel Sambuc  end:
346ebfedea0SLionel Sambuc     if (in)
347ebfedea0SLionel Sambuc         BIO_free(in);
348ebfedea0SLionel Sambuc     if (out)
349ebfedea0SLionel Sambuc         BIO_free_all(out);
350ebfedea0SLionel Sambuc     if (eckey)
351ebfedea0SLionel Sambuc         EC_KEY_free(eckey);
352ebfedea0SLionel Sambuc     if (passin)
353ebfedea0SLionel Sambuc         OPENSSL_free(passin);
354ebfedea0SLionel Sambuc     if (passout)
355ebfedea0SLionel Sambuc         OPENSSL_free(passout);
356ebfedea0SLionel Sambuc     apps_shutdown();
357ebfedea0SLionel Sambuc     OPENSSL_EXIT(ret);
358ebfedea0SLionel Sambuc }
359ebfedea0SLionel Sambuc #else                           /* !OPENSSL_NO_EC */
360ebfedea0SLionel Sambuc 
361ebfedea0SLionel Sambuc # if PEDANTIC
362ebfedea0SLionel Sambuc static void *dummy = &dummy;
363ebfedea0SLionel Sambuc # endif
364ebfedea0SLionel Sambuc 
365ebfedea0SLionel Sambuc #endif
366