|
@@ -12,6 +12,8 @@
|
|
|
#include <openssl/hmac.h>
|
|
|
|
|
|
#include <cryptolink/openssl_common.h>
|
|
|
+#define KEA_HMAC
|
|
|
+#include <cryptolink/openssl_compat.h>
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
@@ -42,10 +44,12 @@ public:
|
|
|
isc_throw(BadKey, "Bad HMAC secret length: 0");
|
|
|
}
|
|
|
|
|
|
- md_.reset(new HMAC_CTX);
|
|
|
- HMAC_CTX_init(md_.get());
|
|
|
+ md_ = HMAC_CTX_new();
|
|
|
+ if (md_ == 0) {
|
|
|
+ isc_throw(LibraryError, "HMAC_CTX_new");
|
|
|
+ }
|
|
|
|
|
|
- if (!HMAC_Init_ex(md_.get(), secret,
|
|
|
+ if (!HMAC_Init_ex(md_, secret,
|
|
|
static_cast<int>(secret_len),
|
|
|
algo, NULL)) {
|
|
|
isc_throw(LibraryError, "HMAC_Init_ex");
|
|
@@ -55,8 +59,9 @@ public:
|
|
|
/// @brief Destructor
|
|
|
~HMACImpl() {
|
|
|
if (md_) {
|
|
|
- HMAC_CTX_cleanup(md_.get());
|
|
|
+ HMAC_CTX_free(md_);
|
|
|
}
|
|
|
+ md_ = 0;
|
|
|
}
|
|
|
|
|
|
/// @brief Returns the HashAlgorithm of the object
|
|
@@ -68,7 +73,7 @@ public:
|
|
|
///
|
|
|
/// @return output size of the digest
|
|
|
size_t getOutputLength() const {
|
|
|
- int size = HMAC_size(md_.get());
|
|
|
+ int size = HMAC_size(md_);
|
|
|
if (size < 0) {
|
|
|
isc_throw(LibraryError, "HMAC_size");
|
|
|
}
|
|
@@ -79,7 +84,7 @@ public:
|
|
|
///
|
|
|
/// See @ref isc::cryptolink::HMAC::update() for details.
|
|
|
void update(const void* data, const size_t len) {
|
|
|
- if (!HMAC_Update(md_.get(),
|
|
|
+ if (!HMAC_Update(md_,
|
|
|
static_cast<const unsigned char*>(data),
|
|
|
len)) {
|
|
|
isc_throw(LibraryError, "HMAC_Update");
|
|
@@ -92,7 +97,7 @@ public:
|
|
|
void sign(isc::util::OutputBuffer& result, size_t len) {
|
|
|
size_t size = getOutputLength();
|
|
|
ossl::SecBuf<unsigned char> digest(size);
|
|
|
- if (!HMAC_Final(md_.get(), &digest[0], NULL)) {
|
|
|
+ if (!HMAC_Final(md_, &digest[0], NULL)) {
|
|
|
isc_throw(LibraryError, "HMAC_Final");
|
|
|
}
|
|
|
if (len > size) {
|
|
@@ -107,7 +112,7 @@ public:
|
|
|
void sign(void* result, size_t len) {
|
|
|
size_t size = getOutputLength();
|
|
|
ossl::SecBuf<unsigned char> digest(size);
|
|
|
- if (!HMAC_Final(md_.get(), &digest[0], NULL)) {
|
|
|
+ if (!HMAC_Final(md_, &digest[0], NULL)) {
|
|
|
isc_throw(LibraryError, "HMAC_Final");
|
|
|
}
|
|
|
if (len > size) {
|
|
@@ -122,7 +127,7 @@ public:
|
|
|
std::vector<uint8_t> sign(size_t len) {
|
|
|
size_t size = getOutputLength();
|
|
|
ossl::SecBuf<unsigned char> digest(size);
|
|
|
- if (!HMAC_Final(md_.get(), &digest[0], NULL)) {
|
|
|
+ if (!HMAC_Final(md_, &digest[0], NULL)) {
|
|
|
isc_throw(LibraryError, "HMAC_Final");
|
|
|
}
|
|
|
if (len < size) {
|
|
@@ -141,17 +146,20 @@ public:
|
|
|
return (false);
|
|
|
}
|
|
|
// Get the digest from a copy of the context
|
|
|
- HMAC_CTX tmp;
|
|
|
- HMAC_CTX_init(&tmp);
|
|
|
- if (!HMAC_CTX_copy(&tmp, md_.get())) {
|
|
|
+ HMAC_CTX* tmp = HMAC_CTX_new();
|
|
|
+ if (tmp == 0) {
|
|
|
+ isc_throw(LibraryError, "HMAC_CTX_new");
|
|
|
+ }
|
|
|
+ if (!HMAC_CTX_copy(tmp, md_)) {
|
|
|
+ HMAC_CTX_free(tmp);
|
|
|
isc_throw(LibraryError, "HMAC_CTX_copy");
|
|
|
}
|
|
|
ossl::SecBuf<unsigned char> digest(size);
|
|
|
- if (!HMAC_Final(&tmp, &digest[0], NULL)) {
|
|
|
- HMAC_CTX_cleanup(&tmp);
|
|
|
+ if (!HMAC_Final(tmp, &digest[0], NULL)) {
|
|
|
+ HMAC_CTX_free(tmp);
|
|
|
isc_throw(LibraryError, "HMAC_Final");
|
|
|
}
|
|
|
- HMAC_CTX_cleanup(&tmp);
|
|
|
+ HMAC_CTX_free(tmp);
|
|
|
if (len > size) {
|
|
|
len = size;
|
|
|
}
|
|
@@ -163,7 +171,7 @@ private:
|
|
|
HashAlgorithm hash_algorithm_;
|
|
|
|
|
|
/// @brief The protected pointer to the OpenSSL HMAC_CTX structure
|
|
|
- boost::scoped_ptr<HMAC_CTX> md_;
|
|
|
+ HMAC_CTX* md_;
|
|
|
};
|
|
|
|
|
|
HMAC::HMAC(const void* secret, size_t secret_length,
|