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