2 * Copyright 2006-2020 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_CIPHER, OPT_TRADITIONAL, OPT_CHECK, OPT_PUB_CHECK,
40 OPT_EC_PARAM_ENC, OPT_EC_CONV_FORM,
44 const OPTIONS pkey_options[] = {
45 OPT_SECTION("General"),
46 {"help", OPT_HELP, '-', "Display this summary"},
47 #ifndef OPENSSL_NO_ENGINE
48 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
52 {"check", OPT_CHECK, '-', "Check key consistency"},
53 {"pubcheck", OPT_PUB_CHECK, '-', "Check public key consistency"},
56 {"in", OPT_IN, 's', "Input key"},
57 {"inform", OPT_INFORM, 'f',
58 "Key input format (ENGINE, other values ignored)"},
59 {"passin", OPT_PASSIN, 's', "Key input pass phrase source"},
60 {"pubin", OPT_PUBIN, '-',
61 "Read only public components from key input"},
63 OPT_SECTION("Output"),
64 {"out", OPT_OUT, '>', "Output file for encoded and/or text output"},
65 {"outform", OPT_OUTFORM, 'F', "Output encoding format (DER or PEM)"},
66 {"", OPT_CIPHER, '-', "Any supported cipher to be used for encryption"},
67 {"passout", OPT_PASSOUT, 's', "Output PEM file pass phrase source"},
68 {"traditional", OPT_TRADITIONAL, '-',
69 "Use traditional format for private key PEM output"},
70 {"pubout", OPT_PUBOUT, '-', "Restrict encoded output to public components"},
71 {"noout", OPT_NOOUT, '-', "Do not output the key in encoded form"},
72 {"text", OPT_TEXT, '-', "Output key components in plaintext"},
73 {"text_pub", OPT_TEXT_PUB, '-',
74 "Output only public key components in text form"},
75 {"ec_conv_form", OPT_EC_CONV_FORM, 's',
76 "Specifies the EC point conversion form in the encoding"},
77 {"ec_param_enc", OPT_EC_PARAM_ENC, 's',
78 "Specifies the way the EC parameters are encoded"},
83 int pkey_main(int argc, char **argv)
85 BIO *in = NULL, *out = NULL;
87 EVP_PKEY *pkey = NULL;
88 EVP_PKEY_CTX *ctx = NULL;
89 const EVP_CIPHER *cipher = NULL;
90 char *infile = NULL, *outfile = NULL, *passin = NULL, *passout = NULL;
91 char *passinarg = NULL, *passoutarg = NULL, *prog;
93 int informat = FORMAT_PEM, outformat = FORMAT_PEM;
94 int pubin = 0, pubout = 0, text_pub = 0, text = 0, noout = 0, ret = 1;
95 int private = 0, traditional = 0, check = 0, pub_check = 0;
98 int ec_asn1_flag = OPENSSL_EC_NAMED_CURVE, new_ec_asn1_flag = 0;
99 int i, new_ec_form = 0;
100 point_conversion_form_t ec_form = POINT_CONVERSION_UNCOMPRESSED;
103 prog = opt_init(argc, argv, pkey_options);
104 while ((o = opt_next()) != OPT_EOF) {
109 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
112 opt_help(pkey_options);
116 if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
120 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
124 passinarg = opt_arg();
127 passoutarg = opt_arg();
130 e = setup_engine(opt_arg(), 0);
153 case OPT_TRADITIONAL:
163 if (!opt_cipher(opt_unknown(), &cipher))
166 case OPT_EC_CONV_FORM:
170 if (!opt_pair(opt_arg(), ec_conv_forms, &i))
176 case OPT_EC_PARAM_ENC:
180 if (!opt_pair(opt_arg(), ec_param_enc, &i))
182 new_ec_asn1_flag = 1;
187 if (!opt_provider(o))
193 /* No extra arguments. */
194 argc = opt_num_rest();
200 "Warning: The -pubout option is ignored with -noout\n");
201 if (text && text_pub)
203 "Warning: The -text option is ignored with -text_pub\n");
204 if (traditional && (noout || outformat != FORMAT_PEM))
206 "Warning: The -traditional is ignored since there is no PEM output\n");
207 private = (!noout && !pubout) || (text && !text_pub);
209 if (cipher == NULL) {
210 if (passoutarg != NULL)
212 "Warning: The -passout option is ignored without a cipher option\n");
214 if (noout || outformat != FORMAT_PEM) {
216 "Error: Cipher options are supported only for PEM output\n");
220 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
221 BIO_printf(bio_err, "Error getting passwords\n");
225 out = bio_open_owner(outfile, outformat, private);
230 pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
232 pkey = load_key(infile, informat, 1, passin, e, "key");
236 #ifndef OPENSSL_NO_EC
238 * TODO: remove this and use a set params call with a 'pkeyopt' command
239 * line option instead.
241 if (new_ec_form || new_ec_asn1_flag) {
242 if ((eckey = EVP_PKEY_get0_EC_KEY(pkey)) == NULL) {
243 ERR_print_errors(bio_err);
247 EC_KEY_set_conv_form(eckey, ec_form);
249 if (new_ec_asn1_flag)
250 EC_KEY_set_asn1_flag(eckey, ec_asn1_flag);
254 if (check || pub_check) {
257 ctx = EVP_PKEY_CTX_new(pkey, e);
259 ERR_print_errors(bio_err);
264 r = EVP_PKEY_check(ctx);
266 r = EVP_PKEY_public_check(ctx);
269 BIO_printf(out, "Key is valid\n");
272 * Note: at least for RSA keys if this function returns
273 * -1, there will be no error reasons.
277 BIO_printf(out, "Key is invalid\n");
279 while ((err = ERR_peek_error()) != 0) {
280 BIO_printf(out, "Detailed error: %s\n",
281 ERR_reason_error_string(err));
282 ERR_get_error(); /* remove err from error stack */
289 if (outformat == FORMAT_PEM) {
291 if (!PEM_write_bio_PUBKEY(out, pkey))
296 if (!PEM_write_bio_PrivateKey_traditional(out, pkey, cipher,
301 if (!PEM_write_bio_PrivateKey(out, pkey, cipher,
302 NULL, 0, NULL, passout))
306 } else if (outformat == FORMAT_ASN1) {
307 if (text || text_pub) {
309 "Error: Text output cannot be combined with DER output\n");
313 if (!i2d_PUBKEY_bio(out, pkey))
317 if (!i2d_PrivateKey_bio(out, pkey))
321 BIO_printf(bio_err, "Bad format specified for key\n");
327 if (EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
331 if (EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)
339 ERR_print_errors(bio_err);
340 EVP_PKEY_CTX_free(ctx);
345 OPENSSL_free(passin);
346 OPENSSL_free(passout);