sl@0: /* rsautl.c */ sl@0: /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL sl@0: * project 2000. sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 2000 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * licensing@OpenSSL.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: sl@0: #include sl@0: #ifndef OPENSSL_NO_RSA sl@0: sl@0: #include "apps.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #define RSA_SIGN 1 sl@0: #define RSA_VERIFY 2 sl@0: #define RSA_ENCRYPT 3 sl@0: #define RSA_DECRYPT 4 sl@0: sl@0: #define KEY_PRIVKEY 1 sl@0: #define KEY_PUBKEY 2 sl@0: #define KEY_CERT 3 sl@0: sl@0: static void usage(void); sl@0: sl@0: #undef PROG sl@0: sl@0: #define PROG rsautl_main sl@0: sl@0: sl@0: int MAIN(int argc, char **); sl@0: sl@0: int MAIN(int argc, char **argv) sl@0: { sl@0: ENGINE *e = NULL; sl@0: BIO *in = NULL, *out = NULL; sl@0: char *infile = NULL, *outfile = NULL; sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: char *engine = NULL; sl@0: #endif sl@0: char *keyfile = NULL; sl@0: char rsa_mode = RSA_VERIFY, key_type = KEY_PRIVKEY; sl@0: int keyform = FORMAT_PEM; sl@0: char need_priv = 0, badarg = 0, rev = 0; sl@0: char hexdump = 0, asn1parse = 0; sl@0: X509 *x; sl@0: EVP_PKEY *pkey = NULL; sl@0: RSA *rsa = NULL; sl@0: unsigned char *rsa_in = NULL, *rsa_out = NULL, pad; sl@0: char *passargin = NULL, *passin = NULL; sl@0: int rsa_inlen, rsa_outlen = 0; sl@0: int keysize; sl@0: sl@0: int ret = 1; sl@0: sl@0: argc--; sl@0: argv++; sl@0: if(!bio_err) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE); sl@0: sl@0: if (!load_config(bio_err, NULL)) sl@0: goto end; sl@0: ERR_load_crypto_strings(); sl@0: OpenSSL_add_all_algorithms(); sl@0: pad = RSA_PKCS1_PADDING; sl@0: sl@0: while(argc >= 1) sl@0: { sl@0: if (!strcmp(*argv,"-in")) { sl@0: if (--argc < 1) badarg = 1; sl@0: infile= *(++argv); sl@0: } else if (!strcmp(*argv,"-out")) { sl@0: if (--argc < 1) badarg = 1; sl@0: outfile= *(++argv); sl@0: } else if(!strcmp(*argv, "-inkey")) { sl@0: if (--argc < 1) badarg = 1; sl@0: keyfile = *(++argv); sl@0: } else if (!strcmp(*argv,"-passin")) { sl@0: if (--argc < 1) badarg = 1; sl@0: passargin= *(++argv); sl@0: } else if (strcmp(*argv,"-keyform") == 0) { sl@0: if (--argc < 1) badarg = 1; sl@0: keyform=str2fmt(*(++argv)); sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: } else if(!strcmp(*argv, "-engine")) { sl@0: if (--argc < 1) badarg = 1; sl@0: engine = *(++argv); sl@0: #endif sl@0: } else if(!strcmp(*argv, "-pubin")) { sl@0: key_type = KEY_PUBKEY; sl@0: } else if(!strcmp(*argv, "-certin")) { sl@0: key_type = KEY_CERT; sl@0: } sl@0: else if(!strcmp(*argv, "-asn1parse")) asn1parse = 1; sl@0: else if(!strcmp(*argv, "-hexdump")) hexdump = 1; sl@0: else if(!strcmp(*argv, "-raw")) pad = RSA_NO_PADDING; sl@0: else if(!strcmp(*argv, "-oaep")) pad = RSA_PKCS1_OAEP_PADDING; sl@0: else if(!strcmp(*argv, "-ssl")) pad = RSA_SSLV23_PADDING; sl@0: else if(!strcmp(*argv, "-pkcs")) pad = RSA_PKCS1_PADDING; sl@0: else if(!strcmp(*argv, "-x931")) pad = RSA_X931_PADDING; sl@0: else if(!strcmp(*argv, "-sign")) { sl@0: rsa_mode = RSA_SIGN; sl@0: need_priv = 1; sl@0: } else if(!strcmp(*argv, "-verify")) rsa_mode = RSA_VERIFY; sl@0: else if(!strcmp(*argv, "-rev")) rev = 1; sl@0: else if(!strcmp(*argv, "-encrypt")) rsa_mode = RSA_ENCRYPT; sl@0: else if(!strcmp(*argv, "-decrypt")) { sl@0: rsa_mode = RSA_DECRYPT; sl@0: need_priv = 1; sl@0: } else badarg = 1; sl@0: if(badarg) { sl@0: usage(); sl@0: goto end; sl@0: } sl@0: argc--; sl@0: argv++; sl@0: } sl@0: sl@0: if(need_priv && (key_type != KEY_PRIVKEY)) { sl@0: BIO_printf(bio_err, "A private key is needed for this operation\n"); sl@0: goto end; sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: e = setup_engine(bio_err, engine, 0); sl@0: #endif sl@0: if(!app_passwd(bio_err, passargin, NULL, &passin, NULL)) { sl@0: BIO_printf(bio_err, "Error getting password\n"); sl@0: goto end; sl@0: } sl@0: sl@0: /* FIXME: seed PRNG only if needed */ sl@0: app_RAND_load_file(NULL, bio_err, 0); sl@0: sl@0: switch(key_type) { sl@0: case KEY_PRIVKEY: sl@0: pkey = load_key(bio_err, keyfile, keyform, 0, sl@0: passin, e, "Private Key"); sl@0: break; sl@0: sl@0: case KEY_PUBKEY: sl@0: pkey = load_pubkey(bio_err, keyfile, keyform, 0, sl@0: NULL, e, "Public Key"); sl@0: break; sl@0: sl@0: case KEY_CERT: sl@0: x = load_cert(bio_err, keyfile, keyform, sl@0: NULL, e, "Certificate"); sl@0: if(x) { sl@0: pkey = X509_get_pubkey(x); sl@0: X509_free(x); sl@0: } sl@0: break; sl@0: } sl@0: sl@0: if(!pkey) { sl@0: return 1; sl@0: } sl@0: sl@0: rsa = EVP_PKEY_get1_RSA(pkey); sl@0: EVP_PKEY_free(pkey); sl@0: sl@0: if(!rsa) { sl@0: BIO_printf(bio_err, "Error getting RSA key\n"); sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: sl@0: sl@0: if(infile) { sl@0: if(!(in = BIO_new_file(infile, "rb"))) { sl@0: BIO_printf(bio_err, "Error Reading Input File\n"); sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: } sl@0: else sl@0: in = BIO_new_fp(stdin, BIO_NOCLOSE); sl@0: sl@0: if(outfile) { sl@0: if(!(out = BIO_new_file(outfile, "wb"))) { sl@0: BIO_printf(bio_err, "Error Reading Output File\n"); sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: } else { sl@0: sl@0: out = BIO_new_fp(stdout, BIO_NOCLOSE); sl@0: #ifdef OPENSSL_SYS_VMS sl@0: { sl@0: BIO *tmpbio = BIO_new(BIO_f_linebuffer()); sl@0: out = BIO_push(tmpbio, out); sl@0: } sl@0: #endif sl@0: } sl@0: sl@0: keysize = RSA_size(rsa); sl@0: sl@0: rsa_in = OPENSSL_malloc(keysize * 2); sl@0: rsa_out = OPENSSL_malloc(keysize); sl@0: sl@0: /* Read the input data */ sl@0: rsa_inlen = BIO_read(in, rsa_in, keysize * 2); sl@0: if(rsa_inlen <= 0) { sl@0: BIO_printf(bio_err, "Error reading input Data\n"); sl@0: exit(1); sl@0: } sl@0: if(rev) { sl@0: int i; sl@0: unsigned char ctmp; sl@0: for(i = 0; i < rsa_inlen/2; i++) { sl@0: ctmp = rsa_in[i]; sl@0: rsa_in[i] = rsa_in[rsa_inlen - 1 - i]; sl@0: rsa_in[rsa_inlen - 1 - i] = ctmp; sl@0: } sl@0: } sl@0: switch(rsa_mode) { sl@0: sl@0: case RSA_VERIFY: sl@0: rsa_outlen = RSA_public_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad); sl@0: break; sl@0: sl@0: case RSA_SIGN: sl@0: rsa_outlen = RSA_private_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad); sl@0: break; sl@0: sl@0: case RSA_ENCRYPT: sl@0: rsa_outlen = RSA_public_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad); sl@0: break; sl@0: sl@0: case RSA_DECRYPT: sl@0: rsa_outlen = RSA_private_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad); sl@0: break; sl@0: sl@0: } sl@0: sl@0: if(rsa_outlen <= 0) { sl@0: BIO_printf(bio_err, "RSA operation error\n"); sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: ret = 0; sl@0: if(asn1parse) { sl@0: if(!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) { sl@0: ERR_print_errors(bio_err); sl@0: } sl@0: } else if(hexdump) BIO_dump(out, (char *)rsa_out, rsa_outlen); sl@0: else BIO_write(out, rsa_out, rsa_outlen); sl@0: end: sl@0: RSA_free(rsa); sl@0: BIO_free(in); sl@0: BIO_free_all(out); sl@0: if(rsa_in) OPENSSL_free(rsa_in); sl@0: if(rsa_out) OPENSSL_free(rsa_out); sl@0: if(passin) OPENSSL_free(passin); sl@0: return ret; sl@0: } sl@0: sl@0: static void usage() sl@0: { sl@0: BIO_printf(bio_err, "Usage: rsautl [options]\n"); sl@0: BIO_printf(bio_err, "-in file input file\n"); sl@0: BIO_printf(bio_err, "-out file output file\n"); sl@0: BIO_printf(bio_err, "-inkey file input key\n"); sl@0: BIO_printf(bio_err, "-keyform arg private key format - default PEM\n"); sl@0: BIO_printf(bio_err, "-pubin input is an RSA public\n"); sl@0: BIO_printf(bio_err, "-certin input is a certificate carrying an RSA public key\n"); sl@0: BIO_printf(bio_err, "-ssl use SSL v2 padding\n"); sl@0: BIO_printf(bio_err, "-raw use no padding\n"); sl@0: BIO_printf(bio_err, "-pkcs use PKCS#1 v1.5 padding (default)\n"); sl@0: BIO_printf(bio_err, "-oaep use PKCS#1 OAEP\n"); sl@0: BIO_printf(bio_err, "-sign sign with private key\n"); sl@0: BIO_printf(bio_err, "-verify verify with public key\n"); sl@0: BIO_printf(bio_err, "-encrypt encrypt with public key\n"); sl@0: BIO_printf(bio_err, "-decrypt decrypt with private key\n"); sl@0: BIO_printf(bio_err, "-hexdump hex dump output\n"); sl@0: #ifndef OPENSSL_NO_ENGINE sl@0: BIO_printf(bio_err, "-engine e use engine e, possibly a hardware device.\n"); sl@0: BIO_printf (bio_err, "-passin arg pass phrase source\n"); sl@0: #endif sl@0: sl@0: } sl@0: sl@0: #endif