2 * Copyright 2016 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.
21 #include <folly/Optional.h>
22 #include <folly/String.h>
23 #include <folly/io/async/AsyncSocket.h>
24 #include <folly/io/async/AsyncTimeout.h>
25 #include <folly/io/async/SSLContext.h>
26 #include <folly/io/async/TimeoutManager.h>
27 #include <folly/io/async/ssl/OpenSSLPtrTypes.h>
28 #include <folly/io/async/ssl/OpenSSLUtils.h>
29 #include <folly/io/async/ssl/SSLErrors.h>
30 #include <folly/io/async/ssl/TLSDefinitions.h>
32 #include <folly/Bits.h>
33 #include <folly/io/IOBuf.h>
34 #include <folly/io/Cursor.h>
35 #include <folly/portability/Sockets.h>
40 * A class for performing asynchronous I/O on an SSL connection.
42 * AsyncSSLSocket allows users to asynchronously wait for data on an
43 * SSL connection, and to asynchronously send data.
45 * The APIs for reading and writing are intentionally asymmetric.
46 * Waiting for data to read is a persistent API: a callback is
47 * installed, and is notified whenever new data is available. It
48 * continues to be notified of new events until it is uninstalled.
50 * AsyncSSLSocket does not provide read timeout functionality,
51 * because it typically cannot determine when the timeout should be
52 * active. Generally, a timeout should only be enabled when
53 * processing is blocked waiting on data from the remote endpoint.
54 * For server connections, the timeout should not be active if the
55 * server is currently processing one or more outstanding requests for
56 * this connection. For client connections, the timeout should not be
57 * active if there are no requests pending on the connection.
58 * Additionally, if a client has multiple pending requests, it will
59 * ususally want a separate timeout for each request, rather than a
60 * single read timeout.
62 * The write API is fairly intuitive: a user can request to send a
63 * block of data, and a callback will be informed once the entire
64 * block has been transferred to the kernel, or on error.
65 * AsyncSSLSocket does provide a send timeout, since most callers
66 * want to give up if the remote end stops responding and no further
67 * progress can be made sending the data.
69 class AsyncSSLSocket : public virtual AsyncSocket {
71 typedef std::unique_ptr<AsyncSSLSocket, Destructor> UniquePtr;
72 using X509_deleter = folly::static_function_deleter<X509, &X509_free>;
76 virtual ~HandshakeCB() = default;
79 * handshakeVer() is invoked during handshaking to give the
80 * application chance to validate it's peer's certificate.
82 * Note that OpenSSL performs only rudimentary internal
83 * consistency verification checks by itself. Any other validation
84 * like whether or not the certificate was issued by a trusted CA.
85 * The default implementation of this callback mimics what what
86 * OpenSSL does internally if SSL_VERIFY_PEER is set with no
87 * verification callback.
89 * See the passages on verify_callback in SSL_CTX_set_verify(3)
92 virtual bool handshakeVer(AsyncSSLSocket* /*sock*/,
94 X509_STORE_CTX* /*ctx*/) noexcept {
99 * handshakeSuc() is called when a new SSL connection is
100 * established, i.e., after SSL_accept/connect() returns successfully.
102 * The HandshakeCB will be uninstalled before handshakeSuc()
105 * @param sock SSL socket on which the handshake was initiated
107 virtual void handshakeSuc(AsyncSSLSocket *sock) noexcept = 0;
110 * handshakeErr() is called if an error occurs while
111 * establishing the SSL connection.
113 * The HandshakeCB will be uninstalled before handshakeErr()
116 * @param sock SSL socket on which the handshake was initiated
117 * @param ex An exception representing the error.
119 virtual void handshakeErr(
120 AsyncSSLSocket *sock,
121 const AsyncSocketException& ex)
125 class HandshakeTimeout : public AsyncTimeout {
127 HandshakeTimeout(AsyncSSLSocket* sslSocket, EventBase* eventBase)
128 : AsyncTimeout(eventBase)
129 , sslSocket_(sslSocket) {}
131 virtual void timeoutExpired() noexcept {
132 sslSocket_->timeoutExpired();
136 AsyncSSLSocket* sslSocket_;
140 * Create a client AsyncSSLSocket
142 AsyncSSLSocket(const std::shared_ptr<folly::SSLContext> &ctx,
143 EventBase* evb, bool deferSecurityNegotiation = false);
146 * Create a server/client AsyncSSLSocket from an already connected
147 * socket file descriptor.
149 * Note that while AsyncSSLSocket enables TCP_NODELAY for sockets it creates
150 * when connecting, it does not change the socket options when given an
151 * existing file descriptor. If callers want TCP_NODELAY enabled when using
152 * this version of the constructor, they need to explicitly call
153 * setNoDelay(true) after the constructor returns.
155 * @param ctx SSL context for this connection.
156 * @param evb EventBase that will manage this socket.
157 * @param fd File descriptor to take over (should be a connected socket).
158 * @param server Is socket in server mode?
159 * @param deferSecurityNegotiation
160 * unencrypted data can be sent before sslConn/Accept
162 AsyncSSLSocket(const std::shared_ptr<folly::SSLContext>& ctx,
163 EventBase* evb, int fd,
164 bool server = true, bool deferSecurityNegotiation = false);
168 * Helper function to create a server/client shared_ptr<AsyncSSLSocket>.
170 static std::shared_ptr<AsyncSSLSocket> newSocket(
171 const std::shared_ptr<folly::SSLContext>& ctx,
172 EventBase* evb, int fd, bool server=true,
173 bool deferSecurityNegotiation = false) {
174 return std::shared_ptr<AsyncSSLSocket>(
175 new AsyncSSLSocket(ctx, evb, fd, server, deferSecurityNegotiation),
180 * Helper function to create a client shared_ptr<AsyncSSLSocket>.
182 static std::shared_ptr<AsyncSSLSocket> newSocket(
183 const std::shared_ptr<folly::SSLContext>& ctx,
184 EventBase* evb, bool deferSecurityNegotiation = false) {
185 return std::shared_ptr<AsyncSSLSocket>(
186 new AsyncSSLSocket(ctx, evb, deferSecurityNegotiation),
191 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
193 * Create a client AsyncSSLSocket with tlsext_servername in
194 * the Client Hello message.
196 AsyncSSLSocket(const std::shared_ptr<folly::SSLContext> &ctx,
198 const std::string& serverName,
199 bool deferSecurityNegotiation = false);
202 * Create a client AsyncSSLSocket from an already connected
203 * socket file descriptor.
205 * Note that while AsyncSSLSocket enables TCP_NODELAY for sockets it creates
206 * when connecting, it does not change the socket options when given an
207 * existing file descriptor. If callers want TCP_NODELAY enabled when using
208 * this version of the constructor, they need to explicitly call
209 * setNoDelay(true) after the constructor returns.
211 * @param ctx SSL context for this connection.
212 * @param evb EventBase that will manage this socket.
213 * @param fd File descriptor to take over (should be a connected socket).
214 * @param serverName tlsext_hostname that will be sent in ClientHello.
216 AsyncSSLSocket(const std::shared_ptr<folly::SSLContext>& ctx,
219 const std::string& serverName,
220 bool deferSecurityNegotiation = false);
222 static std::shared_ptr<AsyncSSLSocket> newSocket(
223 const std::shared_ptr<folly::SSLContext>& ctx,
225 const std::string& serverName,
226 bool deferSecurityNegotiation = false) {
227 return std::shared_ptr<AsyncSSLSocket>(
228 new AsyncSSLSocket(ctx, evb, serverName, deferSecurityNegotiation),
234 * TODO: implement support for SSL renegotiation.
236 * This involves proper handling of the SSL_ERROR_WANT_READ/WRITE
237 * code as a result of SSL_write/read(), instead of returning an
238 * error. In that case, the READ/WRITE event should be registered,
239 * and a flag (e.g., writeBlockedOnRead) should be set to indiciate
240 * the condition. In the next invocation of read/write callback, if
241 * the flag is on, performWrite()/performRead() should be called in
242 * addition to the normal call to performRead()/performWrite(), and
243 * the flag should be reset.
246 // Inherit TAsyncTransport methods from AsyncSocket except the
248 // See the documentation in TAsyncTransport.h
249 // TODO: implement graceful shutdown in close()
250 // TODO: implement detachSSL() that returns the SSL connection
251 virtual void closeNow() override;
252 virtual void shutdownWrite() override;
253 virtual void shutdownWriteNow() override;
254 virtual bool good() const override;
255 virtual bool connecting() const override;
256 virtual std::string getApplicationProtocol() noexcept override;
258 virtual std::string getSecurityProtocol() const override { return "TLS"; }
260 bool isEorTrackingEnabled() const override;
261 virtual void setEorTracking(bool track) override;
262 virtual size_t getRawBytesWritten() const override;
263 virtual size_t getRawBytesReceived() const override;
264 void enableClientHelloParsing();
267 * Accept an SSL connection on the socket.
269 * The callback will be invoked and uninstalled when an SSL
270 * connection has been established on the underlying socket.
271 * The value of verifyPeer determines the client verification method.
272 * By default, its set to use the value in the underlying context
274 * @param callback callback object to invoke on success/failure
275 * @param timeout timeout for this function in milliseconds, or 0 for no
277 * @param verifyPeer SSLVerifyPeerEnum uses the options specified in the
278 * context by default, can be set explcitly to override the
279 * method in the context
281 virtual void sslAccept(HandshakeCB* callback, uint32_t timeout = 0,
282 const folly::SSLContext::SSLVerifyPeerEnum& verifyPeer =
283 folly::SSLContext::SSLVerifyPeerEnum::USE_CTX);
286 * Invoke SSL accept following an asynchronous session cache lookup
288 void restartSSLAccept();
291 * Connect to the given address, invoking callback when complete or on error
293 * Note timeout applies to TCP + SSL connection time
295 void connect(ConnectCallback* callback,
296 const folly::SocketAddress& address,
298 const OptionMap &options = emptyOptionMap,
299 const folly::SocketAddress& bindAddr = anyAddress())
302 using AsyncSocket::connect;
305 * Initiate an SSL connection on the socket
306 * The callback will be invoked and uninstalled when an SSL connection
307 * has been establshed on the underlying socket.
308 * The verification option verifyPeer is applied if it's passed explicitly.
309 * If it's not, the options in SSLContext set on the underlying SSLContext
312 * @param callback callback object to invoke on success/failure
313 * @param timeout timeout for this function in milliseconds, or 0 for no
315 * @param verifyPeer SSLVerifyPeerEnum uses the options specified in the
316 * context by default, can be set explcitly to override the
317 * method in the context. If verification is turned on sets
318 * SSL_VERIFY_PEER and invokes
319 * HandshakeCB::handshakeVer().
321 virtual void sslConn(HandshakeCB *callback, uint64_t timeout = 0,
322 const folly::SSLContext::SSLVerifyPeerEnum& verifyPeer =
323 folly::SSLContext::SSLVerifyPeerEnum::USE_CTX);
333 STATE_REMOTE_CLOSED, /// remote end closed; we can still write
334 STATE_CLOSING, ///< close() called, but waiting on writes to complete
335 /// close() called with pending writes, before connect() has completed
336 STATE_CONNECTING_CLOSING,
341 SSLStateEnum getSSLState() const { return sslState_;}
344 * Get a handle to the negotiated SSL session. This increments the session
345 * refcount and must be deallocated by the caller.
347 SSL_SESSION *getSSLSession();
350 * Get a handle to the SSL struct.
352 const SSL* getSSL() const;
355 * Set the SSL session to be used during sslConn. AsyncSSLSocket will
356 * hold a reference to the session until it is destroyed or released by the
357 * underlying SSL structure.
359 * @param takeOwnership if true, AsyncSSLSocket will assume the caller's
360 * reference count to session.
362 void setSSLSession(SSL_SESSION *session, bool takeOwnership = false);
365 * Get the name of the protocol selected by the client during
366 * Next Protocol Negotiation (NPN) or Application Layer Protocol Negotiation
369 * Throw an exception if openssl does not support NPN
371 * @param protoName Name of the protocol (not guaranteed to be
372 * null terminated); will be set to nullptr if
373 * the client did not negotiate a protocol.
374 * Note: the AsyncSSLSocket retains ownership
376 * @param protoNameLen Length of the name.
377 * @param protoType Whether this was an NPN or ALPN negotiation
379 virtual void getSelectedNextProtocol(
380 const unsigned char** protoName,
382 SSLContext::NextProtocolType* protoType = nullptr) const;
385 * Get the name of the protocol selected by the client during
386 * Next Protocol Negotiation (NPN) or Application Layer Protocol Negotiation
389 * @param protoName Name of the protocol (not guaranteed to be
390 * null terminated); will be set to nullptr if
391 * the client did not negotiate a protocol.
392 * Note: the AsyncSSLSocket retains ownership
394 * @param protoNameLen Length of the name.
395 * @param protoType Whether this was an NPN or ALPN negotiation
396 * @return false if openssl does not support NPN
398 virtual bool getSelectedNextProtocolNoThrow(
399 const unsigned char** protoName,
401 SSLContext::NextProtocolType* protoType = nullptr) const;
404 * Determine if the session specified during setSSLSession was reused
405 * or if the server rejected it and issued a new session.
407 virtual bool getSSLSessionReused() const;
410 * true if the session was resumed using session ID
412 bool sessionIDResumed() const { return sessionIDResumed_; }
414 void setSessionIDResumed(bool resumed) {
415 sessionIDResumed_ = resumed;
419 * Get the negociated cipher name for this SSL connection.
420 * Returns the cipher used or the constant value "NONE" when no SSL session
421 * has been established.
423 virtual const char* getNegotiatedCipherName() const;
426 * Get the server name for this SSL connection.
427 * Returns the server name used or the constant value "NONE" when no SSL
428 * session has been established.
429 * If openssl has no SNI support, throw TTransportException.
431 const char *getSSLServerName() const;
434 * Get the server name for this SSL connection.
435 * Returns the server name used or the constant value "NONE" when no SSL
436 * session has been established.
437 * If openssl has no SNI support, return "NONE"
439 const char *getSSLServerNameNoThrow() const;
442 * Get the SSL version for this connection.
443 * Possible return values are SSL2_VERSION, SSL3_VERSION, TLS1_VERSION,
444 * with hexa representations 0x200, 0x300, 0x301,
445 * or 0 if no SSL session has been established.
447 int getSSLVersion() const;
450 * Get the signature algorithm used in the cert that is used for this
453 const char *getSSLCertSigAlgName() const;
456 * Get the certificate size used for this SSL connection.
458 int getSSLCertSize() const;
460 virtual void attachEventBase(EventBase* eventBase) override {
461 AsyncSocket::attachEventBase(eventBase);
462 handshakeTimeout_.attachEventBase(eventBase);
465 virtual void detachEventBase() override {
466 AsyncSocket::detachEventBase();
467 handshakeTimeout_.detachEventBase();
470 virtual bool isDetachable() const override {
471 return AsyncSocket::isDetachable() && !handshakeTimeout_.isScheduled();
474 virtual void attachTimeoutManager(TimeoutManager* manager) {
475 handshakeTimeout_.attachTimeoutManager(manager);
478 virtual void detachTimeoutManager() {
479 handshakeTimeout_.detachTimeoutManager();
482 #if OPENSSL_VERSION_NUMBER >= 0x009080bfL
484 * This function will set the SSL context for this socket to the
485 * argument. This should only be used on client SSL Sockets that have
486 * already called detachSSLContext();
488 void attachSSLContext(const std::shared_ptr<folly::SSLContext>& ctx);
491 * Detaches the SSL context for this socket.
493 void detachSSLContext();
496 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
498 * Switch the SSLContext to continue the SSL handshake.
499 * It can only be used in server mode.
501 void switchServerSSLContext(
502 const std::shared_ptr<folly::SSLContext>& handshakeCtx);
505 * Did server recognize/support the tlsext_hostname in Client Hello?
506 * It can only be used in client mode.
508 * @return true - tlsext_hostname is matched by the server
509 * false - tlsext_hostname is not matched or
510 * is not supported by server
512 bool isServerNameMatch() const;
515 * Set the SNI hostname that we'll advertise to the server in the
516 * ClientHello message.
518 void setServerName(std::string serverName) noexcept;
521 void timeoutExpired() noexcept;
524 * Get the list of supported ciphers sent by the client in the client's
527 void getSSLClientCiphers(
528 std::string& clientCiphers,
529 bool convertToString = true) const {
530 std::stringstream ciphersStream;
531 std::string cipherName;
533 if (parseClientHello_ == false
534 || clientHelloInfo_->clientHelloCipherSuites_.empty()) {
539 for (auto originalCipherCode : clientHelloInfo_->clientHelloCipherSuites_)
541 const SSL_CIPHER* cipher = nullptr;
542 if (convertToString) {
543 // OpenSSL expects code as a big endian char array
544 auto cipherCode = htons(originalCipherCode);
546 #if defined(SSL_OP_NO_TLSv1_2)
548 TLSv1_2_method()->get_cipher_by_char((unsigned char*)&cipherCode);
549 #elif defined(SSL_OP_NO_TLSv1_1)
551 TLSv1_1_method()->get_cipher_by_char((unsigned char*)&cipherCode);
552 #elif defined(SSL_OP_NO_TLSv1)
554 TLSv1_method()->get_cipher_by_char((unsigned char*)&cipherCode);
557 SSLv3_method()->get_cipher_by_char((unsigned char*)&cipherCode);
561 if (cipher == nullptr) {
562 ciphersStream << std::setfill('0') << std::setw(4) << std::hex
563 << originalCipherCode << ":";
565 ciphersStream << SSL_CIPHER_get_name(cipher) << ":";
569 clientCiphers = ciphersStream.str();
570 clientCiphers.erase(clientCiphers.end() - 1);
574 * Get the list of compression methods sent by the client in TLS Hello.
576 std::string getSSLClientComprMethods() const {
577 if (!parseClientHello_) {
580 return folly::join(":", clientHelloInfo_->clientHelloCompressionMethods_);
584 * Get the list of TLS extensions sent by the client in the TLS Hello.
586 std::string getSSLClientExts() const {
587 if (!parseClientHello_) {
590 return folly::join(":", clientHelloInfo_->clientHelloExtensions_);
593 std::string getSSLClientSigAlgs() const {
594 if (!parseClientHello_) {
599 sigAlgs.reserve(clientHelloInfo_->clientHelloSigAlgs_.size() * 4);
600 for (size_t i = 0; i < clientHelloInfo_->clientHelloSigAlgs_.size(); i++) {
602 sigAlgs.push_back(':');
604 sigAlgs.append(folly::to<std::string>(
605 clientHelloInfo_->clientHelloSigAlgs_[i].first));
606 sigAlgs.push_back(',');
607 sigAlgs.append(folly::to<std::string>(
608 clientHelloInfo_->clientHelloSigAlgs_[i].second));
614 std::string getSSLAlertsReceived() const {
617 for (const auto& alert : alertsReceived_) {
621 ret.append(folly::to<std::string>(alert.first, ": ", alert.second));
628 * Get the list of shared ciphers between the server and the client.
629 * Works well for only SSLv2, not so good for SSLv3 or TLSv1.
631 void getSSLSharedCiphers(std::string& sharedCiphers) const {
632 char ciphersBuffer[1024];
633 ciphersBuffer[0] = '\0';
634 SSL_get_shared_ciphers(ssl_, ciphersBuffer, sizeof(ciphersBuffer) - 1);
635 sharedCiphers = ciphersBuffer;
639 * Get the list of ciphers supported by the server in the server's
642 void getSSLServerCiphers(std::string& serverCiphers) const {
643 serverCiphers = SSL_get_cipher_list(ssl_, 0);
646 while ((cipher = SSL_get_cipher_list(ssl_, i)) != nullptr) {
647 serverCiphers.append(":");
648 serverCiphers.append(cipher);
653 static int getSSLExDataIndex();
654 static AsyncSSLSocket* getFromSSL(const SSL *ssl);
655 static int eorAwareBioWrite(BIO *b, const char *in, int inl);
656 void resetClientHelloParsing(SSL *ssl);
657 static void clientHelloParsingCallback(int write_p, int version,
658 int content_type, const void *buf, size_t len, SSL *ssl, void *arg);
659 static const char* getSSLServerNameFromSSL(SSL* ssl);
662 ssl::ClientHelloInfo* getClientHelloInfo() const {
663 return clientHelloInfo_.get();
667 * Returns the time taken to complete a handshake.
669 virtual std::chrono::nanoseconds getHandshakeTime() const {
670 return handshakeEndTime_ - handshakeStartTime_;
673 void setMinWriteSize(size_t minWriteSize) {
674 minWriteSize_ = minWriteSize;
677 size_t getMinWriteSize() const {
678 return minWriteSize_;
681 void setReadCB(ReadCallback* callback) override;
684 * Tries to enable the buffer movable experimental feature in openssl.
685 * This is not guaranteed to succeed in case openssl does not have
686 * the experimental feature built in.
688 void setBufferMovableEnabled(bool enabled);
691 * Returns the peer certificate, or nullptr if no peer certificate received.
693 virtual ssl::X509UniquePtr getPeerCert() const override {
698 X509* cert = SSL_get_peer_certificate(ssl_);
699 return ssl::X509UniquePtr(cert);
703 * Force AsyncSSLSocket object to cache local and peer socket addresses.
704 * If called with "true" before connect() this function forces full local
705 * and remote socket addresses to be cached in the socket object and available
706 * through getLocalAddress()/getPeerAddress() methods even after the socket is
709 void forceCacheAddrOnFailure(bool force) { cacheAddrOnFailure_ = force; }
711 const std::string& getServiceIdentity() const { return serviceIdentity_; }
713 void setServiceIdentity(std::string serviceIdentity) {
714 serviceIdentity_ = std::move(serviceIdentity);
724 * Protected destructor.
726 * Users of AsyncSSLSocket must never delete it directly. Instead, invoke
727 * destroy() instead. (See the documentation in DelayedDestruction.h for
732 // Inherit event notification methods from AsyncSocket except
734 void prepareReadBuffer(void** buf, size_t* buflen) noexcept override;
735 void handleRead() noexcept override;
736 void handleWrite() noexcept override;
737 void handleAccept() noexcept;
738 void handleConnect() noexcept override;
740 void invalidState(HandshakeCB* callback);
741 bool willBlock(int ret,
743 unsigned long* errErrorOut) noexcept;
745 virtual void checkForImmediateRead() noexcept override;
746 // AsyncSocket calls this at the wrong time for SSL
747 void handleInitialReadWrite() noexcept override {}
749 WriteResult interpretSSLError(int rc, int error);
750 ReadResult performRead(void** buf, size_t* buflen, size_t* offset) override;
751 WriteResult performWrite(
755 uint32_t* countWritten,
756 uint32_t* partialWritten) override;
758 ssize_t performWriteIovec(const iovec* vec, uint32_t count,
759 WriteFlags flags, uint32_t* countWritten,
760 uint32_t* partialWritten);
762 // This virtual wrapper around SSL_write exists solely for testing/mockability
763 virtual int sslWriteImpl(SSL *ssl, const void *buf, int n) {
764 return SSL_write(ssl, buf, n);
768 * Apply verification options passed to sslConn/sslAccept or those set
769 * in the underlying SSLContext object.
771 * @param ssl pointer to the SSL object on which verification options will be
772 * applied. If verifyPeer_ was explicitly set either via sslConn/sslAccept,
773 * those options override the settings in the underlying SSLContext.
775 void applyVerificationOptions(SSL * ssl);
778 * A SSL_write wrapper that understand EOR
780 * @param ssl: SSL* object
781 * @param buf: Buffer to be written
782 * @param n: Number of bytes to be written
783 * @param eor: Does the last byte (buf[n-1]) have the app-last-byte?
784 * @return: The number of app bytes successfully written to the socket
786 int eorAwareSSLWrite(SSL *ssl, const void *buf, int n, bool eor);
788 // Inherit error handling methods from AsyncSocket, plus the following.
789 void failHandshake(const char* fn, const AsyncSocketException& ex);
791 void invokeHandshakeErr(const AsyncSocketException& ex);
792 void invokeHandshakeCB();
794 void cacheLocalPeerAddr();
796 static void sslInfoCallback(const SSL *ssl, int type, int val);
798 // Whether we've applied the TCP_CORK option to the socket
800 // SSL related members.
802 // Used to prevent client-initiated renegotiation. Note that AsyncSSLSocket
803 // doesn't fully support renegotiation, so we could just fail all attempts
804 // to enforce this. Once it is supported, we should make it an option
805 // to disable client-initiated renegotiation.
806 bool handshakeComplete_{false};
807 bool renegotiateAttempted_{false};
808 SSLStateEnum sslState_{STATE_UNINIT};
809 std::shared_ptr<folly::SSLContext> ctx_;
810 // Callback for SSL_accept() or SSL_connect()
811 HandshakeCB* handshakeCallback_{nullptr};
813 SSL_SESSION *sslSession_{nullptr};
814 HandshakeTimeout handshakeTimeout_;
815 // whether the SSL session was resumed using session ID or not
816 bool sessionIDResumed_{false};
818 // The app byte num that we are tracking for the MSG_EOR
819 // Only one app EOR byte can be tracked.
820 size_t appEorByteNo_{0};
822 // Try to avoid calling SSL_write() for buffers smaller than this.
823 // It doesn't take effect when it is 0.
824 size_t minWriteSize_{1500};
826 // When openssl is about to sendmsg() across the minEorRawBytesNo_,
827 // it will pass MSG_EOR to sendmsg().
828 size_t minEorRawByteNo_{0};
829 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
830 std::shared_ptr<folly::SSLContext> handshakeCtx_;
831 std::string tlsextHostname_;
834 // a service identity that this socket/connection is associated with
835 std::string serviceIdentity_;
837 folly::SSLContext::SSLVerifyPeerEnum
838 verifyPeer_{folly::SSLContext::SSLVerifyPeerEnum::USE_CTX};
840 // Callback for SSL_CTX_set_verify()
841 static int sslVerifyCallback(int preverifyOk, X509_STORE_CTX* ctx);
843 bool parseClientHello_{false};
844 bool cacheAddrOnFailure_{false};
845 bool bufferMovableEnabled_{false};
846 std::unique_ptr<ssl::ClientHelloInfo> clientHelloInfo_;
847 std::vector<std::pair<char, StringPiece>> alertsReceived_;
849 // Time taken to complete the ssl handshake.
850 std::chrono::steady_clock::time_point handshakeStartTime_;
851 std::chrono::steady_clock::time_point handshakeEndTime_;