2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
5 /* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
59 #include <openssl/opensslconf.h>
60 #ifndef OPENSSL_NO_RSA
64 # include <openssl/err.h>
65 # include <openssl/pem.h>
66 # include <openssl/rsa.h>
70 # define RSA_ENCRYPT 3
71 # define RSA_DECRYPT 4
73 # define KEY_PRIVKEY 1
77 typedef enum OPTION_choice {
78 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
79 OPT_ENGINE, OPT_IN, OPT_OUT, OPT_ASN1PARSE, OPT_HEXDUMP,
80 OPT_RAW, OPT_OAEP, OPT_SSL, OPT_PKCS, OPT_X931,
81 OPT_SIGN, OPT_VERIFY, OPT_REV, OPT_ENCRYPT, OPT_DECRYPT,
82 OPT_PUBIN, OPT_CERTIN, OPT_INKEY, OPT_PASSIN, OPT_KEYFORM
85 OPTIONS rsautl_options[] = {
86 {"help", OPT_HELP, '-', "Display this summary"},
87 {"in", OPT_IN, '<', "Input file"},
88 {"out", OPT_OUT, '>', "Output file"},
89 {"inkey", OPT_INKEY, '<', "Input key"},
90 {"keyform", OPT_KEYFORM, 'F', "Private key format - default PEM"},
91 {"pubin", OPT_PUBIN, '-', "Input is an RSA public"},
92 {"certin", OPT_CERTIN, '-', "Input is a cert carrying an RSA public key"},
93 {"ssl", OPT_SSL, '-', "Use SSL v2 padding"},
94 {"raw", OPT_RAW, '-', "Use no padding"},
95 {"pkcs", OPT_PKCS, '-', "Use PKCS#1 v1.5 padding (default)"},
96 {"oaep", OPT_OAEP, '-', "Use PKCS#1 OAEP"},
97 {"sign", OPT_SIGN, '-', "Sign with private key"},
98 {"verify", OPT_VERIFY, '-', "Verify with public key"},
99 {"asn1parse", OPT_ASN1PARSE, '-'},
100 {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"},
101 {"x931", OPT_X931, '-', "Use ANSI X9.31 padding"},
102 {"rev", OPT_REV, '-'},
103 {"encrypt", OPT_ENCRYPT, '-', "Encrypt with public key"},
104 {"decrypt", OPT_DECRYPT, '-', "Decrypt with private key"},
105 {"passin", OPT_PASSIN, 's', "Pass phrase source"},
106 # ifndef OPENSSL_NO_ENGINE
107 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
112 int rsautl_main(int argc, char **argv)
114 BIO *in = NULL, *out = NULL;
116 EVP_PKEY *pkey = NULL;
119 char *infile = NULL, *outfile = NULL, *keyfile = NULL;
120 char *passinarg = NULL, *passin = NULL, *prog;
121 char rsa_mode = RSA_VERIFY, key_type = KEY_PRIVKEY;
122 unsigned char *rsa_in = NULL, *rsa_out = NULL, pad = RSA_PKCS1_PADDING;
123 int rsa_inlen, keyformat = FORMAT_PEM, keysize, ret = 1;
124 int rsa_outlen = 0, hexdump = 0, asn1parse = 0, need_priv = 0, rev = 0;
127 prog = opt_init(argc, argv, rsautl_options);
128 while ((o = opt_next()) != OPT_EOF) {
133 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
136 opt_help(rsautl_options);
140 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &keyformat))
150 e = setup_engine(opt_arg(), 0);
159 pad = RSA_NO_PADDING;
162 pad = RSA_PKCS1_OAEP_PADDING;
165 pad = RSA_SSLV23_PADDING;
168 pad = RSA_PKCS1_PADDING;
171 pad = RSA_X931_PADDING;
178 rsa_mode = RSA_VERIFY;
184 rsa_mode = RSA_ENCRYPT;
187 rsa_mode = RSA_DECRYPT;
191 key_type = KEY_PUBKEY;
200 passinarg = opt_arg();
204 argc = opt_num_rest();
207 if (need_priv && (key_type != KEY_PRIVKEY)) {
208 BIO_printf(bio_err, "A private key is needed for this operation\n");
212 if (!app_passwd(passinarg, NULL, &passin, NULL)) {
213 BIO_printf(bio_err, "Error getting password\n");
217 if (!app_load_modules(NULL))
220 /* FIXME: seed PRNG only if needed */
221 app_RAND_load_file(NULL, 0);
225 pkey = load_key(keyfile, keyformat, 0, passin, e, "Private Key");
229 pkey = load_pubkey(keyfile, keyformat, 0, NULL, e, "Public Key");
233 x = load_cert(keyfile, keyformat, NULL, e, "Certificate");
235 pkey = X509_get_pubkey(x);
245 rsa = EVP_PKEY_get1_RSA(pkey);
249 BIO_printf(bio_err, "Error getting RSA key\n");
250 ERR_print_errors(bio_err);
254 in = bio_open_default(infile, 'r', FORMAT_BINARY);
257 out = bio_open_default(outfile, 'w', FORMAT_BINARY);
261 keysize = RSA_size(rsa);
263 rsa_in = app_malloc(keysize * 2, "hold rsa key");
264 rsa_out = app_malloc(keysize, "output rsa key");
266 /* Read the input data */
267 rsa_inlen = BIO_read(in, rsa_in, keysize * 2);
268 if (rsa_inlen <= 0) {
269 BIO_printf(bio_err, "Error reading input Data\n");
275 for (i = 0; i < rsa_inlen / 2; i++) {
277 rsa_in[i] = rsa_in[rsa_inlen - 1 - i];
278 rsa_in[rsa_inlen - 1 - i] = ctmp;
284 rsa_outlen = RSA_public_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
289 RSA_private_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
293 rsa_outlen = RSA_public_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
298 RSA_private_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
303 if (rsa_outlen <= 0) {
304 BIO_printf(bio_err, "RSA operation error\n");
305 ERR_print_errors(bio_err);
310 if (!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) {
311 ERR_print_errors(bio_err);
314 BIO_dump(out, (char *)rsa_out, rsa_outlen);
316 BIO_write(out, rsa_out, rsa_outlen);
321 OPENSSL_free(rsa_in);
322 OPENSSL_free(rsa_out);
323 OPENSSL_free(passin);
327 #else /* !OPENSSL_NO_RSA */
330 static void *dummy = &dummy;