]>
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> | |
0f113f3e | 25 | |
7e1b7485 RS |
26 | typedef enum OPTION_choice { |
27 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
28 | OPT_INFORM, OPT_OUTFORM, OPT_ENGINE, OPT_IN, OPT_OUT, | |
29 | OPT_PUBIN, OPT_PUBOUT, OPT_PASSOUT, OPT_PASSIN, | |
e7917e38 F |
30 | OPT_RSAPUBKEY_IN, OPT_RSAPUBKEY_OUT, |
31 | /* Do not change the order here; see case statements below */ | |
32 | OPT_PVK_NONE, OPT_PVK_WEAK, OPT_PVK_STRONG, | |
6bd4e3f2 | 33 | OPT_NOOUT, OPT_TEXT, OPT_MODULUS, OPT_CHECK, OPT_CIPHER, |
10203a34 | 34 | OPT_PROV_ENUM, OPT_TRADITIONAL |
7e1b7485 RS |
35 | } OPTION_CHOICE; |
36 | ||
44c83ebd | 37 | const OPTIONS rsa_options[] = { |
5388f986 | 38 | OPT_SECTION("General"), |
7e1b7485 | 39 | {"help", OPT_HELP, '-', "Display this summary"}, |
5388f986 RS |
40 | {"check", OPT_CHECK, '-', "Verify key consistency"}, |
41 | {"", OPT_CIPHER, '-', "Any supported cipher"}, | |
1ae56f2f | 42 | #ifndef OPENSSL_NO_ENGINE |
5388f986 | 43 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, |
1ae56f2f | 44 | #endif |
5388f986 RS |
45 | |
46 | OPT_SECTION("Input"), | |
dd958974 | 47 | {"in", OPT_IN, 's', "Input file"}, |
6d382c74 | 48 | {"inform", OPT_INFORM, 'f', "Input format (DER/PEM/P12/ENGINE"}, |
7e1b7485 | 49 | {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"}, |
bc2f5803 | 50 | {"RSAPublicKey_in", OPT_RSAPUBKEY_IN, '-', "Input is an RSAPublicKey"}, |
5388f986 RS |
51 | {"passin", OPT_PASSIN, 's', "Input file pass phrase source"}, |
52 | ||
53 | OPT_SECTION("Output"), | |
54 | {"out", OPT_OUT, '>', "Output file"}, | |
55 | {"outform", OPT_OUTFORM, 'f', "Output format, one of DER PEM PVK"}, | |
56 | {"pubout", OPT_PUBOUT, '-', "Output a public key"}, | |
bc2f5803 | 57 | {"RSAPublicKey_out", OPT_RSAPUBKEY_OUT, '-', "Output is an RSAPublicKey"}, |
5388f986 | 58 | {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"}, |
7e1b7485 RS |
59 | {"noout", OPT_NOOUT, '-', "Don't print key out"}, |
60 | {"text", OPT_TEXT, '-', "Print the key in text"}, | |
61 | {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"}, | |
10203a34 KR |
62 | {"traditional", OPT_TRADITIONAL, '-', |
63 | "Use traditional format for private keys"}, | |
5388f986 | 64 | |
1ae56f2f | 65 | #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4) |
5388f986 | 66 | OPT_SECTION("PVK"), |
e7917e38 F |
67 | {"pvk-strong", OPT_PVK_STRONG, '-', "Enable 'Strong' PVK encoding level (default)"}, |
68 | {"pvk-weak", OPT_PVK_WEAK, '-', "Enable 'Weak' PVK encoding level"}, | |
69 | {"pvk-none", OPT_PVK_NONE, '-', "Don't enforce PVK encoding"}, | |
2edb571b | 70 | #endif |
6bd4e3f2 P |
71 | |
72 | OPT_PROV_OPTIONS, | |
7e1b7485 RS |
73 | {NULL} |
74 | }; | |
667ac4ec | 75 | |
7e1b7485 | 76 | int rsa_main(int argc, char **argv) |
0f113f3e MC |
77 | { |
78 | ENGINE *e = NULL; | |
7e1b7485 | 79 | BIO *out = NULL; |
0f113f3e | 80 | RSA *rsa = NULL; |
54affb77 P |
81 | EVP_PKEY *pkey = NULL; |
82 | EVP_PKEY_CTX *pctx; | |
0f113f3e | 83 | const EVP_CIPHER *enc = NULL; |
333b070e | 84 | char *infile = NULL, *outfile = NULL, *prog; |
7e1b7485 | 85 | char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL; |
3b061a00 | 86 | int i, private = 0; |
7e1b7485 | 87 | int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, check = 0; |
83ae8124 | 88 | int noout = 0, modulus = 0, pubin = 0, pubout = 0, ret = 1; |
1ae56f2f | 89 | #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4) |
83ae8124 | 90 | int pvk_encr = 2; |
1ae56f2f | 91 | #endif |
7e1b7485 | 92 | OPTION_CHOICE o; |
10203a34 | 93 | int traditional = 0; |
7e1b7485 RS |
94 | |
95 | prog = opt_init(argc, argv, rsa_options); | |
96 | while ((o = opt_next()) != OPT_EOF) { | |
97 | switch (o) { | |
98 | case OPT_EOF: | |
99 | case OPT_ERR: | |
7e1b7485 RS |
100 | opthelp: |
101 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
102 | goto end; | |
103 | case OPT_HELP: | |
104 | opt_help(rsa_options); | |
105 | ret = 0; | |
106 | goto end; | |
107 | case OPT_INFORM: | |
108 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat)) | |
109 | goto opthelp; | |
110 | break; | |
111 | case OPT_IN: | |
112 | infile = opt_arg(); | |
113 | break; | |
114 | case OPT_OUTFORM: | |
115 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat)) | |
116 | goto opthelp; | |
117 | break; | |
118 | case OPT_OUT: | |
119 | outfile = opt_arg(); | |
120 | break; | |
121 | case OPT_PASSIN: | |
122 | passinarg = opt_arg(); | |
123 | break; | |
124 | case OPT_PASSOUT: | |
125 | passoutarg = opt_arg(); | |
126 | break; | |
127 | case OPT_ENGINE: | |
333b070e | 128 | e = setup_engine(opt_arg(), 0); |
7e1b7485 RS |
129 | break; |
130 | case OPT_PUBIN: | |
0f113f3e | 131 | pubin = 1; |
7e1b7485 RS |
132 | break; |
133 | case OPT_PUBOUT: | |
0f113f3e | 134 | pubout = 1; |
7e1b7485 RS |
135 | break; |
136 | case OPT_RSAPUBKEY_IN: | |
0f113f3e | 137 | pubin = 2; |
7e1b7485 RS |
138 | break; |
139 | case OPT_RSAPUBKEY_OUT: | |
0f113f3e | 140 | pubout = 2; |
7e1b7485 | 141 | break; |
e7917e38 F |
142 | case OPT_PVK_STRONG: /* pvk_encr:= 2 */ |
143 | case OPT_PVK_WEAK: /* pvk_encr:= 1 */ | |
144 | case OPT_PVK_NONE: /* pvk_encr:= 0 */ | |
1ae56f2f | 145 | #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_RC4) |
e7917e38 | 146 | pvk_encr = (o - OPT_PVK_NONE); |
1ae56f2f | 147 | #endif |
e7917e38 | 148 | break; |
7e1b7485 | 149 | case OPT_NOOUT: |
0f113f3e | 150 | noout = 1; |
7e1b7485 RS |
151 | break; |
152 | case OPT_TEXT: | |
0f113f3e | 153 | text = 1; |
7e1b7485 RS |
154 | break; |
155 | case OPT_MODULUS: | |
0f113f3e | 156 | modulus = 1; |
7e1b7485 RS |
157 | break; |
158 | case OPT_CHECK: | |
0f113f3e | 159 | check = 1; |
7e1b7485 RS |
160 | break; |
161 | case OPT_CIPHER: | |
162 | if (!opt_cipher(opt_unknown(), &enc)) | |
163 | goto opthelp; | |
0f113f3e | 164 | break; |
6bd4e3f2 P |
165 | case OPT_PROV_CASES: |
166 | if (!opt_provider(o)) | |
167 | goto end; | |
168 | break; | |
10203a34 KR |
169 | case OPT_TRADITIONAL: |
170 | traditional = 1; | |
171 | break; | |
0f113f3e | 172 | } |
0f113f3e | 173 | } |
7e1b7485 | 174 | argc = opt_num_rest(); |
03358517 KR |
175 | if (argc != 0) |
176 | goto opthelp; | |
177 | ||
7eff6aa0 | 178 | private = (text && !pubin) || (!pubout && !noout) ? 1 : 0; |
0f113f3e | 179 | |
7e1b7485 | 180 | if (!app_passwd(passinarg, passoutarg, &passin, &passout)) { |
0f113f3e MC |
181 | BIO_printf(bio_err, "Error getting passwords\n"); |
182 | goto end; | |
183 | } | |
0f113f3e MC |
184 | if (check && pubin) { |
185 | BIO_printf(bio_err, "Only private keys can be checked\n"); | |
186 | goto end; | |
187 | } | |
188 | ||
54affb77 P |
189 | if (pubin) { |
190 | int tmpformat = -1; | |
0f113f3e | 191 | |
54affb77 P |
192 | if (pubin == 2) { |
193 | if (informat == FORMAT_PEM) | |
194 | tmpformat = FORMAT_PEMRSA; | |
195 | else if (informat == FORMAT_ASN1) | |
196 | tmpformat = FORMAT_ASN1RSA; | |
2234212c | 197 | } else { |
54affb77 | 198 | tmpformat = informat; |
2234212c | 199 | } |
0f113f3e | 200 | |
50eb2a50 | 201 | pkey = load_pubkey(infile, tmpformat, 1, passin, e, "public key"); |
54affb77 | 202 | } else { |
50eb2a50 | 203 | pkey = load_key(infile, informat, 1, passin, e, "private key"); |
0f113f3e MC |
204 | } |
205 | ||
54affb77 P |
206 | if (pkey != NULL) |
207 | rsa = EVP_PKEY_get1_RSA(pkey); | |
208 | ||
0f113f3e MC |
209 | if (rsa == NULL) { |
210 | ERR_print_errors(bio_err); | |
211 | goto end; | |
212 | } | |
213 | ||
bdd58d98 | 214 | out = bio_open_owner(outfile, outformat, private); |
7e1b7485 RS |
215 | if (out == NULL) |
216 | goto end; | |
0f113f3e | 217 | |
3b061a00 | 218 | if (text) { |
7eff6aa0 | 219 | assert(pubin || private); |
54affb77 P |
220 | if ((pubin && EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0) |
221 | || (!pubin && EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)) { | |
0f113f3e MC |
222 | perror(outfile); |
223 | ERR_print_errors(bio_err); | |
224 | goto end; | |
225 | } | |
3b061a00 | 226 | } |
0f113f3e MC |
227 | |
228 | if (modulus) { | |
2ac6115d | 229 | const BIGNUM *n; |
9862e9aa | 230 | RSA_get0_key(rsa, &n, NULL, NULL); |
0f113f3e | 231 | BIO_printf(out, "Modulus="); |
9862e9aa | 232 | BN_print(out, n); |
0f113f3e MC |
233 | BIO_printf(out, "\n"); |
234 | } | |
235 | ||
236 | if (check) { | |
54affb77 P |
237 | int r; |
238 | ||
239 | pctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL); | |
240 | if (pctx == NULL) { | |
241 | BIO_printf(out, "RSA unable to create PKEY context\n"); | |
242 | ERR_print_errors(bio_err); | |
243 | goto end; | |
244 | } | |
245 | r = EVP_PKEY_check(pctx); | |
246 | EVP_PKEY_CTX_free(pctx); | |
0f113f3e | 247 | |
2234212c | 248 | if (r == 1) { |
0f113f3e | 249 | BIO_printf(out, "RSA key ok\n"); |
2234212c | 250 | } else if (r == 0) { |
0f113f3e MC |
251 | unsigned long err; |
252 | ||
253 | while ((err = ERR_peek_error()) != 0 && | |
254 | ERR_GET_LIB(err) == ERR_LIB_RSA && | |
0f113f3e MC |
255 | ERR_GET_REASON(err) != ERR_R_MALLOC_FAILURE) { |
256 | BIO_printf(out, "RSA key error: %s\n", | |
257 | ERR_reason_error_string(err)); | |
67d4fee8 | 258 | ERR_get_error(); /* remove err from error stack */ |
0f113f3e | 259 | } |
63db6b77 | 260 | } else if (r == -1) { |
0f113f3e MC |
261 | ERR_print_errors(bio_err); |
262 | goto end; | |
263 | } | |
264 | } | |
265 | ||
266 | if (noout) { | |
267 | ret = 0; | |
268 | goto end; | |
269 | } | |
270 | BIO_printf(bio_err, "writing RSA key\n"); | |
271 | if (outformat == FORMAT_ASN1) { | |
272 | if (pubout || pubin) { | |
273 | if (pubout == 2) | |
274 | i = i2d_RSAPublicKey_bio(out, rsa); | |
275 | else | |
276 | i = i2d_RSA_PUBKEY_bio(out, rsa); | |
3b061a00 RS |
277 | } else { |
278 | assert(private); | |
0f113f3e | 279 | i = i2d_RSAPrivateKey_bio(out, rsa); |
3b061a00 | 280 | } |
2234212c | 281 | } else if (outformat == FORMAT_PEM) { |
0f113f3e MC |
282 | if (pubout || pubin) { |
283 | if (pubout == 2) | |
284 | i = PEM_write_bio_RSAPublicKey(out, rsa); | |
285 | else | |
286 | i = PEM_write_bio_RSA_PUBKEY(out, rsa); | |
3b061a00 RS |
287 | } else { |
288 | assert(private); | |
10203a34 KR |
289 | if (traditional) { |
290 | i = PEM_write_bio_PrivateKey_traditional(out, pkey, enc, NULL, 0, | |
291 | NULL, passout); | |
292 | } else { | |
293 | i = PEM_write_bio_PrivateKey(out, pkey, | |
294 | enc, NULL, 0, NULL, passout); | |
295 | } | |
3b061a00 | 296 | } |
1ae56f2f | 297 | #ifndef OPENSSL_NO_DSA |
0f113f3e MC |
298 | } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) { |
299 | EVP_PKEY *pk; | |
300 | pk = EVP_PKEY_new(); | |
d896b79b MA |
301 | if (pk == NULL) |
302 | goto end; | |
303 | ||
0f113f3e | 304 | EVP_PKEY_set1_RSA(pk, rsa); |
7eff6aa0 VD |
305 | if (outformat == FORMAT_PVK) { |
306 | if (pubin) { | |
307 | BIO_printf(bio_err, "PVK form impossible with public key input\n"); | |
308 | EVP_PKEY_free(pk); | |
309 | goto end; | |
310 | } | |
311 | assert(private); | |
1ae56f2f | 312 | # ifdef OPENSSL_NO_RC4 |
b6c68982 DSH |
313 | BIO_printf(bio_err, "PVK format not supported\n"); |
314 | EVP_PKEY_free(pk); | |
315 | goto end; | |
1ae56f2f | 316 | # else |
0f113f3e | 317 | i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout); |
1ae56f2f | 318 | # endif |
7eff6aa0 | 319 | } else if (pubin || pubout) { |
0f113f3e | 320 | i = i2b_PublicKey_bio(out, pk); |
7eff6aa0 | 321 | } else { |
3b061a00 | 322 | assert(private); |
0f113f3e | 323 | i = i2b_PrivateKey_bio(out, pk); |
3b061a00 | 324 | } |
0f113f3e | 325 | EVP_PKEY_free(pk); |
1ae56f2f | 326 | #endif |
0f113f3e MC |
327 | } else { |
328 | BIO_printf(bio_err, "bad output format specified for outfile\n"); | |
329 | goto end; | |
330 | } | |
331 | if (i <= 0) { | |
332 | BIO_printf(bio_err, "unable to write key\n"); | |
333 | ERR_print_errors(bio_err); | |
2234212c | 334 | } else { |
0f113f3e | 335 | ret = 0; |
2234212c | 336 | } |
0f113f3e | 337 | end: |
dd1abd44 | 338 | release_engine(e); |
ca3a82c3 | 339 | BIO_free_all(out); |
54affb77 | 340 | EVP_PKEY_free(pkey); |
d6407083 | 341 | RSA_free(rsa); |
b548a1f1 RS |
342 | OPENSSL_free(passin); |
343 | OPENSSL_free(passout); | |
26a7d938 | 344 | return ret; |
0f113f3e | 345 | } |