rsautl.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. #ifndef OPENSSL_NO_RSA
  59. #include "apps.h"
  60. #include <string.h>
  61. #include <openssl/err.h>
  62. #include <openssl/pem.h>
  63. #include <openssl/engine.h>
  64. #define RSA_SIGN 1
  65. #define RSA_VERIFY 2
  66. #define RSA_ENCRYPT 3
  67. #define RSA_DECRYPT 4
  68. #define KEY_PRIVKEY 1
  69. #define KEY_PUBKEY 2
  70. #define KEY_CERT 3
  71. static void usage(void);
  72. #undef PROG
  73. #define PROG rsautl_main
  74. int MAIN(int argc, char **);
  75. int MAIN(int argc, char **argv)
  76. {
  77. ENGINE *e = NULL;
  78. BIO *in = NULL, *out = NULL;
  79. char *infile = NULL, *outfile = NULL;
  80. char *engine = NULL;
  81. char *keyfile = NULL;
  82. char rsa_mode = RSA_VERIFY, key_type = KEY_PRIVKEY;
  83. int keyform = FORMAT_PEM;
  84. char need_priv = 0, badarg = 0, rev = 0;
  85. char hexdump = 0, asn1parse = 0;
  86. X509 *x;
  87. EVP_PKEY *pkey = NULL;
  88. RSA *rsa = NULL;
  89. unsigned char *rsa_in = NULL, *rsa_out = NULL, pad;
  90. int rsa_inlen, rsa_outlen = 0;
  91. int keysize;
  92. int ret = 1;
  93. argc--;
  94. argv++;
  95. if(!bio_err) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  96. ERR_load_crypto_strings();
  97. OpenSSL_add_all_algorithms();
  98. pad = RSA_PKCS1_PADDING;
  99. while(argc >= 1)
  100. {
  101. if (!strcmp(*argv,"-in")) {
  102. if (--argc < 1) badarg = 1;
  103. infile= *(++argv);
  104. } else if (!strcmp(*argv,"-out")) {
  105. if (--argc < 1) badarg = 1;
  106. outfile= *(++argv);
  107. } else if(!strcmp(*argv, "-inkey")) {
  108. if (--argc < 1) badarg = 1;
  109. keyfile = *(++argv);
  110. } else if(!strcmp(*argv, "-engine")) {
  111. if (--argc < 1) badarg = 1;
  112. engine = *(++argv);
  113. } else if(!strcmp(*argv, "-pubin")) {
  114. key_type = KEY_PUBKEY;
  115. } else if(!strcmp(*argv, "-certin")) {
  116. key_type = KEY_CERT;
  117. }
  118. else if(!strcmp(*argv, "-asn1parse")) asn1parse = 1;
  119. else if(!strcmp(*argv, "-hexdump")) hexdump = 1;
  120. else if(!strcmp(*argv, "-raw")) pad = RSA_NO_PADDING;
  121. else if(!strcmp(*argv, "-oaep")) pad = RSA_PKCS1_OAEP_PADDING;
  122. else if(!strcmp(*argv, "-ssl")) pad = RSA_SSLV23_PADDING;
  123. else if(!strcmp(*argv, "-pkcs")) pad = RSA_PKCS1_PADDING;
  124. else if(!strcmp(*argv, "-sign")) {
  125. rsa_mode = RSA_SIGN;
  126. need_priv = 1;
  127. } else if(!strcmp(*argv, "-verify")) rsa_mode = RSA_VERIFY;
  128. else if(!strcmp(*argv, "-rev")) rev = 1;
  129. else if(!strcmp(*argv, "-encrypt")) rsa_mode = RSA_ENCRYPT;
  130. else if(!strcmp(*argv, "-decrypt")) {
  131. rsa_mode = RSA_DECRYPT;
  132. need_priv = 1;
  133. } else badarg = 1;
  134. if(badarg) {
  135. usage();
  136. goto end;
  137. }
  138. argc--;
  139. argv++;
  140. }
  141. if(need_priv && (key_type != KEY_PRIVKEY)) {
  142. BIO_printf(bio_err, "A private key is needed for this operation\n");
  143. goto end;
  144. }
  145. if (engine != NULL)
  146. {
  147. if((e = ENGINE_by_id(engine)) == NULL)
  148. {
  149. BIO_printf(bio_err,"invalid engine \"%s\"\n",
  150. engine);
  151. goto end;
  152. }
  153. if(!ENGINE_set_default(e, ENGINE_METHOD_ALL))
  154. {
  155. BIO_printf(bio_err,"can't use that engine\n");
  156. goto end;
  157. }
  158. BIO_printf(bio_err,"engine \"%s\" set.\n", engine);
  159. /* Free our "structural" reference. */
  160. ENGINE_free(e);
  161. }
  162. /* FIXME: seed PRNG only if needed */
  163. app_RAND_load_file(NULL, bio_err, 0);
  164. switch(key_type) {
  165. case KEY_PRIVKEY:
  166. pkey = load_key(bio_err, keyfile, keyform, NULL, e);
  167. break;
  168. case KEY_PUBKEY:
  169. pkey = load_pubkey(bio_err, keyfile, keyform, e);
  170. break;
  171. case KEY_CERT:
  172. x = load_cert(bio_err, keyfile, keyform);
  173. if(x) {
  174. pkey = X509_get_pubkey(x);
  175. X509_free(x);
  176. }
  177. break;
  178. }
  179. if(!pkey) {
  180. BIO_printf(bio_err, "Error loading key\n");
  181. return 1;
  182. }
  183. rsa = EVP_PKEY_get1_RSA(pkey);
  184. EVP_PKEY_free(pkey);
  185. if(!rsa) {
  186. BIO_printf(bio_err, "Error getting RSA key\n");
  187. ERR_print_errors(bio_err);
  188. goto end;
  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. if(outfile) {
  198. if(!(out = BIO_new_file(outfile, "wb"))) {
  199. BIO_printf(bio_err, "Error Reading Output File\n");
  200. ERR_print_errors(bio_err);
  201. goto end;
  202. }
  203. } else {
  204. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  205. #ifdef OPENSSL_SYS_VMS
  206. {
  207. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  208. out = BIO_push(tmpbio, out);
  209. }
  210. #endif
  211. }
  212. keysize = RSA_size(rsa);
  213. rsa_in = OPENSSL_malloc(keysize * 2);
  214. rsa_out = OPENSSL_malloc(keysize);
  215. /* Read the input data */
  216. rsa_inlen = BIO_read(in, rsa_in, keysize * 2);
  217. if(rsa_inlen <= 0) {
  218. BIO_printf(bio_err, "Error reading input Data\n");
  219. exit(1);
  220. }
  221. if(rev) {
  222. int i;
  223. unsigned char ctmp;
  224. for(i = 0; i < rsa_inlen/2; i++) {
  225. ctmp = rsa_in[i];
  226. rsa_in[i] = rsa_in[rsa_inlen - 1 - i];
  227. rsa_in[rsa_inlen - 1 - i] = ctmp;
  228. }
  229. }
  230. switch(rsa_mode) {
  231. case RSA_VERIFY:
  232. rsa_outlen = RSA_public_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
  233. break;
  234. case RSA_SIGN:
  235. rsa_outlen = RSA_private_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
  236. break;
  237. case RSA_ENCRYPT:
  238. rsa_outlen = RSA_public_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
  239. break;
  240. case RSA_DECRYPT:
  241. rsa_outlen = RSA_private_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
  242. break;
  243. }
  244. if(rsa_outlen <= 0) {
  245. BIO_printf(bio_err, "RSA operation error\n");
  246. ERR_print_errors(bio_err);
  247. goto end;
  248. }
  249. ret = 0;
  250. if(asn1parse) {
  251. if(!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) {
  252. ERR_print_errors(bio_err);
  253. }
  254. } else if(hexdump) BIO_dump(out, (char *)rsa_out, rsa_outlen);
  255. else BIO_write(out, rsa_out, rsa_outlen);
  256. end:
  257. RSA_free(rsa);
  258. BIO_free(in);
  259. BIO_free_all(out);
  260. if(rsa_in) OPENSSL_free(rsa_in);
  261. if(rsa_out) OPENSSL_free(rsa_out);
  262. return ret;
  263. }
  264. static void usage()
  265. {
  266. BIO_printf(bio_err, "Usage: rsautl [options]\n");
  267. BIO_printf(bio_err, "-in file input file\n");
  268. BIO_printf(bio_err, "-out file output file\n");
  269. BIO_printf(bio_err, "-inkey file input key\n");
  270. BIO_printf(bio_err, "-pubin input is an RSA public\n");
  271. BIO_printf(bio_err, "-certin input is a certificate carrying an RSA public key\n");
  272. BIO_printf(bio_err, "-ssl use SSL v2 padding\n");
  273. BIO_printf(bio_err, "-raw use no padding\n");
  274. BIO_printf(bio_err, "-pkcs use PKCS#1 v1.5 padding (default)\n");
  275. BIO_printf(bio_err, "-oaep use PKCS#1 OAEP\n");
  276. BIO_printf(bio_err, "-sign sign with private key\n");
  277. BIO_printf(bio_err, "-verify verify with public key\n");
  278. BIO_printf(bio_err, "-encrypt encrypt with public key\n");
  279. BIO_printf(bio_err, "-decrypt decrypt with private key\n");
  280. BIO_printf(bio_err, "-hexdump hex dump output\n");
  281. }
  282. #endif