Skip to content

Commit

Permalink
Improvement for OSX and Boost
Browse files Browse the repository at this point in the history
Add support Boost 1.66
Add support for OpenSSL over 1.0.0.
--with-unsupported-ssl
  • Loading branch information
Chris committed Jan 27, 2018
1 parent 492d6d4 commit 96b1622
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 52 deletions.
8 changes: 4 additions & 4 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -766,10 +766,10 @@ else
fi

AC_CHECK_LIB([crypto],[RAND_egd],[],[
AC_ARG_WITH([libressl],
[AS_HELP_STRING([--with-libressl],[Build with system LibreSSL (default is no; DANGEROUS; NOT SUPPORTED)])],
[AC_MSG_WARN([Detected LibreSSL: This is NOT supported, and may break consensus compatibility!])],
[AC_MSG_ERROR([Detected LibreSSL: This is NOT supported, and may break consensus compatibility!])]
AC_ARG_WITH([unsupported-ssl],
[AS_HELP_STRING([--with-unsupported-ssl],[Build with system SSL (default is no; DANGEROUS; NOT SUPPORTED; You should use OpenSSL 1.0)])],
[AC_MSG_WARN([Detected unsupported SSL version: This is NOT supported, and may break consensus compatibility!])],
[AC_MSG_ERROR([Detected unsupported SSL version: This is NOT supported, and may break consensus compatibility!])]
)
])

Expand Down
2 changes: 1 addition & 1 deletion src/bitcloud-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Object CallRPC(const string& strMethod, const Array& params)
// Connect to localhost
bool fUseSSL = GetBoolArg("-rpcssl", false);
asio::io_service io_service;
ssl::context context(io_service, ssl::context::sslv23);
ssl::context context(ssl::context::sslv23);
context.set_options(ssl::context::no_sslv2 | ssl::context::no_sslv3);
asio::ssl::stream<asio::ip::tcp::socket> sslStream(io_service, context);
SSLIOStreamDevice<asio::ip::tcp> d(sslStream, fUseSSL);
Expand Down
48 changes: 22 additions & 26 deletions src/crypter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,13 @@ bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned
int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
vchCiphertext = std::vector<unsigned char>(nCLen);

EVP_CIPHER_CTX ctx;

bool fOk = true;

EVP_CIPHER_CTX_init(&ctx);
if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
if (fOk) fOk = EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0;
if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0;
EVP_CIPHER_CTX_cleanup(&ctx);
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
if (fOk) fOk = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
if (fOk) fOk = EVP_EncryptUpdate(ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0;
if (fOk) fOk = EVP_EncryptFinal_ex(ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0;
EVP_CIPHER_CTX_free(ctx);

if (!fOk) return false;

Expand All @@ -84,15 +82,13 @@ bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingM

vchPlaintext = CKeyingMaterial(nPLen);

EVP_CIPHER_CTX ctx;

bool fOk = true;

EVP_CIPHER_CTX_init(&ctx);
if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
if (fOk) fOk = EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0;
if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0;
EVP_CIPHER_CTX_cleanup(&ctx);
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
if (fOk) fOk = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
if (fOk) fOk = EVP_DecryptUpdate(ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0;
if (fOk) fOk = EVP_DecryptFinal_ex(ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0;
EVP_CIPHER_CTX_free(ctx);

if (!fOk) return false;

Expand Down Expand Up @@ -131,15 +127,15 @@ bool EncryptAES256(const SecureString& sKey, const SecureString& sPlaintext, con
sCiphertext.resize(nCLen);

// Perform the encryption
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX* ctx;

bool fOk = true;

EVP_CIPHER_CTX_init(&ctx);
if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, (const unsigned char*)&sKey[0], (const unsigned char*)&sIV[0]);
if (fOk) fOk = EVP_EncryptUpdate(&ctx, (unsigned char*)&sCiphertext[0], &nCLen, (const unsigned char*)&sPlaintext[0], nLen);
if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (unsigned char*)(&sCiphertext[0]) + nCLen, &nFLen);
EVP_CIPHER_CTX_cleanup(&ctx);
ctx = EVP_CIPHER_CTX_new();
if (fOk) fOk = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, (const unsigned char*)&sKey[0], (const unsigned char*)&sIV[0]);
if (fOk) fOk = EVP_EncryptUpdate(ctx, (unsigned char*)&sCiphertext[0], &nCLen, (const unsigned char*)&sPlaintext[0], nLen);
if (fOk) fOk = EVP_EncryptFinal_ex(ctx, (unsigned char*)(&sCiphertext[0]) + nCLen, &nFLen);
EVP_CIPHER_CTX_free(ctx);

if (!fOk) return false;

Expand Down Expand Up @@ -172,15 +168,15 @@ bool DecryptAES256(const SecureString& sKey, const std::string& sCiphertext, con

sPlaintext.resize(nPLen);

EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX* ctx;

bool fOk = true;

EVP_CIPHER_CTX_init(&ctx);
if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, (const unsigned char*)&sKey[0], (const unsigned char*)&sIV[0]);
if (fOk) fOk = EVP_DecryptUpdate(&ctx, (unsigned char*)&sPlaintext[0], &nPLen, (const unsigned char*)&sCiphertext[0], nLen);
if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (unsigned char*)(&sPlaintext[0]) + nPLen, &nFLen);
EVP_CIPHER_CTX_cleanup(&ctx);
ctx = EVP_CIPHER_CTX_new();
if (fOk) fOk = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, (const unsigned char*)&sKey[0], (const unsigned char*)&sIV[0]);
if (fOk) fOk = EVP_DecryptUpdate(ctx, (unsigned char*)&sPlaintext[0], &nPLen, (const unsigned char*)&sCiphertext[0], nLen);
if (fOk) fOk = EVP_DecryptFinal_ex(ctx, (unsigned char*)(&sPlaintext[0]) + nPLen, &nFLen);
EVP_CIPHER_CTX_free(ctx);

if (!fOk) return false;

Expand Down
31 changes: 30 additions & 1 deletion src/ecwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ int ECDSA_SIG_recover_key_GFp(EC_KEY* eckey, ECDSA_SIG* ecsig, const unsigned ch
int n = 0;
int i = recid / 2;

#if OPENSSL_VERSION_NUMBER >= 0x10100000L
const BIGNUM *sig_r, *sig_s;
ECDSA_SIG_get0(ecsig, &sig_r, &sig_s);
#endif

const EC_GROUP* group = EC_KEY_get0_group(eckey);
if ((ctx = BN_CTX_new()) == NULL) {
ret = -1;
Expand All @@ -59,7 +64,11 @@ int ECDSA_SIG_recover_key_GFp(EC_KEY* eckey, ECDSA_SIG* ecsig, const unsigned ch
ret = -1;
goto err;
}
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
if (!BN_add(x, x, sig_r)) {
#else
if (!BN_add(x, x, ecsig->r)) {
#endif
ret = -1;
goto err;
}
Expand Down Expand Up @@ -115,12 +124,20 @@ int ECDSA_SIG_recover_key_GFp(EC_KEY* eckey, ECDSA_SIG* ecsig, const unsigned ch
goto err;
}
rr = BN_CTX_get(ctx);
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
if (!BN_mod_inverse(rr, sig_r, order, ctx)) {
#else
if (!BN_mod_inverse(rr, ecsig->r, order, ctx)) {
#endif
ret = -1;
goto err;
}
sor = BN_CTX_get(ctx);
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
if (!BN_mod_mul(sor, sig_s, rr, order, ctx)) {
#else
if (!BN_mod_mul(sor, ecsig->s, rr, order, ctx)) {
#endif
ret = -1;
goto err;
}
Expand Down Expand Up @@ -218,8 +235,20 @@ bool CECKey::Recover(const uint256& hash, const unsigned char* p64, int rec)
if (rec < 0 || rec >= 3)
return false;
ECDSA_SIG* sig = ECDSA_SIG_new();
BN_bin2bn(&p64[0], 32, sig->r);
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
BIGNUM *sig_r = NULL;
BIGNUM *sig_s = NULL;
if (!(sig_r = BN_bin2bn(&p64[0], 32, nullptr)) ||
!(sig_s = BN_bin2bn(&p64[32], 32, nullptr)) ||
!ECDSA_SIG_set0(sig, sig_r, sig_s)) {
BN_free(sig_r);
BN_free(sig_s);
return false;
}
#else
BN_bin2bn(&p64[0], 32, sig->r);
BN_bin2bn(&p64[32], 32, sig->s);
#endif
bool ret = ECDSA_SIG_recover_key_GFp(pkey, sig, (unsigned char*)&hash, sizeof(hash), rec, 0) == 1;
ECDSA_SIG_free(sig);
return ret;
Expand Down
17 changes: 12 additions & 5 deletions src/qt/paymentrequestplus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,21 @@ bool PaymentRequestPlus::getMerchant(X509_STORE* certStore, QString& merchant) c
std::string data_to_verify; // Everything but the signature
rcopy.SerializeToString(&data_to_verify);

EVP_MD_CTX ctx;
EVP_MD_CTX* ctx;
EVP_PKEY* pubkey = X509_get_pubkey(signing_cert);
EVP_MD_CTX_init(&ctx);
if (!EVP_VerifyInit_ex(&ctx, digestAlgorithm, NULL) ||
!EVP_VerifyUpdate(&ctx, data_to_verify.data(), data_to_verify.size()) ||
!EVP_VerifyFinal(&ctx, (const unsigned char*)paymentRequest.signature().data(), paymentRequest.signature().size(), pubkey)) {
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
ctx = EVP_MD_CTX_new();
#else
EVP_MD_CTX_init(ctx);
#endif
if (!EVP_VerifyInit_ex(ctx, digestAlgorithm, NULL) ||
!EVP_VerifyUpdate(ctx, data_to_verify.data(), data_to_verify.size()) ||
!EVP_VerifyFinal(ctx, (const unsigned char*)paymentRequest.signature().data(), paymentRequest.signature().size(), pubkey)) {
throw SSLVerifyError("Bad signature, invalid PaymentRequest.");
}
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
EVP_MD_CTX_free(ctx);
#endif

// OpenSSL API for getting human printable strings from certs is baroque.
int textlen = X509_NAME_get_text_by_NID(certname, NID_commonName, NULL, 0);
Expand Down
30 changes: 15 additions & 15 deletions src/rpcserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,28 +472,28 @@ class AcceptedConnectionImpl : public AcceptedConnection
void ServiceConnection(AcceptedConnection* conn);

//! Forward declaration required for RPCListen
template <typename Protocol, typename SocketAcceptorService>
static void RPCAcceptHandler(boost::shared_ptr<basic_socket_acceptor<Protocol, SocketAcceptorService> > acceptor,
ssl::context& context,
bool fUseSSL,
boost::shared_ptr<AcceptedConnection> conn,
template <typename Protocol>
static void RPCAcceptHandler(boost::shared_ptr< basic_socket_acceptor<Protocol> > acceptor,
ssl::context& context,
bool fUseSSL,
boost::shared_ptr< AcceptedConnection > conn,
const boost::system::error_code& error);

/**
* Sets up I/O resources to accept and handle a new connection.
*/
template <typename Protocol, typename SocketAcceptorService>
static void RPCListen(boost::shared_ptr<basic_socket_acceptor<Protocol, SocketAcceptorService> > acceptor,
ssl::context& context,
const bool fUseSSL)
template <typename Protocol>
static void RPCListen(boost::shared_ptr< basic_socket_acceptor<Protocol> > acceptor,
ssl::context& context,
const bool fUseSSL)
{
// Accept connection
boost::shared_ptr<AcceptedConnectionImpl<Protocol> > conn(new AcceptedConnectionImpl<Protocol>(acceptor->get_io_service(), context, fUseSSL));

acceptor->async_accept(
conn->sslStream.lowest_layer(),
conn->peer,
boost::bind(&RPCAcceptHandler<Protocol, SocketAcceptorService>,
boost::bind(&RPCAcceptHandler<Protocol>,
acceptor,
boost::ref(context),
fUseSSL,
Expand All @@ -505,11 +505,11 @@ static void RPCListen(boost::shared_ptr<basic_socket_acceptor<Protocol, SocketAc
/**
* Accept and handle incoming connection.
*/
template <typename Protocol, typename SocketAcceptorService>
static void RPCAcceptHandler(boost::shared_ptr<basic_socket_acceptor<Protocol, SocketAcceptorService> > acceptor,
template <typename Protocol>
static void RPCAcceptHandler(boost::shared_ptr< basic_socket_acceptor<Protocol> > acceptor,
ssl::context& context,
const bool fUseSSL,
boost::shared_ptr<AcceptedConnection> conn,
boost::shared_ptr< AcceptedConnection > conn,
const boost::system::error_code& error)
{
// Immediately start accepting new connections, except when we're cancelled or our socket is closed.
Expand Down Expand Up @@ -594,7 +594,7 @@ void StartRPCThreads()

assert(rpc_io_service == NULL);
rpc_io_service = new asio::io_service();
rpc_ssl_context = new ssl::context(*rpc_io_service, ssl::context::sslv23);
rpc_ssl_context = new ssl::context(ssl::context::sslv23);

const bool fUseSSL = GetBoolArg("-rpcssl", false);

Expand All @@ -616,7 +616,7 @@ void StartRPCThreads()
LogPrintf("ThreadRPCServer ERROR: missing server private key file %s\n", pathPKFile.string());

string strCiphers = GetArg("-rpcsslciphers", "TLSv1.2+HIGH:TLSv1+HIGH:!SSLv2:!aNULL:!eNULL:!3DES:@STRENGTH");
SSL_CTX_set_cipher_list(rpc_ssl_context->impl(), strCiphers.c_str());
SSL_CTX_set_cipher_list(rpc_ssl_context->native_handle(), strCiphers.c_str());
}

std::vector<ip::tcp::endpoint> vEndpoints;
Expand Down

0 comments on commit 96b1622

Please sign in to comment.