2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
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 #ifndef OPENSSL_NO_RSA
63 #include <openssl/err.h>
64 #include <openssl/pem.h>
75 static void usage(void);
79 #define PROG rsautl_main
81 int MAIN(int argc, char **);
83 int MAIN(int argc, char **argv)
86 BIO *in = NULL, *out = NULL;
87 char *infile = NULL, *outfile = NULL;
90 char rsa_mode = RSA_VERIFY, key_type = KEY_PRIVKEY;
91 int keyform = FORMAT_PEM;
92 char need_priv = 0, badarg = 0, rev = 0;
93 char hexdump = 0, asn1parse = 0;
95 EVP_PKEY *pkey = NULL;
97 unsigned char *rsa_in = NULL, *rsa_out = NULL, pad;
98 int rsa_inlen, rsa_outlen = 0;
106 if(!bio_err) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
108 if (!load_config(bio_err, NULL))
110 ERR_load_crypto_strings();
111 OpenSSL_add_all_algorithms();
112 pad = RSA_PKCS1_PADDING;
116 if (!strcmp(*argv,"-in")) {
117 if (--argc < 1) badarg = 1;
119 } else if (!strcmp(*argv,"-out")) {
120 if (--argc < 1) badarg = 1;
122 } else if(!strcmp(*argv, "-inkey")) {
123 if (--argc < 1) badarg = 1;
125 } else if (strcmp(*argv,"-keyform") == 0) {
126 if (--argc < 1) badarg = 1;
127 keyform=str2fmt(*(++argv));
128 } else if(!strcmp(*argv, "-engine")) {
129 if (--argc < 1) badarg = 1;
131 } else if(!strcmp(*argv, "-pubin")) {
132 key_type = KEY_PUBKEY;
133 } else if(!strcmp(*argv, "-certin")) {
136 else if(!strcmp(*argv, "-asn1parse")) asn1parse = 1;
137 else if(!strcmp(*argv, "-hexdump")) hexdump = 1;
138 else if(!strcmp(*argv, "-raw")) pad = RSA_NO_PADDING;
139 else if(!strcmp(*argv, "-oaep")) pad = RSA_PKCS1_OAEP_PADDING;
140 else if(!strcmp(*argv, "-ssl")) pad = RSA_SSLV23_PADDING;
141 else if(!strcmp(*argv, "-pkcs")) pad = RSA_PKCS1_PADDING;
142 else if(!strcmp(*argv, "-sign")) {
145 } else if(!strcmp(*argv, "-verify")) rsa_mode = RSA_VERIFY;
146 else if(!strcmp(*argv, "-rev")) rev = 1;
147 else if(!strcmp(*argv, "-encrypt")) rsa_mode = RSA_ENCRYPT;
148 else if(!strcmp(*argv, "-decrypt")) {
149 rsa_mode = RSA_DECRYPT;
160 if(need_priv && (key_type != KEY_PRIVKEY)) {
161 BIO_printf(bio_err, "A private key is needed for this operation\n");
165 e = setup_engine(bio_err, engine, 0);
167 /* FIXME: seed PRNG only if needed */
168 app_RAND_load_file(NULL, bio_err, 0);
172 pkey = load_key(bio_err, keyfile, keyform, 0,
173 NULL, e, "Private Key");
177 pkey = load_pubkey(bio_err, keyfile, keyform, 0,
178 NULL, e, "Public Key");
182 x = load_cert(bio_err, keyfile, keyform,
183 NULL, e, "Certificate");
185 pkey = X509_get_pubkey(x);
195 rsa = EVP_PKEY_get1_RSA(pkey);
199 BIO_printf(bio_err, "Error getting RSA key\n");
200 ERR_print_errors(bio_err);
206 if(!(in = BIO_new_file(infile, "rb"))) {
207 BIO_printf(bio_err, "Error Reading Input File\n");
208 ERR_print_errors(bio_err);
211 } else in = BIO_new_fp(stdin, BIO_NOCLOSE);
214 if(!(out = BIO_new_file(outfile, "wb"))) {
215 BIO_printf(bio_err, "Error Reading Output File\n");
216 ERR_print_errors(bio_err);
220 out = BIO_new_fp(stdout, BIO_NOCLOSE);
221 #ifdef OPENSSL_SYS_VMS
223 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
224 out = BIO_push(tmpbio, out);
229 keysize = RSA_size(rsa);
231 rsa_in = OPENSSL_malloc(keysize * 2);
232 rsa_out = OPENSSL_malloc(keysize);
234 /* Read the input data */
235 rsa_inlen = BIO_read(in, rsa_in, keysize * 2);
237 BIO_printf(bio_err, "Error reading input Data\n");
243 for(i = 0; i < rsa_inlen/2; i++) {
245 rsa_in[i] = rsa_in[rsa_inlen - 1 - i];
246 rsa_in[rsa_inlen - 1 - i] = ctmp;
252 rsa_outlen = RSA_public_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
256 rsa_outlen = RSA_private_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
260 rsa_outlen = RSA_public_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
264 rsa_outlen = RSA_private_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
269 if(rsa_outlen <= 0) {
270 BIO_printf(bio_err, "RSA operation error\n");
271 ERR_print_errors(bio_err);
276 if(!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) {
277 ERR_print_errors(bio_err);
279 } else if(hexdump) BIO_dump(out, (char *)rsa_out, rsa_outlen);
280 else BIO_write(out, rsa_out, rsa_outlen);
285 if(rsa_in) OPENSSL_free(rsa_in);
286 if(rsa_out) OPENSSL_free(rsa_out);
292 BIO_printf(bio_err, "Usage: rsautl [options]\n");
293 BIO_printf(bio_err, "-in file input file\n");
294 BIO_printf(bio_err, "-out file output file\n");
295 BIO_printf(bio_err, "-inkey file input key\n");
296 BIO_printf(bio_err, "-keyform arg private key format - default PEM\n");
297 BIO_printf(bio_err, "-pubin input is an RSA public\n");
298 BIO_printf(bio_err, "-certin input is a certificate carrying an RSA public key\n");
299 BIO_printf(bio_err, "-ssl use SSL v2 padding\n");
300 BIO_printf(bio_err, "-raw use no padding\n");
301 BIO_printf(bio_err, "-pkcs use PKCS#1 v1.5 padding (default)\n");
302 BIO_printf(bio_err, "-oaep use PKCS#1 OAEP\n");
303 BIO_printf(bio_err, "-sign sign with private key\n");
304 BIO_printf(bio_err, "-verify verify with public key\n");
305 BIO_printf(bio_err, "-encrypt encrypt with public key\n");
306 BIO_printf(bio_err, "-decrypt decrypt with private key\n");
307 BIO_printf(bio_err, "-hexdump hex dump output\n");
308 BIO_printf(bio_err, "-engine e use engine e, possibly a hardware device.\n");