f8f68d9422a15616ad7b6821d0485d245d55b393
[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 #include "apps.h"
59 #include <openssl/err.h>
60 #include <openssl/pem.h>
61
62 #define RSA_SIGN        1
63 #define RSA_VERIFY      2
64 #define RSA_ENCRYPT     3
65 #define RSA_DECRYPT     4
66
67 #define KEY_PRIVKEY     1
68 #define KEY_PUBKEY      2
69 #define KEY_CERT        3
70
71 static void usage(void);
72
73 #undef PROG
74
75 #define PROG rsautl_main
76
77 int MAIN(int argc, char **);
78
79 int MAIN(int argc, char **argv)
80 {
81         BIO *in = NULL, *out = NULL;
82         char *infile = NULL, *outfile = NULL;
83         char *keyfile = NULL;
84         char rsa_mode = RSA_VERIFY, key_type = KEY_PRIVKEY;
85         int keyform = FORMAT_PEM;
86         char need_priv = 0, badarg = 0, rev = 0;
87         char hexdump = 0, asn1parse = 0;
88         X509 *x;
89         EVP_PKEY *pkey = NULL;
90         RSA *rsa = NULL;
91         unsigned char *rsa_in = NULL, *rsa_out = NULL, pad;
92         int rsa_inlen, rsa_outlen = 0;
93         int keysize;
94
95         int ret = 1;
96
97         argc--;
98         argv++;
99
100         if(!bio_err) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
101         ERR_load_crypto_strings();
102         OpenSSL_add_all_algorithms();
103         pad = RSA_PKCS1_PADDING;
104         
105         while(argc >= 1)
106         {
107                 if (!strcmp(*argv,"-in")) {
108                         if (--argc < 1) badarg = 1;
109                         infile= *(++argv);
110                 } else if (!strcmp(*argv,"-out")) {
111                         if (--argc < 1) badarg = 1;
112                         outfile= *(++argv);
113                 } else if(!strcmp(*argv, "-inkey")) {
114                         if (--argc < 1) badarg = 1;
115                         keyfile = *(++argv);
116                 } else if(!strcmp(*argv, "-pubin")) {
117                         key_type = KEY_PUBKEY;
118                 } else if(!strcmp(*argv, "-certin")) {
119                         key_type = KEY_CERT;
120                 } 
121                 else if(!strcmp(*argv, "-asn1parse")) asn1parse = 1;
122                 else if(!strcmp(*argv, "-hexdump")) hexdump = 1;
123                 else if(!strcmp(*argv, "-raw")) pad = RSA_NO_PADDING;
124                 else if(!strcmp(*argv, "-ssl")) pad = RSA_SSLV23_PADDING;
125                 else if(!strcmp(*argv, "-pkcs")) pad = RSA_PKCS1_PADDING;
126                 else if(!strcmp(*argv, "-sign")) {
127                         rsa_mode = RSA_SIGN;
128                         need_priv = 1;
129                 } else if(!strcmp(*argv, "-verify")) rsa_mode = RSA_VERIFY;
130                 else if(!strcmp(*argv, "-rev")) rev = 1;
131                 else if(!strcmp(*argv, "-encrypt")) rsa_mode = RSA_ENCRYPT;
132                 else if(!strcmp(*argv, "-decrypt")) {
133                         rsa_mode = RSA_DECRYPT;
134                         need_priv = 1;
135                 } else badarg = 1;
136                 if(badarg) {
137                         usage();
138                         goto end;
139                 }
140                 argc--;
141                 argv++;
142         }
143
144         if(need_priv && (key_type != KEY_PRIVKEY)) {
145                 BIO_printf(bio_err, "A private key is needed for this operation\n");
146                 goto end;
147         }
148
149         switch(key_type) {
150                 case KEY_PRIVKEY:
151                 pkey = load_key(bio_err, keyfile, keyform, NULL);
152                 break;
153
154                 case KEY_PUBKEY:
155                 pkey = load_pubkey(bio_err, keyfile, keyform);
156                 break;
157
158                 case KEY_CERT:
159                 x = load_cert(bio_err, keyfile, keyform);
160                 if(x) {
161                         pkey = X509_get_pubkey(x);
162                         X509_free(x);
163                 }
164                 break;
165         }
166
167         if(!pkey) {
168                 BIO_printf(bio_err, "Error loading key\n");
169                 return 1;
170         }
171
172         rsa = EVP_PKEY_get1_RSA(pkey);
173         EVP_PKEY_free(pkey);
174
175         if(!rsa) {
176                 BIO_printf(bio_err, "Error getting RSA key\n");
177                 ERR_print_errors(bio_err);
178                 goto end;
179         }
180
181
182         if(infile) {
183                 if(!(in = BIO_new_file(infile, "rb"))) {
184                         BIO_printf(bio_err, "Error Reading Input File\n");
185                         ERR_print_errors(bio_err);      
186                         goto end;
187                 }
188         } else in = BIO_new_fp(stdin, BIO_NOCLOSE);
189
190         if(outfile) {
191                 if(!(out = BIO_new_file(outfile, "wb"))) {
192                         BIO_printf(bio_err, "Error Reading Output File\n");
193                         ERR_print_errors(bio_err);      
194                         goto end;
195                 }
196         } else out = BIO_new_fp(stdout, BIO_NOCLOSE);
197
198         keysize = RSA_size(rsa);
199
200         rsa_in = OPENSSL_malloc(keysize * 2);
201         rsa_out = OPENSSL_malloc(keysize);
202
203         /* Read the input data */
204         rsa_inlen = BIO_read(in, rsa_in, keysize * 2);
205         if(rsa_inlen <= 0) {
206                 BIO_printf(bio_err, "Error reading input Data\n");
207                 exit(1);
208         }
209         if(rev) {
210                 int i;
211                 unsigned char ctmp;
212                 for(i = 0; i < rsa_inlen/2; i++) {
213                         ctmp = rsa_in[i];
214                         rsa_in[i] = rsa_in[rsa_inlen - 1 - i];
215                         rsa_in[rsa_inlen - 1 - i] = ctmp;
216                 }
217         }
218         switch(rsa_mode) {
219
220                 case RSA_VERIFY:
221                         rsa_outlen  = RSA_public_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
222                 break;
223
224                 case RSA_SIGN:
225                         rsa_outlen  = RSA_private_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
226                 break;
227
228                 case RSA_ENCRYPT:
229                         rsa_outlen  = RSA_public_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
230                 break;
231
232                 case RSA_DECRYPT:
233                         rsa_outlen  = RSA_private_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
234                 break;
235
236         }
237
238         if(rsa_outlen <= 0) {
239                 BIO_printf(bio_err, "RSA operation error\n");
240                 ERR_print_errors(bio_err);
241                 goto end;
242         }
243         ret = 0;
244         if(asn1parse) {
245                 if(!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) {
246                         ERR_print_errors(bio_err);
247                 }
248         } else if(hexdump) BIO_dump(out, (char *)rsa_out, rsa_outlen);
249         else BIO_write(out, rsa_out, rsa_outlen);
250         end:
251         RSA_free(rsa);
252         BIO_free(in);
253         BIO_free(out);
254         if(rsa_in) OPENSSL_free(rsa_in);
255         if(rsa_out) OPENSSL_free(rsa_out);
256         return ret;
257 }
258
259 static void usage()
260 {
261         BIO_printf(bio_err, "Usage: rsautl [options]\n");
262         BIO_printf(bio_err, "-in file        input file\n");
263         BIO_printf(bio_err, "-out file       output file\n");
264         BIO_printf(bio_err, "-inkey file     input key\n");
265         BIO_printf(bio_err, "-pubin          input is an RSA public\n");
266         BIO_printf(bio_err, "-certin         input is a certificate carrying an RSA public key\n");
267         BIO_printf(bio_err, "-ssl            use SSL v2 padding\n");
268         BIO_printf(bio_err, "-raw            use no padding\n");
269         BIO_printf(bio_err, "-pkcs           use PKCS#1 padding (default)\n");
270         BIO_printf(bio_err, "-sign           sign with private key\n");
271         BIO_printf(bio_err, "-verify         verify with public key\n");
272         BIO_printf(bio_err, "-encrypt        encrypt with public key\n");
273         BIO_printf(bio_err, "-decrypt        decrypt with private key\n");
274         BIO_printf(bio_err, "-hexdump        hex dump output\n");
275 }
276