1 /* $OpenBSD: rsautl.c,v 1.17 2019/02/04 11:21:05 tb Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
59 #include <openssl/opensslconf.h>
65 #include <openssl/err.h>
66 #include <openssl/pem.h>
67 #include <openssl/rsa.h>
92 struct option rsautl_options[] = {
95 .desc = "ASN.1 parse the output data",
97 .opt.flag = &rsautl_config.asn1parse,
101 .desc = "Input is a certificate containing an RSA public key",
102 .type = OPTION_VALUE,
104 .opt.value = &rsautl_config.key_type,
108 .desc = "Decrypt the input data using RSA private key",
109 .type = OPTION_VALUE,
110 .value = RSA_DECRYPT,
111 .opt.value = &rsautl_config.rsa_mode,
115 .desc = "Encrypt the input data using RSA public key",
116 .type = OPTION_VALUE,
117 .value = RSA_ENCRYPT,
118 .opt.value = &rsautl_config.rsa_mode,
122 .desc = "Hex dump the output data",
124 .opt.flag = &rsautl_config.hexdump,
129 .desc = "Input file (default stdin)",
131 .opt.arg = &rsautl_config.infile,
136 .desc = "Input key file",
138 .opt.arg = &rsautl_config.keyfile,
143 .desc = "Input key format (DER, TXT or PEM (default))",
144 .type = OPTION_ARG_FORMAT,
145 .opt.value = &rsautl_config.keyform,
149 .desc = "Use PKCS#1 OAEP padding",
150 .type = OPTION_VALUE,
151 .value = RSA_PKCS1_OAEP_PADDING,
152 .opt.value = &rsautl_config.pad,
157 .desc = "Output file (default stdout)",
159 .opt.arg = &rsautl_config.outfile,
164 .desc = "Key password source",
166 .opt.arg = &rsautl_config.passargin,
170 .desc = "Use PKCS#1 v1.5 padding (default)",
171 .type = OPTION_VALUE,
172 .value = RSA_PKCS1_PADDING,
173 .opt.value = &rsautl_config.pad,
177 .desc = "Input is an RSA public key",
178 .type = OPTION_VALUE,
180 .opt.value = &rsautl_config.key_type,
184 .desc = "Use no padding",
185 .type = OPTION_VALUE,
186 .value = RSA_NO_PADDING,
187 .opt.value = &rsautl_config.pad,
191 .desc = "Reverse the input data",
193 .opt.flag = &rsautl_config.rev,
197 .desc = "Sign the input data using RSA private key",
198 .type = OPTION_VALUE,
200 .opt.value = &rsautl_config.rsa_mode,
204 .desc = "Verify the input data using RSA public key",
205 .type = OPTION_VALUE,
207 .opt.value = &rsautl_config.rsa_mode,
211 .desc = "Use ANSI X9.31 padding",
212 .type = OPTION_VALUE,
213 .value = RSA_X931_PADDING,
214 .opt.value = &rsautl_config.pad,
224 "usage: rsautl [-asn1parse] [-certin] [-decrypt] [-encrypt] "
226 " [-in file] [-inkey file] [-keyform der | pem]\n"
227 " [-oaep | -pkcs | -raw | -x931] [-out file] [-passin arg]\n"
228 " [-pubin] [-rev] [-sign] [-verify]\n\n");
230 options_usage(rsautl_options);
234 rsautl_main(int argc, char **argv)
236 BIO *in = NULL, *out = NULL;
238 EVP_PKEY *pkey = NULL;
240 unsigned char *rsa_in = NULL, *rsa_out = NULL;
242 int rsa_inlen, rsa_outlen = 0;
247 if (single_execution) {
248 if (pledge("stdio cpath wpath rpath tty", NULL) == -1) {
254 memset(&rsautl_config, 0, sizeof(rsautl_config));
255 rsautl_config.keyform = FORMAT_PEM;
256 rsautl_config.key_type = KEY_PRIVKEY;
257 rsautl_config.pad = RSA_PKCS1_PADDING;
258 rsautl_config.rsa_mode = RSA_VERIFY;
260 if (options_parse(argc, argv, rsautl_options, NULL, NULL) != 0) {
265 if (rsautl_config.rsa_mode == RSA_SIGN ||
266 rsautl_config.rsa_mode == RSA_DECRYPT)
269 if (need_priv && rsautl_config.key_type != KEY_PRIVKEY) {
270 BIO_printf(bio_err, "A private key is needed for this operation\n");
273 if (!app_passwd(bio_err, rsautl_config.passargin, NULL, &passin, NULL)) {
274 BIO_printf(bio_err, "Error getting password\n");
278 switch (rsautl_config.key_type) {
280 pkey = load_key(bio_err, rsautl_config.keyfile,
281 rsautl_config.keyform, 0, passin, "Private Key");
285 pkey = load_pubkey(bio_err, rsautl_config.keyfile,
286 rsautl_config.keyform, 0, NULL, "Public Key");
290 x = load_cert(bio_err, rsautl_config.keyfile,
291 rsautl_config.keyform, NULL, "Certificate");
293 pkey = X509_get_pubkey(x);
302 rsa = EVP_PKEY_get1_RSA(pkey);
306 BIO_printf(bio_err, "Error getting RSA key\n");
307 ERR_print_errors(bio_err);
310 if (rsautl_config.infile) {
311 if (!(in = BIO_new_file(rsautl_config.infile, "rb"))) {
312 BIO_printf(bio_err, "Error Reading Input File\n");
313 ERR_print_errors(bio_err);
317 in = BIO_new_fp(stdin, BIO_NOCLOSE);
319 if (rsautl_config.outfile) {
320 if (!(out = BIO_new_file(rsautl_config.outfile, "wb"))) {
321 BIO_printf(bio_err, "Error Reading Output File\n");
322 ERR_print_errors(bio_err);
326 out = BIO_new_fp(stdout, BIO_NOCLOSE);
329 keysize = RSA_size(rsa);
331 rsa_in = reallocarray(NULL, keysize, 2);
332 if (rsa_in == NULL) {
333 BIO_printf(bio_err, "Error allocating memory for input data\n");
336 rsa_out = malloc(keysize);
337 if (rsa_out == NULL) {
338 BIO_printf(bio_err, "Error allocating memory for output data\n");
342 /* Read the input data */
343 rsa_inlen = BIO_read(in, rsa_in, keysize * 2);
344 if (rsa_inlen <= 0) {
345 BIO_printf(bio_err, "Error reading input Data\n");
348 if (rsautl_config.rev) {
351 for (i = 0; i < rsa_inlen / 2; i++) {
353 rsa_in[i] = rsa_in[rsa_inlen - 1 - i];
354 rsa_in[rsa_inlen - 1 - i] = ctmp;
358 switch (rsautl_config.rsa_mode) {
360 rsa_outlen = RSA_public_decrypt(rsa_inlen, rsa_in, rsa_out,
361 rsa, rsautl_config.pad);
365 rsa_outlen = RSA_private_encrypt(rsa_inlen, rsa_in, rsa_out,
366 rsa, rsautl_config.pad);
370 rsa_outlen = RSA_public_encrypt(rsa_inlen, rsa_in, rsa_out,
371 rsa, rsautl_config.pad);
375 rsa_outlen = RSA_private_decrypt(rsa_inlen, rsa_in, rsa_out,
376 rsa, rsautl_config.pad);
380 if (rsa_outlen <= 0) {
381 BIO_printf(bio_err, "RSA operation error\n");
382 ERR_print_errors(bio_err);
386 if (rsautl_config.asn1parse) {
387 if (!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) {
388 ERR_print_errors(bio_err);
390 } else if (rsautl_config.hexdump)
391 BIO_dump(out, (char *) rsa_out, rsa_outlen);
393 BIO_write(out, rsa_out, rsa_outlen);