rsautl.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright 2000-2021 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_COMMON,
  25. OPT_ENGINE, OPT_IN, OPT_OUT, OPT_ASN1PARSE, OPT_HEXDUMP,
  26. OPT_RSA_RAW, OPT_OAEP, 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. {"raw", OPT_RSA_RAW, '-', "Use no padding"},
  52. {"pkcs", OPT_PKCS, '-', "Use PKCS#1 v1.5 padding (default)"},
  53. {"x931", OPT_X931, '-', "Use ANSI X9.31 padding"},
  54. {"oaep", OPT_OAEP, '-', "Use PKCS#1 OAEP"},
  55. {"asn1parse", OPT_ASN1PARSE, '-',
  56. "Run output through asn1parse; useful with -verify"},
  57. {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"},
  58. OPT_R_OPTIONS,
  59. OPT_PROV_OPTIONS,
  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. EVP_PKEY_CTX *ctx = 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. size_t rsa_inlen, rsa_outlen = 0;
  74. int keyformat = FORMAT_UNDEF, keysize, ret = 1, rv;
  75. int hexdump = 0, asn1parse = 0, need_priv = 0, rev = 0;
  76. OPTION_CHOICE o;
  77. prog = opt_init(argc, argv, rsautl_options);
  78. while ((o = opt_next()) != OPT_EOF) {
  79. switch (o) {
  80. case OPT_EOF:
  81. case OPT_ERR:
  82. opthelp:
  83. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  84. goto end;
  85. case OPT_HELP:
  86. opt_help(rsautl_options);
  87. ret = 0;
  88. goto end;
  89. case OPT_KEYFORM:
  90. if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
  91. goto opthelp;
  92. break;
  93. case OPT_IN:
  94. infile = opt_arg();
  95. break;
  96. case OPT_OUT:
  97. outfile = opt_arg();
  98. break;
  99. case OPT_ENGINE:
  100. e = setup_engine(opt_arg(), 0);
  101. break;
  102. case OPT_ASN1PARSE:
  103. asn1parse = 1;
  104. break;
  105. case OPT_HEXDUMP:
  106. hexdump = 1;
  107. break;
  108. case OPT_RSA_RAW:
  109. pad = RSA_NO_PADDING;
  110. break;
  111. case OPT_OAEP:
  112. pad = RSA_PKCS1_OAEP_PADDING;
  113. break;
  114. case OPT_PKCS:
  115. pad = RSA_PKCS1_PADDING;
  116. break;
  117. case OPT_X931:
  118. pad = RSA_X931_PADDING;
  119. break;
  120. case OPT_SIGN:
  121. rsa_mode = RSA_SIGN;
  122. need_priv = 1;
  123. break;
  124. case OPT_VERIFY:
  125. rsa_mode = RSA_VERIFY;
  126. break;
  127. case OPT_REV:
  128. rev = 1;
  129. break;
  130. case OPT_ENCRYPT:
  131. rsa_mode = RSA_ENCRYPT;
  132. break;
  133. case OPT_DECRYPT:
  134. rsa_mode = RSA_DECRYPT;
  135. need_priv = 1;
  136. break;
  137. case OPT_PUBIN:
  138. key_type = KEY_PUBKEY;
  139. break;
  140. case OPT_CERTIN:
  141. key_type = KEY_CERT;
  142. break;
  143. case OPT_INKEY:
  144. keyfile = opt_arg();
  145. break;
  146. case OPT_PASSIN:
  147. passinarg = opt_arg();
  148. break;
  149. case OPT_R_CASES:
  150. if (!opt_rand(o))
  151. goto end;
  152. break;
  153. case OPT_PROV_CASES:
  154. if (!opt_provider(o))
  155. goto end;
  156. break;
  157. }
  158. }
  159. /* No extra arguments. */
  160. argc = opt_num_rest();
  161. if (argc != 0)
  162. goto opthelp;
  163. if (!app_RAND_load())
  164. goto end;
  165. if (need_priv && (key_type != KEY_PRIVKEY)) {
  166. BIO_printf(bio_err, "A private key is needed for this operation\n");
  167. goto end;
  168. }
  169. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  170. BIO_printf(bio_err, "Error getting password\n");
  171. goto end;
  172. }
  173. switch (key_type) {
  174. case KEY_PRIVKEY:
  175. pkey = load_key(keyfile, keyformat, 0, passin, e, "private key");
  176. break;
  177. case KEY_PUBKEY:
  178. pkey = load_pubkey(keyfile, keyformat, 0, NULL, e, "public key");
  179. break;
  180. case KEY_CERT:
  181. x = load_cert(keyfile, FORMAT_UNDEF, "Certificate");
  182. if (x) {
  183. pkey = X509_get_pubkey(x);
  184. X509_free(x);
  185. }
  186. break;
  187. }
  188. if (pkey == NULL)
  189. return 1;
  190. in = bio_open_default(infile, 'r', FORMAT_BINARY);
  191. if (in == NULL)
  192. goto end;
  193. out = bio_open_default(outfile, 'w', FORMAT_BINARY);
  194. if (out == NULL)
  195. goto end;
  196. keysize = EVP_PKEY_get_size(pkey);
  197. rsa_in = app_malloc(keysize * 2, "hold rsa key");
  198. rsa_out = app_malloc(keysize, "output rsa key");
  199. rsa_outlen = keysize;
  200. /* Read the input data */
  201. rv = BIO_read(in, rsa_in, keysize * 2);
  202. if (rv < 0) {
  203. BIO_printf(bio_err, "Error reading input Data\n");
  204. goto end;
  205. }
  206. rsa_inlen = rv;
  207. if (rev) {
  208. size_t 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. if ((ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL)) == NULL)
  217. goto end;
  218. switch (rsa_mode) {
  219. case RSA_VERIFY:
  220. rv = EVP_PKEY_verify_recover_init(ctx)
  221. && EVP_PKEY_CTX_set_rsa_padding(ctx, pad)
  222. && EVP_PKEY_verify_recover(ctx, rsa_out, &rsa_outlen,
  223. rsa_in, rsa_inlen);
  224. break;
  225. case RSA_SIGN:
  226. rv = EVP_PKEY_sign_init(ctx)
  227. && EVP_PKEY_CTX_set_rsa_padding(ctx, pad)
  228. && EVP_PKEY_sign(ctx, rsa_out, &rsa_outlen, rsa_in, rsa_inlen);
  229. break;
  230. case RSA_ENCRYPT:
  231. rv = EVP_PKEY_encrypt_init(ctx)
  232. && EVP_PKEY_CTX_set_rsa_padding(ctx, pad)
  233. && EVP_PKEY_encrypt(ctx, rsa_out, &rsa_outlen, rsa_in, rsa_inlen);
  234. break;
  235. case RSA_DECRYPT:
  236. rv = EVP_PKEY_decrypt_init(ctx)
  237. && EVP_PKEY_CTX_set_rsa_padding(ctx, pad)
  238. && EVP_PKEY_decrypt(ctx, rsa_out, &rsa_outlen, rsa_in, rsa_inlen);
  239. break;
  240. }
  241. if (!rv) {
  242. BIO_printf(bio_err, "RSA operation error\n");
  243. ERR_print_errors(bio_err);
  244. goto end;
  245. }
  246. ret = 0;
  247. if (asn1parse) {
  248. if (!ASN1_parse_dump(out, rsa_out, rsa_outlen, 1, -1)) {
  249. ERR_print_errors(bio_err);
  250. }
  251. } else if (hexdump) {
  252. BIO_dump(out, (char *)rsa_out, rsa_outlen);
  253. } else {
  254. BIO_write(out, rsa_out, rsa_outlen);
  255. }
  256. end:
  257. EVP_PKEY_CTX_free(ctx);
  258. EVP_PKEY_free(pkey);
  259. release_engine(e);
  260. BIO_free(in);
  261. BIO_free_all(out);
  262. OPENSSL_free(rsa_in);
  263. OPENSSL_free(rsa_out);
  264. OPENSSL_free(passin);
  265. return ret;
  266. }