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