10Sstevel@tonic-gate /* rsautl.c */
20Sstevel@tonic-gate /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
30Sstevel@tonic-gate * project 2000.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate /* ====================================================================
60Sstevel@tonic-gate * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate * are met:
110Sstevel@tonic-gate *
120Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate *
150Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate * the documentation and/or other materials provided with the
180Sstevel@tonic-gate * distribution.
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate * software must display the following acknowledgment:
220Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate * endorse or promote products derived from this software without
270Sstevel@tonic-gate * prior written permission. For written permission, please contact
280Sstevel@tonic-gate * licensing@OpenSSL.org.
290Sstevel@tonic-gate *
300Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate * permission of the OpenSSL Project.
330Sstevel@tonic-gate *
340Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate * acknowledgment:
360Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
380Sstevel@tonic-gate *
390Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate * ====================================================================
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
550Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate *
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
59*2139Sjp161948 #include <openssl/opensslconf.h>
600Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
610Sstevel@tonic-gate
620Sstevel@tonic-gate #include "apps.h"
630Sstevel@tonic-gate #include <string.h>
640Sstevel@tonic-gate #include <openssl/err.h>
650Sstevel@tonic-gate #include <openssl/pem.h>
66*2139Sjp161948 #include <openssl/rsa.h>
670Sstevel@tonic-gate
680Sstevel@tonic-gate #define RSA_SIGN 1
690Sstevel@tonic-gate #define RSA_VERIFY 2
700Sstevel@tonic-gate #define RSA_ENCRYPT 3
710Sstevel@tonic-gate #define RSA_DECRYPT 4
720Sstevel@tonic-gate
730Sstevel@tonic-gate #define KEY_PRIVKEY 1
740Sstevel@tonic-gate #define KEY_PUBKEY 2
750Sstevel@tonic-gate #define KEY_CERT 3
760Sstevel@tonic-gate
770Sstevel@tonic-gate static void usage(void);
780Sstevel@tonic-gate
790Sstevel@tonic-gate #undef PROG
800Sstevel@tonic-gate
810Sstevel@tonic-gate #define PROG rsautl_main
820Sstevel@tonic-gate
830Sstevel@tonic-gate int MAIN(int argc, char **);
840Sstevel@tonic-gate
MAIN(int argc,char ** argv)850Sstevel@tonic-gate int MAIN(int argc, char **argv)
860Sstevel@tonic-gate {
870Sstevel@tonic-gate ENGINE *e = NULL;
880Sstevel@tonic-gate BIO *in = NULL, *out = NULL;
890Sstevel@tonic-gate char *infile = NULL, *outfile = NULL;
900Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
910Sstevel@tonic-gate char *engine = NULL;
920Sstevel@tonic-gate #endif
930Sstevel@tonic-gate char *keyfile = NULL;
940Sstevel@tonic-gate char rsa_mode = RSA_VERIFY, key_type = KEY_PRIVKEY;
950Sstevel@tonic-gate int keyform = FORMAT_PEM;
960Sstevel@tonic-gate char need_priv = 0, badarg = 0, rev = 0;
970Sstevel@tonic-gate char hexdump = 0, asn1parse = 0;
980Sstevel@tonic-gate X509 *x;
990Sstevel@tonic-gate EVP_PKEY *pkey = NULL;
1000Sstevel@tonic-gate RSA *rsa = NULL;
1010Sstevel@tonic-gate unsigned char *rsa_in = NULL, *rsa_out = NULL, pad;
1020Sstevel@tonic-gate char *passargin = NULL, *passin = NULL;
1030Sstevel@tonic-gate int rsa_inlen, rsa_outlen = 0;
1040Sstevel@tonic-gate int keysize;
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate int ret = 1;
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate argc--;
1090Sstevel@tonic-gate argv++;
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate if(!bio_err) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate if (!load_config(bio_err, NULL))
1140Sstevel@tonic-gate goto end;
1150Sstevel@tonic-gate ERR_load_crypto_strings();
1160Sstevel@tonic-gate OpenSSL_add_all_algorithms();
1170Sstevel@tonic-gate pad = RSA_PKCS1_PADDING;
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate while(argc >= 1)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate if (!strcmp(*argv,"-in")) {
1220Sstevel@tonic-gate if (--argc < 1) badarg = 1;
1230Sstevel@tonic-gate infile= *(++argv);
1240Sstevel@tonic-gate } else if (!strcmp(*argv,"-out")) {
1250Sstevel@tonic-gate if (--argc < 1) badarg = 1;
1260Sstevel@tonic-gate outfile= *(++argv);
1270Sstevel@tonic-gate } else if(!strcmp(*argv, "-inkey")) {
1280Sstevel@tonic-gate if (--argc < 1) badarg = 1;
1290Sstevel@tonic-gate keyfile = *(++argv);
1300Sstevel@tonic-gate } else if (!strcmp(*argv,"-passin")) {
1310Sstevel@tonic-gate if (--argc < 1) badarg = 1;
1320Sstevel@tonic-gate passargin= *(++argv);
1330Sstevel@tonic-gate } else if (strcmp(*argv,"-keyform") == 0) {
1340Sstevel@tonic-gate if (--argc < 1) badarg = 1;
1350Sstevel@tonic-gate keyform=str2fmt(*(++argv));
1360Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1370Sstevel@tonic-gate } else if(!strcmp(*argv, "-engine")) {
1380Sstevel@tonic-gate if (--argc < 1) badarg = 1;
1390Sstevel@tonic-gate engine = *(++argv);
1400Sstevel@tonic-gate #endif
1410Sstevel@tonic-gate } else if(!strcmp(*argv, "-pubin")) {
1420Sstevel@tonic-gate key_type = KEY_PUBKEY;
1430Sstevel@tonic-gate } else if(!strcmp(*argv, "-certin")) {
1440Sstevel@tonic-gate key_type = KEY_CERT;
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate else if(!strcmp(*argv, "-asn1parse")) asn1parse = 1;
1470Sstevel@tonic-gate else if(!strcmp(*argv, "-hexdump")) hexdump = 1;
1480Sstevel@tonic-gate else if(!strcmp(*argv, "-raw")) pad = RSA_NO_PADDING;
1490Sstevel@tonic-gate else if(!strcmp(*argv, "-oaep")) pad = RSA_PKCS1_OAEP_PADDING;
1500Sstevel@tonic-gate else if(!strcmp(*argv, "-ssl")) pad = RSA_SSLV23_PADDING;
1510Sstevel@tonic-gate else if(!strcmp(*argv, "-pkcs")) pad = RSA_PKCS1_PADDING;
152*2139Sjp161948 else if(!strcmp(*argv, "-x931")) pad = RSA_X931_PADDING;
1530Sstevel@tonic-gate else if(!strcmp(*argv, "-sign")) {
1540Sstevel@tonic-gate rsa_mode = RSA_SIGN;
1550Sstevel@tonic-gate need_priv = 1;
1560Sstevel@tonic-gate } else if(!strcmp(*argv, "-verify")) rsa_mode = RSA_VERIFY;
1570Sstevel@tonic-gate else if(!strcmp(*argv, "-rev")) rev = 1;
1580Sstevel@tonic-gate else if(!strcmp(*argv, "-encrypt")) rsa_mode = RSA_ENCRYPT;
1590Sstevel@tonic-gate else if(!strcmp(*argv, "-decrypt")) {
1600Sstevel@tonic-gate rsa_mode = RSA_DECRYPT;
1610Sstevel@tonic-gate need_priv = 1;
1620Sstevel@tonic-gate } else badarg = 1;
1630Sstevel@tonic-gate if(badarg) {
1640Sstevel@tonic-gate usage();
1650Sstevel@tonic-gate goto end;
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate argc--;
1680Sstevel@tonic-gate argv++;
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate if(need_priv && (key_type != KEY_PRIVKEY)) {
1720Sstevel@tonic-gate BIO_printf(bio_err, "A private key is needed for this operation\n");
1730Sstevel@tonic-gate goto end;
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1770Sstevel@tonic-gate e = setup_engine(bio_err, engine, 0);
1780Sstevel@tonic-gate #endif
1790Sstevel@tonic-gate if(!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
1800Sstevel@tonic-gate BIO_printf(bio_err, "Error getting password\n");
1810Sstevel@tonic-gate goto end;
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate /* FIXME: seed PRNG only if needed */
1850Sstevel@tonic-gate app_RAND_load_file(NULL, bio_err, 0);
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate switch(key_type) {
1880Sstevel@tonic-gate case KEY_PRIVKEY:
1890Sstevel@tonic-gate pkey = load_key(bio_err, keyfile, keyform, 0,
1900Sstevel@tonic-gate passin, e, "Private Key");
1910Sstevel@tonic-gate break;
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate case KEY_PUBKEY:
1940Sstevel@tonic-gate pkey = load_pubkey(bio_err, keyfile, keyform, 0,
1950Sstevel@tonic-gate NULL, e, "Public Key");
1960Sstevel@tonic-gate break;
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate case KEY_CERT:
1990Sstevel@tonic-gate x = load_cert(bio_err, keyfile, keyform,
2000Sstevel@tonic-gate NULL, e, "Certificate");
2010Sstevel@tonic-gate if(x) {
2020Sstevel@tonic-gate pkey = X509_get_pubkey(x);
2030Sstevel@tonic-gate X509_free(x);
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate break;
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate if(!pkey) {
2090Sstevel@tonic-gate return 1;
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate rsa = EVP_PKEY_get1_RSA(pkey);
2130Sstevel@tonic-gate EVP_PKEY_free(pkey);
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate if(!rsa) {
2160Sstevel@tonic-gate BIO_printf(bio_err, "Error getting RSA key\n");
2170Sstevel@tonic-gate ERR_print_errors(bio_err);
2180Sstevel@tonic-gate goto end;
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate if(infile) {
2230Sstevel@tonic-gate if(!(in = BIO_new_file(infile, "rb"))) {
2240Sstevel@tonic-gate BIO_printf(bio_err, "Error Reading Input File\n");
2250Sstevel@tonic-gate ERR_print_errors(bio_err);
2260Sstevel@tonic-gate goto end;
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate } else in = BIO_new_fp(stdin, BIO_NOCLOSE);
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate if(outfile) {
2310Sstevel@tonic-gate if(!(out = BIO_new_file(outfile, "wb"))) {
2320Sstevel@tonic-gate BIO_printf(bio_err, "Error Reading Output File\n");
2330Sstevel@tonic-gate ERR_print_errors(bio_err);
2340Sstevel@tonic-gate goto end;
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate } else {
2370Sstevel@tonic-gate out = BIO_new_fp(stdout, BIO_NOCLOSE);
2380Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
2390Sstevel@tonic-gate {
2400Sstevel@tonic-gate BIO *tmpbio = BIO_new(BIO_f_linebuffer());
2410Sstevel@tonic-gate out = BIO_push(tmpbio, out);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate #endif
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate keysize = RSA_size(rsa);
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate rsa_in = OPENSSL_malloc(keysize * 2);
2490Sstevel@tonic-gate rsa_out = OPENSSL_malloc(keysize);
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate /* Read the input data */
2520Sstevel@tonic-gate rsa_inlen = BIO_read(in, rsa_in, keysize * 2);
2530Sstevel@tonic-gate if(rsa_inlen <= 0) {
2540Sstevel@tonic-gate BIO_printf(bio_err, "Error reading input Data\n");
2550Sstevel@tonic-gate exit(1);
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate if(rev) {
2580Sstevel@tonic-gate int i;
2590Sstevel@tonic-gate unsigned char ctmp;
2600Sstevel@tonic-gate for(i = 0; i < rsa_inlen/2; i++) {
2610Sstevel@tonic-gate ctmp = rsa_in[i];
2620Sstevel@tonic-gate rsa_in[i] = rsa_in[rsa_inlen - 1 - i];
2630Sstevel@tonic-gate rsa_in[rsa_inlen - 1 - i] = ctmp;
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate switch(rsa_mode) {
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate case RSA_VERIFY:
2690Sstevel@tonic-gate rsa_outlen = RSA_public_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
2700Sstevel@tonic-gate break;
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate case RSA_SIGN:
2730Sstevel@tonic-gate rsa_outlen = RSA_private_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
2740Sstevel@tonic-gate break;
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate case RSA_ENCRYPT:
2770Sstevel@tonic-gate rsa_outlen = RSA_public_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
2780Sstevel@tonic-gate break;
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate case RSA_DECRYPT:
2810Sstevel@tonic-gate rsa_outlen = RSA_private_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
2820Sstevel@tonic-gate break;
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate
2860Sstevel@tonic-gate if(rsa_outlen <= 0) {
2870Sstevel@tonic-gate BIO_printf(bio_err, "RSA operation error\n");
2880Sstevel@tonic-gate ERR_print_errors(bio_err);
2890Sstevel@tonic-gate goto end;
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate ret = 0;
2920Sstevel@tonic-gate if(asn1parse) {
2930Sstevel@tonic-gate if(!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) {
2940Sstevel@tonic-gate ERR_print_errors(bio_err);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate } else if(hexdump) BIO_dump(out, (char *)rsa_out, rsa_outlen);
2970Sstevel@tonic-gate else BIO_write(out, rsa_out, rsa_outlen);
2980Sstevel@tonic-gate end:
2990Sstevel@tonic-gate RSA_free(rsa);
3000Sstevel@tonic-gate BIO_free(in);
3010Sstevel@tonic-gate BIO_free_all(out);
3020Sstevel@tonic-gate if(rsa_in) OPENSSL_free(rsa_in);
3030Sstevel@tonic-gate if(rsa_out) OPENSSL_free(rsa_out);
3040Sstevel@tonic-gate if(passin) OPENSSL_free(passin);
3050Sstevel@tonic-gate return ret;
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate
usage()3080Sstevel@tonic-gate static void usage()
3090Sstevel@tonic-gate {
3100Sstevel@tonic-gate BIO_printf(bio_err, "Usage: rsautl [options]\n");
3110Sstevel@tonic-gate BIO_printf(bio_err, "-in file input file\n");
3120Sstevel@tonic-gate BIO_printf(bio_err, "-out file output file\n");
3130Sstevel@tonic-gate BIO_printf(bio_err, "-inkey file input key\n");
3140Sstevel@tonic-gate BIO_printf(bio_err, "-keyform arg private key format - default PEM\n");
3150Sstevel@tonic-gate BIO_printf(bio_err, "-pubin input is an RSA public\n");
3160Sstevel@tonic-gate BIO_printf(bio_err, "-certin input is a certificate carrying an RSA public key\n");
3170Sstevel@tonic-gate BIO_printf(bio_err, "-ssl use SSL v2 padding\n");
3180Sstevel@tonic-gate BIO_printf(bio_err, "-raw use no padding\n");
3190Sstevel@tonic-gate BIO_printf(bio_err, "-pkcs use PKCS#1 v1.5 padding (default)\n");
3200Sstevel@tonic-gate BIO_printf(bio_err, "-oaep use PKCS#1 OAEP\n");
3210Sstevel@tonic-gate BIO_printf(bio_err, "-sign sign with private key\n");
3220Sstevel@tonic-gate BIO_printf(bio_err, "-verify verify with public key\n");
3230Sstevel@tonic-gate BIO_printf(bio_err, "-encrypt encrypt with public key\n");
3240Sstevel@tonic-gate BIO_printf(bio_err, "-decrypt decrypt with private key\n");
3250Sstevel@tonic-gate BIO_printf(bio_err, "-hexdump hex dump output\n");
3260Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
3270Sstevel@tonic-gate BIO_printf(bio_err, "-engine e use engine e, possibly a hardware device.\n");
3280Sstevel@tonic-gate BIO_printf (bio_err, "-passin arg pass phrase source\n");
3290Sstevel@tonic-gate #endif
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate #endif
334