ECDSA async/offloading support for proxygen
[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 <arpa/inet.h>
20 #include <iomanip>
21
22 #include <folly/Optional.h>
23 #include <folly/String.h>
24 #include <folly/io/async/AsyncSocket.h>
25 #include <folly/io/async/SSLContext.h>
26 #include <folly/io/async/AsyncTimeout.h>
27 #include <folly/io/async/OpenSSLPtrTypes.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 namespace folly {
35
36 class SSLException: public folly::AsyncSocketException {
37  public:
38   SSLException(int sslError, int errno_copy);
39
40   int getSSLError() const { return error_; }
41
42  protected:
43   int error_;
44   char msg_[256];
45 };
46
47 /**
48  * A class for performing asynchronous I/O on an SSL connection.
49  *
50  * AsyncSSLSocket allows users to asynchronously wait for data on an
51  * SSL connection, and to asynchronously send data.
52  *
53  * The APIs for reading and writing are intentionally asymmetric.
54  * Waiting for data to read is a persistent API: a callback is
55  * installed, and is notified whenever new data is available.  It
56  * continues to be notified of new events until it is uninstalled.
57  *
58  * AsyncSSLSocket does not provide read timeout functionality,
59  * because it typically cannot determine when the timeout should be
60  * active.  Generally, a timeout should only be enabled when
61  * processing is blocked waiting on data from the remote endpoint.
62  * For server connections, the timeout should not be active if the
63  * server is currently processing one or more outstanding requests for
64  * this connection.  For client connections, the timeout should not be
65  * active if there are no requests pending on the connection.
66  * Additionally, if a client has multiple pending requests, it will
67  * ususally want a separate timeout for each request, rather than a
68  * single read timeout.
69  *
70  * The write API is fairly intuitive: a user can request to send a
71  * block of data, and a callback will be informed once the entire
72  * block has been transferred to the kernel, or on error.
73  * AsyncSSLSocket does provide a send timeout, since most callers
74  * want to give up if the remote end stops responding and no further
75  * progress can be made sending the data.
76  */
77 class AsyncSSLSocket : public virtual AsyncSocket {
78  public:
79   typedef std::unique_ptr<AsyncSSLSocket, Destructor> UniquePtr;
80   using X509_deleter = folly::static_function_deleter<X509, &X509_free>;
81
82   class HandshakeCB {
83    public:
84     virtual ~HandshakeCB() = default;
85
86     /**
87      * handshakeVer() is invoked during handshaking to give the
88      * application chance to validate it's peer's certificate.
89      *
90      * Note that OpenSSL performs only rudimentary internal
91      * consistency verification checks by itself. Any other validation
92      * like whether or not the certificate was issued by a trusted CA.
93      * The default implementation of this callback mimics what what
94      * OpenSSL does internally if SSL_VERIFY_PEER is set with no
95      * verification callback.
96      *
97      * See the passages on verify_callback in SSL_CTX_set_verify(3)
98      * for more details.
99      */
100     virtual bool handshakeVer(AsyncSSLSocket* /*sock*/,
101                                  bool preverifyOk,
102                                  X509_STORE_CTX* /*ctx*/) noexcept {
103       return preverifyOk;
104     }
105
106     /**
107      * handshakeSuc() is called when a new SSL connection is
108      * established, i.e., after SSL_accept/connect() returns successfully.
109      *
110      * The HandshakeCB will be uninstalled before handshakeSuc()
111      * is called.
112      *
113      * @param sock  SSL socket on which the handshake was initiated
114      */
115     virtual void handshakeSuc(AsyncSSLSocket *sock) noexcept = 0;
116
117     /**
118      * handshakeErr() is called if an error occurs while
119      * establishing the SSL connection.
120      *
121      * The HandshakeCB will be uninstalled before handshakeErr()
122      * is called.
123      *
124      * @param sock  SSL socket on which the handshake was initiated
125      * @param ex  An exception representing the error.
126      */
127     virtual void handshakeErr(
128       AsyncSSLSocket *sock,
129       const AsyncSocketException& ex)
130       noexcept = 0;
131   };
132
133   class HandshakeTimeout : public AsyncTimeout {
134    public:
135     HandshakeTimeout(AsyncSSLSocket* sslSocket, EventBase* eventBase)
136       : AsyncTimeout(eventBase)
137       , sslSocket_(sslSocket) {}
138
139     virtual void timeoutExpired() noexcept {
140       sslSocket_->timeoutExpired();
141     }
142
143    private:
144     AsyncSSLSocket* sslSocket_;
145   };
146
147
148   /**
149    * These are passed to the application via errno, packed in an SSL err which
150    * are outside the valid errno range.  The values are chosen to be unique
151    * against values in ssl.h
152    */
153   enum SSLError {
154     SSL_CLIENT_RENEGOTIATION_ATTEMPT = 900,
155     SSL_INVALID_RENEGOTIATION = 901,
156     SSL_EARLY_WRITE = 902
157   };
158
159   /**
160    * Create a client AsyncSSLSocket
161    */
162   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext> &ctx,
163                  EventBase* evb, bool deferSecurityNegotiation = false);
164
165   /**
166    * Create a server/client AsyncSSLSocket from an already connected
167    * socket file descriptor.
168    *
169    * Note that while AsyncSSLSocket enables TCP_NODELAY for sockets it creates
170    * when connecting, it does not change the socket options when given an
171    * existing file descriptor.  If callers want TCP_NODELAY enabled when using
172    * this version of the constructor, they need to explicitly call
173    * setNoDelay(true) after the constructor returns.
174    *
175    * @param ctx             SSL context for this connection.
176    * @param evb EventBase that will manage this socket.
177    * @param fd  File descriptor to take over (should be a connected socket).
178    * @param server Is socket in server mode?
179    * @param deferSecurityNegotiation
180    *          unencrypted data can be sent before sslConn/Accept
181    */
182   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext>& ctx,
183                  EventBase* evb, int fd,
184                  bool server = true, bool deferSecurityNegotiation = false);
185
186
187   /**
188    * Helper function to create a server/client shared_ptr<AsyncSSLSocket>.
189    */
190   static std::shared_ptr<AsyncSSLSocket> newSocket(
191     const std::shared_ptr<folly::SSLContext>& ctx,
192     EventBase* evb, int fd, bool server=true,
193     bool deferSecurityNegotiation = false) {
194     return std::shared_ptr<AsyncSSLSocket>(
195       new AsyncSSLSocket(ctx, evb, fd, server, deferSecurityNegotiation),
196       Destructor());
197   }
198
199   /**
200    * Helper function to create a client shared_ptr<AsyncSSLSocket>.
201    */
202   static std::shared_ptr<AsyncSSLSocket> newSocket(
203     const std::shared_ptr<folly::SSLContext>& ctx,
204     EventBase* evb, bool deferSecurityNegotiation = false) {
205     return std::shared_ptr<AsyncSSLSocket>(
206       new AsyncSSLSocket(ctx, evb, deferSecurityNegotiation),
207       Destructor());
208   }
209
210
211 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
212   /**
213    * Create a client AsyncSSLSocket with tlsext_servername in
214    * the Client Hello message.
215    */
216   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext> &ctx,
217                   EventBase* evb,
218                  const std::string& serverName,
219                 bool deferSecurityNegotiation = false);
220
221   /**
222    * Create a client AsyncSSLSocket from an already connected
223    * socket file descriptor.
224    *
225    * Note that while AsyncSSLSocket enables TCP_NODELAY for sockets it creates
226    * when connecting, it does not change the socket options when given an
227    * existing file descriptor.  If callers want TCP_NODELAY enabled when using
228    * this version of the constructor, they need to explicitly call
229    * setNoDelay(true) after the constructor returns.
230    *
231    * @param ctx  SSL context for this connection.
232    * @param evb  EventBase that will manage this socket.
233    * @param fd   File descriptor to take over (should be a connected socket).
234    * @param serverName tlsext_hostname that will be sent in ClientHello.
235    */
236   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext>& ctx,
237                   EventBase* evb,
238                   int fd,
239                  const std::string& serverName,
240                 bool deferSecurityNegotiation = false);
241
242   static std::shared_ptr<AsyncSSLSocket> newSocket(
243     const std::shared_ptr<folly::SSLContext>& ctx,
244     EventBase* evb,
245     const std::string& serverName,
246     bool deferSecurityNegotiation = false) {
247     return std::shared_ptr<AsyncSSLSocket>(
248       new AsyncSSLSocket(ctx, evb, serverName, deferSecurityNegotiation),
249       Destructor());
250   }
251 #endif
252
253   /**
254    * TODO: implement support for SSL renegotiation.
255    *
256    * This involves proper handling of the SSL_ERROR_WANT_READ/WRITE
257    * code as a result of SSL_write/read(), instead of returning an
258    * error. In that case, the READ/WRITE event should be registered,
259    * and a flag (e.g., writeBlockedOnRead) should be set to indiciate
260    * the condition. In the next invocation of read/write callback, if
261    * the flag is on, performWrite()/performRead() should be called in
262    * addition to the normal call to performRead()/performWrite(), and
263    * the flag should be reset.
264    */
265
266   // Inherit TAsyncTransport methods from AsyncSocket except the
267   // following.
268   // See the documentation in TAsyncTransport.h
269   // TODO: implement graceful shutdown in close()
270   // TODO: implement detachSSL() that returns the SSL connection
271   virtual void closeNow() override;
272   virtual void shutdownWrite() override;
273   virtual void shutdownWriteNow() override;
274   virtual bool good() const override;
275   virtual bool connecting() const override;
276   virtual std::string getApplicationProtocol() noexcept override;
277
278   virtual std::string getSecurityProtocol() const override { return "TLS"; }
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_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) or Application Layer Protocol Negotiation
382    * (ALPN)
383    *
384    * Throw an exception if openssl does not support NPN
385    *
386    * @param protoName      Name of the protocol (not guaranteed to be
387    *                       null terminated); will be set to nullptr if
388    *                       the client did not negotiate a protocol.
389    *                       Note: the AsyncSSLSocket retains ownership
390    *                       of this string.
391    * @param protoNameLen   Length of the name.
392    * @param protoType      Whether this was an NPN or ALPN negotiation
393    */
394   virtual void getSelectedNextProtocol(
395       const unsigned char** protoName,
396       unsigned* protoLen,
397       SSLContext::NextProtocolType* protoType = nullptr) const;
398
399   /**
400    * Get the name of the protocol selected by the client during
401    * Next Protocol Negotiation (NPN) or Application Layer Protocol Negotiation
402    * (ALPN)
403    *
404    * @param protoName      Name of the protocol (not guaranteed to be
405    *                       null terminated); will be set to nullptr if
406    *                       the client did not negotiate a protocol.
407    *                       Note: the AsyncSSLSocket retains ownership
408    *                       of this string.
409    * @param protoNameLen   Length of the name.
410    * @param protoType      Whether this was an NPN or ALPN negotiation
411    * @return false if openssl does not support NPN
412    */
413   virtual bool getSelectedNextProtocolNoThrow(
414       const unsigned char** protoName,
415       unsigned* protoLen,
416       SSLContext::NextProtocolType* protoType = nullptr) const;
417
418   /**
419    * Determine if the session specified during setSSLSession was reused
420    * or if the server rejected it and issued a new session.
421    */
422   virtual bool getSSLSessionReused() const;
423
424   /**
425    * true if the session was resumed using session ID
426    */
427   bool sessionIDResumed() const { return sessionIDResumed_; }
428
429   void setSessionIDResumed(bool resumed) {
430     sessionIDResumed_ = resumed;
431   }
432
433   /**
434    * Get the negociated cipher name for this SSL connection.
435    * Returns the cipher used or the constant value "NONE" when no SSL session
436    * has been established.
437    */
438   virtual const char* getNegotiatedCipherName() 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, throw TTransportException.
445    */
446   const char *getSSLServerName() const;
447
448   /**
449    * Get the server name for this SSL connection.
450    * Returns the server name used or the constant value "NONE" when no SSL
451    * session has been established.
452    * If openssl has no SNI support, return "NONE"
453    */
454   const char *getSSLServerNameNoThrow() const;
455
456   /**
457    * Get the SSL version for this connection.
458    * Possible return values are SSL2_VERSION, SSL3_VERSION, TLS1_VERSION,
459    * with hexa representations 0x200, 0x300, 0x301,
460    * or 0 if no SSL session has been established.
461    */
462   int getSSLVersion() const;
463
464   /**
465    * Get the signature algorithm used in the cert that is used for this
466    * connection.
467    */
468   const char *getSSLCertSigAlgName() const;
469
470   /**
471    * Get the certificate size used for this SSL connection.
472    */
473   int getSSLCertSize() const;
474
475   virtual void attachEventBase(EventBase* eventBase) override {
476     AsyncSocket::attachEventBase(eventBase);
477     handshakeTimeout_.attachEventBase(eventBase);
478   }
479
480   virtual void detachEventBase() override {
481     AsyncSocket::detachEventBase();
482     handshakeTimeout_.detachEventBase();
483   }
484
485   virtual bool isDetachable() const override {
486     return AsyncSocket::isDetachable() && !handshakeTimeout_.isScheduled();
487   }
488
489   virtual void attachTimeoutManager(TimeoutManager* manager) {
490     handshakeTimeout_.attachTimeoutManager(manager);
491   }
492
493   virtual void detachTimeoutManager() {
494     handshakeTimeout_.detachTimeoutManager();
495   }
496
497 #if OPENSSL_VERSION_NUMBER >= 0x009080bfL
498   /**
499    * This function will set the SSL context for this socket to the
500    * argument. This should only be used on client SSL Sockets that have
501    * already called detachSSLContext();
502    */
503   void attachSSLContext(const std::shared_ptr<folly::SSLContext>& ctx);
504
505   /**
506    * Detaches the SSL context for this socket.
507    */
508   void detachSSLContext();
509 #endif
510
511 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
512   /**
513    * Switch the SSLContext to continue the SSL handshake.
514    * It can only be used in server mode.
515    */
516   void switchServerSSLContext(
517     const std::shared_ptr<folly::SSLContext>& handshakeCtx);
518
519   /**
520    * Did server recognize/support the tlsext_hostname in Client Hello?
521    * It can only be used in client mode.
522    *
523    * @return true - tlsext_hostname is matched by the server
524    *         false - tlsext_hostname is not matched or
525    *                 is not supported by server
526    */
527   bool isServerNameMatch() const;
528
529   /**
530    * Set the SNI hostname that we'll advertise to the server in the
531    * ClientHello message.
532    */
533   void setServerName(std::string serverName) noexcept;
534 #endif
535
536   void timeoutExpired() noexcept;
537
538   /**
539    * Get the list of supported ciphers sent by the client in the client's
540    * preference order.
541    */
542   void getSSLClientCiphers(std::string& clientCiphers) const {
543     std::stringstream ciphersStream;
544     std::string cipherName;
545
546     if (parseClientHello_ == false
547         || clientHelloInfo_->clientHelloCipherSuites_.empty()) {
548       clientCiphers = "";
549       return;
550     }
551
552     for (auto originalCipherCode : clientHelloInfo_->clientHelloCipherSuites_)
553     {
554       // OpenSSL expects code as a big endian char array
555       auto cipherCode = htons(originalCipherCode);
556
557 #if defined(SSL_OP_NO_TLSv1_2)
558       const SSL_CIPHER* cipher =
559           TLSv1_2_method()->get_cipher_by_char((unsigned char*)&cipherCode);
560 #elif defined(SSL_OP_NO_TLSv1_1)
561       const SSL_CIPHER* cipher =
562           TLSv1_1_method()->get_cipher_by_char((unsigned char*)&cipherCode);
563 #elif defined(SSL_OP_NO_TLSv1)
564       const SSL_CIPHER* cipher =
565           TLSv1_method()->get_cipher_by_char((unsigned char*)&cipherCode);
566 #else
567       const SSL_CIPHER* cipher =
568           SSLv3_method()->get_cipher_by_char((unsigned char*)&cipherCode);
569 #endif
570
571       if (cipher == nullptr) {
572         ciphersStream << std::setfill('0') << std::setw(4) << std::hex
573                       << originalCipherCode << ":";
574       } else {
575         ciphersStream << SSL_CIPHER_get_name(cipher) << ":";
576       }
577     }
578
579     clientCiphers = ciphersStream.str();
580     clientCiphers.erase(clientCiphers.end() - 1);
581   }
582
583   /**
584    * Get the list of compression methods sent by the client in TLS Hello.
585    */
586   std::string getSSLClientComprMethods() const {
587     if (!parseClientHello_) {
588       return "";
589     }
590     return folly::join(":", clientHelloInfo_->clientHelloCompressionMethods_);
591   }
592
593   /**
594    * Get the list of TLS extensions sent by the client in the TLS Hello.
595    */
596   std::string getSSLClientExts() const {
597     if (!parseClientHello_) {
598       return "";
599     }
600     return folly::join(":", clientHelloInfo_->clientHelloExtensions_);
601   }
602
603   std::string getSSLClientSigAlgs() const {
604     if (!parseClientHello_) {
605       return "";
606     }
607
608     std::string sigAlgs;
609     sigAlgs.reserve(clientHelloInfo_->clientHelloSigAlgs_.size() * 4);
610     for (size_t i = 0; i < clientHelloInfo_->clientHelloSigAlgs_.size(); i++) {
611       if (i) {
612         sigAlgs.push_back(':');
613       }
614       sigAlgs.append(folly::to<std::string>(
615           clientHelloInfo_->clientHelloSigAlgs_[i].first));
616       sigAlgs.push_back(',');
617       sigAlgs.append(folly::to<std::string>(
618           clientHelloInfo_->clientHelloSigAlgs_[i].second));
619     }
620
621     return sigAlgs;
622   }
623
624   /**
625    * Get the list of shared ciphers between the server and the client.
626    * Works well for only SSLv2, not so good for SSLv3 or TLSv1.
627    */
628   void getSSLSharedCiphers(std::string& sharedCiphers) const {
629     char ciphersBuffer[1024];
630     ciphersBuffer[0] = '\0';
631     SSL_get_shared_ciphers(ssl_, ciphersBuffer, sizeof(ciphersBuffer) - 1);
632     sharedCiphers = ciphersBuffer;
633   }
634
635   /**
636    * Get the list of ciphers supported by the server in the server's
637    * preference order.
638    */
639   void getSSLServerCiphers(std::string& serverCiphers) const {
640     serverCiphers = SSL_get_cipher_list(ssl_, 0);
641     int i = 1;
642     const char *cipher;
643     while ((cipher = SSL_get_cipher_list(ssl_, i)) != nullptr) {
644       serverCiphers.append(":");
645       serverCiphers.append(cipher);
646       i++;
647     }
648   }
649
650   static int getSSLExDataIndex();
651   static AsyncSSLSocket* getFromSSL(const SSL *ssl);
652   static int eorAwareBioWrite(BIO *b, const char *in, int inl);
653   void resetClientHelloParsing(SSL *ssl);
654   static void clientHelloParsingCallback(int write_p, int version,
655       int content_type, const void *buf, size_t len, SSL *ssl, void *arg);
656   static const char* getSSLServerNameFromSSL(SSL* ssl);
657
658   // http://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml
659   enum class TLSExtension: uint16_t {
660     SERVER_NAME = 0,
661     MAX_FRAGMENT_LENGTH = 1,
662     CLIENT_CERTIFICATE_URL = 2,
663     TRUSTED_CA_KEYS = 3,
664     TRUNCATED_HMAC = 4,
665     STATUS_REQUEST = 5,
666     USER_MAPPING = 6,
667     CLIENT_AUTHZ = 7,
668     SERVER_AUTHZ = 8,
669     CERT_TYPE = 9,
670     SUPPORTED_GROUPS = 10,
671     EC_POINT_FORMATS = 11,
672     SRP = 12,
673     SIGNATURE_ALGORITHMS = 13,
674     USE_SRTP = 14,
675     HEARTBEAT = 15,
676     APPLICATION_LAYER_PROTOCOL_NEGOTIATION = 16,
677     STATUS_REQUEST_V2 = 17,
678     SIGNED_CERTIFICATE_TIMESTAMP = 18,
679     CLIENT_CERTIFICATE_TYPE = 19,
680     SERVER_CERTIFICATE_TYPE = 20,
681     PADDING = 21,
682     ENCRYPT_THEN_MAC = 22,
683     EXTENDED_MASTER_SECRET = 23,
684     SESSION_TICKET = 35,
685     RENEGOTIATION_INFO = 65281
686   };
687
688   // http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-18
689   enum class HashAlgorithm: uint8_t {
690     NONE = 0,
691     MD5 = 1,
692     SHA1 = 2,
693     SHA224 = 3,
694     SHA256 = 4,
695     SHA384 = 5,
696     SHA512 = 6
697   };
698
699   // http://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-16
700   enum class SignatureAlgorithm: uint8_t {
701     ANONYMOUS = 0,
702     RSA = 1,
703     DSA = 2,
704     ECDSA = 3
705   };
706
707   struct ClientHelloInfo {
708     folly::IOBufQueue clientHelloBuf_;
709     uint8_t clientHelloMajorVersion_;
710     uint8_t clientHelloMinorVersion_;
711     std::vector<uint16_t> clientHelloCipherSuites_;
712     std::vector<uint8_t> clientHelloCompressionMethods_;
713     std::vector<TLSExtension> clientHelloExtensions_;
714     std::vector<
715       std::pair<HashAlgorithm, SignatureAlgorithm>> clientHelloSigAlgs_;
716   };
717
718   // For unit-tests
719   ClientHelloInfo* getClientHelloInfo() const {
720     return clientHelloInfo_.get();
721   }
722
723   /**
724    * Returns the time taken to complete a handshake.
725    */
726   virtual std::chrono::nanoseconds getHandshakeTime() const {
727     return handshakeEndTime_ - handshakeStartTime_;
728   }
729
730   void setMinWriteSize(size_t minWriteSize) {
731     minWriteSize_ = minWriteSize;
732   }
733
734   size_t getMinWriteSize() const {
735     return minWriteSize_;
736   }
737
738   void setReadCB(ReadCallback* callback) override;
739
740   /**
741    * Returns the peer certificate, or nullptr if no peer certificate received.
742    */
743   virtual X509_UniquePtr getPeerCert() const override {
744     if (!ssl_) {
745       return nullptr;
746     }
747
748     X509* cert = SSL_get_peer_certificate(ssl_);
749     return X509_UniquePtr(cert);
750   }
751
752   /**
753    * Force AsyncSSLSocket object to cache local and peer socket addresses.
754    * If called with "true" before connect() this function forces full local
755    * and remote socket addresses to be cached in the socket object and available
756    * through getLocalAddress()/getPeerAddress() methods even after the socket is
757    * closed.
758    */
759   void forceCacheAddrOnFailure(bool force) { cacheAddrOnFailure_ = force; }
760
761  private:
762
763   void init();
764
765  protected:
766
767   /**
768    * Protected destructor.
769    *
770    * Users of AsyncSSLSocket must never delete it directly.  Instead, invoke
771    * destroy() instead.  (See the documentation in DelayedDestruction.h for
772    * more details.)
773    */
774   ~AsyncSSLSocket();
775
776   // Inherit event notification methods from AsyncSocket except
777   // the following.
778   void prepareReadBuffer(void** buf, size_t* buflen) noexcept override;
779   void handleRead() noexcept override;
780   void handleWrite() noexcept override;
781   void handleAccept() noexcept;
782   void handleConnect() noexcept override;
783
784   void invalidState(HandshakeCB* callback);
785   bool willBlock(int ret, int *errorOut) noexcept;
786
787   virtual void checkForImmediateRead() noexcept override;
788   // AsyncSocket calls this at the wrong time for SSL
789   void handleInitialReadWrite() noexcept override {}
790
791   int interpretSSLError(int rc, int error);
792   ssize_t performRead(void** buf, size_t* buflen, size_t* offset) override;
793   ssize_t performWrite(const iovec* vec, uint32_t count, WriteFlags flags,
794                        uint32_t* countWritten, uint32_t* partialWritten)
795     override;
796
797   ssize_t performWriteIovec(const iovec* vec, uint32_t count,
798                             WriteFlags flags, uint32_t* countWritten,
799                             uint32_t* partialWritten);
800
801   // This virtual wrapper around SSL_write exists solely for testing/mockability
802   virtual int sslWriteImpl(SSL *ssl, const void *buf, int n) {
803     return SSL_write(ssl, buf, n);
804   }
805
806   /**
807    * Apply verification options passed to sslConn/sslAccept or those set
808    * in the underlying SSLContext object.
809    *
810    * @param ssl pointer to the SSL object on which verification options will be
811    * applied. If verifyPeer_ was explicitly set either via sslConn/sslAccept,
812    * those options override the settings in the underlying SSLContext.
813    */
814   void applyVerificationOptions(SSL * ssl);
815
816   /**
817    * A SSL_write wrapper that understand EOR
818    *
819    * @param ssl: SSL* object
820    * @param buf: Buffer to be written
821    * @param n:   Number of bytes to be written
822    * @param eor: Does the last byte (buf[n-1]) have the app-last-byte?
823    * @return:    The number of app bytes successfully written to the socket
824    */
825   int eorAwareSSLWrite(SSL *ssl, const void *buf, int n, bool eor);
826
827   // Inherit error handling methods from AsyncSocket, plus the following.
828   void failHandshake(const char* fn, const AsyncSocketException& ex);
829
830   void invokeHandshakeErr(const AsyncSocketException& ex);
831   void invokeHandshakeCB();
832
833   void cacheLocalPeerAddr();
834
835   static void sslInfoCallback(const SSL *ssl, int type, int val);
836
837   // SSL related members.
838   bool server_{false};
839   // Used to prevent client-initiated renegotiation.  Note that AsyncSSLSocket
840   // doesn't fully support renegotiation, so we could just fail all attempts
841   // to enforce this.  Once it is supported, we should make it an option
842   // to disable client-initiated renegotiation.
843   bool handshakeComplete_{false};
844   bool renegotiateAttempted_{false};
845   SSLStateEnum sslState_{STATE_UNINIT};
846   std::shared_ptr<folly::SSLContext> ctx_;
847   // Callback for SSL_accept() or SSL_connect()
848   HandshakeCB* handshakeCallback_{nullptr};
849   SSL* ssl_{nullptr};
850   SSL_SESSION *sslSession_{nullptr};
851   HandshakeTimeout handshakeTimeout_;
852   // whether the SSL session was resumed using session ID or not
853   bool sessionIDResumed_{false};
854
855   // The app byte num that we are tracking for the MSG_EOR
856   // Only one app EOR byte can be tracked.
857   size_t appEorByteNo_{0};
858
859   // Try to avoid calling SSL_write() for buffers smaller than this.
860   // It doesn't take effect when it is 0.
861   size_t minWriteSize_{1500};
862
863   // When openssl is about to sendmsg() across the minEorRawBytesNo_,
864   // it will pass MSG_EOR to sendmsg().
865   size_t minEorRawByteNo_{0};
866 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
867   std::shared_ptr<folly::SSLContext> handshakeCtx_;
868   std::string tlsextHostname_;
869 #endif
870   folly::SSLContext::SSLVerifyPeerEnum
871     verifyPeer_{folly::SSLContext::SSLVerifyPeerEnum::USE_CTX};
872
873   // Callback for SSL_CTX_set_verify()
874   static int sslVerifyCallback(int preverifyOk, X509_STORE_CTX* ctx);
875
876   bool parseClientHello_{false};
877   bool cacheAddrOnFailure_{false};
878   std::unique_ptr<ClientHelloInfo> clientHelloInfo_;
879
880   // Time taken to complete the ssl handshake.
881   std::chrono::steady_clock::time_point handshakeStartTime_;
882   std::chrono::steady_clock::time_point handshakeEndTime_;
883 };
884
885 } // namespace