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