45fbaea4a97d5f391da1ee23c466a90309153cae
[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   // Timer for if we fallback from SSL connects to TCP connects
140   class ConnectionTimeout : public AsyncTimeout {
141    public:
142     ConnectionTimeout(AsyncSSLSocket* sslSocket, EventBase* eventBase)
143         : AsyncTimeout(eventBase), sslSocket_(sslSocket) {}
144
145     virtual void timeoutExpired() noexcept override {
146       sslSocket_->timeoutExpired();
147     }
148
149    private:
150     AsyncSSLSocket* sslSocket_;
151   };
152
153   /**
154    * Create a client AsyncSSLSocket
155    */
156   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext> &ctx,
157                  EventBase* evb, bool deferSecurityNegotiation = false);
158
159   /**
160    * Create a server/client AsyncSSLSocket from an already connected
161    * socket file descriptor.
162    *
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.
168    *
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
175    */
176   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext>& ctx,
177                  EventBase* evb, int fd,
178                  bool server = true, bool deferSecurityNegotiation = false);
179
180
181   /**
182    * Helper function to create a server/client shared_ptr<AsyncSSLSocket>.
183    */
184   static std::shared_ptr<AsyncSSLSocket> newSocket(
185     const std::shared_ptr<folly::SSLContext>& ctx,
186     EventBase* evb, int fd, bool server=true,
187     bool deferSecurityNegotiation = false) {
188     return std::shared_ptr<AsyncSSLSocket>(
189       new AsyncSSLSocket(ctx, evb, fd, server, deferSecurityNegotiation),
190       Destructor());
191   }
192
193   /**
194    * Helper function to create a client shared_ptr<AsyncSSLSocket>.
195    */
196   static std::shared_ptr<AsyncSSLSocket> newSocket(
197     const std::shared_ptr<folly::SSLContext>& ctx,
198     EventBase* evb, bool deferSecurityNegotiation = false) {
199     return std::shared_ptr<AsyncSSLSocket>(
200       new AsyncSSLSocket(ctx, evb, deferSecurityNegotiation),
201       Destructor());
202   }
203
204
205 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
206   /**
207    * Create a client AsyncSSLSocket with tlsext_servername in
208    * the Client Hello message.
209    */
210   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext> &ctx,
211                   EventBase* evb,
212                  const std::string& serverName,
213                 bool deferSecurityNegotiation = false);
214
215   /**
216    * Create a client AsyncSSLSocket from an already connected
217    * socket file descriptor.
218    *
219    * Note that while AsyncSSLSocket enables TCP_NODELAY for sockets it creates
220    * when connecting, it does not change the socket options when given an
221    * existing file descriptor.  If callers want TCP_NODELAY enabled when using
222    * this version of the constructor, they need to explicitly call
223    * setNoDelay(true) after the constructor returns.
224    *
225    * @param ctx  SSL context for this connection.
226    * @param evb  EventBase that will manage this socket.
227    * @param fd   File descriptor to take over (should be a connected socket).
228    * @param serverName tlsext_hostname that will be sent in ClientHello.
229    */
230   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext>& ctx,
231                   EventBase* evb,
232                   int fd,
233                  const std::string& serverName,
234                 bool deferSecurityNegotiation = false);
235
236   static std::shared_ptr<AsyncSSLSocket> newSocket(
237     const std::shared_ptr<folly::SSLContext>& ctx,
238     EventBase* evb,
239     const std::string& serverName,
240     bool deferSecurityNegotiation = false) {
241     return std::shared_ptr<AsyncSSLSocket>(
242       new AsyncSSLSocket(ctx, evb, serverName, deferSecurityNegotiation),
243       Destructor());
244   }
245 #endif
246
247   /**
248    * TODO: implement support for SSL renegotiation.
249    *
250    * This involves proper handling of the SSL_ERROR_WANT_READ/WRITE
251    * code as a result of SSL_write/read(), instead of returning an
252    * error. In that case, the READ/WRITE event should be registered,
253    * and a flag (e.g., writeBlockedOnRead) should be set to indiciate
254    * the condition. In the next invocation of read/write callback, if
255    * the flag is on, performWrite()/performRead() should be called in
256    * addition to the normal call to performRead()/performWrite(), and
257    * the flag should be reset.
258    */
259
260   // Inherit TAsyncTransport methods from AsyncSocket except the
261   // following.
262   // See the documentation in TAsyncTransport.h
263   // TODO: implement graceful shutdown in close()
264   // TODO: implement detachSSL() that returns the SSL connection
265   virtual void closeNow() override;
266   virtual void shutdownWrite() override;
267   virtual void shutdownWriteNow() override;
268   virtual bool good() const override;
269   virtual bool connecting() const override;
270   virtual std::string getApplicationProtocol() noexcept override;
271
272   virtual std::string getSecurityProtocol() const override { return "TLS"; }
273
274   bool isEorTrackingEnabled() const override;
275   virtual void setEorTracking(bool track) override;
276   virtual size_t getRawBytesWritten() const override;
277   virtual size_t getRawBytesReceived() const override;
278   void enableClientHelloParsing();
279
280   /**
281    * Accept an SSL connection on the socket.
282    *
283    * The callback will be invoked and uninstalled when an SSL
284    * connection has been established on the underlying socket.
285    * The value of verifyPeer determines the client verification method.
286    * By default, its set to use the value in the underlying context
287    *
288    * @param callback callback object to invoke on success/failure
289    * @param timeout timeout for this function in milliseconds, or 0 for no
290    *                timeout
291    * @param verifyPeer  SSLVerifyPeerEnum uses the options specified in the
292    *                context by default, can be set explcitly to override the
293    *                method in the context
294    */
295   virtual void sslAccept(HandshakeCB* callback, uint32_t timeout = 0,
296       const folly::SSLContext::SSLVerifyPeerEnum& verifyPeer =
297             folly::SSLContext::SSLVerifyPeerEnum::USE_CTX);
298
299   /**
300    * Invoke SSL accept following an asynchronous session cache lookup
301    */
302   void restartSSLAccept();
303
304   /**
305    * Connect to the given address, invoking callback when complete or on error
306    *
307    * Note timeout applies to TCP + SSL connection time
308    */
309   void connect(ConnectCallback* callback,
310                const folly::SocketAddress& address,
311                int timeout = 0,
312                const OptionMap &options = emptyOptionMap,
313                const folly::SocketAddress& bindAddr = anyAddress())
314                noexcept override;
315
316   using AsyncSocket::connect;
317
318   /**
319    * Initiate an SSL connection on the socket
320    * The callback will be invoked and uninstalled when an SSL connection
321    * has been establshed on the underlying socket.
322    * The verification option verifyPeer is applied if it's passed explicitly.
323    * If it's not, the options in SSLContext set on the underlying SSLContext
324    * are applied.
325    *
326    * @param callback callback object to invoke on success/failure
327    * @param timeout timeout for this function in milliseconds, or 0 for no
328    *                timeout
329    * @param verifyPeer  SSLVerifyPeerEnum uses the options specified in the
330    *                context by default, can be set explcitly to override the
331    *                method in the context. If verification is turned on sets
332    *                SSL_VERIFY_PEER and invokes
333    *                HandshakeCB::handshakeVer().
334    */
335   virtual void sslConn(HandshakeCB *callback, uint64_t timeout = 0,
336             const folly::SSLContext::SSLVerifyPeerEnum& verifyPeer =
337                   folly::SSLContext::SSLVerifyPeerEnum::USE_CTX);
338
339   enum SSLStateEnum {
340     STATE_UNINIT,
341     STATE_UNENCRYPTED,
342     STATE_ACCEPTING,
343     STATE_CACHE_LOOKUP,
344     STATE_ASYNC_PENDING,
345     STATE_CONNECTING,
346     STATE_ESTABLISHED,
347     STATE_REMOTE_CLOSED, /// remote end closed; we can still write
348     STATE_CLOSING,       ///< close() called, but waiting on writes to complete
349     /// close() called with pending writes, before connect() has completed
350     STATE_CONNECTING_CLOSING,
351     STATE_CLOSED,
352     STATE_ERROR
353   };
354
355   SSLStateEnum getSSLState() const { return sslState_;}
356
357   /**
358    * Get a handle to the negotiated SSL session.  This increments the session
359    * refcount and must be deallocated by the caller.
360    */
361   SSL_SESSION *getSSLSession();
362
363   /**
364    * Get a handle to the SSL struct.
365    */
366   const SSL* getSSL() const;
367
368   /**
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.
372    *
373    * @param takeOwnership if true, AsyncSSLSocket will assume the caller's
374    *                      reference count to session.
375    */
376   void setSSLSession(SSL_SESSION *session, bool takeOwnership = false);
377
378   /**
379    * Get the name of the protocol selected by the client during
380    * Next Protocol Negotiation (NPN) or Application Layer Protocol Negotiation
381    * (ALPN)
382    *
383    * Throw an exception if openssl does not support NPN
384    *
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
389    *                       of this string.
390    * @param protoNameLen   Length of the name.
391    * @param protoType      Whether this was an NPN or ALPN negotiation
392    */
393   virtual void getSelectedNextProtocol(
394       const unsigned char** protoName,
395       unsigned* protoLen,
396       SSLContext::NextProtocolType* protoType = nullptr) const;
397
398   /**
399    * Get the name of the protocol selected by the client during
400    * Next Protocol Negotiation (NPN) or Application Layer Protocol Negotiation
401    * (ALPN)
402    *
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
407    *                       of this string.
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
411    */
412   virtual bool getSelectedNextProtocolNoThrow(
413       const unsigned char** protoName,
414       unsigned* protoLen,
415       SSLContext::NextProtocolType* protoType = nullptr) const;
416
417   /**
418    * Determine if the session specified during setSSLSession was reused
419    * or if the server rejected it and issued a new session.
420    */
421   virtual bool getSSLSessionReused() const;
422
423   /**
424    * true if the session was resumed using session ID
425    */
426   bool sessionIDResumed() const { return sessionIDResumed_; }
427
428   void setSessionIDResumed(bool resumed) {
429     sessionIDResumed_ = resumed;
430   }
431
432   /**
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.
436    */
437   virtual const char* getNegotiatedCipherName() const;
438
439   /**
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.
444    */
445   const char *getSSLServerName() const;
446
447   /**
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"
452    */
453   const char *getSSLServerNameNoThrow() const;
454
455   /**
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.
460    */
461   int getSSLVersion() const;
462
463   /**
464    * Get the signature algorithm used in the cert that is used for this
465    * connection.
466    */
467   const char *getSSLCertSigAlgName() const;
468
469   /**
470    * Get the certificate size used for this SSL connection.
471    */
472   int getSSLCertSize() const;
473
474   /**
475    * Get the certificate used for this SSL connection. May be null
476    */
477   virtual const X509* getSelfCert() const override;
478
479   virtual void attachEventBase(EventBase* eventBase) override {
480     AsyncSocket::attachEventBase(eventBase);
481     handshakeTimeout_.attachEventBase(eventBase);
482     connectionTimeout_.attachEventBase(eventBase);
483   }
484
485   virtual void detachEventBase() override {
486     AsyncSocket::detachEventBase();
487     handshakeTimeout_.detachEventBase();
488     connectionTimeout_.detachEventBase();
489   }
490
491   virtual bool isDetachable() const override {
492     return AsyncSocket::isDetachable() && !handshakeTimeout_.isScheduled();
493   }
494
495   virtual void attachTimeoutManager(TimeoutManager* manager) {
496     handshakeTimeout_.attachTimeoutManager(manager);
497   }
498
499   virtual void detachTimeoutManager() {
500     handshakeTimeout_.detachTimeoutManager();
501   }
502
503 #if OPENSSL_VERSION_NUMBER >= 0x009080bfL
504   /**
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();
508    */
509   void attachSSLContext(const std::shared_ptr<folly::SSLContext>& ctx);
510
511   /**
512    * Detaches the SSL context for this socket.
513    */
514   void detachSSLContext();
515 #endif
516
517 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
518   /**
519    * Switch the SSLContext to continue the SSL handshake.
520    * It can only be used in server mode.
521    */
522   void switchServerSSLContext(
523     const std::shared_ptr<folly::SSLContext>& handshakeCtx);
524
525   /**
526    * Did server recognize/support the tlsext_hostname in Client Hello?
527    * It can only be used in client mode.
528    *
529    * @return true - tlsext_hostname is matched by the server
530    *         false - tlsext_hostname is not matched or
531    *                 is not supported by server
532    */
533   bool isServerNameMatch() const;
534
535   /**
536    * Set the SNI hostname that we'll advertise to the server in the
537    * ClientHello message.
538    */
539   void setServerName(std::string serverName) noexcept;
540 #endif
541
542   void timeoutExpired() noexcept;
543
544   /**
545    * Get the list of supported ciphers sent by the client in the client's
546    * preference order.
547    */
548   void getSSLClientCiphers(
549       std::string& clientCiphers,
550       bool convertToString = true) const {
551     std::stringstream ciphersStream;
552     std::string cipherName;
553
554     if (parseClientHello_ == false
555         || clientHelloInfo_->clientHelloCipherSuites_.empty()) {
556       clientCiphers = "";
557       return;
558     }
559
560     for (auto originalCipherCode : clientHelloInfo_->clientHelloCipherSuites_)
561     {
562       const SSL_CIPHER* cipher = nullptr;
563       if (convertToString) {
564         // OpenSSL expects code as a big endian char array
565         auto cipherCode = htons(originalCipherCode);
566
567 #if defined(SSL_OP_NO_TLSv1_2)
568         cipher =
569             TLSv1_2_method()->get_cipher_by_char((unsigned char*)&cipherCode);
570 #elif defined(SSL_OP_NO_TLSv1_1)
571         cipher =
572             TLSv1_1_method()->get_cipher_by_char((unsigned char*)&cipherCode);
573 #elif defined(SSL_OP_NO_TLSv1)
574         cipher =
575             TLSv1_method()->get_cipher_by_char((unsigned char*)&cipherCode);
576 #else
577         cipher =
578             SSLv3_method()->get_cipher_by_char((unsigned char*)&cipherCode);
579 #endif
580       }
581
582       if (cipher == nullptr) {
583         ciphersStream << std::setfill('0') << std::setw(4) << std::hex
584                       << originalCipherCode << ":";
585       } else {
586         ciphersStream << SSL_CIPHER_get_name(cipher) << ":";
587       }
588     }
589
590     clientCiphers = ciphersStream.str();
591     clientCiphers.erase(clientCiphers.end() - 1);
592   }
593
594   /**
595    * Get the list of compression methods sent by the client in TLS Hello.
596    */
597   std::string getSSLClientComprMethods() const {
598     if (!parseClientHello_) {
599       return "";
600     }
601     return folly::join(":", clientHelloInfo_->clientHelloCompressionMethods_);
602   }
603
604   /**
605    * Get the list of TLS extensions sent by the client in the TLS Hello.
606    */
607   std::string getSSLClientExts() const {
608     if (!parseClientHello_) {
609       return "";
610     }
611     return folly::join(":", clientHelloInfo_->clientHelloExtensions_);
612   }
613
614   std::string getSSLClientSigAlgs() const {
615     if (!parseClientHello_) {
616       return "";
617     }
618
619     std::string sigAlgs;
620     sigAlgs.reserve(clientHelloInfo_->clientHelloSigAlgs_.size() * 4);
621     for (size_t i = 0; i < clientHelloInfo_->clientHelloSigAlgs_.size(); i++) {
622       if (i) {
623         sigAlgs.push_back(':');
624       }
625       sigAlgs.append(folly::to<std::string>(
626           clientHelloInfo_->clientHelloSigAlgs_[i].first));
627       sigAlgs.push_back(',');
628       sigAlgs.append(folly::to<std::string>(
629           clientHelloInfo_->clientHelloSigAlgs_[i].second));
630     }
631
632     return sigAlgs;
633   }
634
635   std::string getSSLAlertsReceived() const {
636     std::string ret;
637
638     for (const auto& alert : alertsReceived_) {
639       if (!ret.empty()) {
640         ret.append(",");
641       }
642       ret.append(folly::to<std::string>(alert.first, ": ", alert.second));
643     }
644
645     return ret;
646   }
647
648   /**
649    * Get the list of shared ciphers between the server and the client.
650    * Works well for only SSLv2, not so good for SSLv3 or TLSv1.
651    */
652   void getSSLSharedCiphers(std::string& sharedCiphers) const {
653     char ciphersBuffer[1024];
654     ciphersBuffer[0] = '\0';
655     SSL_get_shared_ciphers(ssl_, ciphersBuffer, sizeof(ciphersBuffer) - 1);
656     sharedCiphers = ciphersBuffer;
657   }
658
659   /**
660    * Get the list of ciphers supported by the server in the server's
661    * preference order.
662    */
663   void getSSLServerCiphers(std::string& serverCiphers) const {
664     serverCiphers = SSL_get_cipher_list(ssl_, 0);
665     int i = 1;
666     const char *cipher;
667     while ((cipher = SSL_get_cipher_list(ssl_, i)) != nullptr) {
668       serverCiphers.append(":");
669       serverCiphers.append(cipher);
670       i++;
671     }
672   }
673
674   static int getSSLExDataIndex();
675   static AsyncSSLSocket* getFromSSL(const SSL *ssl);
676   static int bioWrite(BIO* b, const char* in, int inl);
677   void resetClientHelloParsing(SSL *ssl);
678   static void clientHelloParsingCallback(int write_p, int version,
679       int content_type, const void *buf, size_t len, SSL *ssl, void *arg);
680   static const char* getSSLServerNameFromSSL(SSL* ssl);
681
682   // For unit-tests
683   ssl::ClientHelloInfo* getClientHelloInfo() const {
684     return clientHelloInfo_.get();
685   }
686
687   /**
688    * Returns the time taken to complete a handshake.
689    */
690   virtual std::chrono::nanoseconds getHandshakeTime() const {
691     return handshakeEndTime_ - handshakeStartTime_;
692   }
693
694   void setMinWriteSize(size_t minWriteSize) {
695     minWriteSize_ = minWriteSize;
696   }
697
698   size_t getMinWriteSize() const {
699     return minWriteSize_;
700   }
701
702   void setReadCB(ReadCallback* callback) override;
703
704   /**
705    * Tries to enable the buffer movable experimental feature in openssl.
706    * This is not guaranteed to succeed in case openssl does not have
707    * the experimental feature built in.
708    */
709   void setBufferMovableEnabled(bool enabled);
710
711   /**
712    * Returns the peer certificate, or nullptr if no peer certificate received.
713    */
714   virtual ssl::X509UniquePtr getPeerCert() const override {
715     if (!ssl_) {
716       return nullptr;
717     }
718
719     X509* cert = SSL_get_peer_certificate(ssl_);
720     return ssl::X509UniquePtr(cert);
721   }
722
723   /**
724    * Force AsyncSSLSocket object to cache local and peer socket addresses.
725    * If called with "true" before connect() this function forces full local
726    * and remote socket addresses to be cached in the socket object and available
727    * through getLocalAddress()/getPeerAddress() methods even after the socket is
728    * closed.
729    */
730   void forceCacheAddrOnFailure(bool force) { cacheAddrOnFailure_ = force; }
731
732   const std::string& getServiceIdentity() const { return serviceIdentity_; }
733
734   void setServiceIdentity(std::string serviceIdentity) {
735     serviceIdentity_ = std::move(serviceIdentity);
736   }
737
738   void setCertCacheHit(bool hit) {
739     certCacheHit_ = hit;
740   }
741
742   bool getCertCacheHit() const {
743     return certCacheHit_;
744   }
745
746   bool sessionResumptionAttempted() const {
747     return sessionResumptionAttempted_;
748   }
749
750  private:
751
752   void init();
753
754  protected:
755
756   /**
757    * Protected destructor.
758    *
759    * Users of AsyncSSLSocket must never delete it directly.  Instead, invoke
760    * destroy() instead.  (See the documentation in DelayedDestruction.h for
761    * more details.)
762    */
763   ~AsyncSSLSocket();
764
765   // Inherit event notification methods from AsyncSocket except
766   // the following.
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;
772
773   void invalidState(HandshakeCB* callback);
774   bool willBlock(int ret,
775                  int* sslErrorOut,
776                  unsigned long* errErrorOut) noexcept;
777
778   virtual void checkForImmediateRead() noexcept override;
779   // AsyncSocket calls this at the wrong time for SSL
780   void handleInitialReadWrite() noexcept override {}
781
782   WriteResult interpretSSLError(int rc, int error);
783   ReadResult performRead(void** buf, size_t* buflen, size_t* offset) override;
784   WriteResult performWrite(
785       const iovec* vec,
786       uint32_t count,
787       WriteFlags flags,
788       uint32_t* countWritten,
789       uint32_t* partialWritten) override;
790
791   ssize_t performWriteIovec(const iovec* vec, uint32_t count,
792                             WriteFlags flags, uint32_t* countWritten,
793                             uint32_t* partialWritten);
794
795   // This virtual wrapper around SSL_write exists solely for testing/mockability
796   virtual int sslWriteImpl(SSL *ssl, const void *buf, int n) {
797     return SSL_write(ssl, buf, n);
798   }
799
800   /**
801    * Apply verification options passed to sslConn/sslAccept or those set
802    * in the underlying SSLContext object.
803    *
804    * @param ssl pointer to the SSL object on which verification options will be
805    * applied. If verifyPeer_ was explicitly set either via sslConn/sslAccept,
806    * those options override the settings in the underlying SSLContext.
807    */
808   void applyVerificationOptions(SSL * ssl);
809
810   /**
811    * Sets up SSL with a custom write bio which intercepts all writes.
812    *
813    * @return true, if succeeds and false if there is an error creating the bio.
814    */
815   bool setupSSLBio();
816
817   /**
818    * A SSL_write wrapper that understand EOR
819    *
820    * @param ssl: SSL* object
821    * @param buf: Buffer to be written
822    * @param n:   Number of bytes to be written
823    * @param eor: Does the last byte (buf[n-1]) have the app-last-byte?
824    * @return:    The number of app bytes successfully written to the socket
825    */
826   int eorAwareSSLWrite(SSL *ssl, const void *buf, int n, bool eor);
827
828   // Inherit error handling methods from AsyncSocket, plus the following.
829   void failHandshake(const char* fn, const AsyncSocketException& ex);
830
831   void invokeHandshakeErr(const AsyncSocketException& ex);
832   void invokeHandshakeCB();
833
834   void invokeConnectErr(const AsyncSocketException& ex) override;
835   void invokeConnectSuccess() override;
836   void scheduleConnectTimeout() override;
837
838   void cacheLocalPeerAddr();
839
840   void startSSLConnect();
841
842   static void sslInfoCallback(const SSL *ssl, int type, int val);
843
844   // Whether we've applied the TCP_CORK option to the socket
845   bool corked_{false};
846   // SSL related members.
847   bool server_{false};
848   // Used to prevent client-initiated renegotiation.  Note that AsyncSSLSocket
849   // doesn't fully support renegotiation, so we could just fail all attempts
850   // to enforce this.  Once it is supported, we should make it an option
851   // to disable client-initiated renegotiation.
852   bool handshakeComplete_{false};
853   bool renegotiateAttempted_{false};
854   SSLStateEnum sslState_{STATE_UNINIT};
855   std::shared_ptr<folly::SSLContext> ctx_;
856   // Callback for SSL_accept() or SSL_connect()
857   HandshakeCB* handshakeCallback_{nullptr};
858   SSL* ssl_{nullptr};
859   SSL_SESSION *sslSession_{nullptr};
860   HandshakeTimeout handshakeTimeout_;
861   ConnectionTimeout connectionTimeout_;
862   // whether the SSL session was resumed using session ID or not
863   bool sessionIDResumed_{false};
864
865   // Whether to track EOR or not.
866   bool trackEor_{false};
867
868   // The app byte num that we are tracking for the MSG_EOR
869   // Only one app EOR byte can be tracked.
870   size_t appEorByteNo_{0};
871
872   // Try to avoid calling SSL_write() for buffers smaller than this.
873   // It doesn't take effect when it is 0.
874   size_t minWriteSize_{1500};
875
876   // When openssl is about to sendmsg() across the minEorRawBytesNo_,
877   // it will pass MSG_EOR to sendmsg().
878   size_t minEorRawByteNo_{0};
879 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
880   std::shared_ptr<folly::SSLContext> handshakeCtx_;
881   std::string tlsextHostname_;
882 #endif
883
884   // a service identity that this socket/connection is associated with
885   std::string serviceIdentity_;
886
887   folly::SSLContext::SSLVerifyPeerEnum
888     verifyPeer_{folly::SSLContext::SSLVerifyPeerEnum::USE_CTX};
889
890   // Callback for SSL_CTX_set_verify()
891   static int sslVerifyCallback(int preverifyOk, X509_STORE_CTX* ctx);
892
893   bool parseClientHello_{false};
894   bool cacheAddrOnFailure_{false};
895   bool bufferMovableEnabled_{false};
896   bool certCacheHit_{false};
897   std::unique_ptr<ssl::ClientHelloInfo> clientHelloInfo_;
898   std::vector<std::pair<char, StringPiece>> alertsReceived_;
899
900   // Time taken to complete the ssl handshake.
901   std::chrono::steady_clock::time_point handshakeStartTime_;
902   std::chrono::steady_clock::time_point handshakeEndTime_;
903   uint64_t handshakeConnectTimeout_{0};
904   bool sessionResumptionAttempted_{false};
905 };
906
907 } // namespace