2 * Copyright 2002-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 #include <openssl/opensslconf.h>
12 #include <openssl/evp.h>
13 #include <openssl/encoder.h>
14 #include <openssl/decoder.h>
15 #include <openssl/core_names.h>
16 #include <openssl/core_dispatch.h>
17 #include <openssl/params.h>
18 #include <openssl/err.h>
22 #include "ec_common.h"
24 typedef enum OPTION_choice {
26 OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
27 OPT_NOOUT, OPT_TEXT, OPT_PARAM_OUT, OPT_PUBIN, OPT_PUBOUT,
28 OPT_PASSIN, OPT_PASSOUT, OPT_PARAM_ENC, OPT_CONV_FORM, OPT_CIPHER,
29 OPT_NO_PUBLIC, OPT_CHECK, OPT_PROV_ENUM
32 const OPTIONS ec_options[] = {
33 OPT_SECTION("General"),
34 {"help", OPT_HELP, '-', "Display this summary"},
35 #ifndef OPENSSL_NO_ENGINE
36 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
40 {"in", OPT_IN, 's', "Input file"},
41 {"inform", OPT_INFORM, 'f', "Input format (DER/PEM/P12/ENGINE)"},
42 {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
43 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
44 {"check", OPT_CHECK, '-', "check key consistency"},
45 {"", OPT_CIPHER, '-', "Any supported cipher"},
46 {"param_enc", OPT_PARAM_ENC, 's',
47 "Specifies the way the ec parameters are encoded"},
48 {"conv_form", OPT_CONV_FORM, 's', "Specifies the point conversion form "},
50 OPT_SECTION("Output"),
51 {"out", OPT_OUT, '>', "Output file"},
52 {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
53 {"noout", OPT_NOOUT, '-', "Don't print key out"},
54 {"text", OPT_TEXT, '-', "Print the key"},
55 {"param_out", OPT_PARAM_OUT, '-', "Print the elliptic curve parameters"},
56 {"pubout", OPT_PUBOUT, '-', "Output public key, not private"},
57 {"no_public", OPT_NO_PUBLIC, '-', "exclude public key from private key"},
58 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
64 int ec_main(int argc, char **argv)
66 OSSL_ENCODER_CTX *ectx = NULL;
67 OSSL_DECODER_CTX *dctx = NULL;
68 EVP_PKEY_CTX *pctx = NULL;
69 EVP_PKEY *eckey = NULL;
72 EVP_CIPHER *enc = NULL;
73 char *infile = NULL, *outfile = NULL, *ciphername = NULL, *prog;
74 char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
76 int informat = FORMAT_UNDEF, outformat = FORMAT_PEM, text = 0, noout = 0;
77 int pubin = 0, pubout = 0, param_out = 0, ret = 1, private = 0;
79 char *asn1_encoding = NULL;
80 char *point_format = NULL;
83 prog = opt_init(argc, argv, ec_options);
84 while ((o = opt_next()) != OPT_EOF) {
89 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
96 if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
103 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
125 passinarg = opt_arg();
128 passoutarg = opt_arg();
131 e = setup_engine(opt_arg(), 0);
134 ciphername = opt_unknown();
137 point_format = opt_arg();
138 if (!opt_string(point_format, point_format_options))
142 asn1_encoding = opt_arg();
143 if (!opt_string(asn1_encoding, asn1_encoding_options))
153 if (!opt_provider(o))
159 /* No extra arguments. */
160 if (!opt_check_rest_arg(NULL))
163 if (!opt_cipher(ciphername, &enc))
165 private = param_out || pubin || pubout ? 0 : 1;
169 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
170 BIO_printf(bio_err, "Error getting passwords\n");
174 BIO_printf(bio_err, "read EC key\n");
177 eckey = load_pubkey(infile, informat, 1, passin, e, "public key");
179 eckey = load_key(infile, informat, 1, passin, e, "private key");
182 BIO_printf(bio_err, "unable to load Key\n");
186 out = bio_open_owner(outfile, outformat, private);
191 && !EVP_PKEY_set_utf8_string_param(
192 eckey, OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
194 BIO_printf(bio_err, "unable to set point conversion format\n");
198 if (asn1_encoding != NULL
199 && !EVP_PKEY_set_utf8_string_param(
200 eckey, OSSL_PKEY_PARAM_EC_ENCODING, asn1_encoding)) {
201 BIO_printf(bio_err, "unable to set asn1 encoding format\n");
206 if (!EVP_PKEY_set_int_param(eckey, OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, 0)) {
207 BIO_printf(bio_err, "unable to disable public key encoding\n");
211 if (!EVP_PKEY_set_int_param(eckey, OSSL_PKEY_PARAM_EC_INCLUDE_PUBLIC, 1)) {
212 BIO_printf(bio_err, "unable to enable public key encoding\n");
218 assert(pubin || private);
219 if ((pubin && EVP_PKEY_print_public(out, eckey, 0, NULL) <= 0)
220 || (!pubin && EVP_PKEY_print_private(out, eckey, 0, NULL) <= 0)) {
221 BIO_printf(bio_err, "unable to print EC key\n");
227 pctx = EVP_PKEY_CTX_new_from_pkey(NULL, eckey, NULL);
229 BIO_printf(bio_err, "unable to check EC key\n");
232 if (!EVP_PKEY_check(pctx))
233 BIO_printf(bio_err, "EC Key Invalid!\n");
235 BIO_printf(bio_err, "EC Key valid.\n");
236 ERR_print_errors(bio_err);
241 const char *output_type = outformat == FORMAT_ASN1 ? "DER" : "PEM";
242 const char *output_structure = "type-specific";
244 BIO_printf(bio_err, "writing EC key\n");
246 selection = OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
247 } else if (pubin || pubout) {
248 selection = OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
249 | OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
250 output_structure = "SubjectPublicKeyInfo";
252 selection = OSSL_KEYMGMT_SELECT_ALL;
256 ectx = OSSL_ENCODER_CTX_new_for_pkey(eckey, selection,
257 output_type, output_structure,
260 OSSL_ENCODER_CTX_set_cipher(ectx, EVP_CIPHER_get0_name(enc), NULL);
261 /* Default passphrase prompter */
262 OSSL_ENCODER_CTX_set_passphrase_ui(ectx, get_ui_method(), NULL);
264 /* When passout given, override the passphrase prompter */
265 OSSL_ENCODER_CTX_set_passphrase(ectx,
266 (const unsigned char *)passout,
269 if (!OSSL_ENCODER_to_bio(ectx, out)) {
270 BIO_printf(bio_err, "unable to write EC key\n");
278 ERR_print_errors(bio_err);
280 EVP_PKEY_free(eckey);
281 EVP_CIPHER_free(enc);
282 OSSL_ENCODER_CTX_free(ectx);
283 OSSL_DECODER_CTX_free(dctx);
284 EVP_PKEY_CTX_free(pctx);
287 OPENSSL_clear_free(passin, strlen(passin));
289 OPENSSL_clear_free(passout, strlen(passout));