pkeyutl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * Copyright 2006-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 "apps.h"
  10. #include "progs.h"
  11. #include <string.h>
  12. #include <openssl/err.h>
  13. #include <openssl/pem.h>
  14. #include <openssl/evp.h>
  15. #define KEY_NONE 0
  16. #define KEY_PRIVKEY 1
  17. #define KEY_PUBKEY 2
  18. #define KEY_CERT 3
  19. static EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize,
  20. const char *keyfile, int keyform, int key_type,
  21. char *passinarg, int pkey_op, ENGINE *e,
  22. const int impl);
  23. static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
  24. ENGINE *e);
  25. static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
  26. unsigned char *out, size_t *poutlen,
  27. const unsigned char *in, size_t inlen);
  28. typedef enum OPTION_choice {
  29. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  30. OPT_ENGINE, OPT_ENGINE_IMPL, OPT_IN, OPT_OUT,
  31. OPT_PUBIN, OPT_CERTIN, OPT_ASN1PARSE, OPT_HEXDUMP, OPT_SIGN,
  32. OPT_VERIFY, OPT_VERIFYRECOVER, OPT_REV, OPT_ENCRYPT, OPT_DECRYPT,
  33. OPT_DERIVE, OPT_SIGFILE, OPT_INKEY, OPT_PEERKEY, OPT_PASSIN,
  34. OPT_PEERFORM, OPT_KEYFORM, OPT_PKEYOPT, OPT_KDF, OPT_KDFLEN,
  35. OPT_R_ENUM
  36. } OPTION_CHOICE;
  37. const OPTIONS pkeyutl_options[] = {
  38. {"help", OPT_HELP, '-', "Display this summary"},
  39. {"in", OPT_IN, '<', "Input file - default stdin"},
  40. {"out", OPT_OUT, '>', "Output file - default stdout"},
  41. {"pubin", OPT_PUBIN, '-', "Input is a public key"},
  42. {"certin", OPT_CERTIN, '-', "Input is a cert with a public key"},
  43. {"asn1parse", OPT_ASN1PARSE, '-', "asn1parse the output data"},
  44. {"hexdump", OPT_HEXDUMP, '-', "Hex dump output"},
  45. {"sign", OPT_SIGN, '-', "Sign input data with private key"},
  46. {"verify", OPT_VERIFY, '-', "Verify with public key"},
  47. {"verifyrecover", OPT_VERIFYRECOVER, '-',
  48. "Verify with public key, recover original data"},
  49. {"rev", OPT_REV, '-', "Reverse the order of the input buffer"},
  50. {"encrypt", OPT_ENCRYPT, '-', "Encrypt input data with public key"},
  51. {"decrypt", OPT_DECRYPT, '-', "Decrypt input data with private key"},
  52. {"derive", OPT_DERIVE, '-', "Derive shared secret"},
  53. {"kdf", OPT_KDF, 's', "Use KDF algorithm"},
  54. {"kdflen", OPT_KDFLEN, 'p', "KDF algorithm output length"},
  55. {"sigfile", OPT_SIGFILE, '<', "Signature file (verify operation only)"},
  56. {"inkey", OPT_INKEY, 's', "Input private key file"},
  57. {"peerkey", OPT_PEERKEY, 's', "Peer key file used in key derivation"},
  58. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  59. {"peerform", OPT_PEERFORM, 'E', "Peer key format - default PEM"},
  60. {"keyform", OPT_KEYFORM, 'E', "Private key format - default PEM"},
  61. {"pkeyopt", OPT_PKEYOPT, 's', "Public key options as opt:value"},
  62. OPT_R_OPTIONS,
  63. #ifndef OPENSSL_NO_ENGINE
  64. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  65. {"engine_impl", OPT_ENGINE_IMPL, '-',
  66. "Also use engine given by -engine for crypto operations"},
  67. #endif
  68. {NULL}
  69. };
  70. int pkeyutl_main(int argc, char **argv)
  71. {
  72. BIO *in = NULL, *out = NULL;
  73. ENGINE *e = NULL;
  74. EVP_PKEY_CTX *ctx = NULL;
  75. char *infile = NULL, *outfile = NULL, *sigfile = NULL, *passinarg = NULL;
  76. char hexdump = 0, asn1parse = 0, rev = 0, *prog;
  77. unsigned char *buf_in = NULL, *buf_out = NULL, *sig = NULL;
  78. OPTION_CHOICE o;
  79. int buf_inlen = 0, siglen = -1, keyform = FORMAT_PEM, peerform = FORMAT_PEM;
  80. int keysize = -1, pkey_op = EVP_PKEY_OP_SIGN, key_type = KEY_PRIVKEY;
  81. int engine_impl = 0;
  82. int ret = 1, rv = -1;
  83. size_t buf_outlen;
  84. const char *inkey = NULL;
  85. const char *peerkey = NULL;
  86. const char *kdfalg = NULL;
  87. int kdflen = 0;
  88. STACK_OF(OPENSSL_STRING) *pkeyopts = NULL;
  89. prog = opt_init(argc, argv, pkeyutl_options);
  90. while ((o = opt_next()) != OPT_EOF) {
  91. switch (o) {
  92. case OPT_EOF:
  93. case OPT_ERR:
  94. opthelp:
  95. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  96. goto end;
  97. case OPT_HELP:
  98. opt_help(pkeyutl_options);
  99. ret = 0;
  100. goto end;
  101. case OPT_IN:
  102. infile = opt_arg();
  103. break;
  104. case OPT_OUT:
  105. outfile = opt_arg();
  106. break;
  107. case OPT_SIGFILE:
  108. sigfile = opt_arg();
  109. break;
  110. case OPT_ENGINE_IMPL:
  111. engine_impl = 1;
  112. break;
  113. case OPT_INKEY:
  114. inkey = opt_arg();
  115. break;
  116. case OPT_PEERKEY:
  117. peerkey = opt_arg();
  118. break;
  119. case OPT_PASSIN:
  120. passinarg = opt_arg();
  121. break;
  122. case OPT_PEERFORM:
  123. if (!opt_format(opt_arg(), OPT_FMT_PDE, &peerform))
  124. goto opthelp;
  125. break;
  126. case OPT_KEYFORM:
  127. if (!opt_format(opt_arg(), OPT_FMT_PDE, &keyform))
  128. goto opthelp;
  129. break;
  130. case OPT_R_CASES:
  131. if (!opt_rand(o))
  132. goto end;
  133. break;
  134. case OPT_ENGINE:
  135. e = setup_engine(opt_arg(), 0);
  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_ASN1PARSE:
  144. asn1parse = 1;
  145. break;
  146. case OPT_HEXDUMP:
  147. hexdump = 1;
  148. break;
  149. case OPT_SIGN:
  150. pkey_op = EVP_PKEY_OP_SIGN;
  151. break;
  152. case OPT_VERIFY:
  153. pkey_op = EVP_PKEY_OP_VERIFY;
  154. break;
  155. case OPT_VERIFYRECOVER:
  156. pkey_op = EVP_PKEY_OP_VERIFYRECOVER;
  157. break;
  158. case OPT_ENCRYPT:
  159. pkey_op = EVP_PKEY_OP_ENCRYPT;
  160. break;
  161. case OPT_DECRYPT:
  162. pkey_op = EVP_PKEY_OP_DECRYPT;
  163. break;
  164. case OPT_DERIVE:
  165. pkey_op = EVP_PKEY_OP_DERIVE;
  166. break;
  167. case OPT_KDF:
  168. pkey_op = EVP_PKEY_OP_DERIVE;
  169. key_type = KEY_NONE;
  170. kdfalg = opt_arg();
  171. break;
  172. case OPT_KDFLEN:
  173. kdflen = atoi(opt_arg());
  174. break;
  175. case OPT_REV:
  176. rev = 1;
  177. break;
  178. case OPT_PKEYOPT:
  179. if ((pkeyopts == NULL &&
  180. (pkeyopts = sk_OPENSSL_STRING_new_null()) == NULL) ||
  181. sk_OPENSSL_STRING_push(pkeyopts, opt_arg()) == 0) {
  182. BIO_puts(bio_err, "out of memory\n");
  183. goto end;
  184. }
  185. break;
  186. }
  187. }
  188. argc = opt_num_rest();
  189. if (argc != 0)
  190. goto opthelp;
  191. if (kdfalg != NULL) {
  192. if (kdflen == 0) {
  193. BIO_printf(bio_err,
  194. "%s: no KDF length given (-kdflen parameter).\n", prog);
  195. goto opthelp;
  196. }
  197. } else if (inkey == NULL) {
  198. BIO_printf(bio_err,
  199. "%s: no private key given (-inkey parameter).\n", prog);
  200. goto opthelp;
  201. } else if (peerkey != NULL && pkey_op != EVP_PKEY_OP_DERIVE) {
  202. BIO_printf(bio_err,
  203. "%s: no peer key given (-peerkey parameter).\n", prog);
  204. goto opthelp;
  205. }
  206. ctx = init_ctx(kdfalg, &keysize, inkey, keyform, key_type,
  207. passinarg, pkey_op, e, engine_impl);
  208. if (ctx == NULL) {
  209. BIO_printf(bio_err, "%s: Error initializing context\n", prog);
  210. ERR_print_errors(bio_err);
  211. goto end;
  212. }
  213. if (peerkey != NULL && !setup_peer(ctx, peerform, peerkey, e)) {
  214. BIO_printf(bio_err, "%s: Error setting up peer key\n", prog);
  215. ERR_print_errors(bio_err);
  216. goto end;
  217. }
  218. if (pkeyopts != NULL) {
  219. int num = sk_OPENSSL_STRING_num(pkeyopts);
  220. int i;
  221. for (i = 0; i < num; ++i) {
  222. const char *opt = sk_OPENSSL_STRING_value(pkeyopts, i);
  223. if (pkey_ctrl_string(ctx, opt) <= 0) {
  224. BIO_printf(bio_err, "%s: Can't set parameter \"%s\":\n",
  225. prog, opt);
  226. ERR_print_errors(bio_err);
  227. goto end;
  228. }
  229. }
  230. }
  231. if (sigfile != NULL && (pkey_op != EVP_PKEY_OP_VERIFY)) {
  232. BIO_printf(bio_err,
  233. "%s: Signature file specified for non verify\n", prog);
  234. goto end;
  235. }
  236. if (sigfile == NULL && (pkey_op == EVP_PKEY_OP_VERIFY)) {
  237. BIO_printf(bio_err,
  238. "%s: No signature file specified for verify\n", prog);
  239. goto end;
  240. }
  241. if (pkey_op != EVP_PKEY_OP_DERIVE) {
  242. in = bio_open_default(infile, 'r', FORMAT_BINARY);
  243. if (in == NULL)
  244. goto end;
  245. }
  246. out = bio_open_default(outfile, 'w', FORMAT_BINARY);
  247. if (out == NULL)
  248. goto end;
  249. if (sigfile != NULL) {
  250. BIO *sigbio = BIO_new_file(sigfile, "rb");
  251. if (sigbio == NULL) {
  252. BIO_printf(bio_err, "Can't open signature file %s\n", sigfile);
  253. goto end;
  254. }
  255. siglen = bio_to_mem(&sig, keysize * 10, sigbio);
  256. BIO_free(sigbio);
  257. if (siglen < 0) {
  258. BIO_printf(bio_err, "Error reading signature data\n");
  259. goto end;
  260. }
  261. }
  262. if (in != NULL) {
  263. /* Read the input data */
  264. buf_inlen = bio_to_mem(&buf_in, keysize * 10, in);
  265. if (buf_inlen < 0) {
  266. BIO_printf(bio_err, "Error reading input Data\n");
  267. goto end;
  268. }
  269. if (rev) {
  270. size_t i;
  271. unsigned char ctmp;
  272. size_t l = (size_t)buf_inlen;
  273. for (i = 0; i < l / 2; i++) {
  274. ctmp = buf_in[i];
  275. buf_in[i] = buf_in[l - 1 - i];
  276. buf_in[l - 1 - i] = ctmp;
  277. }
  278. }
  279. }
  280. /* Sanity check the input */
  281. if (buf_inlen > EVP_MAX_MD_SIZE
  282. && (pkey_op == EVP_PKEY_OP_SIGN
  283. || pkey_op == EVP_PKEY_OP_VERIFY)) {
  284. BIO_printf(bio_err,
  285. "Error: The input data looks too long to be a hash\n");
  286. goto end;
  287. }
  288. if (pkey_op == EVP_PKEY_OP_VERIFY) {
  289. rv = EVP_PKEY_verify(ctx, sig, (size_t)siglen,
  290. buf_in, (size_t)buf_inlen);
  291. if (rv == 1) {
  292. BIO_puts(out, "Signature Verified Successfully\n");
  293. ret = 0;
  294. } else {
  295. BIO_puts(out, "Signature Verification Failure\n");
  296. }
  297. goto end;
  298. }
  299. if (kdflen != 0) {
  300. buf_outlen = kdflen;
  301. rv = 1;
  302. } else {
  303. rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen,
  304. buf_in, (size_t)buf_inlen);
  305. }
  306. if (rv > 0 && buf_outlen != 0) {
  307. buf_out = app_malloc(buf_outlen, "buffer output");
  308. rv = do_keyop(ctx, pkey_op,
  309. buf_out, (size_t *)&buf_outlen,
  310. buf_in, (size_t)buf_inlen);
  311. }
  312. if (rv <= 0) {
  313. if (pkey_op != EVP_PKEY_OP_DERIVE) {
  314. BIO_puts(bio_err, "Public Key operation error\n");
  315. } else {
  316. BIO_puts(bio_err, "Key derivation failed\n");
  317. }
  318. ERR_print_errors(bio_err);
  319. goto end;
  320. }
  321. ret = 0;
  322. if (asn1parse) {
  323. if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1))
  324. ERR_print_errors(bio_err);
  325. } else if (hexdump) {
  326. BIO_dump(out, (char *)buf_out, buf_outlen);
  327. } else {
  328. BIO_write(out, buf_out, buf_outlen);
  329. }
  330. end:
  331. EVP_PKEY_CTX_free(ctx);
  332. release_engine(e);
  333. BIO_free(in);
  334. BIO_free_all(out);
  335. OPENSSL_free(buf_in);
  336. OPENSSL_free(buf_out);
  337. OPENSSL_free(sig);
  338. sk_OPENSSL_STRING_free(pkeyopts);
  339. return ret;
  340. }
  341. static EVP_PKEY_CTX *init_ctx(const char *kdfalg, int *pkeysize,
  342. const char *keyfile, int keyform, int key_type,
  343. char *passinarg, int pkey_op, ENGINE *e,
  344. const int engine_impl)
  345. {
  346. EVP_PKEY *pkey = NULL;
  347. EVP_PKEY_CTX *ctx = NULL;
  348. ENGINE *impl = NULL;
  349. char *passin = NULL;
  350. int rv = -1;
  351. X509 *x;
  352. if (((pkey_op == EVP_PKEY_OP_SIGN) || (pkey_op == EVP_PKEY_OP_DECRYPT)
  353. || (pkey_op == EVP_PKEY_OP_DERIVE))
  354. && (key_type != KEY_PRIVKEY && kdfalg == NULL)) {
  355. BIO_printf(bio_err, "A private key is needed for this operation\n");
  356. goto end;
  357. }
  358. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  359. BIO_printf(bio_err, "Error getting password\n");
  360. goto end;
  361. }
  362. switch (key_type) {
  363. case KEY_PRIVKEY:
  364. pkey = load_key(keyfile, keyform, 0, passin, e, "Private Key");
  365. break;
  366. case KEY_PUBKEY:
  367. pkey = load_pubkey(keyfile, keyform, 0, NULL, e, "Public Key");
  368. break;
  369. case KEY_CERT:
  370. x = load_cert(keyfile, keyform, "Certificate");
  371. if (x) {
  372. pkey = X509_get_pubkey(x);
  373. X509_free(x);
  374. }
  375. break;
  376. case KEY_NONE:
  377. break;
  378. }
  379. #ifndef OPENSSL_NO_ENGINE
  380. if (engine_impl)
  381. impl = e;
  382. #endif
  383. if (kdfalg != NULL) {
  384. int kdfnid = OBJ_sn2nid(kdfalg);
  385. if (kdfnid == NID_undef) {
  386. kdfnid = OBJ_ln2nid(kdfalg);
  387. if (kdfnid == NID_undef) {
  388. BIO_printf(bio_err, "The given KDF \"%s\" is unknown.\n",
  389. kdfalg);
  390. goto end;
  391. }
  392. }
  393. ctx = EVP_PKEY_CTX_new_id(kdfnid, impl);
  394. } else {
  395. if (pkey == NULL)
  396. goto end;
  397. *pkeysize = EVP_PKEY_size(pkey);
  398. ctx = EVP_PKEY_CTX_new(pkey, impl);
  399. EVP_PKEY_free(pkey);
  400. }
  401. if (ctx == NULL)
  402. goto end;
  403. switch (pkey_op) {
  404. case EVP_PKEY_OP_SIGN:
  405. rv = EVP_PKEY_sign_init(ctx);
  406. break;
  407. case EVP_PKEY_OP_VERIFY:
  408. rv = EVP_PKEY_verify_init(ctx);
  409. break;
  410. case EVP_PKEY_OP_VERIFYRECOVER:
  411. rv = EVP_PKEY_verify_recover_init(ctx);
  412. break;
  413. case EVP_PKEY_OP_ENCRYPT:
  414. rv = EVP_PKEY_encrypt_init(ctx);
  415. break;
  416. case EVP_PKEY_OP_DECRYPT:
  417. rv = EVP_PKEY_decrypt_init(ctx);
  418. break;
  419. case EVP_PKEY_OP_DERIVE:
  420. rv = EVP_PKEY_derive_init(ctx);
  421. break;
  422. }
  423. if (rv <= 0) {
  424. EVP_PKEY_CTX_free(ctx);
  425. ctx = NULL;
  426. }
  427. end:
  428. OPENSSL_free(passin);
  429. return ctx;
  430. }
  431. static int setup_peer(EVP_PKEY_CTX *ctx, int peerform, const char *file,
  432. ENGINE *e)
  433. {
  434. EVP_PKEY *peer = NULL;
  435. ENGINE *engine = NULL;
  436. int ret;
  437. if (peerform == FORMAT_ENGINE)
  438. engine = e;
  439. peer = load_pubkey(file, peerform, 0, NULL, engine, "Peer Key");
  440. if (peer == NULL) {
  441. BIO_printf(bio_err, "Error reading peer key %s\n", file);
  442. ERR_print_errors(bio_err);
  443. return 0;
  444. }
  445. ret = EVP_PKEY_derive_set_peer(ctx, peer);
  446. EVP_PKEY_free(peer);
  447. if (ret <= 0)
  448. ERR_print_errors(bio_err);
  449. return ret;
  450. }
  451. static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
  452. unsigned char *out, size_t *poutlen,
  453. const unsigned char *in, size_t inlen)
  454. {
  455. int rv = 0;
  456. switch (pkey_op) {
  457. case EVP_PKEY_OP_VERIFYRECOVER:
  458. rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen);
  459. break;
  460. case EVP_PKEY_OP_SIGN:
  461. rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen);
  462. break;
  463. case EVP_PKEY_OP_ENCRYPT:
  464. rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen);
  465. break;
  466. case EVP_PKEY_OP_DECRYPT:
  467. rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen);
  468. break;
  469. case EVP_PKEY_OP_DERIVE:
  470. rv = EVP_PKEY_derive(ctx, out, poutlen);
  471. break;
  472. }
  473. return rv;
  474. }