2 * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/opensslconf.h>
12 NON_EMPTY_TRANSLATION_UNIT
19 # include <openssl/bio.h>
20 # include <openssl/err.h>
21 # include <openssl/evp.h>
22 # include <openssl/pem.h>
24 static OPT_PAIR conv_forms
[] = {
25 {"compressed", POINT_CONVERSION_COMPRESSED
},
26 {"uncompressed", POINT_CONVERSION_UNCOMPRESSED
},
27 {"hybrid", POINT_CONVERSION_HYBRID
},
31 static OPT_PAIR param_enc
[] = {
32 {"named_curve", OPENSSL_EC_NAMED_CURVE
},
37 typedef enum OPTION_choice
{
38 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
39 OPT_INFORM
, OPT_OUTFORM
, OPT_ENGINE
, OPT_IN
, OPT_OUT
,
40 OPT_NOOUT
, OPT_TEXT
, OPT_PARAM_OUT
, OPT_PUBIN
, OPT_PUBOUT
,
41 OPT_PASSIN
, OPT_PASSOUT
, OPT_PARAM_ENC
, OPT_CONV_FORM
, OPT_CIPHER
,
42 OPT_NO_PUBLIC
, OPT_CHECK
45 const OPTIONS ec_options
[] = {
46 {"help", OPT_HELP
, '-', "Display this summary"},
47 {"in", OPT_IN
, 's', "Input file"},
48 {"inform", OPT_INFORM
, 'f', "Input format - DER or PEM"},
49 {"out", OPT_OUT
, '>', "Output file"},
50 {"outform", OPT_OUTFORM
, 'F', "Output format - DER or PEM"},
51 {"noout", OPT_NOOUT
, '-', "Don't print key out"},
52 {"text", OPT_TEXT
, '-', "Print the key"},
53 {"param_out", OPT_PARAM_OUT
, '-', "Print the elliptic curve parameters"},
54 {"pubin", OPT_PUBIN
, '-', "Expect a public key in input file"},
55 {"pubout", OPT_PUBOUT
, '-', "Output public key, not private"},
56 {"no_public", OPT_NO_PUBLIC
, '-', "exclude public key from private key"},
57 {"check", OPT_CHECK
, '-', "check key consistency"},
58 {"passin", OPT_PASSIN
, 's', "Input file pass phrase source"},
59 {"passout", OPT_PASSOUT
, 's', "Output file pass phrase source"},
60 {"param_enc", OPT_PARAM_ENC
, 's',
61 "Specifies the way the ec parameters are encoded"},
62 {"conv_form", OPT_CONV_FORM
, 's', "Specifies the point conversion form "},
63 {"", OPT_CIPHER
, '-', "Any supported cipher"},
64 # ifndef OPENSSL_NO_ENGINE
65 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
70 int ec_main(int argc
, char **argv
)
72 BIO
*in
= NULL
, *out
= NULL
;
75 const EC_GROUP
*group
;
76 const EVP_CIPHER
*enc
= NULL
;
77 point_conversion_form_t form
= POINT_CONVERSION_UNCOMPRESSED
;
78 char *infile
= NULL
, *outfile
= NULL
, *prog
;
79 char *passin
= NULL
, *passout
= NULL
, *passinarg
= NULL
, *passoutarg
= NULL
;
81 int asn1_flag
= OPENSSL_EC_NAMED_CURVE
, new_form
= 0, new_asn1_flag
= 0;
82 int informat
= FORMAT_PEM
, outformat
= FORMAT_PEM
, text
= 0, noout
= 0;
83 int pubin
= 0, pubout
= 0, param_out
= 0, i
, ret
= 1, private = 0;
84 int no_public
= 0, check
= 0;
86 prog
= opt_init(argc
, argv
, ec_options
);
87 while ((o
= opt_next()) != OPT_EOF
) {
92 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
99 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &informat
))
106 if (!opt_format(opt_arg(), OPT_FMT_PEMDER
, &outformat
))
128 passinarg
= opt_arg();
131 passoutarg
= opt_arg();
134 e
= setup_engine(opt_arg(), 0);
137 if (!opt_cipher(opt_unknown(), &enc
))
141 if (!opt_pair(opt_arg(), conv_forms
, &i
))
147 if (!opt_pair(opt_arg(), param_enc
, &i
))
160 argc
= opt_num_rest();
164 private = param_out
|| pubin
|| pubout
? 0 : 1;
168 if (!app_passwd(passinarg
, passoutarg
, &passin
, &passout
)) {
169 BIO_printf(bio_err
, "Error getting passwords\n");
173 if (informat
!= FORMAT_ENGINE
) {
174 in
= bio_open_default(infile
, 'r', informat
);
179 BIO_printf(bio_err
, "read EC key\n");
180 if (informat
== FORMAT_ASN1
) {
182 eckey
= d2i_EC_PUBKEY_bio(in
, NULL
);
184 eckey
= d2i_ECPrivateKey_bio(in
, NULL
);
185 } else if (informat
== FORMAT_ENGINE
) {
188 pkey
= load_pubkey(infile
, informat
, 1, passin
, e
, "Public Key");
190 pkey
= load_key(infile
, informat
, 1, passin
, e
, "Private Key");
192 eckey
= EVP_PKEY_get1_EC_KEY(pkey
);
197 eckey
= PEM_read_bio_EC_PUBKEY(in
, NULL
, NULL
, NULL
);
199 eckey
= PEM_read_bio_ECPrivateKey(in
, NULL
, NULL
, passin
);
202 BIO_printf(bio_err
, "unable to load Key\n");
203 ERR_print_errors(bio_err
);
207 out
= bio_open_owner(outfile
, outformat
, private);
211 group
= EC_KEY_get0_group(eckey
);
214 EC_KEY_set_conv_form(eckey
, form
);
217 EC_KEY_set_asn1_flag(eckey
, asn1_flag
);
220 EC_KEY_set_enc_flags(eckey
, EC_PKEY_NO_PUBKEY
);
223 assert(pubin
|| private);
224 if (!EC_KEY_print(out
, eckey
, 0)) {
226 ERR_print_errors(bio_err
);
232 if (EC_KEY_check_key(eckey
) == 1) {
233 BIO_printf(bio_err
, "EC Key valid.\n");
235 BIO_printf(bio_err
, "EC Key Invalid!\n");
236 ERR_print_errors(bio_err
);
245 BIO_printf(bio_err
, "writing EC key\n");
246 if (outformat
== FORMAT_ASN1
) {
248 i
= i2d_ECPKParameters_bio(out
, group
);
249 } else if (pubin
|| pubout
) {
250 i
= i2d_EC_PUBKEY_bio(out
, eckey
);
253 i
= i2d_ECPrivateKey_bio(out
, eckey
);
257 i
= PEM_write_bio_ECPKParameters(out
, group
);
258 } else if (pubin
|| pubout
) {
259 i
= PEM_write_bio_EC_PUBKEY(out
, eckey
);
262 i
= PEM_write_bio_ECPrivateKey(out
, eckey
, enc
,
263 NULL
, 0, NULL
, passout
);
268 BIO_printf(bio_err
, "unable to write private key\n");
269 ERR_print_errors(bio_err
);
278 OPENSSL_free(passin
);
279 OPENSSL_free(passout
);