From fee06a4c75d3215f9b502f8145383005bdda2c80 Mon Sep 17 00:00:00 2001 From: Nathan Hourt Date: Tue, 25 Apr 2017 15:50:56 -0500 Subject: [PATCH] Add OpenSSL 1.1.0 support These changes should add support for openssl 1.1.0 while maintaining compatibility with 1.0.2 --- src/crypto/base58.cpp | 142 ++++++++++++++++++++++-------------------- src/crypto/dh.cpp | 47 +++++++++++++- 2 files changed, 119 insertions(+), 70 deletions(-) diff --git a/src/crypto/base58.cpp b/src/crypto/base58.cpp index e1d5d33..46c9adc 100644 --- a/src/crypto/base58.cpp +++ b/src/crypto/base58.cpp @@ -66,74 +66,71 @@ public: /** C++ wrapper for BIGNUM (OpenSSL bignum) */ -class CBigNum : public BIGNUM +class CBigNum { + BIGNUM* bn; public: CBigNum() - { - BN_init(this); - } + : bn(BN_new()) {} CBigNum(const CBigNum& b) + : CBigNum() { - BN_init(this); - if (!BN_copy(this, &b)) + if (!BN_copy(bn, b.bn)) { - BN_clear_free(this); + BN_clear_free(bn); throw bignum_error("CBigNum::CBigNum(const CBigNum&) : BN_copy failed"); } } CBigNum& operator=(const CBigNum& b) { - if (!BN_copy(this, &b)) + if (!BN_copy(bn, b.bn)) throw bignum_error("CBigNum::operator= : BN_copy failed"); return (*this); } ~CBigNum() { - BN_clear_free(this); + BN_clear_free(bn); } //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(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(long n) { BN_init(this); if (n >= 0) setulong(n); else setint64(n); } - CBigNum(int64_t n) { BN_init(this); setint64(n); } - CBigNum(unsigned char n) { BN_init(this); setulong(n); } - CBigNum(unsigned short 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(uint64_t n) { BN_init(this); setuint64(n); } + CBigNum(signed char n) :CBigNum() { if (n >= 0) setulong(n); else setint64(n); } + CBigNum(short n) :CBigNum() { if (n >= 0) setulong(n); else setint64(n); } + CBigNum(int n) :CBigNum() { if (n >= 0) setulong(n); else setint64(n); } + CBigNum(int64_t n) :CBigNum() { setint64(n); } + CBigNum(unsigned char n) :CBigNum() { setulong(n); } + CBigNum(unsigned short n) :CBigNum() { setulong(n); } + CBigNum(unsigned int n) :CBigNum() { setulong(n); } + CBigNum(uint64_t n) :CBigNum() { setuint64(n); } explicit CBigNum(const std::vector& vch) + : CBigNum() { - BN_init(this); setvch(vch); } 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"); } unsigned long getulong() const { - return BN_get_word(this); + return BN_get_word(bn); } unsigned int getuint() const { - return BN_get_word(this); + return BN_get_word(bn); } int getint() const { - unsigned long n = BN_get_word(this); - if (!BN_is_negative(this)) + unsigned long n = BN_get_word(bn); + if (!BN_is_negative(bn)) return (n > (unsigned long)std::numeric_limits::max() ? std::numeric_limits::max() : n); else return (n > (unsigned long)std::numeric_limits::max() ? std::numeric_limits::min() : -(int)n); @@ -171,7 +168,7 @@ public: pch[1] = (nSize >> 16) & 0xff; pch[2] = (nSize >> 8) & 0xff; pch[3] = (nSize) & 0xff; - BN_mpi2bn(pch, p - pch, this); + BN_mpi2bn(pch, p - pch, bn); } void setuint64(uint64_t n) @@ -198,7 +195,7 @@ public: pch[1] = (nSize >> 16) & 0xff; pch[2] = (nSize >> 8) & 0xff; pch[3] = (nSize) & 0xff; - BN_mpi2bn(pch, p - pch, this); + BN_mpi2bn(pch, p - pch, bn); } @@ -214,16 +211,16 @@ public: vch2[3] = (nSize >> 0) & 0xff; // swap data to big endian 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 getvch() const { - unsigned int nSize = BN_bn2mpi(this, NULL); + unsigned int nSize = BN_bn2mpi(bn, NULL); if (nSize <= 4) return std::vector(); std::vector vch(nSize); - BN_bn2mpi(this, &vch[0]); + BN_bn2mpi(bn, &vch[0]); vch.erase(vch.begin(), vch.begin() + 4); reverse(vch.begin(), vch.end()); return vch; @@ -237,16 +234,16 @@ public: if (nSize >= 1) vch[4] = (nCompact >> 16) & 0xff; if (nSize >= 2) vch[5] = (nCompact >> 8) & 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; } unsigned int GetCompact() const { - unsigned int nSize = BN_bn2mpi(this, NULL); + unsigned int nSize = BN_bn2mpi(bn, NULL); std::vector vch(nSize); nSize -= 4; - BN_bn2mpi(this, &vch[0]); + BN_bn2mpi(bn, &vch[0]); unsigned int nCompact = nSize << 24; if (nSize >= 1) nCompact |= (vch[4] << 16); if (nSize >= 2) nCompact |= (vch[5] << 8); @@ -281,7 +278,7 @@ public: *this += n; } if (fNegative) - *this = 0 - *this; + BN_set_negative(bn, 1); } std::string ToString(int nBase=10) const @@ -291,20 +288,20 @@ public: CBigNum bn0 = 0; std::string str; CBigNum bn = *this; - BN_set_negative(&bn, false); + BN_set_negative(bn.bn, false); CBigNum dv; CBigNum rem; - if (BN_cmp(&bn, &bn0) == 0) + if (BN_cmp(bn.bn, bn0.bn) == 0) return "0"; - while (BN_cmp(&bn, &bn0) > 0) + while (BN_cmp(bn.bn, bn0.bn) > 0) { - if (!BN_div(&dv, &rem, &bn, &bnBase, pctx)) + if (!BN_div(dv.bn, rem.bn, bn.bn, bnBase.bn, pctx)) throw bignum_error("CBigNum::ToString() : BN_div failed"); bn = dv; unsigned int c = rem.getulong(); str += "0123456789abcdef"[c]; } - if (BN_is_negative(this)) + if (BN_is_negative(this->bn)) str += "-"; reverse(str.begin(), str.end()); return str; @@ -319,45 +316,50 @@ public: bool operator!() const { - return BN_is_zero(this); + return BN_is_zero(bn); } CBigNum& operator+=(const CBigNum& b) { - if (!BN_add(this, this, &b)) + if (!BN_add(bn, bn, b.bn)) throw bignum_error("CBigNum::operator+= : BN_add failed"); return *this; } CBigNum& operator-=(const CBigNum& b) { - *this = *this - b; + if (!BN_sub(bn, bn, b.bn)) + throw bignum_error("CBigNum::operator-= : BN_sub failed"); return *this; } CBigNum& operator*=(const CBigNum& b) { CAutoBN_CTX pctx; - if (!BN_mul(this, this, &b, pctx)) + if (!BN_mul(bn, bn, b.bn, pctx)) throw bignum_error("CBigNum::operator*= : BN_mul failed"); return *this; } CBigNum& operator/=(const CBigNum& b) { - *this = *this / b; + CAutoBN_CTX pctx; + if (!BN_div(bn, NULL, bn, b.bn, pctx)) + throw bignum_error("CBigNum::operator/= : BN_div failed"); return *this; } CBigNum& operator%=(const CBigNum& b) { - *this = *this % b; + CAutoBN_CTX pctx; + if (!BN_div(NULL, bn, bn, b.bn, pctx)) + throw bignum_error("CBigNum::operator%= : BN_div failed"); return *this; } 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"); return *this; } @@ -368,13 +370,13 @@ public: // if built on ubuntu 9.04 or 9.10, probably depends on version of openssl CBigNum a = 1; a <<= shift; - if (BN_cmp(&a, this) > 0) + if (BN_cmp(a.bn, bn) > 0) { *this = 0; return *this; } - if (!BN_rshift(this, this, shift)) + if (!BN_rshift(bn, bn, shift)) throw bignum_error("CBigNum:operator>>= : BN_rshift failed"); return *this; } @@ -383,7 +385,7 @@ public: CBigNum& 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"); return *this; } @@ -400,7 +402,7 @@ public: { // prefix operator CBigNum r; - if (!BN_sub(&r, this, BN_value_one())) + if (!BN_sub(r.bn, bn, BN_value_one())) throw bignum_error("CBigNum::operator-- : BN_sub failed"); *this = r; return *this; @@ -414,10 +416,12 @@ public: return ret; } - - friend inline const CBigNum operator-(const CBigNum& a, const CBigNum& b); - friend inline const CBigNum operator/(const CBigNum& a, const CBigNum& b); - friend inline const CBigNum operator%(const CBigNum& a, const CBigNum& b); + const BIGNUM* to_bignum() const { + return bn; + } + BIGNUM* to_bignum() { + return bn; + } }; @@ -425,7 +429,7 @@ public: inline const CBigNum operator+(const CBigNum& a, const CBigNum& b) { CBigNum r; - if (!BN_add(&r, &a, &b)) + if (!BN_add(r.to_bignum(), a.to_bignum(), b.to_bignum())) throw bignum_error("CBigNum::operator+ : BN_add failed"); return r; } @@ -433,7 +437,7 @@ inline const CBigNum operator+(const CBigNum& a, const CBigNum& b) inline const CBigNum operator-(const CBigNum& a, const CBigNum& b) { CBigNum r; - if (!BN_sub(&r, &a, &b)) + if (!BN_sub(r.to_bignum(), a.to_bignum(), b.to_bignum())) throw bignum_error("CBigNum::operator- : BN_sub failed"); return r; } @@ -441,7 +445,7 @@ inline const CBigNum operator-(const CBigNum& a, const CBigNum& b) inline const CBigNum operator-(const CBigNum& a) { CBigNum r(a); - BN_set_negative(&r, !BN_is_negative(&r)); + BN_set_negative(r.to_bignum(), !BN_is_negative(r.to_bignum())); return r; } @@ -449,7 +453,7 @@ inline const CBigNum operator*(const CBigNum& a, const CBigNum& b) { CAutoBN_CTX pctx; CBigNum r; - if (!BN_mul(&r, &a, &b, pctx)) + if (!BN_mul(r.to_bignum(), a.to_bignum(), b.to_bignum(), pctx)) throw bignum_error("CBigNum::operator* : BN_mul failed"); return r; } @@ -458,7 +462,7 @@ inline const CBigNum operator/(const CBigNum& a, const CBigNum& b) { CAutoBN_CTX pctx; CBigNum r; - if (!BN_div(&r, NULL, &a, &b, pctx)) + if (!BN_div(r.to_bignum(), NULL, a.to_bignum(), b.to_bignum(), pctx)) throw bignum_error("CBigNum::operator/ : BN_div failed"); return r; } @@ -467,7 +471,7 @@ inline const CBigNum operator%(const CBigNum& a, const CBigNum& b) { CAutoBN_CTX pctx; CBigNum r; - if (!BN_mod(&r, &a, &b, pctx)) + if (!BN_mod(r.to_bignum(), a.to_bignum(), b.to_bignum(), pctx)) throw bignum_error("CBigNum::operator% : BN_div failed"); return r; } @@ -475,7 +479,7 @@ inline const CBigNum operator%(const CBigNum& a, const CBigNum& b) inline const CBigNum operator<<(const CBigNum& a, unsigned int shift) { CBigNum r; - if (!BN_lshift(&r, &a, shift)) + if (!BN_lshift(r.to_bignum(), a.to_bignum(), shift)) throw bignum_error("CBigNum:operator<< : BN_lshift failed"); return r; } @@ -487,12 +491,12 @@ inline const CBigNum operator>>(const CBigNum& a, unsigned int shift) return r; } -inline bool operator==(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) == 0); } -inline bool operator!=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) != 0); } -inline bool operator<=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) <= 0); } -inline bool operator>=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) >= 0); } -inline bool operator<(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) < 0); } -inline bool operator>(const CBigNum& a, const CBigNum& b) { return (BN_cmp(&a, &b) > 0); } +inline bool operator==(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.to_bignum(), b.to_bignum()) == 0); } +inline bool operator!=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.to_bignum(), b.to_bignum()) != 0); } +inline bool operator<=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.to_bignum(), b.to_bignum()) <= 0); } +inline bool operator>=(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.to_bignum(), b.to_bignum()) >= 0); } +inline bool operator<(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.to_bignum(), b.to_bignum()) < 0); } +inline bool operator>(const CBigNum& a, const CBigNum& b) { return (BN_cmp(a.to_bignum(), b.to_bignum()) > 0); } static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; @@ -522,7 +526,7 @@ inline std::string EncodeBase58(const unsigned char* pbegin, const unsigned char CBigNum rem; while (bn > bn0) { - if (!BN_div(&dv, &rem, &bn, &bn58, pctx)) + if (!BN_div(dv.to_bignum(), rem.to_bignum(), bn.to_bignum(), bn58.to_bignum(), pctx)) throw bignum_error("EncodeBase58 : BN_div failed"); bn = dv; unsigned int c = rem.getulong(); @@ -572,7 +576,7 @@ inline bool DecodeBase58(const char* psz, std::vector& vchRet) break; } bnChar.setulong(p1 - pszBase58); - if (!BN_mul(&bn, &bn, &bn58, pctx)) + if (!BN_mul(bn.to_bignum(), bn.to_bignum(), bn58.to_bignum(), pctx)) throw bignum_error("DecodeBase58 : BN_mul failed"); bn += bnChar; } diff --git a/src/crypto/dh.cpp b/src/crypto/dh.cpp index cbd7dcc..e7f6c59 100644 --- a/src/crypto/dh.cpp +++ b/src/crypto/dh.cpp @@ -1,6 +1,9 @@ #include #include +#if OPENSSL_VERSION_NUMBER >= 0x10100000L +#endif + namespace fc { SSL_TYPE(ssl_dh, DH, DH_free) @@ -12,10 +15,19 @@ namespace fc { bool diffie_hellman::generate_params( int s, uint8_t g ) { - ssl_dh dh = DH_generate_parameters( s, g, NULL, NULL ); + ssl_dh dh; + DH_generate_parameters_ex(dh.obj, s, g, NULL); +#if OPENSSL_VERSION_NUMBER >= 0x10100000L + ssl_bignum bn_p; + DH_get0_pqg(dh.obj, (const BIGNUM**)&bn_p.obj, NULL, NULL); + p.resize( BN_num_bytes( bn_p ) ); + if( p.size() ) + BN_bn2bin( bn_p, (unsigned char*)&p.front() ); +#else p.resize( BN_num_bytes( dh->p ) ); if( p.size() ) BN_bn2bin( dh->p, (unsigned char*)&p.front() ); +#endif this->g = g; return fc::validate( dh, valid ); } @@ -25,8 +37,14 @@ namespace fc { if( !p.size() ) return valid = false; ssl_dh dh = DH_new(); +#if OPENSSL_VERSION_NUMBER >= 0x10100000L + const auto bn_p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL ); + const auto bn_g = BN_bin2bn( (unsigned char*)&g, 1, NULL ); + DH_set0_pqg(dh.obj, bn_p, NULL, bn_g); +#else dh->p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL ); dh->g = BN_bin2bn( (unsigned char*)&g, 1, NULL ); +#endif return fc::validate( dh, valid ); } @@ -35,8 +53,14 @@ namespace fc { if( !p.size() ) return valid = false; ssl_dh dh = DH_new(); +#if OPENSSL_VERSION_NUMBER >= 0x10100000L + const auto bn_p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL ); + const auto bn_g = BN_bin2bn( (unsigned char*)&g, 1, NULL ); + DH_set0_pqg(dh.obj, bn_p, NULL, bn_g); +#else dh->p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL ); dh->g = BN_bin2bn( (unsigned char*)&g, 1, NULL ); +#endif if( !fc::validate( dh, valid ) ) { @@ -44,21 +68,42 @@ namespace fc { } DH_generate_key(dh); +#if OPENSSL_VERSION_NUMBER >= 0x10100000L + ssl_bignum bn_pub_key; + ssl_bignum bn_priv_key; + DH_get0_key(dh.obj, (const BIGNUM**)&bn_pub_key.obj, (const BIGNUM**)&bn_priv_key.obj); + pub_key.resize( BN_num_bytes( bn_pub_key ) ); + priv_key.resize( BN_num_bytes( bn_priv_key ) ); + if( pub_key.size() ) + BN_bn2bin( bn_pub_key.obj, (unsigned char*)&pub_key.front() ); + if( priv_key.size() ) + BN_bn2bin( bn_priv_key.obj, (unsigned char*)&priv_key.front() ); +#else pub_key.resize( BN_num_bytes( dh->pub_key ) ); priv_key.resize( BN_num_bytes( dh->priv_key ) ); if( pub_key.size() ) BN_bn2bin( dh->pub_key, (unsigned char*)&pub_key.front() ); if( priv_key.size() ) BN_bn2bin( dh->priv_key, (unsigned char*)&priv_key.front() ); +#endif return true; } bool diffie_hellman::compute_shared_key( const char* buf, uint32_t s ) { ssl_dh dh = DH_new(); +#if OPENSSL_VERSION_NUMBER >= 0x10100000L + auto bn_p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL ); + auto bn_pub_key = BN_bin2bn( (unsigned char*)&pub_key.front(), pub_key.size(), NULL ); + auto bn_priv_key = BN_bin2bn( (unsigned char*)&priv_key.front(), priv_key.size(), NULL ); + auto bn_g = BN_bin2bn( (unsigned char*)&g, 1, NULL ); + DH_set0_pqg(dh.obj, bn_p, NULL, bn_g); + DH_set0_key(dh.obj, bn_pub_key, bn_priv_key); +#else dh->p = BN_bin2bn( (unsigned char*)&p.front(), p.size(), NULL ); dh->pub_key = BN_bin2bn( (unsigned char*)&pub_key.front(), pub_key.size(), NULL ); dh->priv_key = BN_bin2bn( (unsigned char*)&priv_key.front(), priv_key.size(), NULL ); dh->g = BN_bin2bn( (unsigned char*)&g, 1, NULL ); +#endif int check; DH_check(dh,&check);