fix memory leak
[openssl.git] / apps / ecdsaparam.c
1 /* apps/ecdsaparam.c */
2 /* ====================================================================
3  * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer. 
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * 3. All advertising materials mentioning features or use of this
18  *    software must display the following acknowledgment:
19  *    "This product includes software developed by the OpenSSL Project
20  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21  *
22  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23  *    endorse or promote products derived from this software without
24  *    prior written permission. For written permission, please contact
25  *    openssl-core@openssl.org.
26  *
27  * 5. Products derived from this software may not be called "OpenSSL"
28  *    nor may "OpenSSL" appear in their names without prior written
29  *    permission of the OpenSSL Project.
30  *
31  * 6. Redistributions of any form whatsoever must retain the following
32  *    acknowledgment:
33  *    "This product includes software developed by the OpenSSL Project
34  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47  * OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This product includes cryptographic software written by Eric Young
51  * (eay@cryptsoft.com).  This product includes software written by Tim
52  * Hudson (tjh@cryptsoft.com).
53  *
54  */
55 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
56  * All rights reserved.
57  *
58  * This package is an SSL implementation written
59  * by Eric Young (eay@cryptsoft.com).
60  * The implementation was written so as to conform with Netscapes SSL.
61  * 
62  * This library is free for commercial and non-commercial use as long as
63  * the following conditions are aheared to.  The following conditions
64  * apply to all code found in this distribution, be it the RC4, RSA,
65  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
66  * included with this distribution is covered by the same copyright terms
67  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
68  * 
69  * Copyright remains Eric Young's, and as such any Copyright notices in
70  * the code are not to be removed.
71  * If this package is used in a product, Eric Young should be given attribution
72  * as the author of the parts of the library used.
73  * This can be in the form of a textual message at program startup or
74  * in documentation (online or textual) provided with the package.
75  * 
76  * Redistribution and use in source and binary forms, with or without
77  * modification, are permitted provided that the following conditions
78  * are met:
79  * 1. Redistributions of source code must retain the copyright
80  *    notice, this list of conditions and the following disclaimer.
81  * 2. Redistributions in binary form must reproduce the above copyright
82  *    notice, this list of conditions and the following disclaimer in the
83  *    documentation and/or other materials provided with the distribution.
84  * 3. All advertising materials mentioning features or use of this software
85  *    must display the following acknowledgement:
86  *    "This product includes cryptographic software written by
87  *     Eric Young (eay@cryptsoft.com)"
88  *    The word 'cryptographic' can be left out if the rouines from the library
89  *    being used are not cryptographic related :-).
90  * 4. If you include any Windows specific code (or a derivative thereof) from 
91  *    the apps directory (application code) you must include an acknowledgement:
92  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
93  * 
94  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
95  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
97  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
98  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
99  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
100  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
101  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
102  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
103  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
104  * SUCH DAMAGE.
105  * 
106  * The licence and distribution terms for any publically available version or
107  * derivative of this code cannot be changed.  i.e. this code cannot simply be
108  * copied and put under another distribution licence
109  * [including the GNU Public Licence.]
110  */
111
112 #ifndef OPENSSL_NO_ECDSA
113 #include <assert.h>
114 #include <stdio.h>
115 #include <stdlib.h>
116 #include <time.h>
117 #include <string.h>
118 #include "apps.h"
119 #include <openssl/bio.h>
120 #include <openssl/err.h>
121 #include <openssl/bn.h>
122 #include <openssl/ec.h>
123 #include <openssl/ecdsa.h>
124 #include <openssl/x509.h>
125 #include <openssl/pem.h>
126
127 #undef PROG
128 #define PROG    ecdsaparam_main
129
130 /* -inform arg          - input format - default PEM (DER or PEM)
131  * -outform arg         - output format - default PEM
132  * -in arg              - input file  - default stdin
133  * -out arg             - output file - default stdout
134  * -noout
135  * -text
136  * -C
137  * -noout
138  * -genkey              - generate a private public keypair based on the supplied curve
139  * -named_curve         - use the curve oid instead of the parameters
140  * -NIST_192            - use the NIST recommeded curve parameters over a 192 bit prime field
141  * -NIST_224            - use the NIST recommeded curve parameters over a 224 bit prime field
142  * -NIST_256            - use the NIST recommeded curve parameters over a 256 bit prime field
143  * -NIST_384            - use the NIST recommeded curve parameters over a 384 bit prime field
144  * -NIST_521            - use the NIST recommeded curve parameters over a 521 bit prime field
145  * -X9_62_192v1         - use the X9_62 192v1 example curve over a 192 bit prime field
146  * -X9_62_192v2         - use the X9_62 192v2 example curve over a 192 bit prime field
147  * -X9_62_192v3         - use the X9_62 192v3 example curve over a 192 bit prime field
148  * -X9_62_239v1         - use the X9_62 239v1 example curve over a 239 bit prime field
149  * -X9_62_239v2         - use the X9_62 239v2 example curve over a 239 bit prime field
150  * -X9_62_239v3         - use the X9_62 239v3 example curve over a 239 bit prime field
151  * -X9_62_256v1         - use the X9_62 239v1 example curve over a 256 bit prime field
152  */
153
154 int MAIN(int, char **);
155
156 int MAIN(int argc, char **argv)
157 {
158         ENGINE  *e = NULL;
159         ECDSA   *ecdsa = NULL;
160         int     i, badops = 0, text = 0;
161         BIO     *in = NULL, *out = NULL;
162         int     informat, outformat, noout = 0, C = 0, ret = 1;
163         char    *infile, *outfile, *prog, *inrand = NULL;
164         int     genkey = 0;
165         int     need_rand = 0;
166         char    *engine=NULL;
167         int     curve_type = EC_GROUP_NO_CURVE;
168         int     named_curve = 0;
169         BIGNUM  *tmp_1 = NULL, *tmp_2 = NULL, *tmp_3 = NULL, *tmp_4 = NULL, *tmp_5 = NULL,
170                 *tmp_6 = NULL, *tmp_7 = NULL;
171         BN_CTX  *ctx = NULL;
172         EC_POINT *point = NULL;
173         unsigned char *data = NULL;
174
175         apps_startup();
176
177         if (bio_err == NULL)
178                 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
179                         BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
180
181         infile=NULL;
182         outfile=NULL;
183         informat=FORMAT_PEM;
184         outformat=FORMAT_PEM;
185
186         prog=argv[0];
187         argc--;
188         argv++;
189         while (argc >= 1)
190                 {
191                 if      (strcmp(*argv,"-inform") == 0)
192                 {
193                         if (--argc < 1) goto bad;
194                         informat=str2fmt(*(++argv));
195                 }
196                 else if (strcmp(*argv,"-outform") == 0)
197                 {
198                         if (--argc < 1) goto bad;
199                         outformat=str2fmt(*(++argv));
200                 }
201                 else if (strcmp(*argv,"-in") == 0)
202                 {
203                         if (--argc < 1) goto bad;
204                         infile= *(++argv);
205                 }
206                 else if (strcmp(*argv,"-out") == 0)
207                 {
208                         if (--argc < 1) goto bad;
209                         outfile= *(++argv);
210                 }
211                 else if(strcmp(*argv, "-engine") == 0)
212                 {
213                         if (--argc < 1) goto bad;
214                         engine = *(++argv);
215                 }
216                 else if (strcmp(*argv,"-text") == 0)
217                         text = 1;
218                 else if (strcmp(*argv,"-C") == 0)
219                         C = 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, "-named_curve") == 0)
232                         named_curve = 1;
233                 else if (strcmp(*argv, "-NIST_192") == 0)
234                         curve_type = EC_GROUP_NIST_PRIME_192;
235                 else if (strcmp(*argv, "-NIST_224") == 0)
236                         curve_type = EC_GROUP_NIST_PRIME_224;
237                 else if (strcmp(*argv, "-NIST_256") == 0)
238                         curve_type = EC_GROUP_NIST_PRIME_256;
239                 else if (strcmp(*argv, "-NIST_384") == 0)
240                         curve_type = EC_GROUP_NIST_PRIME_384;
241                 else if (strcmp(*argv, "-NIST_521") == 0)
242                         curve_type = EC_GROUP_NIST_PRIME_521;
243                 else if (strcmp(*argv, "-X9_62_192v1") == 0)
244                         curve_type = EC_GROUP_X9_62_PRIME_192V1;
245                 else if (strcmp(*argv, "-X9_62_192v2") == 0)
246                         curve_type = EC_GROUP_X9_62_PRIME_192V2;
247                 else if (strcmp(*argv, "-X9_62_192v3") == 0)
248                         curve_type = EC_GROUP_X9_62_PRIME_192V3;
249                 else if (strcmp(*argv, "-X9_62_239v1") == 0)
250                         curve_type = EC_GROUP_X9_62_PRIME_239V1;
251                 else if (strcmp(*argv, "-X9_62_239v2") == 0)
252                         curve_type = EC_GROUP_X9_62_PRIME_239V2;
253                 else if (strcmp(*argv, "-X9_62_239v3") == 0)
254                         curve_type = EC_GROUP_X9_62_PRIME_239V3;
255                 else if (strcmp(*argv, "-X9_62_256v1") == 0)
256                         curve_type = EC_GROUP_X9_62_PRIME_256V1;
257                 else if (strcmp(*argv, "-noout") == 0)
258                         noout=1;
259                 else
260                 {
261                         BIO_printf(bio_err,"unknown option %s\n",*argv);
262                         badops=1;
263                         break;
264                 }
265                 argc--;
266                 argv++;
267         }
268
269         if (badops)
270         {
271 bad:
272                 BIO_printf(bio_err,"%s [options] [bits] <infile >outfile\n",prog);
273                 BIO_printf(bio_err,"where options are\n");
274                 BIO_printf(bio_err," -inform arg   input format - DER or PEM\n");
275                 BIO_printf(bio_err," -outform arg  output format - DER or PEM\n");
276                 BIO_printf(bio_err," -in arg       input file\n");
277                 BIO_printf(bio_err," -out arg      output file\n");
278                 BIO_printf(bio_err," -text         print the key in text\n");
279                 BIO_printf(bio_err," -C            Output C code\n");
280                 BIO_printf(bio_err," -noout        no output\n");
281                 BIO_printf(bio_err," -rand         files to use for random number input\n");
282                 BIO_printf(bio_err," -engine e     use engine e, possibly a hardware device.\n");
283                 BIO_printf(bio_err," -named_curve  use the curve oid instead of the parameters\n");
284                 BIO_printf(bio_err," -NIST_192     use the NIST recommeded curve parameters over a 192 bit prime field\n");
285                 BIO_printf(bio_err," -NIST_224     use the NIST recommeded curve parameters over a 224 bit prime field\n");
286                 BIO_printf(bio_err," -NIST_256     use the NIST recommeded curve parameters over a 256 bit prime field\n");
287                 BIO_printf(bio_err," -NIST_384     use the NIST recommeded curve parameters over a 384 bit prime field\n");
288                 BIO_printf(bio_err," -NIST_521     use the NIST recommeded curve parameters over a 521 bit prime field\n");
289                 BIO_printf(bio_err," -X9_62_192v1  use the X9_62 192v1 example curve over a 192 bit prime field\n");
290                 BIO_printf(bio_err," -X9_62_192v2  use the X9_62 192v2 example curve over a 192 bit prime field\n");
291                 BIO_printf(bio_err," -X9_62_192v3  use the X9_62 192v3 example curve over a 192 bit prime field\n");
292                 BIO_printf(bio_err," -X9_62_239v1  use the X9_62 239v1 example curve over a 239 bit prime field\n");
293                 BIO_printf(bio_err," -X9_62_239v2  use the X9_62 239v2 example curve over a 239 bit prime field\n");
294                 BIO_printf(bio_err," -X9_62_239v3  use the X9_62 239v3 example curve over a 239 bit prime field\n");
295                 BIO_printf(bio_err," -X9_62_256v1  use the X9_62 239v1 example curve over a 256 bit prime field\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 (need_rand)
341         {
342                 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
343                 if (inrand != NULL)
344                         BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
345                                 app_RAND_load_files(inrand));
346         }
347
348         if (curve_type != EC_GROUP_NO_CURVE)
349         {
350                 if ((ecdsa = ECDSA_new()) == NULL)
351                         goto end;
352                 ecdsa->group = EC_GROUP_new_by_name(curve_type);
353                 if (named_curve)
354                         ECDSA_set_parameter_flags(ecdsa, ECDSA_FLAG_NAMED_CURVE);
355         }
356         else if (informat == FORMAT_ASN1)
357                 ecdsa = d2i_ECDSAParameters_bio(in,NULL);
358         else if (informat == FORMAT_PEM)
359                 ecdsa = PEM_read_bio_ECDSAParameters(in, NULL, NULL, NULL);
360         else
361         {
362                 BIO_printf(bio_err, "bad input format specified\n");
363                 goto end;
364         }
365         if (ecdsa == NULL)
366         {
367                 BIO_printf(bio_err, "unable to load ECDSA parameters\n");
368                 ERR_print_errors(bio_err);
369                 goto end;
370         }
371
372         if (text)
373         {
374                 ECDSAParameters_print(out, ecdsa);
375         }
376         
377         if (C)
378         {       // TODO : characteristic two
379                 int     l, len, bits_p, bits_a, bits_b, bits_x, bits_y, bits_o, bits_c;
380                 if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL ||
381                     (tmp_3 = BN_new()) == NULL || (tmp_4 = BN_new()) == NULL ||
382                     (tmp_5 = BN_new()) == NULL || (tmp_6 = BN_new()) == NULL ||
383                     (tmp_7 = BN_new()) == NULL || (ctx = BN_CTX_new()) == NULL)
384                 {
385                         perror("OPENSSL_malloc");
386                         goto end;
387                 }
388                 if (!EC_GROUP_get_curve_GFp(ecdsa->group, tmp_1, tmp_2, tmp_3, ctx))
389                         goto end;
390                 if ((point = EC_GROUP_get0_generator(ecdsa->group)) == NULL)
391                         goto end;
392                 if (!EC_POINT_get_affine_coordinates_GFp(ecdsa->group, point, tmp_4, tmp_5, ctx))
393                         goto end;
394                 if (!EC_GROUP_get_order(ecdsa->group, tmp_6, ctx))
395                         goto end;
396                 if (!EC_GROUP_get_cofactor(ecdsa->group, tmp_7, ctx))
397                         goto end;
398                 
399                 len    = BN_num_bytes(tmp_1);
400                 bits_p = BN_num_bits(tmp_1);
401                 bits_a = BN_num_bits(tmp_2);
402                 bits_b = BN_num_bits(tmp_3);
403                 bits_x = BN_num_bits(tmp_4);
404                 bits_y = BN_num_bits(tmp_5);
405                 bits_o = BN_num_bits(tmp_6);
406                 bits_c = BN_num_bits(tmp_7);
407                 data=(unsigned char *)OPENSSL_malloc(len+20);
408                 if (data == NULL)
409                 {
410                         perror("OPENSSL_malloc");
411                         goto end;
412                 }
413                 l = BN_bn2bin(tmp_1, data);
414                 printf("static unsigned char ecdsa%d_p[]={", bits_p);
415                 for (i=0; i<l; i++)
416                 {
417                         if ((i%12) == 0) printf("\n\t");
418                         printf("0x%02X,",data[i]);
419                 }
420                 printf("\n\t};\n");
421
422                 l = BN_bn2bin(tmp_2, data);
423                 printf("static unsigned char ecdsa%d_a[]={",bits_p);
424                 for (i=0; i<l; i++)
425                 {
426                         if ((i%12) == 0) printf("\n\t");
427                         printf("0x%02X,",data[i]);
428                 }
429                 printf("\n\t};\n");
430
431                 l = BN_bn2bin(tmp_3, data);
432                 printf("static unsigned char ecdsa%d_b[]={", bits_p);
433                 for (i=0; i<l; i++)
434                 {
435                         if ((i%12) == 0) printf("\n\t");
436                         printf("0x%02X,",data[i]);
437                 }
438                 printf("\n\t};\n\n");
439
440                 l = BN_bn2bin(tmp_3, data);
441                 printf("static unsigned char ecdsa%d_x[]={", bits_x);
442                 for (i=0; i<l; i++)
443                 {
444                         if ((i%12) == 0) printf("\n\t");
445                         printf("0x%02X,",data[i]);
446                 }
447                 printf("\n\t};\n\n");
448
449                 l = BN_bn2bin(tmp_3, data);
450                 printf("static unsigned char ecdsa%d_y[]={", bits_y);
451                 for (i=0; i<l; i++)
452                 {
453                         if ((i%12) == 0) printf("\n\t");
454                         printf("0x%02X,",data[i]);
455                 }
456                 printf("\n\t};\n\n");
457
458                 l = BN_bn2bin(tmp_3, data);
459                 printf("static unsigned char ecdsa%d_o[]={", bits_o);
460                 for (i=0; i<l; i++)
461                 {
462                         if ((i%12) == 0) printf("\n\t");
463                         printf("0x%02X,",data[i]);
464                 }
465                 printf("\n\t};\n\n");
466
467                 l = BN_bn2bin(tmp_3, data);
468                 printf("static unsigned char ecdsa%d_c[]={", bits_c);
469                 for (i=0; i<l; i++)
470                 {
471                         if ((i%12) == 0) printf("\n\t");
472                         printf("0x%02X,",data[i]);
473                 }
474                 printf("\n\t};\n\n");
475
476                 printf("ECDSA *get_ecdsa%d()\n\t{\n",bits_p);
477                 printf("\tint      ok=1;\n");
478                 printf("\tECDSA    *ecdsa=NULL;\n");
479                 printf("\tEC_POINT *point=NULL;\n");
480                 printf("\tBIGNUM   *tmp_1=NULL,*tmp_2=NULL,*tmp_3=NULL;\n");
481                 printf("\tBN_CTX   *ctx=NULL;\n\n");
482                 printf("\tecdsa=ECDSA_new();\n");
483                 printf("\ttmp_1=BN_new();\n");
484                 printf("\ttmp_2=BN_new();\n");
485                 printf("\ttmp_3=BN_new();\n");
486                 printf("\tctx=BN_CTX_new();\n");
487                 printf("\tif (!ecdsa || !tmp_1 || !tmp_2 || !tmp_3 || !ctx) ok=0;\n");
488                 printf("\tif (ok && !ecdsa->group=EC_GROUP_new(EC_GFp_mont_method())) == NULL) ok=0;");
489                 printf("\tif (ok && !BN_bin2bn(ecdsa%d_p,sizeof(ecdsa%d_p),tmp_1)) ok=0;\n", bits_p, bits_p);
490                 printf("\tif (ok && !BN_bin2bn(ecdsa%d_a,sizeof(ecdsa%d_a),tmp_2)) ok=0;\n", bits_p, bits_p);
491                 printf("\tif (ok && !BN_bin2bn(ecdsa%d_b,sizeof(ecdsa%d_b),tmp_3)) ok=0;\n", bits_p, bits_p);
492                 printf("\tif (ok && !EC_GROUP_set_curve_GFp(ecdsa->group,tmp_1,tmp_2,tmp_3,ctx)) ok=0;\n");
493                 printf("\tif (ok && !BN_bin2bn(ecdsa%d_x,sizeof(ecdsa%d_p),tmp_1)) ok=0;\n", bits_p, bits_p);
494                 printf("\tif (ok && !BN_bin2bn(ecdsa%d_y,sizeof(ecdsa%d_a),tmp_2)) ok=0;\n", bits_p, bits_p);
495                 printf("\tif (ok && (point = EC_POINT_new(ecdsa->group)) == NULL) ok=0;\n");
496                 printf("\tif (ok && !EC_POINT_set_affine_coordinates_GFp(ecdsa->group,point,tmp_1,tmp_2,ctx)) ok=0:\n");
497                 printf("\tif (ok && !BN_bin2bn(ecdsa%d_o,sizeof(ecdsa%d_b),tmp_1)) ok=0;\n", bits_p, bits_p);
498                 printf("\tif (ok && !BN_bin2bn(ecdsa%d_c,sizeof(ecdsa%d_b),tmp_2)) ok=0;\n", bits_p, bits_p);
499                 printf("\tif (ok && !EC_GROUP_set_generator(ecdsa->group,point,tmp_1,tmp_2)) ok=0;\n");
500                 printf("\tif ((ecdsa->group == NULL) || (ecdsa->pub_key == NULL) || (ecdsa->priv_key == NULL))\n");
501                 printf("\t\t{ ECDSA_free(ecdsa); return(NULL); }\n");
502                 printf("\treturn(ecdsa);\n\t}\n");
503         }
504
505
506         if (!noout)
507         {
508                 if (outformat == FORMAT_ASN1)
509                         i = i2d_ECDSAParameters_bio(out, ecdsa);
510                 else if (outformat == FORMAT_PEM)
511                         i = PEM_write_bio_ECDSAParameters(out, ecdsa);
512                 else    
513                 {
514                         BIO_printf(bio_err,"bad output format specified for outfile\n");
515                         goto end;
516                 }
517                 if (!i)
518                 {
519                         BIO_printf(bio_err, "unable to write ECDSA parameters\n");
520                         ERR_print_errors(bio_err);
521                         goto end;
522                 }
523         }
524         if (genkey)
525         {
526                 ECDSA *ecdsakey;
527
528                 assert(need_rand);
529                 if ((ecdsakey = ECDSAParameters_dup(ecdsa)) == NULL) goto end;
530                 if (!ECDSA_generate_key(ecdsakey)) goto end;
531                 if (outformat == FORMAT_ASN1)
532                         i = i2d_ECDSAPrivateKey_bio(out, ecdsakey);
533                 else if (outformat == FORMAT_PEM)
534                         i = PEM_write_bio_ECDSAPrivateKey(out, ecdsakey, NULL, NULL, 0, NULL, NULL);
535                 else    
536                 {
537                         BIO_printf(bio_err, "bad output format specified for outfile\n");
538                         goto end;
539                 }
540                 ECDSA_free(ecdsakey);
541         }
542         if (need_rand)
543                 app_RAND_write_file(NULL, bio_err);
544         ret=0;
545 end:
546         if (in != NULL)         BIO_free(in);
547         if (out != NULL)        BIO_free_all(out);
548         if (ecdsa != NULL)      ECDSA_free(ecdsa);
549         if (tmp_1)              BN_free(tmp_1);
550         if (tmp_2)              BN_free(tmp_2);
551         if (tmp_3)              BN_free(tmp_3);
552         if (tmp_3)              BN_free(tmp_4);
553         if (tmp_3)              BN_free(tmp_5);
554         if (tmp_3)              BN_free(tmp_6);
555         if (tmp_3)              BN_free(tmp_7);
556         if (ctx)                BN_CTX_free(ctx);
557         if (data)               OPENSSL_free(data);
558         apps_shutdown();
559         EXIT(ret);
560 }
561 #endif