harmonize options with those for 'ecparam',
[openssl.git] / apps / ecdsa.c
1 /* apps/ecdsa.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
56 #ifndef OPENSSL_NO_ECDSA
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include "apps.h"
61 #include <openssl/bio.h>
62 #include <openssl/err.h>
63 #include <openssl/ecdsa.h>
64 #include <openssl/evp.h>
65 #include <openssl/x509.h>
66 #include <openssl/pem.h>
67
68 #undef PROG
69 #define PROG    ecdsa_main
70
71 /* -inform arg    - input format - default PEM (one of DER, NET or PEM)
72  * -outform arg   - output format - default PEM
73  * -in arg        - input file - default stdin
74  * -out arg       - output file - default stdout
75  * -des           - encrypt output if PEM format with DES in cbc mode
76  * -text          - print a text version
77  * -param_out     - print the elliptic curve parameters
78  * -conv_form arg - specifies the point encoding form
79  * -param_enc arg - specifies the parameter encoding
80  */
81
82 int MAIN(int, char **);
83
84 int MAIN(int argc, char **argv)
85 {
86         ENGINE  *e = NULL;
87         int     ret = 1;
88         ECDSA   *ecdsa = NULL;
89         int     i, badops = 0;
90         const EVP_CIPHER *enc = NULL;
91         BIO     *in = NULL, *out = NULL;
92         int     informat, outformat, text=0, noout=0;
93         int     pubin = 0, pubout = 0, param_out = 0;
94         char    *infile, *outfile, *prog, *engine;
95         char    *passargin = NULL, *passargout = NULL;
96         char    *passin = NULL, *passout = NULL;
97         point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED;
98         int     new_form = 0;
99         int     asn1_flag = OPENSSL_EC_NAMED_CURVE;
100         int     new_asn1_flag = 0;
101
102         apps_startup();
103
104         if (bio_err == NULL)
105                 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
106                         BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
107
108         if (!load_config(bio_err, NULL))
109                 goto end;
110
111         engine = NULL;
112         infile = NULL;
113         outfile = NULL;
114         informat = FORMAT_PEM;
115         outformat = FORMAT_PEM;
116
117         prog = argv[0];
118         argc--;
119         argv++;
120         while (argc >= 1)
121                 {
122                 if (strcmp(*argv,"-inform") == 0)
123                         {
124                         if (--argc < 1) goto bad;
125                         informat=str2fmt(*(++argv));
126                         }
127                 else if (strcmp(*argv,"-outform") == 0)
128                         {
129                         if (--argc < 1) goto bad;
130                         outformat=str2fmt(*(++argv));
131                         }
132                 else if (strcmp(*argv,"-in") == 0)
133                         {
134                         if (--argc < 1) goto bad;
135                         infile= *(++argv);
136                         }
137                 else if (strcmp(*argv,"-out") == 0)
138                         {
139                         if (--argc < 1) goto bad;
140                         outfile= *(++argv);
141                         }
142                 else if (strcmp(*argv,"-passin") == 0)
143                         {
144                         if (--argc < 1) goto bad;
145                         passargin= *(++argv);
146                         }
147                 else if (strcmp(*argv,"-passout") == 0)
148                         {
149                         if (--argc < 1) goto bad;
150                         passargout= *(++argv);
151                         }
152                 else if (strcmp(*argv, "-engine") == 0)
153                         {
154                         if (--argc < 1) goto bad;
155                         engine= *(++argv);
156                         }
157                 else if (strcmp(*argv, "-noout") == 0)
158                         noout = 1;
159                 else if (strcmp(*argv, "-text") == 0)
160                         text = 1;
161                 else if (strcmp(*argv, "-conv_form") == 0)
162                         {
163                         if (--argc < 1)
164                                 goto bad;
165                         ++argv;
166                         new_form = 1;
167                         if (strcmp(*argv, "compressed") == 0)
168                                 form = POINT_CONVERSION_COMPRESSED;
169                         else if (strcmp(*argv, "uncompressed") == 0)
170                                 form = POINT_CONVERSION_UNCOMPRESSED;
171                         else if (strcmp(*argv, "hybrid") == 0)
172                                 form = POINT_CONVERSION_HYBRID;
173                         else
174                                 goto bad;
175                         }
176                 else if (strcmp(*argv, "-param_enc") == 0)
177                         {
178                         if (--argc < 1)
179                                 goto bad;
180                         ++argv;
181                         new_asn1_flag = 1;
182                         if (strcmp(*argv, "named_curve") == 0)
183                                 asn1_flag = OPENSSL_EC_NAMED_CURVE;
184                         else if (strcmp(*argv, "explicit") == 0)
185                                 asn1_flag = 0;
186                         else
187                                 goto bad;
188                         }
189                 else if (strcmp(*argv, "-param_out") == 0)
190                         param_out = 1;
191                 else if (strcmp(*argv, "-pubin") == 0)
192                         pubin=1;
193                 else if (strcmp(*argv, "-pubout") == 0)
194                         pubout=1;
195                 else if ((enc=EVP_get_cipherbyname(&(argv[0][1]))) == NULL)
196                         {
197                         BIO_printf(bio_err, "unknown option %s\n", *argv);
198                         badops=1;
199                         break;
200                         }
201                 argc--;
202                 argv++;
203                 }
204
205         if (badops)
206                 {
207 bad:
208                 BIO_printf(bio_err, "%s [options] <infile >outfile\n", prog);
209                 BIO_printf(bio_err, "where options are\n");
210                 BIO_printf(bio_err, " -inform arg     input format - "
211                                 "DER or PEM\n");
212                 BIO_printf(bio_err, " -outform arg    output format - "
213                                 "DER or PEM\n");
214                 BIO_printf(bio_err, " -in arg         input file\n");
215                 BIO_printf(bio_err, " -passin arg     input file pass "
216                                 "phrase source\n");
217                 BIO_printf(bio_err, " -out arg        output file\n");
218                 BIO_printf(bio_err, " -passout arg    output file pass "
219                                 "phrase source\n");
220                 BIO_printf(bio_err, " -engine e       use engine e, "
221                                 "possibly a hardware device.\n");
222                 BIO_printf(bio_err, " -des            encrypt PEM output, "
223                                 "instead of 'des' every other \n"
224                                 "                 cipher "
225                                 "supported by OpenSSL can be used\n");
226                 BIO_printf(bio_err, " -text           print the key\n");
227                 BIO_printf(bio_err, " -noout          don't print key out\n");
228                 BIO_printf(bio_err, " -param_out      print the elliptic "
229                                 "curve parameters\n");
230                 BIO_printf(bio_err, " -conv_form arg  specifies the "
231                                 "point conversion form \n");
232                 BIO_printf(bio_err, "                 possible values :"
233                                 " compressed\n");
234                 BIO_printf(bio_err, "                                  "
235                                 " uncompressed (default)\n");
236                 BIO_printf(bio_err, "                                  "
237                                 " hybrid\n");
238                 BIO_printf(bio_err, " -param_enc arg  specifies the way"
239                                 " the ec parameters are encoded\n");
240                 BIO_printf(bio_err, "                 in the asn1 der "
241                                 "encoding\n");
242                 BIO_printf(bio_err, "                 possilbe values :"
243                                 " named_curve (default)\n");
244                 BIO_printf(bio_err,"                                   "
245                                 "explicit\n");
246                 goto end;
247                 }
248
249         ERR_load_crypto_strings();
250
251         e = setup_engine(bio_err, engine, 0);
252
253         if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) 
254                 {
255                 BIO_printf(bio_err, "Error getting passwords\n");
256                 goto end;
257                 }
258
259         in = BIO_new(BIO_s_file());
260         out = BIO_new(BIO_s_file());
261         if ((in == NULL) || (out == NULL))
262                 {
263                 ERR_print_errors(bio_err);
264                 goto end;
265                 }
266
267         if (infile == NULL)
268                 BIO_set_fp(in, stdin, BIO_NOCLOSE);
269         else
270                 {
271                 if (BIO_read_filename(in, infile) <= 0)
272                         {
273                         perror(infile);
274                         goto end;
275                         }
276                 }
277
278         BIO_printf(bio_err, "read ECDSA key\n");
279         if (informat == FORMAT_ASN1) 
280                 {
281                 if (pubin) 
282                         ecdsa = d2i_ECDSA_PUBKEY_bio(in, NULL);
283                 else 
284                         ecdsa = d2i_ECDSAPrivateKey_bio(in, NULL);
285                 } 
286         else if (informat == FORMAT_PEM) 
287                 {
288                 if (pubin) 
289                         ecdsa = PEM_read_bio_ECDSA_PUBKEY(in, NULL, NULL, 
290                                 NULL);
291                 else 
292                         ecdsa = PEM_read_bio_ECDSAPrivateKey(in, NULL, NULL,
293                                 passin);
294                 } 
295         else
296                 {
297                 BIO_printf(bio_err, "bad input format specified for key\n");
298                 goto end;
299                 }
300         if (ecdsa == NULL)
301                 {
302                 BIO_printf(bio_err,"unable to load Key\n");
303                 ERR_print_errors(bio_err);
304                 goto end;
305                 }
306
307         if (outfile == NULL)
308                 {
309                 BIO_set_fp(out, stdout, BIO_NOCLOSE);
310 #ifdef OPENSSL_SYS_VMS
311                         {
312                         BIO *tmpbio = BIO_new(BIO_f_linebuffer());
313                         out = BIO_push(tmpbio, out);
314                         }
315 #endif
316                 }
317         else
318                 {
319                 if (BIO_write_filename(out, outfile) <= 0)
320                         {
321                         perror(outfile);
322                         goto end;
323                         }
324                 }
325
326         if (new_form)
327                 {
328                 EC_GROUP_set_point_conversion_form(ecdsa->group, form);
329                 ECDSA_set_conversion_form(ecdsa, form);
330                 }
331
332         if (new_asn1_flag)
333                 EC_GROUP_set_asn1_flag(ecdsa->group, asn1_flag);
334
335         if (text) 
336                 if (!ECDSA_print(out, ecdsa, 0))
337                         {
338                         perror(outfile);
339                         ERR_print_errors(bio_err);
340                         goto end;
341                         }
342
343         if (noout) 
344                 goto end;
345
346         BIO_printf(bio_err, "writing ECDSA key\n");
347         if (outformat == FORMAT_ASN1) 
348                 {
349                 if (param_out)
350                         i = i2d_ECPKParameters_bio(out, ecdsa->group);
351                 else if (pubin || pubout) 
352                         i = i2d_ECDSA_PUBKEY_bio(out, ecdsa);
353                 else 
354                         i = i2d_ECDSAPrivateKey_bio(out, ecdsa);
355                 } 
356         else if (outformat == FORMAT_PEM) 
357                 {
358                 if (param_out)
359                         i = PEM_write_bio_ECPKParameters(out, ecdsa->group);
360                 else if (pubin || pubout)
361                         i = PEM_write_bio_ECDSA_PUBKEY(out, ecdsa);
362                 else 
363                         i = PEM_write_bio_ECDSAPrivateKey(out, ecdsa, enc,
364                                                 NULL, 0, NULL, passout);
365                 } 
366         else 
367                 {
368                 BIO_printf(bio_err, "bad output format specified for "
369                         "outfile\n");
370                 goto end;
371                 }
372
373         if (!i)
374                 {
375                 BIO_printf(bio_err, "unable to write private key\n");
376                 ERR_print_errors(bio_err);
377                 }
378         else
379                 ret=0;
380 end:
381         if (in)
382                 BIO_free(in);
383         if (out)
384                 BIO_free_all(out);
385         if (ecdsa)
386                 ECDSA_free(ecdsa);
387         if (passin)
388                 OPENSSL_free(passin);
389         if (passout)
390                 OPENSSL_free(passout);
391         apps_shutdown();
392         EXIT(ret);
393 }
394 #endif