2 * Copyright 2015 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>
21 #include <openssl/ssl.h>
23 #include <folly/Optional.h>
24 #include <folly/String.h>
25 #include <folly/io/async/AsyncSocket.h>
26 #include <folly/io/async/SSLContext.h>
27 #include <folly/io/async/AsyncTimeout.h>
28 #include <folly/io/async/TimeoutManager.h>
30 #include <folly/Bits.h>
31 #include <folly/io/IOBuf.h>
32 #include <folly/io/Cursor.h>
34 using folly::io::Cursor;
35 using std::unique_ptr;
39 class SSLException: public folly::AsyncSocketException {
41 SSLException(int sslError, int errno_copy);
43 int getSSLError() const { return error_; }
51 * A class for performing asynchronous I/O on an SSL connection.
53 * AsyncSSLSocket allows users to asynchronously wait for data on an
54 * SSL connection, and to asynchronously send data.
56 * The APIs for reading and writing are intentionally asymmetric.
57 * Waiting for data to read is a persistent API: a callback is
58 * installed, and is notified whenever new data is available. It
59 * continues to be notified of new events until it is uninstalled.
61 * AsyncSSLSocket does not provide read timeout functionality,
62 * because it typically cannot determine when the timeout should be
63 * active. Generally, a timeout should only be enabled when
64 * processing is blocked waiting on data from the remote endpoint.
65 * For server connections, the timeout should not be active if the
66 * server is currently processing one or more outstanding requests for
67 * this connection. For client connections, the timeout should not be
68 * active if there are no requests pending on the connection.
69 * Additionally, if a client has multiple pending requests, it will
70 * ususally want a separate timeout for each request, rather than a
71 * single read timeout.
73 * The write API is fairly intuitive: a user can request to send a
74 * block of data, and a callback will be informed once the entire
75 * block has been transferred to the kernel, or on error.
76 * AsyncSSLSocket does provide a send timeout, since most callers
77 * want to give up if the remote end stops responding and no further
78 * progress can be made sending the data.
80 class AsyncSSLSocket : public virtual AsyncSocket {
82 typedef std::unique_ptr<AsyncSSLSocket, Destructor> UniquePtr;
83 using X509_deleter = folly::static_function_deleter<X509, &X509_free>;
87 virtual ~HandshakeCB() = default;
90 * handshakeVer() is invoked during handshaking to give the
91 * application chance to validate it's peer's certificate.
93 * Note that OpenSSL performs only rudimentary internal
94 * consistency verification checks by itself. Any other validation
95 * like whether or not the certificate was issued by a trusted CA.
96 * The default implementation of this callback mimics what what
97 * OpenSSL does internally if SSL_VERIFY_PEER is set with no
98 * verification callback.
100 * See the passages on verify_callback in SSL_CTX_set_verify(3)
103 virtual bool handshakeVer(AsyncSSLSocket* /*sock*/,
105 X509_STORE_CTX* /*ctx*/) noexcept {
110 * handshakeSuc() is called when a new SSL connection is
111 * established, i.e., after SSL_accept/connect() returns successfully.
113 * The HandshakeCB will be uninstalled before handshakeSuc()
116 * @param sock SSL socket on which the handshake was initiated
118 virtual void handshakeSuc(AsyncSSLSocket *sock) noexcept = 0;
121 * handshakeErr() is called if an error occurs while
122 * establishing the SSL connection.
124 * The HandshakeCB will be uninstalled before handshakeErr()
127 * @param sock SSL socket on which the handshake was initiated
128 * @param ex An exception representing the error.
130 virtual void handshakeErr(
131 AsyncSSLSocket *sock,
132 const AsyncSocketException& ex)
136 class HandshakeTimeout : public AsyncTimeout {
138 HandshakeTimeout(AsyncSSLSocket* sslSocket, EventBase* eventBase)
139 : AsyncTimeout(eventBase)
140 , sslSocket_(sslSocket) {}
142 virtual void timeoutExpired() noexcept {
143 sslSocket_->timeoutExpired();
147 AsyncSSLSocket* sslSocket_;
152 * These are passed to the application via errno, packed in an SSL err which
153 * are outside the valid errno range. The values are chosen to be unique
154 * against values in ssl.h
157 SSL_CLIENT_RENEGOTIATION_ATTEMPT = 900,
158 SSL_INVALID_RENEGOTIATION = 901,
159 SSL_EARLY_WRITE = 902
163 * Create a client AsyncSSLSocket
165 AsyncSSLSocket(const std::shared_ptr<folly::SSLContext> &ctx,
166 EventBase* evb, bool deferSecurityNegotiation = false);
169 * Create a server/client AsyncSSLSocket from an already connected
170 * socket file descriptor.
172 * Note that while AsyncSSLSocket enables TCP_NODELAY for sockets it creates
173 * when connecting, it does not change the socket options when given an
174 * existing file descriptor. If callers want TCP_NODELAY enabled when using
175 * this version of the constructor, they need to explicitly call
176 * setNoDelay(true) after the constructor returns.
178 * @param ctx SSL context for this connection.
179 * @param evb EventBase that will manage this socket.
180 * @param fd File descriptor to take over (should be a connected socket).
181 * @param server Is socket in server mode?
182 * @param deferSecurityNegotiation
183 * unencrypted data can be sent before sslConn/Accept
185 AsyncSSLSocket(const std::shared_ptr<folly::SSLContext>& ctx,
186 EventBase* evb, int fd,
187 bool server = true, bool deferSecurityNegotiation = false);
191 * Helper function to create a server/client shared_ptr<AsyncSSLSocket>.
193 static std::shared_ptr<AsyncSSLSocket> newSocket(
194 const std::shared_ptr<folly::SSLContext>& ctx,
195 EventBase* evb, int fd, bool server=true,
196 bool deferSecurityNegotiation = false) {
197 return std::shared_ptr<AsyncSSLSocket>(
198 new AsyncSSLSocket(ctx, evb, fd, server, deferSecurityNegotiation),
203 * Helper function to create a client shared_ptr<AsyncSSLSocket>.
205 static std::shared_ptr<AsyncSSLSocket> newSocket(
206 const std::shared_ptr<folly::SSLContext>& ctx,
207 EventBase* evb, bool deferSecurityNegotiation = false) {
208 return std::shared_ptr<AsyncSSLSocket>(
209 new AsyncSSLSocket(ctx, evb, deferSecurityNegotiation),
214 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
216 * Create a client AsyncSSLSocket with tlsext_servername in
217 * the Client Hello message.
219 AsyncSSLSocket(const std::shared_ptr<folly::SSLContext> &ctx,
221 const std::string& serverName,
222 bool deferSecurityNegotiation = false);
225 * Create a client AsyncSSLSocket from an already connected
226 * socket file descriptor.
228 * Note that while AsyncSSLSocket enables TCP_NODELAY for sockets it creates
229 * when connecting, it does not change the socket options when given an
230 * existing file descriptor. If callers want TCP_NODELAY enabled when using
231 * this version of the constructor, they need to explicitly call
232 * setNoDelay(true) after the constructor returns.
234 * @param ctx SSL context for this connection.
235 * @param evb EventBase that will manage this socket.
236 * @param fd File descriptor to take over (should be a connected socket).
237 * @param serverName tlsext_hostname that will be sent in ClientHello.
239 AsyncSSLSocket(const std::shared_ptr<folly::SSLContext>& ctx,
242 const std::string& serverName,
243 bool deferSecurityNegotiation = false);
245 static std::shared_ptr<AsyncSSLSocket> newSocket(
246 const std::shared_ptr<folly::SSLContext>& ctx,
248 const std::string& serverName,
249 bool deferSecurityNegotiation = false) {
250 return std::shared_ptr<AsyncSSLSocket>(
251 new AsyncSSLSocket(ctx, evb, serverName, deferSecurityNegotiation),
257 * TODO: implement support for SSL renegotiation.
259 * This involves proper handling of the SSL_ERROR_WANT_READ/WRITE
260 * code as a result of SSL_write/read(), instead of returning an
261 * error. In that case, the READ/WRITE event should be registered,
262 * and a flag (e.g., writeBlockedOnRead) should be set to indiciate
263 * the condition. In the next invocation of read/write callback, if
264 * the flag is on, performWrite()/performRead() should be called in
265 * addition to the normal call to performRead()/performWrite(), and
266 * the flag should be reset.
269 // Inherit TAsyncTransport methods from AsyncSocket except the
271 // See the documentation in TAsyncTransport.h
272 // TODO: implement graceful shutdown in close()
273 // TODO: implement detachSSL() that returns the SSL connection
274 virtual void closeNow() override;
275 virtual void shutdownWrite() override;
276 virtual void shutdownWriteNow() override;
277 virtual bool good() const override;
278 virtual bool connecting() const override;
280 bool isEorTrackingEnabled() const override;
281 virtual void setEorTracking(bool track) override;
282 virtual size_t getRawBytesWritten() const override;
283 virtual size_t getRawBytesReceived() const override;
284 void enableClientHelloParsing();
287 * Accept an SSL connection on the socket.
289 * The callback will be invoked and uninstalled when an SSL
290 * connection has been established on the underlying socket.
291 * The value of verifyPeer determines the client verification method.
292 * By default, its set to use the value in the underlying context
294 * @param callback callback object to invoke on success/failure
295 * @param timeout timeout for this function in milliseconds, or 0 for no
297 * @param verifyPeer SSLVerifyPeerEnum uses the options specified in the
298 * context by default, can be set explcitly to override the
299 * method in the context
301 virtual void sslAccept(HandshakeCB* callback, uint32_t timeout = 0,
302 const folly::SSLContext::SSLVerifyPeerEnum& verifyPeer =
303 folly::SSLContext::SSLVerifyPeerEnum::USE_CTX);
306 * Invoke SSL accept following an asynchronous session cache lookup
308 void restartSSLAccept();
311 * Connect to the given address, invoking callback when complete or on error
313 * Note timeout applies to TCP + SSL connection time
315 void connect(ConnectCallback* callback,
316 const folly::SocketAddress& address,
318 const OptionMap &options = emptyOptionMap,
319 const folly::SocketAddress& bindAddr = anyAddress())
322 using AsyncSocket::connect;
325 * Initiate an SSL connection on the socket
326 * The callback will be invoked and uninstalled when an SSL connection
327 * has been establshed on the underlying socket.
328 * The verification option verifyPeer is applied if it's passed explicitly.
329 * If it's not, the options in SSLContext set on the underlying SSLContext
332 * @param callback callback object to invoke on success/failure
333 * @param timeout timeout for this function in milliseconds, or 0 for no
335 * @param verifyPeer SSLVerifyPeerEnum uses the options specified in the
336 * context by default, can be set explcitly to override the
337 * method in the context. If verification is turned on sets
338 * SSL_VERIFY_PEER and invokes
339 * HandshakeCB::handshakeVer().
341 virtual void sslConn(HandshakeCB *callback, uint64_t timeout = 0,
342 const folly::SSLContext::SSLVerifyPeerEnum& verifyPeer =
343 folly::SSLContext::SSLVerifyPeerEnum::USE_CTX);
350 STATE_RSA_ASYNC_PENDING,
353 STATE_REMOTE_CLOSED, /// remote end closed; we can still write
354 STATE_CLOSING, ///< close() called, but waiting on writes to complete
355 /// close() called with pending writes, before connect() has completed
356 STATE_CONNECTING_CLOSING,
361 SSLStateEnum getSSLState() const { return sslState_;}
364 * Get a handle to the negotiated SSL session. This increments the session
365 * refcount and must be deallocated by the caller.
367 SSL_SESSION *getSSLSession();
370 * Set the SSL session to be used during sslConn. AsyncSSLSocket will
371 * hold a reference to the session until it is destroyed or released by the
372 * underlying SSL structure.
374 * @param takeOwnership if true, AsyncSSLSocket will assume the caller's
375 * reference count to session.
377 void setSSLSession(SSL_SESSION *session, bool takeOwnership = false);
380 * Get the name of the protocol selected by the client during
381 * Next Protocol Negotiation (NPN)
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.
392 virtual void getSelectedNextProtocol(const unsigned char** protoName,
393 unsigned* protoLen) const;
396 * Get the name of the protocol selected by the client during
397 * Next Protocol Negotiation (NPN)
399 * @param protoName Name of the protocol (not guaranteed to be
400 * null terminated); will be set to nullptr if
401 * the client did not negotiate a protocol.
402 * Note: the AsyncSSLSocket retains ownership
404 * @param protoNameLen Length of the name.
405 * @return false if openssl does not support NPN
407 virtual bool getSelectedNextProtocolNoThrow(const unsigned char** protoName,
408 unsigned* protoLen) const;
411 * Determine if the session specified during setSSLSession was reused
412 * or if the server rejected it and issued a new session.
414 bool getSSLSessionReused() const;
417 * true if the session was resumed using session ID
419 bool sessionIDResumed() const { return sessionIDResumed_; }
421 void setSessionIDResumed(bool resumed) {
422 sessionIDResumed_ = resumed;
426 * Get the negociated cipher name for this SSL connection.
427 * Returns the cipher used or the constant value "NONE" when no SSL session
428 * has been established.
430 const char *getNegotiatedCipherName() const;
433 * Get the server name for this SSL connection.
434 * Returns the server name used or the constant value "NONE" when no SSL
435 * session has been established.
436 * If openssl has no SNI support, throw TTransportException.
438 const char *getSSLServerName() const;
441 * Get the server name for this SSL connection.
442 * Returns the server name used or the constant value "NONE" when no SSL
443 * session has been established.
444 * If openssl has no SNI support, return "NONE"
446 const char *getSSLServerNameNoThrow() const;
449 * Get the SSL version for this connection.
450 * Possible return values are SSL2_VERSION, SSL3_VERSION, TLS1_VERSION,
451 * with hexa representations 0x200, 0x300, 0x301,
452 * or 0 if no SSL session has been established.
454 int getSSLVersion() const;
457 * Get the certificate size used for this SSL connection.
459 int getSSLCertSize() const;
461 /* Get the number of bytes read from the wire (including protocol
462 * overhead). Returns 0 once the connection has been closed.
464 unsigned long getBytesRead() const {
465 if (ssl_ != nullptr) {
466 return BIO_number_read(SSL_get_rbio(ssl_));
471 /* Get the number of bytes written to the wire (including protocol
472 * overhead). Returns 0 once the connection has been closed.
474 unsigned long getBytesWritten() const {
475 if (ssl_ != nullptr) {
476 return BIO_number_written(SSL_get_wbio(ssl_));
481 virtual void attachEventBase(EventBase* eventBase) override {
482 AsyncSocket::attachEventBase(eventBase);
483 handshakeTimeout_.attachEventBase(eventBase);
486 virtual void detachEventBase() override {
487 AsyncSocket::detachEventBase();
488 handshakeTimeout_.detachEventBase();
491 virtual bool isDetachable() const override {
492 return AsyncSocket::isDetachable() && !handshakeTimeout_.isScheduled();
495 virtual void attachTimeoutManager(TimeoutManager* manager) {
496 handshakeTimeout_.attachTimeoutManager(manager);
499 virtual void detachTimeoutManager() {
500 handshakeTimeout_.detachTimeoutManager();
503 #if OPENSSL_VERSION_NUMBER >= 0x009080bfL
505 * This function will set the SSL context for this socket to the
506 * argument. This should only be used on client SSL Sockets that have
507 * already called detachSSLContext();
509 void attachSSLContext(const std::shared_ptr<folly::SSLContext>& ctx);
512 * Detaches the SSL context for this socket.
514 void detachSSLContext();
517 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
519 * Switch the SSLContext to continue the SSL handshake.
520 * It can only be used in server mode.
522 void switchServerSSLContext(
523 const std::shared_ptr<folly::SSLContext>& handshakeCtx);
526 * Did server recognize/support the tlsext_hostname in Client Hello?
527 * It can only be used in client mode.
529 * @return true - tlsext_hostname is matched by the server
530 * false - tlsext_hostname is not matched or
531 * is not supported by server
533 bool isServerNameMatch() const;
536 * Set the SNI hostname that we'll advertise to the server in the
537 * ClientHello message.
539 void setServerName(std::string serverName) noexcept;
542 void timeoutExpired() noexcept;
545 * Get the list of supported ciphers sent by the client in the client's
548 void getSSLClientCiphers(std::string& clientCiphers) const {
549 std::stringstream ciphersStream;
550 std::string cipherName;
552 if (parseClientHello_ == false
553 || clientHelloInfo_->clientHelloCipherSuites_.empty()) {
558 for (auto originalCipherCode : clientHelloInfo_->clientHelloCipherSuites_)
560 // OpenSSL expects code as a big endian char array
561 auto cipherCode = htons(originalCipherCode);
563 #if defined(SSL_OP_NO_TLSv1_2)
564 const SSL_CIPHER* cipher =
565 TLSv1_2_method()->get_cipher_by_char((unsigned char*)&cipherCode);
566 #elif defined(SSL_OP_NO_TLSv1_1)
567 const SSL_CIPHER* cipher =
568 TLSv1_1_method()->get_cipher_by_char((unsigned char*)&cipherCode);
569 #elif defined(SSL_OP_NO_TLSv1)
570 const SSL_CIPHER* cipher =
571 TLSv1_method()->get_cipher_by_char((unsigned char*)&cipherCode);
573 const SSL_CIPHER* cipher =
574 SSLv3_method()->get_cipher_by_char((unsigned char*)&cipherCode);
577 if (cipher == nullptr) {
578 ciphersStream << std::setfill('0') << std::setw(4) << std::hex
579 << originalCipherCode << ":";
581 ciphersStream << SSL_CIPHER_get_name(cipher) << ":";
585 clientCiphers = ciphersStream.str();
586 clientCiphers.erase(clientCiphers.end() - 1);
590 * Get the list of compression methods sent by the client in TLS Hello.
592 std::string getSSLClientComprMethods() const {
593 if (!parseClientHello_) {
596 return folly::join(":", clientHelloInfo_->clientHelloCompressionMethods_);
600 * Get the list of TLS extensions sent by the client in the TLS Hello.
602 std::string getSSLClientExts() const {
603 if (!parseClientHello_) {
606 return folly::join(":", clientHelloInfo_->clientHelloExtensions_);
609 std::string getSSLClientSigAlgs() const {
610 if (!parseClientHello_) {
615 sigAlgs.reserve(clientHelloInfo_->clientHelloSigAlgs_.size() * 4);
616 for (size_t i = 0; i < clientHelloInfo_->clientHelloSigAlgs_.size(); i++) {
618 sigAlgs.push_back(':');
620 sigAlgs.append(folly::to<std::string>(
621 clientHelloInfo_->clientHelloSigAlgs_[i].first));
622 sigAlgs.push_back(',');
623 sigAlgs.append(folly::to<std::string>(
624 clientHelloInfo_->clientHelloSigAlgs_[i].second));
631 * Get the list of shared ciphers between the server and the client.
632 * Works well for only SSLv2, not so good for SSLv3 or TLSv1.
634 void getSSLSharedCiphers(std::string& sharedCiphers) const {
635 char ciphersBuffer[1024];
636 ciphersBuffer[0] = '\0';
637 SSL_get_shared_ciphers(ssl_, ciphersBuffer, sizeof(ciphersBuffer) - 1);
638 sharedCiphers = ciphersBuffer;
642 * Get the list of ciphers supported by the server in the server's
645 void getSSLServerCiphers(std::string& serverCiphers) const {
646 serverCiphers = SSL_get_cipher_list(ssl_, 0);
649 while ((cipher = SSL_get_cipher_list(ssl_, i)) != nullptr) {
650 serverCiphers.append(":");
651 serverCiphers.append(cipher);
656 static int getSSLExDataIndex();
657 static AsyncSSLSocket* getFromSSL(const SSL *ssl);
658 static int eorAwareBioWrite(BIO *b, const char *in, int inl);
659 void resetClientHelloParsing(SSL *ssl);
660 static void clientHelloParsingCallback(int write_p, int version,
661 int content_type, const void *buf, size_t len, SSL *ssl, void *arg);
663 // http://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml
664 enum class TLSExtension: uint16_t {
666 MAX_FRAGMENT_LENGTH = 1,
667 CLIENT_CERTIFICATE_URL = 2,
675 SUPPORTED_GROUPS = 10,
676 EC_POINT_FORMATS = 11,
678 SIGNATURE_ALGORITHMS = 13,
681 APPLICATION_LAYER_PROTOCOL_NEGOTIATION = 16,
682 STATUS_REQUEST_V2 = 17,
683 SIGNED_CERTIFICATE_TIMESTAMP = 18,
684 CLIENT_CERTIFICATE_TYPE = 19,
685 SERVER_CERTIFICATE_TYPE = 20,
687 ENCRYPT_THEN_MAC = 22,
688 EXTENDED_MASTER_SECRET = 23,
690 RENEGOTIATION_INFO = 65281
693 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-18
694 enum class HashAlgorithm: uint8_t {
704 // http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-16
705 enum class SignatureAlgorithm: uint8_t {
712 struct ClientHelloInfo {
713 folly::IOBufQueue clientHelloBuf_;
714 uint8_t clientHelloMajorVersion_;
715 uint8_t clientHelloMinorVersion_;
716 std::vector<uint16_t> clientHelloCipherSuites_;
717 std::vector<uint8_t> clientHelloCompressionMethods_;
718 std::vector<TLSExtension> clientHelloExtensions_;
720 std::pair<HashAlgorithm, SignatureAlgorithm>> clientHelloSigAlgs_;
724 ClientHelloInfo* getClientHelloInfo() const {
725 return clientHelloInfo_.get();
728 void setMinWriteSize(size_t minWriteSize) {
729 minWriteSize_ = minWriteSize;
732 size_t getMinWriteSize() const {
733 return minWriteSize_;
736 void setReadCB(ReadCallback* callback) override;
739 * Returns the peer certificate, or nullptr if no peer certificate received.
741 std::unique_ptr<X509, X509_deleter> getPeerCert() const {
746 X509* cert = SSL_get_peer_certificate(ssl_);
747 return std::unique_ptr<X509, X509_deleter>(cert);
757 * Protected destructor.
759 * Users of AsyncSSLSocket must never delete it directly. Instead, invoke
760 * destroy() instead. (See the documentation in DelayedDestruction.h for
765 // Inherit event notification methods from AsyncSocket except
767 void prepareReadBuffer(void** buf, size_t* buflen) noexcept override;
768 void handleRead() noexcept override;
769 void handleWrite() noexcept override;
770 void handleAccept() noexcept;
771 void handleConnect() noexcept override;
773 void invalidState(HandshakeCB* callback);
774 bool willBlock(int ret, int *errorOut) noexcept;
776 virtual void checkForImmediateRead() noexcept override;
777 // AsyncSocket calls this at the wrong time for SSL
778 void handleInitialReadWrite() noexcept override {}
780 int interpretSSLError(int rc, int error);
781 ssize_t performRead(void** buf, size_t* buflen, size_t* offset) override;
782 ssize_t performWrite(const iovec* vec, uint32_t count, WriteFlags flags,
783 uint32_t* countWritten, uint32_t* partialWritten)
786 ssize_t performWriteIovec(const iovec* vec, uint32_t count,
787 WriteFlags flags, uint32_t* countWritten,
788 uint32_t* partialWritten);
790 // This virtual wrapper around SSL_write exists solely for testing/mockability
791 virtual int sslWriteImpl(SSL *ssl, const void *buf, int n) {
792 return SSL_write(ssl, buf, n);
796 * Apply verification options passed to sslConn/sslAccept or those set
797 * in the underlying SSLContext object.
799 * @param ssl pointer to the SSL object on which verification options will be
800 * applied. If verifyPeer_ was explicitly set either via sslConn/sslAccept,
801 * those options override the settings in the underlying SSLContext.
803 void applyVerificationOptions(SSL * ssl);
806 * A SSL_write wrapper that understand EOR
808 * @param ssl: SSL* object
809 * @param buf: Buffer to be written
810 * @param n: Number of bytes to be written
811 * @param eor: Does the last byte (buf[n-1]) have the app-last-byte?
812 * @return: The number of app bytes successfully written to the socket
814 int eorAwareSSLWrite(SSL *ssl, const void *buf, int n, bool eor);
816 // Inherit error handling methods from AsyncSocket, plus the following.
817 void failHandshake(const char* fn, const AsyncSocketException& ex);
819 void invokeHandshakeCB();
821 static void sslInfoCallback(const SSL *ssl, int type, int val);
823 // Whether we've applied the TCP_CORK option to the socket
825 // SSL related members.
827 // Used to prevent client-initiated renegotiation. Note that AsyncSSLSocket
828 // doesn't fully support renegotiation, so we could just fail all attempts
829 // to enforce this. Once it is supported, we should make it an option
830 // to disable client-initiated renegotiation.
831 bool handshakeComplete_{false};
832 bool renegotiateAttempted_{false};
833 SSLStateEnum sslState_{STATE_UNINIT};
834 std::shared_ptr<folly::SSLContext> ctx_;
835 // Callback for SSL_accept() or SSL_connect()
836 HandshakeCB* handshakeCallback_{nullptr};
838 SSL_SESSION *sslSession_{nullptr};
839 HandshakeTimeout handshakeTimeout_;
840 // whether the SSL session was resumed using session ID or not
841 bool sessionIDResumed_{false};
843 // The app byte num that we are tracking for the MSG_EOR
844 // Only one app EOR byte can be tracked.
845 size_t appEorByteNo_{0};
847 // Try to avoid calling SSL_write() for buffers smaller than this.
848 // It doesn't take effect when it is 0.
849 size_t minWriteSize_{1500};
851 // When openssl is about to sendmsg() across the minEorRawBytesNo_,
852 // it will pass MSG_EOR to sendmsg().
853 size_t minEorRawByteNo_{0};
854 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
855 std::shared_ptr<folly::SSLContext> handshakeCtx_;
856 std::string tlsextHostname_;
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 unique_ptr<ClientHelloInfo> clientHelloInfo_;