19 #include <glog/logging.h> 21 #include <unordered_map> 29 #ifdef OPENSSL_IS_BORINGSSL 32 static int boringssl_bio_fd_should_retry(
int err);
41 const SSL_SESSION* session,
43 #if FOLLY_OPENSSL_IS_101 || FOLLY_OPENSSL_IS_102 45 session->master_key_length == static_cast<int>(keyOut.
size())) {
46 auto masterKey = session->master_key;
48 masterKey, masterKey + session->master_key_length, keyOut.
begin());
61 #if FOLLY_OPENSSL_IS_101 || FOLLY_OPENSSL_IS_102 62 if ((SSL_version(ssl) >> 8) == TLS1_VERSION_MAJOR && ssl->s3 &&
63 randomOut.
size() == SSL3_RANDOM_SIZE) {
64 auto clientRandom = ssl->s3->client_random;
65 std::copy(clientRandom, clientRandom + SSL3_RANDOM_SIZE, randomOut.
begin());
77 sockaddr_storage* addrStorage,
81 auto sslIdx = SSL_get_ex_data_X509_STORE_CTX_idx();
82 auto ssl =
reinterpret_cast<SSL*
>(X509_STORE_CTX_get_ex_data(ctx, sslIdx));
83 int fd = SSL_get_fd(ssl);
85 LOG(ERROR) <<
"Inexplicably couldn't get fd from SSL";
89 *addrLen =
sizeof(*addrStorage);
90 if (
getpeername(fd, reinterpret_cast<sockaddr*>(addrStorage), addrLen) != 0) {
91 PLOG(ERROR) <<
"Unable to get peer name";
94 CHECK(*addrLen <=
sizeof(*addrStorage));
100 const sockaddr*
addr,
103 auto altNames =
reinterpret_cast<STACK_OF(GENERAL_NAME)*
>(
104 X509_get_ext_d2i(cert, NID_subject_alt_name,
nullptr,
nullptr));
106 if (altNames !=
nullptr) {
107 sk_GENERAL_NAME_pop_free(altNames, GENERAL_NAME_free);
110 if (altNames ==
nullptr) {
111 LOG(
WARNING) <<
"No subjectAltName provided and we only support ip auth";
115 const sockaddr_in* addr4 =
nullptr;
116 const sockaddr_in6* addr6 =
nullptr;
117 if (addr !=
nullptr) {
118 if (addr->sa_family == AF_INET) {
119 addr4 =
reinterpret_cast<const sockaddr_in*
>(
addr);
120 }
else if (addr->sa_family == AF_INET6) {
121 addr6 =
reinterpret_cast<const sockaddr_in6*
>(
addr);
123 LOG(
FATAL) <<
"Unsupported sockaddr family: " << addr->sa_family;
127 for (
int i = 0;
i < sk_GENERAL_NAME_num(altNames);
i++) {
128 auto name = sk_GENERAL_NAME_value(altNames,
i);
129 if ((addr4 !=
nullptr || addr6 !=
nullptr) &&
name->type == GEN_IPADD) {
131 unsigned char const*
const rawIpStr =
name->d.iPAddress->data;
132 size_t const rawIpLen = size_t(
name->d.iPAddress->length);
134 if (rawIpLen == 4 && addr4 !=
nullptr) {
135 if (::memcmp(rawIpStr, &addr4->sin_addr, rawIpLen) == 0) {
138 }
else if (rawIpLen == 16 && addr6 !=
nullptr) {
139 if (::memcmp(rawIpStr, &addr6->sin6_addr, rawIpLen) == 0) {
142 }
else if (rawIpLen != 4 && rawIpLen != 16) {
143 LOG(
WARNING) <<
"Unexpected IP length: " << rawIpLen;
148 LOG(
WARNING) <<
"Unable to match client cert against alt name ip";
154 std::unordered_map<uint16_t, std::string> ret;
155 SSL_CTX* ctx =
nullptr;
158 const SSL_METHOD* meth = SSLv23_server_method();
159 OpenSSL_add_ssl_algorithms();
161 if ((ctx = SSL_CTX_new(meth)) ==
nullptr) {
168 if ((ssl = SSL_new(ctx)) ==
nullptr) {
175 STACK_OF(SSL_CIPHER)* sk = SSL_get_ciphers(ssl);
176 for (
int i = 0;
i < sk_SSL_CIPHER_num(sk);
i++) {
177 const SSL_CIPHER*
c = sk_SSL_CIPHER_value(sk,
i);
178 unsigned long id = SSL_CIPHER_get_id(c);
183 ret[cipherCode] = SSL_CIPHER_get_name(c);
190 static std::unordered_map<uint16_t, std::string> cipherCodeToName(
193 const auto& iter = cipherCodeToName.find(cipherCode);
194 if (iter != cipherCodeToName.end()) {
205 #if !FOLLY_OPENSSL_IS_110 && !defined(OPENSSL_NO_TLSEXT) 210 ssl->initial_ctx = ctx;
217 #if !FOLLY_OPENSSL_IS_110 && !defined(OPENSSL_NO_TLSEXT) 219 return ssl->initial_ctx;
226 BIO_METHOD* newmeth =
nullptr;
227 #if FOLLY_OPENSSL_IS_110 228 if (!(newmeth =
BIO_meth_new(BIO_TYPE_SOCKET,
"socket_bio_method"))) {
231 auto meth =
const_cast<BIO_METHOD*
>(BIO_s_socket());
235 BIO_meth_set_callback_ctrl(newmeth, BIO_meth_get_callback_ctrl(meth));
241 if (!(newmeth = (BIO_METHOD*)OPENSSL_malloc(
sizeof(BIO_METHOD)))) {
244 memcpy(newmeth, BIO_s_socket(),
sizeof(BIO_METHOD));
252 int (*meth)(BIO*,
char*,
int)) {
260 int (*meth)(BIO*,
const char*,
int)) {
268 #ifdef OPENSSL_IS_BORINGSSL 269 ret = boringssl_bio_fd_should_retry(r);
271 ret = BIO_sock_should_retry(r);
277 #ifdef OPENSSL_IS_BORINGSSL 278 BIO_set_callback_arg(b, static_cast<char*>(ptr));
280 BIO_set_app_data(b, ptr);
285 #ifdef OPENSSL_IS_BORINGSSL 286 return BIO_get_callback_arg(b);
288 return BIO_get_app_data(b);
294 int ret = portability::sockets::socket_to_fd((SOCKET)BIO_get_fd(b, fd));
300 return BIO_get_fd(b, fd);
306 SOCKET
socket = portability::sockets::fd_to_socket(fd);
310 int sock = int(socket);
314 BIO_set_fd(b, sock, flags);
318 if (x509 ==
nullptr) {
321 X509_NAME* subject = X509_get_subject_name(x509);
323 cn.resize(ub_common_name);
324 X509_NAME_get_text_by_NID(
325 subject, NID_commonName, const_cast<char*>(cn.data()), ub_common_name);
333 #ifdef OPENSSL_IS_BORINGSSL 335 static int boringssl_bio_fd_non_fatal_error(
int err) {
338 err == EWOULDBLOCK ||
340 #ifdef WSAEWOULDBLOCK
341 err == WSAEWOULDBLOCK ||
356 err == EINPROGRESS ||
367 #if defined(OPENSSL_WINDOWS) 369 int boringssl_bio_fd_should_retry(
int i) {
371 return boringssl_bio_fd_non_fatal_error((
int)GetLastError());
376 #else // !OPENSSL_WINDOWS 378 int boringssl_bio_fd_should_retry(
int i) {
380 return boringssl_bio_fd_non_fatal_error(errno);
384 #endif // OPENSSL_WINDOWS 386 #endif // OEPNSSL_IS_BORINGSSL
static std::string getCommonName(X509 *x509)
static SSL_CTX * getSSLInitialCtx(SSL *ssl)
static bool getTLSMasterKey(const SSL_SESSION *session, MutableByteRange keyOut)
static bool setCustomBioReadMethod(BIO_METHOD *bioMeth, int(*meth)(BIO *, char *, int))
int BIO_meth_set_destroy(BIO_METHOD *biom, int(*destroy)(BIO *))
STACK_OF(X509_OBJECT)*X509_STORE_get0_objects(X509_STORE *store)
static void * getBioAppData(BIO *b)
constexpr size_type size() const
int BIO_meth_set_create(BIO_METHOD *biom, int(*create)(BIO *))
—— Concurrent Priority Queue Implementation ——
static bool getTLSClientRandom(const SSL *ssl, MutableByteRange randomOut)
int BIO_meth_set_write(BIO_METHOD *biom, int(*write)(BIO *, const char *, int))
static void setSSLInitialCtx(SSL *ssl, SSL_CTX *ctx)
int BIO_meth_set_gets(BIO_METHOD *biom, int(*bgets)(BIO *, char *, int))
constexpr std::decay< T >::type copy(T &&value) noexcept(noexcept(typename std::decay< T >::type(std::forward< T >(value))))
static void setBioFd(BIO *b, int fd, int flags)
constexpr auto empty(C const &c) -> decltype(c.empty())
int BIO_meth_set_ctrl(BIO_METHOD *biom, long(*ctrl)(BIO *, int, long, void *))
static int getBioFd(BIO *b, int *fd)
static bool setCustomBioWriteMethod(BIO_METHOD *bioMeth, int(*meth)(BIO *, const char *, int))
static BioMethodUniquePtr newSocketBioMethod()
NetworkSocket socket(int af, int type, int protocol)
static bool validatePeerCertNames(X509 *cert, const sockaddr *addr, socklen_t addrLen)
constexpr Iter begin() const
static int getBioShouldRetryWrite(int ret)
static bool getPeerAddressFromX509StoreCtx(X509_STORE_CTX *ctx, sockaddr_storage *addrStorage, socklen_t *addrLen)
int getpeername(NetworkSocket s, sockaddr *name, socklen_t *namelen)
static void setBioAppData(BIO *b, void *ptr)
BIO_METHOD * BIO_meth_new(int type, const char *name)
std::unique_ptr< BIO_METHOD, BioMethodDeleter > BioMethodUniquePtr
ThreadPoolListHook * addr
static const std::string & getCipherName(uint16_t cipherCode)
int BIO_meth_set_puts(BIO_METHOD *biom, int(*bputs)(BIO *, const char *))
static std::unordered_map< uint16_t, std::string > getOpenSSLCipherNames()
int BIO_meth_set_read(BIO_METHOD *biom, int(*read)(BIO *, char *, int))