2 * Copyright 2000-2021 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>
15 #include <openssl/err.h>
16 #include <openssl/pem.h>
17 #include <openssl/rsa.h>
28 typedef enum OPTION_choice {
30 OPT_ENGINE, OPT_IN, OPT_OUT, OPT_ASN1PARSE, OPT_HEXDUMP,
31 OPT_RSA_RAW, OPT_OAEP, OPT_PKCS, OPT_X931,
32 OPT_SIGN, OPT_VERIFY, OPT_REV, OPT_ENCRYPT, OPT_DECRYPT,
33 OPT_PUBIN, OPT_CERTIN, OPT_INKEY, OPT_PASSIN, OPT_KEYFORM,
34 OPT_R_ENUM, OPT_PROV_ENUM
37 const OPTIONS rsautl_options[] = {
38 OPT_SECTION("General"),
39 {"help", OPT_HELP, '-', "Display this summary"},
40 {"sign", OPT_SIGN, '-', "Sign with private key"},
41 {"verify", OPT_VERIFY, '-', "Verify with public key"},
42 {"encrypt", OPT_ENCRYPT, '-', "Encrypt with public key"},
43 {"decrypt", OPT_DECRYPT, '-', "Decrypt with private key"},
44 #ifndef OPENSSL_NO_ENGINE
45 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
49 {"in", OPT_IN, '<', "Input file"},
50 {"inkey", OPT_INKEY, 's', "Input key"},
51 {"keyform", OPT_KEYFORM, 'E', "Private key format (ENGINE, other values ignored)"},
52 {"pubin", OPT_PUBIN, '-', "Input is an RSA public"},
53 {"certin", OPT_CERTIN, '-', "Input is a cert carrying an RSA public key"},
54 {"rev", OPT_REV, '-', "Reverse the order of the input buffer"},
55 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
57 OPT_SECTION("Output"),
58 {"out", OPT_OUT, '>', "Output file"},
59 {"raw", OPT_RSA_RAW, '-', "Use no padding"},
60 {"pkcs", OPT_PKCS, '-', "Use PKCS#1 v1.5 padding (default)"},
61 {"x931", OPT_X931, '-', "Use ANSI X9.31 padding"},
62 {"oaep", OPT_OAEP, '-', "Use PKCS#1 OAEP"},
63 {"asn1parse", OPT_ASN1PARSE, '-',
64 "Run output through asn1parse; useful with -verify"},
65 {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"},
72 int rsautl_main(int argc, char **argv)
74 BIO *in = NULL, *out = NULL;
76 EVP_PKEY *pkey = NULL;
77 EVP_PKEY_CTX *ctx = NULL;
79 char *infile = NULL, *outfile = NULL, *keyfile = NULL;
80 char *passinarg = NULL, *passin = NULL, *prog;
81 char rsa_mode = RSA_VERIFY, key_type = KEY_PRIVKEY;
82 unsigned char *rsa_in = NULL, *rsa_out = NULL, pad = RSA_PKCS1_PADDING;
83 size_t rsa_inlen, rsa_outlen = 0;
84 int keyformat = FORMAT_UNDEF, keysize, ret = 1, rv;
85 int hexdump = 0, asn1parse = 0, need_priv = 0, rev = 0;
88 prog = opt_init(argc, argv, rsautl_options);
89 while ((o = opt_next()) != OPT_EOF) {
94 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
97 opt_help(rsautl_options);
101 if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
111 e = setup_engine(opt_arg(), 0);
120 pad = RSA_NO_PADDING;
123 pad = RSA_PKCS1_OAEP_PADDING;
126 pad = RSA_PKCS1_PADDING;
129 pad = RSA_X931_PADDING;
136 rsa_mode = RSA_VERIFY;
142 rsa_mode = RSA_ENCRYPT;
145 rsa_mode = RSA_DECRYPT;
149 key_type = KEY_PUBKEY;
158 passinarg = opt_arg();
165 if (!opt_provider(o))
171 /* No extra arguments. */
172 if (!opt_check_rest_arg(NULL))
175 if (!app_RAND_load())
178 if (need_priv && (key_type != KEY_PRIVKEY)) {
179 BIO_printf(bio_err, "A private key is needed for this operation\n");
183 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
184 BIO_printf(bio_err, "Error getting password\n");
190 pkey = load_key(keyfile, keyformat, 0, passin, e, "private key");
194 pkey = load_pubkey(keyfile, keyformat, 0, NULL, e, "public key");
198 x = load_cert(keyfile, FORMAT_UNDEF, "Certificate");
200 pkey = X509_get_pubkey(x);
209 in = bio_open_default(infile, 'r', FORMAT_BINARY);
212 out = bio_open_default(outfile, 'w', FORMAT_BINARY);
216 keysize = EVP_PKEY_get_size(pkey);
218 rsa_in = app_malloc(keysize * 2, "hold rsa key");
219 rsa_out = app_malloc(keysize, "output rsa key");
220 rsa_outlen = keysize;
222 /* Read the input data */
223 rv = BIO_read(in, rsa_in, keysize * 2);
225 BIO_printf(bio_err, "Error reading input Data\n");
233 for (i = 0; i < rsa_inlen / 2; i++) {
235 rsa_in[i] = rsa_in[rsa_inlen - 1 - i];
236 rsa_in[rsa_inlen - 1 - i] = ctmp;
240 if ((ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL)) == NULL)
245 rv = EVP_PKEY_verify_recover_init(ctx) > 0
246 && EVP_PKEY_CTX_set_rsa_padding(ctx, pad) > 0
247 && EVP_PKEY_verify_recover(ctx, rsa_out, &rsa_outlen,
248 rsa_in, rsa_inlen) > 0;
251 rv = EVP_PKEY_sign_init(ctx) > 0
252 && EVP_PKEY_CTX_set_rsa_padding(ctx, pad) > 0
253 && EVP_PKEY_sign(ctx, rsa_out, &rsa_outlen, rsa_in, rsa_inlen) > 0;
256 rv = EVP_PKEY_encrypt_init(ctx) > 0
257 && EVP_PKEY_CTX_set_rsa_padding(ctx, pad) > 0
258 && EVP_PKEY_encrypt(ctx, rsa_out, &rsa_outlen, rsa_in, rsa_inlen) > 0;
261 rv = EVP_PKEY_decrypt_init(ctx) > 0
262 && EVP_PKEY_CTX_set_rsa_padding(ctx, pad) > 0
263 && EVP_PKEY_decrypt(ctx, rsa_out, &rsa_outlen, rsa_in, rsa_inlen) > 0;
268 BIO_printf(bio_err, "RSA operation error\n");
269 ERR_print_errors(bio_err);
274 if (!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) {
275 ERR_print_errors(bio_err);
277 } else if (hexdump) {
278 BIO_dump(out, (char *)rsa_out, rsa_outlen);
280 BIO_write(out, rsa_out, rsa_outlen);
283 EVP_PKEY_CTX_free(ctx);
288 OPENSSL_free(rsa_in);
289 OPENSSL_free(rsa_out);
290 OPENSSL_free(passin);