From: Kyle Nekritz Date: Mon, 9 Jan 2017 19:51:33 +0000 (-0800) Subject: Set custom ssl bio read method. X-Git-Tag: v2017.03.06.00~114 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=f3c3434c45603ab8a17614f0fc92cbc84ba5beec;p=folly.git Set custom ssl bio read method. Summary: Similar to the bio write method. This should be essentially a no-op, bioRead has the same functionality as the openssl version. This is needed in the next diff. Reviewed By: siyengar Differential Revision: D4325622 fbshipit-source-id: a90b9ec06bee920a1a73551a3ea4c77e1ee0ab08 --- diff --git a/folly/io/async/AsyncSSLSocket.cpp b/folly/io/async/AsyncSSLSocket.cpp index 20cc9630..70a4640e 100644 --- a/folly/io/async/AsyncSSLSocket.cpp +++ b/folly/io/async/AsyncSSLSocket.cpp @@ -176,15 +176,16 @@ void setup_SSL_CTX(SSL_CTX *ctx) { } -BIO_METHOD sslWriteBioMethod; +BIO_METHOD sslBioMethod; -void* initsslWriteBioMethod(void) { - memcpy(&sslWriteBioMethod, BIO_s_socket(), sizeof(sslWriteBioMethod)); +void* initsslBioMethod(void) { + memcpy(&sslBioMethod, BIO_s_socket(), sizeof(sslBioMethod)); // override the bwrite method for MSG_EOR support OpenSSLUtils::setCustomBioWriteMethod( - &sslWriteBioMethod, AsyncSSLSocket::bioWrite); + &sslBioMethod, AsyncSSLSocket::bioWrite); + OpenSSLUtils::setCustomBioReadMethod(&sslBioMethod, AsyncSSLSocket::bioRead); - // Note that the sslWriteBioMethod.type and sslWriteBioMethod.name are not + // Note that the sslBioMethod.type and sslBioMethod.name are not // set here. openssl code seems to be checking ".type == BIO_TYPE_SOCKET" and // then have specific handlings. The sslWriteBioWrite should be compatible // with the one in openssl. @@ -270,8 +271,8 @@ AsyncSSLSocket::~AsyncSSLSocket() { void AsyncSSLSocket::init() { // Do this here to ensure we initialize this once before any use of // AsyncSSLSocket instances and not as part of library load. - static const auto sslWriteBioMethodInitializer = initsslWriteBioMethod(); - (void)sslWriteBioMethodInitializer; + static const auto sslBioMethodInitializer = initsslBioMethod(); + (void)sslBioMethodInitializer; setup_SSL_CTX(ctx_->getSSLCtx()); } @@ -670,15 +671,15 @@ void AsyncSSLSocket::applyVerificationOptions(SSL * ssl) { } bool AsyncSSLSocket::setupSSLBio() { - auto wb = BIO_new(&sslWriteBioMethod); + auto sslBio = BIO_new(&sslBioMethod); - if (!wb) { + if (!sslBio) { return false; } - OpenSSLUtils::setBioAppData(wb, this); - OpenSSLUtils::setBioFd(wb, fd_, BIO_NOCLOSE); - SSL_set_bio(ssl_, wb, wb); + OpenSSLUtils::setBioAppData(sslBio, this); + OpenSSLUtils::setBioFd(sslBio, fd_, BIO_NOCLOSE); + SSL_set_bio(ssl_, sslBio, sslBio); return true; } @@ -1605,6 +1606,18 @@ int AsyncSSLSocket::bioWrite(BIO* b, const char* in, int inl) { return int(result.writeReturn); } +int AsyncSSLSocket::bioRead(BIO* b, char* out, int outl) { + if (!out) { + return 0; + } + auto result = recv(OpenSSLUtils::getBioFd(b, nullptr), out, outl, 0); + BIO_clear_retry_flags(b); + if (result <= 0 && OpenSSLUtils::getBioShouldRetryWrite(result)) { + BIO_set_retry_read(b); + } + return result; +} + int AsyncSSLSocket::sslVerifyCallback( int preverifyOk, X509_STORE_CTX* x509Ctx) { diff --git a/folly/io/async/AsyncSSLSocket.h b/folly/io/async/AsyncSSLSocket.h index c8406a92..0d106214 100644 --- a/folly/io/async/AsyncSSLSocket.h +++ b/folly/io/async/AsyncSSLSocket.h @@ -589,6 +589,7 @@ class AsyncSSLSocket : public virtual AsyncSocket { static int getSSLExDataIndex(); static AsyncSSLSocket* getFromSSL(const SSL *ssl); static int bioWrite(BIO* b, const char* in, int inl); + static int bioRead(BIO* b, char* out, int outl); void resetClientHelloParsing(SSL *ssl); static void clientHelloParsingCallback(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg);