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>
25 #include <openssl/encoder.h>
28 * TODO: This include is to get OSSL_KEYMGMT_SELECT_*, which feels a bit
29 * much just for those macros... they might serve better as EVP macros.
31 #include <openssl/core_dispatch.h>
33 typedef enum OPTION_choice
{
34 OPT_ERR
= -1, OPT_EOF
= 0, OPT_HELP
,
35 OPT_INFORM
, OPT_OUTFORM
, OPT_ENGINE
, OPT_IN
, OPT_OUT
,
36 OPT_PUBIN
, OPT_PUBOUT
, OPT_PASSOUT
, OPT_PASSIN
,
37 OPT_RSAPUBKEY_IN
, OPT_RSAPUBKEY_OUT
,
38 /* Do not change the order here; see case statements below */
39 OPT_PVK_NONE
, OPT_PVK_WEAK
, OPT_PVK_STRONG
,
40 OPT_NOOUT
, OPT_TEXT
, OPT_MODULUS
, OPT_CHECK
, OPT_CIPHER
,
41 OPT_PROV_ENUM
, OPT_TRADITIONAL
44 const OPTIONS rsa_options
[] = {
45 OPT_SECTION("General"),
46 {"help", OPT_HELP
, '-', "Display this summary"},
47 {"check", OPT_CHECK
, '-', "Verify key consistency"},
48 {"", OPT_CIPHER
, '-', "Any supported cipher"},
49 #ifndef OPENSSL_NO_ENGINE
50 {"engine", OPT_ENGINE
, 's', "Use engine, possibly a hardware device"},
54 {"in", OPT_IN
, 's', "Input file"},
55 {"inform", OPT_INFORM
, 'f', "Input format (DER/PEM/P12/ENGINE"},
56 {"pubin", OPT_PUBIN
, '-', "Expect a public key in input file"},
57 {"RSAPublicKey_in", OPT_RSAPUBKEY_IN
, '-', "Input is an RSAPublicKey"},
58 {"passin", OPT_PASSIN
, 's', "Input file pass phrase source"},
60 OPT_SECTION("Output"),
61 {"out", OPT_OUT
, '>', "Output file"},
62 {"outform", OPT_OUTFORM
, 'f', "Output format, one of DER PEM PVK"},
63 {"pubout", OPT_PUBOUT
, '-', "Output a public key"},
64 {"RSAPublicKey_out", OPT_RSAPUBKEY_OUT
, '-', "Output is an RSAPublicKey"},
65 {"passout", OPT_PASSOUT
, 's', "Output file pass phrase source"},
66 {"noout", OPT_NOOUT
, '-', "Don't print key out"},
67 {"text", OPT_TEXT
, '-', "Print the key in text"},
68 {"modulus", OPT_MODULUS
, '-', "Print the RSA key modulus"},
69 {"traditional", OPT_TRADITIONAL
, '-',
70 "Use traditional format for private keys"},
73 {"pvk-strong", OPT_PVK_STRONG
, '-', "Enable 'Strong' PVK encoding level (default)"},
74 {"pvk-weak", OPT_PVK_WEAK
, '-', "Enable 'Weak' PVK encoding level"},
75 {"pvk-none", OPT_PVK_NONE
, '-', "Don't enforce PVK encoding"},
81 int rsa_main(int argc
, char **argv
)
85 EVP_PKEY
*pkey
= NULL
;
87 const EVP_CIPHER
*enc
= NULL
;
88 char *infile
= NULL
, *outfile
= NULL
, *prog
;
89 char *passin
= NULL
, *passout
= NULL
, *passinarg
= NULL
, *passoutarg
= NULL
;
91 int informat
= FORMAT_PEM
, outformat
= FORMAT_PEM
, text
= 0, check
= 0;
92 int noout
= 0, modulus
= 0, pubin
= 0, pubout
= 0, ret
= 1;
96 const char *output_type
= NULL
;
97 const char *output_structure
= NULL
;
99 OSSL_ENCODER_CTX
*ectx
= NULL
;
101 prog
= opt_init(argc
, argv
, rsa_options
);
102 while ((o
= opt_next()) != OPT_EOF
) {
107 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
110 opt_help(rsa_options
);
114 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &informat
))
121 if (!opt_format(opt_arg(), OPT_FMT_ANY
, &outformat
))
128 passinarg
= opt_arg();
131 passoutarg
= opt_arg();
134 e
= setup_engine(opt_arg(), 0);
142 case OPT_RSAPUBKEY_IN
:
145 case OPT_RSAPUBKEY_OUT
:
148 case OPT_PVK_STRONG
: /* pvk_encr:= 2 */
149 case OPT_PVK_WEAK
: /* pvk_encr:= 1 */
150 case OPT_PVK_NONE
: /* pvk_encr:= 0 */
151 pvk_encr
= (o
- OPT_PVK_NONE
);
166 if (!opt_cipher(opt_unknown(), &enc
))
170 if (!opt_provider(o
))
173 case OPT_TRADITIONAL
:
179 /* No extra arguments. */
180 argc
= opt_num_rest();
184 private = (text
&& !pubin
) || (!pubout
&& !noout
) ? 1 : 0;
186 if (!app_passwd(passinarg
, passoutarg
, &passin
, &passout
)) {
187 BIO_printf(bio_err
, "Error getting passwords\n");
190 if (check
&& pubin
) {
191 BIO_printf(bio_err
, "Only private keys can be checked\n");
199 if (informat
== FORMAT_PEM
)
200 tmpformat
= FORMAT_PEMRSA
;
201 else if (informat
== FORMAT_ASN1
)
202 tmpformat
= FORMAT_ASN1RSA
;
204 tmpformat
= informat
;
207 pkey
= load_pubkey(infile
, tmpformat
, 1, passin
, e
, "public key");
209 pkey
= load_key(infile
, informat
, 1, passin
, e
, "private key");
213 ERR_print_errors(bio_err
);
216 if (!EVP_PKEY_is_a(pkey
, "RSA")) {
217 BIO_printf(bio_err
, "Not an RSA key\n");
221 out
= bio_open_owner(outfile
, outformat
, private);
226 assert(pubin
|| private);
227 if ((pubin
&& EVP_PKEY_print_public(out
, pkey
, 0, NULL
) <= 0)
228 || (!pubin
&& EVP_PKEY_print_private(out
, pkey
, 0, NULL
) <= 0)) {
230 ERR_print_errors(bio_err
);
238 /* Every RSA key has an 'n' */
239 EVP_PKEY_get_bn_param(pkey
, "n", &n
);
240 BIO_printf(out
, "Modulus=");
242 BIO_printf(out
, "\n");
249 pctx
= EVP_PKEY_CTX_new_from_pkey(NULL
, pkey
, NULL
);
251 BIO_printf(out
, "RSA unable to create PKEY context\n");
252 ERR_print_errors(bio_err
);
255 r
= EVP_PKEY_check(pctx
);
256 EVP_PKEY_CTX_free(pctx
);
259 BIO_printf(out
, "RSA key ok\n");
263 while ((err
= ERR_peek_error()) != 0 &&
264 ERR_GET_LIB(err
) == ERR_LIB_RSA
&&
265 ERR_GET_REASON(err
) != ERR_R_MALLOC_FAILURE
) {
266 BIO_printf(out
, "RSA key error: %s\n",
267 ERR_reason_error_string(err
));
268 ERR_get_error(); /* remove err from error stack */
270 } else if (r
== -1) {
271 ERR_print_errors(bio_err
);
280 BIO_printf(bio_err
, "writing RSA key\n");
282 /* Choose output type for the format */
283 if (outformat
== FORMAT_ASN1
) {
285 } else if (outformat
== FORMAT_PEM
) {
287 } else if (outformat
== FORMAT_MSBLOB
) {
288 output_type
= "MSBLOB";
289 } else if (outformat
== FORMAT_PVK
) {
291 BIO_printf(bio_err
, "PVK form impossible with public key input\n");
296 BIO_printf(bio_err
, "bad output format specified for outfile\n");
300 /* Select what you want in the output */
301 if (pubout
|| pubin
) {
302 selection
= OSSL_KEYMGMT_SELECT_PUBLIC_KEY
;
305 selection
= (OSSL_KEYMGMT_SELECT_KEYPAIR
306 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
);
309 /* For DER based output, select the desired output structure */
310 if (outformat
== FORMAT_ASN1
|| outformat
== FORMAT_PEM
) {
311 if (pubout
|| pubin
) {
313 output_structure
= "pkcs1"; /* "type-specific" would work too */
315 output_structure
= "SubjectPublicKeyInfo";
319 output_structure
= "pkcs1"; /* "type-specific" would work too */
321 output_structure
= "pkcs8";
325 /* Now, perform the encoding */
326 ectx
= OSSL_ENCODER_CTX_new_by_EVP_PKEY(pkey
, selection
,
327 output_type
, output_structure
,
329 if (OSSL_ENCODER_CTX_get_num_encoders(ectx
) == 0) {
330 BIO_printf(bio_err
, "%s format not supported\n", output_type
);
334 /* PVK is a bit special... */
335 if (outformat
== FORMAT_PVK
) {
336 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
338 params
[0] = OSSL_PARAM_construct_int("encrypt-level", &pvk_encr
);
339 if (!OSSL_ENCODER_CTX_set_params(ectx
, params
)) {
340 BIO_printf(bio_err
, "invalid PVK encryption level\n");
345 if (!OSSL_ENCODER_to_bio(ectx
, out
)) {
346 BIO_printf(bio_err
, "unable to write key\n");
347 ERR_print_errors(bio_err
);
352 OSSL_ENCODER_CTX_free(ectx
);
356 OPENSSL_free(passin
);
357 OPENSSL_free(passout
);