SSL cleanup: moving some OpenSSL definitions to new dir folly/io/async/ssl
[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/AsyncTimeout.h>
26 #include <folly/io/async/SSLContext.h>
27 #include <folly/io/async/TimeoutManager.h>
28 #include <folly/io/async/ssl/OpenSSLPtrTypes.h>
29 #include <folly/io/async/ssl/OpenSSLUtils.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
36 namespace folly {
37
38 class SSLException: public folly::AsyncSocketException {
39  public:
40   SSLException(int sslError,
41                unsigned long errError,
42                int sslOperationReturnValue,
43                int errno_copy);
44 };
45
46 /**
47  * A class for performing asynchronous I/O on an SSL connection.
48  *
49  * AsyncSSLSocket allows users to asynchronously wait for data on an
50  * SSL connection, and to asynchronously send data.
51  *
52  * The APIs for reading and writing are intentionally asymmetric.
53  * Waiting for data to read is a persistent API: a callback is
54  * installed, and is notified whenever new data is available.  It
55  * continues to be notified of new events until it is uninstalled.
56  *
57  * AsyncSSLSocket does not provide read timeout functionality,
58  * because it typically cannot determine when the timeout should be
59  * active.  Generally, a timeout should only be enabled when
60  * processing is blocked waiting on data from the remote endpoint.
61  * For server connections, the timeout should not be active if the
62  * server is currently processing one or more outstanding requests for
63  * this connection.  For client connections, the timeout should not be
64  * active if there are no requests pending on the connection.
65  * Additionally, if a client has multiple pending requests, it will
66  * ususally want a separate timeout for each request, rather than a
67  * single read timeout.
68  *
69  * The write API is fairly intuitive: a user can request to send a
70  * block of data, and a callback will be informed once the entire
71  * block has been transferred to the kernel, or on error.
72  * AsyncSSLSocket does provide a send timeout, since most callers
73  * want to give up if the remote end stops responding and no further
74  * progress can be made sending the data.
75  */
76 class AsyncSSLSocket : public virtual AsyncSocket {
77  public:
78   typedef std::unique_ptr<AsyncSSLSocket, Destructor> UniquePtr;
79   using X509_deleter = folly::static_function_deleter<X509, &X509_free>;
80
81   class HandshakeCB {
82    public:
83     virtual ~HandshakeCB() = default;
84
85     /**
86      * handshakeVer() is invoked during handshaking to give the
87      * application chance to validate it's peer's certificate.
88      *
89      * Note that OpenSSL performs only rudimentary internal
90      * consistency verification checks by itself. Any other validation
91      * like whether or not the certificate was issued by a trusted CA.
92      * The default implementation of this callback mimics what what
93      * OpenSSL does internally if SSL_VERIFY_PEER is set with no
94      * verification callback.
95      *
96      * See the passages on verify_callback in SSL_CTX_set_verify(3)
97      * for more details.
98      */
99     virtual bool handshakeVer(AsyncSSLSocket* /*sock*/,
100                                  bool preverifyOk,
101                                  X509_STORE_CTX* /*ctx*/) noexcept {
102       return preverifyOk;
103     }
104
105     /**
106      * handshakeSuc() is called when a new SSL connection is
107      * established, i.e., after SSL_accept/connect() returns successfully.
108      *
109      * The HandshakeCB will be uninstalled before handshakeSuc()
110      * is called.
111      *
112      * @param sock  SSL socket on which the handshake was initiated
113      */
114     virtual void handshakeSuc(AsyncSSLSocket *sock) noexcept = 0;
115
116     /**
117      * handshakeErr() is called if an error occurs while
118      * establishing the SSL connection.
119      *
120      * The HandshakeCB will be uninstalled before handshakeErr()
121      * is called.
122      *
123      * @param sock  SSL socket on which the handshake was initiated
124      * @param ex  An exception representing the error.
125      */
126     virtual void handshakeErr(
127       AsyncSSLSocket *sock,
128       const AsyncSocketException& ex)
129       noexcept = 0;
130   };
131
132   class HandshakeTimeout : public AsyncTimeout {
133    public:
134     HandshakeTimeout(AsyncSSLSocket* sslSocket, EventBase* eventBase)
135       : AsyncTimeout(eventBase)
136       , sslSocket_(sslSocket) {}
137
138     virtual void timeoutExpired() noexcept {
139       sslSocket_->timeoutExpired();
140     }
141
142    private:
143     AsyncSSLSocket* sslSocket_;
144   };
145
146
147   /**
148    * These are passed to the application via errno, packed in an SSL err which
149    * are outside the valid errno range.  The values are chosen to be unique
150    * against values in ssl.h
151    */
152   enum SSLError {
153     SSL_CLIENT_RENEGOTIATION_ATTEMPT = 900,
154     SSL_INVALID_RENEGOTIATION = 901,
155     SSL_EARLY_WRITE = 902
156   };
157
158   /**
159    * Create a client AsyncSSLSocket
160    */
161   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext> &ctx,
162                  EventBase* evb, bool deferSecurityNegotiation = false);
163
164   /**
165    * Create a server/client AsyncSSLSocket from an already connected
166    * socket file descriptor.
167    *
168    * Note that while AsyncSSLSocket enables TCP_NODELAY for sockets it creates
169    * when connecting, it does not change the socket options when given an
170    * existing file descriptor.  If callers want TCP_NODELAY enabled when using
171    * this version of the constructor, they need to explicitly call
172    * setNoDelay(true) after the constructor returns.
173    *
174    * @param ctx             SSL context for this connection.
175    * @param evb EventBase that will manage this socket.
176    * @param fd  File descriptor to take over (should be a connected socket).
177    * @param server Is socket in server mode?
178    * @param deferSecurityNegotiation
179    *          unencrypted data can be sent before sslConn/Accept
180    */
181   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext>& ctx,
182                  EventBase* evb, int fd,
183                  bool server = true, bool deferSecurityNegotiation = false);
184
185
186   /**
187    * Helper function to create a server/client shared_ptr<AsyncSSLSocket>.
188    */
189   static std::shared_ptr<AsyncSSLSocket> newSocket(
190     const std::shared_ptr<folly::SSLContext>& ctx,
191     EventBase* evb, int fd, bool server=true,
192     bool deferSecurityNegotiation = false) {
193     return std::shared_ptr<AsyncSSLSocket>(
194       new AsyncSSLSocket(ctx, evb, fd, server, deferSecurityNegotiation),
195       Destructor());
196   }
197
198   /**
199    * Helper function to create a client shared_ptr<AsyncSSLSocket>.
200    */
201   static std::shared_ptr<AsyncSSLSocket> newSocket(
202     const std::shared_ptr<folly::SSLContext>& ctx,
203     EventBase* evb, bool deferSecurityNegotiation = false) {
204     return std::shared_ptr<AsyncSSLSocket>(
205       new AsyncSSLSocket(ctx, evb, deferSecurityNegotiation),
206       Destructor());
207   }
208
209
210 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
211   /**
212    * Create a client AsyncSSLSocket with tlsext_servername in
213    * the Client Hello message.
214    */
215   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext> &ctx,
216                   EventBase* evb,
217                  const std::string& serverName,
218                 bool deferSecurityNegotiation = false);
219
220   /**
221    * Create a client AsyncSSLSocket from an already connected
222    * socket file descriptor.
223    *
224    * Note that while AsyncSSLSocket enables TCP_NODELAY for sockets it creates
225    * when connecting, it does not change the socket options when given an
226    * existing file descriptor.  If callers want TCP_NODELAY enabled when using
227    * this version of the constructor, they need to explicitly call
228    * setNoDelay(true) after the constructor returns.
229    *
230    * @param ctx  SSL context for this connection.
231    * @param evb  EventBase that will manage this socket.
232    * @param fd   File descriptor to take over (should be a connected socket).
233    * @param serverName tlsext_hostname that will be sent in ClientHello.
234    */
235   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext>& ctx,
236                   EventBase* evb,
237                   int fd,
238                  const std::string& serverName,
239                 bool deferSecurityNegotiation = false);
240
241   static std::shared_ptr<AsyncSSLSocket> newSocket(
242     const std::shared_ptr<folly::SSLContext>& ctx,
243     EventBase* evb,
244     const std::string& serverName,
245     bool deferSecurityNegotiation = false) {
246     return std::shared_ptr<AsyncSSLSocket>(
247       new AsyncSSLSocket(ctx, evb, serverName, deferSecurityNegotiation),
248       Destructor());
249   }
250 #endif
251
252   /**
253    * TODO: implement support for SSL renegotiation.
254    *
255    * This involves proper handling of the SSL_ERROR_WANT_READ/WRITE
256    * code as a result of SSL_write/read(), instead of returning an
257    * error. In that case, the READ/WRITE event should be registered,
258    * and a flag (e.g., writeBlockedOnRead) should be set to indiciate
259    * the condition. In the next invocation of read/write callback, if
260    * the flag is on, performWrite()/performRead() should be called in
261    * addition to the normal call to performRead()/performWrite(), and
262    * the flag should be reset.
263    */
264
265   // Inherit TAsyncTransport methods from AsyncSocket except the
266   // following.
267   // See the documentation in TAsyncTransport.h
268   // TODO: implement graceful shutdown in close()
269   // TODO: implement detachSSL() that returns the SSL connection
270   virtual void closeNow() override;
271   virtual void shutdownWrite() override;
272   virtual void shutdownWriteNow() override;
273   virtual bool good() const override;
274   virtual bool connecting() const override;
275   virtual std::string getApplicationProtocol() noexcept override;
276
277   virtual std::string getSecurityProtocol() const override { return "TLS"; }
278
279   bool isEorTrackingEnabled() const override;
280   virtual void setEorTracking(bool track) override;
281   virtual size_t getRawBytesWritten() const override;
282   virtual size_t getRawBytesReceived() const override;
283   void enableClientHelloParsing();
284
285   /**
286    * Accept an SSL connection on the socket.
287    *
288    * The callback will be invoked and uninstalled when an SSL
289    * connection has been established on the underlying socket.
290    * The value of verifyPeer determines the client verification method.
291    * By default, its set to use the value in the underlying context
292    *
293    * @param callback callback object to invoke on success/failure
294    * @param timeout timeout for this function in milliseconds, or 0 for no
295    *                timeout
296    * @param verifyPeer  SSLVerifyPeerEnum uses the options specified in the
297    *                context by default, can be set explcitly to override the
298    *                method in the context
299    */
300   virtual void sslAccept(HandshakeCB* callback, uint32_t timeout = 0,
301       const folly::SSLContext::SSLVerifyPeerEnum& verifyPeer =
302             folly::SSLContext::SSLVerifyPeerEnum::USE_CTX);
303
304   /**
305    * Invoke SSL accept following an asynchronous session cache lookup
306    */
307   void restartSSLAccept();
308
309   /**
310    * Connect to the given address, invoking callback when complete or on error
311    *
312    * Note timeout applies to TCP + SSL connection time
313    */
314   void connect(ConnectCallback* callback,
315                const folly::SocketAddress& address,
316                int timeout = 0,
317                const OptionMap &options = emptyOptionMap,
318                const folly::SocketAddress& bindAddr = anyAddress())
319                noexcept override;
320
321   using AsyncSocket::connect;
322
323   /**
324    * Initiate an SSL connection on the socket
325    * The callback will be invoked and uninstalled when an SSL connection
326    * has been establshed on the underlying socket.
327    * The verification option verifyPeer is applied if it's passed explicitly.
328    * If it's not, the options in SSLContext set on the underlying SSLContext
329    * are applied.
330    *
331    * @param callback callback object to invoke on success/failure
332    * @param timeout timeout for this function in milliseconds, or 0 for no
333    *                timeout
334    * @param verifyPeer  SSLVerifyPeerEnum uses the options specified in the
335    *                context by default, can be set explcitly to override the
336    *                method in the context. If verification is turned on sets
337    *                SSL_VERIFY_PEER and invokes
338    *                HandshakeCB::handshakeVer().
339    */
340   virtual void sslConn(HandshakeCB *callback, uint64_t timeout = 0,
341             const folly::SSLContext::SSLVerifyPeerEnum& verifyPeer =
342                   folly::SSLContext::SSLVerifyPeerEnum::USE_CTX);
343
344   enum SSLStateEnum {
345     STATE_UNINIT,
346     STATE_UNENCRYPTED,
347     STATE_ACCEPTING,
348     STATE_CACHE_LOOKUP,
349     STATE_ASYNC_PENDING,
350     STATE_CONNECTING,
351     STATE_ESTABLISHED,
352     STATE_REMOTE_CLOSED, /// remote end closed; we can still write
353     STATE_CLOSING,       ///< close() called, but waiting on writes to complete
354     /// close() called with pending writes, before connect() has completed
355     STATE_CONNECTING_CLOSING,
356     STATE_CLOSED,
357     STATE_ERROR
358   };
359
360   SSLStateEnum getSSLState() const { return sslState_;}
361
362   /**
363    * Get a handle to the negotiated SSL session.  This increments the session
364    * refcount and must be deallocated by the caller.
365    */
366   SSL_SESSION *getSSLSession();
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   virtual void attachEventBase(EventBase* eventBase) override {
475     AsyncSocket::attachEventBase(eventBase);
476     handshakeTimeout_.attachEventBase(eventBase);
477   }
478
479   virtual void detachEventBase() override {
480     AsyncSocket::detachEventBase();
481     handshakeTimeout_.detachEventBase();
482   }
483
484   virtual bool isDetachable() const override {
485     return AsyncSocket::isDetachable() && !handshakeTimeout_.isScheduled();
486   }
487
488   virtual void attachTimeoutManager(TimeoutManager* manager) {
489     handshakeTimeout_.attachTimeoutManager(manager);
490   }
491
492   virtual void detachTimeoutManager() {
493     handshakeTimeout_.detachTimeoutManager();
494   }
495
496 #if OPENSSL_VERSION_NUMBER >= 0x009080bfL
497   /**
498    * This function will set the SSL context for this socket to the
499    * argument. This should only be used on client SSL Sockets that have
500    * already called detachSSLContext();
501    */
502   void attachSSLContext(const std::shared_ptr<folly::SSLContext>& ctx);
503
504   /**
505    * Detaches the SSL context for this socket.
506    */
507   void detachSSLContext();
508 #endif
509
510 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
511   /**
512    * Switch the SSLContext to continue the SSL handshake.
513    * It can only be used in server mode.
514    */
515   void switchServerSSLContext(
516     const std::shared_ptr<folly::SSLContext>& handshakeCtx);
517
518   /**
519    * Did server recognize/support the tlsext_hostname in Client Hello?
520    * It can only be used in client mode.
521    *
522    * @return true - tlsext_hostname is matched by the server
523    *         false - tlsext_hostname is not matched or
524    *                 is not supported by server
525    */
526   bool isServerNameMatch() const;
527
528   /**
529    * Set the SNI hostname that we'll advertise to the server in the
530    * ClientHello message.
531    */
532   void setServerName(std::string serverName) noexcept;
533 #endif
534
535   void timeoutExpired() noexcept;
536
537   /**
538    * Get the list of supported ciphers sent by the client in the client's
539    * preference order.
540    */
541   void getSSLClientCiphers(std::string& clientCiphers) const {
542     std::stringstream ciphersStream;
543     std::string cipherName;
544
545     if (parseClientHello_ == false
546         || clientHelloInfo_->clientHelloCipherSuites_.empty()) {
547       clientCiphers = "";
548       return;
549     }
550
551     for (auto originalCipherCode : clientHelloInfo_->clientHelloCipherSuites_)
552     {
553       // OpenSSL expects code as a big endian char array
554       auto cipherCode = htons(originalCipherCode);
555
556 #if defined(SSL_OP_NO_TLSv1_2)
557       const SSL_CIPHER* cipher =
558           TLSv1_2_method()->get_cipher_by_char((unsigned char*)&cipherCode);
559 #elif defined(SSL_OP_NO_TLSv1_1)
560       const SSL_CIPHER* cipher =
561           TLSv1_1_method()->get_cipher_by_char((unsigned char*)&cipherCode);
562 #elif defined(SSL_OP_NO_TLSv1)
563       const SSL_CIPHER* cipher =
564           TLSv1_method()->get_cipher_by_char((unsigned char*)&cipherCode);
565 #else
566       const SSL_CIPHER* cipher =
567           SSLv3_method()->get_cipher_by_char((unsigned char*)&cipherCode);
568 #endif
569
570       if (cipher == nullptr) {
571         ciphersStream << std::setfill('0') << std::setw(4) << std::hex
572                       << originalCipherCode << ":";
573       } else {
574         ciphersStream << SSL_CIPHER_get_name(cipher) << ":";
575       }
576     }
577
578     clientCiphers = ciphersStream.str();
579     clientCiphers.erase(clientCiphers.end() - 1);
580   }
581
582   /**
583    * Get the list of compression methods sent by the client in TLS Hello.
584    */
585   std::string getSSLClientComprMethods() const {
586     if (!parseClientHello_) {
587       return "";
588     }
589     return folly::join(":", clientHelloInfo_->clientHelloCompressionMethods_);
590   }
591
592   /**
593    * Get the list of TLS extensions sent by the client in the TLS Hello.
594    */
595   std::string getSSLClientExts() const {
596     if (!parseClientHello_) {
597       return "";
598     }
599     return folly::join(":", clientHelloInfo_->clientHelloExtensions_);
600   }
601
602   std::string getSSLClientSigAlgs() const {
603     if (!parseClientHello_) {
604       return "";
605     }
606
607     std::string sigAlgs;
608     sigAlgs.reserve(clientHelloInfo_->clientHelloSigAlgs_.size() * 4);
609     for (size_t i = 0; i < clientHelloInfo_->clientHelloSigAlgs_.size(); i++) {
610       if (i) {
611         sigAlgs.push_back(':');
612       }
613       sigAlgs.append(folly::to<std::string>(
614           clientHelloInfo_->clientHelloSigAlgs_[i].first));
615       sigAlgs.push_back(',');
616       sigAlgs.append(folly::to<std::string>(
617           clientHelloInfo_->clientHelloSigAlgs_[i].second));
618     }
619
620     return sigAlgs;
621   }
622
623   /**
624    * Get the list of shared ciphers between the server and the client.
625    * Works well for only SSLv2, not so good for SSLv3 or TLSv1.
626    */
627   void getSSLSharedCiphers(std::string& sharedCiphers) const {
628     char ciphersBuffer[1024];
629     ciphersBuffer[0] = '\0';
630     SSL_get_shared_ciphers(ssl_, ciphersBuffer, sizeof(ciphersBuffer) - 1);
631     sharedCiphers = ciphersBuffer;
632   }
633
634   /**
635    * Get the list of ciphers supported by the server in the server's
636    * preference order.
637    */
638   void getSSLServerCiphers(std::string& serverCiphers) const {
639     serverCiphers = SSL_get_cipher_list(ssl_, 0);
640     int i = 1;
641     const char *cipher;
642     while ((cipher = SSL_get_cipher_list(ssl_, i)) != nullptr) {
643       serverCiphers.append(":");
644       serverCiphers.append(cipher);
645       i++;
646     }
647   }
648
649   static int getSSLExDataIndex();
650   static AsyncSSLSocket* getFromSSL(const SSL *ssl);
651   static int eorAwareBioWrite(BIO *b, const char *in, int inl);
652   void resetClientHelloParsing(SSL *ssl);
653   static void clientHelloParsingCallback(int write_p, int version,
654       int content_type, const void *buf, size_t len, SSL *ssl, void *arg);
655   static const char* getSSLServerNameFromSSL(SSL* ssl);
656
657   // For unit-tests
658   ssl::ClientHelloInfo* getClientHelloInfo() const {
659     return clientHelloInfo_.get();
660   }
661
662   /**
663    * Returns the time taken to complete a handshake.
664    */
665   virtual std::chrono::nanoseconds getHandshakeTime() const {
666     return handshakeEndTime_ - handshakeStartTime_;
667   }
668
669   void setMinWriteSize(size_t minWriteSize) {
670     minWriteSize_ = minWriteSize;
671   }
672
673   size_t getMinWriteSize() const {
674     return minWriteSize_;
675   }
676
677   void setReadCB(ReadCallback* callback) override;
678
679   /**
680    * Returns the peer certificate, or nullptr if no peer certificate received.
681    */
682   virtual ssl::X509UniquePtr getPeerCert() const override {
683     if (!ssl_) {
684       return nullptr;
685     }
686
687     X509* cert = SSL_get_peer_certificate(ssl_);
688     return ssl::X509UniquePtr(cert);
689   }
690
691   /**
692    * Force AsyncSSLSocket object to cache local and peer socket addresses.
693    * If called with "true" before connect() this function forces full local
694    * and remote socket addresses to be cached in the socket object and available
695    * through getLocalAddress()/getPeerAddress() methods even after the socket is
696    * closed.
697    */
698   void forceCacheAddrOnFailure(bool force) { cacheAddrOnFailure_ = force; }
699
700   const std::string& getServiceIdentity() const { return serviceIdentity_; }
701
702   void setServiceIdentity(std::string serviceIdentity) {
703     serviceIdentity_ = std::move(serviceIdentity);
704   }
705
706  private:
707
708   void init();
709
710  protected:
711
712   /**
713    * Protected destructor.
714    *
715    * Users of AsyncSSLSocket must never delete it directly.  Instead, invoke
716    * destroy() instead.  (See the documentation in DelayedDestruction.h for
717    * more details.)
718    */
719   ~AsyncSSLSocket();
720
721   // Inherit event notification methods from AsyncSocket except
722   // the following.
723   void prepareReadBuffer(void** buf, size_t* buflen) noexcept override;
724   void handleRead() noexcept override;
725   void handleWrite() noexcept override;
726   void handleAccept() noexcept;
727   void handleConnect() noexcept override;
728
729   void invalidState(HandshakeCB* callback);
730   bool willBlock(int ret,
731                  int* sslErrorOut,
732                  unsigned long* errErrorOut) noexcept;
733
734   virtual void checkForImmediateRead() noexcept override;
735   // AsyncSocket calls this at the wrong time for SSL
736   void handleInitialReadWrite() noexcept override {}
737
738   int interpretSSLError(int rc, int error);
739   ssize_t performRead(void** buf, size_t* buflen, size_t* offset) override;
740   ssize_t performWrite(const iovec* vec, uint32_t count, WriteFlags flags,
741                        uint32_t* countWritten, uint32_t* partialWritten)
742     override;
743
744   ssize_t performWriteIovec(const iovec* vec, uint32_t count,
745                             WriteFlags flags, uint32_t* countWritten,
746                             uint32_t* partialWritten);
747
748   // This virtual wrapper around SSL_write exists solely for testing/mockability
749   virtual int sslWriteImpl(SSL *ssl, const void *buf, int n) {
750     return SSL_write(ssl, buf, n);
751   }
752
753   /**
754    * Apply verification options passed to sslConn/sslAccept or those set
755    * in the underlying SSLContext object.
756    *
757    * @param ssl pointer to the SSL object on which verification options will be
758    * applied. If verifyPeer_ was explicitly set either via sslConn/sslAccept,
759    * those options override the settings in the underlying SSLContext.
760    */
761   void applyVerificationOptions(SSL * ssl);
762
763   /**
764    * A SSL_write wrapper that understand EOR
765    *
766    * @param ssl: SSL* object
767    * @param buf: Buffer to be written
768    * @param n:   Number of bytes to be written
769    * @param eor: Does the last byte (buf[n-1]) have the app-last-byte?
770    * @return:    The number of app bytes successfully written to the socket
771    */
772   int eorAwareSSLWrite(SSL *ssl, const void *buf, int n, bool eor);
773
774   // Inherit error handling methods from AsyncSocket, plus the following.
775   void failHandshake(const char* fn, const AsyncSocketException& ex);
776
777   void invokeHandshakeErr(const AsyncSocketException& ex);
778   void invokeHandshakeCB();
779
780   void cacheLocalPeerAddr();
781
782   static void sslInfoCallback(const SSL *ssl, int type, int val);
783
784   // Whether we've applied the TCP_CORK option to the socket
785   bool corked_{false};
786   // SSL related members.
787   bool server_{false};
788   // Used to prevent client-initiated renegotiation.  Note that AsyncSSLSocket
789   // doesn't fully support renegotiation, so we could just fail all attempts
790   // to enforce this.  Once it is supported, we should make it an option
791   // to disable client-initiated renegotiation.
792   bool handshakeComplete_{false};
793   bool renegotiateAttempted_{false};
794   SSLStateEnum sslState_{STATE_UNINIT};
795   std::shared_ptr<folly::SSLContext> ctx_;
796   // Callback for SSL_accept() or SSL_connect()
797   HandshakeCB* handshakeCallback_{nullptr};
798   SSL* ssl_{nullptr};
799   SSL_SESSION *sslSession_{nullptr};
800   HandshakeTimeout handshakeTimeout_;
801   // whether the SSL session was resumed using session ID or not
802   bool sessionIDResumed_{false};
803
804   // The app byte num that we are tracking for the MSG_EOR
805   // Only one app EOR byte can be tracked.
806   size_t appEorByteNo_{0};
807
808   // Try to avoid calling SSL_write() for buffers smaller than this.
809   // It doesn't take effect when it is 0.
810   size_t minWriteSize_{1500};
811
812   // When openssl is about to sendmsg() across the minEorRawBytesNo_,
813   // it will pass MSG_EOR to sendmsg().
814   size_t minEorRawByteNo_{0};
815 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
816   std::shared_ptr<folly::SSLContext> handshakeCtx_;
817   std::string tlsextHostname_;
818 #endif
819
820   // a service identity that this socket/connection is associated with
821   std::string serviceIdentity_;
822
823   folly::SSLContext::SSLVerifyPeerEnum
824     verifyPeer_{folly::SSLContext::SSLVerifyPeerEnum::USE_CTX};
825
826   // Callback for SSL_CTX_set_verify()
827   static int sslVerifyCallback(int preverifyOk, X509_STORE_CTX* ctx);
828
829   bool parseClientHello_{false};
830   bool cacheAddrOnFailure_{false};
831   std::unique_ptr<ssl::ClientHelloInfo> clientHelloInfo_;
832
833   // Time taken to complete the ssl handshake.
834   std::chrono::steady_clock::time_point handshakeStartTime_;
835   std::chrono::steady_clock::time_point handshakeEndTime_;
836 };
837
838 } // namespace