rsautl.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/opensslconf.h>
  10. #ifdef OPENSSL_NO_RSA
  11. NON_EMPTY_TRANSLATION_UNIT
  12. #else
  13. # include "apps.h"
  14. # include <string.h>
  15. # include <openssl/err.h>
  16. # include <openssl/pem.h>
  17. # include <openssl/rsa.h>
  18. # define RSA_SIGN 1
  19. # define RSA_VERIFY 2
  20. # define RSA_ENCRYPT 3
  21. # define RSA_DECRYPT 4
  22. # define KEY_PRIVKEY 1
  23. # define KEY_PUBKEY 2
  24. # define KEY_CERT 3
  25. typedef enum OPTION_choice {
  26. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  27. OPT_ENGINE, OPT_IN, OPT_OUT, OPT_ASN1PARSE, OPT_HEXDUMP,
  28. OPT_RAW, OPT_OAEP, OPT_SSL, OPT_PKCS, OPT_X931,
  29. OPT_SIGN, OPT_VERIFY, OPT_REV, OPT_ENCRYPT, OPT_DECRYPT,
  30. OPT_PUBIN, OPT_CERTIN, OPT_INKEY, OPT_PASSIN, OPT_KEYFORM
  31. } OPTION_CHOICE;
  32. const OPTIONS rsautl_options[] = {
  33. {"help", OPT_HELP, '-', "Display this summary"},
  34. {"in", OPT_IN, '<', "Input file"},
  35. {"out", OPT_OUT, '>', "Output file"},
  36. {"inkey", OPT_INKEY, 's', "Input key"},
  37. {"keyform", OPT_KEYFORM, 'E', "Private key format - default PEM"},
  38. {"pubin", OPT_PUBIN, '-', "Input is an RSA public"},
  39. {"certin", OPT_CERTIN, '-', "Input is a cert carrying an RSA public key"},
  40. {"ssl", OPT_SSL, '-', "Use SSL v2 padding"},
  41. {"raw", OPT_RAW, '-', "Use no padding"},
  42. {"pkcs", OPT_PKCS, '-', "Use PKCS#1 v1.5 padding (default)"},
  43. {"oaep", OPT_OAEP, '-', "Use PKCS#1 OAEP"},
  44. {"sign", OPT_SIGN, '-', "Sign with private key"},
  45. {"verify", OPT_VERIFY, '-', "Verify with public key"},
  46. {"asn1parse", OPT_ASN1PARSE, '-',
  47. "Run output through asn1parse; useful with -verify"},
  48. {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"},
  49. {"x931", OPT_X931, '-', "Use ANSI X9.31 padding"},
  50. {"rev", OPT_REV, '-', "Reverse the order of the input buffer"},
  51. {"encrypt", OPT_ENCRYPT, '-', "Encrypt with public key"},
  52. {"decrypt", OPT_DECRYPT, '-', "Decrypt with private key"},
  53. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  54. # ifndef OPENSSL_NO_ENGINE
  55. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  56. # endif
  57. {NULL}
  58. };
  59. int rsautl_main(int argc, char **argv)
  60. {
  61. BIO *in = NULL, *out = NULL;
  62. ENGINE *e = NULL;
  63. EVP_PKEY *pkey = NULL;
  64. RSA *rsa = NULL;
  65. X509 *x;
  66. char *infile = NULL, *outfile = NULL, *keyfile = NULL;
  67. char *passinarg = NULL, *passin = NULL, *prog;
  68. char rsa_mode = RSA_VERIFY, key_type = KEY_PRIVKEY;
  69. unsigned char *rsa_in = NULL, *rsa_out = NULL, pad = RSA_PKCS1_PADDING;
  70. int rsa_inlen, keyformat = FORMAT_PEM, keysize, ret = 1;
  71. int rsa_outlen = 0, hexdump = 0, asn1parse = 0, need_priv = 0, rev = 0;
  72. OPTION_CHOICE o;
  73. prog = opt_init(argc, argv, rsautl_options);
  74. while ((o = opt_next()) != OPT_EOF) {
  75. switch (o) {
  76. case OPT_EOF:
  77. case OPT_ERR:
  78. opthelp:
  79. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  80. goto end;
  81. case OPT_HELP:
  82. opt_help(rsautl_options);
  83. ret = 0;
  84. goto end;
  85. case OPT_KEYFORM:
  86. if (!opt_format(opt_arg(), OPT_FMT_PDE, &keyformat))
  87. goto opthelp;
  88. break;
  89. case OPT_IN:
  90. infile = opt_arg();
  91. break;
  92. case OPT_OUT:
  93. outfile = opt_arg();
  94. break;
  95. case OPT_ENGINE:
  96. e = setup_engine(opt_arg(), 0);
  97. break;
  98. case OPT_ASN1PARSE:
  99. asn1parse = 1;
  100. break;
  101. case OPT_HEXDUMP:
  102. hexdump = 1;
  103. break;
  104. case OPT_RAW:
  105. pad = RSA_NO_PADDING;
  106. break;
  107. case OPT_OAEP:
  108. pad = RSA_PKCS1_OAEP_PADDING;
  109. break;
  110. case OPT_SSL:
  111. pad = RSA_SSLV23_PADDING;
  112. break;
  113. case OPT_PKCS:
  114. pad = RSA_PKCS1_PADDING;
  115. break;
  116. case OPT_X931:
  117. pad = RSA_X931_PADDING;
  118. break;
  119. case OPT_SIGN:
  120. rsa_mode = RSA_SIGN;
  121. need_priv = 1;
  122. break;
  123. case OPT_VERIFY:
  124. rsa_mode = RSA_VERIFY;
  125. break;
  126. case OPT_REV:
  127. rev = 1;
  128. break;
  129. case OPT_ENCRYPT:
  130. rsa_mode = RSA_ENCRYPT;
  131. break;
  132. case OPT_DECRYPT:
  133. rsa_mode = RSA_DECRYPT;
  134. need_priv = 1;
  135. break;
  136. case OPT_PUBIN:
  137. key_type = KEY_PUBKEY;
  138. break;
  139. case OPT_CERTIN:
  140. key_type = KEY_CERT;
  141. break;
  142. case OPT_INKEY:
  143. keyfile = opt_arg();
  144. break;
  145. case OPT_PASSIN:
  146. passinarg = opt_arg();
  147. break;
  148. }
  149. }
  150. argc = opt_num_rest();
  151. if (argc != 0)
  152. goto opthelp;
  153. if (need_priv && (key_type != KEY_PRIVKEY)) {
  154. BIO_printf(bio_err, "A private key is needed for this operation\n");
  155. goto end;
  156. }
  157. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  158. BIO_printf(bio_err, "Error getting password\n");
  159. goto end;
  160. }
  161. /* FIXME: seed PRNG only if needed */
  162. app_RAND_load_file(NULL, 0);
  163. switch (key_type) {
  164. case KEY_PRIVKEY:
  165. pkey = load_key(keyfile, keyformat, 0, passin, e, "Private Key");
  166. break;
  167. case KEY_PUBKEY:
  168. pkey = load_pubkey(keyfile, keyformat, 0, NULL, e, "Public Key");
  169. break;
  170. case KEY_CERT:
  171. x = load_cert(keyfile, keyformat, "Certificate");
  172. if (x) {
  173. pkey = X509_get_pubkey(x);
  174. X509_free(x);
  175. }
  176. break;
  177. }
  178. if (!pkey) {
  179. return 1;
  180. }
  181. rsa = EVP_PKEY_get1_RSA(pkey);
  182. EVP_PKEY_free(pkey);
  183. if (!rsa) {
  184. BIO_printf(bio_err, "Error getting RSA key\n");
  185. ERR_print_errors(bio_err);
  186. goto end;
  187. }
  188. in = bio_open_default(infile, 'r', FORMAT_BINARY);
  189. if (in == NULL)
  190. goto end;
  191. out = bio_open_default(outfile, 'w', FORMAT_BINARY);
  192. if (out == NULL)
  193. goto end;
  194. keysize = RSA_size(rsa);
  195. rsa_in = app_malloc(keysize * 2, "hold rsa key");
  196. rsa_out = app_malloc(keysize, "output rsa key");
  197. /* Read the input data */
  198. rsa_inlen = BIO_read(in, rsa_in, keysize * 2);
  199. if (rsa_inlen < 0) {
  200. BIO_printf(bio_err, "Error reading input Data\n");
  201. goto end;
  202. }
  203. if (rev) {
  204. int i;
  205. unsigned char ctmp;
  206. for (i = 0; i < rsa_inlen / 2; i++) {
  207. ctmp = rsa_in[i];
  208. rsa_in[i] = rsa_in[rsa_inlen - 1 - i];
  209. rsa_in[rsa_inlen - 1 - i] = ctmp;
  210. }
  211. }
  212. switch (rsa_mode) {
  213. case RSA_VERIFY:
  214. rsa_outlen = RSA_public_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
  215. break;
  216. case RSA_SIGN:
  217. rsa_outlen =
  218. RSA_private_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
  219. break;
  220. case RSA_ENCRYPT:
  221. rsa_outlen = RSA_public_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
  222. break;
  223. case RSA_DECRYPT:
  224. rsa_outlen =
  225. RSA_private_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
  226. break;
  227. }
  228. if (rsa_outlen < 0) {
  229. BIO_printf(bio_err, "RSA operation error\n");
  230. ERR_print_errors(bio_err);
  231. goto end;
  232. }
  233. ret = 0;
  234. if (asn1parse) {
  235. if (!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) {
  236. ERR_print_errors(bio_err);
  237. }
  238. } else if (hexdump)
  239. BIO_dump(out, (char *)rsa_out, rsa_outlen);
  240. else
  241. BIO_write(out, rsa_out, rsa_outlen);
  242. end:
  243. RSA_free(rsa);
  244. release_engine(e);
  245. BIO_free(in);
  246. BIO_free_all(out);
  247. OPENSSL_free(rsa_in);
  248. OPENSSL_free(rsa_out);
  249. OPENSSL_free(passin);
  250. return ret;
  251. }
  252. #endif