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.
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/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/OpenSSL.h>
36 #include <folly/portability/Sockets.h>
41 * A class for performing asynchronous I/O on an SSL connection.
43 * AsyncSSLSocket allows users to asynchronously wait for data on an
44 * SSL connection, and to asynchronously send data.
46 * The APIs for reading and writing are intentionally asymmetric.
47 * Waiting for data to read is a persistent API: a callback is
48 * installed, and is notified whenever new data is available. It
49 * continues to be notified of new events until it is uninstalled.
51 * AsyncSSLSocket does not provide read timeout functionality,
52 * because it typically cannot determine when the timeout should be
53 * active. Generally, a timeout should only be enabled when
54 * processing is blocked waiting on data from the remote endpoint.
55 * For server connections, the timeout should not be active if the
56 * server is currently processing one or more outstanding requests for
57 * this connection. For client connections, the timeout should not be
58 * active if there are no requests pending on the connection.
59 * Additionally, if a client has multiple pending requests, it will
60 * ususally want a separate timeout for each request, rather than a
61 * single read timeout.
63 * The write API is fairly intuitive: a user can request to send a
64 * block of data, and a callback will be informed once the entire
65 * block has been transferred to the kernel, or on error.
66 * AsyncSSLSocket does provide a send timeout, since most callers
67 * want to give up if the remote end stops responding and no further
68 * progress can be made sending the data.
70 class AsyncSSLSocket : public virtual AsyncSocket {
72 typedef std::unique_ptr<AsyncSSLSocket, Destructor> UniquePtr;
73 using X509_deleter = folly::static_function_deleter<X509, &X509_free>;
77 virtual ~HandshakeCB() = default;
80 * handshakeVer() is invoked during handshaking to give the
81 * application chance to validate it's peer's certificate.
83 * Note that OpenSSL performs only rudimentary internal
84 * consistency verification checks by itself. Any other validation
85 * like whether or not the certificate was issued by a trusted CA.
86 * The default implementation of this callback mimics what what
87 * OpenSSL does internally if SSL_VERIFY_PEER is set with no
88 * verification callback.
90 * See the passages on verify_callback in SSL_CTX_set_verify(3)
93 virtual bool handshakeVer(AsyncSSLSocket* /*sock*/,
95 X509_STORE_CTX* /*ctx*/) noexcept {
100 * handshakeSuc() is called when a new SSL connection is
101 * established, i.e., after SSL_accept/connect() returns successfully.
103 * The HandshakeCB will be uninstalled before handshakeSuc()
106 * @param sock SSL socket on which the handshake was initiated
108 virtual void handshakeSuc(AsyncSSLSocket *sock) noexcept = 0;
111 * handshakeErr() is called if an error occurs while
112 * establishing the SSL connection.
114 * The HandshakeCB will be uninstalled before handshakeErr()
117 * @param sock SSL socket on which the handshake was initiated
118 * @param ex An exception representing the error.
120 virtual void handshakeErr(
121 AsyncSSLSocket *sock,
122 const AsyncSocketException& ex)
126 class Timeout : public AsyncTimeout {
128 Timeout(AsyncSSLSocket* sslSocket, EventBase* eventBase)
129 : AsyncTimeout(eventBase), sslSocket_(sslSocket) {}
131 bool scheduleTimeout(TimeoutManager::timeout_type timeout) {
133 return AsyncTimeout::scheduleTimeout(timeout);
136 bool scheduleTimeout(uint32_t timeoutMs) {
137 return scheduleTimeout(std::chrono::milliseconds{timeoutMs});
140 TimeoutManager::timeout_type getTimeout() {
144 void timeoutExpired() noexcept override {
145 sslSocket_->timeoutExpired(timeout_);
149 AsyncSSLSocket* sslSocket_;
150 TimeoutManager::timeout_type timeout_;
154 * Create a client AsyncSSLSocket
156 AsyncSSLSocket(const std::shared_ptr<folly::SSLContext> &ctx,
157 EventBase* evb, bool deferSecurityNegotiation = false);
160 * Create a server/client AsyncSSLSocket from an already connected
161 * socket file descriptor.
163 * Note that while AsyncSSLSocket enables TCP_NODELAY for sockets it creates
164 * when connecting, it does not change the socket options when given an
165 * existing file descriptor. If callers want TCP_NODELAY enabled when using
166 * this version of the constructor, they need to explicitly call
167 * setNoDelay(true) after the constructor returns.
169 * @param ctx SSL context for this connection.
170 * @param evb EventBase that will manage this socket.
171 * @param fd File descriptor to take over (should be a connected socket).
172 * @param server Is socket in server mode?
173 * @param deferSecurityNegotiation
174 * unencrypted data can be sent before sslConn/Accept
177 const std::shared_ptr<folly::SSLContext>& ctx,
181 bool deferSecurityNegotiation = false);
184 * Create a server/client AsyncSSLSocket from an already connected
188 const std::shared_ptr<folly::SSLContext>& ctx,
189 AsyncSocket::UniquePtr oldAsyncSocket,
191 bool deferSecurityNegotiation = false);
194 * Helper function to create a server/client shared_ptr<AsyncSSLSocket>.
196 static std::shared_ptr<AsyncSSLSocket> newSocket(
197 const std::shared_ptr<folly::SSLContext>& ctx,
198 EventBase* evb, int fd, bool server=true,
199 bool deferSecurityNegotiation = false) {
200 return std::shared_ptr<AsyncSSLSocket>(
201 new AsyncSSLSocket(ctx, evb, fd, server, deferSecurityNegotiation),
206 * Helper function to create a client shared_ptr<AsyncSSLSocket>.
208 static std::shared_ptr<AsyncSSLSocket> newSocket(
209 const std::shared_ptr<folly::SSLContext>& ctx,
210 EventBase* evb, bool deferSecurityNegotiation = false) {
211 return std::shared_ptr<AsyncSSLSocket>(
212 new AsyncSSLSocket(ctx, evb, deferSecurityNegotiation),
217 #if FOLLY_OPENSSL_HAS_SNI
219 * Create a client AsyncSSLSocket with tlsext_servername in
220 * the Client Hello message.
222 AsyncSSLSocket(const std::shared_ptr<folly::SSLContext> &ctx,
224 const std::string& serverName,
225 bool deferSecurityNegotiation = false);
228 * Create a client AsyncSSLSocket from an already connected
229 * socket file descriptor.
231 * Note that while AsyncSSLSocket enables TCP_NODELAY for sockets it creates
232 * when connecting, it does not change the socket options when given an
233 * existing file descriptor. If callers want TCP_NODELAY enabled when using
234 * this version of the constructor, they need to explicitly call
235 * setNoDelay(true) after the constructor returns.
237 * @param ctx SSL context for this connection.
238 * @param evb EventBase that will manage this socket.
239 * @param fd File descriptor to take over (should be a connected socket).
240 * @param serverName tlsext_hostname that will be sent in ClientHello.
243 const std::shared_ptr<folly::SSLContext>& ctx,
246 const std::string& serverName,
247 bool deferSecurityNegotiation = false);
249 static std::shared_ptr<AsyncSSLSocket> newSocket(
250 const std::shared_ptr<folly::SSLContext>& ctx,
252 const std::string& serverName,
253 bool deferSecurityNegotiation = false) {
254 return std::shared_ptr<AsyncSSLSocket>(
255 new AsyncSSLSocket(ctx, evb, serverName, deferSecurityNegotiation),
258 #endif // FOLLY_OPENSSL_HAS_SNI
261 * TODO: implement support for SSL renegotiation.
263 * This involves proper handling of the SSL_ERROR_WANT_READ/WRITE
264 * code as a result of SSL_write/read(), instead of returning an
265 * error. In that case, the READ/WRITE event should be registered,
266 * and a flag (e.g., writeBlockedOnRead) should be set to indiciate
267 * the condition. In the next invocation of read/write callback, if
268 * the flag is on, performWrite()/performRead() should be called in
269 * addition to the normal call to performRead()/performWrite(), and
270 * the flag should be reset.
273 // Inherit TAsyncTransport methods from AsyncSocket except the
275 // See the documentation in TAsyncTransport.h
276 // TODO: implement graceful shutdown in close()
277 // TODO: implement detachSSL() that returns the SSL connection
278 void closeNow() override;
279 void shutdownWrite() override;
280 void shutdownWriteNow() override;
281 bool good() const override;
282 bool connecting() const override;
283 std::string getApplicationProtocol() noexcept override;
285 std::string getSecurityProtocol() const override {
289 void setEorTracking(bool track) override;
290 size_t getRawBytesWritten() const override;
291 size_t getRawBytesReceived() const override;
292 void enableClientHelloParsing();
295 * Accept an SSL connection on the socket.
297 * The callback will be invoked and uninstalled when an SSL
298 * connection has been established on the underlying socket.
299 * The value of verifyPeer determines the client verification method.
300 * By default, its set to use the value in the underlying context
302 * @param callback callback object to invoke on success/failure
303 * @param timeout timeout for this function in milliseconds, or 0 for no
305 * @param verifyPeer SSLVerifyPeerEnum uses the options specified in the
306 * context by default, can be set explcitly to override the
307 * method in the context
309 virtual void sslAccept(
310 HandshakeCB* callback,
311 std::chrono::milliseconds timeout = std::chrono::milliseconds::zero(),
312 const folly::SSLContext::SSLVerifyPeerEnum& verifyPeer =
313 folly::SSLContext::SSLVerifyPeerEnum::USE_CTX);
316 * Invoke SSL accept following an asynchronous session cache lookup
318 void restartSSLAccept();
321 * Connect to the given address, invoking callback when complete or on error
323 * Note timeout applies to TCP + SSL connection time
325 void connect(ConnectCallback* callback,
326 const folly::SocketAddress& address,
328 const OptionMap &options = emptyOptionMap,
329 const folly::SocketAddress& bindAddr = anyAddress())
333 * A variant of connect that allows the caller to specify
334 * the timeout for the regular connect and the ssl connect
336 * connectTimeout is specified as the time to establish a TCP
338 * totalConnectTimeout defines the
339 * time it takes from starting the TCP connection to the time
340 * the ssl connection is established. The reason the timeout is
341 * defined this way is because user's rarely need to specify the SSL
342 * timeout independently of the connect timeout. It allows us to
343 * bound the time for a connect and SSL connection in
344 * a finer grained manner than if timeout was just defined
345 * independently for SSL.
347 virtual void connect(
348 ConnectCallback* callback,
349 const folly::SocketAddress& address,
350 std::chrono::milliseconds connectTimeout,
351 std::chrono::milliseconds totalConnectTimeout,
352 const OptionMap& options = emptyOptionMap,
353 const folly::SocketAddress& bindAddr = anyAddress()) noexcept;
355 using AsyncSocket::connect;
358 * Initiate an SSL connection on the socket
359 * The callback will be invoked and uninstalled when an SSL connection
360 * has been establshed on the underlying socket.
361 * The verification option verifyPeer is applied if it's passed explicitly.
362 * If it's not, the options in SSLContext set on the underlying SSLContext
365 * @param callback callback object to invoke on success/failure
366 * @param timeout timeout for this function in milliseconds, or 0 for no
368 * @param verifyPeer SSLVerifyPeerEnum uses the options specified in the
369 * context by default, can be set explcitly to override the
370 * method in the context. If verification is turned on sets
371 * SSL_VERIFY_PEER and invokes
372 * HandshakeCB::handshakeVer().
374 virtual void sslConn(
375 HandshakeCB* callback,
376 std::chrono::milliseconds timeout = std::chrono::milliseconds::zero(),
377 const folly::SSLContext::SSLVerifyPeerEnum& verifyPeer =
378 folly::SSLContext::SSLVerifyPeerEnum::USE_CTX);
388 STATE_REMOTE_CLOSED, /// remote end closed; we can still write
389 STATE_CLOSING, ///< close() called, but waiting on writes to complete
390 /// close() called with pending writes, before connect() has completed
391 STATE_CONNECTING_CLOSING,
396 SSLStateEnum getSSLState() const { return sslState_;}
399 * Get a handle to the negotiated SSL session. This increments the session
400 * refcount and must be deallocated by the caller.
402 SSL_SESSION *getSSLSession();
405 * Get a handle to the SSL struct.
407 const SSL* getSSL() const;
410 * Set the SSL session to be used during sslConn. AsyncSSLSocket will
411 * hold a reference to the session until it is destroyed or released by the
412 * underlying SSL structure.
414 * @param takeOwnership if true, AsyncSSLSocket will assume the caller's
415 * reference count to session.
417 void setSSLSession(SSL_SESSION *session, bool takeOwnership = false);
420 * Get the name of the protocol selected by the client during
421 * Next Protocol Negotiation (NPN) or Application Layer Protocol Negotiation
424 * Throw an exception if openssl does not support NPN
426 * @param protoName Name of the protocol (not guaranteed to be
427 * null terminated); will be set to nullptr if
428 * the client did not negotiate a protocol.
429 * Note: the AsyncSSLSocket retains ownership
431 * @param protoNameLen Length of the name.
432 * @param protoType Whether this was an NPN or ALPN negotiation
434 virtual void getSelectedNextProtocol(
435 const unsigned char** protoName,
437 SSLContext::NextProtocolType* protoType = nullptr) const;
440 * Get the name of the protocol selected by the client during
441 * Next Protocol Negotiation (NPN) or Application Layer Protocol Negotiation
444 * @param protoName Name of the protocol (not guaranteed to be
445 * null terminated); will be set to nullptr if
446 * the client did not negotiate a protocol.
447 * Note: the AsyncSSLSocket retains ownership
449 * @param protoNameLen Length of the name.
450 * @param protoType Whether this was an NPN or ALPN negotiation
451 * @return false if openssl does not support NPN
453 virtual bool getSelectedNextProtocolNoThrow(
454 const unsigned char** protoName,
456 SSLContext::NextProtocolType* protoType = nullptr) const;
459 * Determine if the session specified during setSSLSession was reused
460 * or if the server rejected it and issued a new session.
462 virtual bool getSSLSessionReused() const;
465 * true if the session was resumed using session ID
467 bool sessionIDResumed() const { return sessionIDResumed_; }
469 void setSessionIDResumed(bool resumed) {
470 sessionIDResumed_ = resumed;
474 * Get the negociated cipher name for this SSL connection.
475 * Returns the cipher used or the constant value "NONE" when no SSL session
476 * has been established.
478 virtual const char* getNegotiatedCipherName() const;
481 * Get the server name for this SSL connection.
482 * Returns the server name used or the constant value "NONE" when no SSL
483 * session has been established.
484 * If openssl has no SNI support, throw TTransportException.
486 const char *getSSLServerName() const;
489 * Get the server name for this SSL connection.
490 * Returns the server name used or the constant value "NONE" when no SSL
491 * session has been established.
492 * If openssl has no SNI support, return "NONE"
494 const char *getSSLServerNameNoThrow() const;
497 * Get the SSL version for this connection.
498 * Possible return values are SSL2_VERSION, SSL3_VERSION, TLS1_VERSION,
499 * with hexa representations 0x200, 0x300, 0x301,
500 * or 0 if no SSL session has been established.
502 int getSSLVersion() const;
505 * Get the signature algorithm used in the cert that is used for this
508 const char *getSSLCertSigAlgName() const;
511 * Get the certificate size used for this SSL connection.
513 int getSSLCertSize() const;
516 * Get the certificate used for this SSL connection. May be null
518 const X509* getSelfCert() const override;
520 void attachEventBase(EventBase* eventBase) override {
521 AsyncSocket::attachEventBase(eventBase);
522 handshakeTimeout_.attachEventBase(eventBase);
523 connectionTimeout_.attachEventBase(eventBase);
526 void detachEventBase() override {
527 AsyncSocket::detachEventBase();
528 handshakeTimeout_.detachEventBase();
529 connectionTimeout_.detachEventBase();
532 bool isDetachable() const override {
533 return AsyncSocket::isDetachable() && !handshakeTimeout_.isScheduled();
536 virtual void attachTimeoutManager(TimeoutManager* manager) {
537 handshakeTimeout_.attachTimeoutManager(manager);
540 virtual void detachTimeoutManager() {
541 handshakeTimeout_.detachTimeoutManager();
544 #if OPENSSL_VERSION_NUMBER >= 0x009080bfL
546 * This function will set the SSL context for this socket to the
547 * argument. This should only be used on client SSL Sockets that have
548 * already called detachSSLContext();
550 void attachSSLContext(const std::shared_ptr<folly::SSLContext>& ctx);
553 * Detaches the SSL context for this socket.
555 void detachSSLContext();
558 #if FOLLY_OPENSSL_HAS_SNI
560 * Switch the SSLContext to continue the SSL handshake.
561 * It can only be used in server mode.
563 void switchServerSSLContext(
564 const std::shared_ptr<folly::SSLContext>& handshakeCtx);
567 * Did server recognize/support the tlsext_hostname in Client Hello?
568 * It can only be used in client mode.
570 * @return true - tlsext_hostname is matched by the server
571 * false - tlsext_hostname is not matched or
572 * is not supported by server
574 bool isServerNameMatch() const;
577 * Set the SNI hostname that we'll advertise to the server in the
578 * ClientHello message.
580 void setServerName(std::string serverName) noexcept;
581 #endif // FOLLY_OPENSSL_HAS_SNI
583 void timeoutExpired(std::chrono::milliseconds timeout) noexcept;
586 * Get the list of supported ciphers sent by the client in the client's
589 void getSSLClientCiphers(
590 std::string& clientCiphers,
591 bool convertToString = true) const;
594 * Get the list of compression methods sent by the client in TLS Hello.
596 std::string getSSLClientComprMethods() const;
599 * Get the list of TLS extensions sent by the client in the TLS Hello.
601 std::string getSSLClientExts() const;
603 std::string getSSLClientSigAlgs() const;
606 * Get the list of versions in the supported versions extension (used to
607 * negotiate TLS 1.3).
609 std::string getSSLClientSupportedVersions() const;
611 std::string getSSLAlertsReceived() const;
614 * Save an optional alert message generated during certificate verify
616 void setSSLCertVerificationAlert(std::string alert);
618 std::string getSSLCertVerificationAlert() const;
621 * Get the list of shared ciphers between the server and the client.
622 * Works well for only SSLv2, not so good for SSLv3 or TLSv1.
624 void getSSLSharedCiphers(std::string& sharedCiphers) const;
627 * Get the list of ciphers supported by the server in the server's
630 void getSSLServerCiphers(std::string& serverCiphers) const;
633 * Method to check if peer verfication is set.
635 * @return true if peer verification is required.
637 bool needsPeerVerification() const;
639 static int getSSLExDataIndex();
640 static AsyncSSLSocket* getFromSSL(const SSL *ssl);
641 static int bioWrite(BIO* b, const char* in, int inl);
642 static int bioRead(BIO* b, char* out, int outl);
643 void resetClientHelloParsing(SSL *ssl);
644 static void clientHelloParsingCallback(int write_p, int version,
645 int content_type, const void *buf, size_t len, SSL *ssl, void *arg);
646 static const char* getSSLServerNameFromSSL(SSL* ssl);
649 ssl::ClientHelloInfo* getClientHelloInfo() const {
650 return clientHelloInfo_.get();
654 * Returns the time taken to complete a handshake.
656 virtual std::chrono::nanoseconds getHandshakeTime() const {
657 return handshakeEndTime_ - handshakeStartTime_;
660 void setMinWriteSize(size_t minWriteSize) {
661 minWriteSize_ = minWriteSize;
664 size_t getMinWriteSize() const {
665 return minWriteSize_;
668 void setReadCB(ReadCallback* callback) override;
671 * Tries to enable the buffer movable experimental feature in openssl.
672 * This is not guaranteed to succeed in case openssl does not have
673 * the experimental feature built in.
675 void setBufferMovableEnabled(bool enabled);
678 * Returns the peer certificate, or nullptr if no peer certificate received.
680 ssl::X509UniquePtr getPeerCert() const override {
685 X509* cert = SSL_get_peer_certificate(ssl_);
686 return ssl::X509UniquePtr(cert);
690 * Force AsyncSSLSocket object to cache local and peer socket addresses.
691 * If called with "true" before connect() this function forces full local
692 * and remote socket addresses to be cached in the socket object and available
693 * through getLocalAddress()/getPeerAddress() methods even after the socket is
696 void forceCacheAddrOnFailure(bool force) { cacheAddrOnFailure_ = force; }
698 const std::string& getServiceIdentity() const { return serviceIdentity_; }
700 void setServiceIdentity(std::string serviceIdentity) {
701 serviceIdentity_ = std::move(serviceIdentity);
704 void setCertCacheHit(bool hit) {
708 bool getCertCacheHit() const {
709 return certCacheHit_;
712 bool sessionResumptionAttempted() const {
713 return sessionResumptionAttempted_;
717 * If the SSL socket was used to connect as well
718 * as establish an SSL connection, this gives the total
719 * timeout for the connect + SSL connection that was
722 std::chrono::milliseconds getTotalConnectTimeout() const {
723 return totalConnectTimeout_;
733 * Protected destructor.
735 * Users of AsyncSSLSocket must never delete it directly. Instead, invoke
736 * destroy() instead. (See the documentation in DelayedDestruction.h for
739 ~AsyncSSLSocket() override;
741 // Inherit event notification methods from AsyncSocket except
743 void prepareReadBuffer(void** buf, size_t* buflen) override;
744 void handleRead() noexcept override;
745 void handleWrite() noexcept override;
746 void handleAccept() noexcept;
747 void handleConnect() noexcept override;
749 void invalidState(HandshakeCB* callback);
750 bool willBlock(int ret,
752 unsigned long* errErrorOut) noexcept;
754 void checkForImmediateRead() noexcept override;
755 // AsyncSocket calls this at the wrong time for SSL
756 void handleInitialReadWrite() noexcept override {}
758 WriteResult interpretSSLError(int rc, int error);
759 ReadResult performRead(void** buf, size_t* buflen, size_t* offset) override;
760 WriteResult performWrite(
764 uint32_t* countWritten,
765 uint32_t* partialWritten) override;
767 ssize_t performWriteIovec(const iovec* vec, uint32_t count,
768 WriteFlags flags, uint32_t* countWritten,
769 uint32_t* partialWritten);
771 // This virtual wrapper around SSL_write exists solely for testing/mockability
772 virtual int sslWriteImpl(SSL *ssl, const void *buf, int n) {
773 return SSL_write(ssl, buf, n);
777 * Apply verification options passed to sslConn/sslAccept or those set
778 * in the underlying SSLContext object.
780 * @param ssl pointer to the SSL object on which verification options will be
781 * applied. If verifyPeer_ was explicitly set either via sslConn/sslAccept,
782 * those options override the settings in the underlying SSLContext.
784 void applyVerificationOptions(SSL * ssl);
787 * Sets up SSL with a custom write bio which intercepts all writes.
789 * @return true, if succeeds and false if there is an error creating the bio.
794 * A SSL_write wrapper that understand EOR
796 * @param ssl: SSL* object
797 * @param buf: Buffer to be written
798 * @param n: Number of bytes to be written
799 * @param eor: Does the last byte (buf[n-1]) have the app-last-byte?
800 * @return: The number of app bytes successfully written to the socket
802 int eorAwareSSLWrite(SSL *ssl, const void *buf, int n, bool eor);
804 // Inherit error handling methods from AsyncSocket, plus the following.
805 void failHandshake(const char* fn, const AsyncSocketException& ex);
807 void invokeHandshakeErr(const AsyncSocketException& ex);
808 void invokeHandshakeCB();
810 void invokeConnectErr(const AsyncSocketException& ex) override;
811 void invokeConnectSuccess() override;
812 void scheduleConnectTimeout() override;
814 void startSSLConnect();
816 static void sslInfoCallback(const SSL *ssl, int type, int val);
818 // Whether the current write to the socket should use MSG_MORE.
819 bool corkCurrentWrite_{false};
820 // SSL related members.
822 // Used to prevent client-initiated renegotiation. Note that AsyncSSLSocket
823 // doesn't fully support renegotiation, so we could just fail all attempts
824 // to enforce this. Once it is supported, we should make it an option
825 // to disable client-initiated renegotiation.
826 bool handshakeComplete_{false};
827 bool renegotiateAttempted_{false};
828 SSLStateEnum sslState_{STATE_UNINIT};
829 std::shared_ptr<folly::SSLContext> ctx_;
830 // Callback for SSL_accept() or SSL_connect()
831 HandshakeCB* handshakeCallback_{nullptr};
833 SSL_SESSION *sslSession_{nullptr};
834 Timeout handshakeTimeout_;
835 Timeout connectionTimeout_;
836 // whether the SSL session was resumed using session ID or not
837 bool sessionIDResumed_{false};
839 // The app byte num that we are tracking for the MSG_EOR
840 // Only one app EOR byte can be tracked.
841 size_t appEorByteNo_{0};
843 // Try to avoid calling SSL_write() for buffers smaller than this.
844 // It doesn't take effect when it is 0.
845 size_t minWriteSize_{1500};
847 // When openssl is about to sendmsg() across the minEorRawBytesNo_,
848 // it will pass MSG_EOR to sendmsg().
849 size_t minEorRawByteNo_{0};
850 #if FOLLY_OPENSSL_HAS_SNI
851 std::shared_ptr<folly::SSLContext> handshakeCtx_;
852 std::string tlsextHostname_;
855 // a service identity that this socket/connection is associated with
856 std::string serviceIdentity_;
858 folly::SSLContext::SSLVerifyPeerEnum
859 verifyPeer_{folly::SSLContext::SSLVerifyPeerEnum::USE_CTX};
861 // Callback for SSL_CTX_set_verify()
862 static int sslVerifyCallback(int preverifyOk, X509_STORE_CTX* ctx);
864 bool parseClientHello_{false};
865 bool cacheAddrOnFailure_{false};
866 bool bufferMovableEnabled_{false};
867 bool certCacheHit_{false};
868 std::unique_ptr<ssl::ClientHelloInfo> clientHelloInfo_;
869 std::vector<std::pair<char, StringPiece>> alertsReceived_;
871 // Time taken to complete the ssl handshake.
872 std::chrono::steady_clock::time_point handshakeStartTime_;
873 std::chrono::steady_clock::time_point handshakeEndTime_;
874 std::chrono::milliseconds handshakeConnectTimeout_{0};
875 bool sessionResumptionAttempted_{false};
876 std::chrono::milliseconds totalConnectTimeout_{0};
878 std::string sslVerificationAlert_;