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).
56 #ifndef OPENSSL_NO_ECDSA
61 #include <openssl/bio.h>
62 #include <openssl/err.h>
63 #include <openssl/evp.h>
64 #include <openssl/ecdsa.h>
65 #include <openssl/x509.h>
66 #include <openssl/pem.h>
69 #define PROG ecdsa_main
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
82 int MAIN(int, char **);
84 int MAIN(int argc
, char **argv
)
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
;
99 int asn1_flag
= OPENSSL_EC_NAMED_CURVE
;
100 int new_asn1_flag
= 0;
105 if ((bio_err
=BIO_new(BIO_s_file())) != NULL
)
106 BIO_set_fp(bio_err
, stderr
, BIO_NOCLOSE
|BIO_FP_TEXT
);
108 if (!load_config(bio_err
, NULL
))
114 informat
= FORMAT_PEM
;
115 outformat
= FORMAT_PEM
;
122 if (strcmp(*argv
,"-inform") == 0)
124 if (--argc
< 1) goto bad
;
125 informat
=str2fmt(*(++argv
));
127 else if (strcmp(*argv
,"-outform") == 0)
129 if (--argc
< 1) goto bad
;
130 outformat
=str2fmt(*(++argv
));
132 else if (strcmp(*argv
,"-in") == 0)
134 if (--argc
< 1) goto bad
;
137 else if (strcmp(*argv
,"-out") == 0)
139 if (--argc
< 1) goto bad
;
142 else if (strcmp(*argv
,"-passin") == 0)
144 if (--argc
< 1) goto bad
;
145 passargin
= *(++argv
);
147 else if (strcmp(*argv
,"-passout") == 0)
149 if (--argc
< 1) goto bad
;
150 passargout
= *(++argv
);
152 else if (strcmp(*argv
, "-engine") == 0)
154 if (--argc
< 1) goto bad
;
157 else if (strcmp(*argv
, "-noout") == 0)
159 else if (strcmp(*argv
, "-text") == 0)
161 else if (strcmp(*argv
, "-conv_form") == 0)
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
;
176 else if (strcmp(*argv
, "-param_enc") == 0)
182 if (strcmp(*argv
, "named_curve") == 0)
183 asn1_flag
= OPENSSL_EC_NAMED_CURVE
;
184 else if (strcmp(*argv
, "explicit") == 0)
189 else if (strcmp(*argv
, "-param_out") == 0)
191 else if (strcmp(*argv
, "-pubin") == 0)
193 else if (strcmp(*argv
, "-pubout") == 0)
195 else if ((enc
=EVP_get_cipherbyname(&(argv
[0][1]))) == NULL
)
197 BIO_printf(bio_err
, "unknown option %s\n", *argv
);
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 - "
212 BIO_printf(bio_err
, " -outform arg output format - "
214 BIO_printf(bio_err
, " -in arg input file\n");
215 BIO_printf(bio_err
, " -passin arg input file pass "
217 BIO_printf(bio_err
, " -out arg output file\n");
218 BIO_printf(bio_err
, " -passout arg output file pass "
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"
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 :"
234 BIO_printf(bio_err
, " "
235 " uncompressed (default)\n");
236 BIO_printf(bio_err
, " "
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 "
242 BIO_printf(bio_err
, " possilbe values :"
243 " named_curve (default)\n");
244 BIO_printf(bio_err
," "
249 ERR_load_crypto_strings();
251 e
= setup_engine(bio_err
, engine
, 0);
253 if(!app_passwd(bio_err
, passargin
, passargout
, &passin
, &passout
))
255 BIO_printf(bio_err
, "Error getting passwords\n");
259 in
= BIO_new(BIO_s_file());
260 out
= BIO_new(BIO_s_file());
261 if ((in
== NULL
) || (out
== NULL
))
263 ERR_print_errors(bio_err
);
268 BIO_set_fp(in
, stdin
, BIO_NOCLOSE
);
271 if (BIO_read_filename(in
, infile
) <= 0)
278 BIO_printf(bio_err
, "read ECDSA key\n");
279 if (informat
== FORMAT_ASN1
)
282 eckey
= d2i_EC_PUBKEY_bio(in
, NULL
);
284 eckey
= d2i_ECPrivateKey_bio(in
, NULL
);
286 else if (informat
== FORMAT_PEM
)
289 eckey
= PEM_read_bio_EC_PUBKEY(in
, NULL
, NULL
,
292 eckey
= PEM_read_bio_ECPrivateKey(in
, NULL
, NULL
,
297 BIO_printf(bio_err
, "bad input format specified for key\n");
302 BIO_printf(bio_err
,"unable to load Key\n");
303 ERR_print_errors(bio_err
);
309 BIO_set_fp(out
, stdout
, BIO_NOCLOSE
);
310 #ifdef OPENSSL_SYS_VMS
312 BIO
*tmpbio
= BIO_new(BIO_f_linebuffer());
313 out
= BIO_push(tmpbio
, out
);
319 if (BIO_write_filename(out
, outfile
) <= 0)
328 EC_GROUP_set_point_conversion_form(eckey
->group
, form
);
329 eckey
->conv_form
= form
;
333 EC_GROUP_set_asn1_flag(eckey
->group
, asn1_flag
);
336 if (!EC_KEY_print(out
, eckey
, 0))
339 ERR_print_errors(bio_err
);
346 BIO_printf(bio_err
, "writing EC key\n");
347 if (outformat
== FORMAT_ASN1
)
350 i
= i2d_ECPKParameters_bio(out
, eckey
->group
);
351 else if (pubin
|| pubout
)
352 i
= i2d_EC_PUBKEY_bio(out
, eckey
);
354 i
= i2d_ECPrivateKey_bio(out
, eckey
);
356 else if (outformat
== FORMAT_PEM
)
359 i
= PEM_write_bio_ECPKParameters(out
, eckey
->group
);
360 else if (pubin
|| pubout
)
361 i
= PEM_write_bio_EC_PUBKEY(out
, eckey
);
363 i
= PEM_write_bio_ECPrivateKey(out
, eckey
, enc
,
364 NULL
, 0, NULL
, passout
);
368 BIO_printf(bio_err
, "bad output format specified for "
375 BIO_printf(bio_err
, "unable to write private key\n");
376 ERR_print_errors(bio_err
);
388 OPENSSL_free(passin
);
390 OPENSSL_free(passout
);