Skip to content

Commit b2f71ca

Browse files
jeredfloydbryancall
authored andcommitted
Move HKDF to OpenSSL 3 interfaces (#8909)
* Move HKDF and HMACs to openssl 3 interfaces * clang-format changed files * Revert HMAC change, move EVP_KDF to separate implementation file, add missing QUIC changes * switch back to OPENSSL_IS_OPENSSL3 symbol due to conflict with API_COMPAT symbol * Get digest size from name * Remove extraneous HKDF member variable '_digest' (cherry picked from commit 8ca74ee)
1 parent 7e78553 commit b2f71ca

14 files changed

Lines changed: 140 additions & 28 deletions

File tree

build/crypto.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ int main() {
7373
return 1;
7474
}
7575
])],
76-
[AC_MSG_RESULT(yes) TS_ADDTO(CPPFLAGS, -DOPENSSL_API_COMPAT=10002)], [AC_MSG_RESULT(no)]
76+
[AC_MSG_RESULT(yes) TS_ADDTO(CPPFLAGS, -DOPENSSL_API_COMPAT=10002 -DOPENSSL_IS_OPENSSL3) openssl_is_openssl3=1], [AC_MSG_RESULT(no)]
7777
)
7878
])
7979

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,7 @@ TS_CHECK_CRYPTO_VERSION
12511251

12521252
# Check for OpenSSL Version 3 and add compatiblity define if needed
12531253
TS_CHECK_OPENSSL3
1254+
AM_CONDITIONAL([OPENSSL_IS_OPENSSL3], [test -n "$openssl_is_openssl3"])
12541255

12551256
# Check for openssl ASYNC jobs
12561257
TS_CHECK_CRYPTO_ASYNC

include/tscore/HKDF.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,25 @@
3030
#include <openssl/evp.h>
3131
#endif
3232

33+
#ifdef OPENSSL_IS_OPENSSL3
34+
#include <openssl/core.h>
35+
#endif
36+
3337
class HKDF
3438
{
3539
public:
36-
HKDF(const EVP_MD *digest);
40+
HKDF(const char *digest);
3741
~HKDF();
3842
int extract(uint8_t *dst, size_t *dst_len, const uint8_t *salt, size_t salt_len, const uint8_t *ikm, size_t ikm_len);
3943
int expand(uint8_t *dst, size_t *dst_len, const uint8_t *prk, size_t prk_len, const uint8_t *info, size_t info_len,
4044
uint16_t length);
4145

4246
protected:
43-
const EVP_MD *_digest = nullptr;
44-
EVP_PKEY_CTX *_pctx = nullptr;
47+
#ifdef OPENSSL_IS_OPENSSL3
48+
EVP_KDF_CTX *_kctx = nullptr;
49+
OSSL_PARAM params[5];
50+
#else
51+
EVP_PKEY_CTX *_pctx = nullptr;
52+
const EVP_MD *_digest_md = nullptr;
53+
#endif
4554
};

iocore/net/quic/QUICHKDF.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
class QUICHKDF : public HKDF
2929
{
3030
public:
31-
QUICHKDF(const EVP_MD *digest) : HKDF(digest) {}
31+
QUICHKDF(const char *digest) : HKDF(digest) {}
3232
int expand(uint8_t *dst, size_t *dst_len, const uint8_t *secret, size_t secret_len, const char *label, size_t label_len,
3333
uint16_t length);
3434
};

iocore/net/quic/QUICKeyGenerator.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ void
4949
QUICKeyGenerator::generate(QUICVersion version, uint8_t *hp_key, uint8_t *pp_key, uint8_t *iv, size_t *iv_len, QUICConnectionId cid)
5050
{
5151
const EVP_CIPHER *cipher = this->_get_cipher_for_initial();
52-
const EVP_MD *md = EVP_sha256();
52+
const char *md = "SHA256";
5353
uint8_t secret[512];
5454
size_t secret_len = sizeof(secret);
5555
QUICHKDF hkdf(md);
5656

5757
switch (this->_ctx) {
5858
case Context::CLIENT:
5959
this->_generate_initial_secret(version, secret, &secret_len, hkdf, cid, LABEL_FOR_CLIENT_INITIAL_SECRET.data(),
60-
LABEL_FOR_CLIENT_INITIAL_SECRET.length(), EVP_MD_size(md));
60+
LABEL_FOR_CLIENT_INITIAL_SECRET.length(), EVP_MD_size(EVP_get_digestbyname(md)));
6161
if (is_debug_tag_set("vv_quic_crypto")) {
6262
uint8_t print_buf[1024 + 1];
6363
QUICDebug::to_hex(print_buf, secret, secret_len);
@@ -67,7 +67,7 @@ QUICKeyGenerator::generate(QUICVersion version, uint8_t *hp_key, uint8_t *pp_key
6767
break;
6868
case Context::SERVER:
6969
this->_generate_initial_secret(version, secret, &secret_len, hkdf, cid, LABEL_FOR_SERVER_INITIAL_SECRET.data(),
70-
LABEL_FOR_SERVER_INITIAL_SECRET.length(), EVP_MD_size(md));
70+
LABEL_FOR_SERVER_INITIAL_SECRET.length(), EVP_MD_size(EVP_get_digestbyname(md)));
7171
if (is_debug_tag_set("vv_quic_crypto")) {
7272
uint8_t print_buf[1024 + 1];
7373
QUICDebug::to_hex(print_buf, secret, secret_len);

iocore/net/quic/QUICTLS.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class QUICTLS : public QUICHandshakeProtocol
9090
private:
9191
QUICKeyGenerator _keygen_for_client = QUICKeyGenerator(QUICKeyGenerator::Context::CLIENT);
9292
QUICKeyGenerator _keygen_for_server = QUICKeyGenerator(QUICKeyGenerator::Context::SERVER);
93-
const EVP_MD *_get_handshake_digest() const;
93+
const char *_get_handshake_digest() const;
9494

9595
int _read_early_data();
9696
int _write_early_data();

iocore/net/quic/QUICTLS_boringssl.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,15 +352,15 @@ QUICTLS::_pass_quic_data_to_ssl_impl(const QUICHandshakeMsgs &in)
352352
}
353353
}
354354

355-
const EVP_MD *
355+
const char *
356356
QUICTLS::_get_handshake_digest() const
357357
{
358358
switch (SSL_CIPHER_get_id(SSL_get_current_cipher(this->_ssl))) {
359359
case TLS1_CK_AES_128_GCM_SHA256:
360360
case TLS1_CK_CHACHA20_POLY1305_SHA256:
361-
return EVP_sha256();
361+
return "SHA256";
362362
case TLS1_CK_AES_256_GCM_SHA384:
363-
return EVP_sha384();
363+
return "SHA384";
364364
default:
365365
ink_assert(false);
366366
return nullptr;

iocore/net/quic/QUICTLS_openssl.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,17 +316,17 @@ QUICTLS::_pass_quic_data_to_ssl_impl(const QUICHandshakeMsgs &in)
316316
}
317317
}
318318

319-
const EVP_MD *
319+
const char *
320320
QUICTLS::_get_handshake_digest() const
321321
{
322322
switch (SSL_CIPHER_get_id(SSL_get_current_cipher(this->_ssl))) {
323323
case TLS1_3_CK_AES_128_GCM_SHA256:
324324
case TLS1_3_CK_CHACHA20_POLY1305_SHA256:
325325
case TLS1_3_CK_AES_128_CCM_SHA256:
326326
case TLS1_3_CK_AES_128_CCM_8_SHA256:
327-
return EVP_sha256();
327+
return "SHA256";
328328
case TLS1_3_CK_AES_256_GCM_SHA384:
329-
return EVP_sha384();
329+
return "SHA384";
330330
default:
331331
ink_assert(false);
332332
return nullptr;

plugins/experimental/access_control/unit_tests/test_utils.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* @brief Unit tests for functions used in utils.cc
2222
*/
2323

24+
#include <openssl/opensslv.h>
2425
#include <catch.hpp> /* catch unit-test framework */
2526
#include "../utils.h"
2627
#include "../common.h"
@@ -253,6 +254,13 @@ TEST_CASE("HMAC Digest: test various supported/unsupported types", "[MAC][access
253254
digests.push_back("ccf3230972bcf229fb3b16741495c74a72bbdd14");
254255
#endif
255256

257+
#ifdef OPENSSL_IS_OPENSSL3 // MD4, RIPEMD160 are deprecated in OpenSSL 3
258+
types.pop_front();
259+
digests.pop_front();
260+
types.pop_back();
261+
digests.pop_back();
262+
#endif
263+
256264
StringList::iterator digestIter = digests.begin();
257265
for (String digestType : types) {
258266
size_t outLen = cryptoMessageDigestGet(digestType.c_str(), data.c_str(), data.length(), key.c_str(), key.length(), out,

src/tscore/HKDF_boringssl.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,24 @@
2222
*/
2323
#include "tscore/HKDF.h"
2424
#include <openssl/hkdf.h>
25+
#include <openssl/digest.h>
2526

26-
HKDF::HKDF(const EVP_MD *digest) : _digest(digest) {}
27+
HKDF::HKDF(const char *digest)
28+
{
29+
this->_digest_md = EVP_get_digestbyname(digest);
30+
}
2731
HKDF::~HKDF() {}
2832

2933
int
3034
HKDF::extract(uint8_t *dst, size_t *dst_len, const uint8_t *salt, size_t salt_len, const uint8_t *ikm, size_t ikm_len)
3135
{
32-
return HKDF_extract(dst, dst_len, this->_digest, ikm, ikm_len, salt, salt_len);
36+
return HKDF_extract(dst, dst_len, this->_digest_md, ikm, ikm_len, salt, salt_len);
3337
}
3438

3539
int
3640
HKDF::expand(uint8_t *dst, size_t *dst_len, const uint8_t *prk, size_t prk_len, const uint8_t *info, size_t info_len,
3741
uint16_t length)
3842
{
3943
*dst_len = length;
40-
return HKDF_expand(dst, length, this->_digest, prk, prk_len, info, info_len);
44+
return HKDF_expand(dst, length, this->_digest_md, prk, prk_len, info, info_len);
4145
}

0 commit comments

Comments
 (0)