4d57ca4446170d40896b3a81d38aa3587b8c5818
[oweals/openssl.git] / apps / ecparam.c
1 /* apps/ecparam.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  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60  *
61  * Portions of the attached software ("Contribution") are developed by
62  * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
63  *
64  * The Contribution is licensed pursuant to the OpenSSL open source
65  * license provided above.
66  *
67  * The elliptic curve binary polynomial software is originally written by
68  * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
69  *
70  */
71
72 #include <openssl/opensslconf.h>
73 #ifndef OPENSSL_NO_EC
74 # include <assert.h>
75 # include <stdio.h>
76 # include <stdlib.h>
77 # include <time.h>
78 # include <string.h>
79 # include "apps.h"
80 # include <openssl/bio.h>
81 # include <openssl/err.h>
82 # include <openssl/bn.h>
83 # include <openssl/ec.h>
84 # include <openssl/x509.h>
85 # include <openssl/pem.h>
86
87 # undef PROG
88 # define PROG    ecparam_main
89
90 /*-
91  * -inform arg      - input format - default PEM (DER or PEM)
92  * -outform arg     - output format - default PEM
93  * -in  arg         - input file  - default stdin
94  * -out arg         - output file - default stdout
95  * -noout           - do not print the ec parameter
96  * -text            - print the ec parameters in text form
97  * -check           - validate the ec parameters
98  * -C               - print a 'C' function creating the parameters
99  * -name arg        - use the ec parameters with 'short name' name
100  * -list_curves     - prints a list of all currently available curve 'short names'
101  * -conv_form arg   - specifies the point conversion form
102  *                  - possible values: compressed
103  *                                     uncompressed (default)
104  *                                     hybrid
105  * -param_enc arg   - specifies the way the ec parameters are encoded
106  *                    in the asn1 der encoding
107  *                    possible values: named_curve (default)
108  *                                     explicit
109  * -no_seed         - if 'explicit' parameters are chosen do not use the seed
110  * -genkey          - generate ec key
111  * -rand file       - files to use for random number input
112  * -engine e        - use engine e, possibly a hardware device
113  */
114
115 static int ecparam_print_var(BIO *, BIGNUM *, const char *, int,
116                              unsigned char *);
117
118 int MAIN(int, char **);
119
120 int MAIN(int argc, char **argv)
121 {
122     EC_GROUP *group = NULL;
123     point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
124     int new_form = 0;
125     int asn1_flag = OPENSSL_EC_NAMED_CURVE;
126     int new_asn1_flag = 0;
127     char *curve_name = NULL, *inrand = NULL;
128     int list_curves = 0, no_seed = 0, check = 0,
129         badops = 0, text = 0, i, need_rand = 0, genkey = 0;
130     char *infile = NULL, *outfile = NULL, *prog;
131     BIO *in = NULL, *out = NULL;
132     int informat, outformat, noout = 0, C = 0, ret = 1;
133     char *engine = NULL;
134 # ifndef OPENSSL_NO_ENGINE
135     ENGINE *e = NULL;
136 # endif
137
138     BIGNUM *ec_p = NULL, *ec_a = NULL, *ec_b = NULL,
139         *ec_gen = NULL, *ec_order = NULL, *ec_cofactor = NULL;
140     unsigned char *buffer = NULL;
141
142     apps_startup();
143
144     if (bio_err == NULL)
145         if ((bio_err = BIO_new(BIO_s_file())) != NULL)
146             BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
147
148     if (!load_config(bio_err, NULL))
149         goto end;
150
151     informat = FORMAT_PEM;
152     outformat = FORMAT_PEM;
153
154     prog = argv[0];
155     argc--;
156     argv++;
157     while (argc >= 1) {
158         if (strcmp(*argv, "-inform") == 0) {
159             if (--argc < 1)
160                 goto bad;
161             informat = str2fmt(*(++argv));
162         } else if (strcmp(*argv, "-outform") == 0) {
163             if (--argc < 1)
164                 goto bad;
165             outformat = str2fmt(*(++argv));
166         } else if (strcmp(*argv, "-in") == 0) {
167             if (--argc < 1)
168                 goto bad;
169             infile = *(++argv);
170         } else if (strcmp(*argv, "-out") == 0) {
171             if (--argc < 1)
172                 goto bad;
173             outfile = *(++argv);
174         } else if (strcmp(*argv, "-text") == 0)
175             text = 1;
176         else if (strcmp(*argv, "-C") == 0)
177             C = 1;
178         else if (strcmp(*argv, "-check") == 0)
179             check = 1;
180         else if (strcmp(*argv, "-name") == 0) {
181             if (--argc < 1)
182                 goto bad;
183             curve_name = *(++argv);
184         } else if (strcmp(*argv, "-list_curves") == 0)
185             list_curves = 1;
186         else if (strcmp(*argv, "-conv_form") == 0) {
187             if (--argc < 1)
188                 goto bad;
189             ++argv;
190             new_form = 1;
191             if (strcmp(*argv, "compressed") == 0)
192                 form = POINT_CONVERSION_COMPRESSED;
193             else if (strcmp(*argv, "uncompressed") == 0)
194                 form = POINT_CONVERSION_UNCOMPRESSED;
195             else if (strcmp(*argv, "hybrid") == 0)
196                 form = POINT_CONVERSION_HYBRID;
197             else
198                 goto bad;
199         } else if (strcmp(*argv, "-param_enc") == 0) {
200             if (--argc < 1)
201                 goto bad;
202             ++argv;
203             new_asn1_flag = 1;
204             if (strcmp(*argv, "named_curve") == 0)
205                 asn1_flag = OPENSSL_EC_NAMED_CURVE;
206             else if (strcmp(*argv, "explicit") == 0)
207                 asn1_flag = 0;
208             else
209                 goto bad;
210         } else if (strcmp(*argv, "-no_seed") == 0)
211             no_seed = 1;
212         else if (strcmp(*argv, "-noout") == 0)
213             noout = 1;
214         else if (strcmp(*argv, "-genkey") == 0) {
215             genkey = 1;
216             need_rand = 1;
217         } else if (strcmp(*argv, "-rand") == 0) {
218             if (--argc < 1)
219                 goto bad;
220             inrand = *(++argv);
221             need_rand = 1;
222         } else if (strcmp(*argv, "-engine") == 0) {
223             if (--argc < 1)
224                 goto bad;
225             engine = *(++argv);
226         } else {
227             BIO_printf(bio_err, "unknown option %s\n", *argv);
228             badops = 1;
229             break;
230         }
231         argc--;
232         argv++;
233     }
234
235     if (badops) {
236  bad:
237         BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
238         BIO_printf(bio_err, "where options are\n");
239         BIO_printf(bio_err, " -inform arg       input format - "
240                    "default PEM (DER or PEM)\n");
241         BIO_printf(bio_err, " -outform arg      output format - "
242                    "default PEM\n");
243         BIO_printf(bio_err, " -in  arg          input file  - "
244                    "default stdin\n");
245         BIO_printf(bio_err, " -out arg          output file - "
246                    "default stdout\n");
247         BIO_printf(bio_err, " -noout            do not print the "
248                    "ec parameter\n");
249         BIO_printf(bio_err, " -text             print the ec "
250                    "parameters in text form\n");
251         BIO_printf(bio_err, " -check            validate the ec "
252                    "parameters\n");
253         BIO_printf(bio_err, " -C                print a 'C' "
254                    "function creating the parameters\n");
255         BIO_printf(bio_err, " -name arg         use the "
256                    "ec parameters with 'short name' name\n");
257         BIO_printf(bio_err, " -list_curves      prints a list of "
258                    "all currently available curve 'short names'\n");
259         BIO_printf(bio_err, " -conv_form arg    specifies the "
260                    "point conversion form \n");
261         BIO_printf(bio_err, "                   possible values:"
262                    " compressed\n");
263         BIO_printf(bio_err, "                                   "
264                    " uncompressed (default)\n");
265         BIO_printf(bio_err, "                                   "
266                    " hybrid\n");
267         BIO_printf(bio_err, " -param_enc arg    specifies the way"
268                    " the ec parameters are encoded\n");
269         BIO_printf(bio_err, "                   in the asn1 der "
270                    "encoding\n");
271         BIO_printf(bio_err, "                   possible values:"
272                    " named_curve (default)\n");
273         BIO_printf(bio_err, "                                   "
274                    " explicit\n");
275         BIO_printf(bio_err, " -no_seed          if 'explicit'"
276                    " parameters are chosen do not" " use the seed\n");
277         BIO_printf(bio_err, " -genkey           generate ec" " key\n");
278         BIO_printf(bio_err, " -rand file        files to use for"
279                    " random number input\n");
280         BIO_printf(bio_err, " -engine e         use engine e, "
281                    "possibly a hardware device\n");
282         goto end;
283     }
284
285     ERR_load_crypto_strings();
286
287     in = BIO_new(BIO_s_file());
288     out = BIO_new(BIO_s_file());
289     if ((in == NULL) || (out == NULL)) {
290         ERR_print_errors(bio_err);
291         goto end;
292     }
293
294     if (infile == NULL)
295         BIO_set_fp(in, stdin, BIO_NOCLOSE);
296     else {
297         if (BIO_read_filename(in, infile) <= 0) {
298             perror(infile);
299             goto end;
300         }
301     }
302     if (outfile == NULL) {
303         BIO_set_fp(out, stdout, BIO_NOCLOSE);
304 # ifdef OPENSSL_SYS_VMS
305         {
306             BIO *tmpbio = BIO_new(BIO_f_linebuffer());
307             out = BIO_push(tmpbio, out);
308         }
309 # endif
310     } else {
311         if (BIO_write_filename(out, outfile) <= 0) {
312             perror(outfile);
313             goto end;
314         }
315     }
316
317 # ifndef OPENSSL_NO_ENGINE
318     e = setup_engine(bio_err, engine, 0);
319 # endif
320
321     if (list_curves) {
322         EC_builtin_curve *curves = NULL;
323         size_t crv_len = 0;
324         size_t n = 0;
325
326         crv_len = EC_get_builtin_curves(NULL, 0);
327
328         curves = OPENSSL_malloc((int)(sizeof(EC_builtin_curve) * crv_len));
329
330         if (curves == NULL)
331             goto end;
332
333         if (!EC_get_builtin_curves(curves, crv_len)) {
334             OPENSSL_free(curves);
335             goto end;
336         }
337
338         for (n = 0; n < crv_len; n++) {
339             const char *comment;
340             const char *sname;
341             comment = curves[n].comment;
342             sname = OBJ_nid2sn(curves[n].nid);
343             if (comment == NULL)
344                 comment = "CURVE DESCRIPTION NOT AVAILABLE";
345             if (sname == NULL)
346                 sname = "";
347
348             BIO_printf(out, "  %-10s: ", sname);
349             BIO_printf(out, "%s\n", comment);
350         }
351
352         OPENSSL_free(curves);
353         ret = 0;
354         goto end;
355     }
356
357     if (curve_name != NULL) {
358         int nid;
359
360         /*
361          * workaround for the SECG curve names secp192r1 and secp256r1 (which
362          * are the same as the curves prime192v1 and prime256v1 defined in
363          * X9.62)
364          */
365         if (!strcmp(curve_name, "secp192r1")) {
366             BIO_printf(bio_err, "using curve name prime192v1 "
367                        "instead of secp192r1\n");
368             nid = NID_X9_62_prime192v1;
369         } else if (!strcmp(curve_name, "secp256r1")) {
370             BIO_printf(bio_err, "using curve name prime256v1 "
371                        "instead of secp256r1\n");
372             nid = NID_X9_62_prime256v1;
373         } else
374             nid = OBJ_sn2nid(curve_name);
375
376         if (nid == 0)
377             nid = EC_curve_nist2nid(curve_name);
378
379         if (nid == 0) {
380             BIO_printf(bio_err, "unknown curve name (%s)\n", curve_name);
381             goto end;
382         }
383
384         group = EC_GROUP_new_by_curve_name(nid);
385         if (group == NULL) {
386             BIO_printf(bio_err, "unable to create curve (%s)\n", curve_name);
387             goto end;
388         }
389         EC_GROUP_set_asn1_flag(group, asn1_flag);
390         EC_GROUP_set_point_conversion_form(group, form);
391     } else if (informat == FORMAT_ASN1) {
392         group = d2i_ECPKParameters_bio(in, NULL);
393     } else if (informat == FORMAT_PEM) {
394         group = PEM_read_bio_ECPKParameters(in, NULL, NULL, NULL);
395     } else {
396         BIO_printf(bio_err, "bad input format specified\n");
397         goto end;
398     }
399
400     if (group == NULL) {
401         BIO_printf(bio_err, "unable to load elliptic curve parameters\n");
402         ERR_print_errors(bio_err);
403         goto end;
404     }
405
406     if (new_form)
407         EC_GROUP_set_point_conversion_form(group, form);
408
409     if (new_asn1_flag)
410         EC_GROUP_set_asn1_flag(group, asn1_flag);
411
412     if (no_seed) {
413         EC_GROUP_set_seed(group, NULL, 0);
414     }
415
416     if (text) {
417         if (!ECPKParameters_print(out, group, 0))
418             goto end;
419     }
420
421     if (check) {
422         BIO_printf(bio_err, "checking elliptic curve parameters: ");
423         if (!EC_GROUP_check(group, NULL)) {
424             BIO_printf(bio_err, "failed\n");
425             ERR_print_errors(bio_err);
426             goto end;
427         }
428         BIO_printf(bio_err, "ok\n");
429
430     }
431
432     if (C) {
433         size_t buf_len = 0, tmp_len = 0;
434         const EC_POINT *point;
435         int is_prime, len = 0;
436         const EC_METHOD *meth = EC_GROUP_method_of(group);
437
438         if ((ec_p = BN_new()) == NULL || (ec_a = BN_new()) == NULL ||
439             (ec_b = BN_new()) == NULL || (ec_gen = BN_new()) == NULL ||
440             (ec_order = BN_new()) == NULL ||
441             (ec_cofactor = BN_new()) == NULL) {
442             perror("OPENSSL_malloc");
443             goto end;
444         }
445
446         is_prime = (EC_METHOD_get_field_type(meth) == NID_X9_62_prime_field);
447
448         if (is_prime) {
449             if (!EC_GROUP_get_curve_GFp(group, ec_p, ec_a, ec_b, NULL))
450                 goto end;
451         } else {
452             /* TODO */
453             goto end;
454         }
455
456         if ((point = EC_GROUP_get0_generator(group)) == NULL)
457             goto end;
458         if (!EC_POINT_point2bn(group, point,
459                                EC_GROUP_get_point_conversion_form(group),
460                                ec_gen, NULL))
461             goto end;
462         if (!EC_GROUP_get_order(group, ec_order, NULL))
463             goto end;
464         if (!EC_GROUP_get_cofactor(group, ec_cofactor, NULL))
465             goto end;
466
467         if (!ec_p || !ec_a || !ec_b || !ec_gen || !ec_order || !ec_cofactor)
468             goto end;
469
470         len = BN_num_bits(ec_order);
471
472         if ((tmp_len = (size_t)BN_num_bytes(ec_p)) > buf_len)
473             buf_len = tmp_len;
474         if ((tmp_len = (size_t)BN_num_bytes(ec_a)) > buf_len)
475             buf_len = tmp_len;
476         if ((tmp_len = (size_t)BN_num_bytes(ec_b)) > buf_len)
477             buf_len = tmp_len;
478         if ((tmp_len = (size_t)BN_num_bytes(ec_gen)) > buf_len)
479             buf_len = tmp_len;
480         if ((tmp_len = (size_t)BN_num_bytes(ec_order)) > buf_len)
481             buf_len = tmp_len;
482         if ((tmp_len = (size_t)BN_num_bytes(ec_cofactor)) > buf_len)
483             buf_len = tmp_len;
484
485         buffer = (unsigned char *)OPENSSL_malloc(buf_len);
486
487         if (buffer == NULL) {
488             perror("OPENSSL_malloc");
489             goto end;
490         }
491
492         ecparam_print_var(out, ec_p, "ec_p", len, buffer);
493         ecparam_print_var(out, ec_a, "ec_a", len, buffer);
494         ecparam_print_var(out, ec_b, "ec_b", len, buffer);
495         ecparam_print_var(out, ec_gen, "ec_gen", len, buffer);
496         ecparam_print_var(out, ec_order, "ec_order", len, buffer);
497         ecparam_print_var(out, ec_cofactor, "ec_cofactor", len, buffer);
498
499         BIO_printf(out, "\n\n");
500
501         BIO_printf(out, "EC_GROUP *get_ec_group_%d(void)\n\t{\n", len);
502         BIO_printf(out, "\tint ok=0;\n");
503         BIO_printf(out, "\tEC_GROUP *group = NULL;\n");
504         BIO_printf(out, "\tEC_POINT *point = NULL;\n");
505         BIO_printf(out, "\tBIGNUM   *tmp_1 = NULL, *tmp_2 = NULL, "
506                    "*tmp_3 = NULL;\n\n");
507         BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_p_%d, "
508                    "sizeof(ec_p_%d), NULL)) == NULL)\n\t\t"
509                    "goto err;\n", len, len);
510         BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_a_%d, "
511                    "sizeof(ec_a_%d), NULL)) == NULL)\n\t\t"
512                    "goto err;\n", len, len);
513         BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_b_%d, "
514                    "sizeof(ec_b_%d), NULL)) == NULL)\n\t\t"
515                    "goto err;\n", len, len);
516         if (is_prime) {
517             BIO_printf(out, "\tif ((group = EC_GROUP_new_curve_"
518                        "GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL)"
519                        "\n\t\tgoto err;\n\n");
520         } else {
521             /* TODO */
522             goto end;
523         }
524         BIO_printf(out, "\t/* build generator */\n");
525         BIO_printf(out, "\tif ((tmp_1 = BN_bin2bn(ec_gen_%d, "
526                    "sizeof(ec_gen_%d), tmp_1)) == NULL)"
527                    "\n\t\tgoto err;\n", len, len);
528         BIO_printf(out, "\tpoint = EC_POINT_bn2point(group, tmp_1, "
529                    "NULL, NULL);\n");
530         BIO_printf(out, "\tif (point == NULL)\n\t\tgoto err;\n");
531         BIO_printf(out, "\tif ((tmp_2 = BN_bin2bn(ec_order_%d, "
532                    "sizeof(ec_order_%d), tmp_2)) == NULL)"
533                    "\n\t\tgoto err;\n", len, len);
534         BIO_printf(out, "\tif ((tmp_3 = BN_bin2bn(ec_cofactor_%d, "
535                    "sizeof(ec_cofactor_%d), tmp_3)) == NULL)"
536                    "\n\t\tgoto err;\n", len, len);
537         BIO_printf(out, "\tif (!EC_GROUP_set_generator(group, point,"
538                    " tmp_2, tmp_3))\n\t\tgoto err;\n");
539         BIO_printf(out, "\n\tok=1;\n");
540         BIO_printf(out, "err:\n");
541         BIO_printf(out, "\tif (tmp_1)\n\t\tBN_free(tmp_1);\n");
542         BIO_printf(out, "\tif (tmp_2)\n\t\tBN_free(tmp_2);\n");
543         BIO_printf(out, "\tif (tmp_3)\n\t\tBN_free(tmp_3);\n");
544         BIO_printf(out, "\tif (point)\n\t\tEC_POINT_free(point);\n");
545         BIO_printf(out, "\tif (!ok)\n");
546         BIO_printf(out, "\t\t{\n");
547         BIO_printf(out, "\t\tEC_GROUP_free(group);\n");
548         BIO_printf(out, "\t\tgroup = NULL;\n");
549         BIO_printf(out, "\t\t}\n");
550         BIO_printf(out, "\treturn(group);\n\t}\n");
551     }
552
553     if (!noout) {
554         if (outformat == FORMAT_ASN1)
555             i = i2d_ECPKParameters_bio(out, group);
556         else if (outformat == FORMAT_PEM)
557             i = PEM_write_bio_ECPKParameters(out, group);
558         else {
559             BIO_printf(bio_err, "bad output format specified for"
560                        " outfile\n");
561             goto end;
562         }
563         if (!i) {
564             BIO_printf(bio_err, "unable to write elliptic "
565                        "curve parameters\n");
566             ERR_print_errors(bio_err);
567             goto end;
568         }
569     }
570
571     if (need_rand) {
572         app_RAND_load_file(NULL, bio_err, (inrand != NULL));
573         if (inrand != NULL)
574             BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
575                        app_RAND_load_files(inrand));
576     }
577
578     if (genkey) {
579         EC_KEY *eckey = EC_KEY_new();
580
581         if (eckey == NULL)
582             goto end;
583
584         assert(need_rand);
585
586         if (EC_KEY_set_group(eckey, group) == 0)
587             goto end;
588
589         if (!EC_KEY_generate_key(eckey)) {
590             EC_KEY_free(eckey);
591             goto end;
592         }
593         if (outformat == FORMAT_ASN1)
594             i = i2d_ECPrivateKey_bio(out, eckey);
595         else if (outformat == FORMAT_PEM)
596             i = PEM_write_bio_ECPrivateKey(out, eckey, NULL,
597                                            NULL, 0, NULL, NULL);
598         else {
599             BIO_printf(bio_err, "bad output format specified "
600                        "for outfile\n");
601             EC_KEY_free(eckey);
602             goto end;
603         }
604         EC_KEY_free(eckey);
605     }
606
607     if (need_rand)
608         app_RAND_write_file(NULL, bio_err);
609
610     ret = 0;
611  end:
612     if (ec_p)
613         BN_free(ec_p);
614     if (ec_a)
615         BN_free(ec_a);
616     if (ec_b)
617         BN_free(ec_b);
618     if (ec_gen)
619         BN_free(ec_gen);
620     if (ec_order)
621         BN_free(ec_order);
622     if (ec_cofactor)
623         BN_free(ec_cofactor);
624     if (buffer)
625         OPENSSL_free(buffer);
626     if (group != NULL)
627         EC_GROUP_free(group);
628 # ifndef OPENSSL_NO_ENGINE
629     if (e != NULL)
630         release_engine(e);
631 # endif
632     if (in != NULL)
633         BIO_free(in);
634     if (out != NULL)
635         BIO_free_all(out);
636     apps_shutdown();
637     OPENSSL_EXIT(ret);
638 }
639
640 static int ecparam_print_var(BIO *out, BIGNUM *in, const char *var,
641                              int len, unsigned char *buffer)
642 {
643     BIO_printf(out, "static unsigned char %s_%d[] = {", var, len);
644     if (BN_is_zero(in))
645         BIO_printf(out, "\n\t0x00");
646     else {
647         int i, l;
648
649         l = BN_bn2bin(in, buffer);
650         for (i = 0; i < l - 1; i++) {
651             if ((i % 12) == 0)
652                 BIO_printf(out, "\n\t");
653             BIO_printf(out, "0x%02X,", buffer[i]);
654         }
655         if ((i % 12) == 0)
656             BIO_printf(out, "\n\t");
657         BIO_printf(out, "0x%02X", buffer[i]);
658     }
659     BIO_printf(out, "\n\t};\n\n");
660     return 1;
661 }
662 #else                           /* !OPENSSL_NO_EC */
663
664 # if PEDANTIC
665 static void *dummy = &dummy;
666 # endif
667
668 #endif