]>
Commit | Line | Data |
---|---|---|
0f113f3e MC |
1 | /* |
2 | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project | |
3 | * 2000. | |
bd08a2bd DSH |
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 | |
0f113f3e | 13 | * notice, this list of conditions and the following disclaimer. |
bd08a2bd DSH |
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 | |
0f113f3e MC |
62 | # include "apps.h" |
63 | # include <string.h> | |
64 | # include <openssl/err.h> | |
65 | # include <openssl/pem.h> | |
66 | # include <openssl/rsa.h> | |
bd08a2bd | 67 | |
0f113f3e MC |
68 | # define RSA_SIGN 1 |
69 | # define RSA_VERIFY 2 | |
70 | # define RSA_ENCRYPT 3 | |
71 | # define RSA_DECRYPT 4 | |
bd08a2bd | 72 | |
0f113f3e MC |
73 | # define KEY_PRIVKEY 1 |
74 | # define KEY_PUBKEY 2 | |
75 | # define KEY_CERT 3 | |
bd08a2bd | 76 | |
7e1b7485 RS |
77 | typedef enum OPTION_choice { |
78 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
79 | OPT_ENGINE, OPT_IN, OPT_OUT, OPT_ASN1PARSE, OPT_HEXDUMP, | |
80 | OPT_RAW, OPT_OAEP, OPT_SSL, OPT_PKCS, OPT_X931, | |
81 | OPT_SIGN, OPT_VERIFY, OPT_REV, OPT_ENCRYPT, OPT_DECRYPT, | |
82 | OPT_PUBIN, OPT_CERTIN, OPT_INKEY, OPT_PASSIN, OPT_KEYFORM | |
83 | } OPTION_CHOICE; | |
84 | ||
85 | OPTIONS rsautl_options[] = { | |
86 | {"help", OPT_HELP, '-', "Display this summary"}, | |
87 | {"in", OPT_IN, '<', "Input file"}, | |
88 | {"out", OPT_OUT, '>', "Output file"}, | |
89 | {"inkey", OPT_INKEY, '<', "Input key"}, | |
90 | {"keyform", OPT_KEYFORM, 'F', "Private key format - default PEM"}, | |
91 | {"pubin", OPT_PUBIN, '-', "Input is an RSA public"}, | |
92 | {"certin", OPT_CERTIN, '-', "Input is a cert carrying an RSA public key"}, | |
93 | {"ssl", OPT_SSL, '-', "Use SSL v2 padding"}, | |
94 | {"raw", OPT_RAW, '-', "Use no padding"}, | |
95 | {"pkcs", OPT_PKCS, '-', "Use PKCS#1 v1.5 padding (default)"}, | |
96 | {"oaep", OPT_OAEP, '-', "Use PKCS#1 OAEP"}, | |
97 | {"sign", OPT_SIGN, '-', "Sign with private key"}, | |
98 | {"verify", OPT_VERIFY, '-', "Verify with public key"}, | |
99 | {"asn1parse", OPT_ASN1PARSE, '-'}, | |
100 | {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"}, | |
101 | {"x931", OPT_X931, '-', "Use ANSI X9.31 padding"}, | |
102 | {"rev", OPT_REV, '-'}, | |
103 | {"encrypt", OPT_ENCRYPT, '-', "Encrypt with public key"}, | |
104 | {"decrypt", OPT_DECRYPT, '-', "Decrypt with private key"}, | |
105 | {"passin", OPT_PASSIN, 's', "Pass phrase source"}, | |
106 | # ifndef OPENSSL_NO_ENGINE | |
107 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, | |
108 | # endif | |
109 | {NULL} | |
110 | }; | |
bd08a2bd | 111 | |
7e1b7485 | 112 | int rsautl_main(int argc, char **argv) |
bd08a2bd | 113 | { |
0f113f3e | 114 | BIO *in = NULL, *out = NULL; |
7e1b7485 | 115 | ENGINE *e = NULL; |
0f113f3e MC |
116 | EVP_PKEY *pkey = NULL; |
117 | RSA *rsa = NULL; | |
7e1b7485 RS |
118 | X509 *x; |
119 | char *engine = NULL, *infile = NULL, *outfile = NULL, *keyfile = NULL; | |
120 | char *passinarg = NULL, *passin = NULL, *prog; | |
121 | char rsa_mode = RSA_VERIFY, key_type = KEY_PRIVKEY; | |
122 | unsigned char *rsa_in = NULL, *rsa_out = NULL, pad = RSA_PKCS1_PADDING; | |
123 | int rsa_inlen, keyformat = FORMAT_PEM, keysize, ret = 1; | |
124 | int rsa_outlen = 0, hexdump = 0, asn1parse = 0, need_priv = 0, rev = 0; | |
125 | OPTION_CHOICE o; | |
126 | ||
127 | prog = opt_init(argc, argv, rsautl_options); | |
128 | while ((o = opt_next()) != OPT_EOF) { | |
129 | switch (o) { | |
130 | case OPT_EOF: | |
131 | case OPT_ERR: | |
132 | opthelp: | |
133 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
134 | goto end; | |
135 | case OPT_HELP: | |
136 | opt_help(rsautl_options); | |
137 | ret = 0; | |
138 | goto end; | |
139 | case OPT_KEYFORM: | |
140 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &keyformat)) | |
141 | goto opthelp; | |
142 | break; | |
143 | case OPT_IN: | |
144 | infile = opt_arg(); | |
145 | break; | |
146 | case OPT_OUT: | |
147 | outfile = opt_arg(); | |
148 | break; | |
149 | case OPT_ENGINE: | |
150 | engine = opt_arg(); | |
151 | break; | |
152 | case OPT_ASN1PARSE: | |
0f113f3e | 153 | asn1parse = 1; |
7e1b7485 RS |
154 | break; |
155 | case OPT_HEXDUMP: | |
0f113f3e | 156 | hexdump = 1; |
7e1b7485 RS |
157 | break; |
158 | case OPT_RAW: | |
0f113f3e | 159 | pad = RSA_NO_PADDING; |
7e1b7485 RS |
160 | break; |
161 | case OPT_OAEP: | |
0f113f3e | 162 | pad = RSA_PKCS1_OAEP_PADDING; |
7e1b7485 RS |
163 | break; |
164 | case OPT_SSL: | |
0f113f3e | 165 | pad = RSA_SSLV23_PADDING; |
7e1b7485 RS |
166 | break; |
167 | case OPT_PKCS: | |
0f113f3e | 168 | pad = RSA_PKCS1_PADDING; |
7e1b7485 RS |
169 | break; |
170 | case OPT_X931: | |
0f113f3e | 171 | pad = RSA_X931_PADDING; |
7e1b7485 RS |
172 | break; |
173 | case OPT_SIGN: | |
0f113f3e MC |
174 | rsa_mode = RSA_SIGN; |
175 | need_priv = 1; | |
7e1b7485 RS |
176 | break; |
177 | case OPT_VERIFY: | |
0f113f3e | 178 | rsa_mode = RSA_VERIFY; |
7e1b7485 RS |
179 | break; |
180 | case OPT_REV: | |
0f113f3e | 181 | rev = 1; |
7e1b7485 RS |
182 | break; |
183 | case OPT_ENCRYPT: | |
0f113f3e | 184 | rsa_mode = RSA_ENCRYPT; |
7e1b7485 RS |
185 | break; |
186 | case OPT_DECRYPT: | |
0f113f3e MC |
187 | rsa_mode = RSA_DECRYPT; |
188 | need_priv = 1; | |
7e1b7485 RS |
189 | break; |
190 | case OPT_PUBIN: | |
191 | key_type = KEY_PUBKEY; | |
192 | break; | |
193 | case OPT_CERTIN: | |
194 | key_type = KEY_CERT; | |
195 | break; | |
196 | case OPT_INKEY: | |
197 | keyfile = opt_arg(); | |
198 | break; | |
199 | case OPT_PASSIN: | |
200 | passinarg = opt_arg(); | |
201 | break; | |
0f113f3e | 202 | } |
0f113f3e | 203 | } |
7e1b7485 RS |
204 | argc = opt_num_rest(); |
205 | argv = opt_rest(); | |
0f113f3e MC |
206 | |
207 | if (need_priv && (key_type != KEY_PRIVKEY)) { | |
208 | BIO_printf(bio_err, "A private key is needed for this operation\n"); | |
209 | goto end; | |
210 | } | |
211 | # ifndef OPENSSL_NO_ENGINE | |
7e1b7485 | 212 | e = setup_engine(engine, 0); |
0f113f3e | 213 | # endif |
7e1b7485 | 214 | if (!app_passwd(passinarg, NULL, &passin, NULL)) { |
0f113f3e MC |
215 | BIO_printf(bio_err, "Error getting password\n"); |
216 | goto end; | |
217 | } | |
32d862ed | 218 | |
2b40660e | 219 | /* FIXME: seed PRNG only if needed */ |
7e1b7485 | 220 | app_RAND_load_file(NULL, 0); |
0f113f3e MC |
221 | |
222 | switch (key_type) { | |
223 | case KEY_PRIVKEY: | |
7e1b7485 | 224 | pkey = load_key(keyfile, keyformat, 0, passin, e, "Private Key"); |
0f113f3e MC |
225 | break; |
226 | ||
227 | case KEY_PUBKEY: | |
7e1b7485 | 228 | pkey = load_pubkey(keyfile, keyformat, 0, NULL, e, "Public Key"); |
0f113f3e MC |
229 | break; |
230 | ||
231 | case KEY_CERT: | |
7e1b7485 | 232 | x = load_cert(keyfile, keyformat, NULL, e, "Certificate"); |
0f113f3e MC |
233 | if (x) { |
234 | pkey = X509_get_pubkey(x); | |
235 | X509_free(x); | |
236 | } | |
237 | break; | |
238 | } | |
239 | ||
240 | if (!pkey) { | |
241 | return 1; | |
242 | } | |
243 | ||
244 | rsa = EVP_PKEY_get1_RSA(pkey); | |
245 | EVP_PKEY_free(pkey); | |
246 | ||
247 | if (!rsa) { | |
248 | BIO_printf(bio_err, "Error getting RSA key\n"); | |
249 | ERR_print_errors(bio_err); | |
250 | goto end; | |
251 | } | |
252 | ||
7e1b7485 RS |
253 | in = bio_open_default(infile, "rb"); |
254 | if (in == NULL) | |
255 | goto end; | |
256 | out = bio_open_default(outfile, "wb"); | |
257 | if (out == NULL) | |
258 | goto end; | |
0f113f3e MC |
259 | |
260 | keysize = RSA_size(rsa); | |
261 | ||
262 | rsa_in = OPENSSL_malloc(keysize * 2); | |
263 | rsa_out = OPENSSL_malloc(keysize); | |
918bb865 MC |
264 | if (!rsa_in || !rsa_out) { |
265 | BIO_printf(bio_err, "Out of memory\n"); | |
918bb865 MC |
266 | goto end; |
267 | } | |
0f113f3e MC |
268 | |
269 | /* Read the input data */ | |
270 | rsa_inlen = BIO_read(in, rsa_in, keysize * 2); | |
271 | if (rsa_inlen <= 0) { | |
272 | BIO_printf(bio_err, "Error reading input Data\n"); | |
7e1b7485 | 273 | goto end; |
0f113f3e MC |
274 | } |
275 | if (rev) { | |
276 | int i; | |
277 | unsigned char ctmp; | |
278 | for (i = 0; i < rsa_inlen / 2; i++) { | |
279 | ctmp = rsa_in[i]; | |
280 | rsa_in[i] = rsa_in[rsa_inlen - 1 - i]; | |
281 | rsa_in[rsa_inlen - 1 - i] = ctmp; | |
282 | } | |
283 | } | |
284 | switch (rsa_mode) { | |
285 | ||
286 | case RSA_VERIFY: | |
287 | rsa_outlen = RSA_public_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad); | |
288 | break; | |
289 | ||
290 | case RSA_SIGN: | |
291 | rsa_outlen = | |
292 | RSA_private_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad); | |
293 | break; | |
294 | ||
295 | case RSA_ENCRYPT: | |
296 | rsa_outlen = RSA_public_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad); | |
297 | break; | |
298 | ||
299 | case RSA_DECRYPT: | |
300 | rsa_outlen = | |
301 | RSA_private_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad); | |
302 | break; | |
303 | ||
304 | } | |
305 | ||
306 | if (rsa_outlen <= 0) { | |
307 | BIO_printf(bio_err, "RSA operation error\n"); | |
308 | ERR_print_errors(bio_err); | |
309 | goto end; | |
310 | } | |
311 | ret = 0; | |
312 | if (asn1parse) { | |
313 | if (!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) { | |
314 | ERR_print_errors(bio_err); | |
315 | } | |
316 | } else if (hexdump) | |
317 | BIO_dump(out, (char *)rsa_out, rsa_outlen); | |
318 | else | |
319 | BIO_write(out, rsa_out, rsa_outlen); | |
320 | end: | |
321 | RSA_free(rsa); | |
322 | BIO_free(in); | |
323 | BIO_free_all(out); | |
324 | if (rsa_in) | |
325 | OPENSSL_free(rsa_in); | |
326 | if (rsa_out) | |
327 | OPENSSL_free(rsa_out); | |
328 | if (passin) | |
329 | OPENSSL_free(passin); | |
330 | return ret; | |
bd08a2bd DSH |
331 | } |
332 | ||
0f113f3e | 333 | #else /* !OPENSSL_NO_RSA */ |
d4f0339c DSH |
334 | |
335 | # if PEDANTIC | |
0f113f3e | 336 | static void *dummy = &dummy; |
d4f0339c DSH |
337 | # endif |
338 | ||
28967cf0 | 339 | #endif |