1*0Sstevel@tonic-gate /* rsautl.c */ 2*0Sstevel@tonic-gate /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 3*0Sstevel@tonic-gate * project 2000. 4*0Sstevel@tonic-gate */ 5*0Sstevel@tonic-gate /* ==================================================================== 6*0Sstevel@tonic-gate * Copyright (c) 2000 The OpenSSL Project. All rights reserved. 7*0Sstevel@tonic-gate * 8*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 9*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 10*0Sstevel@tonic-gate * are met: 11*0Sstevel@tonic-gate * 12*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 13*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 14*0Sstevel@tonic-gate * 15*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 16*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in 17*0Sstevel@tonic-gate * the documentation and/or other materials provided with the 18*0Sstevel@tonic-gate * distribution. 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this 21*0Sstevel@tonic-gate * software must display the following acknowledgment: 22*0Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project 23*0Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24*0Sstevel@tonic-gate * 25*0Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26*0Sstevel@tonic-gate * endorse or promote products derived from this software without 27*0Sstevel@tonic-gate * prior written permission. For written permission, please contact 28*0Sstevel@tonic-gate * licensing@OpenSSL.org. 29*0Sstevel@tonic-gate * 30*0Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL" 31*0Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written 32*0Sstevel@tonic-gate * permission of the OpenSSL Project. 33*0Sstevel@tonic-gate * 34*0Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following 35*0Sstevel@tonic-gate * acknowledgment: 36*0Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project 37*0Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38*0Sstevel@tonic-gate * 39*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40*0Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42*0Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43*0Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44*0Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45*0Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46*0Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48*0Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49*0Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50*0Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE. 51*0Sstevel@tonic-gate * ==================================================================== 52*0Sstevel@tonic-gate * 53*0Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young 54*0Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim 55*0Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com). 56*0Sstevel@tonic-gate * 57*0Sstevel@tonic-gate */ 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate #include "apps.h" 62*0Sstevel@tonic-gate #include <string.h> 63*0Sstevel@tonic-gate #include <openssl/err.h> 64*0Sstevel@tonic-gate #include <openssl/pem.h> 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate #define RSA_SIGN 1 67*0Sstevel@tonic-gate #define RSA_VERIFY 2 68*0Sstevel@tonic-gate #define RSA_ENCRYPT 3 69*0Sstevel@tonic-gate #define RSA_DECRYPT 4 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate #define KEY_PRIVKEY 1 72*0Sstevel@tonic-gate #define KEY_PUBKEY 2 73*0Sstevel@tonic-gate #define KEY_CERT 3 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate static void usage(void); 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate #undef PROG 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate #define PROG rsautl_main 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gate int MAIN(int argc, char **); 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate int MAIN(int argc, char **argv) 84*0Sstevel@tonic-gate { 85*0Sstevel@tonic-gate ENGINE *e = NULL; 86*0Sstevel@tonic-gate BIO *in = NULL, *out = NULL; 87*0Sstevel@tonic-gate char *infile = NULL, *outfile = NULL; 88*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE 89*0Sstevel@tonic-gate char *engine = NULL; 90*0Sstevel@tonic-gate #endif 91*0Sstevel@tonic-gate char *keyfile = NULL; 92*0Sstevel@tonic-gate char rsa_mode = RSA_VERIFY, key_type = KEY_PRIVKEY; 93*0Sstevel@tonic-gate int keyform = FORMAT_PEM; 94*0Sstevel@tonic-gate char need_priv = 0, badarg = 0, rev = 0; 95*0Sstevel@tonic-gate char hexdump = 0, asn1parse = 0; 96*0Sstevel@tonic-gate X509 *x; 97*0Sstevel@tonic-gate EVP_PKEY *pkey = NULL; 98*0Sstevel@tonic-gate RSA *rsa = NULL; 99*0Sstevel@tonic-gate unsigned char *rsa_in = NULL, *rsa_out = NULL, pad; 100*0Sstevel@tonic-gate char *passargin = NULL, *passin = NULL; 101*0Sstevel@tonic-gate int rsa_inlen, rsa_outlen = 0; 102*0Sstevel@tonic-gate int keysize; 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gate int ret = 1; 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate argc--; 107*0Sstevel@tonic-gate argv++; 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate if(!bio_err) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE); 110*0Sstevel@tonic-gate 111*0Sstevel@tonic-gate if (!load_config(bio_err, NULL)) 112*0Sstevel@tonic-gate goto end; 113*0Sstevel@tonic-gate ERR_load_crypto_strings(); 114*0Sstevel@tonic-gate OpenSSL_add_all_algorithms(); 115*0Sstevel@tonic-gate pad = RSA_PKCS1_PADDING; 116*0Sstevel@tonic-gate 117*0Sstevel@tonic-gate while(argc >= 1) 118*0Sstevel@tonic-gate { 119*0Sstevel@tonic-gate if (!strcmp(*argv,"-in")) { 120*0Sstevel@tonic-gate if (--argc < 1) badarg = 1; 121*0Sstevel@tonic-gate infile= *(++argv); 122*0Sstevel@tonic-gate } else if (!strcmp(*argv,"-out")) { 123*0Sstevel@tonic-gate if (--argc < 1) badarg = 1; 124*0Sstevel@tonic-gate outfile= *(++argv); 125*0Sstevel@tonic-gate } else if(!strcmp(*argv, "-inkey")) { 126*0Sstevel@tonic-gate if (--argc < 1) badarg = 1; 127*0Sstevel@tonic-gate keyfile = *(++argv); 128*0Sstevel@tonic-gate } else if (!strcmp(*argv,"-passin")) { 129*0Sstevel@tonic-gate if (--argc < 1) badarg = 1; 130*0Sstevel@tonic-gate passargin= *(++argv); 131*0Sstevel@tonic-gate } else if (strcmp(*argv,"-keyform") == 0) { 132*0Sstevel@tonic-gate if (--argc < 1) badarg = 1; 133*0Sstevel@tonic-gate keyform=str2fmt(*(++argv)); 134*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE 135*0Sstevel@tonic-gate } else if(!strcmp(*argv, "-engine")) { 136*0Sstevel@tonic-gate if (--argc < 1) badarg = 1; 137*0Sstevel@tonic-gate engine = *(++argv); 138*0Sstevel@tonic-gate #endif 139*0Sstevel@tonic-gate } else if(!strcmp(*argv, "-pubin")) { 140*0Sstevel@tonic-gate key_type = KEY_PUBKEY; 141*0Sstevel@tonic-gate } else if(!strcmp(*argv, "-certin")) { 142*0Sstevel@tonic-gate key_type = KEY_CERT; 143*0Sstevel@tonic-gate } 144*0Sstevel@tonic-gate else if(!strcmp(*argv, "-asn1parse")) asn1parse = 1; 145*0Sstevel@tonic-gate else if(!strcmp(*argv, "-hexdump")) hexdump = 1; 146*0Sstevel@tonic-gate else if(!strcmp(*argv, "-raw")) pad = RSA_NO_PADDING; 147*0Sstevel@tonic-gate else if(!strcmp(*argv, "-oaep")) pad = RSA_PKCS1_OAEP_PADDING; 148*0Sstevel@tonic-gate else if(!strcmp(*argv, "-ssl")) pad = RSA_SSLV23_PADDING; 149*0Sstevel@tonic-gate else if(!strcmp(*argv, "-pkcs")) pad = RSA_PKCS1_PADDING; 150*0Sstevel@tonic-gate else if(!strcmp(*argv, "-sign")) { 151*0Sstevel@tonic-gate rsa_mode = RSA_SIGN; 152*0Sstevel@tonic-gate need_priv = 1; 153*0Sstevel@tonic-gate } else if(!strcmp(*argv, "-verify")) rsa_mode = RSA_VERIFY; 154*0Sstevel@tonic-gate else if(!strcmp(*argv, "-rev")) rev = 1; 155*0Sstevel@tonic-gate else if(!strcmp(*argv, "-encrypt")) rsa_mode = RSA_ENCRYPT; 156*0Sstevel@tonic-gate else if(!strcmp(*argv, "-decrypt")) { 157*0Sstevel@tonic-gate rsa_mode = RSA_DECRYPT; 158*0Sstevel@tonic-gate need_priv = 1; 159*0Sstevel@tonic-gate } else badarg = 1; 160*0Sstevel@tonic-gate if(badarg) { 161*0Sstevel@tonic-gate usage(); 162*0Sstevel@tonic-gate goto end; 163*0Sstevel@tonic-gate } 164*0Sstevel@tonic-gate argc--; 165*0Sstevel@tonic-gate argv++; 166*0Sstevel@tonic-gate } 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate if(need_priv && (key_type != KEY_PRIVKEY)) { 169*0Sstevel@tonic-gate BIO_printf(bio_err, "A private key is needed for this operation\n"); 170*0Sstevel@tonic-gate goto end; 171*0Sstevel@tonic-gate } 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE 174*0Sstevel@tonic-gate e = setup_engine(bio_err, engine, 0); 175*0Sstevel@tonic-gate #endif 176*0Sstevel@tonic-gate if(!app_passwd(bio_err, passargin, NULL, &passin, NULL)) { 177*0Sstevel@tonic-gate BIO_printf(bio_err, "Error getting password\n"); 178*0Sstevel@tonic-gate goto end; 179*0Sstevel@tonic-gate } 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gate /* FIXME: seed PRNG only if needed */ 182*0Sstevel@tonic-gate app_RAND_load_file(NULL, bio_err, 0); 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate switch(key_type) { 185*0Sstevel@tonic-gate case KEY_PRIVKEY: 186*0Sstevel@tonic-gate pkey = load_key(bio_err, keyfile, keyform, 0, 187*0Sstevel@tonic-gate passin, e, "Private Key"); 188*0Sstevel@tonic-gate break; 189*0Sstevel@tonic-gate 190*0Sstevel@tonic-gate case KEY_PUBKEY: 191*0Sstevel@tonic-gate pkey = load_pubkey(bio_err, keyfile, keyform, 0, 192*0Sstevel@tonic-gate NULL, e, "Public Key"); 193*0Sstevel@tonic-gate break; 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gate case KEY_CERT: 196*0Sstevel@tonic-gate x = load_cert(bio_err, keyfile, keyform, 197*0Sstevel@tonic-gate NULL, e, "Certificate"); 198*0Sstevel@tonic-gate if(x) { 199*0Sstevel@tonic-gate pkey = X509_get_pubkey(x); 200*0Sstevel@tonic-gate X509_free(x); 201*0Sstevel@tonic-gate } 202*0Sstevel@tonic-gate break; 203*0Sstevel@tonic-gate } 204*0Sstevel@tonic-gate 205*0Sstevel@tonic-gate if(!pkey) { 206*0Sstevel@tonic-gate return 1; 207*0Sstevel@tonic-gate } 208*0Sstevel@tonic-gate 209*0Sstevel@tonic-gate rsa = EVP_PKEY_get1_RSA(pkey); 210*0Sstevel@tonic-gate EVP_PKEY_free(pkey); 211*0Sstevel@tonic-gate 212*0Sstevel@tonic-gate if(!rsa) { 213*0Sstevel@tonic-gate BIO_printf(bio_err, "Error getting RSA key\n"); 214*0Sstevel@tonic-gate ERR_print_errors(bio_err); 215*0Sstevel@tonic-gate goto end; 216*0Sstevel@tonic-gate } 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gate 219*0Sstevel@tonic-gate if(infile) { 220*0Sstevel@tonic-gate if(!(in = BIO_new_file(infile, "rb"))) { 221*0Sstevel@tonic-gate BIO_printf(bio_err, "Error Reading Input File\n"); 222*0Sstevel@tonic-gate ERR_print_errors(bio_err); 223*0Sstevel@tonic-gate goto end; 224*0Sstevel@tonic-gate } 225*0Sstevel@tonic-gate } else in = BIO_new_fp(stdin, BIO_NOCLOSE); 226*0Sstevel@tonic-gate 227*0Sstevel@tonic-gate if(outfile) { 228*0Sstevel@tonic-gate if(!(out = BIO_new_file(outfile, "wb"))) { 229*0Sstevel@tonic-gate BIO_printf(bio_err, "Error Reading Output File\n"); 230*0Sstevel@tonic-gate ERR_print_errors(bio_err); 231*0Sstevel@tonic-gate goto end; 232*0Sstevel@tonic-gate } 233*0Sstevel@tonic-gate } else { 234*0Sstevel@tonic-gate out = BIO_new_fp(stdout, BIO_NOCLOSE); 235*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS 236*0Sstevel@tonic-gate { 237*0Sstevel@tonic-gate BIO *tmpbio = BIO_new(BIO_f_linebuffer()); 238*0Sstevel@tonic-gate out = BIO_push(tmpbio, out); 239*0Sstevel@tonic-gate } 240*0Sstevel@tonic-gate #endif 241*0Sstevel@tonic-gate } 242*0Sstevel@tonic-gate 243*0Sstevel@tonic-gate keysize = RSA_size(rsa); 244*0Sstevel@tonic-gate 245*0Sstevel@tonic-gate rsa_in = OPENSSL_malloc(keysize * 2); 246*0Sstevel@tonic-gate rsa_out = OPENSSL_malloc(keysize); 247*0Sstevel@tonic-gate 248*0Sstevel@tonic-gate /* Read the input data */ 249*0Sstevel@tonic-gate rsa_inlen = BIO_read(in, rsa_in, keysize * 2); 250*0Sstevel@tonic-gate if(rsa_inlen <= 0) { 251*0Sstevel@tonic-gate BIO_printf(bio_err, "Error reading input Data\n"); 252*0Sstevel@tonic-gate exit(1); 253*0Sstevel@tonic-gate } 254*0Sstevel@tonic-gate if(rev) { 255*0Sstevel@tonic-gate int i; 256*0Sstevel@tonic-gate unsigned char ctmp; 257*0Sstevel@tonic-gate for(i = 0; i < rsa_inlen/2; i++) { 258*0Sstevel@tonic-gate ctmp = rsa_in[i]; 259*0Sstevel@tonic-gate rsa_in[i] = rsa_in[rsa_inlen - 1 - i]; 260*0Sstevel@tonic-gate rsa_in[rsa_inlen - 1 - i] = ctmp; 261*0Sstevel@tonic-gate } 262*0Sstevel@tonic-gate } 263*0Sstevel@tonic-gate switch(rsa_mode) { 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gate case RSA_VERIFY: 266*0Sstevel@tonic-gate rsa_outlen = RSA_public_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad); 267*0Sstevel@tonic-gate break; 268*0Sstevel@tonic-gate 269*0Sstevel@tonic-gate case RSA_SIGN: 270*0Sstevel@tonic-gate rsa_outlen = RSA_private_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad); 271*0Sstevel@tonic-gate break; 272*0Sstevel@tonic-gate 273*0Sstevel@tonic-gate case RSA_ENCRYPT: 274*0Sstevel@tonic-gate rsa_outlen = RSA_public_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad); 275*0Sstevel@tonic-gate break; 276*0Sstevel@tonic-gate 277*0Sstevel@tonic-gate case RSA_DECRYPT: 278*0Sstevel@tonic-gate rsa_outlen = RSA_private_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad); 279*0Sstevel@tonic-gate break; 280*0Sstevel@tonic-gate 281*0Sstevel@tonic-gate } 282*0Sstevel@tonic-gate 283*0Sstevel@tonic-gate if(rsa_outlen <= 0) { 284*0Sstevel@tonic-gate BIO_printf(bio_err, "RSA operation error\n"); 285*0Sstevel@tonic-gate ERR_print_errors(bio_err); 286*0Sstevel@tonic-gate goto end; 287*0Sstevel@tonic-gate } 288*0Sstevel@tonic-gate ret = 0; 289*0Sstevel@tonic-gate if(asn1parse) { 290*0Sstevel@tonic-gate if(!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) { 291*0Sstevel@tonic-gate ERR_print_errors(bio_err); 292*0Sstevel@tonic-gate } 293*0Sstevel@tonic-gate } else if(hexdump) BIO_dump(out, (char *)rsa_out, rsa_outlen); 294*0Sstevel@tonic-gate else BIO_write(out, rsa_out, rsa_outlen); 295*0Sstevel@tonic-gate end: 296*0Sstevel@tonic-gate RSA_free(rsa); 297*0Sstevel@tonic-gate BIO_free(in); 298*0Sstevel@tonic-gate BIO_free_all(out); 299*0Sstevel@tonic-gate if(rsa_in) OPENSSL_free(rsa_in); 300*0Sstevel@tonic-gate if(rsa_out) OPENSSL_free(rsa_out); 301*0Sstevel@tonic-gate if(passin) OPENSSL_free(passin); 302*0Sstevel@tonic-gate return ret; 303*0Sstevel@tonic-gate } 304*0Sstevel@tonic-gate 305*0Sstevel@tonic-gate static void usage() 306*0Sstevel@tonic-gate { 307*0Sstevel@tonic-gate BIO_printf(bio_err, "Usage: rsautl [options]\n"); 308*0Sstevel@tonic-gate BIO_printf(bio_err, "-in file input file\n"); 309*0Sstevel@tonic-gate BIO_printf(bio_err, "-out file output file\n"); 310*0Sstevel@tonic-gate BIO_printf(bio_err, "-inkey file input key\n"); 311*0Sstevel@tonic-gate BIO_printf(bio_err, "-keyform arg private key format - default PEM\n"); 312*0Sstevel@tonic-gate BIO_printf(bio_err, "-pubin input is an RSA public\n"); 313*0Sstevel@tonic-gate BIO_printf(bio_err, "-certin input is a certificate carrying an RSA public key\n"); 314*0Sstevel@tonic-gate BIO_printf(bio_err, "-ssl use SSL v2 padding\n"); 315*0Sstevel@tonic-gate BIO_printf(bio_err, "-raw use no padding\n"); 316*0Sstevel@tonic-gate BIO_printf(bio_err, "-pkcs use PKCS#1 v1.5 padding (default)\n"); 317*0Sstevel@tonic-gate BIO_printf(bio_err, "-oaep use PKCS#1 OAEP\n"); 318*0Sstevel@tonic-gate BIO_printf(bio_err, "-sign sign with private key\n"); 319*0Sstevel@tonic-gate BIO_printf(bio_err, "-verify verify with public key\n"); 320*0Sstevel@tonic-gate BIO_printf(bio_err, "-encrypt encrypt with public key\n"); 321*0Sstevel@tonic-gate BIO_printf(bio_err, "-decrypt decrypt with private key\n"); 322*0Sstevel@tonic-gate BIO_printf(bio_err, "-hexdump hex dump output\n"); 323*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE 324*0Sstevel@tonic-gate BIO_printf(bio_err, "-engine e use engine e, possibly a hardware device.\n"); 325*0Sstevel@tonic-gate BIO_printf (bio_err, "-passin arg pass phrase source\n"); 326*0Sstevel@tonic-gate #endif 327*0Sstevel@tonic-gate 328*0Sstevel@tonic-gate } 329*0Sstevel@tonic-gate 330*0Sstevel@tonic-gate #endif 331