2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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>
17 #include <openssl/bio.h>
18 #include <openssl/err.h>
19 #include <openssl/rsa.h>
20 #include <openssl/evp.h>
21 #include <openssl/x509.h>
22 #include <openssl/pem.h>
23 #include <openssl/bn.h>
25 typedef enum OPTION_choice {
26 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
27 OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
28 OPT_PUBIN, OPT_PUBOUT, OPT_PASSOUT, OPT_PASSIN,
29 OPT_RSAPUBKEY_IN, OPT_RSAPUBKEY_OUT,
30 /* Do not change the order here; see case statements below */
31 OPT_PVK_NONE, OPT_PVK_WEAK, OPT_PVK_STRONG,
32 OPT_NOOUT, OPT_TEXT, OPT_MODULUS, OPT_CHECK, OPT_CIPHER
35 const OPTIONS rsa_options[] = {
36 {"help", OPT_HELP, '-', "Display this summary"},
37 {"inform", OPT_INFORM, 'f', "Input format, one of DER PEM"},
38 {"outform", OPT_OUTFORM, 'f', "Output format, one of DER PEM PVK"},
39 {"in", OPT_IN, 's', "Input file"},
40 {"out", OPT_OUT, '>', "Output file"},
41 {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
42 {"pubout", OPT_PUBOUT, '-', "Output a public key"},
43 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
44 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
45 {"RSAPublicKey_in", OPT_RSAPUBKEY_IN, '-', "Input is an RSAPublicKey"},
46 {"RSAPublicKey_out", OPT_RSAPUBKEY_OUT, '-', "Output is an RSAPublicKey"},
47 {"noout", OPT_NOOUT, '-', "Don't print key out"},
48 {"text", OPT_TEXT, '-', "Print the key in text"},
49 {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"},
50 {"check", OPT_CHECK, '-', "Verify key consistency"},
51 {"", OPT_CIPHER, '-', "Any supported cipher"},
52 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
53 {"pvk-strong", OPT_PVK_STRONG, '-', "Enable 'Strong' PVK encoding level (default)"},
54 {"pvk-weak", OPT_PVK_WEAK, '-', "Enable 'Weak' PVK encoding level"},
55 {"pvk-none", OPT_PVK_NONE, '-', "Don't enforce PVK encoding"},
57 #ifndef OPENSSL_NO_ENGINE
58 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
63 int rsa_main(int argc, char **argv)
68 const EVP_CIPHER *enc = NULL;
69 char *infile = NULL, *outfile = NULL, *prog;
70 char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
72 int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, check = 0;
73 int noout = 0, modulus = 0, pubin = 0, pubout = 0, ret = 1;
74 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
79 prog = opt_init(argc, argv, rsa_options);
80 while ((o = opt_next()) != OPT_EOF) {
85 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
88 opt_help(rsa_options);
92 if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
99 if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
106 passinarg = opt_arg();
109 passoutarg = opt_arg();
112 e = setup_engine(opt_arg(), 0);
120 case OPT_RSAPUBKEY_IN:
123 case OPT_RSAPUBKEY_OUT:
126 case OPT_PVK_STRONG: /* pvk_encr:= 2 */
127 case OPT_PVK_WEAK: /* pvk_encr:= 1 */
128 case OPT_PVK_NONE: /* pvk_encr:= 0 */
129 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
130 pvk_encr = (o - OPT_PVK_NONE);
146 if (!opt_cipher(opt_unknown(), &enc))
151 argc = opt_num_rest();
155 private = (text && !pubin) || (!pubout && !noout) ? 1 : 0;
157 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
158 BIO_printf(bio_err, "Error getting passwords\n");
161 if (check && pubin) {
162 BIO_printf(bio_err, "Only private keys can be checked\n");
172 if (informat == FORMAT_PEM)
173 tmpformat = FORMAT_PEMRSA;
174 else if (informat == FORMAT_ASN1)
175 tmpformat = FORMAT_ASN1RSA;
177 tmpformat = informat;
180 pkey = load_pubkey(infile, tmpformat, 1, passin, e, "Public Key");
182 pkey = load_key(infile, informat, 1, passin, e, "Private Key");
186 rsa = EVP_PKEY_get1_RSA(pkey);
191 ERR_print_errors(bio_err);
195 out = bio_open_owner(outfile, outformat, private);
200 assert(pubin || private);
201 if (!RSA_print(out, rsa, 0)) {
203 ERR_print_errors(bio_err);
210 RSA_get0_key(rsa, &n, NULL, NULL);
211 BIO_printf(out, "Modulus=");
213 BIO_printf(out, "\n");
217 int r = RSA_check_key_ex(rsa, NULL);
220 BIO_printf(out, "RSA key ok\n");
224 while ((err = ERR_peek_error()) != 0 &&
225 ERR_GET_LIB(err) == ERR_LIB_RSA &&
226 ERR_GET_FUNC(err) == RSA_F_RSA_CHECK_KEY_EX &&
227 ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) {
228 BIO_printf(out, "RSA key error: %s\n",
229 ERR_reason_error_string(err));
230 ERR_get_error(); /* remove err from error stack */
232 } else if (r == -1) {
233 ERR_print_errors(bio_err);
242 BIO_printf(bio_err, "writing RSA key\n");
243 if (outformat == FORMAT_ASN1) {
244 if (pubout || pubin) {
246 i = i2d_RSAPublicKey_bio(out, rsa);
248 i = i2d_RSA_PUBKEY_bio(out, rsa);
251 i = i2d_RSAPrivateKey_bio(out, rsa);
253 } else if (outformat == FORMAT_PEM) {
254 if (pubout || pubin) {
256 i = PEM_write_bio_RSAPublicKey(out, rsa);
258 i = PEM_write_bio_RSA_PUBKEY(out, rsa);
261 i = PEM_write_bio_RSAPrivateKey(out, rsa,
262 enc, NULL, 0, NULL, passout);
264 #ifndef OPENSSL_NO_DSA
265 } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
271 EVP_PKEY_set1_RSA(pk, rsa);
272 if (outformat == FORMAT_PVK) {
274 BIO_printf(bio_err, "PVK form impossible with public key input\n");
279 # ifdef OPENSSL_NO_RC4
280 BIO_printf(bio_err, "PVK format not supported\n");
284 i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);
286 } else if (pubin || pubout) {
287 i = i2b_PublicKey_bio(out, pk);
290 i = i2b_PrivateKey_bio(out, pk);
295 BIO_printf(bio_err, "bad output format specified for outfile\n");
299 BIO_printf(bio_err, "unable to write key\n");
300 ERR_print_errors(bio_err);
308 OPENSSL_free(passin);
309 OPENSSL_free(passout);