Merge pull request #48 from Nuitari/master

OpenSSL 1.1.x fix and Boost 1.70+
This commit is contained in:
cygnusxi 2020-05-17 23:02:11 -05:00 committed by GitHub
commit 2ca5f19d05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 317 additions and 70 deletions

View File

@ -48,38 +48,64 @@ public:
/** C++ wrapper for BIGNUM (OpenSSL bignum) */ /** C++ wrapper for BIGNUM (OpenSSL bignum) */
class CBigNum : public BIGNUM class CBigNum
{ {
protected:
BIGNUM *bn;
void init()
{
bn = BN_new();
}
public: public:
CBigNum() CBigNum()
{ {
BN_init(this); init();
} }
CBigNum(const CBigNum& b) CBigNum(const CBigNum& b)
{ {
BN_init(this); init();
if (!BN_copy(this, &b)) if (!BN_copy(bn, &b))
{ {
BN_clear_free(this); BN_clear_free(bn);
throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_copy failed"); throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_copy failed");
} }
} }
CBigNum& operator=(const CBigNum& b) CBigNum& operator=(const CBigNum& b)
{ {
if (!BN_copy(this, &b)) if (!BN_copy(bn, &b))
throw bignum_error("CBigNum::operator= : BN_copy failed"); throw bignum_error("CBigNum::operator= : BN_copy failed");
return (*this); return (*this);
} }
~CBigNum() ~CBigNum()
{ {
BN_clear_free(this); BN_clear_free(bn);
} }
BIGNUM *operator &() const
{
return bn;
}
CBigNum(signed char n) { init(); if (n >= 0) setulong(n); else setint64(n); }
CBigNum(short n) { init(); if (n >= 0) setulong(n); else setint64(n); }
CBigNum(int n) { init(); if (n >= 0) setulong(n); else setint64(n); }
// CBigNum(long n) { init(); if (n >= 0) setulong(n); else setint64(n); }
CBigNum(int64_t n) { init(); setint64(n); }
CBigNum(unsigned char n) { init(); setulong(n); }
CBigNum(unsigned short n) { init(); setulong(n); }
CBigNum(unsigned int n) { init(); setulong(n); }
// CBigNum(unsigned long n) { init(); setulong(n); }
CBigNum(uint64_t n) { init(); setuint64(n); }
CBigNum(long long int n) { init(); setuint64(n); }
explicit CBigNum(uint256 n) { init(); setuint256(n); }
//CBigNum(char n) is not portable. Use 'signed char' or 'unsigned char'. //CBigNum(char n) is not portable. Use 'signed char' or 'unsigned char'.
CBigNum(signed char n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } /* CBigNum(signed char n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }
CBigNum(short n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } CBigNum(short n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }
CBigNum(int n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } CBigNum(int n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }
CBigNum(long n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } CBigNum(long n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); }
@ -89,34 +115,35 @@ public:
CBigNum(unsigned int n) { BN_init(this); setulong(n); } CBigNum(unsigned int n) { BN_init(this); setulong(n); }
CBigNum(unsigned long n) { BN_init(this); setulong(n); } CBigNum(unsigned long n) { BN_init(this); setulong(n); }
CBigNum(uint64 n) { BN_init(this); setuint64(n); } CBigNum(uint64 n) { BN_init(this); setuint64(n); }
explicit CBigNum(uint256 n) { BN_init(this); setuint256(n); } */
// explicit CBigNum(uint256 n) { BN_init(this); setuint256(n); }
explicit CBigNum(const std::vector<unsigned char>& vch) explicit CBigNum(const std::vector<unsigned char>& vch)
{ {
BN_init(this); init();
setvch(vch); setvch(vch);
} }
void setulong(unsigned long n) void setulong(unsigned long n)
{ {
if (!BN_set_word(this, n)) if (!BN_set_word(bn, n))
throw bignum_error("CBigNum conversion from unsigned long : BN_set_word failed"); throw bignum_error("CBigNum conversion from unsigned long : BN_set_word failed");
} }
unsigned long getulong() const unsigned long getulong() const
{ {
return BN_get_word(this); return BN_get_word(bn);
} }
unsigned int getuint() const unsigned int getuint() const
{ {
return BN_get_word(this); return BN_get_word(bn);
} }
int getint() const int getint() const
{ {
unsigned long n = BN_get_word(this); unsigned long n = BN_get_word(bn);
if (!BN_is_negative(this)) if (!BN_is_negative(bn))
return (n > (unsigned long)std::numeric_limits<int>::max() ? std::numeric_limits<int>::max() : n); return (n > (unsigned long)std::numeric_limits<int>::max() ? std::numeric_limits<int>::max() : n);
else else
return (n > (unsigned long)std::numeric_limits<int>::max() ? std::numeric_limits<int>::min() : -(int)n); return (n > (unsigned long)std::numeric_limits<int>::max() ? std::numeric_limits<int>::min() : -(int)n);
@ -162,16 +189,16 @@ public:
pch[1] = (nSize >> 16) & 0xff; pch[1] = (nSize >> 16) & 0xff;
pch[2] = (nSize >> 8) & 0xff; pch[2] = (nSize >> 8) & 0xff;
pch[3] = (nSize) & 0xff; pch[3] = (nSize) & 0xff;
BN_mpi2bn(pch, p - pch, this); BN_mpi2bn(pch, p - pch, bn);
} }
uint64 getuint64() uint64 getuint64()
{ {
unsigned int nSize = BN_bn2mpi(this, NULL); unsigned int nSize = BN_bn2mpi(bn, NULL);
if (nSize < 4) if (nSize < 4)
return 0; return 0;
std::vector<unsigned char> vch(nSize); std::vector<unsigned char> vch(nSize);
BN_bn2mpi(this, &vch[0]); BN_bn2mpi(bn, &vch[0]);
if (vch.size() > 4) if (vch.size() > 4)
vch[4] &= 0x7f; vch[4] &= 0x7f;
uint64 n = 0; uint64 n = 0;
@ -204,7 +231,7 @@ public:
pch[1] = (nSize >> 16) & 0xff; pch[1] = (nSize >> 16) & 0xff;
pch[2] = (nSize >> 8) & 0xff; pch[2] = (nSize >> 8) & 0xff;
pch[3] = (nSize) & 0xff; pch[3] = (nSize) & 0xff;
BN_mpi2bn(pch, p - pch, this); BN_mpi2bn(pch, p - pch, bn);
} }
void setuint256(uint256 n) void setuint256(uint256 n)
@ -232,16 +259,16 @@ public:
pch[1] = (nSize >> 16) & 0xff; pch[1] = (nSize >> 16) & 0xff;
pch[2] = (nSize >> 8) & 0xff; pch[2] = (nSize >> 8) & 0xff;
pch[3] = (nSize >> 0) & 0xff; pch[3] = (nSize >> 0) & 0xff;
BN_mpi2bn(pch, p - pch, this); BN_mpi2bn(pch, p - pch, bn);
} }
uint256 getuint256() uint256 getuint256()
{ {
unsigned int nSize = BN_bn2mpi(this, NULL); unsigned int nSize = BN_bn2mpi(bn, NULL);
if (nSize < 4) if (nSize < 4)
return 0; return 0;
std::vector<unsigned char> vch(nSize); std::vector<unsigned char> vch(nSize);
BN_bn2mpi(this, &vch[0]); BN_bn2mpi(bn, &vch[0]);
if (vch.size() > 4) if (vch.size() > 4)
vch[4] &= 0x7f; vch[4] &= 0x7f;
uint256 n = 0; uint256 n = 0;
@ -263,16 +290,16 @@ public:
vch2[3] = (nSize >> 0) & 0xff; vch2[3] = (nSize >> 0) & 0xff;
// swap data to big endian // swap data to big endian
reverse_copy(vch.begin(), vch.end(), vch2.begin() + 4); reverse_copy(vch.begin(), vch.end(), vch2.begin() + 4);
BN_mpi2bn(&vch2[0], vch2.size(), this); BN_mpi2bn(&vch2[0], vch2.size(), bn);
} }
std::vector<unsigned char> getvch() const std::vector<unsigned char> getvch() const
{ {
unsigned int nSize = BN_bn2mpi(this, NULL); unsigned int nSize = BN_bn2mpi(bn, NULL);
if (nSize <= 4) if (nSize <= 4)
return std::vector<unsigned char>(); return std::vector<unsigned char>();
std::vector<unsigned char> vch(nSize); std::vector<unsigned char> vch(nSize);
BN_bn2mpi(this, &vch[0]); BN_bn2mpi(bn, &vch[0]);
vch.erase(vch.begin(), vch.begin() + 4); vch.erase(vch.begin(), vch.begin() + 4);
reverse(vch.begin(), vch.end()); reverse(vch.begin(), vch.end());
return vch; return vch;
@ -286,16 +313,16 @@ public:
if (nSize >= 1) vch[4] = (nCompact >> 16) & 0xff; if (nSize >= 1) vch[4] = (nCompact >> 16) & 0xff;
if (nSize >= 2) vch[5] = (nCompact >> 8) & 0xff; if (nSize >= 2) vch[5] = (nCompact >> 8) & 0xff;
if (nSize >= 3) vch[6] = (nCompact >> 0) & 0xff; if (nSize >= 3) vch[6] = (nCompact >> 0) & 0xff;
BN_mpi2bn(&vch[0], vch.size(), this); BN_mpi2bn(&vch[0], vch.size(), bn);
return *this; return *this;
} }
unsigned int GetCompact() const unsigned int GetCompact() const
{ {
unsigned int nSize = BN_bn2mpi(this, NULL); unsigned int nSize = BN_bn2mpi(bn, NULL);
std::vector<unsigned char> vch(nSize); std::vector<unsigned char> vch(nSize);
nSize -= 4; nSize -= 4;
BN_bn2mpi(this, &vch[0]); BN_bn2mpi(bn, &vch[0]);
unsigned int nCompact = nSize << 24; unsigned int nCompact = nSize << 24;
if (nSize >= 1) nCompact |= (vch[4] << 16); if (nSize >= 1) nCompact |= (vch[4] << 16);
if (nSize >= 2) nCompact |= (vch[5] << 8); if (nSize >= 2) nCompact |= (vch[5] << 8);
@ -339,21 +366,21 @@ public:
CBigNum bnBase = nBase; CBigNum bnBase = nBase;
CBigNum bn0 = 0; CBigNum bn0 = 0;
std::string str; std::string str;
CBigNum bn = *this; CBigNum bn1 = *this;
BN_set_negative(&bn, false); BN_set_negative(&bn1, false);
CBigNum dv; CBigNum dv;
CBigNum rem; CBigNum rem;
if (BN_cmp(&bn, &bn0) == 0) if (BN_cmp(&bn1, &bn0) == 0)
return "0"; return "0";
while (BN_cmp(&bn, &bn0) > 0) while (BN_cmp(&bn1, &bn0) > 0)
{ {
if (!BN_div(&dv, &rem, &bn, &bnBase, pctx)) if (!BN_div(&dv, &rem, &bn1, &bnBase, pctx))
throw bignum_error("CBigNum::ToString() : BN_div failed"); throw bignum_error("CBigNum::ToString() : BN_div failed");
bn = dv; bn1 = dv;
unsigned int c = rem.getulong(); unsigned int c = rem.getulong();
str += "0123456789abcdef"[c]; str += "0123456789abcdef"[c];
} }
if (BN_is_negative(this)) if (BN_is_negative(bn))
str += "-"; str += "-";
reverse(str.begin(), str.end()); reverse(str.begin(), str.end());
return str; return str;
@ -386,12 +413,12 @@ public:
bool operator!() const bool operator!() const
{ {
return BN_is_zero(this); return BN_is_zero(bn);
} }
CBigNum& operator+=(const CBigNum& b) CBigNum& operator+=(const CBigNum& b)
{ {
if (!BN_add(this, this, &b)) if (!BN_add(bn, bn, &b))
throw bignum_error("CBigNum::operator+= : BN_add failed"); throw bignum_error("CBigNum::operator+= : BN_add failed");
return *this; return *this;
} }
@ -405,7 +432,7 @@ public:
CBigNum& operator*=(const CBigNum& b) CBigNum& operator*=(const CBigNum& b)
{ {
CAutoBN_CTX pctx; CAutoBN_CTX pctx;
if (!BN_mul(this, this, &b, pctx)) if (!BN_mul(bn, bn, &b, pctx))
throw bignum_error("CBigNum::operator*= : BN_mul failed"); throw bignum_error("CBigNum::operator*= : BN_mul failed");
return *this; return *this;
} }
@ -424,7 +451,7 @@ public:
CBigNum& operator<<=(unsigned int shift) CBigNum& operator<<=(unsigned int shift)
{ {
if (!BN_lshift(this, this, shift)) if (!BN_lshift(bn, bn, shift))
throw bignum_error("CBigNum:operator<<= : BN_lshift failed"); throw bignum_error("CBigNum:operator<<= : BN_lshift failed");
return *this; return *this;
} }
@ -435,13 +462,13 @@ public:
// if built on ubuntu 9.04 or 9.10, probably depends on version of OpenSSL // if built on ubuntu 9.04 or 9.10, probably depends on version of OpenSSL
CBigNum a = 1; CBigNum a = 1;
a <<= shift; a <<= shift;
if (BN_cmp(&a, this) > 0) if (BN_cmp(&a, bn) > 0)
{ {
*this = 0; *this = 0;
return *this; return *this;
} }
if (!BN_rshift(this, this, shift)) if (!BN_rshift(bn, bn, shift))
throw bignum_error("CBigNum:operator>>= : BN_rshift failed"); throw bignum_error("CBigNum:operator>>= : BN_rshift failed");
return *this; return *this;
} }
@ -450,7 +477,7 @@ public:
CBigNum& operator++() CBigNum& operator++()
{ {
// prefix operator // prefix operator
if (!BN_add(this, this, BN_value_one())) if (!BN_add(bn, bn, BN_value_one()))
throw bignum_error("CBigNum::operator++ : BN_add failed"); throw bignum_error("CBigNum::operator++ : BN_add failed");
return *this; return *this;
} }
@ -467,7 +494,7 @@ public:
{ {
// prefix operator // prefix operator
CBigNum r; CBigNum r;
if (!BN_sub(&r, this, BN_value_one())) if (!BN_sub(&r, bn, BN_value_one()))
throw bignum_error("CBigNum::operator-- : BN_sub failed"); throw bignum_error("CBigNum::operator-- : BN_sub failed");
*this = r; *this = r;
return *this; return *this;

View File

@ -56,15 +56,15 @@ bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned
int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0; int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
vchCiphertext = std::vector<unsigned char> (nCLen); vchCiphertext = std::vector<unsigned char> (nCLen);
EVP_CIPHER_CTX ctx; EVP_CIPHER_CTX *ctx;
bool fOk = true; bool fOk = true;
EVP_CIPHER_CTX_init(&ctx); EVP_CIPHER_CTX_init(ctx);
if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV); if (fOk) fOk = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
if (fOk) fOk = EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen); if (fOk) fOk = EVP_EncryptUpdate(ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen);
if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0])+nCLen, &nFLen); if (fOk) fOk = EVP_EncryptFinal_ex(ctx, (&vchCiphertext[0])+nCLen, &nFLen);
EVP_CIPHER_CTX_cleanup(&ctx); EVP_CIPHER_CTX_cleanup(ctx);
if (!fOk) return false; if (!fOk) return false;
@ -83,15 +83,15 @@ bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingM
vchPlaintext = CKeyingMaterial(nPLen); vchPlaintext = CKeyingMaterial(nPLen);
EVP_CIPHER_CTX ctx; EVP_CIPHER_CTX *ctx;
bool fOk = true; bool fOk = true;
EVP_CIPHER_CTX_init(&ctx); EVP_CIPHER_CTX_init(ctx);
if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV); if (fOk) fOk = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
if (fOk) fOk = EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen); if (fOk) fOk = EVP_DecryptUpdate(ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen);
if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0])+nPLen, &nFLen); if (fOk) fOk = EVP_DecryptFinal_ex(ctx, (&vchPlaintext[0])+nPLen, &nFLen);
EVP_CIPHER_CTX_cleanup(&ctx); EVP_CIPHER_CTX_cleanup(ctx);
if (!fOk) return false; if (!fOk) return false;

View File

@ -559,7 +559,7 @@ public:
} }
bool connect(const std::string& server, const std::string& port) bool connect(const std::string& server, const std::string& port)
{ {
ip::tcp::resolver resolver(stream.get_io_service()); ip::tcp::resolver resolver(GetIOService(stream));
ip::tcp::resolver::query query(server.c_str(), port.c_str()); ip::tcp::resolver::query query(server.c_str(), port.c_str());
ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
ip::tcp::resolver::iterator end; ip::tcp::resolver::iterator end;
@ -665,7 +665,7 @@ static void RPCListen(boost::shared_ptr< basic_socket_acceptor<Protocol, SocketA
const bool fUseSSL) const bool fUseSSL)
{ {
// Accept connection // Accept connection
AcceptedConnectionImpl<Protocol>* conn = new AcceptedConnectionImpl<Protocol>(acceptor->get_io_service(), context, fUseSSL); AcceptedConnectionImpl<Protocol>* conn = new AcceptedConnectionImpl<Protocol>(GetIOServiceFromPtr(acceptor), context, fUseSSL);
acceptor->async_accept( acceptor->async_accept(
conn->sslStream.lowest_layer(), conn->sslStream.lowest_layer(),

View File

@ -16,9 +16,23 @@ class CBlockIndex;
#include "json/json_spirit_writer_template.h" #include "json/json_spirit_writer_template.h"
#include "json/json_spirit_utils.h" #include "json/json_spirit_utils.h"
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include "util.h" #include "util.h"
#include "checkpoints.h" #include "checkpoints.h"
// Boost Support for 1.70+
#if BOOST_VERSION >= 107000
#define GetIOService(s) ((boost::asio::io_context&)(s).get_executor().context())
#define GetIOServiceFromPtr(s) ((boost::asio::io_context&)(s->get_executor().context())) // this one
typedef boost::asio::io_context ioContext;
#else
#define GetIOService(s) ((s).get_io_service())
#define GetIOServiceFromPtr(s) ((s)->get_io_service())
typedef boost::asio::io_service ioContext;
#endif
// HTTP status codes // HTTP status codes
enum HTTPStatusCode enum HTTPStatusCode
{ {

View File

@ -67,6 +67,7 @@ int ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, ECDSA_SIG *ecsig, const unsigned ch
EC_POINT *Q = NULL; EC_POINT *Q = NULL;
BIGNUM *rr = NULL; BIGNUM *rr = NULL;
BIGNUM *zero = NULL; BIGNUM *zero = NULL;
BIGNUM *s = 0;
int n = 0; int n = 0;
int i = recid / 2; int i = recid / 2;
@ -78,7 +79,8 @@ int ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, ECDSA_SIG *ecsig, const unsigned ch
x = BN_CTX_get(ctx); x = BN_CTX_get(ctx);
if (!BN_copy(x, order)) { ret=-1; goto err; } if (!BN_copy(x, order)) { ret=-1; goto err; }
if (!BN_mul_word(x, i)) { ret=-1; goto err; } if (!BN_mul_word(x, i)) { ret=-1; goto err; }
if (!BN_add(x, x, ecsig->r)) { ret=-1; goto err; } ECDSA_SIG_get0(ecsig, (const BIGNUM **)&s, 0);
if (!BN_add(x, x, s)) { ret=-1; goto err; }
field = BN_CTX_get(ctx); field = BN_CTX_get(ctx);
if (!EC_GROUP_get_curve_GFp(group, field, NULL, NULL, ctx)) { ret=-2; goto err; } if (!EC_GROUP_get_curve_GFp(group, field, NULL, NULL, ctx)) { ret=-2; goto err; }
if (BN_cmp(x, field) >= 0) { ret=0; goto err; } if (BN_cmp(x, field) >= 0) { ret=0; goto err; }
@ -99,9 +101,11 @@ int ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, ECDSA_SIG *ecsig, const unsigned ch
if (!BN_zero(zero)) { ret=-1; goto err; } if (!BN_zero(zero)) { ret=-1; goto err; }
if (!BN_mod_sub(e, zero, e, order, ctx)) { ret=-1; goto err; } if (!BN_mod_sub(e, zero, e, order, ctx)) { ret=-1; goto err; }
rr = BN_CTX_get(ctx); rr = BN_CTX_get(ctx);
if (!BN_mod_inverse(rr, ecsig->r, order, ctx)) { ret=-1; goto err; } ECDSA_SIG_get0(ecsig, (const BIGNUM **)&s, 0);
if (!BN_mod_inverse(rr, s, order, ctx)) { ret=-1; goto err; }
sor = BN_CTX_get(ctx); sor = BN_CTX_get(ctx);
if (!BN_mod_mul(sor, ecsig->s, rr, order, ctx)) { ret=-1; goto err; } ECDSA_SIG_get0(ecsig, 0, (const BIGNUM **)&s);
if (!BN_mod_mul(sor, s, rr, order, ctx)) { ret=-1; goto err; }
eor = BN_CTX_get(ctx); eor = BN_CTX_get(ctx);
if (!BN_mod_mul(eor, e, rr, order, ctx)) { ret=-1; goto err; } if (!BN_mod_mul(eor, e, rr, order, ctx)) { ret=-1; goto err; }
if (!EC_POINT_mul(group, Q, eor, R, sor, ctx)) { ret=-2; goto err; } if (!EC_POINT_mul(group, Q, eor, R, sor, ctx)) { ret=-2; goto err; }
@ -303,13 +307,16 @@ bool CKey::Sign(uint256 hash, std::vector<unsigned char>& vchSig)
bool CKey::SignCompact(uint256 hash, std::vector<unsigned char>& vchSig) bool CKey::SignCompact(uint256 hash, std::vector<unsigned char>& vchSig)
{ {
bool fOk = false; bool fOk = false;
BIGNUM *s = 0;
BIGNUM *r = 0;
ECDSA_SIG *sig = ECDSA_do_sign((unsigned char*)&hash, sizeof(hash), pkey); ECDSA_SIG *sig = ECDSA_do_sign((unsigned char*)&hash, sizeof(hash), pkey);
if (sig==NULL) if (sig==NULL)
return false; return false;
vchSig.clear(); vchSig.clear();
vchSig.resize(65,0); vchSig.resize(65,0);
int nBitsR = BN_num_bits(sig->r); ECDSA_SIG_get0(sig, (const BIGNUM **)&r, (const BIGNUM **)&s);
int nBitsS = BN_num_bits(sig->s); int nBitsR = BN_num_bits(r);
int nBitsS = BN_num_bits(s);
if (nBitsR <= 256 && nBitsS <= 256) if (nBitsR <= 256 && nBitsS <= 256)
{ {
int nRecId = -1; int nRecId = -1;
@ -331,8 +338,9 @@ bool CKey::SignCompact(uint256 hash, std::vector<unsigned char>& vchSig)
throw key_error("CKey::SignCompact() : unable to construct recoverable key"); throw key_error("CKey::SignCompact() : unable to construct recoverable key");
vchSig[0] = nRecId+27+(fCompressedPubKey ? 4 : 0); vchSig[0] = nRecId+27+(fCompressedPubKey ? 4 : 0);
BN_bn2bin(sig->r,&vchSig[33-(nBitsR+7)/8]); ECDSA_SIG_get0(sig, (const BIGNUM **)&r, (const BIGNUM **)&s);
BN_bn2bin(sig->s,&vchSig[65-(nBitsS+7)/8]); BN_bn2bin(r,&vchSig[33-(nBitsR+7)/8]);
BN_bn2bin(s,&vchSig[65-(nBitsS+7)/8]);
fOk = true; fOk = true;
} }
ECDSA_SIG_free(sig); ECDSA_SIG_free(sig);
@ -345,14 +353,17 @@ bool CKey::SignCompact(uint256 hash, std::vector<unsigned char>& vchSig)
// (the signature is a valid signature of the given data for that key) // (the signature is a valid signature of the given data for that key)
bool CKey::SetCompactSignature(uint256 hash, const std::vector<unsigned char>& vchSig) bool CKey::SetCompactSignature(uint256 hash, const std::vector<unsigned char>& vchSig)
{ {
BIGNUM *s = 0;
BIGNUM *r = 0;
if (vchSig.size() != 65) if (vchSig.size() != 65)
return false; return false;
int nV = vchSig[0]; int nV = vchSig[0];
if (nV<27 || nV>=35) if (nV<27 || nV>=35)
return false; return false;
ECDSA_SIG *sig = ECDSA_SIG_new(); ECDSA_SIG *sig = ECDSA_SIG_new();
BN_bin2bn(&vchSig[1],32,sig->r); ECDSA_SIG_get0(sig, (const BIGNUM **)&r, (const BIGNUM **)&s);
BN_bin2bn(&vchSig[33],32,sig->s); BN_bin2bn(&vchSig[1],32,r);
BN_bin2bn(&vchSig[33],32,s);
EC_KEY_free(pkey); EC_KEY_free(pkey);
pkey = EC_KEY_new_by_curve_name(NID_secp256k1); pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
@ -404,3 +415,15 @@ bool CKey::IsValid()
key2.SetSecret(secret, fCompr); key2.SetSecret(secret, fCompr);
return GetPubKey() == key2.GetPubKey(); return GetPubKey() == key2.GetPubKey();
} }
#if OPENSSL_VERSION_NUMBER < 0x10100000L
void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
{
if (pr != NULL)
*pr = sig->r;
if (ps != NULL)
*ps = sig->s;
}
#endif

View File

@ -38,6 +38,11 @@
// see www.keylength.com // see www.keylength.com
// script supports up to 75 for single byte push // script supports up to 75 for single byte push
#if OPENSSL_VERSION_NUMBER < 0x10100000L
#include <openssl/ecdsa.h>
void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps);
#endif
class key_error : public std::runtime_error class key_error : public std::runtime_error
{ {
public: public:

View File

@ -224,6 +224,186 @@ inline std::string StackString(const std::vector<std::vector<unsigned char> >& v
return str; return str;
} }
class scriptnum_error : public std::runtime_error
{
public:
explicit scriptnum_error(const std::string& str) : std::runtime_error(str) {}
};
class CScriptNum
{
/**
* Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers.
* The semantics are subtle, though: operands must be in the range [-2^31 +1...2^31 -1],
* but results may overflow (and are valid as long as they are not used in a subsequent
* numeric operation). CScriptNum enforces those semantics by storing results as
* an int64 and allowing out-of-range values to be returned as a vector of bytes but
* throwing an exception if arithmetic is done or the result is interpreted as an integer.
*/
public:
explicit CScriptNum(const int64_t& n)
{
m_value = n;
}
static const size_t nDefaultMaxNumSize = 4;
explicit CScriptNum(const std::vector<unsigned char>& vch, bool fRequireMinimal,
const size_t nMaxNumSize = nDefaultMaxNumSize)
{
if (vch.size() > nMaxNumSize) {
throw scriptnum_error("script number overflow");
}
if (fRequireMinimal && vch.size() > 0) {
// Check that the number is encoded with the minimum possible
// number of bytes.
//
// If the most-significant-byte - excluding the sign bit - is zero
// then we're not minimal. Note how this test also rejects the
// negative-zero encoding, 0x80.
if ((vch.back() & 0x7f) == 0) {
// One exception: if there's more than one byte and the most
// significant bit of the second-most-significant-byte is set
// it would conflict with the sign bit. An example of this case
// is +-255, which encode to 0xff00 and 0xff80 respectively.
// (big-endian).
if (vch.size() <= 1 || (vch[vch.size() - 2] & 0x80) == 0) {
throw scriptnum_error("non-minimally encoded script number");
}
}
}
m_value = set_vch(vch);
}
inline bool operator==(const int64_t& rhs) const { return m_value == rhs; }
inline bool operator!=(const int64_t& rhs) const { return m_value != rhs; }
inline bool operator<=(const int64_t& rhs) const { return m_value <= rhs; }
inline bool operator< (const int64_t& rhs) const { return m_value < rhs; }
inline bool operator>=(const int64_t& rhs) const { return m_value >= rhs; }
inline bool operator> (const int64_t& rhs) const { return m_value > rhs; }
inline bool operator==(const CScriptNum& rhs) const { return operator==(rhs.m_value); }
inline bool operator!=(const CScriptNum& rhs) const { return operator!=(rhs.m_value); }
inline bool operator<=(const CScriptNum& rhs) const { return operator<=(rhs.m_value); }
inline bool operator< (const CScriptNum& rhs) const { return operator< (rhs.m_value); }
inline bool operator>=(const CScriptNum& rhs) const { return operator>=(rhs.m_value); }
inline bool operator> (const CScriptNum& rhs) const { return operator> (rhs.m_value); }
inline CScriptNum operator+( const int64_t& rhs) const { return CScriptNum(m_value + rhs);}
inline CScriptNum operator-( const int64_t& rhs) const { return CScriptNum(m_value - rhs);}
inline CScriptNum operator+( const CScriptNum& rhs) const { return operator+(rhs.m_value); }
inline CScriptNum operator-( const CScriptNum& rhs) const { return operator-(rhs.m_value); }
inline CScriptNum& operator+=( const CScriptNum& rhs) { return operator+=(rhs.m_value); }
inline CScriptNum& operator-=( const CScriptNum& rhs) { return operator-=(rhs.m_value); }
inline CScriptNum operator&( const int64_t& rhs) const { return CScriptNum(m_value & rhs);}
inline CScriptNum operator&( const CScriptNum& rhs) const { return operator&(rhs.m_value); }
inline CScriptNum& operator&=( const CScriptNum& rhs) { return operator&=(rhs.m_value); }
inline CScriptNum operator-() const
{
assert(m_value != std::numeric_limits<int64_t>::min());
return CScriptNum(-m_value);
}
inline CScriptNum& operator=( const int64_t& rhs)
{
m_value = rhs;
return *this;
}
inline CScriptNum& operator+=( const int64_t& rhs)
{
assert(rhs == 0 || (rhs > 0 && m_value <= std::numeric_limits<int64_t>::max() - rhs) ||
(rhs < 0 && m_value >= std::numeric_limits<int64_t>::min() - rhs));
m_value += rhs;
return *this;
}
inline CScriptNum& operator-=( const int64_t& rhs)
{
assert(rhs == 0 || (rhs > 0 && m_value >= std::numeric_limits<int64_t>::min() + rhs) ||
(rhs < 0 && m_value <= std::numeric_limits<int64_t>::max() + rhs));
m_value -= rhs;
return *this;
}
inline CScriptNum& operator&=( const int64_t& rhs)
{
m_value &= rhs;
return *this;
}
int getint() const
{
if (m_value > std::numeric_limits<int>::max())
return std::numeric_limits<int>::max();
else if (m_value < std::numeric_limits<int>::min())
return std::numeric_limits<int>::min();
return m_value;
}
std::vector<unsigned char> getvch() const
{
return serialize(m_value);
}
static std::vector<unsigned char> serialize(const int64_t& value)
{
if(value == 0)
return std::vector<unsigned char>();
std::vector<unsigned char> result;
const bool neg = value < 0;
uint64_t absvalue = neg ? -value : value;
while(absvalue)
{
result.push_back(absvalue & 0xff);
absvalue >>= 8;
}
// - If the most significant byte is >= 0x80 and the value is positive, push a
// new zero-byte to make the significant byte < 0x80 again.
// - If the most significant byte is >= 0x80 and the value is negative, push a
// new 0x80 byte that will be popped off when converting to an integral.
// - If the most significant byte is < 0x80 and the value is negative, add
// 0x80 to it, since it will be subtracted and interpreted as a negative when
// converting to an integral.
if (result.back() & 0x80)
result.push_back(neg ? 0x80 : 0);
else if (neg)
result.back() |= 0x80;
return result;
}
private:
static int64_t set_vch(const std::vector<unsigned char>& vch)
{
if (vch.empty())
return 0;
int64_t result = 0;
for (size_t i = 0; i != vch.size(); ++i)
result |= static_cast<int64_t>(vch[i]) << 8*i;
// If the input vector's most significant byte is 0x80, remove it from
// the result's msb and return a negative.
if (vch.back() & 0x80)
return -((int64_t)(result & ~(0x80ULL << (8 * (vch.size() - 1)))));
return result;
}
int64_t m_value;
};
@ -243,8 +423,7 @@ protected:
} }
else else
{ {
CBigNum bn(n); *this << CScriptNum::serialize(n);
*this << bn.getvch();
} }
return *this; return *this;
} }
@ -257,8 +436,7 @@ protected:
} }
else else
{ {
CBigNum bn(n); *this << CScriptNum::serialize(n);
*this << bn.getvch();
} }
return *this; return *this;
} }