rsautl.c 8.2 KB

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