#include <folly/io/async/AsyncSocketException.h>
#include <folly/io/async/ssl/OpenSSLUtils.h>
#include <unordered_map>
#include <glog/logging.h>
#include <folly/ScopeGuard.h>
#include <folly/portability/Sockets.h>
#include <folly/portability/Unistd.h>
#include <folly/ssl/detail/OpenSSLSession.h>
namespace {
#ifdef OPENSSL_IS_BORINGSSL
static int boringssl_bio_fd_should_retry(int err);
#endif
}
namespace folly {
namespace ssl {
bool OpenSSLUtils::getTLSMasterKey(
const SSL_SESSION* session, MutableByteRange keyOut) { … }
bool OpenSSLUtils::getTLSMasterKey(
const std::shared_ptr<SSLSession> session, MutableByteRange keyOut) { … }
bool OpenSSLUtils::getTLSClientRandom(
const SSL* ssl, MutableByteRange randomOut) { … }
bool OpenSSLUtils::getPeerAddressFromX509StoreCtx(
X509_STORE_CTX* ctx, sockaddr_storage* addrStorage, socklen_t* addrLen) { … }
bool OpenSSLUtils::validatePeerCertNames(
X509* cert, const sockaddr* addr, socklen_t ) { … }
static std::unordered_map<uint16_t, std::string> getOpenSSLCipherNames() { … }
const std::string& OpenSSLUtils::getCipherName(uint16_t cipherCode) { … }
void OpenSSLUtils::setSSLInitialCtx(SSL* ssl, SSL_CTX* ctx) { … }
SSL_CTX* OpenSSLUtils::getSSLInitialCtx(SSL* ssl) { … }
BioMethodUniquePtr OpenSSLUtils::newSocketBioMethod() { … }
bool OpenSSLUtils::setCustomBioReadMethod(
BIO_METHOD* bioMeth, int (*meth)(BIO*, char*, int)) { … }
bool OpenSSLUtils::setCustomBioWriteMethod(
BIO_METHOD* bioMeth, int (*meth)(BIO*, const char*, int)) { … }
int OpenSSLUtils::getBioShouldRetryWrite(int r) { … }
void OpenSSLUtils::setBioAppData(BIO* b, void* ptr) { … }
void* OpenSSLUtils::getBioAppData(BIO* b) { … }
NetworkSocket OpenSSLUtils::getBioFd(BIO* b) { … }
void OpenSSLUtils::setBioFd(BIO* b, NetworkSocket fd, int flags) { … }
std::string OpenSSLUtils::getCommonName(X509* x509) { … }
std::string OpenSSLUtils::encodeALPNString(
const std::vector<std::string>& supportedProtocols) { … }
template <class Callback>
static void forEachX509(BIO* source, Callback cb) { … }
static std::vector<X509NameUniquePtr> getSubjectNamesFromBIO(BIO* b) { … }
std::vector<X509NameUniquePtr> OpenSSLUtils::subjectNamesInPEMFile(
const char* filename) { … }
std::vector<X509NameUniquePtr> OpenSSLUtils::subjectNamesInPEMBuffer(
folly::ByteRange buffer) { … }
}
}
namespace {
#ifdef OPENSSL_IS_BORINGSSL
static int boringssl_bio_fd_non_fatal_error(int err) {
if (
#ifdef EWOULDBLOCK
err == EWOULDBLOCK ||
#endif
#ifdef WSAEWOULDBLOCK
err == WSAEWOULDBLOCK ||
#endif
#ifdef ENOTCONN
err == ENOTCONN ||
#endif
#ifdef EINTR
err == EINTR ||
#endif
#ifdef EAGAIN
err == EAGAIN ||
#endif
#ifdef EPROTO
err == EPROTO ||
#endif
#ifdef EINPROGRESS
err == EINPROGRESS ||
#endif
#ifdef EALREADY
err == EALREADY ||
#endif
0) {
return 1;
}
return 0;
}
#if defined(OPENSSL_WINDOWS)
int boringssl_bio_fd_should_retry(int i) {
if (i == -1) {
return boringssl_bio_fd_non_fatal_error((int)GetLastError());
}
return 0;
}
#else
int boringssl_bio_fd_should_retry(int i) {
if (i == -1) {
return boringssl_bio_fd_non_fatal_error(errno);
}
return 0;
}
#endif
#endif
}