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