pkeyutl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. char *keyfile, int keyform, int key_type,
  71. char *passargin, int pkey_op, ENGINE *e);
  72. static int setup_peer(BIO *err, EVP_PKEY_CTX *ctx, int peerform,
  73. const char *file);
  74. static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
  75. unsigned char *out, size_t *poutlen,
  76. unsigned char *in, size_t inlen);
  77. int MAIN(int argc, char **);
  78. int MAIN(int argc, char **argv)
  79. {
  80. BIO *in = NULL, *out = NULL;
  81. char *infile = NULL, *outfile = NULL, *sigfile = NULL;
  82. ENGINE *e = NULL;
  83. int pkey_op = EVP_PKEY_OP_SIGN, key_type = KEY_PRIVKEY;
  84. int keyform = FORMAT_PEM, peerform = FORMAT_PEM;
  85. char badarg = 0, rev = 0;
  86. char hexdump = 0, asn1parse = 0;
  87. EVP_PKEY_CTX *ctx = NULL;
  88. char *passargin = NULL;
  89. int keysize = -1;
  90. unsigned char *buf_in = NULL, *buf_out = NULL, *sig = NULL;
  91. size_t buf_outlen;
  92. int buf_inlen = 0, siglen = -1;
  93. int ret = 1, rv = -1;
  94. argc--;
  95. argv++;
  96. if (!bio_err)
  97. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  98. if (!load_config(bio_err, NULL))
  99. goto end;
  100. ERR_load_crypto_strings();
  101. OpenSSL_add_all_algorithms();
  102. while (argc >= 1) {
  103. if (!strcmp(*argv, "-in")) {
  104. if (--argc < 1)
  105. badarg = 1;
  106. else
  107. infile = *(++argv);
  108. } else if (!strcmp(*argv, "-out")) {
  109. if (--argc < 1)
  110. badarg = 1;
  111. else
  112. outfile = *(++argv);
  113. } else if (!strcmp(*argv, "-sigfile")) {
  114. if (--argc < 1)
  115. badarg = 1;
  116. else
  117. sigfile = *(++argv);
  118. } else if (!strcmp(*argv, "-inkey")) {
  119. if (--argc < 1)
  120. badarg = 1;
  121. else {
  122. ctx = init_ctx(&keysize,
  123. *(++argv), keyform, key_type,
  124. passargin, pkey_op, e);
  125. if (!ctx) {
  126. BIO_puts(bio_err, "Error initializing context\n");
  127. ERR_print_errors(bio_err);
  128. badarg = 1;
  129. }
  130. }
  131. } else if (!strcmp(*argv, "-peerkey")) {
  132. if (--argc < 1)
  133. badarg = 1;
  134. else if (!setup_peer(bio_err, ctx, peerform, *(++argv)))
  135. badarg = 1;
  136. } else if (!strcmp(*argv, "-passin")) {
  137. if (--argc < 1)
  138. badarg = 1;
  139. else
  140. passargin = *(++argv);
  141. } else if (strcmp(*argv, "-peerform") == 0) {
  142. if (--argc < 1)
  143. badarg = 1;
  144. else
  145. peerform = str2fmt(*(++argv));
  146. } else if (strcmp(*argv, "-keyform") == 0) {
  147. if (--argc < 1)
  148. badarg = 1;
  149. else
  150. keyform = str2fmt(*(++argv));
  151. }
  152. #ifndef OPENSSL_NO_ENGINE
  153. else if (!strcmp(*argv, "-engine")) {
  154. if (--argc < 1)
  155. badarg = 1;
  156. else
  157. e = setup_engine(bio_err, *(++argv), 0);
  158. }
  159. #endif
  160. else if (!strcmp(*argv, "-pubin"))
  161. key_type = KEY_PUBKEY;
  162. else if (!strcmp(*argv, "-certin"))
  163. key_type = KEY_CERT;
  164. else if (!strcmp(*argv, "-asn1parse"))
  165. asn1parse = 1;
  166. else if (!strcmp(*argv, "-hexdump"))
  167. hexdump = 1;
  168. else if (!strcmp(*argv, "-sign"))
  169. pkey_op = EVP_PKEY_OP_SIGN;
  170. else if (!strcmp(*argv, "-verify"))
  171. pkey_op = EVP_PKEY_OP_VERIFY;
  172. else if (!strcmp(*argv, "-verifyrecover"))
  173. pkey_op = EVP_PKEY_OP_VERIFYRECOVER;
  174. else if (!strcmp(*argv, "-rev"))
  175. rev = 1;
  176. else if (!strcmp(*argv, "-encrypt"))
  177. pkey_op = EVP_PKEY_OP_ENCRYPT;
  178. else if (!strcmp(*argv, "-decrypt"))
  179. pkey_op = EVP_PKEY_OP_DECRYPT;
  180. else if (!strcmp(*argv, "-derive"))
  181. pkey_op = EVP_PKEY_OP_DERIVE;
  182. else if (strcmp(*argv, "-pkeyopt") == 0) {
  183. if (--argc < 1)
  184. badarg = 1;
  185. else if (!ctx) {
  186. BIO_puts(bio_err, "-pkeyopt command before -inkey\n");
  187. badarg = 1;
  188. } else if (pkey_ctrl_string(ctx, *(++argv)) <= 0) {
  189. BIO_puts(bio_err, "parameter setting error\n");
  190. ERR_print_errors(bio_err);
  191. goto end;
  192. }
  193. } else
  194. badarg = 1;
  195. if (badarg) {
  196. usage();
  197. goto end;
  198. }
  199. argc--;
  200. argv++;
  201. }
  202. if (!ctx) {
  203. usage();
  204. goto end;
  205. }
  206. if (sigfile && (pkey_op != EVP_PKEY_OP_VERIFY)) {
  207. BIO_puts(bio_err, "Signature file specified for non verify\n");
  208. goto end;
  209. }
  210. if (!sigfile && (pkey_op == EVP_PKEY_OP_VERIFY)) {
  211. BIO_puts(bio_err, "No signature file specified for verify\n");
  212. goto end;
  213. }
  214. /* FIXME: seed PRNG only if needed */
  215. app_RAND_load_file(NULL, bio_err, 0);
  216. if (pkey_op != EVP_PKEY_OP_DERIVE) {
  217. if (infile) {
  218. if (!(in = BIO_new_file(infile, "rb"))) {
  219. BIO_puts(bio_err, "Error Opening Input File\n");
  220. ERR_print_errors(bio_err);
  221. goto end;
  222. }
  223. } else
  224. in = BIO_new_fp(stdin, BIO_NOCLOSE);
  225. }
  226. if (outfile) {
  227. if (!(out = BIO_new_file(outfile, "wb"))) {
  228. BIO_printf(bio_err, "Error Creating Output File\n");
  229. ERR_print_errors(bio_err);
  230. goto end;
  231. }
  232. } else {
  233. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  234. #ifdef OPENSSL_SYS_VMS
  235. {
  236. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  237. out = BIO_push(tmpbio, out);
  238. }
  239. #endif
  240. }
  241. if (sigfile) {
  242. BIO *sigbio = BIO_new_file(sigfile, "rb");
  243. if (!sigbio) {
  244. BIO_printf(bio_err, "Can't open signature file %s\n", sigfile);
  245. goto end;
  246. }
  247. siglen = bio_to_mem(&sig, keysize * 10, sigbio);
  248. BIO_free(sigbio);
  249. if (siglen <= 0) {
  250. BIO_printf(bio_err, "Error reading signature data\n");
  251. goto end;
  252. }
  253. }
  254. if (in) {
  255. /* Read the input data */
  256. buf_inlen = bio_to_mem(&buf_in, keysize * 10, in);
  257. if (buf_inlen <= 0) {
  258. BIO_printf(bio_err, "Error reading input Data\n");
  259. exit(1);
  260. }
  261. if (rev) {
  262. size_t i;
  263. unsigned char ctmp;
  264. size_t l = (size_t)buf_inlen;
  265. for (i = 0; i < l / 2; i++) {
  266. ctmp = buf_in[i];
  267. buf_in[i] = buf_in[l - 1 - i];
  268. buf_in[l - 1 - i] = ctmp;
  269. }
  270. }
  271. }
  272. if (pkey_op == EVP_PKEY_OP_VERIFY) {
  273. rv = EVP_PKEY_verify(ctx, sig, (size_t)siglen,
  274. buf_in, (size_t)buf_inlen);
  275. if (rv == 0)
  276. BIO_puts(out, "Signature Verification Failure\n");
  277. else if (rv == 1)
  278. BIO_puts(out, "Signature Verified Successfully\n");
  279. if (rv >= 0)
  280. goto end;
  281. } else {
  282. rv = do_keyop(ctx, pkey_op, NULL, (size_t *)&buf_outlen,
  283. buf_in, (size_t)buf_inlen);
  284. if (rv > 0) {
  285. buf_out = OPENSSL_malloc(buf_outlen);
  286. if (!buf_out)
  287. rv = -1;
  288. else
  289. rv = do_keyop(ctx, pkey_op,
  290. buf_out, (size_t *)&buf_outlen,
  291. buf_in, (size_t)buf_inlen);
  292. }
  293. }
  294. if (rv <= 0) {
  295. BIO_printf(bio_err, "Public Key operation error\n");
  296. ERR_print_errors(bio_err);
  297. goto end;
  298. }
  299. ret = 0;
  300. if (asn1parse) {
  301. if (!ASN1_parse_dump(out, buf_out, buf_outlen, 1, -1))
  302. ERR_print_errors(bio_err);
  303. } else if (hexdump)
  304. BIO_dump(out, (char *)buf_out, buf_outlen);
  305. else
  306. BIO_write(out, buf_out, buf_outlen);
  307. end:
  308. if (ctx)
  309. EVP_PKEY_CTX_free(ctx);
  310. BIO_free(in);
  311. BIO_free_all(out);
  312. if (buf_in)
  313. OPENSSL_free(buf_in);
  314. if (buf_out)
  315. OPENSSL_free(buf_out);
  316. if (sig)
  317. OPENSSL_free(sig);
  318. return ret;
  319. }
  320. static void usage()
  321. {
  322. BIO_printf(bio_err, "Usage: pkeyutl [options]\n");
  323. BIO_printf(bio_err, "-in file input file\n");
  324. BIO_printf(bio_err, "-out file output file\n");
  325. BIO_printf(bio_err,
  326. "-sigfile file signature file (verify operation only)\n");
  327. BIO_printf(bio_err, "-inkey file input key\n");
  328. BIO_printf(bio_err, "-keyform arg private key format - default PEM\n");
  329. BIO_printf(bio_err, "-pubin input is a public key\n");
  330. BIO_printf(bio_err,
  331. "-certin input is a certificate carrying a public key\n");
  332. BIO_printf(bio_err, "-pkeyopt X:Y public key options\n");
  333. BIO_printf(bio_err, "-sign sign with private key\n");
  334. BIO_printf(bio_err, "-verify verify with public key\n");
  335. BIO_printf(bio_err,
  336. "-verifyrecover verify with public key, recover original data\n");
  337. BIO_printf(bio_err, "-encrypt encrypt with public key\n");
  338. BIO_printf(bio_err, "-decrypt decrypt with private key\n");
  339. BIO_printf(bio_err, "-derive derive shared secret\n");
  340. BIO_printf(bio_err, "-hexdump hex dump output\n");
  341. #ifndef OPENSSL_NO_ENGINE
  342. BIO_printf(bio_err,
  343. "-engine e use engine e, possibly a hardware device.\n");
  344. #endif
  345. BIO_printf(bio_err, "-passin arg pass phrase source\n");
  346. }
  347. static EVP_PKEY_CTX *init_ctx(int *pkeysize,
  348. char *keyfile, int keyform, int key_type,
  349. char *passargin, int pkey_op, ENGINE *e)
  350. {
  351. EVP_PKEY *pkey = NULL;
  352. EVP_PKEY_CTX *ctx = NULL;
  353. char *passin = NULL;
  354. int rv = -1;
  355. X509 *x;
  356. if (((pkey_op == EVP_PKEY_OP_SIGN) || (pkey_op == EVP_PKEY_OP_DECRYPT)
  357. || (pkey_op == EVP_PKEY_OP_DERIVE))
  358. && (key_type != KEY_PRIVKEY)) {
  359. BIO_printf(bio_err, "A private key is needed for this operation\n");
  360. goto end;
  361. }
  362. if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
  363. BIO_printf(bio_err, "Error getting password\n");
  364. goto end;
  365. }
  366. switch (key_type) {
  367. case KEY_PRIVKEY:
  368. pkey = load_key(bio_err, keyfile, keyform, 0,
  369. passin, e, "Private Key");
  370. break;
  371. case KEY_PUBKEY:
  372. pkey = load_pubkey(bio_err, keyfile, keyform, 0,
  373. NULL, e, "Public Key");
  374. break;
  375. case KEY_CERT:
  376. x = load_cert(bio_err, keyfile, keyform, NULL, e, "Certificate");
  377. if (x) {
  378. pkey = X509_get_pubkey(x);
  379. X509_free(x);
  380. }
  381. break;
  382. }
  383. *pkeysize = EVP_PKEY_size(pkey);
  384. if (!pkey)
  385. goto end;
  386. ctx = EVP_PKEY_CTX_new(pkey, e);
  387. EVP_PKEY_free(pkey);
  388. if (!ctx)
  389. goto end;
  390. switch (pkey_op) {
  391. case EVP_PKEY_OP_SIGN:
  392. rv = EVP_PKEY_sign_init(ctx);
  393. break;
  394. case EVP_PKEY_OP_VERIFY:
  395. rv = EVP_PKEY_verify_init(ctx);
  396. break;
  397. case EVP_PKEY_OP_VERIFYRECOVER:
  398. rv = EVP_PKEY_verify_recover_init(ctx);
  399. break;
  400. case EVP_PKEY_OP_ENCRYPT:
  401. rv = EVP_PKEY_encrypt_init(ctx);
  402. break;
  403. case EVP_PKEY_OP_DECRYPT:
  404. rv = EVP_PKEY_decrypt_init(ctx);
  405. break;
  406. case EVP_PKEY_OP_DERIVE:
  407. rv = EVP_PKEY_derive_init(ctx);
  408. break;
  409. }
  410. if (rv <= 0) {
  411. EVP_PKEY_CTX_free(ctx);
  412. ctx = NULL;
  413. }
  414. end:
  415. if (passin)
  416. OPENSSL_free(passin);
  417. return ctx;
  418. }
  419. static int setup_peer(BIO *err, EVP_PKEY_CTX *ctx, int peerform,
  420. const char *file)
  421. {
  422. EVP_PKEY *peer = NULL;
  423. int ret;
  424. if (!ctx) {
  425. BIO_puts(err, "-peerkey command before -inkey\n");
  426. return 0;
  427. }
  428. peer = load_pubkey(bio_err, file, peerform, 0, NULL, NULL, "Peer Key");
  429. if (!peer) {
  430. BIO_printf(bio_err, "Error reading peer key %s\n", file);
  431. ERR_print_errors(err);
  432. return 0;
  433. }
  434. ret = EVP_PKEY_derive_set_peer(ctx, peer);
  435. EVP_PKEY_free(peer);
  436. if (ret <= 0)
  437. ERR_print_errors(err);
  438. return ret;
  439. }
  440. static int do_keyop(EVP_PKEY_CTX *ctx, int pkey_op,
  441. unsigned char *out, size_t *poutlen,
  442. unsigned char *in, size_t inlen)
  443. {
  444. int rv = 0;
  445. switch (pkey_op) {
  446. case EVP_PKEY_OP_VERIFYRECOVER:
  447. rv = EVP_PKEY_verify_recover(ctx, out, poutlen, in, inlen);
  448. break;
  449. case EVP_PKEY_OP_SIGN:
  450. rv = EVP_PKEY_sign(ctx, out, poutlen, in, inlen);
  451. break;
  452. case EVP_PKEY_OP_ENCRYPT:
  453. rv = EVP_PKEY_encrypt(ctx, out, poutlen, in, inlen);
  454. break;
  455. case EVP_PKEY_OP_DECRYPT:
  456. rv = EVP_PKEY_decrypt(ctx, out, poutlen, in, inlen);
  457. break;
  458. case EVP_PKEY_OP_DERIVE:
  459. rv = EVP_PKEY_derive(ctx, out, poutlen);
  460. break;
  461. }
  462. return rv;
  463. }