2 * Copyright 1995-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
10 #include <openssl/opensslconf.h>
18 #include <openssl/bio.h>
19 #include <openssl/err.h>
20 #include <openssl/rsa.h>
21 #include <openssl/evp.h>
22 #include <openssl/x509.h>
23 #include <openssl/pem.h>
24 #include <openssl/bn.h>
26 typedef enum OPTION_choice {
27 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
28 OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
29 OPT_PUBIN, OPT_PUBOUT, OPT_PASSOUT, OPT_PASSIN,
30 OPT_RSAPUBKEY_IN, OPT_RSAPUBKEY_OUT,
31 /* Do not change the order here; see case statements below */
32 OPT_PVK_NONE, OPT_PVK_WEAK, OPT_PVK_STRONG,
33 OPT_NOOUT, OPT_TEXT, OPT_MODULUS, OPT_CHECK, OPT_CIPHER,
37 const OPTIONS rsa_options[] = {
38 OPT_SECTION("General"),
39 {"help", OPT_HELP, '-', "Display this summary"},
40 {"check", OPT_CHECK, '-', "Verify key consistency"},
41 {"", OPT_CIPHER, '-', "Any supported cipher"},
42 #ifndef OPENSSL_NO_ENGINE
43 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
47 {"in", OPT_IN, 's', "Input file"},
48 {"inform", OPT_INFORM, 'f', "Input format, one of DER PEM"},
49 {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
50 {"RSAPublicKey_in", OPT_RSAPUBKEY_IN, '-', "Input is an RSAPublicKey"},
51 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
53 OPT_SECTION("Output"),
54 {"out", OPT_OUT, '>', "Output file"},
55 {"outform", OPT_OUTFORM, 'f', "Output format, one of DER PEM PVK"},
56 {"pubout", OPT_PUBOUT, '-', "Output a public key"},
57 {"RSAPublicKey_out", OPT_RSAPUBKEY_OUT, '-', "Output is an RSAPublicKey"},
58 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
59 {"noout", OPT_NOOUT, '-', "Don't print key out"},
60 {"text", OPT_TEXT, '-', "Print the key in text"},
61 {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"},
63 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
65 {"pvk-strong", OPT_PVK_STRONG, '-', "Enable 'Strong' PVK encoding level (default)"},
66 {"pvk-weak", OPT_PVK_WEAK, '-', "Enable 'Weak' PVK encoding level"},
67 {"pvk-none", OPT_PVK_NONE, '-', "Don't enforce PVK encoding"},
74 int rsa_main(int argc, char **argv)
79 EVP_PKEY *pkey = NULL;
81 const EVP_CIPHER *enc = NULL;
82 char *infile = NULL, *outfile = NULL, *prog;
83 char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
85 int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, check = 0;
86 int noout = 0, modulus = 0, pubin = 0, pubout = 0, ret = 1;
87 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
92 prog = opt_init(argc, argv, rsa_options);
93 while ((o = opt_next()) != OPT_EOF) {
98 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
101 opt_help(rsa_options);
105 if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
112 if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
119 passinarg = opt_arg();
122 passoutarg = opt_arg();
125 e = setup_engine(opt_arg(), 0);
133 case OPT_RSAPUBKEY_IN:
136 case OPT_RSAPUBKEY_OUT:
139 case OPT_PVK_STRONG: /* pvk_encr:= 2 */
140 case OPT_PVK_WEAK: /* pvk_encr:= 1 */
141 case OPT_PVK_NONE: /* pvk_encr:= 0 */
142 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
143 pvk_encr = (o - OPT_PVK_NONE);
159 if (!opt_cipher(opt_unknown(), &enc))
163 if (!opt_provider(o))
168 argc = opt_num_rest();
172 private = (text && !pubin) || (!pubout && !noout) ? 1 : 0;
174 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
175 BIO_printf(bio_err, "Error getting passwords\n");
178 if (check && pubin) {
179 BIO_printf(bio_err, "Only private keys can be checked\n");
187 if (informat == FORMAT_PEM)
188 tmpformat = FORMAT_PEMRSA;
189 else if (informat == FORMAT_ASN1)
190 tmpformat = FORMAT_ASN1RSA;
192 tmpformat = informat;
195 pkey = load_pubkey(infile, tmpformat, 1, passin, e, "Public Key");
197 pkey = load_key(infile, informat, 1, passin, e, "Private Key");
201 rsa = EVP_PKEY_get1_RSA(pkey);
204 ERR_print_errors(bio_err);
208 out = bio_open_owner(outfile, outformat, private);
213 assert(pubin || private);
214 if ((pubin && EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
215 || (!pubin && EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)) {
217 ERR_print_errors(bio_err);
224 RSA_get0_key(rsa, &n, NULL, NULL);
225 BIO_printf(out, "Modulus=");
227 BIO_printf(out, "\n");
233 pctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
235 BIO_printf(out, "RSA unable to create PKEY context\n");
236 ERR_print_errors(bio_err);
239 r = EVP_PKEY_check(pctx);
240 EVP_PKEY_CTX_free(pctx);
243 BIO_printf(out, "RSA key ok\n");
247 while ((err = ERR_peek_error()) != 0 &&
248 ERR_GET_LIB(err) == ERR_LIB_RSA &&
249 ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) {
250 BIO_printf(out, "RSA key error: %s\n",
251 ERR_reason_error_string(err));
252 ERR_get_error(); /* remove err from error stack */
254 } else if (r == -1) {
255 ERR_print_errors(bio_err);
264 BIO_printf(bio_err, "writing RSA key\n");
265 if (outformat == FORMAT_ASN1) {
266 if (pubout || pubin) {
268 i = i2d_RSAPublicKey_bio(out, rsa);
270 i = i2d_RSA_PUBKEY_bio(out, rsa);
273 i = i2d_RSAPrivateKey_bio(out, rsa);
275 } else if (outformat == FORMAT_PEM) {
276 if (pubout || pubin) {
278 i = PEM_write_bio_RSAPublicKey(out, rsa);
280 i = PEM_write_bio_RSA_PUBKEY(out, rsa);
283 i = PEM_write_bio_RSAPrivateKey(out, rsa,
284 enc, NULL, 0, NULL, passout);
286 #ifndef OPENSSL_NO_DSA
287 } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
293 EVP_PKEY_set1_RSA(pk, rsa);
294 if (outformat == FORMAT_PVK) {
296 BIO_printf(bio_err, "PVK form impossible with public key input\n");
301 # ifdef OPENSSL_NO_RC4
302 BIO_printf(bio_err, "PVK format not supported\n");
306 i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);
308 } else if (pubin || pubout) {
309 i = i2b_PublicKey_bio(out, pk);
312 i = i2b_PrivateKey_bio(out, pk);
317 BIO_printf(bio_err, "bad output format specified for outfile\n");
321 BIO_printf(bio_err, "unable to write key\n");
322 ERR_print_errors(bio_err);
331 OPENSSL_free(passin);
332 OPENSSL_free(passout);