2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/io/async/AsyncSSLSocket.h>
19 #include <folly/io/async/EventBase.h>
20 #include <folly/portability/Sockets.h>
22 #include <boost/noncopyable.hpp>
25 #include <openssl/err.h>
26 #include <openssl/asn1.h>
27 #include <openssl/ssl.h>
28 #include <sys/types.h>
31 #include <folly/Bits.h>
32 #include <folly/SocketAddress.h>
33 #include <folly/SpinLock.h>
34 #include <folly/io/Cursor.h>
35 #include <folly/io/IOBuf.h>
36 #include <folly/portability/OpenSSL.h>
38 using folly::SocketAddress;
39 using folly::SSLContext;
41 using std::shared_ptr;
45 using folly::SpinLock;
46 using folly::SpinLockGuard;
47 using folly::io::Cursor;
48 using std::unique_ptr;
52 using folly::AsyncSocket;
53 using folly::AsyncSocketException;
54 using folly::AsyncSSLSocket;
55 using folly::Optional;
56 using folly::SSLContext;
57 // For OpenSSL portability API
58 using namespace folly::ssl;
59 using folly::ssl::OpenSSLUtils;
62 // We have one single dummy SSL context so that we can implement attach
63 // and detach methods in a thread safe fashion without modifying opnessl.
64 static SSLContext *dummyCtx = nullptr;
65 static SpinLock dummyCtxLock;
67 // If given min write size is less than this, buffer will be allocated on
68 // stack, otherwise it is allocated on heap
69 const size_t MAX_STACK_BUF_SIZE = 2048;
71 // This converts "illegal" shutdowns into ZERO_RETURN
72 inline bool zero_return(int error, int rc) {
73 return (error == SSL_ERROR_ZERO_RETURN || (rc == 0 && errno == 0));
76 class AsyncSSLSocketConnector: public AsyncSocket::ConnectCallback,
77 public AsyncSSLSocket::HandshakeCB {
80 AsyncSSLSocket *sslSocket_;
81 AsyncSSLSocket::ConnectCallback *callback_;
86 ~AsyncSSLSocketConnector() override {}
89 AsyncSSLSocketConnector(AsyncSSLSocket *sslSocket,
90 AsyncSocket::ConnectCallback *callback,
92 sslSocket_(sslSocket),
95 startTime_(std::chrono::duration_cast<std::chrono::milliseconds>(
96 std::chrono::steady_clock::now().time_since_epoch()).count()) {
99 void connectSuccess() noexcept override {
100 VLOG(7) << "client socket connected";
102 int64_t timeoutLeft = 0;
104 auto curTime = std::chrono::duration_cast<std::chrono::milliseconds>(
105 std::chrono::steady_clock::now().time_since_epoch()).count();
107 timeoutLeft = timeout_ - (curTime - startTime_);
108 if (timeoutLeft <= 0) {
109 AsyncSocketException ex(
110 AsyncSocketException::TIMED_OUT,
111 folly::sformat("SSL connect timed out after {}ms", timeout_));
117 sslSocket_->sslConn(this, std::chrono::milliseconds(timeoutLeft));
120 void connectErr(const AsyncSocketException& ex) noexcept override {
121 VLOG(1) << "TCP connect failed: " << ex.what();
126 void handshakeSuc(AsyncSSLSocket* /* sock */) noexcept override {
127 VLOG(7) << "client handshake success";
129 callback_->connectSuccess();
134 void handshakeErr(AsyncSSLSocket* /* socket */,
135 const AsyncSocketException& ex) noexcept override {
136 VLOG(1) << "client handshakeErr: " << ex.what();
141 void fail(const AsyncSocketException &ex) {
142 // fail is a noop if called twice
144 AsyncSSLSocket::ConnectCallback *cb = callback_;
148 sslSocket_->closeNow();
149 // closeNow can call handshakeErr if it hasn't been called already.
150 // So this may have been deleted, no member variable access beyond this
152 // Note that closeNow may invoke writeError callbacks if the socket had
153 // write data pending connection completion.
158 void setup_SSL_CTX(SSL_CTX *ctx) {
159 #ifdef SSL_MODE_RELEASE_BUFFERS
160 SSL_CTX_set_mode(ctx,
161 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
162 SSL_MODE_ENABLE_PARTIAL_WRITE
163 | SSL_MODE_RELEASE_BUFFERS
166 SSL_CTX_set_mode(ctx,
167 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
168 SSL_MODE_ENABLE_PARTIAL_WRITE
171 // SSL_CTX_set_mode is a Macro
172 #ifdef SSL_MODE_WRITE_IOVEC
173 SSL_CTX_set_mode(ctx,
174 SSL_CTX_get_mode(ctx)
175 | SSL_MODE_WRITE_IOVEC);
180 BIO_METHOD sslBioMethod;
182 void* initsslBioMethod(void) {
183 memcpy(&sslBioMethod, BIO_s_socket(), sizeof(sslBioMethod));
184 // override the bwrite method for MSG_EOR support
185 OpenSSLUtils::setCustomBioWriteMethod(
186 &sslBioMethod, AsyncSSLSocket::bioWrite);
187 OpenSSLUtils::setCustomBioReadMethod(&sslBioMethod, AsyncSSLSocket::bioRead);
189 // Note that the sslBioMethod.type and sslBioMethod.name are not
190 // set here. openssl code seems to be checking ".type == BIO_TYPE_SOCKET" and
191 // then have specific handlings. The sslWriteBioWrite should be compatible
192 // with the one in openssl.
194 // Return something here to enable AsyncSSLSocket to call this method using
195 // a function-scoped static.
199 } // anonymous namespace
204 * Create a client AsyncSSLSocket
206 AsyncSSLSocket::AsyncSSLSocket(const shared_ptr<SSLContext> &ctx,
207 EventBase* evb, bool deferSecurityNegotiation) :
210 handshakeTimeout_(this, evb),
211 connectionTimeout_(this, evb) {
213 if (deferSecurityNegotiation) {
214 sslState_ = STATE_UNENCRYPTED;
219 * Create a server/client AsyncSSLSocket
221 AsyncSSLSocket::AsyncSSLSocket(
222 const shared_ptr<SSLContext>& ctx,
226 bool deferSecurityNegotiation)
227 : AsyncSocket(evb, fd),
230 handshakeTimeout_(this, evb),
231 connectionTimeout_(this, evb) {
232 noTransparentTls_ = true;
235 SSL_CTX_set_info_callback(
236 ctx_->getSSLCtx(), AsyncSSLSocket::sslInfoCallback);
238 if (deferSecurityNegotiation) {
239 sslState_ = STATE_UNENCRYPTED;
243 AsyncSSLSocket::AsyncSSLSocket(
244 const shared_ptr<SSLContext>& ctx,
245 AsyncSocket::UniquePtr oldAsyncSocket,
247 bool deferSecurityNegotiation)
248 : AsyncSocket(std::move(oldAsyncSocket)),
251 handshakeTimeout_(this, AsyncSocket::getEventBase()),
252 connectionTimeout_(this, AsyncSocket::getEventBase()) {
253 noTransparentTls_ = true;
256 SSL_CTX_set_info_callback(ctx_->getSSLCtx(),
257 AsyncSSLSocket::sslInfoCallback);
259 if (deferSecurityNegotiation) {
260 sslState_ = STATE_UNENCRYPTED;
264 #if FOLLY_OPENSSL_HAS_SNI
266 * Create a client AsyncSSLSocket and allow tlsext_hostname
267 * to be sent in Client Hello.
269 AsyncSSLSocket::AsyncSSLSocket(const shared_ptr<SSLContext> &ctx,
271 const std::string& serverName,
272 bool deferSecurityNegotiation) :
273 AsyncSSLSocket(ctx, evb, deferSecurityNegotiation) {
274 tlsextHostname_ = serverName;
278 * Create a client AsyncSSLSocket from an already connected fd
279 * and allow tlsext_hostname to be sent in Client Hello.
281 AsyncSSLSocket::AsyncSSLSocket(
282 const shared_ptr<SSLContext>& ctx,
285 const std::string& serverName,
286 bool deferSecurityNegotiation)
287 : AsyncSSLSocket(ctx, evb, fd, false, deferSecurityNegotiation) {
288 tlsextHostname_ = serverName;
290 #endif // FOLLY_OPENSSL_HAS_SNI
292 AsyncSSLSocket::~AsyncSSLSocket() {
293 VLOG(3) << "actual destruction of AsyncSSLSocket(this=" << this
294 << ", evb=" << eventBase_ << ", fd=" << fd_
295 << ", state=" << int(state_) << ", sslState="
296 << sslState_ << ", events=" << eventFlags_ << ")";
299 void AsyncSSLSocket::init() {
300 // Do this here to ensure we initialize this once before any use of
301 // AsyncSSLSocket instances and not as part of library load.
302 static const auto sslBioMethodInitializer = initsslBioMethod();
303 (void)sslBioMethodInitializer;
305 setup_SSL_CTX(ctx_->getSSLCtx());
308 void AsyncSSLSocket::closeNow() {
309 // Close the SSL connection.
310 if (ssl_ != nullptr && fd_ != -1) {
311 int rc = SSL_shutdown(ssl_);
313 rc = SSL_shutdown(ssl_);
320 if (sslSession_ != nullptr) {
321 SSL_SESSION_free(sslSession_);
322 sslSession_ = nullptr;
325 sslState_ = STATE_CLOSED;
327 if (handshakeTimeout_.isScheduled()) {
328 handshakeTimeout_.cancelTimeout();
331 DestructorGuard dg(this);
334 AsyncSocketException(
335 AsyncSocketException::END_OF_FILE,
336 "SSL connection closed locally"));
338 if (ssl_ != nullptr) {
344 AsyncSocket::closeNow();
347 void AsyncSSLSocket::shutdownWrite() {
348 // SSL sockets do not support half-shutdown, so just perform a full shutdown.
350 // (Performing a full shutdown here is more desirable than doing nothing at
351 // all. The purpose of shutdownWrite() is normally to notify the other end
352 // of the connection that no more data will be sent. If we do nothing, the
353 // other end will never know that no more data is coming, and this may result
354 // in protocol deadlock.)
358 void AsyncSSLSocket::shutdownWriteNow() {
362 bool AsyncSSLSocket::good() const {
363 return (AsyncSocket::good() &&
364 (sslState_ == STATE_ACCEPTING || sslState_ == STATE_CONNECTING ||
365 sslState_ == STATE_ESTABLISHED || sslState_ == STATE_UNENCRYPTED));
368 // The TAsyncTransport definition of 'good' states that the transport is
369 // ready to perform reads and writes, so sslState_ == UNINIT must report !good.
370 // connecting can be true when the sslState_ == UNINIT because the AsyncSocket
371 // is connected but we haven't initiated the call to SSL_connect.
372 bool AsyncSSLSocket::connecting() const {
374 (AsyncSocket::connecting() ||
375 (AsyncSocket::good() && (sslState_ == STATE_UNINIT ||
376 sslState_ == STATE_CONNECTING))));
379 std::string AsyncSSLSocket::getApplicationProtocol() noexcept {
380 const unsigned char* protoName = nullptr;
381 unsigned protoLength;
382 if (getSelectedNextProtocolNoThrow(&protoName, &protoLength)) {
383 return std::string(reinterpret_cast<const char*>(protoName), protoLength);
388 void AsyncSSLSocket::setEorTracking(bool track) {
389 if (isEorTrackingEnabled() != track) {
390 AsyncSocket::setEorTracking(track);
392 minEorRawByteNo_ = 0;
396 size_t AsyncSSLSocket::getRawBytesWritten() const {
397 // The bio(s) in the write path are in a chain
398 // each bio flushes to the next and finally written into the socket
399 // to get the rawBytesWritten on the socket,
400 // get the write bytes of the last bio
402 if (!ssl_ || !(b = SSL_get_wbio(ssl_))) {
405 BIO* next = BIO_next(b);
406 while (next != NULL) {
411 return BIO_number_written(b);
414 size_t AsyncSSLSocket::getRawBytesReceived() const {
416 if (!ssl_ || !(b = SSL_get_rbio(ssl_))) {
420 return BIO_number_read(b);
424 void AsyncSSLSocket::invalidState(HandshakeCB* callback) {
425 LOG(ERROR) << "AsyncSSLSocket(this=" << this << ", fd=" << fd_
426 << ", state=" << int(state_) << ", sslState=" << sslState_ << ", "
427 << "events=" << eventFlags_ << ", server=" << short(server_)
428 << "): " << "sslAccept/Connect() called in invalid "
429 << "state, handshake callback " << handshakeCallback_
430 << ", new callback " << callback;
431 assert(!handshakeTimeout_.isScheduled());
432 sslState_ = STATE_ERROR;
434 AsyncSocketException ex(AsyncSocketException::INVALID_STATE,
435 "sslAccept() called with socket in invalid state");
437 handshakeEndTime_ = std::chrono::steady_clock::now();
439 callback->handshakeErr(this, ex);
442 failHandshake(__func__, ex);
445 void AsyncSSLSocket::sslAccept(
446 HandshakeCB* callback,
447 std::chrono::milliseconds timeout,
448 const SSLContext::SSLVerifyPeerEnum& verifyPeer) {
449 DestructorGuard dg(this);
450 assert(eventBase_->isInEventBaseThread());
451 verifyPeer_ = verifyPeer;
453 // Make sure we're in the uninitialized state
454 if (!server_ || (sslState_ != STATE_UNINIT &&
455 sslState_ != STATE_UNENCRYPTED) ||
456 handshakeCallback_ != nullptr) {
457 return invalidState(callback);
460 // Cache local and remote socket addresses to keep them available
461 // after socket file descriptor is closed.
462 if (cacheAddrOnFailure_ && -1 != getFd()) {
463 cacheLocalPeerAddr();
466 handshakeStartTime_ = std::chrono::steady_clock::now();
467 // Make end time at least >= start time.
468 handshakeEndTime_ = handshakeStartTime_;
470 sslState_ = STATE_ACCEPTING;
471 handshakeCallback_ = callback;
473 if (timeout > std::chrono::milliseconds::zero()) {
474 handshakeTimeout_.scheduleTimeout(timeout);
477 /* register for a read operation (waiting for CLIENT HELLO) */
478 updateEventRegistration(EventHandler::READ, EventHandler::WRITE);
480 checkForImmediateRead();
483 #if OPENSSL_VERSION_NUMBER >= 0x009080bfL
484 void AsyncSSLSocket::attachSSLContext(
485 const std::shared_ptr<SSLContext>& ctx) {
487 // Check to ensure we are in client mode. Changing a server's ssl
488 // context doesn't make sense since clients of that server would likely
489 // become confused when the server's context changes.
493 DCHECK(ctx->getSSLCtx());
496 // It's possible this could be attached before ssl_ is set up
501 // In order to call attachSSLContext, detachSSLContext must have been
502 // previously called.
503 // We need to update the initial_ctx if necessary
504 auto sslCtx = ctx->getSSLCtx();
505 SSL_CTX_up_ref(sslCtx);
506 #ifndef OPENSSL_NO_TLSEXT
507 // note that detachSSLContext has already freed ssl_->initial_ctx
508 ssl_->initial_ctx = sslCtx;
510 // Detach sets the socket's context to the dummy context. Thus we must acquire
512 SpinLockGuard guard(dummyCtxLock);
513 SSL_set_SSL_CTX(ssl_, sslCtx);
516 void AsyncSSLSocket::detachSSLContext() {
519 // It's possible for this to be called before ssl_ has been
524 // Detach the initial_ctx as well. Internally w/ OPENSSL_NO_TLSEXT
525 // it is used for session info. It will be reattached in attachSSLContext
526 #ifndef OPENSSL_NO_TLSEXT
527 if (ssl_->initial_ctx) {
528 SSL_CTX_free(ssl_->initial_ctx);
529 ssl_->initial_ctx = nullptr;
532 SpinLockGuard guard(dummyCtxLock);
533 if (nullptr == dummyCtx) {
534 // We need to lazily initialize the dummy context so we don't
535 // accidentally override any programmatic settings to openssl
536 dummyCtx = new SSLContext;
538 // We must remove this socket's references to its context right now
539 // since this socket could get passed to any thread. If the context has
540 // had its locking disabled, just doing a set in attachSSLContext()
541 // would not be thread safe.
542 SSL_set_SSL_CTX(ssl_, dummyCtx->getSSLCtx());
546 #if FOLLY_OPENSSL_HAS_SNI
547 void AsyncSSLSocket::switchServerSSLContext(
548 const std::shared_ptr<SSLContext>& handshakeCtx) {
550 if (sslState_ != STATE_ACCEPTING) {
551 // We log it here and allow the switch.
552 // It should not affect our re-negotiation support (which
553 // is not supported now).
554 VLOG(6) << "fd=" << getFd()
555 << " renegotation detected when switching SSL_CTX";
558 setup_SSL_CTX(handshakeCtx->getSSLCtx());
559 SSL_CTX_set_info_callback(handshakeCtx->getSSLCtx(),
560 AsyncSSLSocket::sslInfoCallback);
561 handshakeCtx_ = handshakeCtx;
562 SSL_set_SSL_CTX(ssl_, handshakeCtx->getSSLCtx());
565 bool AsyncSSLSocket::isServerNameMatch() const {
572 SSL_SESSION *ss = SSL_get_session(ssl_);
577 if(!ss->tlsext_hostname) {
580 return (tlsextHostname_.compare(ss->tlsext_hostname) ? false : true);
583 void AsyncSSLSocket::setServerName(std::string serverName) noexcept {
584 tlsextHostname_ = std::move(serverName);
587 #endif // FOLLY_OPENSSL_HAS_SNI
589 void AsyncSSLSocket::timeoutExpired(
590 std::chrono::milliseconds timeout) noexcept {
591 if (state_ == StateEnum::ESTABLISHED &&
592 (sslState_ == STATE_CACHE_LOOKUP || sslState_ == STATE_ASYNC_PENDING)) {
593 sslState_ = STATE_ERROR;
594 // We are expecting a callback in restartSSLAccept. The cache lookup
595 // and rsa-call necessarily have pointers to this ssl socket, so delay
596 // the cleanup until he calls us back.
597 } else if (state_ == StateEnum::CONNECTING) {
598 assert(sslState_ == STATE_CONNECTING);
599 DestructorGuard dg(this);
600 AsyncSocketException ex(AsyncSocketException::TIMED_OUT,
601 "Fallback connect timed out during TFO");
602 failHandshake(__func__, ex);
604 assert(state_ == StateEnum::ESTABLISHED &&
605 (sslState_ == STATE_CONNECTING || sslState_ == STATE_ACCEPTING));
606 DestructorGuard dg(this);
607 AsyncSocketException ex(
608 AsyncSocketException::TIMED_OUT,
610 "SSL {} timed out after {}ms",
611 (sslState_ == STATE_CONNECTING) ? "connect" : "accept",
613 failHandshake(__func__, ex);
617 int AsyncSSLSocket::getSSLExDataIndex() {
618 static auto index = SSL_get_ex_new_index(
619 0, (void*)"AsyncSSLSocket data index", nullptr, nullptr, nullptr);
623 AsyncSSLSocket* AsyncSSLSocket::getFromSSL(const SSL *ssl) {
624 return static_cast<AsyncSSLSocket *>(SSL_get_ex_data(ssl,
625 getSSLExDataIndex()));
628 void AsyncSSLSocket::failHandshake(const char* /* fn */,
629 const AsyncSocketException& ex) {
631 if (handshakeTimeout_.isScheduled()) {
632 handshakeTimeout_.cancelTimeout();
634 invokeHandshakeErr(ex);
638 void AsyncSSLSocket::invokeHandshakeErr(const AsyncSocketException& ex) {
639 handshakeEndTime_ = std::chrono::steady_clock::now();
640 if (handshakeCallback_ != nullptr) {
641 HandshakeCB* callback = handshakeCallback_;
642 handshakeCallback_ = nullptr;
643 callback->handshakeErr(this, ex);
647 void AsyncSSLSocket::invokeHandshakeCB() {
648 handshakeEndTime_ = std::chrono::steady_clock::now();
649 if (handshakeTimeout_.isScheduled()) {
650 handshakeTimeout_.cancelTimeout();
652 if (handshakeCallback_) {
653 HandshakeCB* callback = handshakeCallback_;
654 handshakeCallback_ = nullptr;
655 callback->handshakeSuc(this);
659 void AsyncSSLSocket::cacheLocalPeerAddr() {
660 SocketAddress address;
662 getLocalAddress(&address);
663 getPeerAddress(&address);
664 } catch (const std::system_error& e) {
665 // The handle can be still valid while the connection is already closed.
666 if (e.code() != std::error_code(ENOTCONN, std::system_category())) {
672 void AsyncSSLSocket::connect(
673 ConnectCallback* callback,
674 const folly::SocketAddress& address,
676 const OptionMap& options,
677 const folly::SocketAddress& bindAddr) noexcept {
678 auto timeoutChrono = std::chrono::milliseconds(timeout);
679 connect(callback, address, timeoutChrono, timeoutChrono, options, bindAddr);
682 void AsyncSSLSocket::connect(
683 ConnectCallback* callback,
684 const folly::SocketAddress& address,
685 std::chrono::milliseconds connectTimeout,
686 std::chrono::milliseconds totalConnectTimeout,
687 const OptionMap& options,
688 const folly::SocketAddress& bindAddr) noexcept {
690 assert(state_ == StateEnum::UNINIT);
691 assert(sslState_ == STATE_UNINIT);
692 noTransparentTls_ = true;
693 totalConnectTimeout_ = totalConnectTimeout;
694 AsyncSSLSocketConnector* connector =
695 new AsyncSSLSocketConnector(this, callback, totalConnectTimeout.count());
696 AsyncSocket::connect(
697 connector, address, connectTimeout.count(), options, bindAddr);
700 bool AsyncSSLSocket::needsPeerVerification() const {
701 if (verifyPeer_ == SSLContext::SSLVerifyPeerEnum::USE_CTX) {
702 return ctx_->needsPeerVerification();
705 verifyPeer_ == SSLContext::SSLVerifyPeerEnum::VERIFY ||
706 verifyPeer_ == SSLContext::SSLVerifyPeerEnum::VERIFY_REQ_CLIENT_CERT);
709 void AsyncSSLSocket::applyVerificationOptions(SSL * ssl) {
710 // apply the settings specified in verifyPeer_
711 if (verifyPeer_ == SSLContext::SSLVerifyPeerEnum::USE_CTX) {
712 if(ctx_->needsPeerVerification()) {
713 SSL_set_verify(ssl, ctx_->getVerificationMode(),
714 AsyncSSLSocket::sslVerifyCallback);
717 if (verifyPeer_ == SSLContext::SSLVerifyPeerEnum::VERIFY ||
718 verifyPeer_ == SSLContext::SSLVerifyPeerEnum::VERIFY_REQ_CLIENT_CERT) {
719 SSL_set_verify(ssl, SSLContext::getVerificationMode(verifyPeer_),
720 AsyncSSLSocket::sslVerifyCallback);
725 bool AsyncSSLSocket::setupSSLBio() {
726 auto sslBio = BIO_new(&sslBioMethod);
732 OpenSSLUtils::setBioAppData(sslBio, this);
733 OpenSSLUtils::setBioFd(sslBio, fd_, BIO_NOCLOSE);
734 SSL_set_bio(ssl_, sslBio, sslBio);
738 void AsyncSSLSocket::sslConn(
739 HandshakeCB* callback,
740 std::chrono::milliseconds timeout,
741 const SSLContext::SSLVerifyPeerEnum& verifyPeer) {
742 DestructorGuard dg(this);
743 assert(eventBase_->isInEventBaseThread());
745 // Cache local and remote socket addresses to keep them available
746 // after socket file descriptor is closed.
747 if (cacheAddrOnFailure_ && -1 != getFd()) {
748 cacheLocalPeerAddr();
751 verifyPeer_ = verifyPeer;
753 // Make sure we're in the uninitialized state
754 if (server_ || (sslState_ != STATE_UNINIT && sslState_ !=
755 STATE_UNENCRYPTED) ||
756 handshakeCallback_ != nullptr) {
757 return invalidState(callback);
760 sslState_ = STATE_CONNECTING;
761 handshakeCallback_ = callback;
764 ssl_ = ctx_->createSSL();
765 } catch (std::exception &e) {
766 sslState_ = STATE_ERROR;
767 AsyncSocketException ex(AsyncSocketException::INTERNAL_ERROR,
768 "error calling SSLContext::createSSL()");
769 LOG(ERROR) << "AsyncSSLSocket::sslConn(this=" << this << ", fd="
770 << fd_ << "): " << e.what();
771 return failHandshake(__func__, ex);
774 if (!setupSSLBio()) {
775 sslState_ = STATE_ERROR;
776 AsyncSocketException ex(
777 AsyncSocketException::INTERNAL_ERROR, "error creating SSL bio");
778 return failHandshake(__func__, ex);
781 applyVerificationOptions(ssl_);
783 if (sslSession_ != nullptr) {
784 sessionResumptionAttempted_ = true;
785 SSL_set_session(ssl_, sslSession_);
786 SSL_SESSION_free(sslSession_);
787 sslSession_ = nullptr;
789 #if FOLLY_OPENSSL_HAS_SNI
790 if (tlsextHostname_.size()) {
791 SSL_set_tlsext_host_name(ssl_, tlsextHostname_.c_str());
795 SSL_set_ex_data(ssl_, getSSLExDataIndex(), this);
797 handshakeConnectTimeout_ = timeout;
801 // This could be called multiple times, during normal ssl connections
802 // and after TFO fallback.
803 void AsyncSSLSocket::startSSLConnect() {
804 handshakeStartTime_ = std::chrono::steady_clock::now();
805 // Make end time at least >= start time.
806 handshakeEndTime_ = handshakeStartTime_;
807 if (handshakeConnectTimeout_ > std::chrono::milliseconds::zero()) {
808 handshakeTimeout_.scheduleTimeout(handshakeConnectTimeout_);
813 SSL_SESSION *AsyncSSLSocket::getSSLSession() {
814 if (ssl_ != nullptr && sslState_ == STATE_ESTABLISHED) {
815 return SSL_get1_session(ssl_);
821 const SSL* AsyncSSLSocket::getSSL() const {
825 void AsyncSSLSocket::setSSLSession(SSL_SESSION *session, bool takeOwnership) {
826 sslSession_ = session;
827 if (!takeOwnership && session != nullptr) {
828 // Increment the reference count
829 // This API exists in BoringSSL and OpenSSL 1.1.0
830 SSL_SESSION_up_ref(session);
834 void AsyncSSLSocket::getSelectedNextProtocol(
835 const unsigned char** protoName,
837 SSLContext::NextProtocolType* protoType) const {
838 if (!getSelectedNextProtocolNoThrow(protoName, protoLen, protoType)) {
839 throw AsyncSocketException(AsyncSocketException::NOT_SUPPORTED,
840 "NPN not supported");
844 bool AsyncSSLSocket::getSelectedNextProtocolNoThrow(
845 const unsigned char** protoName,
847 SSLContext::NextProtocolType* protoType) const {
848 *protoName = nullptr;
850 #if FOLLY_OPENSSL_HAS_ALPN
851 SSL_get0_alpn_selected(ssl_, protoName, protoLen);
854 *protoType = SSLContext::NextProtocolType::ALPN;
859 #ifdef OPENSSL_NPN_NEGOTIATED
860 SSL_get0_next_proto_negotiated(ssl_, protoName, protoLen);
862 *protoType = SSLContext::NextProtocolType::NPN;
871 bool AsyncSSLSocket::getSSLSessionReused() const {
872 if (ssl_ != nullptr && sslState_ == STATE_ESTABLISHED) {
873 return SSL_session_reused(ssl_);
878 const char *AsyncSSLSocket::getNegotiatedCipherName() const {
879 return (ssl_ != nullptr) ? SSL_get_cipher_name(ssl_) : nullptr;
883 const char* AsyncSSLSocket::getSSLServerNameFromSSL(SSL* ssl) {
884 if (ssl == nullptr) {
887 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
888 return SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
894 const char *AsyncSSLSocket::getSSLServerName() const {
895 #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
896 return getSSLServerNameFromSSL(ssl_);
898 throw AsyncSocketException(AsyncSocketException::NOT_SUPPORTED,
899 "SNI not supported");
903 const char *AsyncSSLSocket::getSSLServerNameNoThrow() const {
904 return getSSLServerNameFromSSL(ssl_);
907 int AsyncSSLSocket::getSSLVersion() const {
908 return (ssl_ != nullptr) ? SSL_version(ssl_) : 0;
911 const char *AsyncSSLSocket::getSSLCertSigAlgName() const {
912 X509 *cert = (ssl_ != nullptr) ? SSL_get_certificate(ssl_) : nullptr;
914 int nid = OBJ_obj2nid(cert->sig_alg->algorithm);
915 return OBJ_nid2ln(nid);
920 int AsyncSSLSocket::getSSLCertSize() const {
922 X509 *cert = (ssl_ != nullptr) ? SSL_get_certificate(ssl_) : nullptr;
924 EVP_PKEY *key = X509_get_pubkey(cert);
925 certSize = EVP_PKEY_bits(key);
931 const X509* AsyncSSLSocket::getSelfCert() const {
932 return (ssl_ != nullptr) ? SSL_get_certificate(ssl_) : nullptr;
935 bool AsyncSSLSocket::willBlock(int ret,
937 unsigned long* errErrorOut) noexcept {
939 int error = *sslErrorOut = SSL_get_error(ssl_, ret);
940 if (error == SSL_ERROR_WANT_READ) {
941 // Register for read event if not already.
942 updateEventRegistration(EventHandler::READ, EventHandler::WRITE);
944 } else if (error == SSL_ERROR_WANT_WRITE) {
945 VLOG(3) << "AsyncSSLSocket(fd=" << fd_
946 << ", state=" << int(state_) << ", sslState="
947 << sslState_ << ", events=" << eventFlags_ << "): "
948 << "SSL_ERROR_WANT_WRITE";
949 // Register for write event if not already.
950 updateEventRegistration(EventHandler::WRITE, EventHandler::READ);
952 #ifdef SSL_ERROR_WANT_SESS_CACHE_LOOKUP
953 } else if (error == SSL_ERROR_WANT_SESS_CACHE_LOOKUP) {
954 // We will block but we can't register our own socket. The callback that
955 // triggered this code will re-call handleAccept at the appropriate time.
957 // We can only get here if the linked libssl.so has support for this feature
958 // as well, otherwise SSL_get_error cannot return our error code.
959 sslState_ = STATE_CACHE_LOOKUP;
961 // Unregister for all events while blocked here
962 updateEventRegistration(EventHandler::NONE,
963 EventHandler::READ | EventHandler::WRITE);
965 // The timeout (if set) keeps running here
969 #ifdef SSL_ERROR_WANT_RSA_ASYNC_PENDING
970 || error == SSL_ERROR_WANT_RSA_ASYNC_PENDING
972 #ifdef SSL_ERROR_WANT_ECDSA_ASYNC_PENDING
973 || error == SSL_ERROR_WANT_ECDSA_ASYNC_PENDING
976 // Our custom openssl function has kicked off an async request to do
977 // rsa/ecdsa private key operation. When that call returns, a callback will
978 // be invoked that will re-call handleAccept.
979 sslState_ = STATE_ASYNC_PENDING;
981 // Unregister for all events while blocked here
982 updateEventRegistration(
984 EventHandler::READ | EventHandler::WRITE
987 // The timeout (if set) keeps running here
990 unsigned long lastError = *errErrorOut = ERR_get_error();
991 VLOG(6) << "AsyncSSLSocket(fd=" << fd_ << ", "
992 << "state=" << state_ << ", "
993 << "sslState=" << sslState_ << ", "
994 << "events=" << std::hex << eventFlags_ << "): "
995 << "SSL error: " << error << ", "
996 << "errno: " << errno << ", "
997 << "ret: " << ret << ", "
998 << "read: " << BIO_number_read(SSL_get_rbio(ssl_)) << ", "
999 << "written: " << BIO_number_written(SSL_get_wbio(ssl_)) << ", "
1000 << "func: " << ERR_func_error_string(lastError) << ", "
1001 << "reason: " << ERR_reason_error_string(lastError);
1006 void AsyncSSLSocket::checkForImmediateRead() noexcept {
1007 // openssl may have buffered data that it read from the socket already.
1008 // In this case we have to process it immediately, rather than waiting for
1009 // the socket to become readable again.
1010 if (ssl_ != nullptr && SSL_pending(ssl_) > 0) {
1011 AsyncSocket::handleRead();
1013 AsyncSocket::checkForImmediateRead();
1018 AsyncSSLSocket::restartSSLAccept()
1020 VLOG(3) << "AsyncSSLSocket::restartSSLAccept() this=" << this
1021 << ", fd=" << fd_ << ", state=" << int(state_) << ", "
1022 << "sslState=" << sslState_ << ", events=" << eventFlags_;
1023 DestructorGuard dg(this);
1025 sslState_ == STATE_CACHE_LOOKUP ||
1026 sslState_ == STATE_ASYNC_PENDING ||
1027 sslState_ == STATE_ERROR ||
1028 sslState_ == STATE_CLOSED);
1029 if (sslState_ == STATE_CLOSED) {
1030 // I sure hope whoever closed this socket didn't delete it already,
1031 // but this is not strictly speaking an error
1034 if (sslState_ == STATE_ERROR) {
1035 // go straight to fail if timeout expired during lookup
1036 AsyncSocketException ex(
1037 AsyncSocketException::TIMED_OUT, "SSL accept timed out");
1038 failHandshake(__func__, ex);
1041 sslState_ = STATE_ACCEPTING;
1042 this->handleAccept();
1046 AsyncSSLSocket::handleAccept() noexcept {
1047 VLOG(3) << "AsyncSSLSocket::handleAccept() this=" << this
1048 << ", fd=" << fd_ << ", state=" << int(state_) << ", "
1049 << "sslState=" << sslState_ << ", events=" << eventFlags_;
1051 assert(state_ == StateEnum::ESTABLISHED &&
1052 sslState_ == STATE_ACCEPTING);
1054 /* lazily create the SSL structure */
1056 ssl_ = ctx_->createSSL();
1057 } catch (std::exception &e) {
1058 sslState_ = STATE_ERROR;
1059 AsyncSocketException ex(AsyncSocketException::INTERNAL_ERROR,
1060 "error calling SSLContext::createSSL()");
1061 LOG(ERROR) << "AsyncSSLSocket::handleAccept(this=" << this
1062 << ", fd=" << fd_ << "): " << e.what();
1063 return failHandshake(__func__, ex);
1066 if (!setupSSLBio()) {
1067 sslState_ = STATE_ERROR;
1068 AsyncSocketException ex(
1069 AsyncSocketException::INTERNAL_ERROR, "error creating write bio");
1070 return failHandshake(__func__, ex);
1073 SSL_set_ex_data(ssl_, getSSLExDataIndex(), this);
1075 applyVerificationOptions(ssl_);
1078 if (server_ && parseClientHello_) {
1079 SSL_set_msg_callback(ssl_, &AsyncSSLSocket::clientHelloParsingCallback);
1080 SSL_set_msg_callback_arg(ssl_, this);
1083 int ret = SSL_accept(ssl_);
1086 unsigned long errError;
1087 int errnoCopy = errno;
1088 if (willBlock(ret, &sslError, &errError)) {
1091 sslState_ = STATE_ERROR;
1092 SSLException ex(sslError, errError, ret, errnoCopy);
1093 return failHandshake(__func__, ex);
1097 handshakeComplete_ = true;
1098 updateEventRegistration(0, EventHandler::READ | EventHandler::WRITE);
1100 // Move into STATE_ESTABLISHED in the normal case that we are in
1102 sslState_ = STATE_ESTABLISHED;
1104 VLOG(3) << "AsyncSSLSocket " << this << ": fd " << fd_
1105 << " successfully accepted; state=" << int(state_)
1106 << ", sslState=" << sslState_ << ", events=" << eventFlags_;
1108 // Remember the EventBase we are attached to, before we start invoking any
1109 // callbacks (since the callbacks may call detachEventBase()).
1110 EventBase* originalEventBase = eventBase_;
1112 // Call the accept callback.
1113 invokeHandshakeCB();
1115 // Note that the accept callback may have changed our state.
1116 // (set or unset the read callback, called write(), closed the socket, etc.)
1117 // The following code needs to handle these situations correctly.
1119 // If the socket has been closed, readCallback_ and writeReqHead_ will
1120 // always be nullptr, so that will prevent us from trying to read or write.
1122 // The main thing to check for is if eventBase_ is still originalEventBase.
1123 // If not, we have been detached from this event base, so we shouldn't
1124 // perform any more operations.
1125 if (eventBase_ != originalEventBase) {
1129 AsyncSocket::handleInitialReadWrite();
1133 AsyncSSLSocket::handleConnect() noexcept {
1134 VLOG(3) << "AsyncSSLSocket::handleConnect() this=" << this
1135 << ", fd=" << fd_ << ", state=" << int(state_) << ", "
1136 << "sslState=" << sslState_ << ", events=" << eventFlags_;
1138 if (state_ < StateEnum::ESTABLISHED) {
1139 return AsyncSocket::handleConnect();
1143 (state_ == StateEnum::FAST_OPEN || state_ == StateEnum::ESTABLISHED) &&
1144 sslState_ == STATE_CONNECTING);
1147 auto originalState = state_;
1148 int ret = SSL_connect(ssl_);
1151 unsigned long errError;
1152 int errnoCopy = errno;
1153 if (willBlock(ret, &sslError, &errError)) {
1154 // We fell back to connecting state due to TFO
1155 if (state_ == StateEnum::CONNECTING) {
1156 DCHECK_EQ(StateEnum::FAST_OPEN, originalState);
1157 if (handshakeTimeout_.isScheduled()) {
1158 handshakeTimeout_.cancelTimeout();
1163 sslState_ = STATE_ERROR;
1164 SSLException ex(sslError, errError, ret, errnoCopy);
1165 return failHandshake(__func__, ex);
1169 handshakeComplete_ = true;
1170 updateEventRegistration(0, EventHandler::READ | EventHandler::WRITE);
1172 // Move into STATE_ESTABLISHED in the normal case that we are in
1173 // STATE_CONNECTING.
1174 sslState_ = STATE_ESTABLISHED;
1176 VLOG(3) << "AsyncSSLSocket " << this << ": "
1177 << "fd " << fd_ << " successfully connected; "
1178 << "state=" << int(state_) << ", sslState=" << sslState_
1179 << ", events=" << eventFlags_;
1181 // Remember the EventBase we are attached to, before we start invoking any
1182 // callbacks (since the callbacks may call detachEventBase()).
1183 EventBase* originalEventBase = eventBase_;
1185 // Call the handshake callback.
1186 invokeHandshakeCB();
1188 // Note that the connect callback may have changed our state.
1189 // (set or unset the read callback, called write(), closed the socket, etc.)
1190 // The following code needs to handle these situations correctly.
1192 // If the socket has been closed, readCallback_ and writeReqHead_ will
1193 // always be nullptr, so that will prevent us from trying to read or write.
1195 // The main thing to check for is if eventBase_ is still originalEventBase.
1196 // If not, we have been detached from this event base, so we shouldn't
1197 // perform any more operations.
1198 if (eventBase_ != originalEventBase) {
1202 AsyncSocket::handleInitialReadWrite();
1205 void AsyncSSLSocket::invokeConnectErr(const AsyncSocketException& ex) {
1206 connectionTimeout_.cancelTimeout();
1207 AsyncSocket::invokeConnectErr(ex);
1208 if (sslState_ == SSLStateEnum::STATE_CONNECTING) {
1209 if (handshakeTimeout_.isScheduled()) {
1210 handshakeTimeout_.cancelTimeout();
1212 // If we fell back to connecting state during TFO and the connection
1213 // failed, it would be an SSL failure as well.
1214 invokeHandshakeErr(ex);
1218 void AsyncSSLSocket::invokeConnectSuccess() {
1219 connectionTimeout_.cancelTimeout();
1220 if (sslState_ == SSLStateEnum::STATE_CONNECTING) {
1221 assert(tfoAttempted_);
1222 // If we failed TFO, we'd fall back to trying to connect the socket,
1223 // to setup things like timeouts.
1226 // still invoke the base class since it re-sets the connect time.
1227 AsyncSocket::invokeConnectSuccess();
1230 void AsyncSSLSocket::scheduleConnectTimeout() {
1231 if (sslState_ == SSLStateEnum::STATE_CONNECTING) {
1232 // We fell back from TFO, and need to set the timeouts.
1233 // We will not have a connect callback in this case, thus if the timer
1234 // expires we would have no-one to notify.
1235 // Thus we should reset even the connect timers to point to the handshake
1237 assert(connectCallback_ == nullptr);
1238 // We use a different connect timeout here than the handshake timeout, so
1239 // that we can disambiguate the 2 timers.
1240 if (connectTimeout_.count() > 0) {
1241 if (!connectionTimeout_.scheduleTimeout(connectTimeout_)) {
1242 throw AsyncSocketException(
1243 AsyncSocketException::INTERNAL_ERROR,
1244 withAddr("failed to schedule AsyncSSLSocket connect timeout"));
1249 AsyncSocket::scheduleConnectTimeout();
1252 void AsyncSSLSocket::setReadCB(ReadCallback *callback) {
1253 #ifdef SSL_MODE_MOVE_BUFFER_OWNERSHIP
1254 // turn on the buffer movable in openssl
1255 if (bufferMovableEnabled_ && ssl_ != nullptr && !isBufferMovable_ &&
1256 callback != nullptr && callback->isBufferMovable()) {
1257 SSL_set_mode(ssl_, SSL_get_mode(ssl_) | SSL_MODE_MOVE_BUFFER_OWNERSHIP);
1258 isBufferMovable_ = true;
1262 AsyncSocket::setReadCB(callback);
1265 void AsyncSSLSocket::setBufferMovableEnabled(bool enabled) {
1266 bufferMovableEnabled_ = enabled;
1269 void AsyncSSLSocket::prepareReadBuffer(void** buf, size_t* buflen) {
1270 CHECK(readCallback_);
1271 if (isBufferMovable_) {
1275 // buf is necessary for SSLSocket without SSL_MODE_MOVE_BUFFER_OWNERSHIP
1276 readCallback_->getReadBuffer(buf, buflen);
1281 AsyncSSLSocket::handleRead() noexcept {
1282 VLOG(5) << "AsyncSSLSocket::handleRead() this=" << this << ", fd=" << fd_
1283 << ", state=" << int(state_) << ", "
1284 << "sslState=" << sslState_ << ", events=" << eventFlags_;
1285 if (state_ < StateEnum::ESTABLISHED) {
1286 return AsyncSocket::handleRead();
1290 if (sslState_ == STATE_ACCEPTING) {
1295 else if (sslState_ == STATE_CONNECTING) {
1302 AsyncSocket::handleRead();
1305 AsyncSocket::ReadResult
1306 AsyncSSLSocket::performRead(void** buf, size_t* buflen, size_t* offset) {
1307 VLOG(4) << "AsyncSSLSocket::performRead() this=" << this << ", buf=" << *buf
1308 << ", buflen=" << *buflen;
1310 if (sslState_ == STATE_UNENCRYPTED) {
1311 return AsyncSocket::performRead(buf, buflen, offset);
1315 if (!isBufferMovable_) {
1316 bytes = SSL_read(ssl_, *buf, int(*buflen));
1318 #ifdef SSL_MODE_MOVE_BUFFER_OWNERSHIP
1320 bytes = SSL_read_buf(ssl_, buf, (int *) offset, (int *) buflen);
1324 if (server_ && renegotiateAttempted_) {
1325 LOG(ERROR) << "AsyncSSLSocket(fd=" << fd_ << ", state=" << int(state_)
1326 << ", sslstate=" << sslState_ << ", events=" << eventFlags_
1327 << "): client intitiated SSL renegotiation not permitted";
1330 folly::make_unique<SSLException>(SSLError::CLIENT_RENEGOTIATION));
1333 int error = SSL_get_error(ssl_, bytes);
1334 if (error == SSL_ERROR_WANT_READ) {
1335 // The caller will register for read event if not already.
1336 if (errno == EWOULDBLOCK || errno == EAGAIN) {
1337 return ReadResult(READ_BLOCKING);
1339 return ReadResult(READ_ERROR);
1341 } else if (error == SSL_ERROR_WANT_WRITE) {
1342 // TODO: Even though we are attempting to read data, SSL_read() may
1343 // need to write data if renegotiation is being performed. We currently
1344 // don't support this and just fail the read.
1345 LOG(ERROR) << "AsyncSSLSocket(fd=" << fd_ << ", state=" << int(state_)
1346 << ", sslState=" << sslState_ << ", events=" << eventFlags_
1347 << "): unsupported SSL renegotiation during read";
1350 folly::make_unique<SSLException>(SSLError::INVALID_RENEGOTIATION));
1352 if (zero_return(error, bytes)) {
1353 return ReadResult(bytes);
1355 auto errError = ERR_get_error();
1356 VLOG(6) << "AsyncSSLSocket(fd=" << fd_ << ", "
1357 << "state=" << state_ << ", "
1358 << "sslState=" << sslState_ << ", "
1359 << "events=" << std::hex << eventFlags_ << "): "
1360 << "bytes: " << bytes << ", "
1361 << "error: " << error << ", "
1362 << "errno: " << errno << ", "
1363 << "func: " << ERR_func_error_string(errError) << ", "
1364 << "reason: " << ERR_reason_error_string(errError);
1367 folly::make_unique<SSLException>(error, errError, bytes, errno));
1370 appBytesReceived_ += bytes;
1371 return ReadResult(bytes);
1375 void AsyncSSLSocket::handleWrite() noexcept {
1376 VLOG(5) << "AsyncSSLSocket::handleWrite() this=" << this << ", fd=" << fd_
1377 << ", state=" << int(state_) << ", "
1378 << "sslState=" << sslState_ << ", events=" << eventFlags_;
1379 if (state_ < StateEnum::ESTABLISHED) {
1380 return AsyncSocket::handleWrite();
1383 if (sslState_ == STATE_ACCEPTING) {
1389 if (sslState_ == STATE_CONNECTING) {
1396 AsyncSocket::handleWrite();
1399 AsyncSocket::WriteResult AsyncSSLSocket::interpretSSLError(int rc, int error) {
1400 if (error == SSL_ERROR_WANT_READ) {
1401 // Even though we are attempting to write data, SSL_write() may
1402 // need to read data if renegotiation is being performed. We currently
1403 // don't support this and just fail the write.
1404 LOG(ERROR) << "AsyncSSLSocket(fd=" << fd_ << ", state=" << int(state_)
1405 << ", sslState=" << sslState_ << ", events=" << eventFlags_
1407 << "unsupported SSL renegotiation during write";
1410 folly::make_unique<SSLException>(SSLError::INVALID_RENEGOTIATION));
1412 if (zero_return(error, rc)) {
1413 return WriteResult(0);
1415 auto errError = ERR_get_error();
1416 VLOG(3) << "ERROR: AsyncSSLSocket(fd=" << fd_ << ", state=" << int(state_)
1417 << ", sslState=" << sslState_ << ", events=" << eventFlags_ << "): "
1418 << "SSL error: " << error << ", errno: " << errno
1419 << ", func: " << ERR_func_error_string(errError)
1420 << ", reason: " << ERR_reason_error_string(errError);
1423 folly::make_unique<SSLException>(error, errError, rc, errno));
1427 AsyncSocket::WriteResult AsyncSSLSocket::performWrite(
1431 uint32_t* countWritten,
1432 uint32_t* partialWritten) {
1433 if (sslState_ == STATE_UNENCRYPTED) {
1434 return AsyncSocket::performWrite(
1435 vec, count, flags, countWritten, partialWritten);
1437 if (sslState_ != STATE_ESTABLISHED) {
1438 LOG(ERROR) << "AsyncSSLSocket(fd=" << fd_ << ", state=" << int(state_)
1439 << ", sslState=" << sslState_
1440 << ", events=" << eventFlags_ << "): "
1441 << "TODO: AsyncSSLSocket currently does not support calling "
1442 << "write() before the handshake has fully completed";
1444 WRITE_ERROR, folly::make_unique<SSLException>(SSLError::EARLY_WRITE));
1447 // Declare a buffer used to hold small write requests. It could point to a
1448 // memory block either on stack or on heap. If it is on heap, we release it
1449 // manually when scope exits
1450 char* combinedBuf{nullptr};
1452 // Note, always keep this check consistent with what we do below
1453 if (combinedBuf != nullptr && minWriteSize_ > MAX_STACK_BUF_SIZE) {
1454 delete[] combinedBuf;
1459 *partialWritten = 0;
1460 ssize_t totalWritten = 0;
1461 size_t bytesStolenFromNextBuffer = 0;
1462 for (uint32_t i = 0; i < count; i++) {
1463 const iovec* v = vec + i;
1464 size_t offset = bytesStolenFromNextBuffer;
1465 bytesStolenFromNextBuffer = 0;
1466 size_t len = v->iov_len - offset;
1472 buf = ((const char*)v->iov_base) + offset;
1475 uint32_t buffersStolen = 0;
1476 auto sslWriteBuf = buf;
1477 if ((len < minWriteSize_) && ((i + 1) < count)) {
1478 // Combine this buffer with part or all of the next buffers in
1479 // order to avoid really small-grained calls to SSL_write().
1480 // Each call to SSL_write() produces a separate record in
1481 // the egress SSL stream, and we've found that some low-end
1482 // mobile clients can't handle receiving an HTTP response
1483 // header and the first part of the response body in two
1484 // separate SSL records (even if those two records are in
1485 // the same TCP packet).
1487 if (combinedBuf == nullptr) {
1488 if (minWriteSize_ > MAX_STACK_BUF_SIZE) {
1489 // Allocate the buffer on heap
1490 combinedBuf = new char[minWriteSize_];
1492 // Allocate the buffer on stack
1493 combinedBuf = (char*)alloca(minWriteSize_);
1496 assert(combinedBuf != nullptr);
1497 sslWriteBuf = combinedBuf;
1499 memcpy(combinedBuf, buf, len);
1501 // INVARIANT: i + buffersStolen == complete chunks serialized
1502 uint32_t nextIndex = i + buffersStolen + 1;
1503 bytesStolenFromNextBuffer = std::min(vec[nextIndex].iov_len,
1504 minWriteSize_ - len);
1505 if (bytesStolenFromNextBuffer > 0) {
1506 assert(vec[nextIndex].iov_base != nullptr);
1509 vec[nextIndex].iov_base,
1510 bytesStolenFromNextBuffer);
1512 len += bytesStolenFromNextBuffer;
1513 if (bytesStolenFromNextBuffer < vec[nextIndex].iov_len) {
1514 // couldn't steal the whole buffer
1517 bytesStolenFromNextBuffer = 0;
1520 } while ((i + buffersStolen + 1) < count && (len < minWriteSize_));
1523 // Advance any empty buffers immediately after.
1524 if (bytesStolenFromNextBuffer == 0) {
1525 while ((i + buffersStolen + 1) < count &&
1526 vec[i + buffersStolen + 1].iov_len == 0) {
1532 isSet(flags, WriteFlags::CORK) || (i + buffersStolen + 1 < count);
1533 bytes = eorAwareSSLWrite(
1537 (isSet(flags, WriteFlags::EOR) && i + buffersStolen + 1 == count));
1540 int error = SSL_get_error(ssl_, int(bytes));
1541 if (error == SSL_ERROR_WANT_WRITE) {
1542 // The caller will register for write event if not already.
1543 *partialWritten = uint32_t(offset);
1544 return WriteResult(totalWritten);
1546 auto writeResult = interpretSSLError(int(bytes), error);
1547 if (writeResult.writeReturn < 0) {
1549 } // else fall through to below to correctly record totalWritten
1552 totalWritten += bytes;
1554 if (bytes == (ssize_t)len) {
1555 // The full iovec is written.
1556 (*countWritten) += 1 + buffersStolen;
1560 bytes += offset; // adjust bytes to account for all of v
1561 while (bytes >= (ssize_t)v->iov_len) {
1562 // We combined this buf with part or all of the next one, and
1563 // we managed to write all of this buf but not all of the bytes
1564 // from the next one that we'd hoped to write.
1565 bytes -= v->iov_len;
1569 *partialWritten = uint32_t(bytes);
1570 return WriteResult(totalWritten);
1574 return WriteResult(totalWritten);
1577 int AsyncSSLSocket::eorAwareSSLWrite(SSL *ssl, const void *buf, int n,
1579 if (eor && isEorTrackingEnabled()) {
1580 if (appEorByteNo_) {
1581 // cannot track for more than one app byte EOR
1582 CHECK(appEorByteNo_ == appBytesWritten_ + n);
1584 appEorByteNo_ = appBytesWritten_ + n;
1587 // 1. It is fine to keep updating minEorRawByteNo_.
1588 // 2. It is _min_ in the sense that SSL record will add some overhead.
1589 minEorRawByteNo_ = getRawBytesWritten() + n;
1592 n = sslWriteImpl(ssl, buf, n);
1594 appBytesWritten_ += n;
1595 if (appEorByteNo_) {
1596 if (getRawBytesWritten() >= minEorRawByteNo_) {
1597 minEorRawByteNo_ = 0;
1599 if(appBytesWritten_ == appEorByteNo_) {
1602 CHECK(appBytesWritten_ < appEorByteNo_);
1609 void AsyncSSLSocket::sslInfoCallback(const SSL* ssl, int where, int ret) {
1610 AsyncSSLSocket *sslSocket = AsyncSSLSocket::getFromSSL(ssl);
1611 if (sslSocket->handshakeComplete_ && (where & SSL_CB_HANDSHAKE_START)) {
1612 sslSocket->renegotiateAttempted_ = true;
1614 if (where & SSL_CB_READ_ALERT) {
1615 const char* type = SSL_alert_type_string(ret);
1617 const char* desc = SSL_alert_desc_string(ret);
1618 sslSocket->alertsReceived_.emplace_back(
1619 *type, StringPiece(desc, std::strlen(desc)));
1624 int AsyncSSLSocket::bioWrite(BIO* b, const char* in, int inl) {
1627 AsyncSSLSocket* tsslSock;
1629 iov.iov_base = const_cast<char*>(in);
1630 iov.iov_len = size_t(inl);
1631 memset(&msg, 0, sizeof(msg));
1635 auto appData = OpenSSLUtils::getBioAppData(b);
1638 tsslSock = reinterpret_cast<AsyncSSLSocket*>(appData);
1641 WriteFlags flags = WriteFlags::NONE;
1642 if (tsslSock->isEorTrackingEnabled() && tsslSock->minEorRawByteNo_ &&
1643 tsslSock->minEorRawByteNo_ <= BIO_number_written(b) + inl) {
1644 flags |= WriteFlags::EOR;
1647 if (tsslSock->corkCurrentWrite_) {
1648 flags |= WriteFlags::CORK;
1651 int msg_flags = tsslSock->getSendMsgParamsCB()->getFlags(flags);
1652 msg.msg_controllen =
1653 tsslSock->getSendMsgParamsCB()->getAncillaryDataSize(flags);
1654 CHECK_GE(AsyncSocket::SendMsgParamsCallback::maxAncillaryDataSize,
1655 msg.msg_controllen);
1656 if (msg.msg_controllen != 0) {
1657 msg.msg_control = reinterpret_cast<char*>(alloca(msg.msg_controllen));
1658 tsslSock->getSendMsgParamsCB()->getAncillaryData(flags, msg.msg_control);
1661 auto result = tsslSock->sendSocketMessage(
1662 OpenSSLUtils::getBioFd(b, nullptr), &msg, msg_flags);
1663 BIO_clear_retry_flags(b);
1664 if (!result.exception && result.writeReturn <= 0) {
1665 if (OpenSSLUtils::getBioShouldRetryWrite(int(result.writeReturn))) {
1666 BIO_set_retry_write(b);
1669 return int(result.writeReturn);
1672 int AsyncSSLSocket::bioRead(BIO* b, char* out, int outl) {
1676 BIO_clear_retry_flags(b);
1678 auto appData = OpenSSLUtils::getBioAppData(b);
1680 auto sslSock = reinterpret_cast<AsyncSSLSocket*>(appData);
1682 if (sslSock->preReceivedData_ && !sslSock->preReceivedData_->empty()) {
1683 VLOG(5) << "AsyncSSLSocket::bioRead() this=" << sslSock
1684 << ", reading pre-received data";
1686 Cursor cursor(sslSock->preReceivedData_.get());
1687 auto len = cursor.pullAtMost(out, outl);
1690 queue.append(std::move(sslSock->preReceivedData_));
1691 queue.trimStart(len);
1692 sslSock->preReceivedData_ = queue.move();
1695 auto result = recv(OpenSSLUtils::getBioFd(b, nullptr), out, outl, 0);
1696 if (result <= 0 && OpenSSLUtils::getBioShouldRetryWrite(result)) {
1697 BIO_set_retry_read(b);
1703 int AsyncSSLSocket::sslVerifyCallback(
1705 X509_STORE_CTX* x509Ctx) {
1706 SSL* ssl = (SSL*) X509_STORE_CTX_get_ex_data(
1707 x509Ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
1708 AsyncSSLSocket* self = AsyncSSLSocket::getFromSSL(ssl);
1710 VLOG(3) << "AsyncSSLSocket::sslVerifyCallback() this=" << self << ", "
1711 << "fd=" << self->fd_ << ", preverifyOk=" << preverifyOk;
1712 return (self->handshakeCallback_) ?
1713 self->handshakeCallback_->handshakeVer(self, preverifyOk, x509Ctx) :
1717 void AsyncSSLSocket::enableClientHelloParsing() {
1718 parseClientHello_ = true;
1719 clientHelloInfo_.reset(new ssl::ClientHelloInfo());
1722 void AsyncSSLSocket::resetClientHelloParsing(SSL *ssl) {
1723 SSL_set_msg_callback(ssl, nullptr);
1724 SSL_set_msg_callback_arg(ssl, nullptr);
1725 clientHelloInfo_->clientHelloBuf_.clear();
1728 void AsyncSSLSocket::clientHelloParsingCallback(int written,
1735 AsyncSSLSocket *sock = static_cast<AsyncSSLSocket*>(arg);
1737 sock->resetClientHelloParsing(ssl);
1740 if (contentType != SSL3_RT_HANDSHAKE) {
1747 auto& clientHelloBuf = sock->clientHelloInfo_->clientHelloBuf_;
1748 clientHelloBuf.append(IOBuf::wrapBuffer(buf, len));
1750 Cursor cursor(clientHelloBuf.front());
1751 if (cursor.read<uint8_t>() != SSL3_MT_CLIENT_HELLO) {
1752 sock->resetClientHelloParsing(ssl);
1756 if (cursor.totalLength() < 3) {
1757 clientHelloBuf.trimEnd(len);
1758 clientHelloBuf.append(IOBuf::copyBuffer(buf, len));
1762 uint32_t messageLength = cursor.read<uint8_t>();
1763 messageLength <<= 8;
1764 messageLength |= cursor.read<uint8_t>();
1765 messageLength <<= 8;
1766 messageLength |= cursor.read<uint8_t>();
1767 if (cursor.totalLength() < messageLength) {
1768 clientHelloBuf.trimEnd(len);
1769 clientHelloBuf.append(IOBuf::copyBuffer(buf, len));
1773 sock->clientHelloInfo_->clientHelloMajorVersion_ = cursor.read<uint8_t>();
1774 sock->clientHelloInfo_->clientHelloMinorVersion_ = cursor.read<uint8_t>();
1776 cursor.skip(4); // gmt_unix_time
1777 cursor.skip(28); // random_bytes
1779 cursor.skip(cursor.read<uint8_t>()); // session_id
1781 uint16_t cipherSuitesLength = cursor.readBE<uint16_t>();
1782 for (int i = 0; i < cipherSuitesLength; i += 2) {
1783 sock->clientHelloInfo_->
1784 clientHelloCipherSuites_.push_back(cursor.readBE<uint16_t>());
1787 uint8_t compressionMethodsLength = cursor.read<uint8_t>();
1788 for (int i = 0; i < compressionMethodsLength; ++i) {
1789 sock->clientHelloInfo_->
1790 clientHelloCompressionMethods_.push_back(cursor.readBE<uint8_t>());
1793 if (cursor.totalLength() > 0) {
1794 uint16_t extensionsLength = cursor.readBE<uint16_t>();
1795 while (extensionsLength) {
1796 ssl::TLSExtension extensionType =
1797 static_cast<ssl::TLSExtension>(cursor.readBE<uint16_t>());
1798 sock->clientHelloInfo_->
1799 clientHelloExtensions_.push_back(extensionType);
1800 extensionsLength -= 2;
1801 uint16_t extensionDataLength = cursor.readBE<uint16_t>();
1802 extensionsLength -= 2;
1803 extensionsLength -= extensionDataLength;
1805 if (extensionType == ssl::TLSExtension::SIGNATURE_ALGORITHMS) {
1807 extensionDataLength -= 2;
1808 while (extensionDataLength) {
1809 ssl::HashAlgorithm hashAlg =
1810 static_cast<ssl::HashAlgorithm>(cursor.readBE<uint8_t>());
1811 ssl::SignatureAlgorithm sigAlg =
1812 static_cast<ssl::SignatureAlgorithm>(cursor.readBE<uint8_t>());
1813 extensionDataLength -= 2;
1814 sock->clientHelloInfo_->
1815 clientHelloSigAlgs_.emplace_back(hashAlg, sigAlg);
1817 } else if (extensionType == ssl::TLSExtension::SUPPORTED_VERSIONS) {
1819 extensionDataLength -= 1;
1820 while (extensionDataLength) {
1821 sock->clientHelloInfo_->clientHelloSupportedVersions_.push_back(
1822 cursor.readBE<uint16_t>());
1823 extensionDataLength -= 2;
1826 cursor.skip(extensionDataLength);
1830 } catch (std::out_of_range&) {
1831 // we'll use what we found and cleanup below.
1832 VLOG(4) << "AsyncSSLSocket::clientHelloParsingCallback(): "
1833 << "buffer finished unexpectedly." << " AsyncSSLSocket socket=" << sock;
1836 sock->resetClientHelloParsing(ssl);
1839 void AsyncSSLSocket::getSSLClientCiphers(
1840 std::string& clientCiphers,
1841 bool convertToString) const {
1842 std::string ciphers;
1844 if (parseClientHello_ == false
1845 || clientHelloInfo_->clientHelloCipherSuites_.empty()) {
1851 for (auto originalCipherCode : clientHelloInfo_->clientHelloCipherSuites_)
1859 bool nameFound = convertToString;
1861 if (convertToString) {
1862 const auto& name = OpenSSLUtils::getCipherName(originalCipherCode);
1872 std::array<uint8_t, 2>{{
1873 static_cast<uint8_t>((originalCipherCode >> 8) & 0xffL),
1874 static_cast<uint8_t>(originalCipherCode & 0x00ffL) }},
1876 /* append to ciphers = */ true);
1880 clientCiphers = std::move(ciphers);
1883 std::string AsyncSSLSocket::getSSLClientComprMethods() const {
1884 if (!parseClientHello_) {
1887 return folly::join(":", clientHelloInfo_->clientHelloCompressionMethods_);
1890 std::string AsyncSSLSocket::getSSLClientExts() const {
1891 if (!parseClientHello_) {
1894 return folly::join(":", clientHelloInfo_->clientHelloExtensions_);
1897 std::string AsyncSSLSocket::getSSLClientSigAlgs() const {
1898 if (!parseClientHello_) {
1902 std::string sigAlgs;
1903 sigAlgs.reserve(clientHelloInfo_->clientHelloSigAlgs_.size() * 4);
1904 for (size_t i = 0; i < clientHelloInfo_->clientHelloSigAlgs_.size(); i++) {
1906 sigAlgs.push_back(':');
1908 sigAlgs.append(folly::to<std::string>(
1909 clientHelloInfo_->clientHelloSigAlgs_[i].first));
1910 sigAlgs.push_back(',');
1911 sigAlgs.append(folly::to<std::string>(
1912 clientHelloInfo_->clientHelloSigAlgs_[i].second));
1918 std::string AsyncSSLSocket::getSSLClientSupportedVersions() const {
1919 if (!parseClientHello_) {
1922 return folly::join(":", clientHelloInfo_->clientHelloSupportedVersions_);
1925 std::string AsyncSSLSocket::getSSLAlertsReceived() const {
1928 for (const auto& alert : alertsReceived_) {
1932 ret.append(folly::to<std::string>(alert.first, ": ", alert.second));
1938 void AsyncSSLSocket::setSSLCertVerificationAlert(std::string alert) {
1939 sslVerificationAlert_ = std::move(alert);
1942 std::string AsyncSSLSocket::getSSLCertVerificationAlert() const {
1943 return sslVerificationAlert_;
1946 void AsyncSSLSocket::getSSLSharedCiphers(std::string& sharedCiphers) const {
1947 char ciphersBuffer[1024];
1948 ciphersBuffer[0] = '\0';
1949 SSL_get_shared_ciphers(ssl_, ciphersBuffer, sizeof(ciphersBuffer) - 1);
1950 sharedCiphers = ciphersBuffer;
1953 void AsyncSSLSocket::getSSLServerCiphers(std::string& serverCiphers) const {
1954 serverCiphers = SSL_get_cipher_list(ssl_, 0);
1957 while ((cipher = SSL_get_cipher_list(ssl_, i)) != nullptr) {
1958 serverCiphers.append(":");
1959 serverCiphers.append(cipher);