Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions build/crypto.m4
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,37 @@ AC_DEFUN([TS_CHECK_CRYPTO_DH_GET_2048_256], [
AC_SUBST(use_dh_get_2048_256)
])

AC_DEFUN([TS_CHECK_CRYPTO_HKDF], [
enable_hkdf=no
_hkdf_saved_LIBS=$LIBS
TS_ADDTO(LIBS, [$OPENSSL_LIBS])
AC_MSG_CHECKING([for EVP_PKEY_CTX_hkdf_mode])
AC_LINK_IFELSE(
[
AC_LANG_PROGRAM([[
#include <openssl/kdf.h>
]],
[[
#ifndef EVP_PKEY_CTX_hkdf_mode
# error no EVP_PKEY_CTX_hkdf_mode support
#endif
]])
],
[
AC_MSG_RESULT([yes])
enable_hkdf=yes
],
[
AC_MSG_RESULT([no])
])
AC_CHECK_FUNC(HKDF_extract, [
enable_hkdf=yes
], [])
LIBS=$_hkdf_saved_LIBS
TS_ARG_ENABLE_VAR([use], [hkdf])
AC_SUBST(use_hkdf)
])

dnl
dnl Since OpenSSL 1.1.0
dnl
Expand Down
16 changes: 16 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,10 @@ TS_CHECK_CRYPTO_SET_RBIO
# Check for DH_get_2048_256
TS_CHECK_CRYPTO_DH_GET_2048_256

# Check for HKDF support
TS_CHECK_CRYPTO_HKDF
AM_CONDITIONAL([HAS_HKDF], [test "x$enable_hkdf" = "xyes"])

# Check for OCSP
TS_CHECK_CRYPTO_OCSP

Expand Down Expand Up @@ -1231,6 +1235,18 @@ AC_CHECK_FUNC([EVP_MD_CTX_reset], [],
AC_CHECK_FUNC([EVP_MD_CTX_free], [],
[AC_DEFINE([EVP_MD_CTX_free], [EVP_MD_CTX_destroy], [Renamed in OpenSSL 1.1])])

AC_MSG_CHECKING([for OpenSSL is BoringSSL])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <openssl/base.h>]],
[[
#ifndef OPENSSL_IS_BORINGSSL
# error not boringssl
#endif
]])
],
[AC_MSG_RESULT([yes]); openssl_is_boringssl=1],
[AC_MSG_RESULT([no])])
AM_CONDITIONAL([OPENSSL_IS_BORINGSSL], [test -n "$openssl_is_boringssl"])

LIBS="$saved_LIBS"

#
Expand Down
45 changes: 45 additions & 0 deletions include/tscore/HKDF.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/** @file

HKDF utility

@section license License

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#pragma once

#ifdef OPENSSL_IS_BORINGSSL
#include <openssl/digest.h>
#include <openssl/cipher.h>
#else
#include <openssl/evp.h>
#endif

class HKDF
{
public:
HKDF(const EVP_MD *digest);
~HKDF();
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);
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,
uint16_t length);

protected:
const EVP_MD *_digest = nullptr;
EVP_PKEY_CTX *_pctx = nullptr;
};
41 changes: 41 additions & 0 deletions src/tscore/HKDF_boringssl.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/** @file
*
* HKDF utility (BoringSSL version)
*
* @section license License
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "tscore/HKDF.h"
#include <openssl/hkdf.h>

HKDF::HKDF(const EVP_MD *digest) : _digest(digest) {}
HKDF::~HKDF() {}

int
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)
{
return HKDF_extract(dst, dst_len, this->_digest, ikm, ikm_len, salt, salt_len);
}

int
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,
uint16_t length)
{
*dst_len = length;
return HKDF_expand(dst, length, this->_digest, prk, prk_len, info, info_len);
}
102 changes: 102 additions & 0 deletions src/tscore/HKDF_openssl.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/** @file
*
* HKDF utility (OpenSSL version)
*
* @section license License
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "tscore/HKDF.h"
#include <openssl/kdf.h>

HKDF::HKDF(const EVP_MD *digest) : _digest(digest)
{
// XXX We cannot reuse pctx now due to a bug in OpenSSL
// this->_pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, nullptr);
}

HKDF::~HKDF()
{
EVP_PKEY_CTX_free(this->_pctx);
this->_pctx = nullptr;
}

int
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)
{
// XXX See comments in the constructor
if (this->_pctx) {
EVP_PKEY_CTX_free(this->_pctx);
this->_pctx = nullptr;
}
this->_pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, nullptr);

if (EVP_PKEY_derive_init(this->_pctx) != 1) {
return -1;
}
if (EVP_PKEY_CTX_hkdf_mode(this->_pctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY) != 1) {
return -2;
}
if (EVP_PKEY_CTX_set_hkdf_md(this->_pctx, this->_digest) != 1) {
return -3;
}
if (EVP_PKEY_CTX_set1_hkdf_salt(this->_pctx, salt, salt_len) != 1) {
return -4;
}
if (EVP_PKEY_CTX_set1_hkdf_key(this->_pctx, ikm, ikm_len) != 1) {
return -5;
}
if (EVP_PKEY_derive(this->_pctx, dst, dst_len) != 1) {
return -6;
}

return 1;
}

int
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,
uint16_t length)
{
// XXX See comments in the constructor
if (this->_pctx) {
EVP_PKEY_CTX_free(this->_pctx);
this->_pctx = nullptr;
}
this->_pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, nullptr);

if (EVP_PKEY_derive_init(this->_pctx) != 1) {
return -1;
}
if (EVP_PKEY_CTX_hkdf_mode(this->_pctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY) != 1) {
return -2;
}
if (EVP_PKEY_CTX_set_hkdf_md(this->_pctx, this->_digest) != 1) {
return -3;
}
if (EVP_PKEY_CTX_set1_hkdf_key(this->_pctx, prk, prk_len) != 1) {
return -5;
}
if (EVP_PKEY_CTX_add1_hkdf_info(this->_pctx, info, info_len) != 1) {
return -6;
}
*dst_len = length;
if (EVP_PKEY_derive(this->_pctx, dst, dst_len) != 1) {
return -7;
}

return 1;
}
18 changes: 17 additions & 1 deletion src/tscore/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ libtscore_la_SOURCES = \
Version.cc \
X509HostnameValidator.cc

if HAS_HKDF
if OPENSSL_IS_BORINGSSL
HKDF_impl = HKDF_boringssl.cc
else
HKDF_impl = HKDF_openssl.cc
endif
libtscore_la_SOURCES += \
HKDF.h \
$(HKDF_impl)
endif

BufferWriterFormat.o : AM_CPPFLAGS += -Wno-char-subscripts

#test_UNUSED_SOURCES = \
Expand Down Expand Up @@ -150,7 +161,7 @@ test_tscore_CPPFLAGS = $(AM_CPPFLAGS)\
-I$(abs_top_srcdir)/tests/include

test_tscore_CXXFLAGS = -Wno-array-bounds $(AM_CXXFLAGS)
test_tscore_LDADD = libtscore.la $(top_builddir)/src/tscpp/util/libtscpputil.la $(top_builddir)/iocore/eventsystem/libinkevent.a
test_tscore_LDADD = libtscore.la $(top_builddir)/src/tscpp/util/libtscpputil.la $(top_builddir)/iocore/eventsystem/libinkevent.a @OPENSSL_LIBS@
test_tscore_SOURCES = \
unit_tests/unit_test_main.cc \
unit_tests/test_AcidPtr.cc \
Expand All @@ -175,6 +186,11 @@ test_tscore_SOURCES = \
unit_tests/test_scoped_resource.cc \
unit_tests/test_ts_file.cc

if HAS_HKDF
test_tscore_SOURCES += \
unit_tests/test_HKDF.cc
endif

CompileParseRules_SOURCES = CompileParseRules.cc

clean-local:
Expand Down
Loading