rsautl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  3. * 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. #include <openssl/opensslconf.h>
  59. #ifndef OPENSSL_NO_RSA
  60. # include "apps.h"
  61. # include <string.h>
  62. # include <openssl/err.h>
  63. # include <openssl/pem.h>
  64. # include <openssl/rsa.h>
  65. # define RSA_SIGN 1
  66. # define RSA_VERIFY 2
  67. # define RSA_ENCRYPT 3
  68. # define RSA_DECRYPT 4
  69. # define KEY_PRIVKEY 1
  70. # define KEY_PUBKEY 2
  71. # define KEY_CERT 3
  72. typedef enum OPTION_choice {
  73. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  74. OPT_ENGINE, OPT_IN, OPT_OUT, OPT_ASN1PARSE, OPT_HEXDUMP,
  75. OPT_RAW, OPT_OAEP, OPT_SSL, OPT_PKCS, OPT_X931,
  76. OPT_SIGN, OPT_VERIFY, OPT_REV, OPT_ENCRYPT, OPT_DECRYPT,
  77. OPT_PUBIN, OPT_CERTIN, OPT_INKEY, OPT_PASSIN, OPT_KEYFORM
  78. } OPTION_CHOICE;
  79. OPTIONS rsautl_options[] = {
  80. {"help", OPT_HELP, '-', "Display this summary"},
  81. {"in", OPT_IN, '<', "Input file"},
  82. {"out", OPT_OUT, '>', "Output file"},
  83. {"inkey", OPT_INKEY, '<', "Input key"},
  84. {"keyform", OPT_KEYFORM, 'F', "Private key format - default PEM"},
  85. {"pubin", OPT_PUBIN, '-', "Input is an RSA public"},
  86. {"certin", OPT_CERTIN, '-', "Input is a cert carrying an RSA public key"},
  87. {"ssl", OPT_SSL, '-', "Use SSL v2 padding"},
  88. {"raw", OPT_RAW, '-', "Use no padding"},
  89. {"pkcs", OPT_PKCS, '-', "Use PKCS#1 v1.5 padding (default)"},
  90. {"oaep", OPT_OAEP, '-', "Use PKCS#1 OAEP"},
  91. {"sign", OPT_SIGN, '-', "Sign with private key"},
  92. {"verify", OPT_VERIFY, '-', "Verify with public key"},
  93. {"asn1parse", OPT_ASN1PARSE, '-'},
  94. {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"},
  95. {"x931", OPT_X931, '-', "Use ANSI X9.31 padding"},
  96. {"rev", OPT_REV, '-'},
  97. {"encrypt", OPT_ENCRYPT, '-', "Encrypt with public key"},
  98. {"decrypt", OPT_DECRYPT, '-', "Decrypt with private key"},
  99. {"passin", OPT_PASSIN, 's', "Pass phrase source"},
  100. # ifndef OPENSSL_NO_ENGINE
  101. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  102. # endif
  103. {NULL}
  104. };
  105. int rsautl_main(int argc, char **argv)
  106. {
  107. BIO *in = NULL, *out = NULL;
  108. ENGINE *e = NULL;
  109. EVP_PKEY *pkey = NULL;
  110. RSA *rsa = NULL;
  111. X509 *x;
  112. char *infile = NULL, *outfile = NULL, *keyfile = NULL;
  113. char *passinarg = NULL, *passin = NULL, *prog;
  114. char rsa_mode = RSA_VERIFY, key_type = KEY_PRIVKEY;
  115. unsigned char *rsa_in = NULL, *rsa_out = NULL, pad = RSA_PKCS1_PADDING;
  116. int rsa_inlen, keyformat = FORMAT_PEM, keysize, ret = 1;
  117. int rsa_outlen = 0, hexdump = 0, asn1parse = 0, need_priv = 0, rev = 0;
  118. OPTION_CHOICE o;
  119. prog = opt_init(argc, argv, rsautl_options);
  120. while ((o = opt_next()) != OPT_EOF) {
  121. switch (o) {
  122. case OPT_EOF:
  123. case OPT_ERR:
  124. opthelp:
  125. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  126. goto end;
  127. case OPT_HELP:
  128. opt_help(rsautl_options);
  129. ret = 0;
  130. goto end;
  131. case OPT_KEYFORM:
  132. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &keyformat))
  133. goto opthelp;
  134. break;
  135. case OPT_IN:
  136. infile = opt_arg();
  137. break;
  138. case OPT_OUT:
  139. outfile = opt_arg();
  140. break;
  141. case OPT_ENGINE:
  142. e = setup_engine(opt_arg(), 0);
  143. break;
  144. case OPT_ASN1PARSE:
  145. asn1parse = 1;
  146. break;
  147. case OPT_HEXDUMP:
  148. hexdump = 1;
  149. break;
  150. case OPT_RAW:
  151. pad = RSA_NO_PADDING;
  152. break;
  153. case OPT_OAEP:
  154. pad = RSA_PKCS1_OAEP_PADDING;
  155. break;
  156. case OPT_SSL:
  157. pad = RSA_SSLV23_PADDING;
  158. break;
  159. case OPT_PKCS:
  160. pad = RSA_PKCS1_PADDING;
  161. break;
  162. case OPT_X931:
  163. pad = RSA_X931_PADDING;
  164. break;
  165. case OPT_SIGN:
  166. rsa_mode = RSA_SIGN;
  167. need_priv = 1;
  168. break;
  169. case OPT_VERIFY:
  170. rsa_mode = RSA_VERIFY;
  171. break;
  172. case OPT_REV:
  173. rev = 1;
  174. break;
  175. case OPT_ENCRYPT:
  176. rsa_mode = RSA_ENCRYPT;
  177. break;
  178. case OPT_DECRYPT:
  179. rsa_mode = RSA_DECRYPT;
  180. need_priv = 1;
  181. break;
  182. case OPT_PUBIN:
  183. key_type = KEY_PUBKEY;
  184. break;
  185. case OPT_CERTIN:
  186. key_type = KEY_CERT;
  187. break;
  188. case OPT_INKEY:
  189. keyfile = opt_arg();
  190. break;
  191. case OPT_PASSIN:
  192. passinarg = opt_arg();
  193. break;
  194. }
  195. }
  196. argc = opt_num_rest();
  197. argv = opt_rest();
  198. if (need_priv && (key_type != KEY_PRIVKEY)) {
  199. BIO_printf(bio_err, "A private key is needed for this operation\n");
  200. goto end;
  201. }
  202. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  203. BIO_printf(bio_err, "Error getting password\n");
  204. goto end;
  205. }
  206. /* FIXME: seed PRNG only if needed */
  207. app_RAND_load_file(NULL, 0);
  208. switch (key_type) {
  209. case KEY_PRIVKEY:
  210. pkey = load_key(keyfile, keyformat, 0, passin, e, "Private Key");
  211. break;
  212. case KEY_PUBKEY:
  213. pkey = load_pubkey(keyfile, keyformat, 0, NULL, e, "Public Key");
  214. break;
  215. case KEY_CERT:
  216. x = load_cert(keyfile, keyformat, NULL, e, "Certificate");
  217. if (x) {
  218. pkey = X509_get_pubkey(x);
  219. X509_free(x);
  220. }
  221. break;
  222. }
  223. if (!pkey) {
  224. return 1;
  225. }
  226. rsa = EVP_PKEY_get1_RSA(pkey);
  227. EVP_PKEY_free(pkey);
  228. if (!rsa) {
  229. BIO_printf(bio_err, "Error getting RSA key\n");
  230. ERR_print_errors(bio_err);
  231. goto end;
  232. }
  233. in = bio_open_default(infile, 'r', FORMAT_BINARY);
  234. if (in == NULL)
  235. goto end;
  236. out = bio_open_default(outfile, 'w', FORMAT_BINARY);
  237. if (out == NULL)
  238. goto end;
  239. keysize = RSA_size(rsa);
  240. rsa_in = app_malloc(keysize * 2, "hold rsa key");
  241. rsa_out = app_malloc(keysize, "output rsa key");
  242. /* Read the input data */
  243. rsa_inlen = BIO_read(in, rsa_in, keysize * 2);
  244. if (rsa_inlen <= 0) {
  245. BIO_printf(bio_err, "Error reading input Data\n");
  246. goto end;
  247. }
  248. if (rev) {
  249. int i;
  250. unsigned char ctmp;
  251. for (i = 0; i < rsa_inlen / 2; i++) {
  252. ctmp = rsa_in[i];
  253. rsa_in[i] = rsa_in[rsa_inlen - 1 - i];
  254. rsa_in[rsa_inlen - 1 - i] = ctmp;
  255. }
  256. }
  257. switch (rsa_mode) {
  258. case RSA_VERIFY:
  259. rsa_outlen = RSA_public_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
  260. break;
  261. case RSA_SIGN:
  262. rsa_outlen =
  263. RSA_private_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
  264. break;
  265. case RSA_ENCRYPT:
  266. rsa_outlen = RSA_public_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
  267. break;
  268. case RSA_DECRYPT:
  269. rsa_outlen =
  270. RSA_private_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
  271. break;
  272. }
  273. if (rsa_outlen <= 0) {
  274. BIO_printf(bio_err, "RSA operation error\n");
  275. ERR_print_errors(bio_err);
  276. goto end;
  277. }
  278. ret = 0;
  279. if (asn1parse) {
  280. if (!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) {
  281. ERR_print_errors(bio_err);
  282. }
  283. } else if (hexdump)
  284. BIO_dump(out, (char *)rsa_out, rsa_outlen);
  285. else
  286. BIO_write(out, rsa_out, rsa_outlen);
  287. end:
  288. RSA_free(rsa);
  289. BIO_free(in);
  290. BIO_free_all(out);
  291. OPENSSL_free(rsa_in);
  292. OPENSSL_free(rsa_out);
  293. OPENSSL_free(passin);
  294. return ret;
  295. }
  296. #else /* !OPENSSL_NO_RSA */
  297. # if PEDANTIC
  298. static void *dummy = &dummy;
  299. # endif
  300. #endif