rsautl.c 7.9 KB

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