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