Added backward compatibility support.

This commit is contained in:
XMRig 2022-04-15 14:13:45 +07:00
parent 6106bf30de
commit dfc3b4632a
No known key found for this signature in database
GPG key ID: 446A53638BE94409
4 changed files with 51 additions and 20 deletions

View file

@ -1,6 +1,6 @@
/* XMRig /* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2022 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2022 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -17,6 +17,8 @@
*/ */
#include "base/net/tls/TlsGen.h" #include "base/net/tls/TlsGen.h"
#include "base/io/log/Log.h"
#include "version.h"
#include <openssl/ssl.h> #include <openssl/ssl.h>
@ -31,6 +33,9 @@ namespace xmrig {
static const char *kLocalhost = "localhost"; static const char *kLocalhost = "localhost";
extern const char *tls_tag();
static EVP_PKEY *generate_pkey() static EVP_PKEY *generate_pkey()
{ {
auto pkey = EVP_PKEY_new(); auto pkey = EVP_PKEY_new();
@ -56,7 +61,7 @@ static EVP_PKEY *generate_pkey()
} }
bool isFileExist(const char *fileName) static bool isFileExist(const char *fileName)
{ {
std::ifstream in(fileName); std::ifstream in(fileName);
@ -74,28 +79,40 @@ xmrig::TlsGen::~TlsGen()
} }
const char *xmrig::TlsGen::cn() const
{
return m_cn.isEmpty() ? kLocalhost : m_cn.data();
}
void xmrig::TlsGen::generate(const char *commonName) void xmrig::TlsGen::generate(const char *commonName)
{ {
if (isFileExist(m_cert) && isFileExist(m_certKey)) { if (isFileExist(m_cert) && isFileExist(m_certKey)) {
return; return;
} }
m_pkey = generate_pkey(); m_cn = commonName;
m_pkey = generate_pkey();
if (!m_pkey) { if (!m_pkey) {
throw std::runtime_error("RSA key generation failed."); throw std::runtime_error("RSA key generation failed.");
} }
if (!generate_x509(commonName == nullptr || strlen(commonName) == 0 ? kLocalhost : commonName)) { if (!generate_x509()) {
throw std::runtime_error("x509 certificate generation failed."); throw std::runtime_error("x509 certificate generation failed.");
} }
if (!write()) { if (!write()) {
throw std::runtime_error("unable to write certificate to disk."); throw std::runtime_error("unable to write certificate to disk.");
} }
# if defined(XMRIG_BASE_VERSION)
LOG_NOTICE("%s " MAGENTA_BOLD("generated") WHITE_BOLD(" \"%s/%s\" ") "CN=" WHITE_BOLD("\"%s\""), tls_tag(), m_cert.data(), m_certKey.data(), cn());
# endif
} }
bool xmrig::TlsGen::generate_x509(const char *commonName) bool xmrig::TlsGen::generate_x509()
{ {
m_x509 = X509_new(); m_x509 = X509_new();
if (!m_x509) { if (!m_x509) {
@ -111,7 +128,7 @@ bool xmrig::TlsGen::generate_x509(const char *commonName)
X509_gmtime_adj(X509_get_notAfter(m_x509), 315360000L); X509_gmtime_adj(X509_get_notAfter(m_x509), 315360000L);
auto name = X509_get_subject_name(m_x509); auto name = X509_get_subject_name(m_x509);
X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, reinterpret_cast<const uint8_t *>(commonName), -1, -1, 0); X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, reinterpret_cast<const uint8_t *>(cn()), -1, -1, 0);
X509_set_issuer_name(m_x509, name); X509_set_issuer_name(m_x509, name);

View file

@ -1,6 +1,6 @@
/* XMRig /* XMRig
* Copyright (c) 2018-2021 SChernykh <https://github.com/SChernykh> * Copyright (c) 2018-2022 SChernykh <https://github.com/SChernykh>
* Copyright (c) 2016-2021 XMRig <https://github.com/xmrig>, <support@xmrig.com> * Copyright (c) 2016-2022 XMRig <https://github.com/xmrig>, <support@xmrig.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -42,20 +42,22 @@ public:
inline const String &cert() const { return m_cert; } inline const String &cert() const { return m_cert; }
inline const String &certKey() const { return m_certKey; } inline const String &certKey() const { return m_certKey; }
const char *cn() const;
void generate(const char *commonName = nullptr); void generate(const char *commonName = nullptr);
private: private:
bool generate_x509(const char *commonName); bool generate_x509();
bool write(); bool write();
const String m_cert; const String m_cert;
const String m_certKey; const String m_certKey;
EVP_PKEY *m_pkey = nullptr; EVP_PKEY *m_pkey = nullptr;
String m_cn;
X509 *m_x509 = nullptr; X509 *m_x509 = nullptr;
}; };
} /* namespace xmrig */ } // namespace xmrig
#endif /* XMRIG_TLSGEN_H */ #endif // XMRIG_TLSGEN_H

View file

@ -86,7 +86,7 @@ bool xmrig::WalletAddress::decode(const char *address, size_t size)
} }
uint64_t hi; uint64_t hi;
const uint64_t tmp = num + __umul128(order, static_cast<uint64_t>(digit), &hi); const uint64_t tmp = num + xmrig_umul128(order, static_cast<uint64_t>(digit), &hi);
if ((tmp < num) || hi) { if ((tmp < num) || hi) {
return false; return false;
} }

View file

@ -21,27 +21,31 @@
* *
*/ */
#pragma once #ifndef XMRIG_UMUL128_H
#define XMRIG_UMUL128_H
#include "version.h"
#include <cstdint> #include <cstdint>
#ifdef XMRIG_64_BIT #ifdef XMRIG_64_BIT
# ifdef _MSC_VER # if defined(_MSC_VER)
# include <intrin.h> # include <intrin.h>
# pragma intrinsic(_umul128) # pragma intrinsic(_umul128)
# define __umul128 _umul128 # define xmrig_umul128 _umul128
# elif defined __GNUC__ # elif defined(__GNUC__)
static inline uint64_t _umul128(uint64_t a, uint64_t b, uint64_t* hi) static inline uint64_t xmrig_umul128(uint64_t a, uint64_t b, uint64_t* hi)
{ {
unsigned __int128 r = (unsigned __int128) a * (unsigned __int128) b; unsigned __int128 r = (unsigned __int128) a * (unsigned __int128) b;
*hi = r >> 64; *hi = r >> 64;
return (uint64_t) r; return (uint64_t) r;
} }
# define __umul128 _umul128
# endif # endif
#else #else
static inline uint64_t __umul128(uint64_t multiplier, uint64_t multiplicand, uint64_t *product_hi) { static inline uint64_t xmrig_umul128(uint64_t multiplier, uint64_t multiplicand, uint64_t *product_hi) {
// multiplier = ab = a * 2^32 + b // multiplier = ab = a * 2^32 + b
// multiplicand = cd = c * 2^32 + d // multiplicand = cd = c * 2^32 + d
// ab * cd = a * c * 2^64 + (a * d + b * c) * 2^32 + b * d // ab * cd = a * c * 2^64 + (a * d + b * c) * 2^32 + b * d
@ -66,3 +70,11 @@ static inline uint64_t __umul128(uint64_t multiplier, uint64_t multiplicand, uin
return product_lo; return product_lo;
} }
#endif #endif
#if !defined(XMRIG_BASE_VERSION)
# define __umul128 xmrig_umul128
#endif
#endif // XMRIG_UMUL128_H