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,
34 OPT_PROV_ENUM, OPT_TRADITIONAL
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 (DER/PEM/P12/ENGINE"},
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"},
62 {"traditional", OPT_TRADITIONAL, '-',
63 "Use traditional format for private keys"},
65 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
67 {"pvk-strong", OPT_PVK_STRONG, '-', "Enable 'Strong' PVK encoding level (default)"},
68 {"pvk-weak", OPT_PVK_WEAK, '-', "Enable 'Weak' PVK encoding level"},
69 {"pvk-none", OPT_PVK_NONE, '-', "Don't enforce PVK encoding"},
76 int rsa_main(int argc, char **argv)
81 EVP_PKEY *pkey = NULL;
83 const EVP_CIPHER *enc = NULL;
84 char *infile = NULL, *outfile = NULL, *prog;
85 char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
87 int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, check = 0;
88 int noout = 0, modulus = 0, pubin = 0, pubout = 0, ret = 1;
89 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
95 prog = opt_init(argc, argv, rsa_options);
96 while ((o = opt_next()) != OPT_EOF) {
101 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
104 opt_help(rsa_options);
108 if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
115 if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
122 passinarg = opt_arg();
125 passoutarg = opt_arg();
128 e = setup_engine(opt_arg(), 0);
136 case OPT_RSAPUBKEY_IN:
139 case OPT_RSAPUBKEY_OUT:
142 case OPT_PVK_STRONG: /* pvk_encr:= 2 */
143 case OPT_PVK_WEAK: /* pvk_encr:= 1 */
144 case OPT_PVK_NONE: /* pvk_encr:= 0 */
145 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
146 pvk_encr = (o - OPT_PVK_NONE);
162 if (!opt_cipher(opt_unknown(), &enc))
166 if (!opt_provider(o))
169 case OPT_TRADITIONAL:
174 argc = opt_num_rest();
178 private = (text && !pubin) || (!pubout && !noout) ? 1 : 0;
180 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
181 BIO_printf(bio_err, "Error getting passwords\n");
184 if (check && pubin) {
185 BIO_printf(bio_err, "Only private keys can be checked\n");
193 if (informat == FORMAT_PEM)
194 tmpformat = FORMAT_PEMRSA;
195 else if (informat == FORMAT_ASN1)
196 tmpformat = FORMAT_ASN1RSA;
198 tmpformat = informat;
201 pkey = load_pubkey(infile, tmpformat, 1, passin, e, "public key");
203 pkey = load_key(infile, informat, 1, passin, e, "private key");
207 rsa = EVP_PKEY_get1_RSA(pkey);
210 ERR_print_errors(bio_err);
214 out = bio_open_owner(outfile, outformat, private);
219 assert(pubin || private);
220 if ((pubin && EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
221 || (!pubin && EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)) {
223 ERR_print_errors(bio_err);
230 RSA_get0_key(rsa, &n, NULL, NULL);
231 BIO_printf(out, "Modulus=");
233 BIO_printf(out, "\n");
239 pctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
241 BIO_printf(out, "RSA unable to create PKEY context\n");
242 ERR_print_errors(bio_err);
245 r = EVP_PKEY_check(pctx);
246 EVP_PKEY_CTX_free(pctx);
249 BIO_printf(out, "RSA key ok\n");
253 while ((err = ERR_peek_error()) != 0 &&
254 ERR_GET_LIB(err) == ERR_LIB_RSA &&
255 ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) {
256 BIO_printf(out, "RSA key error: %s\n",
257 ERR_reason_error_string(err));
258 ERR_get_error(); /* remove err from error stack */
260 } else if (r == -1) {
261 ERR_print_errors(bio_err);
270 BIO_printf(bio_err, "writing RSA key\n");
271 if (outformat == FORMAT_ASN1) {
272 if (pubout || pubin) {
274 i = i2d_RSAPublicKey_bio(out, rsa);
276 i = i2d_RSA_PUBKEY_bio(out, rsa);
279 i = i2d_RSAPrivateKey_bio(out, rsa);
281 } else if (outformat == FORMAT_PEM) {
282 if (pubout || pubin) {
284 i = PEM_write_bio_RSAPublicKey(out, rsa);
286 i = PEM_write_bio_RSA_PUBKEY(out, rsa);
290 i = PEM_write_bio_PrivateKey_traditional(out, pkey, enc, NULL, 0,
293 i = PEM_write_bio_PrivateKey(out, pkey,
294 enc, NULL, 0, NULL, passout);
297 #ifndef OPENSSL_NO_DSA
298 } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
304 EVP_PKEY_set1_RSA(pk, rsa);
305 if (outformat == FORMAT_PVK) {
307 BIO_printf(bio_err, "PVK form impossible with public key input\n");
312 # ifdef OPENSSL_NO_RC4
313 BIO_printf(bio_err, "PVK format not supported\n");
317 i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);
319 } else if (pubin || pubout) {
320 i = i2b_PublicKey_bio(out, pk);
323 i = i2b_PrivateKey_bio(out, pk);
328 BIO_printf(bio_err, "bad output format specified for outfile\n");
332 BIO_printf(bio_err, "unable to write key\n");
333 ERR_print_errors(bio_err);
342 OPENSSL_free(passin);
343 OPENSSL_free(passout);