00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 #include <openssl/opensslconf.h>
00060 #ifndef OPENSSL_NO_RSA
00061
00062 #include "apps.h"
00063 #include <string.h>
00064 #include <openssl/err.h>
00065 #include <openssl/pem.h>
00066 #include <openssl/rsa.h>
00067
00068 #define RSA_SIGN 1
00069 #define RSA_VERIFY 2
00070 #define RSA_ENCRYPT 3
00071 #define RSA_DECRYPT 4
00072
00073 #define KEY_PRIVKEY 1
00074 #define KEY_PUBKEY 2
00075 #define KEY_CERT 3
00076
00077 static void usage(void);
00078
00079 #undef PROG
00080
00081 #define PROG rsautl_main
00082
00083 int MAIN(int argc, char **);
00084
00085 int MAIN(int argc, char **argv)
00086 {
00087 ENGINE *e = NULL;
00088 BIO *in = NULL, *out = NULL;
00089 char *infile = NULL, *outfile = NULL;
00090 #ifndef OPENSSL_NO_ENGINE
00091 char *engine = NULL;
00092 #endif
00093 char *keyfile = NULL;
00094 char rsa_mode = RSA_VERIFY, key_type = KEY_PRIVKEY;
00095 int keyform = FORMAT_PEM;
00096 char need_priv = 0, badarg = 0, rev = 0;
00097 char hexdump = 0, asn1parse = 0;
00098 X509 *x;
00099 EVP_PKEY *pkey = NULL;
00100 RSA *rsa = NULL;
00101 unsigned char *rsa_in = NULL, *rsa_out = NULL, pad;
00102 char *passargin = NULL, *passin = NULL;
00103 int rsa_inlen, rsa_outlen = 0;
00104 int keysize;
00105
00106 int ret = 1;
00107
00108 argc--;
00109 argv++;
00110
00111 if(!bio_err) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
00112
00113 if (!load_config(bio_err, NULL))
00114 goto end;
00115 ERR_load_crypto_strings();
00116 OpenSSL_add_all_algorithms();
00117 pad = RSA_PKCS1_PADDING;
00118
00119 while(argc >= 1)
00120 {
00121 if (!strcmp(*argv,"-in")) {
00122 if (--argc < 1) badarg = 1;
00123 infile= *(++argv);
00124 } else if (!strcmp(*argv,"-out")) {
00125 if (--argc < 1) badarg = 1;
00126 outfile= *(++argv);
00127 } else if(!strcmp(*argv, "-inkey")) {
00128 if (--argc < 1) badarg = 1;
00129 keyfile = *(++argv);
00130 } else if (!strcmp(*argv,"-passin")) {
00131 if (--argc < 1) badarg = 1;
00132 passargin= *(++argv);
00133 } else if (strcmp(*argv,"-keyform") == 0) {
00134 if (--argc < 1) badarg = 1;
00135 keyform=str2fmt(*(++argv));
00136 #ifndef OPENSSL_NO_ENGINE
00137 } else if(!strcmp(*argv, "-engine")) {
00138 if (--argc < 1) badarg = 1;
00139 engine = *(++argv);
00140 #endif
00141 } else if(!strcmp(*argv, "-pubin")) {
00142 key_type = KEY_PUBKEY;
00143 } else if(!strcmp(*argv, "-certin")) {
00144 key_type = KEY_CERT;
00145 }
00146 else if(!strcmp(*argv, "-asn1parse")) asn1parse = 1;
00147 else if(!strcmp(*argv, "-hexdump")) hexdump = 1;
00148 else if(!strcmp(*argv, "-raw")) pad = RSA_NO_PADDING;
00149 else if(!strcmp(*argv, "-oaep")) pad = RSA_PKCS1_OAEP_PADDING;
00150 else if(!strcmp(*argv, "-ssl")) pad = RSA_SSLV23_PADDING;
00151 else if(!strcmp(*argv, "-pkcs")) pad = RSA_PKCS1_PADDING;
00152 else if(!strcmp(*argv, "-x931")) pad = RSA_X931_PADDING;
00153 else if(!strcmp(*argv, "-sign")) {
00154 rsa_mode = RSA_SIGN;
00155 need_priv = 1;
00156 } else if(!strcmp(*argv, "-verify")) rsa_mode = RSA_VERIFY;
00157 else if(!strcmp(*argv, "-rev")) rev = 1;
00158 else if(!strcmp(*argv, "-encrypt")) rsa_mode = RSA_ENCRYPT;
00159 else if(!strcmp(*argv, "-decrypt")) {
00160 rsa_mode = RSA_DECRYPT;
00161 need_priv = 1;
00162 } else badarg = 1;
00163 if(badarg) {
00164 usage();
00165 goto end;
00166 }
00167 argc--;
00168 argv++;
00169 }
00170
00171 if(need_priv && (key_type != KEY_PRIVKEY)) {
00172 BIO_printf(bio_err, "A private key is needed for this operation\n");
00173 goto end;
00174 }
00175
00176 #ifndef OPENSSL_NO_ENGINE
00177 e = setup_engine(bio_err, engine, 0);
00178 #endif
00179 if(!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
00180 BIO_printf(bio_err, "Error getting password\n");
00181 goto end;
00182 }
00183
00184
00185 app_RAND_load_file(NULL, bio_err, 0);
00186
00187 switch(key_type) {
00188 case KEY_PRIVKEY:
00189 pkey = load_key(bio_err, keyfile, keyform, 0,
00190 passin, e, "Private Key");
00191 break;
00192
00193 case KEY_PUBKEY:
00194 pkey = load_pubkey(bio_err, keyfile, keyform, 0,
00195 NULL, e, "Public Key");
00196 break;
00197
00198 case KEY_CERT:
00199 x = load_cert(bio_err, keyfile, keyform,
00200 NULL, e, "Certificate");
00201 if(x) {
00202 pkey = X509_get_pubkey(x);
00203 X509_free(x);
00204 }
00205 break;
00206 }
00207
00208 if(!pkey) {
00209 return 1;
00210 }
00211
00212 rsa = EVP_PKEY_get1_RSA(pkey);
00213 EVP_PKEY_free(pkey);
00214
00215 if(!rsa) {
00216 BIO_printf(bio_err, "Error getting RSA key\n");
00217 ERR_print_errors(bio_err);
00218 goto end;
00219 }
00220
00221
00222 if(infile) {
00223 if(!(in = BIO_new_file(infile, "rb"))) {
00224 BIO_printf(bio_err, "Error Reading Input File\n");
00225 ERR_print_errors(bio_err);
00226 goto end;
00227 }
00228 } else in = BIO_new_fp(stdin, BIO_NOCLOSE);
00229
00230 if(outfile) {
00231 if(!(out = BIO_new_file(outfile, "wb"))) {
00232 BIO_printf(bio_err, "Error Reading Output File\n");
00233 ERR_print_errors(bio_err);
00234 goto end;
00235 }
00236 } else {
00237 out = BIO_new_fp(stdout, BIO_NOCLOSE);
00238 #ifdef OPENSSL_SYS_VMS
00239 {
00240 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
00241 out = BIO_push(tmpbio, out);
00242 }
00243 #endif
00244 }
00245
00246 keysize = RSA_size(rsa);
00247
00248 rsa_in = OPENSSL_malloc(keysize * 2);
00249 rsa_out = OPENSSL_malloc(keysize);
00250
00251
00252 rsa_inlen = BIO_read(in, rsa_in, keysize * 2);
00253 if(rsa_inlen <= 0) {
00254 BIO_printf(bio_err, "Error reading input Data\n");
00255 exit(1);
00256 }
00257 if(rev) {
00258 int i;
00259 unsigned char ctmp;
00260 for(i = 0; i < rsa_inlen/2; i++) {
00261 ctmp = rsa_in[i];
00262 rsa_in[i] = rsa_in[rsa_inlen - 1 - i];
00263 rsa_in[rsa_inlen - 1 - i] = ctmp;
00264 }
00265 }
00266 switch(rsa_mode) {
00267
00268 case RSA_VERIFY:
00269 rsa_outlen = RSA_public_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
00270 break;
00271
00272 case RSA_SIGN:
00273 rsa_outlen = RSA_private_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
00274 break;
00275
00276 case RSA_ENCRYPT:
00277 rsa_outlen = RSA_public_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
00278 break;
00279
00280 case RSA_DECRYPT:
00281 rsa_outlen = RSA_private_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
00282 break;
00283
00284 }
00285
00286 if(rsa_outlen <= 0) {
00287 BIO_printf(bio_err, "RSA operation error\n");
00288 ERR_print_errors(bio_err);
00289 goto end;
00290 }
00291 ret = 0;
00292 if(asn1parse) {
00293 if(!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) {
00294 ERR_print_errors(bio_err);
00295 }
00296 } else if(hexdump) BIO_dump(out, (char *)rsa_out, rsa_outlen);
00297 else BIO_write(out, rsa_out, rsa_outlen);
00298 end:
00299 RSA_free(rsa);
00300 BIO_free(in);
00301 BIO_free_all(out);
00302 if(rsa_in) OPENSSL_free(rsa_in);
00303 if(rsa_out) OPENSSL_free(rsa_out);
00304 if(passin) OPENSSL_free(passin);
00305 return ret;
00306 }
00307
00308 static void usage()
00309 {
00310 BIO_printf(bio_err, "Usage: rsautl [options]\n");
00311 BIO_printf(bio_err, "-in file input file\n");
00312 BIO_printf(bio_err, "-out file output file\n");
00313 BIO_printf(bio_err, "-inkey file input key\n");
00314 BIO_printf(bio_err, "-keyform arg private key format - default PEM\n");
00315 BIO_printf(bio_err, "-pubin input is an RSA public\n");
00316 BIO_printf(bio_err, "-certin input is a certificate carrying an RSA public key\n");
00317 BIO_printf(bio_err, "-ssl use SSL v2 padding\n");
00318 BIO_printf(bio_err, "-raw use no padding\n");
00319 BIO_printf(bio_err, "-pkcs use PKCS#1 v1.5 padding (default)\n");
00320 BIO_printf(bio_err, "-oaep use PKCS#1 OAEP\n");
00321 BIO_printf(bio_err, "-sign sign with private key\n");
00322 BIO_printf(bio_err, "-verify verify with public key\n");
00323 BIO_printf(bio_err, "-encrypt encrypt with public key\n");
00324 BIO_printf(bio_err, "-decrypt decrypt with private key\n");
00325 BIO_printf(bio_err, "-hexdump hex dump output\n");
00326 #ifndef OPENSSL_NO_ENGINE
00327 BIO_printf(bio_err, "-engine e use engine e, possibly a hardware device.\n");
00328 BIO_printf (bio_err, "-passin arg pass phrase source\n");
00329 #endif
00330
00331 }
00332
00333 #endif