2 * Copyright 1995-2016 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>
12 NON_EMPTY_TRANSLATION_UNIT
20 # include <openssl/bio.h>
21 # include <openssl/err.h>
22 # include <openssl/rsa.h>
23 # include <openssl/evp.h>
24 # include <openssl/x509.h>
25 # include <openssl/pem.h>
26 # include <openssl/bn.h>
28 typedef enum OPTION_choice {
29 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
30 OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT,
31 OPT_PUBIN, OPT_PUBOUT, OPT_PASSOUT, OPT_PASSIN,
32 OPT_RSAPUBKEY_IN, OPT_RSAPUBKEY_OUT, OPT_PVK_STRONG, OPT_PVK_WEAK,
33 OPT_PVK_NONE, OPT_NOOUT, OPT_TEXT, OPT_MODULUS, OPT_CHECK, OPT_CIPHER
36 OPTIONS rsa_options[] = {
37 {"help", OPT_HELP, '-', "Display this summary"},
38 {"inform", OPT_INFORM, 'f', "Input format, one of DER NET PEM"},
39 {"outform", OPT_OUTFORM, 'f', "Output format, one of DER NET PEM PVK"},
40 {"in", OPT_IN, 's', "Input file"},
41 {"out", OPT_OUT, '>', "Output file"},
42 {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
43 {"pubout", OPT_PUBOUT, '-', "Output a public key"},
44 {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
45 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
46 {"RSAPublicKey_in", OPT_RSAPUBKEY_IN, '-', "Input is an RSAPublicKey"},
47 {"RSAPublicKey_out", OPT_RSAPUBKEY_OUT, '-', "Output is an RSAPublicKey"},
48 {"noout", OPT_NOOUT, '-', "Don't print key out"},
49 {"text", OPT_TEXT, '-', "Print the key in text"},
50 {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"},
51 {"check", OPT_CHECK, '-', "Verify key consistency"},
52 {"", OPT_CIPHER, '-', "Any supported cipher"},
53 # if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
54 {"pvk-strong", OPT_PVK_STRONG, '-'},
55 {"pvk-weak", OPT_PVK_WEAK, '-'},
56 {"pvk-none", OPT_PVK_NONE, '-'},
58 # ifndef OPENSSL_NO_ENGINE
59 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
64 int rsa_main(int argc, char **argv)
69 const EVP_CIPHER *enc = NULL;
70 char *infile = NULL, *outfile = NULL, *prog;
71 char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
73 int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, check = 0;
74 int noout = 0, modulus = 0, pubin = 0, pubout = 0, ret = 1;
75 # if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
80 prog = opt_init(argc, argv, rsa_options);
81 while ((o = opt_next()) != OPT_EOF) {
86 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
89 opt_help(rsa_options);
93 if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
100 if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
107 passinarg = opt_arg();
110 passoutarg = opt_arg();
113 e = setup_engine(opt_arg(), 0);
121 case OPT_RSAPUBKEY_IN:
124 case OPT_RSAPUBKEY_OUT:
127 # if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
156 if (!opt_cipher(opt_unknown(), &enc))
161 argc = opt_num_rest();
165 private = (text && !pubin) || (!pubout && !noout) ? 1 : 0;
167 if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
168 BIO_printf(bio_err, "Error getting passwords\n");
171 if (check && pubin) {
172 BIO_printf(bio_err, "Only private keys can be checked\n");
182 if (informat == FORMAT_PEM)
183 tmpformat = FORMAT_PEMRSA;
184 else if (informat == FORMAT_ASN1)
185 tmpformat = FORMAT_ASN1RSA;
187 tmpformat = informat;
189 pkey = load_pubkey(infile, tmpformat, 1, passin, e, "Public Key");
191 pkey = load_key(infile, informat, 1, passin, e, "Private Key");
194 rsa = EVP_PKEY_get1_RSA(pkey);
199 ERR_print_errors(bio_err);
203 out = bio_open_owner(outfile, outformat, private);
208 assert(pubin || private);
209 if (!RSA_print(out, rsa, 0)) {
211 ERR_print_errors(bio_err);
218 RSA_get0_key(rsa, &n, NULL, NULL);
219 BIO_printf(out, "Modulus=");
221 BIO_printf(out, "\n");
225 int r = RSA_check_key(rsa);
228 BIO_printf(out, "RSA key ok\n");
232 while ((err = ERR_peek_error()) != 0 &&
233 ERR_GET_LIB(err) == ERR_LIB_RSA &&
234 ERR_GET_FUNC(err) == RSA_F_RSA_CHECK_KEY &&
235 ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) {
236 BIO_printf(out, "RSA key error: %s\n",
237 ERR_reason_error_string(err));
238 ERR_get_error(); /* remove e from error stack */
242 /* should happen only if r == -1 */
243 if (r == -1 || ERR_peek_error() != 0) {
244 ERR_print_errors(bio_err);
253 BIO_printf(bio_err, "writing RSA key\n");
254 if (outformat == FORMAT_ASN1) {
255 if (pubout || pubin) {
257 i = i2d_RSAPublicKey_bio(out, rsa);
259 i = i2d_RSA_PUBKEY_bio(out, rsa);
262 i = i2d_RSAPrivateKey_bio(out, rsa);
265 else if (outformat == FORMAT_PEM) {
266 if (pubout || pubin) {
268 i = PEM_write_bio_RSAPublicKey(out, rsa);
270 i = PEM_write_bio_RSA_PUBKEY(out, rsa);
273 i = PEM_write_bio_RSAPrivateKey(out, rsa,
274 enc, NULL, 0, NULL, passout);
276 # if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4)
277 } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
280 EVP_PKEY_set1_RSA(pk, rsa);
281 if (outformat == FORMAT_PVK) {
283 BIO_printf(bio_err, "PVK form impossible with public key input\n");
288 i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);
289 } else if (pubin || pubout) {
290 i = i2b_PublicKey_bio(out, pk);
293 i = i2b_PrivateKey_bio(out, pk);
298 BIO_printf(bio_err, "bad output format specified for outfile\n");
302 BIO_printf(bio_err, "unable to write key\n");
303 ERR_print_errors(bio_err);
309 OPENSSL_free(passin);
310 OPENSSL_free(passout);