提交 c559b958 authored 作者: Nazar Mokrynskyi's avatar Nazar Mokrynskyi

Update to OpenSSL 3.0 API

上级 30ed8d41
......@@ -2,7 +2,7 @@
#define MS_UTILS_HPP
#include "common.hpp"
#include <openssl/hmac.h>
#include <openssl/evp.h>
#include <cmath>
#include <cstring> // std::memcmp(), std::memcpy()
#include <nlohmann/json.hpp>
......@@ -255,7 +255,8 @@ namespace Utils
private:
thread_local static uint32_t seed;
thread_local static HMAC_CTX* hmacSha1Ctx;
thread_local static EVP_MAC* mac;
thread_local static EVP_MAC_CTX* hmacSha1Ctx;
thread_local static uint8_t hmacSha1Buffer[];
static const uint32_t crc32Table[256];
};
......
......@@ -6,11 +6,8 @@
#include "MediaSoupErrors.hpp"
#include "Settings.hpp"
#include "Utils.hpp"
#include <openssl/asn1.h>
#include <openssl/bn.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <uv.h>
#include <cstdio> // std::sprintf(), std::fopen()
#include <cstring> // std::memcpy(), std::strcmp()
......@@ -157,56 +154,20 @@ namespace RTC
MS_TRACE();
int ret{ 0 };
EC_KEY* ecKey{ nullptr };
X509_NAME* certName{ nullptr };
std::string subject =
std::string("mediasoup") + std::to_string(Utils::Crypto::GetRandomUInt(100000, 999999));
// Create key with curve.
ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
if (!ecKey)
{
LOG_OPENSSL_ERROR("EC_KEY_new_by_curve_name() failed");
goto error;
}
EC_KEY_set_asn1_flag(ecKey, OPENSSL_EC_NAMED_CURVE);
// NOTE: This can take some time.
ret = EC_KEY_generate_key(ecKey);
if (ret == 0)
{
LOG_OPENSSL_ERROR("EC_KEY_generate_key() failed");
goto error;
}
// Create a private key object.
DtlsTransport::privateKey = EVP_PKEY_new();
DtlsTransport::privateKey = EVP_EC_gen(SN_X9_62_prime256v1);
if (!DtlsTransport::privateKey)
{
LOG_OPENSSL_ERROR("EVP_PKEY_new() failed");
goto error;
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast)
ret = EVP_PKEY_assign_EC_KEY(DtlsTransport::privateKey, ecKey);
if (ret == 0)
{
LOG_OPENSSL_ERROR("EVP_PKEY_assign_EC_KEY() failed");
LOG_OPENSSL_ERROR("EVP_EC_gen() failed");
goto error;
}
// The EC key now belongs to the private key, so don't clean it up separately.
ecKey = nullptr;
// Create the X509 certificate.
DtlsTransport::certificate = X509_new();
......@@ -278,11 +239,8 @@ namespace RTC
error:
if (ecKey)
EC_KEY_free(ecKey);
if (DtlsTransport::privateKey)
EVP_PKEY_free(DtlsTransport::privateKey); // NOTE: This also frees the EC key.
EVP_PKEY_free(DtlsTransport::privateKey);
if (DtlsTransport::certificate)
X509_free(DtlsTransport::certificate);
......
......@@ -10,7 +10,8 @@ namespace Utils
/* Static variables. */
thread_local uint32_t Crypto::seed;
thread_local HMAC_CTX* Crypto::hmacSha1Ctx{ nullptr };
thread_local EVP_MAC* Crypto::mac{ nullptr };
thread_local EVP_MAC_CTX* Crypto::hmacSha1Ctx{ nullptr };
thread_local uint8_t Crypto::hmacSha1Buffer[SHA_DIGEST_LENGTH];
// clang-format off
const uint32_t Crypto::crc32Table[] =
......@@ -61,7 +62,8 @@ namespace Utils
Crypto::seed = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(std::addressof(Crypto::seed)));
// Create an OpenSSL HMAC_CTX context for HMAC SHA1 calculation.
Crypto::hmacSha1Ctx = HMAC_CTX_new();
Crypto::mac = EVP_MAC_fetch(nullptr, "HMAC", nullptr);
Crypto::hmacSha1Ctx = EVP_MAC_CTX_new(mac);
}
void Crypto::ClassDestroy()
......@@ -69,7 +71,10 @@ namespace Utils
MS_TRACE();
if (Crypto::hmacSha1Ctx != nullptr)
HMAC_CTX_free(Crypto::hmacSha1Ctx);
EVP_MAC_CTX_free(Crypto::hmacSha1Ctx);
if (Crypto::mac != nullptr)
EVP_MAC_free(Crypto::mac);
}
const uint8_t* Crypto::GetHmacSha1(const std::string& key, const uint8_t* data, size_t len)
......@@ -78,26 +83,29 @@ namespace Utils
int ret;
ret = HMAC_Init_ex(Crypto::hmacSha1Ctx, key.c_str(), key.length(), EVP_sha1(), nullptr);
OSSL_PARAM sha1[] = { { "digest", OSSL_PARAM_UTF8_STRING, (void*)"sha1", 4, 0 }, OSSL_PARAM_END };
ret = EVP_MAC_init(
Crypto::hmacSha1Ctx, reinterpret_cast<const unsigned char*>(key.c_str()), key.length(), sha1);
MS_ASSERT(ret == 1, "OpenSSL HMAC_Init_ex() failed with key '%s'", key.c_str());
MS_ASSERT(ret == 1, "OpenSSL EVP_MAC_init() failed with key '%s'", key.c_str());
ret = HMAC_Update(Crypto::hmacSha1Ctx, data, static_cast<int>(len));
ret = EVP_MAC_update(Crypto::hmacSha1Ctx, data, len);
MS_ASSERT(
ret == 1,
"OpenSSL HMAC_Update() failed with key '%s' and data length %zu bytes",
"OpenSSL EVP_MAC_update() failed with key '%s' and data length %zu bytes",
key.c_str(),
len);
uint32_t resultLen;
size_t resultLen;
ret = HMAC_Final(Crypto::hmacSha1Ctx, (uint8_t*)Crypto::hmacSha1Buffer, &resultLen);
ret = EVP_MAC_final(Crypto::hmacSha1Ctx, Crypto::hmacSha1Buffer, &resultLen, SHA_DIGEST_LENGTH);
MS_ASSERT(
ret == 1, "OpenSSL HMAC_Final() failed with key '%s' and data length %zu bytes", key.c_str(), len);
MS_ASSERT(
resultLen == SHA_DIGEST_LENGTH, "OpenSSL HMAC_Final() resultLen is %u instead of 20", resultLen);
resultLen == SHA_DIGEST_LENGTH, "OpenSSL HMAC_Final() resultLen is %zu instead of 20", resultLen);
return Crypto::hmacSha1Buffer;
}
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论