2 * Copyright 2006-2018 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
14 #include <openssl/pem.h>
15 #include <openssl/err.h>
16 #include <openssl/evp.h>
19 # include <openssl/ec.h>
21 static OPT_PAIR ec_conv_forms[] = {
22 {"compressed", POINT_CONVERSION_COMPRESSED},
23 {"uncompressed", POINT_CONVERSION_UNCOMPRESSED},
24 {"hybrid", POINT_CONVERSION_HYBRID},
28 static OPT_PAIR ec_param_enc[] = {
29 {"named_curve", OPENSSL_EC_NAMED_CURVE},
35 typedef enum OPTION_choice {
36 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
37 OPT_INFORM, OPT_OUTFORM, OPT_PASSIN, OPT_PASSOUT, OPT_ENGINE,
38 OPT_IN, OPT_OUT, OPT_PUBIN, OPT_PUBOUT, OPT_TEXT_PUB,
39 OPT_TEXT, OPT_NOOUT, OPT_MD, OPT_TRADITIONAL, OPT_CHECK, OPT_PUB_CHECK,
40 OPT_EC_PARAM_ENC, OPT_EC_CONV_FORM
43 const OPTIONS pkey_options[] = {
44 OPT_SECTION("General"),
45 {"help", OPT_HELP, '-', "Display this summary"},
46 #ifndef OPENSSL_NO_ENGINE
47 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
49 {"check", OPT_CHECK, '-', "Check key consistency"},
50 {"pubcheck", OPT_PUB_CHECK, '-', "Check public key consistency"},
51 {"", OPT_MD, '-', "Any supported cipher"},
52 {"ec_param_enc", OPT_EC_PARAM_ENC, 's',
53 "Specifies the way the ec parameters are encoded"},
54 {"ec_conv_form", OPT_EC_CONV_FORM, 's',
55 "Specifies the point conversion form "},
58 {"in", OPT_IN, 's', "Input key"},
59 {"inform", OPT_INFORM, 'f', "Input format (DER or PEM)"},
60 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
61 {"pubin", OPT_PUBIN, '-',
62 "Read public key from input (default is private key)"},
63 {"traditional", OPT_TRADITIONAL, '-',
64 "Use traditional format for private keys"},
66 OPT_SECTION("Output"),
67 {"outform", OPT_OUTFORM, 'F', "Output format (DER or PEM)"},
68 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
69 {"out", OPT_OUT, '>', "Output file"},
70 {"pubout", OPT_PUBOUT, '-', "Output public key, not private"},
71 {"text_pub", OPT_TEXT_PUB, '-', "Only output public key components"},
72 {"text", OPT_TEXT, '-', "Output in plaintext as well"},
73 {"noout", OPT_NOOUT, '-', "Don't output the key"},
78 int pkey_main(int argc, char **argv)
80 BIO *in = NULL, *out = NULL;
82 EVP_PKEY *pkey = NULL;
83 const EVP_CIPHER *cipher = NULL;
84 char *infile = NULL, *outfile = NULL, *passin = NULL, *passout = NULL;
85 char *passinarg = NULL, *passoutarg = NULL, *prog;
87 int informat = FORMAT_PEM, outformat = FORMAT_PEM;
88 int pubin = 0, pubout = 0, pubtext = 0, text = 0, noout = 0, ret = 1;
89 int private = 0, traditional = 0, check = 0, pub_check = 0;
92 int ec_asn1_flag = OPENSSL_EC_NAMED_CURVE, new_ec_asn1_flag = 0;
93 int i, new_ec_form = 0;
94 point_conversion_form_t ec_form = POINT_CONVERSION_UNCOMPRESSED;
97 prog = opt_init(argc, argv, pkey_options);
98 while ((o = opt_next()) != OPT_EOF) {
103 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
106 opt_help(pkey_options);
110 if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
114 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
118 passinarg = opt_arg();
121 passoutarg = opt_arg();
124 e = setup_engine(opt_arg(), 0);
133 pubin = pubout = pubtext = 1;
147 case OPT_TRADITIONAL:
157 if (!opt_cipher(opt_unknown(), &cipher))
160 case OPT_EC_CONV_FORM:
164 if (!opt_pair(opt_arg(), ec_conv_forms, &i))
170 case OPT_EC_PARAM_ENC:
174 if (!opt_pair(opt_arg(), ec_param_enc, &i))
176 new_ec_asn1_flag = 1;
182 argc = opt_num_rest();
186 private = !noout && !pubout ? 1 : 0;
187 if (text && !pubtext)
190 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
191 BIO_printf(bio_err, "Error getting passwords\n");
195 out = bio_open_owner(outfile, outformat, private);
200 pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
202 pkey = load_key(infile, informat, 1, passin, e, "key");
206 #ifndef OPENSSL_NO_EC
208 * TODO: remove this and use a set params call with a 'pkeyopt' command
209 * line option instead.
211 if (new_ec_form || new_ec_asn1_flag) {
212 if ((eckey = EVP_PKEY_get0_EC_KEY(pkey)) == NULL) {
213 ERR_print_errors(bio_err);
217 EC_KEY_set_conv_form(eckey, ec_form);
219 if (new_ec_asn1_flag)
220 EC_KEY_set_asn1_flag(eckey, ec_asn1_flag);
224 if (check || pub_check) {
228 ctx = EVP_PKEY_CTX_new(pkey, e);
230 ERR_print_errors(bio_err);
235 r = EVP_PKEY_check(ctx);
237 r = EVP_PKEY_public_check(ctx);
240 BIO_printf(out, "Key is valid\n");
243 * Note: at least for RSA keys if this function returns
244 * -1, there will be no error reasons.
248 BIO_printf(out, "Key is invalid\n");
250 while ((err = ERR_peek_error()) != 0) {
251 BIO_printf(out, "Detailed error: %s\n",
252 ERR_reason_error_string(err));
253 ERR_get_error(); /* remove err from error stack */
256 EVP_PKEY_CTX_free(ctx);
260 if (outformat == FORMAT_PEM) {
262 if (!PEM_write_bio_PUBKEY(out, pkey))
267 if (!PEM_write_bio_PrivateKey_traditional(out, pkey, cipher,
272 if (!PEM_write_bio_PrivateKey(out, pkey, cipher,
273 NULL, 0, NULL, passout))
277 } else if (outformat == FORMAT_ASN1) {
279 if (!i2d_PUBKEY_bio(out, pkey))
283 if (!i2d_PrivateKey_bio(out, pkey))
287 BIO_printf(bio_err, "Bad format specified for key\n");
294 if (EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
298 if (EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)
307 ERR_print_errors(bio_err);
312 OPENSSL_free(passin);
313 OPENSSL_free(passout);