2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-09-05 00:55:24 +00:00

Refactor the OpenSSL HMAC usage to use newer APIs

OpenSSL 3 deprecates the HMAC* family and associated APIs.

Rewrite portions of OpenSSL library usage code to use a newer
set of HMAC APIs.
This commit is contained in:
Aram Sargsyan
2021-09-01 19:07:58 +00:00
parent 2a6febd5d2
commit 15cb706f22
5 changed files with 37 additions and 75 deletions

View File

@@ -9,7 +9,7 @@
* information regarding copyright ownership.
*/
#include <openssl/hmac.h>
#include <openssl/evp.h>
#include <openssl/opensslv.h>
#include <isc/assertions.h>
@@ -24,9 +24,9 @@
isc_hmac_t *
isc_hmac_new(void) {
HMAC_CTX *hmac = HMAC_CTX_new();
EVP_MD_CTX *hmac = EVP_MD_CTX_new();
RUNTIME_CHECK(hmac != NULL);
return ((struct hmac *)hmac);
return ((isc_hmac_t *)hmac);
}
void
@@ -35,23 +35,33 @@ isc_hmac_free(isc_hmac_t *hmac) {
return;
}
HMAC_CTX_free(hmac);
EVP_MD_CTX_free((EVP_MD_CTX *)hmac);
}
isc_result_t
isc_hmac_init(isc_hmac_t *hmac, const void *key, size_t keylen,
isc_hmac_init(isc_hmac_t *hmac, const void *key, const size_t keylen,
const isc_md_type_t *md_type) {
EVP_PKEY *pkey;
REQUIRE(hmac != NULL);
REQUIRE(key != NULL);
REQUIRE(keylen <= INT_MAX);
if (md_type == NULL) {
return (ISC_R_NOTIMPLEMENTED);
}
if (HMAC_Init_ex(hmac, key, keylen, md_type, NULL) != 1) {
pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL, key, keylen);
if (pkey == NULL) {
return (ISC_R_CRYPTOFAILURE);
}
if (EVP_DigestSignInit(hmac, NULL, md_type, NULL, pkey) != 1) {
return (ISC_R_CRYPTOFAILURE);
}
EVP_PKEY_free(pkey);
return (ISC_R_SUCCESS);
}
@@ -59,7 +69,7 @@ isc_result_t
isc_hmac_reset(isc_hmac_t *hmac) {
REQUIRE(hmac != NULL);
if (HMAC_CTX_reset(hmac) != 1) {
if (EVP_MD_CTX_reset(hmac) != 1) {
return (ISC_R_CRYPTOFAILURE);
}
@@ -74,7 +84,7 @@ isc_hmac_update(isc_hmac_t *hmac, const unsigned char *buf, const size_t len) {
return (ISC_R_SUCCESS);
}
if (HMAC_Update(hmac, buf, len) != 1) {
if (EVP_DigestSignUpdate(hmac, buf, len) != 1) {
return (ISC_R_CRYPTOFAILURE);
}
@@ -84,13 +94,19 @@ isc_hmac_update(isc_hmac_t *hmac, const unsigned char *buf, const size_t len) {
isc_result_t
isc_hmac_final(isc_hmac_t *hmac, unsigned char *digest,
unsigned int *digestlen) {
size_t len = 0;
REQUIRE(hmac != NULL);
REQUIRE(digest != NULL);
if (HMAC_Final(hmac, digest, digestlen) != 1) {
if (EVP_DigestSignFinal(hmac, digest, &len) != 1) {
return (ISC_R_CRYPTOFAILURE);
}
if (digestlen != NULL) {
*digestlen = (unsigned int)len;
}
return (ISC_R_SUCCESS);
}
@@ -98,25 +114,25 @@ const isc_md_type_t *
isc_hmac_get_md_type(isc_hmac_t *hmac) {
REQUIRE(hmac != NULL);
return (HMAC_CTX_get_md(hmac));
return (EVP_MD_CTX_get0_md(hmac));
}
size_t
isc_hmac_get_size(isc_hmac_t *hmac) {
REQUIRE(hmac != NULL);
return ((size_t)EVP_MD_size(HMAC_CTX_get_md(hmac)));
return ((size_t)EVP_MD_CTX_size(hmac));
}
int
isc_hmac_get_block_size(isc_hmac_t *hmac) {
REQUIRE(hmac != NULL);
return (EVP_MD_block_size(HMAC_CTX_get_md(hmac)));
return (EVP_MD_CTX_block_size(hmac));
}
isc_result_t
isc_hmac(const isc_md_type_t *type, const void *key, const int keylen,
isc_hmac(const isc_md_type_t *type, const void *key, const size_t keylen,
const unsigned char *buf, const size_t len, unsigned char *digest,
unsigned int *digestlen) {
isc_result_t res;