a629ff50acddef1a63b53c9addb7168b69e67353
[openssl.git] / apps / rsautl.c
1 /* rsautl.c */
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3  * project 2000.
4  */
5 /* ====================================================================
6  * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
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
18  *    distribution.
19  *
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/)"
24  *
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.
29  *
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.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
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  * ====================================================================
52  *
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).
56  *
57  */
58
59 #ifndef OPENSSL_NO_RSA
60
61 #include "apps.h"
62 #include <string.h>
63 #include <openssl/err.h>
64 #include <openssl/pem.h>
65 #include <openssl/rsa.h>
66
67 #define RSA_SIGN        1
68 #define RSA_VERIFY      2
69 #define RSA_ENCRYPT     3
70 #define RSA_DECRYPT     4
71
72 #define KEY_PRIVKEY     1
73 #define KEY_PUBKEY      2
74 #define KEY_CERT        3
75
76 static void usage(void);
77
78 #undef PROG
79
80 #define PROG rsautl_main
81
82 int MAIN(int argc, char **);
83
84 int MAIN(int argc, char **argv)
85 {
86         ENGINE *e = NULL;
87         BIO *in = NULL, *out = NULL;
88         char *infile = NULL, *outfile = NULL;
89 #ifndef OPENSSL_NO_ENGINE
90         char *engine = NULL;
91 #endif
92         char *keyfile = NULL;
93         char rsa_mode = RSA_VERIFY, key_type = KEY_PRIVKEY;
94         int keyform = FORMAT_PEM;
95         char need_priv = 0, badarg = 0, rev = 0;
96         char hexdump = 0, asn1parse = 0;
97         X509 *x;
98         EVP_PKEY *pkey = NULL;
99         RSA *rsa = NULL;
100         unsigned char *rsa_in = NULL, *rsa_out = NULL, pad;
101         char *passargin = NULL, *passin = NULL;
102         int rsa_inlen, rsa_outlen = 0;
103         int keysize;
104
105         int ret = 1;
106
107         argc--;
108         argv++;
109
110         if(!bio_err) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
111
112         if (!load_config(bio_err, NULL))
113                 goto end;
114         ERR_load_crypto_strings();
115         OpenSSL_add_all_algorithms();
116         pad = RSA_PKCS1_PADDING;
117         
118         while(argc >= 1)
119         {
120                 if (!strcmp(*argv,"-in")) {
121                         if (--argc < 1) badarg = 1;
122                         infile= *(++argv);
123                 } else if (!strcmp(*argv,"-out")) {
124                         if (--argc < 1) badarg = 1;
125                         outfile= *(++argv);
126                 } else if(!strcmp(*argv, "-inkey")) {
127                         if (--argc < 1) badarg = 1;
128                         keyfile = *(++argv);
129                 } else if (!strcmp(*argv,"-passin")) {
130                         if (--argc < 1) badarg = 1;
131                         passargin= *(++argv);
132                 } else if (strcmp(*argv,"-keyform") == 0) {
133                         if (--argc < 1) badarg = 1;
134                         keyform=str2fmt(*(++argv));
135 #ifndef OPENSSL_NO_ENGINE
136                 } else if(!strcmp(*argv, "-engine")) {
137                         if (--argc < 1) badarg = 1;
138                         engine = *(++argv);
139 #endif
140                 } else if(!strcmp(*argv, "-pubin")) {
141                         key_type = KEY_PUBKEY;
142                 } else if(!strcmp(*argv, "-certin")) {
143                         key_type = KEY_CERT;
144                 } 
145                 else if(!strcmp(*argv, "-asn1parse")) asn1parse = 1;
146                 else if(!strcmp(*argv, "-hexdump")) hexdump = 1;
147                 else if(!strcmp(*argv, "-raw")) pad = RSA_NO_PADDING;
148                 else if(!strcmp(*argv, "-oaep")) pad = RSA_PKCS1_OAEP_PADDING;
149                 else if(!strcmp(*argv, "-ssl")) pad = RSA_SSLV23_PADDING;
150                 else if(!strcmp(*argv, "-pkcs")) pad = RSA_PKCS1_PADDING;
151                 else if(!strcmp(*argv, "-x931")) pad = RSA_X931_PADDING;
152                 else if(!strcmp(*argv, "-sign")) {
153                         rsa_mode = RSA_SIGN;
154                         need_priv = 1;
155                 } else if(!strcmp(*argv, "-verify")) rsa_mode = RSA_VERIFY;
156                 else if(!strcmp(*argv, "-rev")) rev = 1;
157                 else if(!strcmp(*argv, "-encrypt")) rsa_mode = RSA_ENCRYPT;
158                 else if(!strcmp(*argv, "-decrypt")) {
159                         rsa_mode = RSA_DECRYPT;
160                         need_priv = 1;
161                 } else badarg = 1;
162                 if(badarg) {
163                         usage();
164                         goto end;
165                 }
166                 argc--;
167                 argv++;
168         }
169
170         if(need_priv && (key_type != KEY_PRIVKEY)) {
171                 BIO_printf(bio_err, "A private key is needed for this operation\n");
172                 goto end;
173         }
174
175 #ifndef OPENSSL_NO_ENGINE
176         e = setup_engine(bio_err, engine, 0);
177 #endif
178         if(!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
179                 BIO_printf(bio_err, "Error getting password\n");
180                 goto end;
181         }
182
183 /* FIXME: seed PRNG only if needed */
184         app_RAND_load_file(NULL, bio_err, 0);
185         
186         switch(key_type) {
187                 case KEY_PRIVKEY:
188                 pkey = load_key(bio_err, keyfile, keyform, 0,
189                         passin, e, "Private Key");
190                 break;
191
192                 case KEY_PUBKEY:
193                 pkey = load_pubkey(bio_err, keyfile, keyform, 0,
194                         NULL, e, "Public Key");
195                 break;
196
197                 case KEY_CERT:
198                 x = load_cert(bio_err, keyfile, keyform,
199                         NULL, e, "Certificate");
200                 if(x) {
201                         pkey = X509_get_pubkey(x);
202                         X509_free(x);
203                 }
204                 break;
205         }
206
207         if(!pkey) {
208                 return 1;
209         }
210
211         rsa = EVP_PKEY_get1_RSA(pkey);
212         EVP_PKEY_free(pkey);
213
214         if(!rsa) {
215                 BIO_printf(bio_err, "Error getting RSA key\n");
216                 ERR_print_errors(bio_err);
217                 goto end;
218         }
219
220
221         if(infile) {
222                 if(!(in = BIO_new_file(infile, "rb"))) {
223                         BIO_printf(bio_err, "Error Reading Input File\n");
224                         ERR_print_errors(bio_err);      
225                         goto end;
226                 }
227         } else in = BIO_new_fp(stdin, BIO_NOCLOSE);
228
229         if(outfile) {
230                 if(!(out = BIO_new_file(outfile, "wb"))) {
231                         BIO_printf(bio_err, "Error Reading Output File\n");
232                         ERR_print_errors(bio_err);      
233                         goto end;
234                 }
235         } else {
236                 out = BIO_new_fp(stdout, BIO_NOCLOSE);
237 #ifdef OPENSSL_SYS_VMS
238                 {
239                     BIO *tmpbio = BIO_new(BIO_f_linebuffer());
240                     out = BIO_push(tmpbio, out);
241                 }
242 #endif
243         }
244
245         keysize = RSA_size(rsa);
246
247         rsa_in = OPENSSL_malloc(keysize * 2);
248         rsa_out = OPENSSL_malloc(keysize);
249
250         /* Read the input data */
251         rsa_inlen = BIO_read(in, rsa_in, keysize * 2);
252         if(rsa_inlen <= 0) {
253                 BIO_printf(bio_err, "Error reading input Data\n");
254                 exit(1);
255         }
256         if(rev) {
257                 int i;
258                 unsigned char ctmp;
259                 for(i = 0; i < rsa_inlen/2; i++) {
260                         ctmp = rsa_in[i];
261                         rsa_in[i] = rsa_in[rsa_inlen - 1 - i];
262                         rsa_in[rsa_inlen - 1 - i] = ctmp;
263                 }
264         }
265         switch(rsa_mode) {
266
267                 case RSA_VERIFY:
268                         rsa_outlen  = RSA_public_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
269                 break;
270
271                 case RSA_SIGN:
272                         rsa_outlen  = RSA_private_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
273                 break;
274
275                 case RSA_ENCRYPT:
276                         rsa_outlen  = RSA_public_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
277                 break;
278
279                 case RSA_DECRYPT:
280                         rsa_outlen  = RSA_private_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
281                 break;
282
283         }
284
285         if(rsa_outlen <= 0) {
286                 BIO_printf(bio_err, "RSA operation error\n");
287                 ERR_print_errors(bio_err);
288                 goto end;
289         }
290         ret = 0;
291         if(asn1parse) {
292                 if(!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) {
293                         ERR_print_errors(bio_err);
294                 }
295         } else if(hexdump) BIO_dump(out, (char *)rsa_out, rsa_outlen);
296         else BIO_write(out, rsa_out, rsa_outlen);
297         end:
298         RSA_free(rsa);
299         BIO_free(in);
300         BIO_free_all(out);
301         if(rsa_in) OPENSSL_free(rsa_in);
302         if(rsa_out) OPENSSL_free(rsa_out);
303         if(passin) OPENSSL_free(passin);
304         return ret;
305 }
306
307 static void usage()
308 {
309         BIO_printf(bio_err, "Usage: rsautl [options]\n");
310         BIO_printf(bio_err, "-in file        input file\n");
311         BIO_printf(bio_err, "-out file       output file\n");
312         BIO_printf(bio_err, "-inkey file     input key\n");
313         BIO_printf(bio_err, "-keyform arg    private key format - default PEM\n");
314         BIO_printf(bio_err, "-pubin          input is an RSA public\n");
315         BIO_printf(bio_err, "-certin         input is a certificate carrying an RSA public key\n");
316         BIO_printf(bio_err, "-ssl            use SSL v2 padding\n");
317         BIO_printf(bio_err, "-raw            use no padding\n");
318         BIO_printf(bio_err, "-pkcs           use PKCS#1 v1.5 padding (default)\n");
319         BIO_printf(bio_err, "-oaep           use PKCS#1 OAEP\n");
320         BIO_printf(bio_err, "-sign           sign with private key\n");
321         BIO_printf(bio_err, "-verify         verify with public key\n");
322         BIO_printf(bio_err, "-encrypt        encrypt with public key\n");
323         BIO_printf(bio_err, "-decrypt        decrypt with private key\n");
324         BIO_printf(bio_err, "-hexdump        hex dump output\n");
325 #ifndef OPENSSL_NO_ENGINE
326         BIO_printf(bio_err, "-engine e       use engine e, possibly a hardware device.\n");
327         BIO_printf (bio_err, "-passin arg    pass phrase source\n");
328 #endif
329
330 }
331
332 #endif