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