1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/tsrc/BC/libcrypto/topenssl/src/rsautl.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,336 @@
1.4 +/* rsautl.c */
1.5 +/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
1.6 + * project 2000.
1.7 + */
1.8 +/* ====================================================================
1.9 + * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
1.10 + *
1.11 + * Redistribution and use in source and binary forms, with or without
1.12 + * modification, are permitted provided that the following conditions
1.13 + * are met:
1.14 + *
1.15 + * 1. Redistributions of source code must retain the above copyright
1.16 + * notice, this list of conditions and the following disclaimer.
1.17 + *
1.18 + * 2. Redistributions in binary form must reproduce the above copyright
1.19 + * notice, this list of conditions and the following disclaimer in
1.20 + * the documentation and/or other materials provided with the
1.21 + * distribution.
1.22 + *
1.23 + * 3. All advertising materials mentioning features or use of this
1.24 + * software must display the following acknowledgment:
1.25 + * "This product includes software developed by the OpenSSL Project
1.26 + * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
1.27 + *
1.28 + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
1.29 + * endorse or promote products derived from this software without
1.30 + * prior written permission. For written permission, please contact
1.31 + * licensing@OpenSSL.org.
1.32 + *
1.33 + * 5. Products derived from this software may not be called "OpenSSL"
1.34 + * nor may "OpenSSL" appear in their names without prior written
1.35 + * permission of the OpenSSL Project.
1.36 + *
1.37 + * 6. Redistributions of any form whatsoever must retain the following
1.38 + * acknowledgment:
1.39 + * "This product includes software developed by the OpenSSL Project
1.40 + * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
1.41 + *
1.42 + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
1.43 + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.44 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1.45 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
1.46 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1.47 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1.48 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1.49 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.50 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1.51 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1.52 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1.53 + * OF THE POSSIBILITY OF SUCH DAMAGE.
1.54 + * ====================================================================
1.55 + *
1.56 + * This product includes cryptographic software written by Eric Young
1.57 + * (eay@cryptsoft.com). This product includes software written by Tim
1.58 + * Hudson (tjh@cryptsoft.com).
1.59 + *
1.60 + */
1.61 +
1.62 +#include <openssl/opensslconf.h>
1.63 +#ifndef OPENSSL_NO_RSA
1.64 +
1.65 +#include "apps.h"
1.66 +#include <string.h>
1.67 +#include <openssl/err.h>
1.68 +#include <openssl/pem.h>
1.69 +#include <openssl/rsa.h>
1.70 +
1.71 +#define RSA_SIGN 1
1.72 +#define RSA_VERIFY 2
1.73 +#define RSA_ENCRYPT 3
1.74 +#define RSA_DECRYPT 4
1.75 +
1.76 +#define KEY_PRIVKEY 1
1.77 +#define KEY_PUBKEY 2
1.78 +#define KEY_CERT 3
1.79 +
1.80 +static void usage(void);
1.81 +
1.82 +#undef PROG
1.83 +
1.84 +#define PROG rsautl_main
1.85 +
1.86 +
1.87 +int MAIN(int argc, char **);
1.88 +
1.89 +int MAIN(int argc, char **argv)
1.90 +{
1.91 + ENGINE *e = NULL;
1.92 + BIO *in = NULL, *out = NULL;
1.93 + char *infile = NULL, *outfile = NULL;
1.94 +#ifndef OPENSSL_NO_ENGINE
1.95 + char *engine = NULL;
1.96 +#endif
1.97 + char *keyfile = NULL;
1.98 + char rsa_mode = RSA_VERIFY, key_type = KEY_PRIVKEY;
1.99 + int keyform = FORMAT_PEM;
1.100 + char need_priv = 0, badarg = 0, rev = 0;
1.101 + char hexdump = 0, asn1parse = 0;
1.102 + X509 *x;
1.103 + EVP_PKEY *pkey = NULL;
1.104 + RSA *rsa = NULL;
1.105 + unsigned char *rsa_in = NULL, *rsa_out = NULL, pad;
1.106 + char *passargin = NULL, *passin = NULL;
1.107 + int rsa_inlen, rsa_outlen = 0;
1.108 + int keysize;
1.109 +
1.110 + int ret = 1;
1.111 +
1.112 + argc--;
1.113 + argv++;
1.114 + if(!bio_err) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
1.115 +
1.116 + if (!load_config(bio_err, NULL))
1.117 + goto end;
1.118 + ERR_load_crypto_strings();
1.119 + OpenSSL_add_all_algorithms();
1.120 + pad = RSA_PKCS1_PADDING;
1.121 +
1.122 + while(argc >= 1)
1.123 + {
1.124 + if (!strcmp(*argv,"-in")) {
1.125 + if (--argc < 1) badarg = 1;
1.126 + infile= *(++argv);
1.127 + } else if (!strcmp(*argv,"-out")) {
1.128 + if (--argc < 1) badarg = 1;
1.129 + outfile= *(++argv);
1.130 + } else if(!strcmp(*argv, "-inkey")) {
1.131 + if (--argc < 1) badarg = 1;
1.132 + keyfile = *(++argv);
1.133 + } else if (!strcmp(*argv,"-passin")) {
1.134 + if (--argc < 1) badarg = 1;
1.135 + passargin= *(++argv);
1.136 + } else if (strcmp(*argv,"-keyform") == 0) {
1.137 + if (--argc < 1) badarg = 1;
1.138 + keyform=str2fmt(*(++argv));
1.139 +#ifndef OPENSSL_NO_ENGINE
1.140 + } else if(!strcmp(*argv, "-engine")) {
1.141 + if (--argc < 1) badarg = 1;
1.142 + engine = *(++argv);
1.143 +#endif
1.144 + } else if(!strcmp(*argv, "-pubin")) {
1.145 + key_type = KEY_PUBKEY;
1.146 + } else if(!strcmp(*argv, "-certin")) {
1.147 + key_type = KEY_CERT;
1.148 + }
1.149 + else if(!strcmp(*argv, "-asn1parse")) asn1parse = 1;
1.150 + else if(!strcmp(*argv, "-hexdump")) hexdump = 1;
1.151 + else if(!strcmp(*argv, "-raw")) pad = RSA_NO_PADDING;
1.152 + else if(!strcmp(*argv, "-oaep")) pad = RSA_PKCS1_OAEP_PADDING;
1.153 + else if(!strcmp(*argv, "-ssl")) pad = RSA_SSLV23_PADDING;
1.154 + else if(!strcmp(*argv, "-pkcs")) pad = RSA_PKCS1_PADDING;
1.155 + else if(!strcmp(*argv, "-x931")) pad = RSA_X931_PADDING;
1.156 + else if(!strcmp(*argv, "-sign")) {
1.157 + rsa_mode = RSA_SIGN;
1.158 + need_priv = 1;
1.159 + } else if(!strcmp(*argv, "-verify")) rsa_mode = RSA_VERIFY;
1.160 + else if(!strcmp(*argv, "-rev")) rev = 1;
1.161 + else if(!strcmp(*argv, "-encrypt")) rsa_mode = RSA_ENCRYPT;
1.162 + else if(!strcmp(*argv, "-decrypt")) {
1.163 + rsa_mode = RSA_DECRYPT;
1.164 + need_priv = 1;
1.165 + } else badarg = 1;
1.166 + if(badarg) {
1.167 + usage();
1.168 + goto end;
1.169 + }
1.170 + argc--;
1.171 + argv++;
1.172 + }
1.173 +
1.174 + if(need_priv && (key_type != KEY_PRIVKEY)) {
1.175 + BIO_printf(bio_err, "A private key is needed for this operation\n");
1.176 + goto end;
1.177 + }
1.178 +
1.179 +#ifndef OPENSSL_NO_ENGINE
1.180 + e = setup_engine(bio_err, engine, 0);
1.181 +#endif
1.182 + if(!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
1.183 + BIO_printf(bio_err, "Error getting password\n");
1.184 + goto end;
1.185 + }
1.186 +
1.187 +/* FIXME: seed PRNG only if needed */
1.188 + app_RAND_load_file(NULL, bio_err, 0);
1.189 +
1.190 + switch(key_type) {
1.191 + case KEY_PRIVKEY:
1.192 + pkey = load_key(bio_err, keyfile, keyform, 0,
1.193 + passin, e, "Private Key");
1.194 + break;
1.195 +
1.196 + case KEY_PUBKEY:
1.197 + pkey = load_pubkey(bio_err, keyfile, keyform, 0,
1.198 + NULL, e, "Public Key");
1.199 + break;
1.200 +
1.201 + case KEY_CERT:
1.202 + x = load_cert(bio_err, keyfile, keyform,
1.203 + NULL, e, "Certificate");
1.204 + if(x) {
1.205 + pkey = X509_get_pubkey(x);
1.206 + X509_free(x);
1.207 + }
1.208 + break;
1.209 + }
1.210 +
1.211 + if(!pkey) {
1.212 + return 1;
1.213 + }
1.214 +
1.215 + rsa = EVP_PKEY_get1_RSA(pkey);
1.216 + EVP_PKEY_free(pkey);
1.217 +
1.218 + if(!rsa) {
1.219 + BIO_printf(bio_err, "Error getting RSA key\n");
1.220 + ERR_print_errors(bio_err);
1.221 + goto end;
1.222 + }
1.223 +
1.224 +
1.225 + if(infile) {
1.226 + if(!(in = BIO_new_file(infile, "rb"))) {
1.227 + BIO_printf(bio_err, "Error Reading Input File\n");
1.228 + ERR_print_errors(bio_err);
1.229 + goto end;
1.230 + }
1.231 + }
1.232 + else
1.233 + in = BIO_new_fp(stdin, BIO_NOCLOSE);
1.234 +
1.235 + if(outfile) {
1.236 + if(!(out = BIO_new_file(outfile, "wb"))) {
1.237 + BIO_printf(bio_err, "Error Reading Output File\n");
1.238 + ERR_print_errors(bio_err);
1.239 + goto end;
1.240 + }
1.241 + } else {
1.242 +
1.243 + out = BIO_new_fp(stdout, BIO_NOCLOSE);
1.244 +#ifdef OPENSSL_SYS_VMS
1.245 + {
1.246 + BIO *tmpbio = BIO_new(BIO_f_linebuffer());
1.247 + out = BIO_push(tmpbio, out);
1.248 + }
1.249 +#endif
1.250 + }
1.251 +
1.252 + keysize = RSA_size(rsa);
1.253 +
1.254 + rsa_in = OPENSSL_malloc(keysize * 2);
1.255 + rsa_out = OPENSSL_malloc(keysize);
1.256 +
1.257 + /* Read the input data */
1.258 + rsa_inlen = BIO_read(in, rsa_in, keysize * 2);
1.259 + if(rsa_inlen <= 0) {
1.260 + BIO_printf(bio_err, "Error reading input Data\n");
1.261 + exit(1);
1.262 + }
1.263 + if(rev) {
1.264 + int i;
1.265 + unsigned char ctmp;
1.266 + for(i = 0; i < rsa_inlen/2; i++) {
1.267 + ctmp = rsa_in[i];
1.268 + rsa_in[i] = rsa_in[rsa_inlen - 1 - i];
1.269 + rsa_in[rsa_inlen - 1 - i] = ctmp;
1.270 + }
1.271 + }
1.272 + switch(rsa_mode) {
1.273 +
1.274 + case RSA_VERIFY:
1.275 + rsa_outlen = RSA_public_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
1.276 + break;
1.277 +
1.278 + case RSA_SIGN:
1.279 + rsa_outlen = RSA_private_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
1.280 + break;
1.281 +
1.282 + case RSA_ENCRYPT:
1.283 + rsa_outlen = RSA_public_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
1.284 + break;
1.285 +
1.286 + case RSA_DECRYPT:
1.287 + rsa_outlen = RSA_private_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
1.288 + break;
1.289 +
1.290 + }
1.291 +
1.292 + if(rsa_outlen <= 0) {
1.293 + BIO_printf(bio_err, "RSA operation error\n");
1.294 + ERR_print_errors(bio_err);
1.295 + goto end;
1.296 + }
1.297 + ret = 0;
1.298 + if(asn1parse) {
1.299 + if(!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) {
1.300 + ERR_print_errors(bio_err);
1.301 + }
1.302 + } else if(hexdump) BIO_dump(out, (char *)rsa_out, rsa_outlen);
1.303 + else BIO_write(out, rsa_out, rsa_outlen);
1.304 + end:
1.305 + RSA_free(rsa);
1.306 + BIO_free(in);
1.307 + BIO_free_all(out);
1.308 + if(rsa_in) OPENSSL_free(rsa_in);
1.309 + if(rsa_out) OPENSSL_free(rsa_out);
1.310 + if(passin) OPENSSL_free(passin);
1.311 + return ret;
1.312 +}
1.313 +
1.314 +static void usage()
1.315 +{
1.316 + BIO_printf(bio_err, "Usage: rsautl [options]\n");
1.317 + BIO_printf(bio_err, "-in file input file\n");
1.318 + BIO_printf(bio_err, "-out file output file\n");
1.319 + BIO_printf(bio_err, "-inkey file input key\n");
1.320 + BIO_printf(bio_err, "-keyform arg private key format - default PEM\n");
1.321 + BIO_printf(bio_err, "-pubin input is an RSA public\n");
1.322 + BIO_printf(bio_err, "-certin input is a certificate carrying an RSA public key\n");
1.323 + BIO_printf(bio_err, "-ssl use SSL v2 padding\n");
1.324 + BIO_printf(bio_err, "-raw use no padding\n");
1.325 + BIO_printf(bio_err, "-pkcs use PKCS#1 v1.5 padding (default)\n");
1.326 + BIO_printf(bio_err, "-oaep use PKCS#1 OAEP\n");
1.327 + BIO_printf(bio_err, "-sign sign with private key\n");
1.328 + BIO_printf(bio_err, "-verify verify with public key\n");
1.329 + BIO_printf(bio_err, "-encrypt encrypt with public key\n");
1.330 + BIO_printf(bio_err, "-decrypt decrypt with private key\n");
1.331 + BIO_printf(bio_err, "-hexdump hex dump output\n");
1.332 +#ifndef OPENSSL_NO_ENGINE
1.333 + BIO_printf(bio_err, "-engine e use engine e, possibly a hardware device.\n");
1.334 + BIO_printf (bio_err, "-passin arg pass phrase source\n");
1.335 +#endif
1.336 +
1.337 +}
1.338 +
1.339 +#endif