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