eng_openssl.c 18 KB

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