168651Skris/* rsautl.c */ 2194206Ssimon/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 368651Skris * project 2000. 468651Skris */ 568651Skris/* ==================================================================== 668651Skris * Copyright (c) 2000 The OpenSSL Project. All rights reserved. 768651Skris * 868651Skris * Redistribution and use in source and binary forms, with or without 968651Skris * modification, are permitted provided that the following conditions 1068651Skris * are met: 1168651Skris * 1268651Skris * 1. Redistributions of source code must retain the above copyright 1368651Skris * notice, this list of conditions and the following disclaimer. 1468651Skris * 1568651Skris * 2. Redistributions in binary form must reproduce the above copyright 1668651Skris * notice, this list of conditions and the following disclaimer in 1768651Skris * the documentation and/or other materials provided with the 1868651Skris * distribution. 1968651Skris * 2068651Skris * 3. All advertising materials mentioning features or use of this 2168651Skris * software must display the following acknowledgment: 2268651Skris * "This product includes software developed by the OpenSSL Project 2368651Skris * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 2468651Skris * 2568651Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 2668651Skris * endorse or promote products derived from this software without 2768651Skris * prior written permission. For written permission, please contact 2868651Skris * licensing@OpenSSL.org. 2968651Skris * 3068651Skris * 5. Products derived from this software may not be called "OpenSSL" 3168651Skris * nor may "OpenSSL" appear in their names without prior written 3268651Skris * permission of the OpenSSL Project. 3368651Skris * 3468651Skris * 6. Redistributions of any form whatsoever must retain the following 3568651Skris * acknowledgment: 3668651Skris * "This product includes software developed by the OpenSSL Project 3768651Skris * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 3868651Skris * 3968651Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 4068651Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4168651Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 4268651Skris * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 4368651Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 4468651Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 4568651Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 4668651Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4768651Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 4868651Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 4968651Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 5068651Skris * OF THE POSSIBILITY OF SUCH DAMAGE. 5168651Skris * ==================================================================== 5268651Skris * 5368651Skris * This product includes cryptographic software written by Eric Young 5468651Skris * (eay@cryptsoft.com). This product includes software written by Tim 5568651Skris * Hudson (tjh@cryptsoft.com). 5668651Skris * 5768651Skris */ 5872613Skris 59160814Ssimon#include <openssl/opensslconf.h> 60109998Smarkm#ifndef OPENSSL_NO_RSA 6172613Skris 6268651Skris#include "apps.h" 6368651Skris#include <string.h> 6468651Skris#include <openssl/err.h> 6568651Skris#include <openssl/pem.h> 66160814Ssimon#include <openssl/rsa.h> 6768651Skris 6868651Skris#define RSA_SIGN 1 6968651Skris#define RSA_VERIFY 2 7068651Skris#define RSA_ENCRYPT 3 7168651Skris#define RSA_DECRYPT 4 7268651Skris 7368651Skris#define KEY_PRIVKEY 1 7468651Skris#define KEY_PUBKEY 2 7568651Skris#define KEY_CERT 3 7668651Skris 7768651Skrisstatic void usage(void); 7868651Skris 7968651Skris#undef PROG 8068651Skris 8168651Skris#define PROG rsautl_main 8268651Skris 8368651Skrisint MAIN(int argc, char **); 8468651Skris 8568651Skrisint MAIN(int argc, char **argv) 8668651Skris{ 87109998Smarkm ENGINE *e = NULL; 8868651Skris BIO *in = NULL, *out = NULL; 8968651Skris char *infile = NULL, *outfile = NULL; 90111147Snectar#ifndef OPENSSL_NO_ENGINE 91109998Smarkm char *engine = NULL; 92111147Snectar#endif 9368651Skris char *keyfile = NULL; 9468651Skris char rsa_mode = RSA_VERIFY, key_type = KEY_PRIVKEY; 9568651Skris int keyform = FORMAT_PEM; 9668651Skris char need_priv = 0, badarg = 0, rev = 0; 9768651Skris char hexdump = 0, asn1parse = 0; 9868651Skris X509 *x; 9968651Skris EVP_PKEY *pkey = NULL; 10068651Skris RSA *rsa = NULL; 10168651Skris unsigned char *rsa_in = NULL, *rsa_out = NULL, pad; 102127128Snectar char *passargin = NULL, *passin = NULL; 10368651Skris int rsa_inlen, rsa_outlen = 0; 10468651Skris int keysize; 10568651Skris 10668651Skris int ret = 1; 10768651Skris 10868651Skris argc--; 10968651Skris argv++; 11068651Skris 11168651Skris if(!bio_err) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE); 112109998Smarkm 113109998Smarkm if (!load_config(bio_err, NULL)) 114109998Smarkm goto end; 11568651Skris ERR_load_crypto_strings(); 11668651Skris OpenSSL_add_all_algorithms(); 11768651Skris pad = RSA_PKCS1_PADDING; 11868651Skris 11968651Skris while(argc >= 1) 12068651Skris { 12168651Skris if (!strcmp(*argv,"-in")) { 122194206Ssimon if (--argc < 1) 123194206Ssimon badarg = 1; 124194206Ssimon else 125194206Ssimon infile= *(++argv); 12668651Skris } else if (!strcmp(*argv,"-out")) { 127194206Ssimon if (--argc < 1) 128194206Ssimon badarg = 1; 129194206Ssimon else 130194206Ssimon outfile= *(++argv); 13168651Skris } else if(!strcmp(*argv, "-inkey")) { 132194206Ssimon if (--argc < 1) 133194206Ssimon badarg = 1; 134194206Ssimon else 135194206Ssimon keyfile = *(++argv); 136127128Snectar } else if (!strcmp(*argv,"-passin")) { 137194206Ssimon if (--argc < 1) 138194206Ssimon badarg = 1; 139194206Ssimon else 140194206Ssimon passargin= *(++argv); 141109998Smarkm } else if (strcmp(*argv,"-keyform") == 0) { 142194206Ssimon if (--argc < 1) 143194206Ssimon badarg = 1; 144194206Ssimon else 145194206Ssimon keyform=str2fmt(*(++argv)); 146111147Snectar#ifndef OPENSSL_NO_ENGINE 147109998Smarkm } else if(!strcmp(*argv, "-engine")) { 148194206Ssimon if (--argc < 1) 149194206Ssimon badarg = 1; 150194206Ssimon else 151194206Ssimon engine = *(++argv); 152111147Snectar#endif 15368651Skris } else if(!strcmp(*argv, "-pubin")) { 15468651Skris key_type = KEY_PUBKEY; 15568651Skris } else if(!strcmp(*argv, "-certin")) { 15668651Skris key_type = KEY_CERT; 15768651Skris } 15868651Skris else if(!strcmp(*argv, "-asn1parse")) asn1parse = 1; 15968651Skris else if(!strcmp(*argv, "-hexdump")) hexdump = 1; 16068651Skris else if(!strcmp(*argv, "-raw")) pad = RSA_NO_PADDING; 16168651Skris else if(!strcmp(*argv, "-oaep")) pad = RSA_PKCS1_OAEP_PADDING; 16268651Skris else if(!strcmp(*argv, "-ssl")) pad = RSA_SSLV23_PADDING; 16368651Skris else if(!strcmp(*argv, "-pkcs")) pad = RSA_PKCS1_PADDING; 164160814Ssimon else if(!strcmp(*argv, "-x931")) pad = RSA_X931_PADDING; 16568651Skris else if(!strcmp(*argv, "-sign")) { 16668651Skris rsa_mode = RSA_SIGN; 16768651Skris need_priv = 1; 16868651Skris } else if(!strcmp(*argv, "-verify")) rsa_mode = RSA_VERIFY; 16968651Skris else if(!strcmp(*argv, "-rev")) rev = 1; 17068651Skris else if(!strcmp(*argv, "-encrypt")) rsa_mode = RSA_ENCRYPT; 17168651Skris else if(!strcmp(*argv, "-decrypt")) { 17268651Skris rsa_mode = RSA_DECRYPT; 17368651Skris need_priv = 1; 17468651Skris } else badarg = 1; 17568651Skris if(badarg) { 17668651Skris usage(); 17768651Skris goto end; 17868651Skris } 17968651Skris argc--; 18068651Skris argv++; 18168651Skris } 18268651Skris 18368651Skris if(need_priv && (key_type != KEY_PRIVKEY)) { 18468651Skris BIO_printf(bio_err, "A private key is needed for this operation\n"); 18568651Skris goto end; 18668651Skris } 18768651Skris 188111147Snectar#ifndef OPENSSL_NO_ENGINE 189109998Smarkm e = setup_engine(bio_err, engine, 0); 190111147Snectar#endif 191127128Snectar if(!app_passwd(bio_err, passargin, NULL, &passin, NULL)) { 192127128Snectar BIO_printf(bio_err, "Error getting password\n"); 193127128Snectar goto end; 194127128Snectar } 195109998Smarkm 19668651Skris/* FIXME: seed PRNG only if needed */ 19768651Skris app_RAND_load_file(NULL, bio_err, 0); 19868651Skris 19968651Skris switch(key_type) { 20068651Skris case KEY_PRIVKEY: 201109998Smarkm pkey = load_key(bio_err, keyfile, keyform, 0, 202127128Snectar passin, e, "Private Key"); 20368651Skris break; 20468651Skris 20568651Skris case KEY_PUBKEY: 206109998Smarkm pkey = load_pubkey(bio_err, keyfile, keyform, 0, 207109998Smarkm NULL, e, "Public Key"); 20868651Skris break; 20968651Skris 21068651Skris case KEY_CERT: 211109998Smarkm x = load_cert(bio_err, keyfile, keyform, 212109998Smarkm NULL, e, "Certificate"); 21368651Skris if(x) { 21468651Skris pkey = X509_get_pubkey(x); 21568651Skris X509_free(x); 21668651Skris } 21768651Skris break; 21868651Skris } 21968651Skris 22068651Skris if(!pkey) { 22168651Skris return 1; 22268651Skris } 22368651Skris 22468651Skris rsa = EVP_PKEY_get1_RSA(pkey); 22568651Skris EVP_PKEY_free(pkey); 22668651Skris 22768651Skris if(!rsa) { 22868651Skris BIO_printf(bio_err, "Error getting RSA key\n"); 22968651Skris ERR_print_errors(bio_err); 23068651Skris goto end; 23168651Skris } 23268651Skris 23368651Skris 23468651Skris if(infile) { 23568651Skris if(!(in = BIO_new_file(infile, "rb"))) { 23668651Skris BIO_printf(bio_err, "Error Reading Input File\n"); 23768651Skris ERR_print_errors(bio_err); 23868651Skris goto end; 23968651Skris } 24068651Skris } else in = BIO_new_fp(stdin, BIO_NOCLOSE); 24168651Skris 24268651Skris if(outfile) { 24368651Skris if(!(out = BIO_new_file(outfile, "wb"))) { 24468651Skris BIO_printf(bio_err, "Error Reading Output File\n"); 24568651Skris ERR_print_errors(bio_err); 24668651Skris goto end; 24768651Skris } 24868651Skris } else { 24968651Skris out = BIO_new_fp(stdout, BIO_NOCLOSE); 250109998Smarkm#ifdef OPENSSL_SYS_VMS 25168651Skris { 25268651Skris BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 25368651Skris out = BIO_push(tmpbio, out); 25468651Skris } 25568651Skris#endif 25668651Skris } 25768651Skris 25868651Skris keysize = RSA_size(rsa); 25968651Skris 26068651Skris rsa_in = OPENSSL_malloc(keysize * 2); 26168651Skris rsa_out = OPENSSL_malloc(keysize); 26268651Skris 26368651Skris /* Read the input data */ 26468651Skris rsa_inlen = BIO_read(in, rsa_in, keysize * 2); 26568651Skris if(rsa_inlen <= 0) { 26668651Skris BIO_printf(bio_err, "Error reading input Data\n"); 26768651Skris exit(1); 26868651Skris } 26968651Skris if(rev) { 27068651Skris int i; 27168651Skris unsigned char ctmp; 27268651Skris for(i = 0; i < rsa_inlen/2; i++) { 27368651Skris ctmp = rsa_in[i]; 27468651Skris rsa_in[i] = rsa_in[rsa_inlen - 1 - i]; 27568651Skris rsa_in[rsa_inlen - 1 - i] = ctmp; 27668651Skris } 27768651Skris } 27868651Skris switch(rsa_mode) { 27968651Skris 28068651Skris case RSA_VERIFY: 28168651Skris rsa_outlen = RSA_public_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad); 28268651Skris break; 28368651Skris 28468651Skris case RSA_SIGN: 28568651Skris rsa_outlen = RSA_private_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad); 28668651Skris break; 28768651Skris 28868651Skris case RSA_ENCRYPT: 28968651Skris rsa_outlen = RSA_public_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad); 29068651Skris break; 29168651Skris 29268651Skris case RSA_DECRYPT: 29368651Skris rsa_outlen = RSA_private_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad); 29468651Skris break; 29568651Skris 29668651Skris } 29768651Skris 29868651Skris if(rsa_outlen <= 0) { 29968651Skris BIO_printf(bio_err, "RSA operation error\n"); 30068651Skris ERR_print_errors(bio_err); 30168651Skris goto end; 30268651Skris } 30368651Skris ret = 0; 30468651Skris if(asn1parse) { 30568651Skris if(!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) { 30668651Skris ERR_print_errors(bio_err); 30768651Skris } 30868651Skris } else if(hexdump) BIO_dump(out, (char *)rsa_out, rsa_outlen); 30968651Skris else BIO_write(out, rsa_out, rsa_outlen); 31068651Skris end: 31168651Skris RSA_free(rsa); 31268651Skris BIO_free(in); 31368651Skris BIO_free_all(out); 31468651Skris if(rsa_in) OPENSSL_free(rsa_in); 31568651Skris if(rsa_out) OPENSSL_free(rsa_out); 316127128Snectar if(passin) OPENSSL_free(passin); 31768651Skris return ret; 31868651Skris} 31968651Skris 32068651Skrisstatic void usage() 32168651Skris{ 32268651Skris BIO_printf(bio_err, "Usage: rsautl [options]\n"); 32368651Skris BIO_printf(bio_err, "-in file input file\n"); 32468651Skris BIO_printf(bio_err, "-out file output file\n"); 32568651Skris BIO_printf(bio_err, "-inkey file input key\n"); 326109998Smarkm BIO_printf(bio_err, "-keyform arg private key format - default PEM\n"); 32768651Skris BIO_printf(bio_err, "-pubin input is an RSA public\n"); 32868651Skris BIO_printf(bio_err, "-certin input is a certificate carrying an RSA public key\n"); 32968651Skris BIO_printf(bio_err, "-ssl use SSL v2 padding\n"); 33068651Skris BIO_printf(bio_err, "-raw use no padding\n"); 33168651Skris BIO_printf(bio_err, "-pkcs use PKCS#1 v1.5 padding (default)\n"); 33268651Skris BIO_printf(bio_err, "-oaep use PKCS#1 OAEP\n"); 33368651Skris BIO_printf(bio_err, "-sign sign with private key\n"); 33468651Skris BIO_printf(bio_err, "-verify verify with public key\n"); 33568651Skris BIO_printf(bio_err, "-encrypt encrypt with public key\n"); 33668651Skris BIO_printf(bio_err, "-decrypt decrypt with private key\n"); 33768651Skris BIO_printf(bio_err, "-hexdump hex dump output\n"); 338111147Snectar#ifndef OPENSSL_NO_ENGINE 339109998Smarkm BIO_printf(bio_err, "-engine e use engine e, possibly a hardware device.\n"); 340127128Snectar BIO_printf (bio_err, "-passin arg pass phrase source\n"); 341111147Snectar#endif 342109998Smarkm 34368651Skris} 34468651Skris 345238405Sjkim#else /* !OPENSSL_NO_RSA */ 346238405Sjkim 347238405Sjkim# if PEDANTIC 348238405Sjkimstatic void *dummy=&dummy; 349238405Sjkim# endif 350238405Sjkim 35172613Skris#endif 352