]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
33388b44 | 2 | * Copyright 1995-2020 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 | |
3eeaab4b | 10 | #include <openssl/opensslconf.h> |
effaf4de | 11 | |
1ae56f2f RS |
12 | #include <stdio.h> |
13 | #include <stdlib.h> | |
14 | #include <string.h> | |
15 | #include <time.h> | |
16 | #include "apps.h" | |
17 | #include "progs.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> | |
d7e498ac RL |
25 | #include <openssl/encoder.h> |
26 | ||
27 | /* | |
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. | |
30 | */ | |
31 | #include <openssl/core_dispatch.h> | |
0f113f3e | 32 | |
7e1b7485 RS |
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, | |
e7917e38 F |
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, | |
6bd4e3f2 | 40 | OPT_NOOUT, OPT_TEXT, OPT_MODULUS, OPT_CHECK, OPT_CIPHER, |
10203a34 | 41 | OPT_PROV_ENUM, OPT_TRADITIONAL |
7e1b7485 RS |
42 | } OPTION_CHOICE; |
43 | ||
44c83ebd | 44 | const OPTIONS rsa_options[] = { |
5388f986 | 45 | OPT_SECTION("General"), |
7e1b7485 | 46 | {"help", OPT_HELP, '-', "Display this summary"}, |
5388f986 RS |
47 | {"check", OPT_CHECK, '-', "Verify key consistency"}, |
48 | {"", OPT_CIPHER, '-', "Any supported cipher"}, | |
1ae56f2f | 49 | #ifndef OPENSSL_NO_ENGINE |
5388f986 | 50 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, |
1ae56f2f | 51 | #endif |
5388f986 RS |
52 | |
53 | OPT_SECTION("Input"), | |
dd958974 | 54 | {"in", OPT_IN, 's', "Input file"}, |
6d382c74 | 55 | {"inform", OPT_INFORM, 'f', "Input format (DER/PEM/P12/ENGINE"}, |
7e1b7485 | 56 | {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"}, |
bc2f5803 | 57 | {"RSAPublicKey_in", OPT_RSAPUBKEY_IN, '-', "Input is an RSAPublicKey"}, |
5388f986 RS |
58 | {"passin", OPT_PASSIN, 's', "Input file pass phrase source"}, |
59 | ||
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"}, | |
bc2f5803 | 64 | {"RSAPublicKey_out", OPT_RSAPUBKEY_OUT, '-', "Output is an RSAPublicKey"}, |
5388f986 | 65 | {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"}, |
7e1b7485 RS |
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"}, | |
10203a34 KR |
69 | {"traditional", OPT_TRADITIONAL, '-', |
70 | "Use traditional format for private keys"}, | |
5388f986 | 71 | |
5388f986 | 72 | OPT_SECTION("PVK"), |
e7917e38 F |
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"}, | |
6bd4e3f2 P |
76 | |
77 | OPT_PROV_OPTIONS, | |
7e1b7485 RS |
78 | {NULL} |
79 | }; | |
667ac4ec | 80 | |
7e1b7485 | 81 | int rsa_main(int argc, char **argv) |
0f113f3e MC |
82 | { |
83 | ENGINE *e = NULL; | |
7e1b7485 | 84 | BIO *out = NULL; |
54affb77 P |
85 | EVP_PKEY *pkey = NULL; |
86 | EVP_PKEY_CTX *pctx; | |
0f113f3e | 87 | const EVP_CIPHER *enc = NULL; |
333b070e | 88 | char *infile = NULL, *outfile = NULL, *prog; |
7e1b7485 | 89 | char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL; |
d7e498ac | 90 | int private = 0; |
7e1b7485 | 91 | int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, check = 0; |
83ae8124 | 92 | int noout = 0, modulus = 0, pubin = 0, pubout = 0, ret = 1; |
83ae8124 | 93 | int pvk_encr = 2; |
7e1b7485 | 94 | OPTION_CHOICE o; |
10203a34 | 95 | int traditional = 0; |
d7e498ac RL |
96 | const char *output_type = NULL; |
97 | const char *output_structure = NULL; | |
98 | int selection = 0; | |
99 | OSSL_ENCODER_CTX *ectx = NULL; | |
7e1b7485 RS |
100 | |
101 | prog = opt_init(argc, argv, rsa_options); | |
102 | while ((o = opt_next()) != OPT_EOF) { | |
103 | switch (o) { | |
104 | case OPT_EOF: | |
105 | case OPT_ERR: | |
7e1b7485 RS |
106 | opthelp: |
107 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
108 | goto end; | |
109 | case OPT_HELP: | |
110 | opt_help(rsa_options); | |
111 | ret = 0; | |
112 | goto end; | |
113 | case OPT_INFORM: | |
114 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat)) | |
115 | goto opthelp; | |
116 | break; | |
117 | case OPT_IN: | |
118 | infile = opt_arg(); | |
119 | break; | |
120 | case OPT_OUTFORM: | |
121 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat)) | |
122 | goto opthelp; | |
123 | break; | |
124 | case OPT_OUT: | |
125 | outfile = opt_arg(); | |
126 | break; | |
127 | case OPT_PASSIN: | |
128 | passinarg = opt_arg(); | |
129 | break; | |
130 | case OPT_PASSOUT: | |
131 | passoutarg = opt_arg(); | |
132 | break; | |
133 | case OPT_ENGINE: | |
333b070e | 134 | e = setup_engine(opt_arg(), 0); |
7e1b7485 RS |
135 | break; |
136 | case OPT_PUBIN: | |
0f113f3e | 137 | pubin = 1; |
7e1b7485 RS |
138 | break; |
139 | case OPT_PUBOUT: | |
0f113f3e | 140 | pubout = 1; |
7e1b7485 RS |
141 | break; |
142 | case OPT_RSAPUBKEY_IN: | |
0f113f3e | 143 | pubin = 2; |
7e1b7485 RS |
144 | break; |
145 | case OPT_RSAPUBKEY_OUT: | |
0f113f3e | 146 | pubout = 2; |
7e1b7485 | 147 | break; |
e7917e38 F |
148 | case OPT_PVK_STRONG: /* pvk_encr:= 2 */ |
149 | case OPT_PVK_WEAK: /* pvk_encr:= 1 */ | |
150 | case OPT_PVK_NONE: /* pvk_encr:= 0 */ | |
e7917e38 | 151 | pvk_encr = (o - OPT_PVK_NONE); |
e7917e38 | 152 | break; |
7e1b7485 | 153 | case OPT_NOOUT: |
0f113f3e | 154 | noout = 1; |
7e1b7485 RS |
155 | break; |
156 | case OPT_TEXT: | |
0f113f3e | 157 | text = 1; |
7e1b7485 RS |
158 | break; |
159 | case OPT_MODULUS: | |
0f113f3e | 160 | modulus = 1; |
7e1b7485 RS |
161 | break; |
162 | case OPT_CHECK: | |
0f113f3e | 163 | check = 1; |
7e1b7485 RS |
164 | break; |
165 | case OPT_CIPHER: | |
166 | if (!opt_cipher(opt_unknown(), &enc)) | |
167 | goto opthelp; | |
0f113f3e | 168 | break; |
6bd4e3f2 P |
169 | case OPT_PROV_CASES: |
170 | if (!opt_provider(o)) | |
171 | goto end; | |
172 | break; | |
10203a34 KR |
173 | case OPT_TRADITIONAL: |
174 | traditional = 1; | |
175 | break; | |
0f113f3e | 176 | } |
0f113f3e | 177 | } |
021410ea RS |
178 | |
179 | /* No extra arguments. */ | |
7e1b7485 | 180 | argc = opt_num_rest(); |
03358517 KR |
181 | if (argc != 0) |
182 | goto opthelp; | |
183 | ||
7eff6aa0 | 184 | private = (text && !pubin) || (!pubout && !noout) ? 1 : 0; |
0f113f3e | 185 | |
7e1b7485 | 186 | if (!app_passwd(passinarg, passoutarg, &passin, &passout)) { |
0f113f3e MC |
187 | BIO_printf(bio_err, "Error getting passwords\n"); |
188 | goto end; | |
189 | } | |
0f113f3e MC |
190 | if (check && pubin) { |
191 | BIO_printf(bio_err, "Only private keys can be checked\n"); | |
192 | goto end; | |
193 | } | |
194 | ||
54affb77 P |
195 | if (pubin) { |
196 | int tmpformat = -1; | |
0f113f3e | 197 | |
54affb77 P |
198 | if (pubin == 2) { |
199 | if (informat == FORMAT_PEM) | |
200 | tmpformat = FORMAT_PEMRSA; | |
201 | else if (informat == FORMAT_ASN1) | |
202 | tmpformat = FORMAT_ASN1RSA; | |
2234212c | 203 | } else { |
54affb77 | 204 | tmpformat = informat; |
2234212c | 205 | } |
0f113f3e | 206 | |
50eb2a50 | 207 | pkey = load_pubkey(infile, tmpformat, 1, passin, e, "public key"); |
54affb77 | 208 | } else { |
50eb2a50 | 209 | pkey = load_key(infile, informat, 1, passin, e, "private key"); |
0f113f3e MC |
210 | } |
211 | ||
d7e498ac | 212 | if (pkey == NULL) { |
0f113f3e MC |
213 | ERR_print_errors(bio_err); |
214 | goto end; | |
215 | } | |
d7e498ac RL |
216 | if (!EVP_PKEY_is_a(pkey, "RSA")) { |
217 | BIO_printf(bio_err, "Not an RSA key\n"); | |
218 | goto end; | |
219 | } | |
0f113f3e | 220 | |
bdd58d98 | 221 | out = bio_open_owner(outfile, outformat, private); |
7e1b7485 RS |
222 | if (out == NULL) |
223 | goto end; | |
0f113f3e | 224 | |
3b061a00 | 225 | if (text) { |
7eff6aa0 | 226 | assert(pubin || private); |
54affb77 P |
227 | if ((pubin && EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0) |
228 | || (!pubin && EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)) { | |
0f113f3e MC |
229 | perror(outfile); |
230 | ERR_print_errors(bio_err); | |
231 | goto end; | |
232 | } | |
3b061a00 | 233 | } |
0f113f3e MC |
234 | |
235 | if (modulus) { | |
d7e498ac RL |
236 | BIGNUM *n = NULL; |
237 | ||
238 | /* Every RSA key has an 'n' */ | |
239 | EVP_PKEY_get_bn_param(pkey, "n", &n); | |
0f113f3e | 240 | BIO_printf(out, "Modulus="); |
9862e9aa | 241 | BN_print(out, n); |
0f113f3e | 242 | BIO_printf(out, "\n"); |
d7e498ac | 243 | BN_free(n); |
0f113f3e MC |
244 | } |
245 | ||
246 | if (check) { | |
54affb77 P |
247 | int r; |
248 | ||
249 | pctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL); | |
250 | if (pctx == NULL) { | |
251 | BIO_printf(out, "RSA unable to create PKEY context\n"); | |
252 | ERR_print_errors(bio_err); | |
253 | goto end; | |
254 | } | |
255 | r = EVP_PKEY_check(pctx); | |
256 | EVP_PKEY_CTX_free(pctx); | |
0f113f3e | 257 | |
2234212c | 258 | if (r == 1) { |
0f113f3e | 259 | BIO_printf(out, "RSA key ok\n"); |
2234212c | 260 | } else if (r == 0) { |
0f113f3e MC |
261 | unsigned long err; |
262 | ||
263 | while ((err = ERR_peek_error()) != 0 && | |
264 | ERR_GET_LIB(err) == ERR_LIB_RSA && | |
0f113f3e MC |
265 | ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) { |
266 | BIO_printf(out, "RSA key error: %s\n", | |
267 | ERR_reason_error_string(err)); | |
67d4fee8 | 268 | ERR_get_error(); /* remove err from error stack */ |
0f113f3e | 269 | } |
63db6b77 | 270 | } else if (r == -1) { |
0f113f3e MC |
271 | ERR_print_errors(bio_err); |
272 | goto end; | |
273 | } | |
274 | } | |
275 | ||
276 | if (noout) { | |
277 | ret = 0; | |
278 | goto end; | |
279 | } | |
280 | BIO_printf(bio_err, "writing RSA key\n"); | |
d7e498ac RL |
281 | |
282 | /* Choose output type for the format */ | |
0f113f3e | 283 | if (outformat == FORMAT_ASN1) { |
d7e498ac | 284 | output_type = "DER"; |
2234212c | 285 | } else if (outformat == FORMAT_PEM) { |
d7e498ac RL |
286 | output_type = "PEM"; |
287 | } else if (outformat == FORMAT_MSBLOB) { | |
288 | output_type = "MSBLOB"; | |
289 | } else if (outformat == FORMAT_PVK) { | |
290 | if (pubin) { | |
291 | BIO_printf(bio_err, "PVK form impossible with public key input\n"); | |
292 | goto end; | |
293 | } | |
294 | output_type = "PVK"; | |
295 | } else { | |
296 | BIO_printf(bio_err, "bad output format specified for outfile\n"); | |
297 | goto end; | |
298 | } | |
299 | ||
300 | /* Select what you want in the output */ | |
301 | if (pubout || pubin) { | |
302 | selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY; | |
303 | } else { | |
304 | assert(private); | |
305 | selection = (OSSL_KEYMGMT_SELECT_KEYPAIR | |
306 | | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS); | |
307 | } | |
308 | ||
309 | /* For DER based output, select the desired output structure */ | |
310 | if (outformat == FORMAT_ASN1 || outformat == FORMAT_PEM) { | |
0f113f3e MC |
311 | if (pubout || pubin) { |
312 | if (pubout == 2) | |
d7e498ac | 313 | output_structure = "SubjectPublicKeyInfo"; |
0f113f3e | 314 | else |
d7e498ac | 315 | output_structure = "pkcs1"; /* "type-specific" would work too */ |
3b061a00 RS |
316 | } else { |
317 | assert(private); | |
d7e498ac RL |
318 | if (traditional) |
319 | output_structure = "pkcs1"; /* "type-specific" would work too */ | |
320 | else | |
321 | output_structure = "pkcs8"; | |
3b061a00 | 322 | } |
d7e498ac | 323 | } |
d896b79b | 324 | |
d7e498ac RL |
325 | /* Now, perform the encoding */ |
326 | ectx = OSSL_ENCODER_CTX_new_by_EVP_PKEY(pkey, selection, | |
327 | output_type, output_structure, | |
b03da688 | 328 | NULL); |
d7e498ac RL |
329 | if (OSSL_ENCODER_CTX_get_num_encoders(ectx) == 0) { |
330 | BIO_printf(bio_err, "%s format not supported\n", output_type); | |
331 | goto end; | |
332 | } | |
333 | ||
334 | /* PVK is a bit special... */ | |
335 | if (outformat == FORMAT_PVK) { | |
336 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
337 | ||
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"); | |
b6c68982 | 341 | goto end; |
3b061a00 | 342 | } |
0f113f3e | 343 | } |
d7e498ac RL |
344 | |
345 | if (!OSSL_ENCODER_to_bio(ectx, out)) { | |
0f113f3e MC |
346 | BIO_printf(bio_err, "unable to write key\n"); |
347 | ERR_print_errors(bio_err); | |
d7e498ac | 348 | goto end; |
2234212c | 349 | } |
d7e498ac | 350 | ret = 0; |
0f113f3e | 351 | end: |
d7e498ac | 352 | OSSL_ENCODER_CTX_free(ectx); |
dd1abd44 | 353 | release_engine(e); |
ca3a82c3 | 354 | BIO_free_all(out); |
54affb77 | 355 | EVP_PKEY_free(pkey); |
b548a1f1 RS |
356 | OPENSSL_free(passin); |
357 | OPENSSL_free(passout); | |
26a7d938 | 358 | return ret; |
0f113f3e | 359 | } |