1e127a0d80155b45dafe77f2b4380e5fa111a3345Chris Lattner/* rsautl.c */
22cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
32cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor * project 2000.
42cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor */
52cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/* ====================================================================
62cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
72cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor *
82cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor * Redistribution and use in source and binary forms, with or without
92cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor * modification, are permitted provided that the following conditions
102cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor * are met:
112cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor *
122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor * 1. Redistributions of source code must retain the above copyright
132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor *    notice, this list of conditions and the following disclaimer.
142cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor *
15e7785040107266d01ebdcc066365f70b7ace371fDouglas Gregor * 2. Redistributions in binary form must reproduce the above copyright
161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump *    notice, this list of conditions and the following disclaimer in
172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor *    the documentation and/or other materials provided with the
182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor *    distribution.
192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor *
200b7489194f9f89fac39d57211c1e7953ae50251fDouglas Gregor * 3. All advertising materials mentioning features or use of this
212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor *    software must display the following acknowledgment:
22a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall *    "This product includes software developed by the OpenSSL Project
2377f4603c8b142e642300959a601ecec2b7c8e288Sebastian Redl *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
247c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner *
256a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
267c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner *    endorse or promote products derived from this software without
2783d63c78810556d26b62ac4cbae2eda6cdd2570cSteve Naroff *    prior written permission. For written permission, please contact
2814f79002e58556798e86168c63e48d533287eda5Douglas Gregor *    licensing@OpenSSL.org.
293251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor *
3014f79002e58556798e86168c63e48d533287eda5Douglas Gregor * 5. Products derived from this software may not be called "OpenSSL"
31bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor *    nor may "OpenSSL" appear in their names without prior written
322bec0410d268779f601bd509e0302a500af7ac6aDouglas Gregor *    permission of the OpenSSL Project.
33ab41e63821dc60ad144d0684df8d79a9eef86b75Douglas Gregor *
3417fc223395d51be582fc666bb6ea21bd1dff26dcDouglas Gregor * 6. Redistributions of any form whatsoever must retain the following
3517fc223395d51be582fc666bb6ea21bd1dff26dcDouglas Gregor *    acknowledgment:
362596e429a61602312bdd149786045b8a90cd2d10Daniel Dunbar *    "This product includes software developed by the OpenSSL Project
372cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
3814f79002e58556798e86168c63e48d533287eda5Douglas Gregor *
39b64c19365deab788753d29c9bc881253c3f16f37Douglas Gregor * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
403c304bd9ec2b4611572d4cbae9e1727bbecb5dc9Chris Lattner * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
412cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
422cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl * OF THE POSSIBILITY OF SUCH DAMAGE.
51ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl * ====================================================================
522cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor *
532cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor * This product includes cryptographic software written by Eric Young
542cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor * (eay@cryptsoft.com).  This product includes software written by Tim
5512b1c7615d4f9a2edc544be499f895f16ac100edChris Lattner * Hudson (tjh@cryptsoft.com).
562cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor *
57bd21828179a61bd0954b082825cfb8a93345f602Benjamin Kramer */
582cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
592cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include <openssl/opensslconf.h>
602cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#ifndef OPENSSL_NO_RSA
612cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
622cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "apps.h"
632cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include <string.h>
642cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include <openssl/err.h>
651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump#include <openssl/pem.h>
664fed3f47f6b9e31d603c5c2d1f6d8ec2e1241e57Douglas Gregor#include <openssl/rsa.h>
672cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
682cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#define RSA_SIGN 	1
692cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#define RSA_VERIFY 	2
702cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#define RSA_ENCRYPT 	3
712cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#define RSA_DECRYPT 	4
722cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
732cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#define KEY_PRIVKEY	1
742cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#define KEY_PUBKEY	2
752cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#define KEY_CERT	3
762cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
772cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorstatic void usage(void);
782cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
792cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#undef PROG
802cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
812cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#define PROG rsautl_main
822cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
832cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorint MAIN(int argc, char **);
842cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
852cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorint MAIN(int argc, char **argv)
862cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor{
872cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	ENGINE *e = NULL;
882cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	BIO *in = NULL, *out = NULL;
892cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	char *infile = NULL, *outfile = NULL;
902cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#ifndef OPENSSL_NO_ENGINE
912cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	char *engine = NULL;
922cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#endif
931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump	char *keyfile = NULL;
942cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	char rsa_mode = RSA_VERIFY, key_type = KEY_PRIVKEY;
952cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	int keyform = FORMAT_PEM;
962cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	char need_priv = 0, badarg = 0, rev = 0;
972cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	char hexdump = 0, asn1parse = 0;
982cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	X509 *x;
992cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	EVP_PKEY *pkey = NULL;
1002cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	RSA *rsa = NULL;
1012cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	unsigned char *rsa_in = NULL, *rsa_out = NULL, pad;
1022cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	char *passargin = NULL, *passin = NULL;
1032cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	int rsa_inlen, rsa_outlen = 0;
1042cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	int keysize;
1052cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1062cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	int ret = 1;
1072cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump	argc--;
1091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump	argv++;
1102cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1112cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	if(!bio_err) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
1122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	if (!load_config(bio_err, NULL))
1142cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		goto end;
1152cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	ERR_load_crypto_strings();
1160953e767ff7817f97b3ab20896b229891eeff45bJohn McCall	OpenSSL_add_all_algorithms();
1172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	pad = RSA_PKCS1_PADDING;
1182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	while(argc >= 1)
1202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	{
1212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		if (!strcmp(*argv,"-in")) {
1222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor			if (--argc < 1)
1232cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor				badarg = 1;
1242cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor			else
1252cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor				infile= *(++argv);
1262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		} else if (!strcmp(*argv,"-out")) {
1272cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor			if (--argc < 1)
1282cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor				badarg = 1;
1292cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor			else
1302cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor				outfile= *(++argv);
1312cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		} else if(!strcmp(*argv, "-inkey")) {
1327e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor			if (--argc < 1)
1337e7eb3da052a6d80ddf2377cab0384c798f73f75Douglas Gregor				badarg = 1;
134c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor			else
1352cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor				keyfile = *(++argv);
1362cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		} else if (!strcmp(*argv,"-passin")) {
1372cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor			if (--argc < 1)
1382cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor				badarg = 1;
1392cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor			else
1402cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor				passargin= *(++argv);
141788b0fd67e1992f23555454efcdb16a19dfefac3Chris Lattner		} else if (strcmp(*argv,"-keyform") == 0) {
1422cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor			if (--argc < 1)
1432cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor				badarg = 1;
1442cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor			else
1452cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor				keyform=str2fmt(*(++argv));
1462cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#ifndef OPENSSL_NO_ENGINE
1472cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		} else if(!strcmp(*argv, "-engine")) {
1482cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor			if (--argc < 1)
1492cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor				badarg = 1;
1502cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor			else
1512cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor				engine = *(++argv);
152264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola#endif
153264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola		} else if(!strcmp(*argv, "-pubin")) {
154425ef72306d4ff6b3698b744353e5f0e56b4b884Rafael Espindola			key_type = KEY_PUBKEY;
155ab8bbf4ebd3e3e6eab913cb044772a62b7581941Douglas Gregor		} else if(!strcmp(*argv, "-certin")) {
156264ba48dc98f3f843935a485d5b086f7e0fdc4f1Rafael Espindola			key_type = KEY_CERT;
1572cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		}
1582cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		else if(!strcmp(*argv, "-asn1parse")) asn1parse = 1;
1592cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		else if(!strcmp(*argv, "-hexdump")) hexdump = 1;
1602cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		else if(!strcmp(*argv, "-raw")) pad = RSA_NO_PADDING;
1612cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		else if(!strcmp(*argv, "-oaep")) pad = RSA_PKCS1_OAEP_PADDING;
1622cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		else if(!strcmp(*argv, "-ssl")) pad = RSA_SSLV23_PADDING;
1632cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		else if(!strcmp(*argv, "-pkcs")) pad = RSA_PKCS1_PADDING;
1642cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		else if(!strcmp(*argv, "-x931")) pad = RSA_X931_PADDING;
1652cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		else if(!strcmp(*argv, "-sign")) {
1662cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor			rsa_mode = RSA_SIGN;
1672cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor			need_priv = 1;
1682cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		} else if(!strcmp(*argv, "-verify")) rsa_mode = RSA_VERIFY;
1692cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		else if(!strcmp(*argv, "-rev")) rev = 1;
1702cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		else if(!strcmp(*argv, "-encrypt")) rsa_mode = RSA_ENCRYPT;
171465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl		else if(!strcmp(*argv, "-decrypt")) {
172465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl			rsa_mode = RSA_DECRYPT;
173465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl			need_priv = 1;
174465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl		} else badarg = 1;
175465226e23a3008bd68973513dda1f9e3cd27dbddSebastian Redl		if(badarg) {
1762cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor			usage();
1772cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor			goto end;
1782cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		}
179ed97649e9574b9d854fa4d6109c9333ae0993554John McCall		argc--;
180ed97649e9574b9d854fa4d6109c9333ae0993554John McCall		argv++;
181ed97649e9574b9d854fa4d6109c9333ae0993554John McCall	}
182ed97649e9574b9d854fa4d6109c9333ae0993554John McCall
183ed97649e9574b9d854fa4d6109c9333ae0993554John McCall	if(need_priv && (key_type != KEY_PRIVKEY)) {
1842cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		BIO_printf(bio_err, "A private key is needed for this operation\n");
1852cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		goto end;
1869763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis	}
1879763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
1882cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#ifndef OPENSSL_NO_ENGINE
1892cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor        e = setup_engine(bio_err, engine, 0);
1902cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#endif
1912cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	if(!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
192c9490c000f515c29f200a1215328d8ab9a0f3818Douglas Gregor		BIO_printf(bio_err, "Error getting password\n");
1932cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		goto end;
1942cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	}
1952cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1962cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor/* FIXME: seed PRNG only if needed */
1972cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	app_RAND_load_file(NULL, bio_err, 0);
1982cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
1992cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	switch(key_type) {
2002cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		case KEY_PRIVKEY:
201395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson		pkey = load_key(bio_err, keyfile, keyform, 0,
202395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson			passin, e, "Private Key");
203395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson		break;
204395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson
205395b475a4474f1c7574d927ad142ca0c7997cbcaAnders Carlsson		case KEY_PUBKEY:
2062cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		pkey = load_pubkey(bio_err, keyfile, keyform, 0,
207be191100e034b23a3e13053757a57b7f5068c24aArgyrios Kyrtzidis			NULL, e, "Public Key");
2082cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		break;
2091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2102cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		case KEY_CERT:
2112cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		x = load_cert(bio_err, keyfile, keyform,
2122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor			NULL, e, "Certificate");
2132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		if(x) {
2142cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor			pkey = X509_get_pubkey(x);
2152cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor			X509_free(x);
2162cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		}
2172cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		break;
2182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	}
2192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	if(!pkey) {
2212cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		return 1;
2222cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	}
2231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22449a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall	rsa = EVP_PKEY_get1_RSA(pkey);
22549a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall	EVP_PKEY_free(pkey);
22649a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall
22749a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall	if(!rsa) {
22849a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall		BIO_printf(bio_err, "Error getting RSA key\n");
22949a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall		ERR_print_errors(bio_err);
23049a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall		goto end;
23149a832bd499d6f61c23655f1fac99f0dd229756eJohn McCall	}
2322cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
2332cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
234be191100e034b23a3e13053757a57b7f5068c24aArgyrios Kyrtzidis	if(infile) {
23590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis		if(!(in = BIO_new_file(infile, "rb"))) {
23690b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis			BIO_printf(bio_err, "Error Reading Input File\n");
23790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis			ERR_print_errors(bio_err);
23890b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis			goto end;
23990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis		}
2409763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis	} else in = BIO_new_fp(stdin, BIO_NOCLOSE);
2419763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis
2429763e221e16026ddf487d2564ed349d2c874a1a1Argyrios Kyrtzidis	if(outfile) {
24390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis		if(!(out = BIO_new_file(outfile, "wb"))) {
24490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis			BIO_printf(bio_err, "Error Reading Output File\n");
24590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis			ERR_print_errors(bio_err);
24690b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis			goto end;
24790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis		}
248ae8b17f1d5d303af53db5a4f4a375ea6b9356566Argyrios Kyrtzidis	} else {
249ae8b17f1d5d303af53db5a4f4a375ea6b9356566Argyrios Kyrtzidis		out = BIO_new_fp(stdout, BIO_NOCLOSE);
250ae8b17f1d5d303af53db5a4f4a375ea6b9356566Argyrios Kyrtzidis#ifdef OPENSSL_SYS_VMS
251ae8b17f1d5d303af53db5a4f4a375ea6b9356566Argyrios Kyrtzidis		{
25290b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis		    BIO *tmpbio = BIO_new(BIO_f_linebuffer());
25390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis		    out = BIO_push(tmpbio, out);
25490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis		}
25590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis#endif
25690b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis	}
25790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
25890b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis	keysize = RSA_size(rsa);
25990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
26090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis	rsa_in = OPENSSL_malloc(keysize * 2);
26190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis	rsa_out = OPENSSL_malloc(keysize);
26290b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
26390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis	/* Read the input data */
26490b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis	rsa_inlen = BIO_read(in, rsa_in, keysize * 2);
26590b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis	if(rsa_inlen <= 0) {
26690b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis		BIO_printf(bio_err, "Error reading input Data\n");
26790b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis		exit(1);
26890b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis	}
26990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis	if(rev) {
27090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis		int i;
27190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis		unsigned char ctmp;
2728dfbd8b252ba4e6cf4b7a3422f6ef0ca21312dfeArgyrios Kyrtzidis		for(i = 0; i < rsa_inlen/2; i++) {
2738dfbd8b252ba4e6cf4b7a3422f6ef0ca21312dfeArgyrios Kyrtzidis			ctmp = rsa_in[i];
2748dfbd8b252ba4e6cf4b7a3422f6ef0ca21312dfeArgyrios Kyrtzidis			rsa_in[i] = rsa_in[rsa_inlen - 1 - i];
275f48d45e3e36c132bdee3373beec4e8b19ae3f9c4Argyrios Kyrtzidis			rsa_in[rsa_inlen - 1 - i] = ctmp;
276f48d45e3e36c132bdee3373beec4e8b19ae3f9c4Argyrios Kyrtzidis		}
277f48d45e3e36c132bdee3373beec4e8b19ae3f9c4Argyrios Kyrtzidis	}
2788dfbd8b252ba4e6cf4b7a3422f6ef0ca21312dfeArgyrios Kyrtzidis	switch(rsa_mode) {
27990b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
28090b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis		case RSA_VERIFY:
28190b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis			rsa_outlen  = RSA_public_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
28290b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis		break;
28390b715e0df34eae2b50b9b43ec60828ed31dcf94Argyrios Kyrtzidis
2843acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis		case RSA_SIGN:
2853acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis			rsa_outlen  = RSA_private_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
2863acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis		break;
2873acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis
2883acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis		case RSA_ENCRYPT:
2893acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis			rsa_outlen  = RSA_public_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
2903acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis		break;
2913acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis
2922cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		case RSA_DECRYPT:
2932cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor			rsa_outlen  = RSA_private_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
294465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara		break;
295465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara
2963acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis	}
2973acad62a239448bef0f5848b2a0d5f7dfefd3d14Argyrios Kyrtzidis
298465d41b92b2c862f3062c412a0538db65c6a2661Abramo Bagnara	if(rsa_outlen <= 0) {
2992cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		BIO_printf(bio_err, "RSA operation error\n");
3002cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		ERR_print_errors(bio_err);
3013cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall		goto end;
3023cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall	}
30331f17ecbef57b5679c017c375db330546b7b5145John McCall	ret = 0;
3043cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall	if(asn1parse) {
3053cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall		if(!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) {
3063cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall			ERR_print_errors(bio_err);
3072cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor		}
3082cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	} else if(hexdump) BIO_dump(out, (char *)rsa_out, rsa_outlen);
309c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall	else BIO_write(out, rsa_out, rsa_outlen);
310c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall	end:
311c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall	RSA_free(rsa);
312c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall	BIO_free(in);
313c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall	BIO_free_all(out);
3142cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	if(rsa_in) OPENSSL_free(rsa_in);
315c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall	if(rsa_out) OPENSSL_free(rsa_out);
316446ee4eb4fc4c705a59365252df7a5c253daafa1Steve Naroff	if(passin) OPENSSL_free(passin);
317446ee4eb4fc4c705a59365252df7a5c253daafa1Steve Naroff	return ret;
318c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall}
3192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
3202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorstatic void usage()
321d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff{
322d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff	BIO_printf(bio_err, "Usage: rsautl [options]\n");
3231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump	BIO_printf(bio_err, "-in file        input file\n");
324d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff	BIO_printf(bio_err, "-out file       output file\n");
3252cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	BIO_printf(bio_err, "-inkey file     input key\n");
3262cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor	BIO_printf(bio_err, "-keyform arg    private key format - default PEM\n");
327a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall	BIO_printf(bio_err, "-pubin          input is an RSA public\n");
328a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall	BIO_printf(bio_err, "-certin         input is a certificate carrying an RSA public key\n");
329a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall	BIO_printf(bio_err, "-ssl            use SSL v2 padding\n");
330a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall	BIO_printf(bio_err, "-raw            use no padding\n");
331a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall	BIO_printf(bio_err, "-pkcs           use PKCS#1 v1.5 padding (default)\n");
332a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall	BIO_printf(bio_err, "-oaep           use PKCS#1 OAEP\n");
333a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall	BIO_printf(bio_err, "-sign           sign with private key\n");
334a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall	BIO_printf(bio_err, "-verify         verify with public key\n");
335a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall	BIO_printf(bio_err, "-encrypt        encrypt with public key\n");
336a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall	BIO_printf(bio_err, "-decrypt        decrypt with private key\n");
33751bd803fbdade51d674598ed45da3d54190a656cJohn McCall	BIO_printf(bio_err, "-hexdump        hex dump output\n");
338a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall#ifndef OPENSSL_NO_ENGINE
33951bd803fbdade51d674598ed45da3d54190a656cJohn McCall	BIO_printf(bio_err, "-engine e       use engine e, possibly a hardware device.\n");
340a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall	BIO_printf (bio_err, "-passin arg    pass phrase source\n");
341a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall#endif
34251bd803fbdade51d674598ed45da3d54190a656cJohn McCall
34351bd803fbdade51d674598ed45da3d54190a656cJohn McCall}
344a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall
345a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall#else /* !OPENSSL_NO_RSA */
346a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall
347a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall# if PEDANTIC
34851bd803fbdade51d674598ed45da3d54190a656cJohn McCallstatic void *dummy=&dummy;
34951bd803fbdade51d674598ed45da3d54190a656cJohn McCall# endif
35051bd803fbdade51d674598ed45da3d54190a656cJohn McCall
35151bd803fbdade51d674598ed45da3d54190a656cJohn McCall#endif
352ddf889a2ad2888f1dea573987bbe952d9912c1a0Douglas Gregor