eng_openssl.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /*
  2. * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. /* We need to use some engine deprecated APIs */
  11. #define OPENSSL_SUPPRESS_DEPRECATED
  12. /*
  13. * RC4 and SHA-1 low level APIs are deprecated for public use, but still ok
  14. * for internal use.
  15. */
  16. #include "internal/deprecated.h"
  17. #include <stdio.h>
  18. #include <openssl/crypto.h>
  19. #include "internal/cryptlib.h"
  20. #include "crypto/engine.h"
  21. #include <openssl/pem.h>
  22. #include <openssl/evp.h>
  23. #include <openssl/rand.h>
  24. #include <openssl/rsa.h>
  25. #include <openssl/dsa.h>
  26. #include <openssl/dh.h>
  27. #include <openssl/hmac.h>
  28. #include <openssl/x509v3.h>
  29. /*
  30. * This testing gunk is implemented (and explained) lower down. It also
  31. * assumes the application explicitly calls "ENGINE_load_openssl()" because
  32. * this is no longer automatic in ENGINE_load_builtin_engines().
  33. */
  34. #define TEST_ENG_OPENSSL_RC4
  35. #ifndef OPENSSL_NO_STDIO
  36. # define TEST_ENG_OPENSSL_PKEY
  37. #endif
  38. /* #define TEST_ENG_OPENSSL_HMAC */
  39. /* #define TEST_ENG_OPENSSL_HMAC_INIT */
  40. /* #define TEST_ENG_OPENSSL_RC4_OTHERS */
  41. #ifndef OPENSSL_NO_STDIO
  42. # define TEST_ENG_OPENSSL_RC4_P_INIT
  43. #endif
  44. /* #define TEST_ENG_OPENSSL_RC4_P_CIPHER */
  45. #define TEST_ENG_OPENSSL_SHA
  46. /* #define TEST_ENG_OPENSSL_SHA_OTHERS */
  47. /* #define TEST_ENG_OPENSSL_SHA_P_INIT */
  48. /* #define TEST_ENG_OPENSSL_SHA_P_UPDATE */
  49. /* #define TEST_ENG_OPENSSL_SHA_P_FINAL */
  50. /* Now check what of those algorithms are actually enabled */
  51. #ifdef OPENSSL_NO_RC4
  52. # undef TEST_ENG_OPENSSL_RC4
  53. # undef TEST_ENG_OPENSSL_RC4_OTHERS
  54. # undef TEST_ENG_OPENSSL_RC4_P_INIT
  55. # undef TEST_ENG_OPENSSL_RC4_P_CIPHER
  56. #endif
  57. static int openssl_destroy(ENGINE *e);
  58. #ifdef TEST_ENG_OPENSSL_RC4
  59. static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  60. const int **nids, int nid);
  61. #endif
  62. #ifdef TEST_ENG_OPENSSL_SHA
  63. static int openssl_digests(ENGINE *e, const EVP_MD **digest,
  64. const int **nids, int nid);
  65. #endif
  66. #ifdef TEST_ENG_OPENSSL_PKEY
  67. static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id,
  68. UI_METHOD *ui_method,
  69. void *callback_data);
  70. #endif
  71. #ifdef TEST_ENG_OPENSSL_HMAC
  72. static int ossl_register_hmac_meth(void);
  73. static int ossl_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
  74. const int **nids, int nid);
  75. #endif
  76. /* The constants used when creating the ENGINE */
  77. static const char *engine_openssl_id = "openssl";
  78. static const char *engine_openssl_name = "Software engine support";
  79. /*
  80. * This internal function is used by ENGINE_openssl() and possibly by the
  81. * "dynamic" ENGINE support too
  82. */
  83. static int bind_helper(ENGINE *e)
  84. {
  85. if (!ENGINE_set_id(e, engine_openssl_id)
  86. || !ENGINE_set_name(e, engine_openssl_name)
  87. || !ENGINE_set_destroy_function(e, openssl_destroy)
  88. #ifndef TEST_ENG_OPENSSL_NO_ALGORITHMS
  89. # ifndef OPENSSL_NO_RSA
  90. || !ENGINE_set_RSA(e, RSA_get_default_method())
  91. # endif
  92. # ifndef OPENSSL_NO_DSA
  93. || !ENGINE_set_DSA(e, DSA_get_default_method())
  94. # endif
  95. # ifndef OPENSSL_NO_EC
  96. || !ENGINE_set_EC(e, EC_KEY_OpenSSL())
  97. # endif
  98. # ifndef OPENSSL_NO_DH
  99. || !ENGINE_set_DH(e, DH_get_default_method())
  100. # endif
  101. || !ENGINE_set_RAND(e, RAND_OpenSSL())
  102. # ifdef TEST_ENG_OPENSSL_RC4
  103. || !ENGINE_set_ciphers(e, openssl_ciphers)
  104. # endif
  105. # ifdef TEST_ENG_OPENSSL_SHA
  106. || !ENGINE_set_digests(e, openssl_digests)
  107. # endif
  108. #endif
  109. #ifdef TEST_ENG_OPENSSL_PKEY
  110. || !ENGINE_set_load_privkey_function(e, openssl_load_privkey)
  111. #endif
  112. #ifdef TEST_ENG_OPENSSL_HMAC
  113. || !ossl_register_hmac_meth()
  114. || !ENGINE_set_pkey_meths(e, ossl_pkey_meths)
  115. #endif
  116. )
  117. return 0;
  118. /*
  119. * If we add errors to this ENGINE, ensure the error handling is setup
  120. * here
  121. */
  122. /* openssl_load_error_strings(); */
  123. return 1;
  124. }
  125. static ENGINE *engine_openssl(void)
  126. {
  127. ENGINE *ret = ENGINE_new();
  128. if (ret == NULL)
  129. return NULL;
  130. if (!bind_helper(ret)) {
  131. ENGINE_free(ret);
  132. return NULL;
  133. }
  134. return ret;
  135. }
  136. void engine_load_openssl_int(void)
  137. {
  138. ENGINE *toadd = engine_openssl();
  139. if (!toadd)
  140. return;
  141. ENGINE_add(toadd);
  142. /*
  143. * If the "add" worked, it gets a structural reference. So either way, we
  144. * release our just-created reference.
  145. */
  146. ENGINE_free(toadd);
  147. ERR_clear_error();
  148. }
  149. /*
  150. * This stuff is needed if this ENGINE is being compiled into a
  151. * self-contained shared-library.
  152. */
  153. #ifdef ENGINE_DYNAMIC_SUPPORT
  154. static int bind_fn(ENGINE *e, const char *id)
  155. {
  156. if (id && (strcmp(id, engine_openssl_id) != 0))
  157. return 0;
  158. if (!bind_helper(e))
  159. return 0;
  160. return 1;
  161. }
  162. IMPLEMENT_DYNAMIC_CHECK_FN()
  163. IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
  164. #endif /* ENGINE_DYNAMIC_SUPPORT */
  165. #ifdef TEST_ENG_OPENSSL_RC4
  166. /*-
  167. * This section of code compiles an "alternative implementation" of two modes of
  168. * RC4 into this ENGINE. The result is that EVP_CIPHER operation for "rc4"
  169. * should under normal circumstances go via this support rather than the default
  170. * EVP support. There are other symbols to tweak the testing;
  171. * TEST_ENC_OPENSSL_RC4_OTHERS - print a one line message to stderr each time
  172. * we're asked for a cipher we don't support (should not happen).
  173. * TEST_ENG_OPENSSL_RC4_P_INIT - print a one line message to stderr each time
  174. * the "init_key" handler is called.
  175. * TEST_ENG_OPENSSL_RC4_P_CIPHER - ditto for the "cipher" handler.
  176. */
  177. # include <openssl/rc4.h>
  178. # define TEST_RC4_KEY_SIZE 16
  179. typedef struct {
  180. unsigned char key[TEST_RC4_KEY_SIZE];
  181. RC4_KEY ks;
  182. } TEST_RC4_KEY;
  183. # define test(ctx) ((TEST_RC4_KEY *)EVP_CIPHER_CTX_get_cipher_data(ctx))
  184. static int test_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  185. const unsigned char *iv, int enc)
  186. {
  187. const int n = EVP_CIPHER_CTX_key_length(ctx);
  188. # ifdef TEST_ENG_OPENSSL_RC4_P_INIT
  189. fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_init_key() called\n");
  190. # endif
  191. if (n <= 0)
  192. return n;
  193. memcpy(&test(ctx)->key[0], key, n);
  194. RC4_set_key(&test(ctx)->ks, n, test(ctx)->key);
  195. return 1;
  196. }
  197. static int test_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  198. const unsigned char *in, size_t inl)
  199. {
  200. # ifdef TEST_ENG_OPENSSL_RC4_P_CIPHER
  201. fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_cipher() called\n");
  202. # endif
  203. RC4(&test(ctx)->ks, inl, in, out);
  204. return 1;
  205. }
  206. static EVP_CIPHER *r4_cipher = NULL;
  207. static const EVP_CIPHER *test_r4_cipher(void)
  208. {
  209. if (r4_cipher == NULL) {
  210. EVP_CIPHER *cipher;
  211. if ((cipher = EVP_CIPHER_meth_new(NID_rc4, 1, TEST_RC4_KEY_SIZE)) == NULL
  212. || !EVP_CIPHER_meth_set_iv_length(cipher, 0)
  213. || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_VARIABLE_LENGTH)
  214. || !EVP_CIPHER_meth_set_init(cipher, test_rc4_init_key)
  215. || !EVP_CIPHER_meth_set_do_cipher(cipher, test_rc4_cipher)
  216. || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(TEST_RC4_KEY))) {
  217. EVP_CIPHER_meth_free(cipher);
  218. cipher = NULL;
  219. }
  220. r4_cipher = cipher;
  221. }
  222. return r4_cipher;
  223. }
  224. static void test_r4_cipher_destroy(void)
  225. {
  226. EVP_CIPHER_meth_free(r4_cipher);
  227. r4_cipher = NULL;
  228. }
  229. static EVP_CIPHER *r4_40_cipher = NULL;
  230. static const EVP_CIPHER *test_r4_40_cipher(void)
  231. {
  232. if (r4_40_cipher == NULL) {
  233. EVP_CIPHER *cipher;
  234. if ((cipher = EVP_CIPHER_meth_new(NID_rc4, 1, 5 /* 40 bits */)) == NULL
  235. || !EVP_CIPHER_meth_set_iv_length(cipher, 0)
  236. || !EVP_CIPHER_meth_set_flags(cipher, EVP_CIPH_VARIABLE_LENGTH)
  237. || !EVP_CIPHER_meth_set_init(cipher, test_rc4_init_key)
  238. || !EVP_CIPHER_meth_set_do_cipher(cipher, test_rc4_cipher)
  239. || !EVP_CIPHER_meth_set_impl_ctx_size(cipher, sizeof(TEST_RC4_KEY))) {
  240. EVP_CIPHER_meth_free(cipher);
  241. cipher = NULL;
  242. }
  243. r4_40_cipher = cipher;
  244. }
  245. return r4_40_cipher;
  246. }
  247. static void test_r4_40_cipher_destroy(void)
  248. {
  249. EVP_CIPHER_meth_free(r4_40_cipher);
  250. r4_40_cipher = NULL;
  251. }
  252. static int test_cipher_nids(const int **nids)
  253. {
  254. static int cipher_nids[4] = { 0, 0, 0, 0 };
  255. static int pos = 0;
  256. static int init = 0;
  257. if (!init) {
  258. const EVP_CIPHER *cipher;
  259. if ((cipher = test_r4_cipher()) != NULL)
  260. cipher_nids[pos++] = EVP_CIPHER_nid(cipher);
  261. if ((cipher = test_r4_40_cipher()) != NULL)
  262. cipher_nids[pos++] = EVP_CIPHER_nid(cipher);
  263. cipher_nids[pos] = 0;
  264. init = 1;
  265. }
  266. *nids = cipher_nids;
  267. return pos;
  268. }
  269. static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  270. const int **nids, int nid)
  271. {
  272. if (!cipher) {
  273. /* We are returning a list of supported nids */
  274. return test_cipher_nids(nids);
  275. }
  276. /* We are being asked for a specific cipher */
  277. if (nid == NID_rc4)
  278. *cipher = test_r4_cipher();
  279. else if (nid == NID_rc4_40)
  280. *cipher = test_r4_40_cipher();
  281. else {
  282. # ifdef TEST_ENG_OPENSSL_RC4_OTHERS
  283. fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) returning NULL for "
  284. "nid %d\n", nid);
  285. # endif
  286. *cipher = NULL;
  287. return 0;
  288. }
  289. return 1;
  290. }
  291. #endif
  292. #ifdef TEST_ENG_OPENSSL_SHA
  293. /* Much the same sort of comment as for TEST_ENG_OPENSSL_RC4 */
  294. # include <openssl/sha.h>
  295. static int test_sha1_init(EVP_MD_CTX *ctx)
  296. {
  297. # ifdef TEST_ENG_OPENSSL_SHA_P_INIT
  298. fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_init() called\n");
  299. # endif
  300. return SHA1_Init(EVP_MD_CTX_md_data(ctx));
  301. }
  302. static int test_sha1_update(EVP_MD_CTX *ctx, const void *data, size_t count)
  303. {
  304. # ifdef TEST_ENG_OPENSSL_SHA_P_UPDATE
  305. fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_update() called\n");
  306. # endif
  307. return SHA1_Update(EVP_MD_CTX_md_data(ctx), data, count);
  308. }
  309. static int test_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
  310. {
  311. # ifdef TEST_ENG_OPENSSL_SHA_P_FINAL
  312. fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_final() called\n");
  313. # endif
  314. return SHA1_Final(md, EVP_MD_CTX_md_data(ctx));
  315. }
  316. static EVP_MD *sha1_md = NULL;
  317. static const EVP_MD *test_sha_md(void)
  318. {
  319. if (sha1_md == NULL) {
  320. EVP_MD *md;
  321. if ((md = EVP_MD_meth_new(NID_sha1, NID_sha1WithRSAEncryption)) == NULL
  322. || !EVP_MD_meth_set_result_size(md, SHA_DIGEST_LENGTH)
  323. || !EVP_MD_meth_set_input_blocksize(md, SHA_CBLOCK)
  324. || !EVP_MD_meth_set_app_datasize(md,
  325. sizeof(EVP_MD *) + sizeof(SHA_CTX))
  326. || !EVP_MD_meth_set_flags(md, 0)
  327. || !EVP_MD_meth_set_init(md, test_sha1_init)
  328. || !EVP_MD_meth_set_update(md, test_sha1_update)
  329. || !EVP_MD_meth_set_final(md, test_sha1_final)) {
  330. EVP_MD_meth_free(md);
  331. md = NULL;
  332. }
  333. sha1_md = md;
  334. }
  335. return sha1_md;
  336. }
  337. static void test_sha_md_destroy(void)
  338. {
  339. EVP_MD_meth_free(sha1_md);
  340. sha1_md = NULL;
  341. }
  342. static int test_digest_nids(const int **nids)
  343. {
  344. static int digest_nids[2] = { 0, 0 };
  345. static int pos = 0;
  346. static int init = 0;
  347. if (!init) {
  348. const EVP_MD *md;
  349. if ((md = test_sha_md()) != NULL)
  350. digest_nids[pos++] = EVP_MD_type(md);
  351. digest_nids[pos] = 0;
  352. init = 1;
  353. }
  354. *nids = digest_nids;
  355. return pos;
  356. }
  357. static int openssl_digests(ENGINE *e, const EVP_MD **digest,
  358. const int **nids, int nid)
  359. {
  360. if (!digest) {
  361. /* We are returning a list of supported nids */
  362. return test_digest_nids(nids);
  363. }
  364. /* We are being asked for a specific digest */
  365. if (nid == NID_sha1)
  366. *digest = test_sha_md();
  367. else {
  368. # ifdef TEST_ENG_OPENSSL_SHA_OTHERS
  369. fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) returning NULL for "
  370. "nid %d\n", nid);
  371. # endif
  372. *digest = NULL;
  373. return 0;
  374. }
  375. return 1;
  376. }
  377. #endif
  378. #ifdef TEST_ENG_OPENSSL_PKEY
  379. static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id,
  380. UI_METHOD *ui_method,
  381. void *callback_data)
  382. {
  383. BIO *in;
  384. EVP_PKEY *key;
  385. fprintf(stderr, "(TEST_ENG_OPENSSL_PKEY)Loading Private key %s\n",
  386. key_id);
  387. in = BIO_new_file(key_id, "r");
  388. if (!in)
  389. return NULL;
  390. key = PEM_read_bio_PrivateKey(in, NULL, 0, NULL);
  391. BIO_free(in);
  392. return key;
  393. }
  394. #endif
  395. #ifdef TEST_ENG_OPENSSL_HMAC
  396. /*
  397. * Experimental HMAC redirection implementation: mainly copied from
  398. * hm_pmeth.c
  399. */
  400. /* HMAC pkey context structure */
  401. typedef struct {
  402. const EVP_MD *md; /* MD for HMAC use */
  403. ASN1_OCTET_STRING ktmp; /* Temp storage for key */
  404. HMAC_CTX *ctx;
  405. } OSSL_HMAC_PKEY_CTX;
  406. static int ossl_hmac_init(EVP_PKEY_CTX *ctx)
  407. {
  408. OSSL_HMAC_PKEY_CTX *hctx;
  409. if ((hctx = OPENSSL_zalloc(sizeof(*hctx))) == NULL) {
  410. ENGINEerr(ENGINE_F_OSSL_HMAC_INIT, ERR_R_MALLOC_FAILURE);
  411. return 0;
  412. }
  413. hctx->ktmp.type = V_ASN1_OCTET_STRING;
  414. hctx->ctx = HMAC_CTX_new();
  415. if (hctx->ctx == NULL) {
  416. OPENSSL_free(hctx);
  417. return 0;
  418. }
  419. EVP_PKEY_CTX_set_data(ctx, hctx);
  420. EVP_PKEY_CTX_set0_keygen_info(ctx, NULL, 0);
  421. # ifdef TEST_ENG_OPENSSL_HMAC_INIT
  422. fprintf(stderr, "(TEST_ENG_OPENSSL_HMAC) ossl_hmac_init() called\n");
  423. # endif
  424. return 1;
  425. }
  426. static void ossl_hmac_cleanup(EVP_PKEY_CTX *ctx);
  427. static int ossl_hmac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
  428. {
  429. OSSL_HMAC_PKEY_CTX *sctx, *dctx;
  430. /* allocate memory for dst->data and a new HMAC_CTX in dst->data->ctx */
  431. if (!ossl_hmac_init(dst))
  432. return 0;
  433. sctx = EVP_PKEY_CTX_get_data(src);
  434. dctx = EVP_PKEY_CTX_get_data(dst);
  435. dctx->md = sctx->md;
  436. if (!HMAC_CTX_copy(dctx->ctx, sctx->ctx))
  437. goto err;
  438. if (sctx->ktmp.data) {
  439. if (!ASN1_OCTET_STRING_set(&dctx->ktmp,
  440. sctx->ktmp.data, sctx->ktmp.length))
  441. goto err;
  442. }
  443. return 1;
  444. err:
  445. /* release HMAC_CTX in dst->data->ctx and memory allocated for dst->data */
  446. ossl_hmac_cleanup(dst);
  447. return 0;
  448. }
  449. static void ossl_hmac_cleanup(EVP_PKEY_CTX *ctx)
  450. {
  451. OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
  452. if (hctx) {
  453. HMAC_CTX_free(hctx->ctx);
  454. OPENSSL_clear_free(hctx->ktmp.data, hctx->ktmp.length);
  455. OPENSSL_free(hctx);
  456. EVP_PKEY_CTX_set_data(ctx, NULL);
  457. }
  458. }
  459. static int ossl_hmac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  460. {
  461. ASN1_OCTET_STRING *hkey = NULL;
  462. OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
  463. if (!hctx->ktmp.data)
  464. return 0;
  465. hkey = ASN1_OCTET_STRING_dup(&hctx->ktmp);
  466. if (!hkey)
  467. return 0;
  468. EVP_PKEY_assign(pkey, EVP_PKEY_HMAC, hkey);
  469. return 1;
  470. }
  471. static int ossl_int_update(EVP_MD_CTX *ctx, const void *data, size_t count)
  472. {
  473. OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(EVP_MD_CTX_pkey_ctx(ctx));
  474. if (!HMAC_Update(hctx->ctx, data, count))
  475. return 0;
  476. return 1;
  477. }
  478. static int ossl_hmac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
  479. {
  480. EVP_MD_CTX_set_flags(mctx, EVP_MD_CTX_FLAG_NO_INIT);
  481. EVP_MD_CTX_set_update_fn(mctx, ossl_int_update);
  482. return 1;
  483. }
  484. static int ossl_hmac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig,
  485. size_t *siglen, EVP_MD_CTX *mctx)
  486. {
  487. unsigned int hlen;
  488. OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
  489. int l = EVP_MD_CTX_size(mctx);
  490. if (l < 0)
  491. return 0;
  492. *siglen = l;
  493. if (!sig)
  494. return 1;
  495. if (!HMAC_Final(hctx->ctx, sig, &hlen))
  496. return 0;
  497. *siglen = (size_t)hlen;
  498. return 1;
  499. }
  500. static int ossl_hmac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  501. {
  502. OSSL_HMAC_PKEY_CTX *hctx = EVP_PKEY_CTX_get_data(ctx);
  503. EVP_PKEY *pk;
  504. ASN1_OCTET_STRING *key;
  505. switch (type) {
  506. case EVP_PKEY_CTRL_SET_MAC_KEY:
  507. if ((!p2 && p1 > 0) || (p1 < -1))
  508. return 0;
  509. if (!ASN1_OCTET_STRING_set(&hctx->ktmp, p2, p1))
  510. return 0;
  511. break;
  512. case EVP_PKEY_CTRL_MD:
  513. hctx->md = p2;
  514. break;
  515. case EVP_PKEY_CTRL_DIGESTINIT:
  516. pk = EVP_PKEY_CTX_get0_pkey(ctx);
  517. key = EVP_PKEY_get0(pk);
  518. if (!HMAC_Init_ex(hctx->ctx, key->data, key->length, hctx->md, NULL))
  519. return 0;
  520. break;
  521. default:
  522. return -2;
  523. }
  524. return 1;
  525. }
  526. static int ossl_hmac_ctrl_str(EVP_PKEY_CTX *ctx,
  527. const char *type, const char *value)
  528. {
  529. if (!value) {
  530. return 0;
  531. }
  532. if (strcmp(type, "key") == 0) {
  533. void *p = (void *)value;
  534. return ossl_hmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, -1, p);
  535. }
  536. if (strcmp(type, "hexkey") == 0) {
  537. unsigned char *key;
  538. int r;
  539. long keylen;
  540. key = OPENSSL_hexstr2buf(value, &keylen);
  541. if (!key)
  542. return 0;
  543. r = ossl_hmac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, keylen, key);
  544. OPENSSL_free(key);
  545. return r;
  546. }
  547. return -2;
  548. }
  549. static EVP_PKEY_METHOD *ossl_hmac_meth;
  550. static int ossl_register_hmac_meth(void)
  551. {
  552. EVP_PKEY_METHOD *meth;
  553. meth = EVP_PKEY_meth_new(EVP_PKEY_HMAC, 0);
  554. if (meth == NULL)
  555. return 0;
  556. EVP_PKEY_meth_set_init(meth, ossl_hmac_init);
  557. EVP_PKEY_meth_set_copy(meth, ossl_hmac_copy);
  558. EVP_PKEY_meth_set_cleanup(meth, ossl_hmac_cleanup);
  559. EVP_PKEY_meth_set_keygen(meth, 0, ossl_hmac_keygen);
  560. EVP_PKEY_meth_set_signctx(meth, ossl_hmac_signctx_init,
  561. ossl_hmac_signctx);
  562. EVP_PKEY_meth_set_ctrl(meth, ossl_hmac_ctrl, ossl_hmac_ctrl_str);
  563. ossl_hmac_meth = meth;
  564. return 1;
  565. }
  566. static int ossl_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
  567. const int **nids, int nid)
  568. {
  569. static int ossl_pkey_nids[] = {
  570. EVP_PKEY_HMAC,
  571. 0
  572. };
  573. if (pmeth == NULL) {
  574. *nids = ossl_pkey_nids;
  575. return 1;
  576. }
  577. if (nid == EVP_PKEY_HMAC) {
  578. *pmeth = ossl_hmac_meth;
  579. return 1;
  580. }
  581. *pmeth = NULL;
  582. return 0;
  583. }
  584. #endif
  585. int openssl_destroy(ENGINE *e)
  586. {
  587. test_sha_md_destroy();
  588. #ifdef TEST_ENG_OPENSSL_RC4
  589. test_r4_cipher_destroy();
  590. test_r4_40_cipher_destroy();
  591. #endif
  592. return 1;
  593. }