fix flaky ConnectTFOTimeout and ConnectTFOFallbackTimeout tests
[folly.git] / folly / io / async / AsyncSSLSocket.h
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #pragma once
18
19 #include <iomanip>
20
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>
31
32 #include <folly/Bits.h>
33 #include <folly/io/IOBuf.h>
34 #include <folly/io/Cursor.h>
35 #include <folly/portability/Sockets.h>
36
37 namespace folly {
38
39 /**
40  * A class for performing asynchronous I/O on an SSL connection.
41  *
42  * AsyncSSLSocket allows users to asynchronously wait for data on an
43  * SSL connection, and to asynchronously send data.
44  *
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.
49  *
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.
61  *
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.
68  */
69 class AsyncSSLSocket : public virtual AsyncSocket {
70  public:
71   typedef std::unique_ptr<AsyncSSLSocket, Destructor> UniquePtr;
72   using X509_deleter = folly::static_function_deleter<X509, &X509_free>;
73
74   class HandshakeCB {
75    public:
76     virtual ~HandshakeCB() = default;
77
78     /**
79      * handshakeVer() is invoked during handshaking to give the
80      * application chance to validate it's peer's certificate.
81      *
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.
88      *
89      * See the passages on verify_callback in SSL_CTX_set_verify(3)
90      * for more details.
91      */
92     virtual bool handshakeVer(AsyncSSLSocket* /*sock*/,
93                                  bool preverifyOk,
94                                  X509_STORE_CTX* /*ctx*/) noexcept {
95       return preverifyOk;
96     }
97
98     /**
99      * handshakeSuc() is called when a new SSL connection is
100      * established, i.e., after SSL_accept/connect() returns successfully.
101      *
102      * The HandshakeCB will be uninstalled before handshakeSuc()
103      * is called.
104      *
105      * @param sock  SSL socket on which the handshake was initiated
106      */
107     virtual void handshakeSuc(AsyncSSLSocket *sock) noexcept = 0;
108
109     /**
110      * handshakeErr() is called if an error occurs while
111      * establishing the SSL connection.
112      *
113      * The HandshakeCB will be uninstalled before handshakeErr()
114      * is called.
115      *
116      * @param sock  SSL socket on which the handshake was initiated
117      * @param ex  An exception representing the error.
118      */
119     virtual void handshakeErr(
120       AsyncSSLSocket *sock,
121       const AsyncSocketException& ex)
122       noexcept = 0;
123   };
124
125   class HandshakeTimeout : public AsyncTimeout {
126    public:
127     HandshakeTimeout(AsyncSSLSocket* sslSocket, EventBase* eventBase)
128       : AsyncTimeout(eventBase)
129       , sslSocket_(sslSocket) {}
130
131     virtual void timeoutExpired() noexcept {
132       sslSocket_->timeoutExpired();
133     }
134
135    private:
136     AsyncSSLSocket* sslSocket_;
137   };
138
139   /**
140    * Create a client AsyncSSLSocket
141    */
142   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext> &ctx,
143                  EventBase* evb, bool deferSecurityNegotiation = false);
144
145   /**
146    * Create a server/client AsyncSSLSocket from an already connected
147    * socket file descriptor.
148    *
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.
154    *
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
161    */
162   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext>& ctx,
163                  EventBase* evb, int fd,
164                  bool server = true, bool deferSecurityNegotiation = false);
165
166
167   /**
168    * Helper function to create a server/client shared_ptr<AsyncSSLSocket>.
169    */
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),
176       Destructor());
177   }
178
179   /**
180    * Helper function to create a client shared_ptr<AsyncSSLSocket>.
181    */
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),
187       Destructor());
188   }
189
190
191 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
192   /**
193    * Create a client AsyncSSLSocket with tlsext_servername in
194    * the Client Hello message.
195    */
196   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext> &ctx,
197                   EventBase* evb,
198                  const std::string& serverName,
199                 bool deferSecurityNegotiation = false);
200
201   /**
202    * Create a client AsyncSSLSocket from an already connected
203    * socket file descriptor.
204    *
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.
210    *
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.
215    */
216   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext>& ctx,
217                   EventBase* evb,
218                   int fd,
219                  const std::string& serverName,
220                 bool deferSecurityNegotiation = false);
221
222   static std::shared_ptr<AsyncSSLSocket> newSocket(
223     const std::shared_ptr<folly::SSLContext>& ctx,
224     EventBase* evb,
225     const std::string& serverName,
226     bool deferSecurityNegotiation = false) {
227     return std::shared_ptr<AsyncSSLSocket>(
228       new AsyncSSLSocket(ctx, evb, serverName, deferSecurityNegotiation),
229       Destructor());
230   }
231 #endif
232
233   /**
234    * TODO: implement support for SSL renegotiation.
235    *
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.
244    */
245
246   // Inherit TAsyncTransport methods from AsyncSocket except the
247   // following.
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;
257
258   virtual std::string getSecurityProtocol() const override { return "TLS"; }
259
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();
265
266   /**
267    * Accept an SSL connection on the socket.
268    *
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
273    *
274    * @param callback callback object to invoke on success/failure
275    * @param timeout timeout for this function in milliseconds, or 0 for no
276    *                timeout
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
280    */
281   virtual void sslAccept(HandshakeCB* callback, uint32_t timeout = 0,
282       const folly::SSLContext::SSLVerifyPeerEnum& verifyPeer =
283             folly::SSLContext::SSLVerifyPeerEnum::USE_CTX);
284
285   /**
286    * Invoke SSL accept following an asynchronous session cache lookup
287    */
288   void restartSSLAccept();
289
290   /**
291    * Connect to the given address, invoking callback when complete or on error
292    *
293    * Note timeout applies to TCP + SSL connection time
294    */
295   void connect(ConnectCallback* callback,
296                const folly::SocketAddress& address,
297                int timeout = 0,
298                const OptionMap &options = emptyOptionMap,
299                const folly::SocketAddress& bindAddr = anyAddress())
300                noexcept override;
301
302   using AsyncSocket::connect;
303
304   /**
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
310    * are applied.
311    *
312    * @param callback callback object to invoke on success/failure
313    * @param timeout timeout for this function in milliseconds, or 0 for no
314    *                timeout
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().
320    */
321   virtual void sslConn(HandshakeCB *callback, uint64_t timeout = 0,
322             const folly::SSLContext::SSLVerifyPeerEnum& verifyPeer =
323                   folly::SSLContext::SSLVerifyPeerEnum::USE_CTX);
324
325   enum SSLStateEnum {
326     STATE_UNINIT,
327     STATE_UNENCRYPTED,
328     STATE_ACCEPTING,
329     STATE_CACHE_LOOKUP,
330     STATE_ASYNC_PENDING,
331     STATE_CONNECTING,
332     STATE_ESTABLISHED,
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,
337     STATE_CLOSED,
338     STATE_ERROR
339   };
340
341   SSLStateEnum getSSLState() const { return sslState_;}
342
343   /**
344    * Get a handle to the negotiated SSL session.  This increments the session
345    * refcount and must be deallocated by the caller.
346    */
347   SSL_SESSION *getSSLSession();
348
349   /**
350    * Get a handle to the SSL struct.
351    */
352   const SSL* getSSL() const;
353
354   /**
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.
358    *
359    * @param takeOwnership if true, AsyncSSLSocket will assume the caller's
360    *                      reference count to session.
361    */
362   void setSSLSession(SSL_SESSION *session, bool takeOwnership = false);
363
364   /**
365    * Get the name of the protocol selected by the client during
366    * Next Protocol Negotiation (NPN) or Application Layer Protocol Negotiation
367    * (ALPN)
368    *
369    * Throw an exception if openssl does not support NPN
370    *
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
375    *                       of this string.
376    * @param protoNameLen   Length of the name.
377    * @param protoType      Whether this was an NPN or ALPN negotiation
378    */
379   virtual void getSelectedNextProtocol(
380       const unsigned char** protoName,
381       unsigned* protoLen,
382       SSLContext::NextProtocolType* protoType = nullptr) const;
383
384   /**
385    * Get the name of the protocol selected by the client during
386    * Next Protocol Negotiation (NPN) or Application Layer Protocol Negotiation
387    * (ALPN)
388    *
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
393    *                       of this string.
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
397    */
398   virtual bool getSelectedNextProtocolNoThrow(
399       const unsigned char** protoName,
400       unsigned* protoLen,
401       SSLContext::NextProtocolType* protoType = nullptr) const;
402
403   /**
404    * Determine if the session specified during setSSLSession was reused
405    * or if the server rejected it and issued a new session.
406    */
407   virtual bool getSSLSessionReused() const;
408
409   /**
410    * true if the session was resumed using session ID
411    */
412   bool sessionIDResumed() const { return sessionIDResumed_; }
413
414   void setSessionIDResumed(bool resumed) {
415     sessionIDResumed_ = resumed;
416   }
417
418   /**
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.
422    */
423   virtual const char* getNegotiatedCipherName() const;
424
425   /**
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.
430    */
431   const char *getSSLServerName() const;
432
433   /**
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"
438    */
439   const char *getSSLServerNameNoThrow() const;
440
441   /**
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.
446    */
447   int getSSLVersion() const;
448
449   /**
450    * Get the signature algorithm used in the cert that is used for this
451    * connection.
452    */
453   const char *getSSLCertSigAlgName() const;
454
455   /**
456    * Get the certificate size used for this SSL connection.
457    */
458   int getSSLCertSize() const;
459
460   virtual void attachEventBase(EventBase* eventBase) override {
461     AsyncSocket::attachEventBase(eventBase);
462     handshakeTimeout_.attachEventBase(eventBase);
463   }
464
465   virtual void detachEventBase() override {
466     AsyncSocket::detachEventBase();
467     handshakeTimeout_.detachEventBase();
468   }
469
470   virtual bool isDetachable() const override {
471     return AsyncSocket::isDetachable() && !handshakeTimeout_.isScheduled();
472   }
473
474   virtual void attachTimeoutManager(TimeoutManager* manager) {
475     handshakeTimeout_.attachTimeoutManager(manager);
476   }
477
478   virtual void detachTimeoutManager() {
479     handshakeTimeout_.detachTimeoutManager();
480   }
481
482 #if OPENSSL_VERSION_NUMBER >= 0x009080bfL
483   /**
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();
487    */
488   void attachSSLContext(const std::shared_ptr<folly::SSLContext>& ctx);
489
490   /**
491    * Detaches the SSL context for this socket.
492    */
493   void detachSSLContext();
494 #endif
495
496 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
497   /**
498    * Switch the SSLContext to continue the SSL handshake.
499    * It can only be used in server mode.
500    */
501   void switchServerSSLContext(
502     const std::shared_ptr<folly::SSLContext>& handshakeCtx);
503
504   /**
505    * Did server recognize/support the tlsext_hostname in Client Hello?
506    * It can only be used in client mode.
507    *
508    * @return true - tlsext_hostname is matched by the server
509    *         false - tlsext_hostname is not matched or
510    *                 is not supported by server
511    */
512   bool isServerNameMatch() const;
513
514   /**
515    * Set the SNI hostname that we'll advertise to the server in the
516    * ClientHello message.
517    */
518   void setServerName(std::string serverName) noexcept;
519 #endif
520
521   void timeoutExpired() noexcept;
522
523   /**
524    * Get the list of supported ciphers sent by the client in the client's
525    * preference order.
526    */
527   void getSSLClientCiphers(
528       std::string& clientCiphers,
529       bool convertToString = true) const {
530     std::stringstream ciphersStream;
531     std::string cipherName;
532
533     if (parseClientHello_ == false
534         || clientHelloInfo_->clientHelloCipherSuites_.empty()) {
535       clientCiphers = "";
536       return;
537     }
538
539     for (auto originalCipherCode : clientHelloInfo_->clientHelloCipherSuites_)
540     {
541       const SSL_CIPHER* cipher = nullptr;
542       if (convertToString) {
543         // OpenSSL expects code as a big endian char array
544         auto cipherCode = htons(originalCipherCode);
545
546 #if defined(SSL_OP_NO_TLSv1_2)
547         cipher =
548             TLSv1_2_method()->get_cipher_by_char((unsigned char*)&cipherCode);
549 #elif defined(SSL_OP_NO_TLSv1_1)
550         cipher =
551             TLSv1_1_method()->get_cipher_by_char((unsigned char*)&cipherCode);
552 #elif defined(SSL_OP_NO_TLSv1)
553         cipher =
554             TLSv1_method()->get_cipher_by_char((unsigned char*)&cipherCode);
555 #else
556         cipher =
557             SSLv3_method()->get_cipher_by_char((unsigned char*)&cipherCode);
558 #endif
559       }
560
561       if (cipher == nullptr) {
562         ciphersStream << std::setfill('0') << std::setw(4) << std::hex
563                       << originalCipherCode << ":";
564       } else {
565         ciphersStream << SSL_CIPHER_get_name(cipher) << ":";
566       }
567     }
568
569     clientCiphers = ciphersStream.str();
570     clientCiphers.erase(clientCiphers.end() - 1);
571   }
572
573   /**
574    * Get the list of compression methods sent by the client in TLS Hello.
575    */
576   std::string getSSLClientComprMethods() const {
577     if (!parseClientHello_) {
578       return "";
579     }
580     return folly::join(":", clientHelloInfo_->clientHelloCompressionMethods_);
581   }
582
583   /**
584    * Get the list of TLS extensions sent by the client in the TLS Hello.
585    */
586   std::string getSSLClientExts() const {
587     if (!parseClientHello_) {
588       return "";
589     }
590     return folly::join(":", clientHelloInfo_->clientHelloExtensions_);
591   }
592
593   std::string getSSLClientSigAlgs() const {
594     if (!parseClientHello_) {
595       return "";
596     }
597
598     std::string sigAlgs;
599     sigAlgs.reserve(clientHelloInfo_->clientHelloSigAlgs_.size() * 4);
600     for (size_t i = 0; i < clientHelloInfo_->clientHelloSigAlgs_.size(); i++) {
601       if (i) {
602         sigAlgs.push_back(':');
603       }
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));
609     }
610
611     return sigAlgs;
612   }
613
614   std::string getSSLAlertsReceived() const {
615     std::string ret;
616
617     for (const auto& alert : alertsReceived_) {
618       if (!ret.empty()) {
619         ret.append(",");
620       }
621       ret.append(folly::to<std::string>(alert.first, ": ", alert.second));
622     }
623
624     return ret;
625   }
626
627   /**
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.
630    */
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;
636   }
637
638   /**
639    * Get the list of ciphers supported by the server in the server's
640    * preference order.
641    */
642   void getSSLServerCiphers(std::string& serverCiphers) const {
643     serverCiphers = SSL_get_cipher_list(ssl_, 0);
644     int i = 1;
645     const char *cipher;
646     while ((cipher = SSL_get_cipher_list(ssl_, i)) != nullptr) {
647       serverCiphers.append(":");
648       serverCiphers.append(cipher);
649       i++;
650     }
651   }
652
653   static int getSSLExDataIndex();
654   static AsyncSSLSocket* getFromSSL(const SSL *ssl);
655   static int bioWrite(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);
660
661   // For unit-tests
662   ssl::ClientHelloInfo* getClientHelloInfo() const {
663     return clientHelloInfo_.get();
664   }
665
666   /**
667    * Returns the time taken to complete a handshake.
668    */
669   virtual std::chrono::nanoseconds getHandshakeTime() const {
670     return handshakeEndTime_ - handshakeStartTime_;
671   }
672
673   void setMinWriteSize(size_t minWriteSize) {
674     minWriteSize_ = minWriteSize;
675   }
676
677   size_t getMinWriteSize() const {
678     return minWriteSize_;
679   }
680
681   void setReadCB(ReadCallback* callback) override;
682
683   /**
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.
687    */
688   void setBufferMovableEnabled(bool enabled);
689
690   /**
691    * Returns the peer certificate, or nullptr if no peer certificate received.
692    */
693   virtual ssl::X509UniquePtr getPeerCert() const override {
694     if (!ssl_) {
695       return nullptr;
696     }
697
698     X509* cert = SSL_get_peer_certificate(ssl_);
699     return ssl::X509UniquePtr(cert);
700   }
701
702   /**
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
707    * closed.
708    */
709   void forceCacheAddrOnFailure(bool force) { cacheAddrOnFailure_ = force; }
710
711   const std::string& getServiceIdentity() const { return serviceIdentity_; }
712
713   void setServiceIdentity(std::string serviceIdentity) {
714     serviceIdentity_ = std::move(serviceIdentity);
715   }
716
717  private:
718
719   void init();
720
721  protected:
722
723   /**
724    * Protected destructor.
725    *
726    * Users of AsyncSSLSocket must never delete it directly.  Instead, invoke
727    * destroy() instead.  (See the documentation in DelayedDestruction.h for
728    * more details.)
729    */
730   ~AsyncSSLSocket();
731
732   // Inherit event notification methods from AsyncSocket except
733   // the following.
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;
739
740   void invalidState(HandshakeCB* callback);
741   bool willBlock(int ret,
742                  int* sslErrorOut,
743                  unsigned long* errErrorOut) noexcept;
744
745   virtual void checkForImmediateRead() noexcept override;
746   // AsyncSocket calls this at the wrong time for SSL
747   void handleInitialReadWrite() noexcept override {}
748
749   WriteResult interpretSSLError(int rc, int error);
750   ReadResult performRead(void** buf, size_t* buflen, size_t* offset) override;
751   WriteResult performWrite(
752       const iovec* vec,
753       uint32_t count,
754       WriteFlags flags,
755       uint32_t* countWritten,
756       uint32_t* partialWritten) override;
757
758   ssize_t performWriteIovec(const iovec* vec, uint32_t count,
759                             WriteFlags flags, uint32_t* countWritten,
760                             uint32_t* partialWritten);
761
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);
765   }
766
767   /**
768    * Apply verification options passed to sslConn/sslAccept or those set
769    * in the underlying SSLContext object.
770    *
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.
774    */
775   void applyVerificationOptions(SSL * ssl);
776
777   /**
778    * Sets up SSL with a custom write bio which intercepts all writes.
779    *
780    * @return true, if succeeds and false if there is an error creating the bio.
781    */
782   bool setupSSLBio();
783
784   /**
785    * A SSL_write wrapper that understand EOR
786    *
787    * @param ssl: SSL* object
788    * @param buf: Buffer to be written
789    * @param n:   Number of bytes to be written
790    * @param eor: Does the last byte (buf[n-1]) have the app-last-byte?
791    * @return:    The number of app bytes successfully written to the socket
792    */
793   int eorAwareSSLWrite(SSL *ssl, const void *buf, int n, bool eor);
794
795   // Inherit error handling methods from AsyncSocket, plus the following.
796   void failHandshake(const char* fn, const AsyncSocketException& ex);
797
798   void invokeHandshakeErr(const AsyncSocketException& ex);
799   void invokeHandshakeCB();
800
801   void invokeConnectSuccess() override;
802
803   void cacheLocalPeerAddr();
804
805   static void sslInfoCallback(const SSL *ssl, int type, int val);
806
807   // Whether we've applied the TCP_CORK option to the socket
808   bool corked_{false};
809   // SSL related members.
810   bool server_{false};
811   // Used to prevent client-initiated renegotiation.  Note that AsyncSSLSocket
812   // doesn't fully support renegotiation, so we could just fail all attempts
813   // to enforce this.  Once it is supported, we should make it an option
814   // to disable client-initiated renegotiation.
815   bool handshakeComplete_{false};
816   bool renegotiateAttempted_{false};
817   SSLStateEnum sslState_{STATE_UNINIT};
818   std::shared_ptr<folly::SSLContext> ctx_;
819   // Callback for SSL_accept() or SSL_connect()
820   HandshakeCB* handshakeCallback_{nullptr};
821   SSL* ssl_{nullptr};
822   SSL_SESSION *sslSession_{nullptr};
823   HandshakeTimeout handshakeTimeout_;
824   // whether the SSL session was resumed using session ID or not
825   bool sessionIDResumed_{false};
826
827   // Whether to track EOR or not.
828   bool trackEor_{false};
829
830   // The app byte num that we are tracking for the MSG_EOR
831   // Only one app EOR byte can be tracked.
832   size_t appEorByteNo_{0};
833
834   // Try to avoid calling SSL_write() for buffers smaller than this.
835   // It doesn't take effect when it is 0.
836   size_t minWriteSize_{1500};
837
838   // When openssl is about to sendmsg() across the minEorRawBytesNo_,
839   // it will pass MSG_EOR to sendmsg().
840   size_t minEorRawByteNo_{0};
841 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
842   std::shared_ptr<folly::SSLContext> handshakeCtx_;
843   std::string tlsextHostname_;
844 #endif
845
846   // a service identity that this socket/connection is associated with
847   std::string serviceIdentity_;
848
849   folly::SSLContext::SSLVerifyPeerEnum
850     verifyPeer_{folly::SSLContext::SSLVerifyPeerEnum::USE_CTX};
851
852   // Callback for SSL_CTX_set_verify()
853   static int sslVerifyCallback(int preverifyOk, X509_STORE_CTX* ctx);
854
855   bool parseClientHello_{false};
856   bool cacheAddrOnFailure_{false};
857   bool bufferMovableEnabled_{false};
858   std::unique_ptr<ssl::ClientHelloInfo> clientHelloInfo_;
859   std::vector<std::pair<char, StringPiece>> alertsReceived_;
860
861   // Time taken to complete the ssl handshake.
862   std::chrono::steady_clock::time_point handshakeStartTime_;
863   std::chrono::steady_clock::time_point handshakeEndTime_;
864 };
865
866 } // namespace