2
0

rsautl.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright 2000-2020 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. #include "apps.h"
  11. #include "progs.h"
  12. #include <string.h>
  13. #include <openssl/err.h>
  14. #include <openssl/pem.h>
  15. #include <openssl/rsa.h>
  16. #define RSA_SIGN 1
  17. #define RSA_VERIFY 2
  18. #define RSA_ENCRYPT 3
  19. #define RSA_DECRYPT 4
  20. #define KEY_PRIVKEY 1
  21. #define KEY_PUBKEY 2
  22. #define KEY_CERT 3
  23. typedef enum OPTION_choice {
  24. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  25. OPT_ENGINE, OPT_IN, OPT_OUT, OPT_ASN1PARSE, OPT_HEXDUMP,
  26. OPT_RAW, OPT_OAEP, OPT_SSL, OPT_PKCS, OPT_X931,
  27. OPT_SIGN, OPT_VERIFY, OPT_REV, OPT_ENCRYPT, OPT_DECRYPT,
  28. OPT_PUBIN, OPT_CERTIN, OPT_INKEY, OPT_PASSIN, OPT_KEYFORM,
  29. OPT_R_ENUM
  30. } OPTION_CHOICE;
  31. const OPTIONS rsautl_options[] = {
  32. {"help", OPT_HELP, '-', "Display this summary"},
  33. {"in", OPT_IN, '<', "Input file"},
  34. {"out", OPT_OUT, '>', "Output file"},
  35. {"inkey", OPT_INKEY, 's', "Input key"},
  36. {"keyform", OPT_KEYFORM, 'E', "Private key format - default PEM"},
  37. {"pubin", OPT_PUBIN, '-', "Input is an RSA public"},
  38. {"certin", OPT_CERTIN, '-', "Input is a cert carrying an RSA public key"},
  39. {"ssl", OPT_SSL, '-', "Use SSL v2 padding"},
  40. {"raw", OPT_RAW, '-', "Use no padding"},
  41. {"pkcs", OPT_PKCS, '-', "Use PKCS#1 v1.5 padding (default)"},
  42. {"oaep", OPT_OAEP, '-', "Use PKCS#1 OAEP"},
  43. {"sign", OPT_SIGN, '-', "Sign with private key"},
  44. {"verify", OPT_VERIFY, '-', "Verify with public key"},
  45. {"asn1parse", OPT_ASN1PARSE, '-',
  46. "Run output through asn1parse; useful with -verify"},
  47. {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"},
  48. {"x931", OPT_X931, '-', "Use ANSI X9.31 padding"},
  49. {"rev", OPT_REV, '-', "Reverse the order of the input buffer"},
  50. {"encrypt", OPT_ENCRYPT, '-', "Encrypt with public key"},
  51. {"decrypt", OPT_DECRYPT, '-', "Decrypt with private key"},
  52. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  53. OPT_R_OPTIONS,
  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. case OPT_R_CASES:
  149. if (!opt_rand(o))
  150. goto end;
  151. break;
  152. }
  153. }
  154. argc = opt_num_rest();
  155. if (argc != 0)
  156. goto opthelp;
  157. if (need_priv && (key_type != KEY_PRIVKEY)) {
  158. BIO_printf(bio_err, "A private key is needed for this operation\n");
  159. goto end;
  160. }
  161. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  162. BIO_printf(bio_err, "Error getting password\n");
  163. goto end;
  164. }
  165. switch (key_type) {
  166. case KEY_PRIVKEY:
  167. pkey = load_key(keyfile, keyformat, 0, passin, e, "Private Key");
  168. break;
  169. case KEY_PUBKEY:
  170. pkey = load_pubkey(keyfile, keyformat, 0, NULL, e, "Public Key");
  171. break;
  172. case KEY_CERT:
  173. x = load_cert(keyfile, keyformat, "Certificate");
  174. if (x) {
  175. pkey = X509_get_pubkey(x);
  176. X509_free(x);
  177. }
  178. break;
  179. }
  180. if (pkey == NULL)
  181. return 1;
  182. rsa = EVP_PKEY_get1_RSA(pkey);
  183. EVP_PKEY_free(pkey);
  184. if (rsa == NULL) {
  185. BIO_printf(bio_err, "Error getting RSA key\n");
  186. ERR_print_errors(bio_err);
  187. goto end;
  188. }
  189. in = bio_open_default(infile, 'r', FORMAT_BINARY);
  190. if (in == NULL)
  191. goto end;
  192. out = bio_open_default(outfile, 'w', FORMAT_BINARY);
  193. if (out == NULL)
  194. goto end;
  195. keysize = RSA_size(rsa);
  196. rsa_in = app_malloc(keysize * 2, "hold rsa key");
  197. rsa_out = app_malloc(keysize, "output rsa key");
  198. /* Read the input data */
  199. rsa_inlen = BIO_read(in, rsa_in, keysize * 2);
  200. if (rsa_inlen < 0) {
  201. BIO_printf(bio_err, "Error reading input Data\n");
  202. goto end;
  203. }
  204. if (rev) {
  205. int i;
  206. unsigned char ctmp;
  207. for (i = 0; i < rsa_inlen / 2; i++) {
  208. ctmp = rsa_in[i];
  209. rsa_in[i] = rsa_in[rsa_inlen - 1 - i];
  210. rsa_in[rsa_inlen - 1 - i] = ctmp;
  211. }
  212. }
  213. switch (rsa_mode) {
  214. case RSA_VERIFY:
  215. rsa_outlen = RSA_public_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
  216. break;
  217. case RSA_SIGN:
  218. rsa_outlen =
  219. RSA_private_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
  220. break;
  221. case RSA_ENCRYPT:
  222. rsa_outlen = RSA_public_encrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
  223. break;
  224. case RSA_DECRYPT:
  225. rsa_outlen =
  226. RSA_private_decrypt(rsa_inlen, rsa_in, rsa_out, rsa, pad);
  227. break;
  228. }
  229. if (rsa_outlen < 0) {
  230. BIO_printf(bio_err, "RSA operation error\n");
  231. ERR_print_errors(bio_err);
  232. goto end;
  233. }
  234. ret = 0;
  235. if (asn1parse) {
  236. if (!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) {
  237. ERR_print_errors(bio_err);
  238. }
  239. } else if (hexdump) {
  240. BIO_dump(out, (char *)rsa_out, rsa_outlen);
  241. } else {
  242. BIO_write(out, rsa_out, rsa_outlen);
  243. }
  244. end:
  245. RSA_free(rsa);
  246. release_engine(e);
  247. BIO_free(in);
  248. BIO_free_all(out);
  249. OPENSSL_free(rsa_in);
  250. OPENSSL_free(rsa_out);
  251. OPENSSL_free(passin);
  252. return ret;
  253. }