2 * Copyright 2000-2018 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 /* We need to use the deprecated RSA low level calls */
11 #define OPENSSL_SUPPRESS_DEPRECATED
13 #include <openssl/opensslconf.h>
15 NON_EMPTY_TRANSLATION_UNIT
21 # include <openssl/err.h>
22 # include <openssl/pem.h>
23 # include <openssl/rsa.h>
27 # define RSA_ENCRYPT 3
28 # define RSA_DECRYPT 4
30 # define KEY_PRIVKEY 1
34 typedef enum OPTION_choice {
35 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
36 OPT_ENGINE, OPT_IN, OPT_OUT, OPT_ASN1PARSE, OPT_HEXDUMP,
37 OPT_RSA_RAW, OPT_OAEP, OPT_SSL, OPT_PKCS, OPT_X931,
38 OPT_SIGN, OPT_VERIFY, OPT_REV, OPT_ENCRYPT, OPT_DECRYPT,
39 OPT_PUBIN, OPT_CERTIN, OPT_INKEY, OPT_PASSIN, OPT_KEYFORM,
40 OPT_R_ENUM, OPT_PROV_ENUM
43 const OPTIONS rsautl_options[] = {
44 OPT_SECTION("General"),
45 {"help", OPT_HELP, '-', "Display this summary"},
46 {"sign", OPT_SIGN, '-', "Sign with private key"},
47 {"verify", OPT_VERIFY, '-', "Verify with public key"},
48 {"encrypt", OPT_ENCRYPT, '-', "Encrypt with public key"},
49 {"decrypt", OPT_DECRYPT, '-', "Decrypt with private key"},
50 # ifndef OPENSSL_NO_ENGINE
51 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
55 {"in", OPT_IN, '<', "Input file"},
56 {"inkey", OPT_INKEY, 's', "Input key"},
57 {"keyform", OPT_KEYFORM, 'E', "Private key format - default PEM"},
58 {"pubin", OPT_PUBIN, '-', "Input is an RSA public"},
59 {"certin", OPT_CERTIN, '-', "Input is a cert carrying an RSA public key"},
60 {"rev", OPT_REV, '-', "Reverse the order of the input buffer"},
61 {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
63 OPT_SECTION("Output"),
64 {"out", OPT_OUT, '>', "Output file"},
65 {"ssl", OPT_SSL, '-', "Use SSL v2 padding"},
66 {"raw", OPT_RSA_RAW, '-', "Use no padding"},
67 {"pkcs", OPT_PKCS, '-', "Use PKCS#1 v1.5 padding (default)"},
68 {"x931", OPT_X931, '-', "Use ANSI X9.31 padding"},
69 {"oaep", OPT_OAEP, '-', "Use PKCS#1 OAEP"},
70 {"asn1parse", OPT_ASN1PARSE, '-',
71 "Run output through asn1parse; useful with -verify"},
72 {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"},
79 int rsautl_main(int argc, char **argv)
81 BIO *in = NULL, *out = NULL;
83 EVP_PKEY *pkey = NULL;
86 char *infile = NULL, *outfile = NULL, *keyfile = NULL;
87 char *passinarg = NULL, *passin = NULL, *prog;
88 char rsa_mode = RSA_VERIFY, key_type = KEY_PRIVKEY;
89 unsigned char *rsa_in = NULL, *rsa_out = NULL, pad = RSA_PKCS1_PADDING;
90 int rsa_inlen, keyformat = FORMAT_PEM, keysize, ret = 1;
91 int rsa_outlen = 0, hexdump = 0, asn1parse = 0, need_priv = 0, rev = 0;
94 prog = opt_init(argc, argv, rsautl_options);
95 while ((o = opt_next()) != OPT_EOF) {
100 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
103 opt_help(rsautl_options);
107 if (!opt_format(opt_arg(), OPT_FMT_PDE, &keyformat))
117 e = setup_engine(opt_arg(), 0);
126 pad = RSA_NO_PADDING;
129 pad = RSA_PKCS1_OAEP_PADDING;
132 pad = RSA_SSLV23_PADDING;
135 pad = RSA_PKCS1_PADDING;
138 pad = RSA_X931_PADDING;
145 rsa_mode = RSA_VERIFY;
151 rsa_mode = RSA_ENCRYPT;
154 rsa_mode = RSA_DECRYPT;
158 key_type = KEY_PUBKEY;
167 passinarg = opt_arg();
174 if (!opt_provider(o))
179 argc = opt_num_rest();
183 if (need_priv && (key_type != KEY_PRIVKEY)) {
184 BIO_printf(bio_err, "A private key is needed for this operation\n");
188 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
189 BIO_printf(bio_err, "Error getting password\n");
195 pkey = load_key(keyfile, keyformat, 0, passin, e, "Private Key");
199 pkey = load_pubkey(keyfile, keyformat, 0, NULL, e, "Public Key");
203 x = load_cert(keyfile, keyformat, "Certificate");
205 pkey = X509_get_pubkey(x);
214 rsa = EVP_PKEY_get1_RSA(pkey);
218 BIO_printf(bio_err, "Error getting RSA key\n");
219 ERR_print_errors(bio_err);
223 in = bio_open_default(infile, 'r', FORMAT_BINARY);
226 out = bio_open_default(outfile, 'w', FORMAT_BINARY);
230 keysize = RSA_size(rsa);
232 rsa_in = app_malloc(keysize * 2, "hold rsa key");
233 rsa_out = app_malloc(keysize, "output rsa key");
235 /* Read the input data */
236 rsa_inlen = BIO_read(in, rsa_in, keysize * 2);
238 BIO_printf(bio_err, "Error reading input Data\n");
244 for (i = 0; i < rsa_inlen / 2; i++) {
246 rsa_in[i] = rsa_in[rsa_inlen - 1 - i];
247 rsa_in[rsa_inlen - 1 - i] = ctmp;
253 rsa_outlen = RSA_public_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
258 RSA_private_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
262 rsa_outlen = RSA_public_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
267 RSA_private_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
271 if (rsa_outlen < 0) {
272 BIO_printf(bio_err, "RSA operation error\n");
273 ERR_print_errors(bio_err);
278 if (!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) {
279 ERR_print_errors(bio_err);
281 } else if (hexdump) {
282 BIO_dump(out, (char *)rsa_out, rsa_outlen);
284 BIO_write(out, rsa_out, rsa_outlen);
291 OPENSSL_free(rsa_in);
292 OPENSSL_free(rsa_out);
293 OPENSSL_free(passin);