make code compatible with openssl 3.x
Signed-off-by: Double Sine <xiao_ai_yu@live.cn>
This commit is contained in:
@@ -1,7 +1,14 @@
|
||||
#include "navicat_serial_generator.hpp"
|
||||
#include <fmt/format.h>
|
||||
#include <openssl/rand.h>
|
||||
#include <algorithm>
|
||||
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/rand.h>
|
||||
#include <openssl/provider.h>
|
||||
|
||||
#include "resource_wrapper.hpp"
|
||||
#include "resource_traits/openssl/evp_cipher_ctx.hpp"
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include "base32_rfc4648.hpp"
|
||||
|
||||
#define NKG_CURRENT_SOURCE_FILE() u8".\\navicat-keygen\\navicat_serial_generator.cpp"
|
||||
@@ -121,9 +128,13 @@ namespace nkg {
|
||||
|
||||
void navicat_serial_generator::set_software_version(int ver) {
|
||||
if (11 <= ver && ver < 16) {
|
||||
static_assert(sizeof(m_des_key) == sizeof(s_des_key0));
|
||||
|
||||
m_data[8] = static_cast<std::uint8_t>((ver << 4) | (m_data[8] & 0x0f));
|
||||
memcpy(m_des_key, s_des_key0, sizeof(s_des_key0));
|
||||
} else if (16 <= ver && ver < 32) {
|
||||
static_assert(sizeof(m_des_key) == sizeof(s_des_key1));
|
||||
|
||||
m_data[8] = static_cast<std::uint8_t>(((ver - 16) << 4) | (m_data[8] & 0x0f));
|
||||
memcpy(m_des_key, s_des_key1, sizeof(s_des_key1));
|
||||
} else {
|
||||
@@ -134,9 +145,28 @@ namespace nkg {
|
||||
void navicat_serial_generator::generate() {
|
||||
RAND_bytes(m_data + 2, 3);
|
||||
|
||||
DES_key_schedule schedule;
|
||||
DES_set_key_unchecked(&m_des_key, &schedule);
|
||||
DES_ecb_encrypt(reinterpret_cast<const_DES_cblock*>(m_data + 2), reinterpret_cast<const_DES_cblock*>(m_data + 2), &schedule, DES_ENCRYPT);
|
||||
#if (OPENSSL_VERSION_NUMBER & 0xf0000000) == 0x30000000 // for openssl 3.x.x
|
||||
if (!OSSL_PROVIDER_available(nullptr, "legacy")) {
|
||||
if (OSSL_PROVIDER_load(nullptr, "legacy") == nullptr) {
|
||||
throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"OSSL_PROVIDER_load failed.");
|
||||
}
|
||||
}
|
||||
#else
|
||||
#error "navicat_serial_generator.cpp: Unexpected OpenSSL version."
|
||||
#endif
|
||||
|
||||
resource_wrapper evp_cipher_context{ resource_traits::openssl::evp_cipher_ctx{}, EVP_CIPHER_CTX_new() };
|
||||
if (!evp_cipher_context.is_valid()) {
|
||||
throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_CIPHER_CTX_new failed.");
|
||||
}
|
||||
|
||||
if (EVP_EncryptInit_ex(evp_cipher_context.get(), EVP_des_ecb(), nullptr, m_des_key, nullptr) <= 0) { // return 1 for success and 0 for failure
|
||||
throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_EncryptInit failed.");
|
||||
}
|
||||
|
||||
if (int _; EVP_EncryptUpdate(evp_cipher_context.get(), m_data + 2, &_, m_data + 2, 8) <= 0) { // return 1 for success and 0 for failure
|
||||
throw backend_error(NKG_CURRENT_SOURCE_FILE(), NKG_CURRENT_SOURCE_LINE(), u8"EVP_EncryptUpdate failed.");
|
||||
}
|
||||
|
||||
m_serial_number = base32_rfc4648::encode(m_data, sizeof(m_data));
|
||||
std::transform(m_serial_number.begin(), m_serial_number.end(), m_serial_number.begin(), _replace_confusing_chars);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <openssl/des.h>
|
||||
#include "exception.hpp"
|
||||
|
||||
namespace nkg {
|
||||
@@ -36,13 +35,14 @@ namespace nkg {
|
||||
class navicat_serial_generator {
|
||||
public:
|
||||
class version_error;
|
||||
class backend_error;
|
||||
|
||||
private:
|
||||
static inline const DES_cblock s_des_key0 = { 0x64, 0xAD, 0xF3, 0x2F, 0xAE, 0xF2, 0x1A, 0x27 };
|
||||
static inline const DES_cblock s_des_key1 = { 0xE9, 0x7F, 0xB0, 0x60, 0x77, 0x45, 0x90, 0xAE };
|
||||
static inline const uint8_t s_des_key0[8] = { 0x64, 0xAD, 0xF3, 0x2F, 0xAE, 0xF2, 0x1A, 0x27 };
|
||||
static inline const uint8_t s_des_key1[8] = { 0xE9, 0x7F, 0xB0, 0x60, 0x77, 0x45, 0x90, 0xAE };
|
||||
|
||||
uint8_t m_data[10];
|
||||
DES_cblock m_des_key;
|
||||
uint8_t m_des_key[8];
|
||||
std::string m_serial_number;
|
||||
std::string m_serial_number_formatted;
|
||||
|
||||
@@ -72,4 +72,8 @@ namespace nkg {
|
||||
using ::nkg::exception::exception;
|
||||
};
|
||||
|
||||
class navicat_serial_generator::backend_error : public ::nkg::exception {
|
||||
using ::nkg::exception::exception;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user