Check if handshake timeout is set in AsyncSSLSocket::isDetachable().
[folly.git] / folly / io / async / AsyncSSLSocket.h
1 /*
2  * Copyright 2015 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 <arpa/inet.h>
20 #include <iomanip>
21 #include <openssl/ssl.h>
22
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>
29
30 #include <folly/Bits.h>
31 #include <folly/io/IOBuf.h>
32 #include <folly/io/Cursor.h>
33
34 using folly::io::Cursor;
35 using std::unique_ptr;
36
37 namespace folly {
38
39 class SSLException: public folly::AsyncSocketException {
40  public:
41   SSLException(int sslError, int errno_copy);
42
43   int getSSLError() const { return error_; }
44
45  protected:
46   int error_;
47   char msg_[256];
48 };
49
50 /**
51  * A class for performing asynchronous I/O on an SSL connection.
52  *
53  * AsyncSSLSocket allows users to asynchronously wait for data on an
54  * SSL connection, and to asynchronously send data.
55  *
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.
60  *
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.
72  *
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.
79  */
80 class AsyncSSLSocket : public virtual AsyncSocket {
81  public:
82   typedef std::unique_ptr<AsyncSSLSocket, Destructor> UniquePtr;
83   using X509_deleter = folly::static_function_deleter<X509, &X509_free>;
84
85   class HandshakeCB {
86    public:
87     virtual ~HandshakeCB() = default;
88
89     /**
90      * handshakeVer() is invoked during handshaking to give the
91      * application chance to validate it's peer's certificate.
92      *
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.
99      *
100      * See the passages on verify_callback in SSL_CTX_set_verify(3)
101      * for more details.
102      */
103     virtual bool handshakeVer(AsyncSSLSocket* /*sock*/,
104                                  bool preverifyOk,
105                                  X509_STORE_CTX* /*ctx*/) noexcept {
106       return preverifyOk;
107     }
108
109     /**
110      * handshakeSuc() is called when a new SSL connection is
111      * established, i.e., after SSL_accept/connect() returns successfully.
112      *
113      * The HandshakeCB will be uninstalled before handshakeSuc()
114      * is called.
115      *
116      * @param sock  SSL socket on which the handshake was initiated
117      */
118     virtual void handshakeSuc(AsyncSSLSocket *sock) noexcept = 0;
119
120     /**
121      * handshakeErr() is called if an error occurs while
122      * establishing the SSL connection.
123      *
124      * The HandshakeCB will be uninstalled before handshakeErr()
125      * is called.
126      *
127      * @param sock  SSL socket on which the handshake was initiated
128      * @param ex  An exception representing the error.
129      */
130     virtual void handshakeErr(
131       AsyncSSLSocket *sock,
132       const AsyncSocketException& ex)
133       noexcept = 0;
134   };
135
136   class HandshakeTimeout : public AsyncTimeout {
137    public:
138     HandshakeTimeout(AsyncSSLSocket* sslSocket, EventBase* eventBase)
139       : AsyncTimeout(eventBase)
140       , sslSocket_(sslSocket) {}
141
142     virtual void timeoutExpired() noexcept {
143       sslSocket_->timeoutExpired();
144     }
145
146    private:
147     AsyncSSLSocket* sslSocket_;
148   };
149
150
151   /**
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
155    */
156   enum SSLError {
157     SSL_CLIENT_RENEGOTIATION_ATTEMPT = 900,
158     SSL_INVALID_RENEGOTIATION = 901,
159     SSL_EARLY_WRITE = 902
160   };
161
162   /**
163    * Create a client AsyncSSLSocket
164    */
165   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext> &ctx,
166                  EventBase* evb, bool deferSecurityNegotiation = false);
167
168   /**
169    * Create a server/client AsyncSSLSocket from an already connected
170    * socket file descriptor.
171    *
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.
177    *
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
184    */
185   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext>& ctx,
186                  EventBase* evb, int fd,
187                  bool server = true, bool deferSecurityNegotiation = false);
188
189
190   /**
191    * Helper function to create a server/client shared_ptr<AsyncSSLSocket>.
192    */
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),
199       Destructor());
200   }
201
202   /**
203    * Helper function to create a client shared_ptr<AsyncSSLSocket>.
204    */
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),
210       Destructor());
211   }
212
213
214 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
215   /**
216    * Create a client AsyncSSLSocket with tlsext_servername in
217    * the Client Hello message.
218    */
219   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext> &ctx,
220                   EventBase* evb,
221                  const std::string& serverName,
222                 bool deferSecurityNegotiation = false);
223
224   /**
225    * Create a client AsyncSSLSocket from an already connected
226    * socket file descriptor.
227    *
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.
233    *
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.
238    */
239   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext>& ctx,
240                   EventBase* evb,
241                   int fd,
242                  const std::string& serverName,
243                 bool deferSecurityNegotiation = false);
244
245   static std::shared_ptr<AsyncSSLSocket> newSocket(
246     const std::shared_ptr<folly::SSLContext>& ctx,
247     EventBase* evb,
248     const std::string& serverName,
249     bool deferSecurityNegotiation = false) {
250     return std::shared_ptr<AsyncSSLSocket>(
251       new AsyncSSLSocket(ctx, evb, serverName, deferSecurityNegotiation),
252       Destructor());
253   }
254 #endif
255
256   /**
257    * TODO: implement support for SSL renegotiation.
258    *
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.
267    */
268
269   // Inherit TAsyncTransport methods from AsyncSocket except the
270   // following.
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;
279
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();
285
286   /**
287    * Accept an SSL connection on the socket.
288    *
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
293    *
294    * @param callback callback object to invoke on success/failure
295    * @param timeout timeout for this function in milliseconds, or 0 for no
296    *                timeout
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
300    */
301   virtual void sslAccept(HandshakeCB* callback, uint32_t timeout = 0,
302       const folly::SSLContext::SSLVerifyPeerEnum& verifyPeer =
303             folly::SSLContext::SSLVerifyPeerEnum::USE_CTX);
304
305   /**
306    * Invoke SSL accept following an asynchronous session cache lookup
307    */
308   void restartSSLAccept();
309
310   /**
311    * Connect to the given address, invoking callback when complete or on error
312    *
313    * Note timeout applies to TCP + SSL connection time
314    */
315   void connect(ConnectCallback* callback,
316                const folly::SocketAddress& address,
317                int timeout = 0,
318                const OptionMap &options = emptyOptionMap,
319                const folly::SocketAddress& bindAddr = anyAddress())
320                noexcept override;
321
322   using AsyncSocket::connect;
323
324   /**
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
330    * are applied.
331    *
332    * @param callback callback object to invoke on success/failure
333    * @param timeout timeout for this function in milliseconds, or 0 for no
334    *                timeout
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().
340    */
341   virtual void sslConn(HandshakeCB *callback, uint64_t timeout = 0,
342             const folly::SSLContext::SSLVerifyPeerEnum& verifyPeer =
343                   folly::SSLContext::SSLVerifyPeerEnum::USE_CTX);
344
345   enum SSLStateEnum {
346     STATE_UNINIT,
347     STATE_UNENCRYPTED,
348     STATE_ACCEPTING,
349     STATE_CACHE_LOOKUP,
350     STATE_RSA_ASYNC_PENDING,
351     STATE_CONNECTING,
352     STATE_ESTABLISHED,
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,
357     STATE_CLOSED,
358     STATE_ERROR
359   };
360
361   SSLStateEnum getSSLState() const { return sslState_;}
362
363   /**
364    * Get a handle to the negotiated SSL session.  This increments the session
365    * refcount and must be deallocated by the caller.
366    */
367   SSL_SESSION *getSSLSession();
368
369   /**
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.
373    *
374    * @param takeOwnership if true, AsyncSSLSocket will assume the caller's
375    *                      reference count to session.
376    */
377   void setSSLSession(SSL_SESSION *session, bool takeOwnership = false);
378
379   /**
380    * Get the name of the protocol selected by the client during
381    * Next Protocol Negotiation (NPN)
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    */
392   virtual void getSelectedNextProtocol(const unsigned char** protoName,
393       unsigned* protoLen) const;
394
395   /**
396    * Get the name of the protocol selected by the client during
397    * Next Protocol Negotiation (NPN)
398    *
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
403    *                       of this string.
404    * @param protoNameLen   Length of the name.
405    * @return false if openssl does not support NPN
406    */
407   virtual bool getSelectedNextProtocolNoThrow(const unsigned char** protoName,
408       unsigned* protoLen) const;
409
410   /**
411    * Determine if the session specified during setSSLSession was reused
412    * or if the server rejected it and issued a new session.
413    */
414   bool getSSLSessionReused() const;
415
416   /**
417    * true if the session was resumed using session ID
418    */
419   bool sessionIDResumed() const { return sessionIDResumed_; }
420
421   void setSessionIDResumed(bool resumed) {
422     sessionIDResumed_ = resumed;
423   }
424
425   /**
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.
429    */
430   const char *getNegotiatedCipherName() const;
431
432   /**
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.
437    */
438   const char *getSSLServerName() const;
439
440   /**
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"
445    */
446   const char *getSSLServerNameNoThrow() const;
447
448   /**
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.
453    */
454   int getSSLVersion() const;
455
456   /**
457    * Get the certificate size used for this SSL connection.
458    */
459   int getSSLCertSize() const;
460
461   /* Get the number of bytes read from the wire (including protocol
462    * overhead). Returns 0 once the connection has been closed.
463    */
464   unsigned long getBytesRead() const {
465     if (ssl_ != nullptr) {
466       return BIO_number_read(SSL_get_rbio(ssl_));
467     }
468     return 0;
469   }
470
471   /* Get the number of bytes written to the wire (including protocol
472    * overhead).  Returns 0 once the connection has been closed.
473    */
474   unsigned long getBytesWritten() const {
475     if (ssl_ != nullptr) {
476       return BIO_number_written(SSL_get_wbio(ssl_));
477     }
478     return 0;
479   }
480
481   virtual void attachEventBase(EventBase* eventBase) override {
482     AsyncSocket::attachEventBase(eventBase);
483     handshakeTimeout_.attachEventBase(eventBase);
484   }
485
486   virtual void detachEventBase() override {
487     AsyncSocket::detachEventBase();
488     handshakeTimeout_.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(std::string& clientCiphers) const {
549     std::stringstream ciphersStream;
550     std::string cipherName;
551
552     if (parseClientHello_ == false
553         || clientHelloInfo_->clientHelloCipherSuites_.empty()) {
554       clientCiphers = "";
555       return;
556     }
557
558     for (auto originalCipherCode : clientHelloInfo_->clientHelloCipherSuites_)
559     {
560       // OpenSSL expects code as a big endian char array
561       auto cipherCode = htons(originalCipherCode);
562
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);
572 #else
573       const SSL_CIPHER* cipher =
574           SSLv3_method()->get_cipher_by_char((unsigned char*)&cipherCode);
575 #endif
576
577       if (cipher == nullptr) {
578         ciphersStream << std::setfill('0') << std::setw(4) << std::hex
579                       << originalCipherCode << ":";
580       } else {
581         ciphersStream << SSL_CIPHER_get_name(cipher) << ":";
582       }
583     }
584
585     clientCiphers = ciphersStream.str();
586     clientCiphers.erase(clientCiphers.end() - 1);
587   }
588
589   /**
590    * Get the list of compression methods sent by the client in TLS Hello.
591    */
592   std::string getSSLClientComprMethods() const {
593     if (!parseClientHello_) {
594       return "";
595     }
596     return folly::join(":", clientHelloInfo_->clientHelloCompressionMethods_);
597   }
598
599   /**
600    * Get the list of TLS extensions sent by the client in the TLS Hello.
601    */
602   std::string getSSLClientExts() const {
603     if (!parseClientHello_) {
604       return "";
605     }
606     return folly::join(":", clientHelloInfo_->clientHelloExtensions_);
607   }
608
609   std::string getSSLClientSigAlgs() const {
610     if (!parseClientHello_) {
611       return "";
612     }
613
614     std::string sigAlgs;
615     sigAlgs.reserve(clientHelloInfo_->clientHelloSigAlgs_.size() * 4);
616     for (size_t i = 0; i < clientHelloInfo_->clientHelloSigAlgs_.size(); i++) {
617       if (i) {
618         sigAlgs.push_back(':');
619       }
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));
625     }
626
627     return sigAlgs;
628   }
629
630   /**
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.
633    */
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;
639   }
640
641   /**
642    * Get the list of ciphers supported by the server in the server's
643    * preference order.
644    */
645   void getSSLServerCiphers(std::string& serverCiphers) const {
646     serverCiphers = SSL_get_cipher_list(ssl_, 0);
647     int i = 1;
648     const char *cipher;
649     while ((cipher = SSL_get_cipher_list(ssl_, i)) != nullptr) {
650       serverCiphers.append(":");
651       serverCiphers.append(cipher);
652       i++;
653     }
654   }
655
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);
662
663   // http://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml
664   enum class TLSExtension: uint16_t {
665     SERVER_NAME = 0,
666     MAX_FRAGMENT_LENGTH = 1,
667     CLIENT_CERTIFICATE_URL = 2,
668     TRUSTED_CA_KEYS = 3,
669     TRUNCATED_HMAC = 4,
670     STATUS_REQUEST = 5,
671     USER_MAPPING = 6,
672     CLIENT_AUTHZ = 7,
673     SERVER_AUTHZ = 8,
674     CERT_TYPE = 9,
675     SUPPORTED_GROUPS = 10,
676     EC_POINT_FORMATS = 11,
677     SRP = 12,
678     SIGNATURE_ALGORITHMS = 13,
679     USE_SRTP = 14,
680     HEARTBEAT = 15,
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,
686     PADDING = 21,
687     ENCRYPT_THEN_MAC = 22,
688     EXTENDED_MASTER_SECRET = 23,
689     SESSION_TICKET = 35,
690     RENEGOTIATION_INFO = 65281
691   };
692
693   // http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-18
694   enum class HashAlgorithm: uint8_t {
695     NONE = 0,
696     MD5 = 1,
697     SHA1 = 2,
698     SHA224 = 3,
699     SHA256 = 4,
700     SHA384 = 5,
701     SHA512 = 6
702   };
703
704   // http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-16
705   enum class SignatureAlgorithm: uint8_t {
706     ANONYMOUS = 0,
707     RSA = 1,
708     DSA = 2,
709     ECDSA = 3
710   };
711
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_;
719     std::vector<
720       std::pair<HashAlgorithm, SignatureAlgorithm>> clientHelloSigAlgs_;
721   };
722
723   // For unit-tests
724   ClientHelloInfo* getClientHelloInfo() const {
725     return clientHelloInfo_.get();
726   }
727
728   void setMinWriteSize(size_t minWriteSize) {
729     minWriteSize_ = minWriteSize;
730   }
731
732   size_t getMinWriteSize() const {
733     return minWriteSize_;
734   }
735
736   void setReadCB(ReadCallback* callback) override;
737
738   /**
739    * Returns the peer certificate, or nullptr if no peer certificate received.
740    */
741   std::unique_ptr<X509, X509_deleter> getPeerCert() const {
742     if (!ssl_) {
743       return nullptr;
744     }
745
746     X509* cert = SSL_get_peer_certificate(ssl_);
747     return std::unique_ptr<X509, X509_deleter>(cert);
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, int *errorOut) noexcept;
775
776   virtual void checkForImmediateRead() noexcept override;
777   // AsyncSocket calls this at the wrong time for SSL
778   void handleInitialReadWrite() noexcept override {}
779
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)
784     override;
785
786   ssize_t performWriteIovec(const iovec* vec, uint32_t count,
787                             WriteFlags flags, uint32_t* countWritten,
788                             uint32_t* partialWritten);
789
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);
793   }
794
795   /**
796    * Apply verification options passed to sslConn/sslAccept or those set
797    * in the underlying SSLContext object.
798    *
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.
802    */
803   void applyVerificationOptions(SSL * ssl);
804
805   /**
806    * A SSL_write wrapper that understand EOR
807    *
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
813    */
814   int eorAwareSSLWrite(SSL *ssl, const void *buf, int n, bool eor);
815
816   // Inherit error handling methods from AsyncSocket, plus the following.
817   void failHandshake(const char* fn, const AsyncSocketException& ex);
818
819   void invokeHandshakeCB();
820
821   static void sslInfoCallback(const SSL *ssl, int type, int val);
822
823   // Whether we've applied the TCP_CORK option to the socket
824   bool corked_{false};
825   // SSL related members.
826   bool server_{false};
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};
837   SSL* ssl_{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};
842
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};
846
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};
850
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_;
857 #endif
858   folly::SSLContext::SSLVerifyPeerEnum
859     verifyPeer_{folly::SSLContext::SSLVerifyPeerEnum::USE_CTX};
860
861   // Callback for SSL_CTX_set_verify()
862   static int sslVerifyCallback(int preverifyOk, X509_STORE_CTX* ctx);
863
864   bool parseClientHello_{false};
865   unique_ptr<ClientHelloInfo> clientHelloInfo_;
866 };
867
868 } // namespace