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>
18 typedef enum OPTION_choice {
19 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
20 OPT_INFORM, OPT_OUTFORM, OPT_PASSIN, OPT_PASSOUT, OPT_ENGINE,
21 OPT_IN, OPT_OUT, OPT_PUBIN, OPT_PUBOUT, OPT_TEXT_PUB,
22 OPT_TEXT, OPT_NOOUT, OPT_MD, OPT_TRADITIONAL, OPT_CHECK, OPT_PUB_CHECK
25 const OPTIONS pkey_options[] = {
26 OPT_SECTION("General"),
27 {"help", OPT_HELP, '-', "Display this summary"},
28 #ifndef OPENSSL_NO_ENGINE
29 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
31 {"check", OPT_CHECK, '-', "Check key consistency"},
32 {"pubcheck", OPT_PUB_CHECK, '-', "Check public key consistency"},
33 {"", OPT_MD, '-', "Any supported cipher"},
36 {"in", OPT_IN, 's', "Input key"},
37 {"inform", OPT_INFORM, 'f', "Input format (DER or PEM)"},
38 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
39 {"pubin", OPT_PUBIN, '-',
40 "Read public key from input (default is private key)"},
41 {"traditional", OPT_TRADITIONAL, '-',
42 "Use traditional format for private keys"},
44 OPT_SECTION("Output"),
45 {"outform", OPT_OUTFORM, 'F', "Output format (DER or PEM)"},
46 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
47 {"out", OPT_OUT, '>', "Output file"},
48 {"pubout", OPT_PUBOUT, '-', "Output public key, not private"},
49 {"text_pub", OPT_TEXT_PUB, '-', "Only output public key components"},
50 {"text", OPT_TEXT, '-', "Output in plaintext as well"},
51 {"noout", OPT_NOOUT, '-', "Don't output the key"},
56 int pkey_main(int argc, char **argv)
58 BIO *in = NULL, *out = NULL;
60 EVP_PKEY *pkey = NULL;
61 const EVP_CIPHER *cipher = NULL;
62 char *infile = NULL, *outfile = NULL, *passin = NULL, *passout = NULL;
63 char *passinarg = NULL, *passoutarg = NULL, *prog;
65 int informat = FORMAT_PEM, outformat = FORMAT_PEM;
66 int pubin = 0, pubout = 0, pubtext = 0, text = 0, noout = 0, ret = 1;
67 int private = 0, traditional = 0, check = 0, pub_check = 0;
69 prog = opt_init(argc, argv, pkey_options);
70 while ((o = opt_next()) != OPT_EOF) {
75 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
78 opt_help(pkey_options);
82 if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
86 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
90 passinarg = opt_arg();
93 passoutarg = opt_arg();
96 e = setup_engine(opt_arg(), 0);
105 pubin = pubout = pubtext = 1;
119 case OPT_TRADITIONAL:
129 if (!opt_cipher(opt_unknown(), &cipher))
133 argc = opt_num_rest();
137 private = !noout && !pubout ? 1 : 0;
138 if (text && !pubtext)
141 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
142 BIO_printf(bio_err, "Error getting passwords\n");
146 out = bio_open_owner(outfile, outformat, private);
151 pkey = load_pubkey(infile, informat, 1, passin, e, "Public Key");
153 pkey = load_key(infile, informat, 1, passin, e, "key");
157 if (check || pub_check) {
161 ctx = EVP_PKEY_CTX_new(pkey, e);
163 ERR_print_errors(bio_err);
168 r = EVP_PKEY_check(ctx);
170 r = EVP_PKEY_public_check(ctx);
173 BIO_printf(out, "Key is valid\n");
176 * Note: at least for RSA keys if this function returns
177 * -1, there will be no error reasons.
181 BIO_printf(out, "Key is invalid\n");
183 while ((err = ERR_peek_error()) != 0) {
184 BIO_printf(out, "Detailed error: %s\n",
185 ERR_reason_error_string(err));
186 ERR_get_error(); /* remove err from error stack */
189 EVP_PKEY_CTX_free(ctx);
193 if (outformat == FORMAT_PEM) {
195 if (!PEM_write_bio_PUBKEY(out, pkey))
200 if (!PEM_write_bio_PrivateKey_traditional(out, pkey, cipher,
205 if (!PEM_write_bio_PrivateKey(out, pkey, cipher,
206 NULL, 0, NULL, passout))
210 } else if (outformat == FORMAT_ASN1) {
212 if (!i2d_PUBKEY_bio(out, pkey))
216 if (!i2d_PrivateKey_bio(out, pkey))
220 BIO_printf(bio_err, "Bad format specified for key\n");
227 if (EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
231 if (EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)
240 ERR_print_errors(bio_err);
245 OPENSSL_free(passin);
246 OPENSSL_free(passout);