]> git.ipfire.org Git - thirdparty/openssl.git/blob - apps/rsautl.c
ba31979618760acdde4232b7bad44cf865146bdb
[thirdparty/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/engine.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 char *engine = NULL;
90 char *keyfile = NULL;
91 char rsa_mode = RSA_VERIFY, key_type = KEY_PRIVKEY;
92 int keyform = FORMAT_PEM;
93 char need_priv = 0, badarg = 0, rev = 0;
94 char hexdump = 0, asn1parse = 0;
95 X509 *x;
96 EVP_PKEY *pkey = NULL;
97 RSA *rsa = NULL;
98 unsigned char *rsa_in = NULL, *rsa_out = NULL, pad;
99 int rsa_inlen, rsa_outlen = 0;
100 int keysize;
101
102 int ret = 1;
103
104 argc--;
105 argv++;
106
107 if(!bio_err) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
108 ERR_load_crypto_strings();
109 OpenSSL_add_all_algorithms();
110 pad = RSA_PKCS1_PADDING;
111
112 while(argc >= 1)
113 {
114 if (!strcmp(*argv,"-in")) {
115 if (--argc < 1) badarg = 1;
116 infile= *(++argv);
117 } else if (!strcmp(*argv,"-out")) {
118 if (--argc < 1) badarg = 1;
119 outfile= *(++argv);
120 } else if(!strcmp(*argv, "-inkey")) {
121 if (--argc < 1) badarg = 1;
122 keyfile = *(++argv);
123 } else if(!strcmp(*argv, "-engine")) {
124 if (--argc < 1) badarg = 1;
125 engine = *(++argv);
126 } else if(!strcmp(*argv, "-pubin")) {
127 key_type = KEY_PUBKEY;
128 } else if(!strcmp(*argv, "-certin")) {
129 key_type = KEY_CERT;
130 }
131 else if(!strcmp(*argv, "-asn1parse")) asn1parse = 1;
132 else if(!strcmp(*argv, "-hexdump")) hexdump = 1;
133 else if(!strcmp(*argv, "-raw")) pad = RSA_NO_PADDING;
134 else if(!strcmp(*argv, "-oaep")) pad = RSA_PKCS1_OAEP_PADDING;
135 else if(!strcmp(*argv, "-ssl")) pad = RSA_SSLV23_PADDING;
136 else if(!strcmp(*argv, "-pkcs")) pad = RSA_PKCS1_PADDING;
137 else if(!strcmp(*argv, "-sign")) {
138 rsa_mode = RSA_SIGN;
139 need_priv = 1;
140 } else if(!strcmp(*argv, "-verify")) rsa_mode = RSA_VERIFY;
141 else if(!strcmp(*argv, "-rev")) rev = 1;
142 else if(!strcmp(*argv, "-encrypt")) rsa_mode = RSA_ENCRYPT;
143 else if(!strcmp(*argv, "-decrypt")) {
144 rsa_mode = RSA_DECRYPT;
145 need_priv = 1;
146 } else badarg = 1;
147 if(badarg) {
148 usage();
149 goto end;
150 }
151 argc--;
152 argv++;
153 }
154
155 if(need_priv && (key_type != KEY_PRIVKEY)) {
156 BIO_printf(bio_err, "A private key is needed for this operation\n");
157 goto end;
158 }
159
160 if (engine != NULL)
161 {
162 if((e = ENGINE_by_id(engine)) == NULL)
163 {
164 BIO_printf(bio_err,"invalid engine \"%s\"\n",
165 engine);
166 goto end;
167 }
168 if(!ENGINE_set_default(e, ENGINE_METHOD_ALL))
169 {
170 BIO_printf(bio_err,"can't use that engine\n");
171 goto end;
172 }
173 BIO_printf(bio_err,"engine \"%s\" set.\n", engine);
174 /* Free our "structural" reference. */
175 ENGINE_free(e);
176 }
177
178 /* FIXME: seed PRNG only if needed */
179 app_RAND_load_file(NULL, bio_err, 0);
180
181 switch(key_type) {
182 case KEY_PRIVKEY:
183 pkey = load_key(bio_err, keyfile, keyform, NULL, e);
184 break;
185
186 case KEY_PUBKEY:
187 pkey = load_pubkey(bio_err, keyfile, keyform, e);
188 break;
189
190 case KEY_CERT:
191 x = load_cert(bio_err, keyfile, keyform);
192 if(x) {
193 pkey = X509_get_pubkey(x);
194 X509_free(x);
195 }
196 break;
197 }
198
199 if(!pkey) {
200 BIO_printf(bio_err, "Error loading key\n");
201 return 1;
202 }
203
204 rsa = EVP_PKEY_get1_RSA(pkey);
205 EVP_PKEY_free(pkey);
206
207 if(!rsa) {
208 BIO_printf(bio_err, "Error getting RSA key\n");
209 ERR_print_errors(bio_err);
210 goto end;
211 }
212
213
214 if(infile) {
215 if(!(in = BIO_new_file(infile, "rb"))) {
216 BIO_printf(bio_err, "Error Reading Input File\n");
217 ERR_print_errors(bio_err);
218 goto end;
219 }
220 } else in = BIO_new_fp(stdin, BIO_NOCLOSE);
221
222 if(outfile) {
223 if(!(out = BIO_new_file(outfile, "wb"))) {
224 BIO_printf(bio_err, "Error Reading Output File\n");
225 ERR_print_errors(bio_err);
226 goto end;
227 }
228 } else {
229 out = BIO_new_fp(stdout, BIO_NOCLOSE);
230 #ifdef VMS
231 {
232 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
233 out = BIO_push(tmpbio, out);
234 }
235 #endif
236 }
237
238 keysize = RSA_size(rsa);
239
240 rsa_in = OPENSSL_malloc(keysize * 2);
241 rsa_out = OPENSSL_malloc(keysize);
242
243 /* Read the input data */
244 rsa_inlen = BIO_read(in, rsa_in, keysize * 2);
245 if(rsa_inlen <= 0) {
246 BIO_printf(bio_err, "Error reading input Data\n");
247 exit(1);
248 }
249 if(rev) {
250 int i;
251 unsigned char ctmp;
252 for(i = 0; i < rsa_inlen/2; i++) {
253 ctmp = rsa_in[i];
254 rsa_in[i] = rsa_in[rsa_inlen - 1 - i];
255 rsa_in[rsa_inlen - 1 - i] = ctmp;
256 }
257 }
258 switch(rsa_mode) {
259
260 case RSA_VERIFY:
261 rsa_outlen = RSA_public_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
262 break;
263
264 case RSA_SIGN:
265 rsa_outlen = RSA_private_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
266 break;
267
268 case RSA_ENCRYPT:
269 rsa_outlen = RSA_public_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
270 break;
271
272 case RSA_DECRYPT:
273 rsa_outlen = RSA_private_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
274 break;
275
276 }
277
278 if(rsa_outlen <= 0) {
279 BIO_printf(bio_err, "RSA operation error\n");
280 ERR_print_errors(bio_err);
281 goto end;
282 }
283 ret = 0;
284 if(asn1parse) {
285 if(!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) {
286 ERR_print_errors(bio_err);
287 }
288 } else if(hexdump) BIO_dump(out, (char *)rsa_out, rsa_outlen);
289 else BIO_write(out, rsa_out, rsa_outlen);
290 end:
291 RSA_free(rsa);
292 BIO_free(in);
293 BIO_free_all(out);
294 if(rsa_in) OPENSSL_free(rsa_in);
295 if(rsa_out) OPENSSL_free(rsa_out);
296 return ret;
297 }
298
299 static void usage()
300 {
301 BIO_printf(bio_err, "Usage: rsautl [options]\n");
302 BIO_printf(bio_err, "-in file input file\n");
303 BIO_printf(bio_err, "-out file output file\n");
304 BIO_printf(bio_err, "-inkey file input key\n");
305 BIO_printf(bio_err, "-pubin input is an RSA public\n");
306 BIO_printf(bio_err, "-certin input is a certificate carrying an RSA public key\n");
307 BIO_printf(bio_err, "-ssl use SSL v2 padding\n");
308 BIO_printf(bio_err, "-raw use no padding\n");
309 BIO_printf(bio_err, "-pkcs use PKCS#1 v1.5 padding (default)\n");
310 BIO_printf(bio_err, "-oaep use PKCS#1 OAEP\n");
311 BIO_printf(bio_err, "-sign sign with private key\n");
312 BIO_printf(bio_err, "-verify verify with public key\n");
313 BIO_printf(bio_err, "-encrypt encrypt with public key\n");
314 BIO_printf(bio_err, "-decrypt decrypt with private key\n");
315 BIO_printf(bio_err, "-hexdump hex dump output\n");
316 }
317
318 #endif