]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
a28d06f3 | 2 | * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
dffa7520 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
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 | |
7e1b7485 | 8 | */ |
d02b48c6 | 9 | |
94736c3a TM |
10 | /* Necessary for legacy RSA public key export */ |
11 | #define OPENSSL_SUPPRESS_DEPRECATED | |
12 | ||
3eeaab4b | 13 | #include <openssl/opensslconf.h> |
effaf4de | 14 | |
1ae56f2f RS |
15 | #include <stdio.h> |
16 | #include <stdlib.h> | |
17 | #include <string.h> | |
18 | #include <time.h> | |
19 | #include "apps.h" | |
20 | #include "progs.h" | |
21 | #include <openssl/bio.h> | |
22 | #include <openssl/err.h> | |
23 | #include <openssl/rsa.h> | |
24 | #include <openssl/evp.h> | |
25 | #include <openssl/x509.h> | |
26 | #include <openssl/pem.h> | |
27 | #include <openssl/bn.h> | |
d7e498ac RL |
28 | #include <openssl/encoder.h> |
29 | ||
30 | /* | |
db70dc2c | 31 | * This include is to get OSSL_KEYMGMT_SELECT_*, which feels a bit |
d7e498ac RL |
32 | * much just for those macros... they might serve better as EVP macros. |
33 | */ | |
34 | #include <openssl/core_dispatch.h> | |
0f113f3e | 35 | |
47422549 P |
36 | #ifndef OPENSSL_NO_RC4 |
37 | # define DEFAULT_PVK_ENCR_STRENGTH 2 | |
38 | #else | |
39 | # define DEFAULT_PVK_ENCR_STRENGTH 0 | |
40 | #endif | |
41 | ||
7e1b7485 | 42 | typedef enum OPTION_choice { |
b0f96018 | 43 | OPT_COMMON, |
7e1b7485 RS |
44 | OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT, |
45 | OPT_PUBIN, OPT_PUBOUT, OPT_PASSOUT, OPT_PASSIN, | |
e7917e38 F |
46 | OPT_RSAPUBKEY_IN, OPT_RSAPUBKEY_OUT, |
47 | /* Do not change the order here; see case statements below */ | |
48 | OPT_PVK_NONE, OPT_PVK_WEAK, OPT_PVK_STRONG, | |
6bd4e3f2 | 49 | OPT_NOOUT, OPT_TEXT, OPT_MODULUS, OPT_CHECK, OPT_CIPHER, |
10203a34 | 50 | OPT_PROV_ENUM, OPT_TRADITIONAL |
7e1b7485 RS |
51 | } OPTION_CHOICE; |
52 | ||
44c83ebd | 53 | const OPTIONS rsa_options[] = { |
5388f986 | 54 | OPT_SECTION("General"), |
7e1b7485 | 55 | {"help", OPT_HELP, '-', "Display this summary"}, |
5388f986 RS |
56 | {"check", OPT_CHECK, '-', "Verify key consistency"}, |
57 | {"", OPT_CIPHER, '-', "Any supported cipher"}, | |
1ae56f2f | 58 | #ifndef OPENSSL_NO_ENGINE |
5388f986 | 59 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, |
1ae56f2f | 60 | #endif |
5388f986 RS |
61 | |
62 | OPT_SECTION("Input"), | |
dd958974 | 63 | {"in", OPT_IN, 's', "Input file"}, |
6d382c74 | 64 | {"inform", OPT_INFORM, 'f', "Input format (DER/PEM/P12/ENGINE"}, |
7e1b7485 | 65 | {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"}, |
bc2f5803 | 66 | {"RSAPublicKey_in", OPT_RSAPUBKEY_IN, '-', "Input is an RSAPublicKey"}, |
5388f986 RS |
67 | {"passin", OPT_PASSIN, 's', "Input file pass phrase source"}, |
68 | ||
69 | OPT_SECTION("Output"), | |
70 | {"out", OPT_OUT, '>', "Output file"}, | |
71 | {"outform", OPT_OUTFORM, 'f', "Output format, one of DER PEM PVK"}, | |
72 | {"pubout", OPT_PUBOUT, '-', "Output a public key"}, | |
bc2f5803 | 73 | {"RSAPublicKey_out", OPT_RSAPUBKEY_OUT, '-', "Output is an RSAPublicKey"}, |
5388f986 | 74 | {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"}, |
7e1b7485 RS |
75 | {"noout", OPT_NOOUT, '-', "Don't print key out"}, |
76 | {"text", OPT_TEXT, '-', "Print the key in text"}, | |
77 | {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"}, | |
10203a34 KR |
78 | {"traditional", OPT_TRADITIONAL, '-', |
79 | "Use traditional format for private keys"}, | |
5388f986 | 80 | |
47422549 | 81 | #ifndef OPENSSL_NO_RC4 |
5388f986 | 82 | OPT_SECTION("PVK"), |
e7917e38 F |
83 | {"pvk-strong", OPT_PVK_STRONG, '-', "Enable 'Strong' PVK encoding level (default)"}, |
84 | {"pvk-weak", OPT_PVK_WEAK, '-', "Enable 'Weak' PVK encoding level"}, | |
85 | {"pvk-none", OPT_PVK_NONE, '-', "Don't enforce PVK encoding"}, | |
47422549 | 86 | #endif |
6bd4e3f2 P |
87 | |
88 | OPT_PROV_OPTIONS, | |
7e1b7485 RS |
89 | {NULL} |
90 | }; | |
667ac4ec | 91 | |
94736c3a TM |
92 | static int try_legacy_encoding(EVP_PKEY *pkey, int outformat, int pubout, |
93 | BIO *out) | |
94 | { | |
95 | int ret = 0; | |
96 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
97 | const RSA *rsa = EVP_PKEY_get0_RSA(pkey); | |
98 | ||
99 | if (rsa == NULL) | |
100 | return 0; | |
101 | ||
102 | if (outformat == FORMAT_ASN1) { | |
103 | if (pubout == 2) | |
104 | ret = i2d_RSAPublicKey_bio(out, rsa) > 0; | |
105 | else | |
106 | ret = i2d_RSA_PUBKEY_bio(out, rsa) > 0; | |
107 | } else if (outformat == FORMAT_PEM) { | |
108 | if (pubout == 2) | |
109 | ret = PEM_write_bio_RSAPublicKey(out, rsa) > 0; | |
110 | else | |
111 | ret = PEM_write_bio_RSA_PUBKEY(out, rsa) > 0; | |
112 | # ifndef OPENSSL_NO_DSA | |
113 | } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) { | |
114 | ret = i2b_PublicKey_bio(out, pkey) > 0; | |
115 | # endif | |
116 | } | |
117 | #endif | |
118 | ||
119 | return ret; | |
120 | } | |
121 | ||
7e1b7485 | 122 | int rsa_main(int argc, char **argv) |
0f113f3e MC |
123 | { |
124 | ENGINE *e = NULL; | |
7e1b7485 | 125 | BIO *out = NULL; |
54affb77 P |
126 | EVP_PKEY *pkey = NULL; |
127 | EVP_PKEY_CTX *pctx; | |
606a417f | 128 | EVP_CIPHER *enc = NULL; |
03bbd346 | 129 | char *infile = NULL, *outfile = NULL, *ciphername = NULL, *prog; |
7e1b7485 | 130 | char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL; |
d7e498ac | 131 | int private = 0; |
d382e796 | 132 | int informat = FORMAT_UNDEF, outformat = FORMAT_PEM, text = 0, check = 0; |
83ae8124 | 133 | int noout = 0, modulus = 0, pubin = 0, pubout = 0, ret = 1; |
47422549 | 134 | int pvk_encr = DEFAULT_PVK_ENCR_STRENGTH; |
7e1b7485 | 135 | OPTION_CHOICE o; |
10203a34 | 136 | int traditional = 0; |
d7e498ac RL |
137 | const char *output_type = NULL; |
138 | const char *output_structure = NULL; | |
139 | int selection = 0; | |
140 | OSSL_ENCODER_CTX *ectx = NULL; | |
7e1b7485 | 141 | |
2c272447 | 142 | opt_set_unknown_name("cipher"); |
7e1b7485 RS |
143 | prog = opt_init(argc, argv, rsa_options); |
144 | while ((o = opt_next()) != OPT_EOF) { | |
145 | switch (o) { | |
146 | case OPT_EOF: | |
147 | case OPT_ERR: | |
7e1b7485 RS |
148 | opthelp: |
149 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
150 | goto end; | |
151 | case OPT_HELP: | |
152 | opt_help(rsa_options); | |
153 | ret = 0; | |
154 | goto end; | |
155 | case OPT_INFORM: | |
156 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat)) | |
157 | goto opthelp; | |
158 | break; | |
159 | case OPT_IN: | |
160 | infile = opt_arg(); | |
161 | break; | |
162 | case OPT_OUTFORM: | |
163 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat)) | |
164 | goto opthelp; | |
165 | break; | |
166 | case OPT_OUT: | |
167 | outfile = opt_arg(); | |
168 | break; | |
169 | case OPT_PASSIN: | |
170 | passinarg = opt_arg(); | |
171 | break; | |
172 | case OPT_PASSOUT: | |
173 | passoutarg = opt_arg(); | |
174 | break; | |
175 | case OPT_ENGINE: | |
333b070e | 176 | e = setup_engine(opt_arg(), 0); |
7e1b7485 RS |
177 | break; |
178 | case OPT_PUBIN: | |
0f113f3e | 179 | pubin = 1; |
7e1b7485 RS |
180 | break; |
181 | case OPT_PUBOUT: | |
0f113f3e | 182 | pubout = 1; |
7e1b7485 RS |
183 | break; |
184 | case OPT_RSAPUBKEY_IN: | |
0f113f3e | 185 | pubin = 2; |
7e1b7485 RS |
186 | break; |
187 | case OPT_RSAPUBKEY_OUT: | |
0f113f3e | 188 | pubout = 2; |
7e1b7485 | 189 | break; |
e7917e38 F |
190 | case OPT_PVK_STRONG: /* pvk_encr:= 2 */ |
191 | case OPT_PVK_WEAK: /* pvk_encr:= 1 */ | |
192 | case OPT_PVK_NONE: /* pvk_encr:= 0 */ | |
e7917e38 | 193 | pvk_encr = (o - OPT_PVK_NONE); |
e7917e38 | 194 | break; |
7e1b7485 | 195 | case OPT_NOOUT: |
0f113f3e | 196 | noout = 1; |
7e1b7485 RS |
197 | break; |
198 | case OPT_TEXT: | |
0f113f3e | 199 | text = 1; |
7e1b7485 RS |
200 | break; |
201 | case OPT_MODULUS: | |
0f113f3e | 202 | modulus = 1; |
7e1b7485 RS |
203 | break; |
204 | case OPT_CHECK: | |
0f113f3e | 205 | check = 1; |
7e1b7485 RS |
206 | break; |
207 | case OPT_CIPHER: | |
03bbd346 | 208 | ciphername = opt_unknown(); |
0f113f3e | 209 | break; |
6bd4e3f2 P |
210 | case OPT_PROV_CASES: |
211 | if (!opt_provider(o)) | |
212 | goto end; | |
213 | break; | |
10203a34 KR |
214 | case OPT_TRADITIONAL: |
215 | traditional = 1; | |
216 | break; | |
0f113f3e | 217 | } |
0f113f3e | 218 | } |
021410ea RS |
219 | |
220 | /* No extra arguments. */ | |
d9f07357 | 221 | if (!opt_check_rest_arg(NULL)) |
03358517 KR |
222 | goto opthelp; |
223 | ||
d9f07357 DDO |
224 | if (!opt_cipher(ciphername, &enc)) |
225 | goto opthelp; | |
7eff6aa0 | 226 | private = (text && !pubin) || (!pubout && !noout) ? 1 : 0; |
0f113f3e | 227 | |
7e1b7485 | 228 | if (!app_passwd(passinarg, passoutarg, &passin, &passout)) { |
0f113f3e MC |
229 | BIO_printf(bio_err, "Error getting passwords\n"); |
230 | goto end; | |
231 | } | |
0f113f3e MC |
232 | if (check && pubin) { |
233 | BIO_printf(bio_err, "Only private keys can be checked\n"); | |
234 | goto end; | |
235 | } | |
236 | ||
54affb77 | 237 | if (pubin) { |
d382e796 | 238 | int tmpformat = FORMAT_UNDEF; |
0f113f3e | 239 | |
54affb77 P |
240 | if (pubin == 2) { |
241 | if (informat == FORMAT_PEM) | |
242 | tmpformat = FORMAT_PEMRSA; | |
243 | else if (informat == FORMAT_ASN1) | |
244 | tmpformat = FORMAT_ASN1RSA; | |
2234212c | 245 | } else { |
54affb77 | 246 | tmpformat = informat; |
2234212c | 247 | } |
0f113f3e | 248 | |
50eb2a50 | 249 | pkey = load_pubkey(infile, tmpformat, 1, passin, e, "public key"); |
54affb77 | 250 | } else { |
50eb2a50 | 251 | pkey = load_key(infile, informat, 1, passin, e, "private key"); |
0f113f3e MC |
252 | } |
253 | ||
d7e498ac | 254 | if (pkey == NULL) { |
0f113f3e MC |
255 | ERR_print_errors(bio_err); |
256 | goto end; | |
257 | } | |
388d6f45 | 258 | if (!EVP_PKEY_is_a(pkey, "RSA") && !EVP_PKEY_is_a(pkey, "RSA-PSS")) { |
d7e498ac RL |
259 | BIO_printf(bio_err, "Not an RSA key\n"); |
260 | goto end; | |
261 | } | |
0f113f3e | 262 | |
bdd58d98 | 263 | out = bio_open_owner(outfile, outformat, private); |
7e1b7485 RS |
264 | if (out == NULL) |
265 | goto end; | |
0f113f3e | 266 | |
3b061a00 | 267 | if (text) { |
7eff6aa0 | 268 | assert(pubin || private); |
54affb77 P |
269 | if ((pubin && EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0) |
270 | || (!pubin && EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)) { | |
0f113f3e MC |
271 | perror(outfile); |
272 | ERR_print_errors(bio_err); | |
273 | goto end; | |
274 | } | |
3b061a00 | 275 | } |
0f113f3e MC |
276 | |
277 | if (modulus) { | |
d7e498ac RL |
278 | BIGNUM *n = NULL; |
279 | ||
280 | /* Every RSA key has an 'n' */ | |
281 | EVP_PKEY_get_bn_param(pkey, "n", &n); | |
0f113f3e | 282 | BIO_printf(out, "Modulus="); |
9862e9aa | 283 | BN_print(out, n); |
0f113f3e | 284 | BIO_printf(out, "\n"); |
d7e498ac | 285 | BN_free(n); |
0f113f3e MC |
286 | } |
287 | ||
288 | if (check) { | |
54affb77 P |
289 | int r; |
290 | ||
291 | pctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL); | |
292 | if (pctx == NULL) { | |
7f90026b | 293 | BIO_printf(bio_err, "RSA unable to create PKEY context\n"); |
54affb77 P |
294 | ERR_print_errors(bio_err); |
295 | goto end; | |
296 | } | |
297 | r = EVP_PKEY_check(pctx); | |
298 | EVP_PKEY_CTX_free(pctx); | |
0f113f3e | 299 | |
2234212c | 300 | if (r == 1) { |
0f113f3e | 301 | BIO_printf(out, "RSA key ok\n"); |
2234212c | 302 | } else if (r == 0) { |
7f90026b DDO |
303 | BIO_printf(bio_err, "RSA key not ok\n"); |
304 | ERR_print_errors(bio_err); | |
63db6b77 | 305 | } else if (r == -1) { |
0f113f3e MC |
306 | ERR_print_errors(bio_err); |
307 | goto end; | |
308 | } | |
309 | } | |
310 | ||
311 | if (noout) { | |
312 | ret = 0; | |
313 | goto end; | |
314 | } | |
315 | BIO_printf(bio_err, "writing RSA key\n"); | |
d7e498ac RL |
316 | |
317 | /* Choose output type for the format */ | |
0f113f3e | 318 | if (outformat == FORMAT_ASN1) { |
d7e498ac | 319 | output_type = "DER"; |
2234212c | 320 | } else if (outformat == FORMAT_PEM) { |
d7e498ac RL |
321 | output_type = "PEM"; |
322 | } else if (outformat == FORMAT_MSBLOB) { | |
323 | output_type = "MSBLOB"; | |
324 | } else if (outformat == FORMAT_PVK) { | |
325 | if (pubin) { | |
326 | BIO_printf(bio_err, "PVK form impossible with public key input\n"); | |
327 | goto end; | |
328 | } | |
329 | output_type = "PVK"; | |
330 | } else { | |
331 | BIO_printf(bio_err, "bad output format specified for outfile\n"); | |
332 | goto end; | |
333 | } | |
334 | ||
335 | /* Select what you want in the output */ | |
336 | if (pubout || pubin) { | |
337 | selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY; | |
338 | } else { | |
339 | assert(private); | |
340 | selection = (OSSL_KEYMGMT_SELECT_KEYPAIR | |
341 | | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS); | |
342 | } | |
343 | ||
344 | /* For DER based output, select the desired output structure */ | |
345 | if (outformat == FORMAT_ASN1 || outformat == FORMAT_PEM) { | |
0f113f3e MC |
346 | if (pubout || pubin) { |
347 | if (pubout == 2) | |
d7e498ac | 348 | output_structure = "pkcs1"; /* "type-specific" would work too */ |
542b8488 RL |
349 | else |
350 | output_structure = "SubjectPublicKeyInfo"; | |
3b061a00 RS |
351 | } else { |
352 | assert(private); | |
d7e498ac RL |
353 | if (traditional) |
354 | output_structure = "pkcs1"; /* "type-specific" would work too */ | |
355 | else | |
6a2b8ff3 | 356 | output_structure = "PrivateKeyInfo"; |
3b061a00 | 357 | } |
d7e498ac | 358 | } |
d896b79b | 359 | |
d7e498ac | 360 | /* Now, perform the encoding */ |
fe75766c TM |
361 | ectx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, |
362 | output_type, output_structure, | |
363 | NULL); | |
d7e498ac | 364 | if (OSSL_ENCODER_CTX_get_num_encoders(ectx) == 0) { |
94736c3a TM |
365 | if ((!pubout && !pubin) |
366 | || !try_legacy_encoding(pkey, outformat, pubout, out)) | |
367 | BIO_printf(bio_err, "%s format not supported\n", output_type); | |
368 | else | |
369 | ret = 0; | |
d7e498ac RL |
370 | goto end; |
371 | } | |
372 | ||
5432d827 RL |
373 | /* Passphrase setup */ |
374 | if (enc != NULL) | |
ed576acd | 375 | OSSL_ENCODER_CTX_set_cipher(ectx, EVP_CIPHER_get0_name(enc), NULL); |
5432d827 RL |
376 | |
377 | /* Default passphrase prompter */ | |
378 | if (enc != NULL || outformat == FORMAT_PVK) { | |
379 | OSSL_ENCODER_CTX_set_passphrase_ui(ectx, get_ui_method(), NULL); | |
380 | if (passout != NULL) | |
381 | /* When passout given, override the passphrase prompter */ | |
382 | OSSL_ENCODER_CTX_set_passphrase(ectx, | |
383 | (const unsigned char *)passout, | |
384 | strlen(passout)); | |
385 | } | |
386 | ||
d7e498ac RL |
387 | /* PVK is a bit special... */ |
388 | if (outformat == FORMAT_PVK) { | |
389 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
390 | ||
391 | params[0] = OSSL_PARAM_construct_int("encrypt-level", &pvk_encr); | |
392 | if (!OSSL_ENCODER_CTX_set_params(ectx, params)) { | |
393 | BIO_printf(bio_err, "invalid PVK encryption level\n"); | |
b6c68982 | 394 | goto end; |
3b061a00 | 395 | } |
0f113f3e | 396 | } |
d7e498ac RL |
397 | |
398 | if (!OSSL_ENCODER_to_bio(ectx, out)) { | |
0f113f3e MC |
399 | BIO_printf(bio_err, "unable to write key\n"); |
400 | ERR_print_errors(bio_err); | |
d7e498ac | 401 | goto end; |
2234212c | 402 | } |
d7e498ac | 403 | ret = 0; |
0f113f3e | 404 | end: |
d7e498ac | 405 | OSSL_ENCODER_CTX_free(ectx); |
dd1abd44 | 406 | release_engine(e); |
ca3a82c3 | 407 | BIO_free_all(out); |
54affb77 | 408 | EVP_PKEY_free(pkey); |
606a417f | 409 | EVP_CIPHER_free(enc); |
b548a1f1 RS |
410 | OPENSSL_free(passin); |
411 | OPENSSL_free(passout); | |
26a7d938 | 412 | return ret; |
0f113f3e | 413 | } |