Uhmmm, if we use && after having tested for the presence of the certificate,
[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  * -check               - validate the ec parameters
137  * -C
138  * -noout
139  * -genkey              - generate a private public keypair based on the supplied curve
140  * -named_curve         - use the curve oid instead of the parameters
141  * -NIST_192            - use the NIST recommended curve parameters over a 192 bit prime field
142  * -NIST_224            - use the NIST recommended curve parameters over a 224 bit prime field
143  * -NIST_256            - use the NIST recommended curve parameters over a 256 bit prime field
144  * -NIST_384            - use the NIST recommended curve parameters over a 384 bit prime field
145  * -NIST_521            - use the NIST recommended curve parameters over a 521 bit prime field
146  * -X9_62_192v1         - use the X9_62 192v1 example curve over a 192 bit prime field
147  * -X9_62_192v2         - use the X9_62 192v2 example curve over a 192 bit prime field
148  * -X9_62_192v3         - use the X9_62 192v3 example curve over a 192 bit prime field
149  * -X9_62_239v1         - use the X9_62 239v1 example curve over a 239 bit prime field
150  * -X9_62_239v2         - use the X9_62 239v2 example curve over a 239 bit prime field
151  * -X9_62_239v3         - use the X9_62 239v3 example curve over a 239 bit prime field
152  * -X9_62_256v1         - use the X9_62 239v1 example curve over a 256 bit prime field
153  * -SECG_PRIME_112R1    - use the SECG 112r1 recommended curve over a 112 bit prime field
154  * -SECG_PRIME_112R2    - use the SECG 112r2 recommended curve over a 112 bit prime field
155  * -SECG_PRIME_128R1    - use the SECG 128r1 recommended curve over a 128 bit prime field
156  * -SECG_PRIME_128R2    - use the SECG 128r2 recommended curve over a 128 bit prime field
157  * -SECG_PRIME_160K1    - use the SECG 160k1 recommended curve over a 160 bit prime field
158  * -SECG_PRIME_160R1    - use the SECG 160r1 recommended curve over a 160 bit prime field
159  * -SECG_PRIME_160R2    - use the SECG 160r2 recommended curve over a 160 bit prime field
160  * -SECG_PRIME_192K1    - use the SECG 192k1 recommended curve over a 192 bit prime field
161  * -SECG_PRIME_192R1    - use the SECG 192r1 recommended curve over a 192 bit prime field
162  * -SECG_PRIME_224K1    - use the SECG 224k1 recommended curve over a 224 bit prime field
163  * -SECG_PRIME_224R1    - use the SECG 224r1 recommended curve over a 224 bit prime field
164  * -SECG_PRIME_256K1    - use the SECG 256k1 recommended curve over a 256 bit prime field
165  * -SECG_PRIME_256R1    - use the SECG 256r1 recommended curve over a 256 bit prime field
166  * -SECG_PRIME_384R1    - use the SECG 384r1 recommended curve over a 384 bit prime field
167  * -SECG_PRIME_521R1    - use the SECG 521r1 recommended curve over a 521 bit prime field
168  * -WTLS_6              - use the WAP/WTLS recommended curve number 6 over a 112 bit field
169  * -WTLS_8              - use the WAP/WTLS recommended curve number 8 over a 112 bit field
170  * -WTLS_9              - use the WAP/WTLS recommended curve number 9 over a 160 bit field
171  */
172
173 int MAIN(int, char **);
174
175 int MAIN(int argc, char **argv)
176 {
177         ENGINE  *e = NULL;
178         ECDSA   *ecdsa = NULL;
179         int     i, badops = 0, text = 0;
180         BIO     *in = NULL, *out = NULL;
181         int     informat, outformat, noout = 0, C = 0, ret = 1;
182         char    *infile, *outfile, *prog, *inrand = NULL;
183         int     genkey = 0;
184         int     check = 0;
185         int     need_rand = 0;
186         char    *engine=NULL;
187         int     curve_type = EC_GROUP_NO_CURVE;
188         int     named_curve = 0;
189         BIGNUM  *tmp_1 = NULL, *tmp_2 = NULL, *tmp_3 = NULL, *tmp_4 = NULL, *tmp_5 = NULL,
190                 *tmp_6 = NULL, *tmp_7 = NULL;
191         BN_CTX  *ctx = NULL;
192         EC_POINT *point = NULL;
193         unsigned char *data = NULL;
194
195         apps_startup();
196
197         if (bio_err == NULL)
198                 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
199                         BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
200
201         if (!load_config(bio_err, NULL))
202                 goto end;
203
204         infile=NULL;
205         outfile=NULL;
206         informat=FORMAT_PEM;
207         outformat=FORMAT_PEM;
208
209         prog=argv[0];
210         argc--;
211         argv++;
212         while (argc >= 1)
213                 {
214                 if      (strcmp(*argv,"-inform") == 0)
215                 {
216                         if (--argc < 1) goto bad;
217                         informat=str2fmt(*(++argv));
218                 }
219                 else if (strcmp(*argv,"-outform") == 0)
220                 {
221                         if (--argc < 1) goto bad;
222                         outformat=str2fmt(*(++argv));
223                 }
224                 else if (strcmp(*argv,"-in") == 0)
225                 {
226                         if (--argc < 1) goto bad;
227                         infile= *(++argv);
228                 }
229                 else if (strcmp(*argv,"-out") == 0)
230                 {
231                         if (--argc < 1) goto bad;
232                         outfile= *(++argv);
233                 }
234                 else if(strcmp(*argv, "-engine") == 0)
235                 {
236                         if (--argc < 1) goto bad;
237                         engine = *(++argv);
238                 }
239                 else if (strcmp(*argv,"-text") == 0)
240                         text = 1;
241                 else if (strcmp(*argv,"-C") == 0)
242                         C = 1;
243                 else if (strcmp(*argv,"-check") == 0)
244                         check = 1;
245                 else if (strcmp(*argv,"-genkey") == 0)
246                 {
247                         genkey = 1;
248                         need_rand = 1;
249                 }
250                 else if (strcmp(*argv,"-rand") == 0)
251                 {
252                         if (--argc < 1) goto bad;
253                         inrand= *(++argv);
254                         need_rand=1;
255                 }
256                 else if (strcmp(*argv, "-named_curve") == 0)
257                         named_curve = 1;
258                 else if (strcmp(*argv, "-NIST_192") == 0)
259                         curve_type = EC_GROUP_NIST_PRIME_192;
260                 else if (strcmp(*argv, "-NIST_224") == 0)
261                         curve_type = EC_GROUP_NIST_PRIME_224;
262                 else if (strcmp(*argv, "-NIST_256") == 0)
263                         curve_type = EC_GROUP_NIST_PRIME_256;
264                 else if (strcmp(*argv, "-NIST_384") == 0)
265                         curve_type = EC_GROUP_NIST_PRIME_384;
266                 else if (strcmp(*argv, "-NIST_521") == 0)
267                         curve_type = EC_GROUP_NIST_PRIME_521;
268                 else if (strcmp(*argv, "-X9_62_192v1") == 0)
269                         curve_type = EC_GROUP_X9_62_PRIME_192V1;
270                 else if (strcmp(*argv, "-X9_62_192v2") == 0)
271                         curve_type = EC_GROUP_X9_62_PRIME_192V2;
272                 else if (strcmp(*argv, "-X9_62_192v3") == 0)
273                         curve_type = EC_GROUP_X9_62_PRIME_192V3;
274                 else if (strcmp(*argv, "-X9_62_239v1") == 0)
275                         curve_type = EC_GROUP_X9_62_PRIME_239V1;
276                 else if (strcmp(*argv, "-X9_62_239v2") == 0)
277                         curve_type = EC_GROUP_X9_62_PRIME_239V2;
278                 else if (strcmp(*argv, "-X9_62_239v3") == 0)
279                         curve_type = EC_GROUP_X9_62_PRIME_239V3;
280                 else if (strcmp(*argv, "-X9_62_256v1") == 0)
281                         curve_type = EC_GROUP_X9_62_PRIME_256V1;
282                 else if (strcmp(*argv, "-SECG_PRIME_112R1") == 0)
283                         curve_type = EC_GROUP_SECG_PRIME_112R1;
284                 else if (strcmp(*argv, "-SECG_PRIME_112R2") == 0)
285                         curve_type = EC_GROUP_SECG_PRIME_112R2;
286                 else if (strcmp(*argv, "-SECG_PRIME_128R1") == 0)
287                         curve_type = EC_GROUP_SECG_PRIME_128R1;
288                 else if (strcmp(*argv, "-SECG_PRIME_128R2") == 0)
289                         curve_type = EC_GROUP_SECG_PRIME_128R2;
290                 else if (strcmp(*argv, "-SECG_PRIME_160K1") == 0)
291                         curve_type = EC_GROUP_SECG_PRIME_160K1;
292                 else if (strcmp(*argv, "-SECG_PRIME_160R1") == 0)
293                         curve_type = EC_GROUP_SECG_PRIME_160R1;
294                 else if (strcmp(*argv, "-SECG_PRIME_160R2") == 0)
295                         curve_type = EC_GROUP_SECG_PRIME_160R2;
296                 else if (strcmp(*argv, "-SECG_PRIME_192K1") == 0)
297                         curve_type = EC_GROUP_SECG_PRIME_192K1;
298                 else if (strcmp(*argv, "-SECG_PRIME_192R1") == 0)
299                         curve_type = EC_GROUP_SECG_PRIME_192R1;
300                 else if (strcmp(*argv, "-SECG_PRIME_224K1") == 0)
301                         curve_type = EC_GROUP_SECG_PRIME_224K1;
302                 else if (strcmp(*argv, "-SECG_PRIME_224R1") == 0)
303                         curve_type = EC_GROUP_SECG_PRIME_224R1;
304                 else if (strcmp(*argv, "-SECG_PRIME_256K1") == 0)
305                         curve_type = EC_GROUP_SECG_PRIME_256K1;
306                 else if (strcmp(*argv, "-SECG_PRIME_256R1") == 0)
307                         curve_type = EC_GROUP_SECG_PRIME_256R1;
308                 else if (strcmp(*argv, "-SECG_PRIME_384R1") == 0)
309                         curve_type = EC_GROUP_SECG_PRIME_384R1;
310                 else if (strcmp(*argv, "-SECG_PRIME_521R1") == 0)
311                         curve_type = EC_GROUP_SECG_PRIME_521R1;
312                 else if (strcmp(*argv, "-WTLS_6") == 0)
313                         curve_type = EC_GROUP_WTLS_6;
314                 else if (strcmp(*argv, "-WTLS_8") == 0)
315                         curve_type = EC_GROUP_WTLS_8;
316                 else if (strcmp(*argv, "-WTLS_9") == 0)
317                         curve_type = EC_GROUP_WTLS_9;
318                 else if (strcmp(*argv, "-noout") == 0)
319                         noout=1;
320                 else
321                 {
322                         BIO_printf(bio_err,"unknown option %s\n",*argv);
323                         badops=1;
324                         break;
325                 }
326                 argc--;
327                 argv++;
328         }
329
330         if (badops)
331         {
332 bad:
333                 BIO_printf(bio_err,"%s [options] [bits] <infile >outfile\n",prog);
334                 BIO_printf(bio_err,"where options are\n");
335                 BIO_printf(bio_err," -inform arg        input format - DER or PEM\n");
336                 BIO_printf(bio_err," -outform arg       output format - DER or PEM\n");
337                 BIO_printf(bio_err," -in arg            input file\n");
338                 BIO_printf(bio_err," -out arg           output file\n");
339                 BIO_printf(bio_err," -text              print the key in text\n");
340                 BIO_printf(bio_err," -C                 Output C code\n");
341                 BIO_printf(bio_err," -check             validate the ec parameters\n");
342                 BIO_printf(bio_err," -noout             no output\n");
343                 BIO_printf(bio_err," -rand              files to use for random number input\n");
344                 BIO_printf(bio_err," -engine e          use engine e, possibly a hardware device.\n");
345                 BIO_printf(bio_err," -named_curve       use the curve oid instead of the parameters\n");
346                 BIO_printf(bio_err," -NIST_192          use the NIST recommended curve parameters over a 192 bit prime field\n");
347                 BIO_printf(bio_err," -NIST_224          use the NIST recommended curve parameters over a 224 bit prime field\n");
348                 BIO_printf(bio_err," -NIST_256          use the NIST recommended curve parameters over a 256 bit prime field\n");
349                 BIO_printf(bio_err," -NIST_384          use the NIST recommended curve parameters over a 384 bit prime field\n");
350                 BIO_printf(bio_err," -NIST_521          use the NIST recommended curve parameters over a 521 bit prime field\n");
351                 BIO_printf(bio_err," -X9_62_192v1       use the X9_62 192v1 example curve over a 192 bit prime field\n");
352                 BIO_printf(bio_err," -X9_62_192v2       use the X9_62 192v2 example curve over a 192 bit prime field\n");
353                 BIO_printf(bio_err," -X9_62_192v3       use the X9_62 192v3 example curve over a 192 bit prime field\n");
354                 BIO_printf(bio_err," -X9_62_239v1       use the X9_62 239v1 example curve over a 239 bit prime field\n");
355                 BIO_printf(bio_err," -X9_62_239v2       use the X9_62 239v2 example curve over a 239 bit prime field\n");
356                 BIO_printf(bio_err," -X9_62_239v3       use the X9_62 239v3 example curve over a 239 bit prime field\n");
357                 BIO_printf(bio_err," -X9_62_256v1       use the X9_62 239v1 example curve over a 256 bit prime field\n");
358                 BIO_printf(bio_err," -SECG_PRIME_112R1  use the SECG 112r1 recommended curve over a 112 bit prime field\n");
359                 BIO_printf(bio_err," -SECG_PRIME_112R2  use the SECG 112r2 recommended curve over a 112 bit prime field\n");
360                 BIO_printf(bio_err," -SECG_PRIME_128R1  use the SECG 128r1 recommended curve over a 128 bit prime field\n");
361                 BIO_printf(bio_err," -SECG_PRIME_128R2  use the SECG 128r2 recommended curve over a 128 bit prime field\n");
362                 BIO_printf(bio_err," -SECG_PRIME_160K1  use the SECG 160k1 recommended curve over a 160 bit prime field\n");
363                 BIO_printf(bio_err," -SECG_PRIME_160R1  use the SECG 160r1 recommended curve over a 160 bit prime field\n");
364                 BIO_printf(bio_err," -SECG_PRIME_160R2  use the SECG 160r2 recommended curve over a 160 bit prime field\n");
365                 BIO_printf(bio_err," -SECG_PRIME_192K1  use the SECG 192k1 recommended curve over a 192 bit prime field\n");
366                 BIO_printf(bio_err," -SECG_PRIME_192R1  use the SECG 192r1 recommended curve over a 192 bit prime field\n");
367                 BIO_printf(bio_err," -SECG_PRIME_224K1  use the SECG 224k1 recommended curve over a 224 bit prime field\n");
368                 BIO_printf(bio_err," -SECG_PRIME_224R1  use the SECG 224r1 recommended curve over a 224 bit prime field\n");
369                 BIO_printf(bio_err," -SECG_PRIME_256K1  use the SECG 256k1 recommended curve over a 256 bit prime field\n");
370                 BIO_printf(bio_err," -SECG_PRIME_256R1  use the SECG 256r1 recommended curve over a 256 bit prime field\n");
371                 BIO_printf(bio_err," -SECG_PRIME_384R1  use the SECG 384r1 recommended curve over a 384 bit prime field\n");
372                 BIO_printf(bio_err," -SECG_PRIME_521R1  use the SECG 521r1 recommended curve over a 521 bit prime field\n");
373                 BIO_printf(bio_err," -WTLS_6            use the WAP/WTLS recommended curve number 6 over a 112 bit field\n");
374                 BIO_printf(bio_err," -WTLS_8            use the WAP/WTLS recommended curve number 8 over a 112 bit field\n");
375                 BIO_printf(bio_err," -WTLS_9            use the WAP/WTLS recommended curve number 9 over a 112 bit field\n");
376                 goto end;
377         }
378
379         ERR_load_crypto_strings();
380
381         in=BIO_new(BIO_s_file());
382         out=BIO_new(BIO_s_file());
383         if ((in == NULL) || (out == NULL))
384         {
385                 ERR_print_errors(bio_err);
386                 goto end;
387         }
388
389         if (infile == NULL)
390                 BIO_set_fp(in,stdin,BIO_NOCLOSE);
391         else
392         {
393                 if (BIO_read_filename(in,infile) <= 0)
394                 {
395                         perror(infile);
396                         goto end;
397                 }
398         }
399         if (outfile == NULL)
400         {
401                 BIO_set_fp(out,stdout,BIO_NOCLOSE);
402 #ifdef OPENSSL_SYS_VMS
403                 {
404                 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
405                 out = BIO_push(tmpbio, out);
406                 }
407 #endif
408         }
409         else
410         {
411                 if (BIO_write_filename(out,outfile) <= 0)
412                 {
413                         perror(outfile);
414                         goto end;
415                 }
416         }
417
418         e = setup_engine(bio_err, engine, 0);
419
420         if (need_rand)
421         {
422                 app_RAND_load_file(NULL, bio_err, (inrand != NULL));
423                 if (inrand != NULL)
424                         BIO_printf(bio_err,"%ld semi-random bytes loaded\n",
425                                 app_RAND_load_files(inrand));
426         }
427
428         if (curve_type != EC_GROUP_NO_CURVE)
429         {
430                 if ((ecdsa = ECDSA_new()) == NULL)
431                         goto end;
432                 ecdsa->group = EC_GROUP_new_by_name(curve_type);
433                 if (named_curve)
434                         ECDSA_set_parameter_flags(ecdsa, ECDSA_FLAG_NAMED_CURVE);
435         }
436         else if (informat == FORMAT_ASN1)
437                 ecdsa = d2i_ECDSAParameters_bio(in,NULL);
438         else if (informat == FORMAT_PEM)
439                 ecdsa = PEM_read_bio_ECDSAParameters(in, NULL, NULL, NULL);
440         else
441         {
442                 BIO_printf(bio_err, "bad input format specified\n");
443                 goto end;
444         }
445         if (ecdsa == NULL)
446         {
447                 BIO_printf(bio_err, "unable to load ECDSA parameters\n");
448                 ERR_print_errors(bio_err);
449                 goto end;
450         }
451
452         if (text)
453         {
454                 ECDSAParameters_print(out, ecdsa);
455         }
456
457         if (check)
458         {
459                 if (ecdsa == NULL)
460                         BIO_printf(bio_err, "no elliptic curve parameters\n");
461                 BIO_printf(bio_err, "checking elliptic curve parameters: ");
462                 if (!EC_GROUP_check(ecdsa->group, NULL))
463                 {
464                         BIO_printf(bio_err, "failed\n");
465                         ERR_print_errors(bio_err);
466                 }
467                 else
468                         BIO_printf(bio_err, "ok\n");
469                         
470         }
471         
472         if (C)
473         {       /* TODO: characteristic two */
474                 int     l, len, bits_p;
475                 if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL ||
476                     (tmp_3 = BN_new()) == NULL || (tmp_4 = BN_new()) == NULL ||
477                     (tmp_5 = BN_new()) == NULL || (tmp_6 = BN_new()) == NULL ||
478                     (tmp_7 = BN_new()) == NULL || (ctx = BN_CTX_new()) == NULL)
479                 {
480                         perror("OPENSSL_malloc");
481                         goto end;
482                 }
483                 if (!EC_GROUP_get_curve_GFp(ecdsa->group, tmp_1, tmp_2, tmp_3, ctx))
484                         goto end;
485                 if ((point = EC_GROUP_get0_generator(ecdsa->group)) == NULL)
486                         goto end;
487                 if (!EC_POINT_get_affine_coordinates_GFp(ecdsa->group, point, tmp_4, tmp_5, ctx))
488                         goto end;
489                 if (!EC_GROUP_get_order(ecdsa->group, tmp_6, ctx))
490                         goto end;
491                 if (!EC_GROUP_get_cofactor(ecdsa->group, tmp_7, ctx))
492                         goto end;
493                 
494                 len    = BN_num_bytes(tmp_1);
495                 bits_p = BN_num_bits(tmp_1);
496                 data=(unsigned char *)OPENSSL_malloc(len+20);
497                 if (data == NULL)
498                 {
499                         perror("OPENSSL_malloc");
500                         goto end;
501                 }
502                 l = BN_bn2bin(tmp_1, data);
503                 printf("static unsigned char ecdsa%d_p[]={", bits_p);
504                 for (i=0; i<l; i++)
505                 {
506                         if ((i%12) == 0) printf("\n\t");
507                         printf("0x%02X,",data[i]);
508                 }
509                 printf("\n\t};\n\n");
510
511                 l = BN_bn2bin(tmp_2, data);
512                 printf("static unsigned char ecdsa%d_a[]={",bits_p);
513                 for (i=0; i<l; i++)
514                 {
515                         if ((i%12) == 0) printf("\n\t");
516                         printf("0x%02X,",data[i]);
517                 }
518                 printf("\n\t};\n");
519
520                 l = BN_bn2bin(tmp_3, data);
521                 printf("static unsigned char ecdsa%d_b[]={", bits_p);
522                 for (i=0; i<l; i++)
523                 {
524                         if ((i%12) == 0) printf("\n\t");
525                         printf("0x%02X,",data[i]);
526                 }
527                 printf("\n\t};\n\n");
528
529                 l = BN_bn2bin(tmp_4, data);
530                 printf("static unsigned char ecdsa%d_x[]={", bits_p);
531                 for (i=0; i<l; i++)
532                 {
533                         if ((i%12) == 0) printf("\n\t");
534                         printf("0x%02X,",data[i]);
535                 }
536                 printf("\n\t};\n");
537
538                 l = BN_bn2bin(tmp_5, data);
539                 printf("static unsigned char ecdsa%d_y[]={", bits_p);
540                 for (i=0; i<l; i++)
541                 {
542                         if ((i%12) == 0) printf("\n\t");
543                         printf("0x%02X,",data[i]);
544                 }
545                 printf("\n\t};\n");
546
547                 l = BN_bn2bin(tmp_6, data);
548                 printf("static unsigned char ecdsa%d_o[]={", bits_p);
549                 for (i=0; i<l; i++)
550                 {
551                         if ((i%12) == 0) printf("\n\t");
552                         printf("0x%02X,",data[i]);
553                 }
554                 printf("\n\t};\n");
555
556                 l = BN_bn2bin(tmp_7, data);
557                 printf("static unsigned char ecdsa%d_c[]={", bits_p);
558                 for (i=0; i<l; i++)
559                 {
560                         if ((i%12) == 0) printf("\n\t");
561                         printf("0x%02X,",data[i]);
562                 }
563                 printf("\n\t};\n\n");
564
565                 /* FIXME:
566                  * generated code should check for errors
567                  */
568
569                 printf("ECDSA *get_ecdsa%d(void)\n\t{\n",bits_p);
570                 printf("\tint ok=0;\n");
571                 printf("\tECDSA    *ecdsa=NULL;\n");
572                 printf("\tEC_POINT *point=NULL;\n");
573                 printf("\tBIGNUM   *tmp_1=NULL,*tmp_2=NULL,*tmp_3=NULL;\n\n");
574                 printf("\tif ((ecdsa=ECDSA_new()) == NULL)\n");
575                 printf("\t\treturn(NULL);\n\n");
576                 printf("\t/* generate EC_GROUP structure */\n");
577                 printf("\tif ((tmp_1 = BN_bin2bn(ecdsa%d_p, sizeof(ecdsa%d_p), NULL)) == NULL) goto err;\n", bits_p, bits_p);
578                 printf("\tif ((tmp_2 = BN_bin2bn(ecdsa%d_a, sizeof(ecdsa%d_a), NULL)) == NULL) goto err;\n", bits_p, bits_p);
579                 printf("\tif ((tmp_3 = BN_bin2bn(ecdsa%d_b, sizeof(ecdsa%d_b), NULL)) == NULL) goto err;\n", bits_p, bits_p);
580                 printf("\tif ((ecdsa->group = EC_GROUP_new_curve_GFp(tmp_1, tmp_2, tmp_3, NULL)) == NULL) goto err;\n\n");
581                 printf("\t/* build generator */\n");
582                 printf("\tif (!BN_bin2bn(ecdsa%d_x, sizeof(ecdsa%d_x), tmp_1)) goto err;\n", bits_p, bits_p);
583                 printf("\tif (!BN_bin2bn(ecdsa%d_y, sizeof(ecdsa%d_y), tmp_2)) goto err;\n", bits_p, bits_p);
584                 printf("\tif ((point = EC_POINT_new(ecdsa->group)) == NULL) goto err;\n");
585                 printf("\tif (!EC_POINT_set_affine_coordinates_GFp(ecdsa->group, point, tmp_1, tmp_2, NULL)) goto err;\n");
586                 printf("\t/* set generator, order and cofactor */\n");
587                 printf("\tif (!BN_bin2bn(ecdsa%d_o, sizeof(ecdsa%d_o), tmp_1)) goto err;\n", bits_p, bits_p);
588                 printf("\tif (!BN_bin2bn(ecdsa%d_c, sizeof(ecdsa%d_c), tmp_2)) goto err;\n", bits_p, bits_p);
589                 printf("\tif (!EC_GROUP_set_generator(ecdsa->group, point, tmp_1, tmp_2)) goto err;\n");
590                 printf("\n\tok=1;\n");
591                 printf("err:\n");
592                 printf("\tif (tmp_1) BN_free(tmp_1);\n");
593                 printf("\tif (tmp_2) BN_free(tmp_2);\n");
594                 printf("\tif (tmp_3) BN_free(tmp_3);\n");
595                 printf("\tif (point) EC_POINT_free(point);\n");
596                 printf("\tif (!ok)\n");
597                 printf("\t\t{\n");
598                 printf("\t\tECDSA_free(ecdsa);\n");
599                 printf("\t\tecdsa = NULL;\n");
600                 printf("\t\t}\n");
601                 printf("\treturn(ecdsa);\n\t}\n");
602         }
603
604
605         if (!noout)
606         {
607                 if (outformat == FORMAT_ASN1)
608                         i = i2d_ECDSAParameters_bio(out, ecdsa);
609                 else if (outformat == FORMAT_PEM)
610                         i = PEM_write_bio_ECDSAParameters(out, ecdsa);
611                 else    
612                 {
613                         BIO_printf(bio_err,"bad output format specified for outfile\n");
614                         goto end;
615                 }
616                 if (!i)
617                 {
618                         BIO_printf(bio_err, "unable to write ECDSA parameters\n");
619                         ERR_print_errors(bio_err);
620                         goto end;
621                 }
622         }
623         if (genkey)
624         {
625                 ECDSA *ecdsakey;
626
627                 assert(need_rand);
628                 if ((ecdsakey = ECDSAParameters_dup(ecdsa)) == NULL) goto end;
629                 if (!ECDSA_generate_key(ecdsakey)) goto end;
630                 if (outformat == FORMAT_ASN1)
631                         i = i2d_ECDSAPrivateKey_bio(out, ecdsakey);
632                 else if (outformat == FORMAT_PEM)
633                         i = PEM_write_bio_ECDSAPrivateKey(out, ecdsakey, NULL, NULL, 0, NULL, NULL);
634                 else    
635                 {
636                         BIO_printf(bio_err, "bad output format specified for outfile\n");
637                         goto end;
638                 }
639                 ECDSA_free(ecdsakey);
640         }
641         if (need_rand)
642                 app_RAND_write_file(NULL, bio_err);
643         ret=0;
644 end:
645         if (in != NULL)         BIO_free(in);
646         if (out != NULL)        BIO_free_all(out);
647         if (ecdsa != NULL)      ECDSA_free(ecdsa);
648         if (tmp_1)              BN_free(tmp_1);
649         if (tmp_2)              BN_free(tmp_2);
650         if (tmp_3)              BN_free(tmp_3);
651         if (tmp_3)              BN_free(tmp_4);
652         if (tmp_3)              BN_free(tmp_5);
653         if (tmp_3)              BN_free(tmp_6);
654         if (tmp_3)              BN_free(tmp_7);
655         if (ctx)                BN_CTX_free(ctx);
656         if (data)               OPENSSL_free(data);
657         apps_shutdown();
658         EXIT(ret);
659 }
660 #endif