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