3 * Written by Nils Larsch for the OpenSSL project.
5 /* ====================================================================
6 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
59 #include <openssl/opensslconf.h>
65 #include <openssl/bio.h>
66 #include <openssl/err.h>
67 #include <openssl/evp.h>
68 #include <openssl/pem.h>
74 * -inform arg - input format - default PEM (one of DER, NET or PEM)
75 * -outform arg - output format - default PEM
76 * -in arg - input file - default stdin
77 * -out arg - output file - default stdout
78 * -des - encrypt output if PEM format with DES in cbc mode
79 * -text - print a text version
80 * -param_out - print the elliptic curve parameters
81 * -conv_form arg - specifies the point encoding form
82 * -param_enc arg - specifies the parameter encoding
85 int MAIN(int, char **);
87 int MAIN(int argc
, char **argv
)
91 const EC_GROUP
*group
;
93 const EVP_CIPHER
*enc
= NULL
;
94 BIO
*in
= NULL
, *out
= NULL
;
95 int informat
, outformat
, text
=0, noout
=0;
96 int pubin
= 0, pubout
= 0, param_out
= 0;
97 char *infile
, *outfile
, *prog
, *engine
;
98 char *passargin
= NULL
, *passargout
= NULL
;
99 char *passin
= NULL
, *passout
= NULL
;
100 point_conversion_form_t form
= POINT_CONVERSION_UNCOMPRESSED
;
102 int asn1_flag
= OPENSSL_EC_NAMED_CURVE
;
103 int new_asn1_flag
= 0;
108 if ((bio_err
=BIO_new(BIO_s_file())) != NULL
)
109 BIO_set_fp(bio_err
, stderr
, BIO_NOCLOSE
|BIO_FP_TEXT
);
111 if (!load_config(bio_err
, NULL
))
117 informat
= FORMAT_PEM
;
118 outformat
= FORMAT_PEM
;
125 if (strcmp(*argv
,"-inform") == 0)
127 if (--argc
< 1) goto bad
;
128 informat
=str2fmt(*(++argv
));
130 else if (strcmp(*argv
,"-outform") == 0)
132 if (--argc
< 1) goto bad
;
133 outformat
=str2fmt(*(++argv
));
135 else if (strcmp(*argv
,"-in") == 0)
137 if (--argc
< 1) goto bad
;
140 else if (strcmp(*argv
,"-out") == 0)
142 if (--argc
< 1) goto bad
;
145 else if (strcmp(*argv
,"-passin") == 0)
147 if (--argc
< 1) goto bad
;
148 passargin
= *(++argv
);
150 else if (strcmp(*argv
,"-passout") == 0)
152 if (--argc
< 1) goto bad
;
153 passargout
= *(++argv
);
155 else if (strcmp(*argv
, "-engine") == 0)
157 if (--argc
< 1) goto bad
;
160 else if (strcmp(*argv
, "-noout") == 0)
162 else if (strcmp(*argv
, "-text") == 0)
164 else if (strcmp(*argv
, "-conv_form") == 0)
170 if (strcmp(*argv
, "compressed") == 0)
171 form
= POINT_CONVERSION_COMPRESSED
;
172 else if (strcmp(*argv
, "uncompressed") == 0)
173 form
= POINT_CONVERSION_UNCOMPRESSED
;
174 else if (strcmp(*argv
, "hybrid") == 0)
175 form
= POINT_CONVERSION_HYBRID
;
179 else if (strcmp(*argv
, "-param_enc") == 0)
185 if (strcmp(*argv
, "named_curve") == 0)
186 asn1_flag
= OPENSSL_EC_NAMED_CURVE
;
187 else if (strcmp(*argv
, "explicit") == 0)
192 else if (strcmp(*argv
, "-param_out") == 0)
194 else if (strcmp(*argv
, "-pubin") == 0)
196 else if (strcmp(*argv
, "-pubout") == 0)
198 else if ((enc
=EVP_get_cipherbyname(&(argv
[0][1]))) == NULL
)
200 BIO_printf(bio_err
, "unknown option %s\n", *argv
);
211 BIO_printf(bio_err
, "%s [options] <infile >outfile\n", prog
);
212 BIO_printf(bio_err
, "where options are\n");
213 BIO_printf(bio_err
, " -inform arg input format - "
215 BIO_printf(bio_err
, " -outform arg output format - "
217 BIO_printf(bio_err
, " -in arg input file\n");
218 BIO_printf(bio_err
, " -passin arg input file pass "
220 BIO_printf(bio_err
, " -out arg output file\n");
221 BIO_printf(bio_err
, " -passout arg output file pass "
223 BIO_printf(bio_err
, " -engine e use engine e, "
224 "possibly a hardware device.\n");
225 BIO_printf(bio_err
, " -des encrypt PEM output, "
226 "instead of 'des' every other \n"
228 "supported by OpenSSL can be used\n");
229 BIO_printf(bio_err
, " -text print the key\n");
230 BIO_printf(bio_err
, " -noout don't print key out\n");
231 BIO_printf(bio_err
, " -param_out print the elliptic "
232 "curve parameters\n");
233 BIO_printf(bio_err
, " -conv_form arg specifies the "
234 "point conversion form \n");
235 BIO_printf(bio_err
, " possible values:"
237 BIO_printf(bio_err
, " "
238 " uncompressed (default)\n");
239 BIO_printf(bio_err
, " "
241 BIO_printf(bio_err
, " -param_enc arg specifies the way"
242 " the ec parameters are encoded\n");
243 BIO_printf(bio_err
, " in the asn1 der "
245 BIO_printf(bio_err
, " possible values:"
246 " named_curve (default)\n");
247 BIO_printf(bio_err
," "
252 ERR_load_crypto_strings();
254 #ifndef OPENSSL_NO_ENGINE
255 setup_engine(bio_err
, engine
, 0);
258 if(!app_passwd(bio_err
, passargin
, passargout
, &passin
, &passout
))
260 BIO_printf(bio_err
, "Error getting passwords\n");
264 in
= BIO_new(BIO_s_file());
265 out
= BIO_new(BIO_s_file());
266 if ((in
== NULL
) || (out
== NULL
))
268 ERR_print_errors(bio_err
);
273 BIO_set_fp(in
, stdin
, BIO_NOCLOSE
);
276 if (BIO_read_filename(in
, infile
) <= 0)
283 BIO_printf(bio_err
, "read EC key\n");
284 if (informat
== FORMAT_ASN1
)
287 eckey
= d2i_EC_PUBKEY_bio(in
, NULL
);
289 eckey
= d2i_ECPrivateKey_bio(in
, NULL
);
291 else if (informat
== FORMAT_PEM
)
294 eckey
= PEM_read_bio_EC_PUBKEY(in
, NULL
, NULL
,
297 eckey
= PEM_read_bio_ECPrivateKey(in
, NULL
, NULL
,
302 BIO_printf(bio_err
, "bad input format specified for key\n");
307 BIO_printf(bio_err
,"unable to load Key\n");
308 ERR_print_errors(bio_err
);
314 BIO_set_fp(out
, stdout
, BIO_NOCLOSE
);
315 #ifdef OPENSSL_SYS_VMS
317 BIO
*tmpbio
= BIO_new(BIO_f_linebuffer());
318 out
= BIO_push(tmpbio
, out
);
324 if (BIO_write_filename(out
, outfile
) <= 0)
331 group
= EC_KEY_get0_group(eckey
);
334 EC_KEY_set_conv_form(eckey
, form
);
337 EC_KEY_set_asn1_flag(eckey
, asn1_flag
);
340 if (!EC_KEY_print(out
, eckey
, 0))
343 ERR_print_errors(bio_err
);
353 BIO_printf(bio_err
, "writing EC key\n");
354 if (outformat
== FORMAT_ASN1
)
357 i
= i2d_ECPKParameters_bio(out
, group
);
358 else if (pubin
|| pubout
)
359 i
= i2d_EC_PUBKEY_bio(out
, eckey
);
361 i
= i2d_ECPrivateKey_bio(out
, eckey
);
363 else if (outformat
== FORMAT_PEM
)
366 i
= PEM_write_bio_ECPKParameters(out
, group
);
367 else if (pubin
|| pubout
)
368 i
= PEM_write_bio_EC_PUBKEY(out
, eckey
);
370 i
= PEM_write_bio_ECPrivateKey(out
, eckey
, enc
,
371 NULL
, 0, NULL
, passout
);
375 BIO_printf(bio_err
, "bad output format specified for "
382 BIO_printf(bio_err
, "unable to write private key\n");
383 ERR_print_errors(bio_err
);
395 OPENSSL_free(passin
);
397 OPENSSL_free(passout
);
401 #else /* !OPENSSL_NO_EC */
404 static void *dummy
=&dummy
;