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.
19 #include <arpa/inet.h>
22 #include <folly/Optional.h>
23 #include <folly/String.h>
24 #include <folly/io/async/AsyncSocket.h>
25 #include <folly/io/async/AsyncTimeout.h>
26 #include <folly/io/async/SSLContext.h>
27 #include <folly/io/async/TimeoutManager.h>
28 #include <folly/io/async/ssl/OpenSSLPtrTypes.h>
29 #include <folly/io/async/ssl/OpenSSLUtils.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>
38 class SSLException: public folly::AsyncSocketException {
40 SSLException(int sslError,
41 unsigned long errError,
42 int sslOperationReturnValue,
47 * A class for performing asynchronous I/O on an SSL connection.
49 * AsyncSSLSocket allows users to asynchronously wait for data on an
50 * SSL connection, and to asynchronously send data.
52 * The APIs for reading and writing are intentionally asymmetric.
53 * Waiting for data to read is a persistent API: a callback is
54 * installed, and is notified whenever new data is available. It
55 * continues to be notified of new events until it is uninstalled.
57 * AsyncSSLSocket does not provide read timeout functionality,
58 * because it typically cannot determine when the timeout should be
59 * active. Generally, a timeout should only be enabled when
60 * processing is blocked waiting on data from the remote endpoint.
61 * For server connections, the timeout should not be active if the
62 * server is currently processing one or more outstanding requests for
63 * this connection. For client connections, the timeout should not be
64 * active if there are no requests pending on the connection.
65 * Additionally, if a client has multiple pending requests, it will
66 * ususally want a separate timeout for each request, rather than a
67 * single read timeout.
69 * The write API is fairly intuitive: a user can request to send a
70 * block of data, and a callback will be informed once the entire
71 * block has been transferred to the kernel, or on error.
72 * AsyncSSLSocket does provide a send timeout, since most callers
73 * want to give up if the remote end stops responding and no further
74 * progress can be made sending the data.
76 class AsyncSSLSocket : public virtual AsyncSocket {
78 typedef std::unique_ptr<AsyncSSLSocket, Destructor> UniquePtr;
79 using X509_deleter = folly::static_function_deleter<X509, &X509_free>;
83 virtual ~HandshakeCB() = default;
86 * handshakeVer() is invoked during handshaking to give the
87 * application chance to validate it's peer's certificate.
89 * Note that OpenSSL performs only rudimentary internal
90 * consistency verification checks by itself. Any other validation
91 * like whether or not the certificate was issued by a trusted CA.
92 * The default implementation of this callback mimics what what
93 * OpenSSL does internally if SSL_VERIFY_PEER is set with no
94 * verification callback.
96 * See the passages on verify_callback in SSL_CTX_set_verify(3)
99 virtual bool handshakeVer(AsyncSSLSocket* /*sock*/,
101 X509_STORE_CTX* /*ctx*/) noexcept {
106 * handshakeSuc() is called when a new SSL connection is
107 * established, i.e., after SSL_accept/connect() returns successfully.
109 * The HandshakeCB will be uninstalled before handshakeSuc()
112 * @param sock SSL socket on which the handshake was initiated
114 virtual void handshakeSuc(AsyncSSLSocket *sock) noexcept = 0;
117 * handshakeErr() is called if an error occurs while
118 * establishing the SSL connection.
120 * The HandshakeCB will be uninstalled before handshakeErr()
123 * @param sock SSL socket on which the handshake was initiated
124 * @param ex An exception representing the error.
126 virtual void handshakeErr(
127 AsyncSSLSocket *sock,
128 const AsyncSocketException& ex)
132 class HandshakeTimeout : public AsyncTimeout {
134 HandshakeTimeout(AsyncSSLSocket* sslSocket, EventBase* eventBase)
135 : AsyncTimeout(eventBase)
136 , sslSocket_(sslSocket) {}
138 virtual void timeoutExpired() noexcept {
139 sslSocket_->timeoutExpired();
143 AsyncSSLSocket* sslSocket_;
148 * These are passed to the application via errno, packed in an SSL err which
149 * are outside the valid errno range. The values are chosen to be unique
150 * against values in ssl.h
153 SSL_CLIENT_RENEGOTIATION_ATTEMPT = 900,
154 SSL_INVALID_RENEGOTIATION = 901,
155 SSL_EARLY_WRITE = 902
159 * Create a client AsyncSSLSocket
161 AsyncSSLSocket(const std::shared_ptr<folly::SSLContext> &ctx,
162 EventBase* evb, bool deferSecurityNegotiation = false);
165 * Create a server/client AsyncSSLSocket from an already connected
166 * socket file descriptor.
168 * Note that while AsyncSSLSocket enables TCP_NODELAY for sockets it creates
169 * when connecting, it does not change the socket options when given an
170 * existing file descriptor. If callers want TCP_NODELAY enabled when using
171 * this version of the constructor, they need to explicitly call
172 * setNoDelay(true) after the constructor returns.
174 * @param ctx SSL context for this connection.
175 * @param evb EventBase that will manage this socket.
176 * @param fd File descriptor to take over (should be a connected socket).
177 * @param server Is socket in server mode?
178 * @param deferSecurityNegotiation
179 * unencrypted data can be sent before sslConn/Accept
181 AsyncSSLSocket(const std::shared_ptr<folly::SSLContext>& ctx,
182 EventBase* evb, int fd,
183 bool server = true, bool deferSecurityNegotiation = false);
187 * Helper function to create a server/client shared_ptr<AsyncSSLSocket>.
189 static std::shared_ptr<AsyncSSLSocket> newSocket(
190 const std::shared_ptr<folly::SSLContext>& ctx,
191 EventBase* evb, int fd, bool server=true,
192 bool deferSecurityNegotiation = false) {
193 return std::shared_ptr<AsyncSSLSocket>(
194 new AsyncSSLSocket(ctx, evb, fd, server, deferSecurityNegotiation),
199 * Helper function to create a client shared_ptr<AsyncSSLSocket>.
201 static std::shared_ptr<AsyncSSLSocket> newSocket(
202 const std::shared_ptr<folly::SSLContext>& ctx,
203 EventBase* evb, bool deferSecurityNegotiation = false) {
204 return std::shared_ptr<AsyncSSLSocket>(
205 new AsyncSSLSocket(ctx, evb, deferSecurityNegotiation),
210 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
212 * Create a client AsyncSSLSocket with tlsext_servername in
213 * the Client Hello message.
215 AsyncSSLSocket(const std::shared_ptr<folly::SSLContext> &ctx,
217 const std::string& serverName,
218 bool deferSecurityNegotiation = false);
221 * Create a client AsyncSSLSocket from an already connected
222 * socket file descriptor.
224 * Note that while AsyncSSLSocket enables TCP_NODELAY for sockets it creates
225 * when connecting, it does not change the socket options when given an
226 * existing file descriptor. If callers want TCP_NODELAY enabled when using
227 * this version of the constructor, they need to explicitly call
228 * setNoDelay(true) after the constructor returns.
230 * @param ctx SSL context for this connection.
231 * @param evb EventBase that will manage this socket.
232 * @param fd File descriptor to take over (should be a connected socket).
233 * @param serverName tlsext_hostname that will be sent in ClientHello.
235 AsyncSSLSocket(const std::shared_ptr<folly::SSLContext>& ctx,
238 const std::string& serverName,
239 bool deferSecurityNegotiation = false);
241 static std::shared_ptr<AsyncSSLSocket> newSocket(
242 const std::shared_ptr<folly::SSLContext>& ctx,
244 const std::string& serverName,
245 bool deferSecurityNegotiation = false) {
246 return std::shared_ptr<AsyncSSLSocket>(
247 new AsyncSSLSocket(ctx, evb, serverName, deferSecurityNegotiation),
253 * TODO: implement support for SSL renegotiation.
255 * This involves proper handling of the SSL_ERROR_WANT_READ/WRITE
256 * code as a result of SSL_write/read(), instead of returning an
257 * error. In that case, the READ/WRITE event should be registered,
258 * and a flag (e.g., writeBlockedOnRead) should be set to indiciate
259 * the condition. In the next invocation of read/write callback, if
260 * the flag is on, performWrite()/performRead() should be called in
261 * addition to the normal call to performRead()/performWrite(), and
262 * the flag should be reset.
265 // Inherit TAsyncTransport methods from AsyncSocket except the
267 // See the documentation in TAsyncTransport.h
268 // TODO: implement graceful shutdown in close()
269 // TODO: implement detachSSL() that returns the SSL connection
270 virtual void closeNow() override;
271 virtual void shutdownWrite() override;
272 virtual void shutdownWriteNow() override;
273 virtual bool good() const override;
274 virtual bool connecting() const override;
275 virtual std::string getApplicationProtocol() noexcept override;
277 virtual std::string getSecurityProtocol() const override { return "TLS"; }
279 bool isEorTrackingEnabled() const override;
280 virtual void setEorTracking(bool track) override;
281 virtual size_t getRawBytesWritten() const override;
282 virtual size_t getRawBytesReceived() const override;
283 void enableClientHelloParsing();
286 * Accept an SSL connection on the socket.
288 * The callback will be invoked and uninstalled when an SSL
289 * connection has been established on the underlying socket.
290 * The value of verifyPeer determines the client verification method.
291 * By default, its set to use the value in the underlying context
293 * @param callback callback object to invoke on success/failure
294 * @param timeout timeout for this function in milliseconds, or 0 for no
296 * @param verifyPeer SSLVerifyPeerEnum uses the options specified in the
297 * context by default, can be set explcitly to override the
298 * method in the context
300 virtual void sslAccept(HandshakeCB* callback, uint32_t timeout = 0,
301 const folly::SSLContext::SSLVerifyPeerEnum& verifyPeer =
302 folly::SSLContext::SSLVerifyPeerEnum::USE_CTX);
305 * Invoke SSL accept following an asynchronous session cache lookup
307 void restartSSLAccept();
310 * Connect to the given address, invoking callback when complete or on error
312 * Note timeout applies to TCP + SSL connection time
314 void connect(ConnectCallback* callback,
315 const folly::SocketAddress& address,
317 const OptionMap &options = emptyOptionMap,
318 const folly::SocketAddress& bindAddr = anyAddress())
321 using AsyncSocket::connect;
324 * Initiate an SSL connection on the socket
325 * The callback will be invoked and uninstalled when an SSL connection
326 * has been establshed on the underlying socket.
327 * The verification option verifyPeer is applied if it's passed explicitly.
328 * If it's not, the options in SSLContext set on the underlying SSLContext
331 * @param callback callback object to invoke on success/failure
332 * @param timeout timeout for this function in milliseconds, or 0 for no
334 * @param verifyPeer SSLVerifyPeerEnum uses the options specified in the
335 * context by default, can be set explcitly to override the
336 * method in the context. If verification is turned on sets
337 * SSL_VERIFY_PEER and invokes
338 * HandshakeCB::handshakeVer().
340 virtual void sslConn(HandshakeCB *callback, uint64_t timeout = 0,
341 const folly::SSLContext::SSLVerifyPeerEnum& verifyPeer =
342 folly::SSLContext::SSLVerifyPeerEnum::USE_CTX);
352 STATE_REMOTE_CLOSED, /// remote end closed; we can still write
353 STATE_CLOSING, ///< close() called, but waiting on writes to complete
354 /// close() called with pending writes, before connect() has completed
355 STATE_CONNECTING_CLOSING,
360 SSLStateEnum getSSLState() const { return sslState_;}
363 * Get a handle to the negotiated SSL session. This increments the session
364 * refcount and must be deallocated by the caller.
366 SSL_SESSION *getSSLSession();
369 * Set the SSL session to be used during sslConn. AsyncSSLSocket will
370 * hold a reference to the session until it is destroyed or released by the
371 * underlying SSL structure.
373 * @param takeOwnership if true, AsyncSSLSocket will assume the caller's
374 * reference count to session.
376 void setSSLSession(SSL_SESSION *session, bool takeOwnership = false);
379 * Get the name of the protocol selected by the client during
380 * Next Protocol Negotiation (NPN) or Application Layer Protocol Negotiation
383 * Throw an exception if openssl does not support NPN
385 * @param protoName Name of the protocol (not guaranteed to be
386 * null terminated); will be set to nullptr if
387 * the client did not negotiate a protocol.
388 * Note: the AsyncSSLSocket retains ownership
390 * @param protoNameLen Length of the name.
391 * @param protoType Whether this was an NPN or ALPN negotiation
393 virtual void getSelectedNextProtocol(
394 const unsigned char** protoName,
396 SSLContext::NextProtocolType* protoType = nullptr) const;
399 * Get the name of the protocol selected by the client during
400 * Next Protocol Negotiation (NPN) or Application Layer Protocol Negotiation
403 * @param protoName Name of the protocol (not guaranteed to be
404 * null terminated); will be set to nullptr if
405 * the client did not negotiate a protocol.
406 * Note: the AsyncSSLSocket retains ownership
408 * @param protoNameLen Length of the name.
409 * @param protoType Whether this was an NPN or ALPN negotiation
410 * @return false if openssl does not support NPN
412 virtual bool getSelectedNextProtocolNoThrow(
413 const unsigned char** protoName,
415 SSLContext::NextProtocolType* protoType = nullptr) const;
418 * Determine if the session specified during setSSLSession was reused
419 * or if the server rejected it and issued a new session.
421 virtual bool getSSLSessionReused() const;
424 * true if the session was resumed using session ID
426 bool sessionIDResumed() const { return sessionIDResumed_; }
428 void setSessionIDResumed(bool resumed) {
429 sessionIDResumed_ = resumed;
433 * Get the negociated cipher name for this SSL connection.
434 * Returns the cipher used or the constant value "NONE" when no SSL session
435 * has been established.
437 virtual const char* getNegotiatedCipherName() const;
440 * Get the server name for this SSL connection.
441 * Returns the server name used or the constant value "NONE" when no SSL
442 * session has been established.
443 * If openssl has no SNI support, throw TTransportException.
445 const char *getSSLServerName() const;
448 * Get the server name for this SSL connection.
449 * Returns the server name used or the constant value "NONE" when no SSL
450 * session has been established.
451 * If openssl has no SNI support, return "NONE"
453 const char *getSSLServerNameNoThrow() const;
456 * Get the SSL version for this connection.
457 * Possible return values are SSL2_VERSION, SSL3_VERSION, TLS1_VERSION,
458 * with hexa representations 0x200, 0x300, 0x301,
459 * or 0 if no SSL session has been established.
461 int getSSLVersion() const;
464 * Get the signature algorithm used in the cert that is used for this
467 const char *getSSLCertSigAlgName() const;
470 * Get the certificate size used for this SSL connection.
472 int getSSLCertSize() const;
474 virtual void attachEventBase(EventBase* eventBase) override {
475 AsyncSocket::attachEventBase(eventBase);
476 handshakeTimeout_.attachEventBase(eventBase);
479 virtual void detachEventBase() override {
480 AsyncSocket::detachEventBase();
481 handshakeTimeout_.detachEventBase();
484 virtual bool isDetachable() const override {
485 return AsyncSocket::isDetachable() && !handshakeTimeout_.isScheduled();
488 virtual void attachTimeoutManager(TimeoutManager* manager) {
489 handshakeTimeout_.attachTimeoutManager(manager);
492 virtual void detachTimeoutManager() {
493 handshakeTimeout_.detachTimeoutManager();
496 #if OPENSSL_VERSION_NUMBER >= 0x009080bfL
498 * This function will set the SSL context for this socket to the
499 * argument. This should only be used on client SSL Sockets that have
500 * already called detachSSLContext();
502 void attachSSLContext(const std::shared_ptr<folly::SSLContext>& ctx);
505 * Detaches the SSL context for this socket.
507 void detachSSLContext();
510 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
512 * Switch the SSLContext to continue the SSL handshake.
513 * It can only be used in server mode.
515 void switchServerSSLContext(
516 const std::shared_ptr<folly::SSLContext>& handshakeCtx);
519 * Did server recognize/support the tlsext_hostname in Client Hello?
520 * It can only be used in client mode.
522 * @return true - tlsext_hostname is matched by the server
523 * false - tlsext_hostname is not matched or
524 * is not supported by server
526 bool isServerNameMatch() const;
529 * Set the SNI hostname that we'll advertise to the server in the
530 * ClientHello message.
532 void setServerName(std::string serverName) noexcept;
535 void timeoutExpired() noexcept;
538 * Get the list of supported ciphers sent by the client in the client's
541 void getSSLClientCiphers(std::string& clientCiphers) const {
542 std::stringstream ciphersStream;
543 std::string cipherName;
545 if (parseClientHello_ == false
546 || clientHelloInfo_->clientHelloCipherSuites_.empty()) {
551 for (auto originalCipherCode : clientHelloInfo_->clientHelloCipherSuites_)
553 // OpenSSL expects code as a big endian char array
554 auto cipherCode = htons(originalCipherCode);
556 #if defined(SSL_OP_NO_TLSv1_2)
557 const SSL_CIPHER* cipher =
558 TLSv1_2_method()->get_cipher_by_char((unsigned char*)&cipherCode);
559 #elif defined(SSL_OP_NO_TLSv1_1)
560 const SSL_CIPHER* cipher =
561 TLSv1_1_method()->get_cipher_by_char((unsigned char*)&cipherCode);
562 #elif defined(SSL_OP_NO_TLSv1)
563 const SSL_CIPHER* cipher =
564 TLSv1_method()->get_cipher_by_char((unsigned char*)&cipherCode);
566 const SSL_CIPHER* cipher =
567 SSLv3_method()->get_cipher_by_char((unsigned char*)&cipherCode);
570 if (cipher == nullptr) {
571 ciphersStream << std::setfill('0') << std::setw(4) << std::hex
572 << originalCipherCode << ":";
574 ciphersStream << SSL_CIPHER_get_name(cipher) << ":";
578 clientCiphers = ciphersStream.str();
579 clientCiphers.erase(clientCiphers.end() - 1);
583 * Get the list of compression methods sent by the client in TLS Hello.
585 std::string getSSLClientComprMethods() const {
586 if (!parseClientHello_) {
589 return folly::join(":", clientHelloInfo_->clientHelloCompressionMethods_);
593 * Get the list of TLS extensions sent by the client in the TLS Hello.
595 std::string getSSLClientExts() const {
596 if (!parseClientHello_) {
599 return folly::join(":", clientHelloInfo_->clientHelloExtensions_);
602 std::string getSSLClientSigAlgs() const {
603 if (!parseClientHello_) {
608 sigAlgs.reserve(clientHelloInfo_->clientHelloSigAlgs_.size() * 4);
609 for (size_t i = 0; i < clientHelloInfo_->clientHelloSigAlgs_.size(); i++) {
611 sigAlgs.push_back(':');
613 sigAlgs.append(folly::to<std::string>(
614 clientHelloInfo_->clientHelloSigAlgs_[i].first));
615 sigAlgs.push_back(',');
616 sigAlgs.append(folly::to<std::string>(
617 clientHelloInfo_->clientHelloSigAlgs_[i].second));
624 * Get the list of shared ciphers between the server and the client.
625 * Works well for only SSLv2, not so good for SSLv3 or TLSv1.
627 void getSSLSharedCiphers(std::string& sharedCiphers) const {
628 char ciphersBuffer[1024];
629 ciphersBuffer[0] = '\0';
630 SSL_get_shared_ciphers(ssl_, ciphersBuffer, sizeof(ciphersBuffer) - 1);
631 sharedCiphers = ciphersBuffer;
635 * Get the list of ciphers supported by the server in the server's
638 void getSSLServerCiphers(std::string& serverCiphers) const {
639 serverCiphers = SSL_get_cipher_list(ssl_, 0);
642 while ((cipher = SSL_get_cipher_list(ssl_, i)) != nullptr) {
643 serverCiphers.append(":");
644 serverCiphers.append(cipher);
649 static int getSSLExDataIndex();
650 static AsyncSSLSocket* getFromSSL(const SSL *ssl);
651 static int eorAwareBioWrite(BIO *b, const char *in, int inl);
652 void resetClientHelloParsing(SSL *ssl);
653 static void clientHelloParsingCallback(int write_p, int version,
654 int content_type, const void *buf, size_t len, SSL *ssl, void *arg);
655 static const char* getSSLServerNameFromSSL(SSL* ssl);
658 ssl::ClientHelloInfo* getClientHelloInfo() const {
659 return clientHelloInfo_.get();
663 * Returns the time taken to complete a handshake.
665 virtual std::chrono::nanoseconds getHandshakeTime() const {
666 return handshakeEndTime_ - handshakeStartTime_;
669 void setMinWriteSize(size_t minWriteSize) {
670 minWriteSize_ = minWriteSize;
673 size_t getMinWriteSize() const {
674 return minWriteSize_;
677 void setReadCB(ReadCallback* callback) override;
680 * Returns the peer certificate, or nullptr if no peer certificate received.
682 virtual ssl::X509UniquePtr getPeerCert() const override {
687 X509* cert = SSL_get_peer_certificate(ssl_);
688 return ssl::X509UniquePtr(cert);
692 * Force AsyncSSLSocket object to cache local and peer socket addresses.
693 * If called with "true" before connect() this function forces full local
694 * and remote socket addresses to be cached in the socket object and available
695 * through getLocalAddress()/getPeerAddress() methods even after the socket is
698 void forceCacheAddrOnFailure(bool force) { cacheAddrOnFailure_ = force; }
700 const std::string& getServiceIdentity() const { return serviceIdentity_; }
702 void setServiceIdentity(std::string serviceIdentity) {
703 serviceIdentity_ = std::move(serviceIdentity);
713 * Protected destructor.
715 * Users of AsyncSSLSocket must never delete it directly. Instead, invoke
716 * destroy() instead. (See the documentation in DelayedDestruction.h for
721 // Inherit event notification methods from AsyncSocket except
723 void prepareReadBuffer(void** buf, size_t* buflen) noexcept override;
724 void handleRead() noexcept override;
725 void handleWrite() noexcept override;
726 void handleAccept() noexcept;
727 void handleConnect() noexcept override;
729 void invalidState(HandshakeCB* callback);
730 bool willBlock(int ret,
732 unsigned long* errErrorOut) noexcept;
734 virtual void checkForImmediateRead() noexcept override;
735 // AsyncSocket calls this at the wrong time for SSL
736 void handleInitialReadWrite() noexcept override {}
738 int interpretSSLError(int rc, int error);
739 ssize_t performRead(void** buf, size_t* buflen, size_t* offset) override;
740 ssize_t performWrite(const iovec* vec, uint32_t count, WriteFlags flags,
741 uint32_t* countWritten, uint32_t* partialWritten)
744 ssize_t performWriteIovec(const iovec* vec, uint32_t count,
745 WriteFlags flags, uint32_t* countWritten,
746 uint32_t* partialWritten);
748 // This virtual wrapper around SSL_write exists solely for testing/mockability
749 virtual int sslWriteImpl(SSL *ssl, const void *buf, int n) {
750 return SSL_write(ssl, buf, n);
754 * Apply verification options passed to sslConn/sslAccept or those set
755 * in the underlying SSLContext object.
757 * @param ssl pointer to the SSL object on which verification options will be
758 * applied. If verifyPeer_ was explicitly set either via sslConn/sslAccept,
759 * those options override the settings in the underlying SSLContext.
761 void applyVerificationOptions(SSL * ssl);
764 * A SSL_write wrapper that understand EOR
766 * @param ssl: SSL* object
767 * @param buf: Buffer to be written
768 * @param n: Number of bytes to be written
769 * @param eor: Does the last byte (buf[n-1]) have the app-last-byte?
770 * @return: The number of app bytes successfully written to the socket
772 int eorAwareSSLWrite(SSL *ssl, const void *buf, int n, bool eor);
774 // Inherit error handling methods from AsyncSocket, plus the following.
775 void failHandshake(const char* fn, const AsyncSocketException& ex);
777 void invokeHandshakeErr(const AsyncSocketException& ex);
778 void invokeHandshakeCB();
780 void cacheLocalPeerAddr();
782 static void sslInfoCallback(const SSL *ssl, int type, int val);
784 // Whether we've applied the TCP_CORK option to the socket
786 // SSL related members.
788 // Used to prevent client-initiated renegotiation. Note that AsyncSSLSocket
789 // doesn't fully support renegotiation, so we could just fail all attempts
790 // to enforce this. Once it is supported, we should make it an option
791 // to disable client-initiated renegotiation.
792 bool handshakeComplete_{false};
793 bool renegotiateAttempted_{false};
794 SSLStateEnum sslState_{STATE_UNINIT};
795 std::shared_ptr<folly::SSLContext> ctx_;
796 // Callback for SSL_accept() or SSL_connect()
797 HandshakeCB* handshakeCallback_{nullptr};
799 SSL_SESSION *sslSession_{nullptr};
800 HandshakeTimeout handshakeTimeout_;
801 // whether the SSL session was resumed using session ID or not
802 bool sessionIDResumed_{false};
804 // The app byte num that we are tracking for the MSG_EOR
805 // Only one app EOR byte can be tracked.
806 size_t appEorByteNo_{0};
808 // Try to avoid calling SSL_write() for buffers smaller than this.
809 // It doesn't take effect when it is 0.
810 size_t minWriteSize_{1500};
812 // When openssl is about to sendmsg() across the minEorRawBytesNo_,
813 // it will pass MSG_EOR to sendmsg().
814 size_t minEorRawByteNo_{0};
815 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
816 std::shared_ptr<folly::SSLContext> handshakeCtx_;
817 std::string tlsextHostname_;
820 // a service identity that this socket/connection is associated with
821 std::string serviceIdentity_;
823 folly::SSLContext::SSLVerifyPeerEnum
824 verifyPeer_{folly::SSLContext::SSLVerifyPeerEnum::USE_CTX};
826 // Callback for SSL_CTX_set_verify()
827 static int sslVerifyCallback(int preverifyOk, X509_STORE_CTX* ctx);
829 bool parseClientHello_{false};
830 bool cacheAddrOnFailure_{false};
831 std::unique_ptr<ssl::ClientHelloInfo> clientHelloInfo_;
833 // Time taken to complete the ssl handshake.
834 std::chrono::steady_clock::time_point handshakeStartTime_;
835 std::chrono::steady_clock::time_point handshakeEndTime_;