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