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