2 /* ====================================================================
3 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
55 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
56 * All rights reserved.
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.
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).
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.
76 * Redistribution and use in source and binary forms, with or without
77 * modification, are permitted provided that the following conditions
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)"
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
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.]
112 #ifndef OPENSSL_NO_ECDSA
118 #include <openssl/bio.h>
119 #include <openssl/err.h>
120 #include <openssl/ecdsa.h>
121 #include <openssl/evp.h>
122 #include <openssl/x509.h>
123 #include <openssl/pem.h>
126 #define PROG ecdsa_main
128 /* -inform arg - input format - default PEM (one of DER, NET or PEM)
129 * -outform arg - output format - default PEM
130 * -in arg - input file - default stdin
131 * -out arg - output file - default stdout
132 * -des - encrypt output if PEM format with DES in cbc mode
133 * -des3 - encrypt output if PEM format
134 * -idea - encrypt output if PEM format
135 * -aes128 - encrypt output if PEM format
136 * -aes192 - encrypt output if PEM format
137 * -aes256 - encrypt output if PEM format
138 * -text - print a text version
139 * -pub - print the ECDSA public key
140 * -compressed - print the public key in compressed form ( default )
141 * -hybrid - print the public key in hybrid form
142 * -uncompressed - print the public key in uncompressed form
143 * the last three options ( compressed, hybrid and uncompressed )
144 * are only used if the "-pub" option is also selected.
145 * For a precise description of the the meaning of compressed,
146 * hybrid and uncompressed please refer to the X9.62 standart.
147 * All three forms represents ways to express the ecdsa public
148 * key ( a point on a elliptic curve ) as octet string. Let len be
149 * the length ( in bytes ) of an element of the field over which
150 * the curve is defined, then a compressed octet string has the form
151 * 0x02 + result of BN_bn2bin() of the x coordinate of the public key
154 int MAIN(int, char **);
156 int MAIN(int argc, char **argv)
162 const EVP_CIPHER *enc = NULL;
163 BIO *in = NULL, *out = NULL;
164 int informat, outformat, text=0, noout=0;
165 int pubin = 0, pubout = 0;
166 char *infile, *outfile, *prog, *engine;
167 char *passargin = NULL, *passargout = NULL;
168 char *passin = NULL, *passout = NULL;
169 int pub = 0, point_form = 0;
170 unsigned char *buffer = NULL;
171 unsigned int buf_len = 0;
172 BIGNUM *tmp_bn = NULL;
177 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
178 BIO_set_fp(bio_err, stderr, BIO_NOCLOSE|BIO_FP_TEXT);
180 if (!load_config(bio_err, NULL))
186 informat = FORMAT_PEM;
187 outformat = FORMAT_PEM;
194 if (strcmp(*argv,"-inform") == 0)
196 if (--argc < 1) goto bad;
197 informat=str2fmt(*(++argv));
199 else if (strcmp(*argv,"-outform") == 0)
201 if (--argc < 1) goto bad;
202 outformat=str2fmt(*(++argv));
204 else if (strcmp(*argv,"-in") == 0)
206 if (--argc < 1) goto bad;
209 else if (strcmp(*argv,"-out") == 0)
211 if (--argc < 1) goto bad;
214 else if (strcmp(*argv,"-passin") == 0)
216 if (--argc < 1) goto bad;
217 passargin= *(++argv);
219 else if (strcmp(*argv,"-passout") == 0)
221 if (--argc < 1) goto bad;
222 passargout= *(++argv);
224 else if (strcmp(*argv, "-engine") == 0)
226 if (--argc < 1) goto bad;
229 else if (strcmp(*argv, "-noout") == 0)
231 else if (strcmp(*argv, "-text") == 0)
233 else if (strcmp(*argv, "-pub") == 0)
236 buffer = (unsigned char *)(*(argv+1));
237 if (strcmp((char *)buffer, "compressed") == 0)
238 point_form = POINT_CONVERSION_COMPRESSED;
239 else if (strcmp((char *)buffer, "hybrid") == 0)
240 point_form = POINT_CONVERSION_HYBRID;
241 else if (strcmp((char *)buffer, "uncompressed") == 0)
242 point_form = POINT_CONVERSION_UNCOMPRESSED;
249 else if (strcmp(*argv, "-pubin") == 0)
251 else if (strcmp(*argv, "-pubout") == 0)
253 else if ((enc=EVP_get_cipherbyname(&(argv[0][1]))) == NULL)
255 BIO_printf(bio_err,"unknown option %s\n",*argv);
266 BIO_printf(bio_err, "%s [options] <infile >outfile\n",prog);
267 BIO_printf(bio_err, "where options are\n");
268 BIO_printf(bio_err, " -inform arg input format - DER or PEM\n");
269 BIO_printf(bio_err, " -outform arg output format - DER or PEM\n");
270 BIO_printf(bio_err, " -in arg input file\n");
271 BIO_printf(bio_err, " -passin arg input file pass phrase source\n");
272 BIO_printf(bio_err, " -out arg output file\n");
273 BIO_printf(bio_err, " -passout arg output file pass phrase source\n");
274 BIO_printf(bio_err, " -engine e use engine e, possibly a hardware device.\n");
275 BIO_printf(bio_err, " -des encrypt PEM output with cbc des\n");
276 BIO_printf(bio_err, " -des3 encrypt PEM output with ede cbc des using 168 bit key\n");
277 #ifndef OPENSSL_NO_IDEA
278 BIO_printf(bio_err, " -idea encrypt PEM output with cbc idea\n");
280 #ifndef OPENSSL_NO_AES
281 BIO_printf(bio_err, " -aes128, -aes192, -aes256\n");
282 BIO_printf(bio_err, " encrypt PEM output with cbc aes\n");
284 BIO_printf(bio_err, " -text print the key in text\n");
285 BIO_printf(bio_err, " -noout don't print key out\n");
286 BIO_printf(bio_err, " -pub [compressed | hybrid | uncompressed] \n");
287 BIO_printf(bio_err, " compressed print the public key in compressed form ( default )\n");
288 BIO_printf(bio_err, " hybrid print the public key in hybrid form\n");
289 BIO_printf(bio_err, " uncompressed print the public key in uncompressed form\n");
293 ERR_load_crypto_strings();
295 e = setup_engine(bio_err, engine, 0);
297 if(!app_passwd(bio_err, passargin, passargout, &passin, &passout))
299 BIO_printf(bio_err, "Error getting passwords\n");
303 in = BIO_new(BIO_s_file());
304 out = BIO_new(BIO_s_file());
305 if ((in == NULL) || (out == NULL))
307 ERR_print_errors(bio_err);
312 BIO_set_fp(in,stdin,BIO_NOCLOSE);
315 if (BIO_read_filename(in,infile) <= 0)
322 BIO_printf(bio_err,"read ECDSA key\n");
323 if (informat == FORMAT_ASN1)
326 ecdsa = d2i_ECDSA_PUBKEY_bio(in, NULL);
328 ecdsa = d2i_ECDSAPrivateKey_bio(in, NULL);
329 } else if (informat == FORMAT_PEM)
332 ecdsa = PEM_read_bio_ECDSA_PUBKEY(in, NULL, NULL, NULL);
334 ecdsa = PEM_read_bio_ECDSAPrivateKey(in, NULL, NULL, passin);
337 BIO_printf(bio_err, "bad input format specified for key\n");
342 BIO_printf(bio_err,"unable to load Key\n");
343 ERR_print_errors(bio_err);
349 BIO_set_fp(out, stdout, BIO_NOCLOSE);
350 #ifdef OPENSSL_SYS_VMS
352 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
353 out = BIO_push(tmpbio, out);
359 if (BIO_write_filename(out, outfile) <= 0)
367 if (!ECDSA_print(out, ecdsa, 0))
370 ERR_print_errors(bio_err);
376 fprintf(stdout, "Public Key (");
377 if (point_form == POINT_CONVERSION_COMPRESSED)
378 fprintf(stdout, "COMPRESSED");
379 else if (point_form == POINT_CONVERSION_UNCOMPRESSED)
380 fprintf(stdout, "UNCOMPRESSED");
381 else if (point_form == POINT_CONVERSION_HYBRID)
382 fprintf(stdout, "HYBRID");
383 fprintf(stdout, ")=");
384 buf_len = EC_POINT_point2oct(ecdsa->group, EC_GROUP_get0_generator(ecdsa->group),
385 point_form, NULL, 0, NULL);
388 BIO_printf(bio_err,"invalid public key length\n");
389 ERR_print_errors(bio_err);
392 if ((tmp_bn = BN_new()) == NULL ||
393 (buffer = OPENSSL_malloc(buf_len)) == NULL) goto end;
394 if (!EC_POINT_point2oct(ecdsa->group, EC_GROUP_get0_generator(ecdsa->group),
395 point_form, buffer, buf_len, NULL) ||
396 !BN_bin2bn(buffer, buf_len, tmp_bn))
398 BIO_printf(bio_err,"can not encode public key\n");
399 ERR_print_errors(bio_err);
400 OPENSSL_free(buffer);
403 BN_print(out, tmp_bn);
404 fprintf(stdout,"\n");
409 BIO_printf(bio_err, "writing ECDSA key\n");
410 if (outformat == FORMAT_ASN1)
413 i = i2d_ECDSA_PUBKEY_bio(out, ecdsa);
415 i = i2d_ECDSAPrivateKey_bio(out, ecdsa);
416 } else if (outformat == FORMAT_PEM)
419 i = PEM_write_bio_ECDSA_PUBKEY(out, ecdsa);
421 i = PEM_write_bio_ECDSAPrivateKey(out, ecdsa, enc,
422 NULL, 0, NULL, passout);
425 BIO_printf(bio_err, "bad output format specified for outfile\n");
430 BIO_printf(bio_err, "unable to write private key\n");
431 ERR_print_errors(bio_err);
436 if (in) BIO_free(in);
437 if (out) BIO_free_all(out);
438 if (ecdsa) ECDSA_free(ecdsa);
439 if (tmp_bn) BN_free(tmp_bn);
440 if (passin) OPENSSL_free(passin);
441 if (passout) OPENSSL_free(passout);