pkeyutl.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*
  2. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  3. * 2006.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include "apps.h"
  59. #include <string.h>
  60. #include <openssl/err.h>
  61. #include <openssl/pem.h>
  62. #include <openssl/evp.h>
  63. #define KEY_PRIVKEY 1
  64. #define KEY_PUBKEY 2
  65. #define KEY_CERT 3
  66. static void usage(void);
  67. #undef PROG
  68. #define PROG pkeyutl_main
  69. static EVP_PKEY_CTX *init_ctx(int *pkeysize,
  70. const char *keyfile, int keyform, int key_type,
  71. char *passargin, int pkey_op, ENGINE *e,
  72. int impl);
  73. static int setup_peer(BIO *err, EVP_PKEY_CTX *ctx, int peerform,
  74. const char *file, ENGINE* e);
  75. static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
  76. unsigned char *out, size_t *poutlen,
  77. unsigned char *in, size_t inlen);
  78. int MAIN(int argc, char **);
  79. int MAIN(int argc, char **argv)
  80. {
  81. BIO *in = NULL, *out = NULL;
  82. char *infile = NULL, *outfile = NULL, *sigfile = NULL;
  83. ENGINE *e = NULL;
  84. int pkey_op = EVP_PKEY_OP_SIGN, key_type = KEY_PRIVKEY;
  85. int keyform = FORMAT_PEM, peerform = FORMAT_PEM;
  86. char badarg = 0, rev = 0;
  87. char hexdump = 0, asn1parse = 0;
  88. EVP_PKEY_CTX *ctx = NULL;
  89. char *passargin = NULL;
  90. int keysize = -1;
  91. int engine_impl = 0;
  92. unsigned char *buf_in = NULL, *buf_out = NULL, *sig = NULL;
  93. size_t buf_outlen = 0;
  94. int buf_inlen = 0, siglen = -1;
  95. const char *inkey = NULL;
  96. const char *peerkey = NULL;
  97. STACK_OF(OPENSSL_STRING) *pkeyopts = NULL;
  98. int ret = 1, rv = -1;
  99. argc--;
  100. argv++;
  101. if (!bio_err)
  102. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  103. if (!load_config(bio_err, NULL))
  104. goto end;
  105. ERR_load_crypto_strings();
  106. OpenSSL_add_all_algorithms();
  107. while (argc >= 1) {
  108. if (!strcmp(*argv, "-in")) {
  109. if (--argc < 1)
  110. badarg = 1;
  111. else
  112. infile = *(++argv);
  113. } else if (!strcmp(*argv, "-out")) {
  114. if (--argc < 1)
  115. badarg = 1;
  116. else
  117. outfile = *(++argv);
  118. } else if (!strcmp(*argv, "-sigfile")) {
  119. if (--argc < 1)
  120. badarg = 1;
  121. else
  122. sigfile = *(++argv);
  123. } else if (!strcmp(*argv, "-inkey")) {
  124. if (--argc < 1)
  125. badarg = 1;
  126. else
  127. inkey = *++argv;
  128. } else if (!strcmp(*argv, "-peerkey")) {
  129. if (--argc < 1)
  130. badarg = 1;
  131. else
  132. peerkey = *++argv;
  133. } else if (!strcmp(*argv, "-passin")) {
  134. if (--argc < 1)
  135. badarg = 1;
  136. else
  137. passargin = *(++argv);
  138. } else if (strcmp(*argv, "-peerform") == 0) {
  139. if (--argc < 1)
  140. badarg = 1;
  141. else
  142. peerform = str2fmt(*(++argv));
  143. } else if (strcmp(*argv, "-keyform") == 0) {
  144. if (--argc < 1)
  145. badarg = 1;
  146. else
  147. keyform = str2fmt(*(++argv));
  148. }
  149. #ifndef OPENSSL_NO_ENGINE
  150. else if (!strcmp(*argv, "-engine")) {
  151. if (--argc < 1)
  152. badarg = 1;
  153. else
  154. e = setup_engine(bio_err, *(++argv), 0);
  155. } else if (!strcmp(*argv, "-engine_impl")) {
  156. engine_impl = 1;
  157. }
  158. #endif
  159. else if (!strcmp(*argv, "-pubin"))
  160. key_type = KEY_PUBKEY;
  161. else if (!strcmp(*argv, "-certin"))
  162. key_type = KEY_CERT;
  163. else if (!strcmp(*argv, "-asn1parse"))
  164. asn1parse = 1;
  165. else if (!strcmp(*argv, "-hexdump"))
  166. hexdump = 1;
  167. else if (!strcmp(*argv, "-sign"))
  168. pkey_op = EVP_PKEY_OP_SIGN;
  169. else if (!strcmp(*argv, "-verify"))
  170. pkey_op = EVP_PKEY_OP_VERIFY;
  171. else if (!strcmp(*argv, "-verifyrecover"))
  172. pkey_op = EVP_PKEY_OP_VERIFYRECOVER;
  173. else if (!strcmp(*argv, "-encrypt"))
  174. pkey_op = EVP_PKEY_OP_ENCRYPT;
  175. else if (!strcmp(*argv, "-decrypt"))
  176. pkey_op = EVP_PKEY_OP_DECRYPT;
  177. else if (!strcmp(*argv, "-derive"))
  178. pkey_op = EVP_PKEY_OP_DERIVE;
  179. else if (!strcmp(*argv, "-rev"))
  180. rev = 1;
  181. else if (strcmp(*argv, "-pkeyopt") == 0) {
  182. if (--argc < 1)
  183. badarg = 1;
  184. else if ((pkeyopts == NULL &&
  185. (pkeyopts = sk_OPENSSL_STRING_new_null()) == NULL) ||
  186. sk_OPENSSL_STRING_push(pkeyopts, *++argv) == 0) {
  187. BIO_puts(bio_err, "out of memory\n");
  188. goto end;
  189. }
  190. } else
  191. badarg = 1;
  192. if (badarg) {
  193. usage();
  194. goto end;
  195. }
  196. argc--;
  197. argv++;
  198. }
  199. if (inkey == NULL ||
  200. (peerkey != NULL && pkey_op != EVP_PKEY_OP_DERIVE)) {
  201. usage();
  202. goto end;
  203. }
  204. ctx = init_ctx(&keysize, inkey, keyform, key_type,
  205. passargin, pkey_op, e, engine_impl);
  206. if (!ctx) {
  207. BIO_puts(bio_err, "Error initializing context\n");
  208. ERR_print_errors(bio_err);
  209. goto end;
  210. }
  211. if (peerkey != NULL && !setup_peer(bio_err, ctx, peerform, peerkey, e)) {
  212. BIO_puts(bio_err, "Error setting up peer key\n");
  213. ERR_print_errors(bio_err);
  214. goto end;
  215. }
  216. if (pkeyopts != NULL) {
  217. int num = sk_OPENSSL_STRING_num(pkeyopts);
  218. int i;
  219. for (i = 0; i < num; ++i) {
  220. const char *opt = sk_OPENSSL_STRING_value(pkeyopts, i);
  221. if (pkey_ctrl_string(ctx, opt) <= 0) {
  222. BIO_puts(bio_err, "parameter setting error\n");
  223. ERR_print_errors(bio_err);
  224. goto end;
  225. }
  226. }
  227. }
  228. if (sigfile && (pkey_op != EVP_PKEY_OP_VERIFY)) {
  229. BIO_puts(bio_err, "Signature file specified for non verify\n");
  230. goto end;
  231. }
  232. if (!sigfile && (pkey_op == EVP_PKEY_OP_VERIFY)) {
  233. BIO_puts(bio_err, "No signature file specified for verify\n");
  234. goto end;
  235. }
  236. /* FIXME: seed PRNG only if needed */
  237. app_RAND_load_file(NULL, bio_err, 0);
  238. if (pkey_op != EVP_PKEY_OP_DERIVE) {
  239. if (infile) {
  240. if (!(in = BIO_new_file(infile, "rb"))) {
  241. BIO_puts(bio_err, "Error Opening Input File\n");
  242. ERR_print_errors(bio_err);
  243. goto end;
  244. }
  245. } else
  246. in = BIO_new_fp(stdin, BIO_NOCLOSE);
  247. }
  248. if (outfile) {
  249. if (!(out = BIO_new_file(outfile, "wb"))) {
  250. BIO_printf(bio_err, "Error Creating Output File\n");
  251. ERR_print_errors(bio_err);
  252. goto end;
  253. }
  254. } else {
  255. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  256. #ifdef OPENSSL_SYS_VMS
  257. {
  258. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  259. out = BIO_push(tmpbio, out);
  260. }
  261. #endif
  262. }
  263. if (sigfile) {
  264. BIO *sigbio = BIO_new_file(sigfile, "rb");
  265. if (!sigbio) {
  266. BIO_printf(bio_err, "Can't open signature file %s\n", sigfile);
  267. goto end;
  268. }
  269. siglen = bio_to_mem(&sig, keysize * 10, sigbio);
  270. BIO_free(sigbio);
  271. if (siglen < 0) {
  272. BIO_printf(bio_err, "Error reading signature data\n");
  273. goto end;
  274. }
  275. }
  276. if (in) {
  277. /* Read the input data */
  278. buf_inlen = bio_to_mem(&buf_in, keysize * 10, in);
  279. if (buf_inlen < 0) {
  280. BIO_printf(bio_err, "Error reading input Data\n");
  281. exit(1);
  282. }
  283. if (rev) {
  284. size_t i;
  285. unsigned char ctmp;
  286. size_t l = (size_t)buf_inlen;
  287. for (i = 0; i < l / 2; i++) {
  288. ctmp = buf_in[i];
  289. buf_in[i] = buf_in[l - 1 - i];
  290. buf_in[l - 1 - i] = ctmp;
  291. }
  292. }
  293. }
  294. if (pkey_op == EVP_PKEY_OP_VERIFY) {
  295. rv = EVP_PKEY_verify(ctx, sig, (size_t)siglen,
  296. buf_in, (size_t)buf_inlen);
  297. if (rv == 0)
  298. BIO_puts(out, "Signature Verification Failure\n");
  299. else if (rv == 1) {
  300. BIO_puts(out, "Signature Verified Successfully\n");
  301. ret = 0;
  302. }
  303. if (rv >= 0)
  304. goto end;
  305. } else {
  306. rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen,
  307. buf_in, (size_t)buf_inlen);
  308. if (rv > 0 && buf_outlen != 0) {
  309. buf_out = OPENSSL_malloc(buf_outlen);
  310. if (!buf_out)
  311. rv = -1;
  312. else
  313. rv = do_keyop(ctx, pkey_op,
  314. buf_out, (size_t *)&buf_outlen,
  315. buf_in, (size_t)buf_inlen);
  316. }
  317. }
  318. if (rv <= 0) {
  319. BIO_printf(bio_err, "Public Key operation error\n");
  320. ERR_print_errors(bio_err);
  321. goto end;
  322. }
  323. ret = 0;
  324. if (asn1parse) {
  325. if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1))
  326. ERR_print_errors(bio_err);
  327. } else if (hexdump)
  328. BIO_dump(out, (char *)buf_out, buf_outlen);
  329. else
  330. BIO_write(out, buf_out, buf_outlen);
  331. end:
  332. if (ctx)
  333. EVP_PKEY_CTX_free(ctx);
  334. release_engine(e);
  335. BIO_free(in);
  336. BIO_free_all(out);
  337. if (buf_in != NULL)
  338. OPENSSL_free(buf_in);
  339. if (buf_out != NULL)
  340. OPENSSL_free(buf_out);
  341. if (sig != NULL)
  342. OPENSSL_free(sig);
  343. if (pkeyopts != NULL)
  344. sk_OPENSSL_STRING_free(pkeyopts);
  345. return ret;
  346. }
  347. static void usage()
  348. {
  349. BIO_printf(bio_err, "Usage: pkeyutl [options]\n");
  350. BIO_printf(bio_err, "-in file input file\n");
  351. BIO_printf(bio_err, "-out file output file\n");
  352. BIO_printf(bio_err,
  353. "-sigfile file signature file (verify operation only)\n");
  354. BIO_printf(bio_err, "-inkey file input key\n");
  355. BIO_printf(bio_err, "-keyform arg private key format - default PEM\n");
  356. BIO_printf(bio_err, "-pubin input is a public key\n");
  357. BIO_printf(bio_err,
  358. "-certin input is a certificate carrying a public key\n");
  359. BIO_printf(bio_err, "-pkeyopt X:Y public key options\n");
  360. BIO_printf(bio_err, "-sign sign with private key\n");
  361. BIO_printf(bio_err, "-verify verify with public key\n");
  362. BIO_printf(bio_err,
  363. "-verifyrecover verify with public key, recover original data\n");
  364. BIO_printf(bio_err, "-encrypt encrypt with public key\n");
  365. BIO_printf(bio_err, "-decrypt decrypt with private key\n");
  366. BIO_printf(bio_err, "-derive derive shared secret\n");
  367. BIO_printf(bio_err, "-hexdump hex dump output\n");
  368. #ifndef OPENSSL_NO_ENGINE
  369. BIO_printf(bio_err,
  370. "-engine e use engine e, maybe a hardware device, for loading keys.\n");
  371. BIO_printf(bio_err, "-engine_impl also use engine given by -engine for crypto operations\n");
  372. #endif
  373. BIO_printf(bio_err, "-passin arg pass phrase source\n");
  374. }
  375. static EVP_PKEY_CTX *init_ctx(int *pkeysize,
  376. const char *keyfile, int keyform, int key_type,
  377. char *passargin, int pkey_op, ENGINE *e,
  378. int engine_impl)
  379. {
  380. EVP_PKEY *pkey = NULL;
  381. EVP_PKEY_CTX *ctx = NULL;
  382. ENGINE *impl = NULL;
  383. char *passin = NULL;
  384. int rv = -1;
  385. X509 *x;
  386. if (((pkey_op == EVP_PKEY_OP_SIGN) || (pkey_op == EVP_PKEY_OP_DECRYPT)
  387. || (pkey_op == EVP_PKEY_OP_DERIVE))
  388. && (key_type != KEY_PRIVKEY)) {
  389. BIO_printf(bio_err, "A private key is needed for this operation\n");
  390. goto end;
  391. }
  392. if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
  393. BIO_printf(bio_err, "Error getting password\n");
  394. goto end;
  395. }
  396. switch (key_type) {
  397. case KEY_PRIVKEY:
  398. pkey = load_key(bio_err, keyfile, keyform, 0,
  399. passin, e, "Private Key");
  400. break;
  401. case KEY_PUBKEY:
  402. pkey = load_pubkey(bio_err, keyfile, keyform, 0,
  403. NULL, e, "Public Key");
  404. break;
  405. case KEY_CERT:
  406. x = load_cert(bio_err, keyfile, keyform, NULL, e, "Certificate");
  407. if (x) {
  408. pkey = X509_get_pubkey(x);
  409. X509_free(x);
  410. }
  411. break;
  412. }
  413. *pkeysize = EVP_PKEY_size(pkey);
  414. if (!pkey)
  415. goto end;
  416. #ifndef OPENSSL_NO_ENGINE
  417. if (engine_impl)
  418. impl = e;
  419. #endif
  420. ctx = EVP_PKEY_CTX_new(pkey, impl);
  421. EVP_PKEY_free(pkey);
  422. if (!ctx)
  423. goto end;
  424. switch (pkey_op) {
  425. case EVP_PKEY_OP_SIGN:
  426. rv = EVP_PKEY_sign_init(ctx);
  427. break;
  428. case EVP_PKEY_OP_VERIFY:
  429. rv = EVP_PKEY_verify_init(ctx);
  430. break;
  431. case EVP_PKEY_OP_VERIFYRECOVER:
  432. rv = EVP_PKEY_verify_recover_init(ctx);
  433. break;
  434. case EVP_PKEY_OP_ENCRYPT:
  435. rv = EVP_PKEY_encrypt_init(ctx);
  436. break;
  437. case EVP_PKEY_OP_DECRYPT:
  438. rv = EVP_PKEY_decrypt_init(ctx);
  439. break;
  440. case EVP_PKEY_OP_DERIVE:
  441. rv = EVP_PKEY_derive_init(ctx);
  442. break;
  443. }
  444. if (rv <= 0) {
  445. EVP_PKEY_CTX_free(ctx);
  446. ctx = NULL;
  447. }
  448. end:
  449. if (passin)
  450. OPENSSL_free(passin);
  451. return ctx;
  452. }
  453. static int setup_peer(BIO *err, EVP_PKEY_CTX *ctx, int peerform,
  454. const char *file, ENGINE* e)
  455. {
  456. EVP_PKEY *peer = NULL;
  457. ENGINE* engine = NULL;
  458. int ret;
  459. if (peerform == FORMAT_ENGINE)
  460. engine = e;
  461. peer = load_pubkey(bio_err, file, peerform, 0, NULL, engine, "Peer Key");
  462. if (!peer) {
  463. BIO_printf(bio_err, "Error reading peer key %s\n", file);
  464. ERR_print_errors(err);
  465. return 0;
  466. }
  467. ret = EVP_PKEY_derive_set_peer(ctx, peer);
  468. EVP_PKEY_free(peer);
  469. if (ret <= 0)
  470. ERR_print_errors(err);
  471. return ret;
  472. }
  473. static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
  474. unsigned char *out, size_t *poutlen,
  475. unsigned char *in, size_t inlen)
  476. {
  477. int rv = 0;
  478. switch (pkey_op) {
  479. case EVP_PKEY_OP_VERIFYRECOVER:
  480. rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen);
  481. break;
  482. case EVP_PKEY_OP_SIGN:
  483. rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen);
  484. break;
  485. case EVP_PKEY_OP_ENCRYPT:
  486. rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen);
  487. break;
  488. case EVP_PKEY_OP_DECRYPT:
  489. rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen);
  490. break;
  491. case EVP_PKEY_OP_DERIVE:
  492. rv = EVP_PKEY_derive(ctx, out, poutlen);
  493. break;
  494. }
  495. return rv;
  496. }