Enable EOR flag configuration for folly::AsyncSocket.
[folly.git] / folly / io / async / AsyncSSLSocket.h
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #pragma once
18
19 #include <iomanip>
20
21 #include <folly/Optional.h>
22 #include <folly/String.h>
23 #include <folly/io/async/AsyncSocket.h>
24 #include <folly/io/async/AsyncTimeout.h>
25 #include <folly/io/async/SSLContext.h>
26 #include <folly/io/async/TimeoutManager.h>
27 #include <folly/io/async/ssl/OpenSSLPtrTypes.h>
28 #include <folly/io/async/ssl/OpenSSLUtils.h>
29 #include <folly/io/async/ssl/SSLErrors.h>
30 #include <folly/io/async/ssl/TLSDefinitions.h>
31
32 #include <folly/Bits.h>
33 #include <folly/io/IOBuf.h>
34 #include <folly/io/Cursor.h>
35 #include <folly/portability/OpenSSL.h>
36 #include <folly/portability/Sockets.h>
37
38 namespace folly {
39
40 /**
41  * A class for performing asynchronous I/O on an SSL connection.
42  *
43  * AsyncSSLSocket allows users to asynchronously wait for data on an
44  * SSL connection, and to asynchronously send data.
45  *
46  * The APIs for reading and writing are intentionally asymmetric.
47  * Waiting for data to read is a persistent API: a callback is
48  * installed, and is notified whenever new data is available.  It
49  * continues to be notified of new events until it is uninstalled.
50  *
51  * AsyncSSLSocket does not provide read timeout functionality,
52  * because it typically cannot determine when the timeout should be
53  * active.  Generally, a timeout should only be enabled when
54  * processing is blocked waiting on data from the remote endpoint.
55  * For server connections, the timeout should not be active if the
56  * server is currently processing one or more outstanding requests for
57  * this connection.  For client connections, the timeout should not be
58  * active if there are no requests pending on the connection.
59  * Additionally, if a client has multiple pending requests, it will
60  * ususally want a separate timeout for each request, rather than a
61  * single read timeout.
62  *
63  * The write API is fairly intuitive: a user can request to send a
64  * block of data, and a callback will be informed once the entire
65  * block has been transferred to the kernel, or on error.
66  * AsyncSSLSocket does provide a send timeout, since most callers
67  * want to give up if the remote end stops responding and no further
68  * progress can be made sending the data.
69  */
70 class AsyncSSLSocket : public virtual AsyncSocket {
71  public:
72   typedef std::unique_ptr<AsyncSSLSocket, Destructor> UniquePtr;
73   using X509_deleter = folly::static_function_deleter<X509, &X509_free>;
74
75   class HandshakeCB {
76    public:
77     virtual ~HandshakeCB() = default;
78
79     /**
80      * handshakeVer() is invoked during handshaking to give the
81      * application chance to validate it's peer's certificate.
82      *
83      * Note that OpenSSL performs only rudimentary internal
84      * consistency verification checks by itself. Any other validation
85      * like whether or not the certificate was issued by a trusted CA.
86      * The default implementation of this callback mimics what what
87      * OpenSSL does internally if SSL_VERIFY_PEER is set with no
88      * verification callback.
89      *
90      * See the passages on verify_callback in SSL_CTX_set_verify(3)
91      * for more details.
92      */
93     virtual bool handshakeVer(AsyncSSLSocket* /*sock*/,
94                                  bool preverifyOk,
95                                  X509_STORE_CTX* /*ctx*/) noexcept {
96       return preverifyOk;
97     }
98
99     /**
100      * handshakeSuc() is called when a new SSL connection is
101      * established, i.e., after SSL_accept/connect() returns successfully.
102      *
103      * The HandshakeCB will be uninstalled before handshakeSuc()
104      * is called.
105      *
106      * @param sock  SSL socket on which the handshake was initiated
107      */
108     virtual void handshakeSuc(AsyncSSLSocket *sock) noexcept = 0;
109
110     /**
111      * handshakeErr() is called if an error occurs while
112      * establishing the SSL connection.
113      *
114      * The HandshakeCB will be uninstalled before handshakeErr()
115      * is called.
116      *
117      * @param sock  SSL socket on which the handshake was initiated
118      * @param ex  An exception representing the error.
119      */
120     virtual void handshakeErr(
121       AsyncSSLSocket *sock,
122       const AsyncSocketException& ex)
123       noexcept = 0;
124   };
125
126   class HandshakeTimeout : public AsyncTimeout {
127    public:
128     HandshakeTimeout(AsyncSSLSocket* sslSocket, EventBase* eventBase)
129       : AsyncTimeout(eventBase)
130       , sslSocket_(sslSocket) {}
131
132     virtual void timeoutExpired() noexcept {
133       sslSocket_->timeoutExpired();
134     }
135
136    private:
137     AsyncSSLSocket* sslSocket_;
138   };
139
140   // Timer for if we fallback from SSL connects to TCP connects
141   class ConnectionTimeout : public AsyncTimeout {
142    public:
143     ConnectionTimeout(AsyncSSLSocket* sslSocket, EventBase* eventBase)
144         : AsyncTimeout(eventBase), sslSocket_(sslSocket) {}
145
146     virtual void timeoutExpired() noexcept override {
147       sslSocket_->timeoutExpired();
148     }
149
150    private:
151     AsyncSSLSocket* sslSocket_;
152   };
153
154   /**
155    * Create a client AsyncSSLSocket
156    */
157   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext> &ctx,
158                  EventBase* evb, bool deferSecurityNegotiation = false);
159
160   /**
161    * Create a server/client AsyncSSLSocket from an already connected
162    * socket file descriptor.
163    *
164    * Note that while AsyncSSLSocket enables TCP_NODELAY for sockets it creates
165    * when connecting, it does not change the socket options when given an
166    * existing file descriptor.  If callers want TCP_NODELAY enabled when using
167    * this version of the constructor, they need to explicitly call
168    * setNoDelay(true) after the constructor returns.
169    *
170    * @param ctx             SSL context for this connection.
171    * @param evb EventBase that will manage this socket.
172    * @param fd  File descriptor to take over (should be a connected socket).
173    * @param server Is socket in server mode?
174    * @param deferSecurityNegotiation
175    *          unencrypted data can be sent before sslConn/Accept
176    */
177   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext>& ctx,
178                  EventBase* evb, int fd,
179                  bool server = true, bool deferSecurityNegotiation = false);
180
181
182   /**
183    * Helper function to create a server/client shared_ptr<AsyncSSLSocket>.
184    */
185   static std::shared_ptr<AsyncSSLSocket> newSocket(
186     const std::shared_ptr<folly::SSLContext>& ctx,
187     EventBase* evb, int fd, bool server=true,
188     bool deferSecurityNegotiation = false) {
189     return std::shared_ptr<AsyncSSLSocket>(
190       new AsyncSSLSocket(ctx, evb, fd, server, deferSecurityNegotiation),
191       Destructor());
192   }
193
194   /**
195    * Helper function to create a client shared_ptr<AsyncSSLSocket>.
196    */
197   static std::shared_ptr<AsyncSSLSocket> newSocket(
198     const std::shared_ptr<folly::SSLContext>& ctx,
199     EventBase* evb, bool deferSecurityNegotiation = false) {
200     return std::shared_ptr<AsyncSSLSocket>(
201       new AsyncSSLSocket(ctx, evb, deferSecurityNegotiation),
202       Destructor());
203   }
204
205
206 #if FOLLY_OPENSSL_HAS_SNI
207   /**
208    * Create a client AsyncSSLSocket with tlsext_servername in
209    * the Client Hello message.
210    */
211   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext> &ctx,
212                   EventBase* evb,
213                  const std::string& serverName,
214                 bool deferSecurityNegotiation = false);
215
216   /**
217    * Create a client AsyncSSLSocket from an already connected
218    * socket file descriptor.
219    *
220    * Note that while AsyncSSLSocket enables TCP_NODELAY for sockets it creates
221    * when connecting, it does not change the socket options when given an
222    * existing file descriptor.  If callers want TCP_NODELAY enabled when using
223    * this version of the constructor, they need to explicitly call
224    * setNoDelay(true) after the constructor returns.
225    *
226    * @param ctx  SSL context for this connection.
227    * @param evb  EventBase that will manage this socket.
228    * @param fd   File descriptor to take over (should be a connected socket).
229    * @param serverName tlsext_hostname that will be sent in ClientHello.
230    */
231   AsyncSSLSocket(const std::shared_ptr<folly::SSLContext>& ctx,
232                   EventBase* evb,
233                   int fd,
234                  const std::string& serverName,
235                 bool deferSecurityNegotiation = false);
236
237   static std::shared_ptr<AsyncSSLSocket> newSocket(
238     const std::shared_ptr<folly::SSLContext>& ctx,
239     EventBase* evb,
240     const std::string& serverName,
241     bool deferSecurityNegotiation = false) {
242     return std::shared_ptr<AsyncSSLSocket>(
243       new AsyncSSLSocket(ctx, evb, serverName, deferSecurityNegotiation),
244       Destructor());
245   }
246 #endif // FOLLY_OPENSSL_HAS_SNI
247
248   /**
249    * TODO: implement support for SSL renegotiation.
250    *
251    * This involves proper handling of the SSL_ERROR_WANT_READ/WRITE
252    * code as a result of SSL_write/read(), instead of returning an
253    * error. In that case, the READ/WRITE event should be registered,
254    * and a flag (e.g., writeBlockedOnRead) should be set to indiciate
255    * the condition. In the next invocation of read/write callback, if
256    * the flag is on, performWrite()/performRead() should be called in
257    * addition to the normal call to performRead()/performWrite(), and
258    * the flag should be reset.
259    */
260
261   // Inherit TAsyncTransport methods from AsyncSocket except the
262   // following.
263   // See the documentation in TAsyncTransport.h
264   // TODO: implement graceful shutdown in close()
265   // TODO: implement detachSSL() that returns the SSL connection
266   virtual void closeNow() override;
267   virtual void shutdownWrite() override;
268   virtual void shutdownWriteNow() override;
269   virtual bool good() const override;
270   virtual bool connecting() const override;
271   virtual std::string getApplicationProtocol() noexcept override;
272
273   virtual std::string getSecurityProtocol() const override { return "TLS"; }
274
275   virtual void setEorTracking(bool track) override;
276   virtual size_t getRawBytesWritten() const override;
277   virtual size_t getRawBytesReceived() const override;
278   void enableClientHelloParsing();
279
280   void setPreReceivedData(std::unique_ptr<IOBuf> data);
281
282   /**
283    * Accept an SSL connection on the socket.
284    *
285    * The callback will be invoked and uninstalled when an SSL
286    * connection has been established on the underlying socket.
287    * The value of verifyPeer determines the client verification method.
288    * By default, its set to use the value in the underlying context
289    *
290    * @param callback callback object to invoke on success/failure
291    * @param timeout timeout for this function in milliseconds, or 0 for no
292    *                timeout
293    * @param verifyPeer  SSLVerifyPeerEnum uses the options specified in the
294    *                context by default, can be set explcitly to override the
295    *                method in the context
296    */
297   virtual void sslAccept(
298       HandshakeCB* callback,
299       std::chrono::milliseconds timeout = std::chrono::milliseconds::zero(),
300       const folly::SSLContext::SSLVerifyPeerEnum& verifyPeer =
301           folly::SSLContext::SSLVerifyPeerEnum::USE_CTX);
302
303   /**
304    * Invoke SSL accept following an asynchronous session cache lookup
305    */
306   void restartSSLAccept();
307
308   /**
309    * Connect to the given address, invoking callback when complete or on error
310    *
311    * Note timeout applies to TCP + SSL connection time
312    */
313   void connect(ConnectCallback* callback,
314                const folly::SocketAddress& address,
315                int timeout = 0,
316                const OptionMap &options = emptyOptionMap,
317                const folly::SocketAddress& bindAddr = anyAddress())
318                noexcept override;
319
320   using AsyncSocket::connect;
321
322   /**
323    * Initiate an SSL connection on the socket
324    * The callback will be invoked and uninstalled when an SSL connection
325    * has been establshed on the underlying socket.
326    * The verification option verifyPeer is applied if it's passed explicitly.
327    * If it's not, the options in SSLContext set on the underlying SSLContext
328    * are applied.
329    *
330    * @param callback callback object to invoke on success/failure
331    * @param timeout timeout for this function in milliseconds, or 0 for no
332    *                timeout
333    * @param verifyPeer  SSLVerifyPeerEnum uses the options specified in the
334    *                context by default, can be set explcitly to override the
335    *                method in the context. If verification is turned on sets
336    *                SSL_VERIFY_PEER and invokes
337    *                HandshakeCB::handshakeVer().
338    */
339   virtual void sslConn(
340       HandshakeCB* callback,
341       std::chrono::milliseconds timeout = std::chrono::milliseconds::zero(),
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    * Get a handle to the SSL struct.
371    */
372   const SSL* getSSL() const;
373
374   /**
375    * Set the SSL session to be used during sslConn.  AsyncSSLSocket will
376    * hold a reference to the session until it is destroyed or released by the
377    * underlying SSL structure.
378    *
379    * @param takeOwnership if true, AsyncSSLSocket will assume the caller's
380    *                      reference count to session.
381    */
382   void setSSLSession(SSL_SESSION *session, bool takeOwnership = false);
383
384   /**
385    * Get the name of the protocol selected by the client during
386    * Next Protocol Negotiation (NPN) or Application Layer Protocol Negotiation
387    * (ALPN)
388    *
389    * Throw an exception if openssl does not support NPN
390    *
391    * @param protoName      Name of the protocol (not guaranteed to be
392    *                       null terminated); will be set to nullptr if
393    *                       the client did not negotiate a protocol.
394    *                       Note: the AsyncSSLSocket retains ownership
395    *                       of this string.
396    * @param protoNameLen   Length of the name.
397    * @param protoType      Whether this was an NPN or ALPN negotiation
398    */
399   virtual void getSelectedNextProtocol(
400       const unsigned char** protoName,
401       unsigned* protoLen,
402       SSLContext::NextProtocolType* protoType = nullptr) const;
403
404   /**
405    * Get the name of the protocol selected by the client during
406    * Next Protocol Negotiation (NPN) or Application Layer Protocol Negotiation
407    * (ALPN)
408    *
409    * @param protoName      Name of the protocol (not guaranteed to be
410    *                       null terminated); will be set to nullptr if
411    *                       the client did not negotiate a protocol.
412    *                       Note: the AsyncSSLSocket retains ownership
413    *                       of this string.
414    * @param protoNameLen   Length of the name.
415    * @param protoType      Whether this was an NPN or ALPN negotiation
416    * @return false if openssl does not support NPN
417    */
418   virtual bool getSelectedNextProtocolNoThrow(
419       const unsigned char** protoName,
420       unsigned* protoLen,
421       SSLContext::NextProtocolType* protoType = nullptr) const;
422
423   /**
424    * Determine if the session specified during setSSLSession was reused
425    * or if the server rejected it and issued a new session.
426    */
427   virtual bool getSSLSessionReused() const;
428
429   /**
430    * true if the session was resumed using session ID
431    */
432   bool sessionIDResumed() const { return sessionIDResumed_; }
433
434   void setSessionIDResumed(bool resumed) {
435     sessionIDResumed_ = resumed;
436   }
437
438   /**
439    * Get the negociated cipher name for this SSL connection.
440    * Returns the cipher used or the constant value "NONE" when no SSL session
441    * has been established.
442    */
443   virtual const char* getNegotiatedCipherName() const;
444
445   /**
446    * Get the server name for this SSL connection.
447    * Returns the server name used or the constant value "NONE" when no SSL
448    * session has been established.
449    * If openssl has no SNI support, throw TTransportException.
450    */
451   const char *getSSLServerName() const;
452
453   /**
454    * Get the server name for this SSL connection.
455    * Returns the server name used or the constant value "NONE" when no SSL
456    * session has been established.
457    * If openssl has no SNI support, return "NONE"
458    */
459   const char *getSSLServerNameNoThrow() const;
460
461   /**
462    * Get the SSL version for this connection.
463    * Possible return values are SSL2_VERSION, SSL3_VERSION, TLS1_VERSION,
464    * with hexa representations 0x200, 0x300, 0x301,
465    * or 0 if no SSL session has been established.
466    */
467   int getSSLVersion() const;
468
469   /**
470    * Get the signature algorithm used in the cert that is used for this
471    * connection.
472    */
473   const char *getSSLCertSigAlgName() const;
474
475   /**
476    * Get the certificate size used for this SSL connection.
477    */
478   int getSSLCertSize() const;
479
480   /**
481    * Get the certificate used for this SSL connection. May be null
482    */
483   virtual const X509* getSelfCert() const override;
484
485   virtual void attachEventBase(EventBase* eventBase) override {
486     AsyncSocket::attachEventBase(eventBase);
487     handshakeTimeout_.attachEventBase(eventBase);
488     connectionTimeout_.attachEventBase(eventBase);
489   }
490
491   virtual void detachEventBase() override {
492     AsyncSocket::detachEventBase();
493     handshakeTimeout_.detachEventBase();
494     connectionTimeout_.detachEventBase();
495   }
496
497   virtual bool isDetachable() const override {
498     return AsyncSocket::isDetachable() && !handshakeTimeout_.isScheduled();
499   }
500
501   virtual void attachTimeoutManager(TimeoutManager* manager) {
502     handshakeTimeout_.attachTimeoutManager(manager);
503   }
504
505   virtual void detachTimeoutManager() {
506     handshakeTimeout_.detachTimeoutManager();
507   }
508
509 #if OPENSSL_VERSION_NUMBER >= 0x009080bfL
510   /**
511    * This function will set the SSL context for this socket to the
512    * argument. This should only be used on client SSL Sockets that have
513    * already called detachSSLContext();
514    */
515   void attachSSLContext(const std::shared_ptr<folly::SSLContext>& ctx);
516
517   /**
518    * Detaches the SSL context for this socket.
519    */
520   void detachSSLContext();
521 #endif
522
523 #if FOLLY_OPENSSL_HAS_SNI
524   /**
525    * Switch the SSLContext to continue the SSL handshake.
526    * It can only be used in server mode.
527    */
528   void switchServerSSLContext(
529     const std::shared_ptr<folly::SSLContext>& handshakeCtx);
530
531   /**
532    * Did server recognize/support the tlsext_hostname in Client Hello?
533    * It can only be used in client mode.
534    *
535    * @return true - tlsext_hostname is matched by the server
536    *         false - tlsext_hostname is not matched or
537    *                 is not supported by server
538    */
539   bool isServerNameMatch() const;
540
541   /**
542    * Set the SNI hostname that we'll advertise to the server in the
543    * ClientHello message.
544    */
545   void setServerName(std::string serverName) noexcept;
546 #endif // FOLLY_OPENSSL_HAS_SNI
547
548   void timeoutExpired() noexcept;
549
550   /**
551    * Get the list of supported ciphers sent by the client in the client's
552    * preference order.
553    */
554   void getSSLClientCiphers(
555       std::string& clientCiphers,
556       bool convertToString = true) const;
557
558   /**
559    * Get the list of compression methods sent by the client in TLS Hello.
560    */
561   std::string getSSLClientComprMethods() const;
562
563   /**
564    * Get the list of TLS extensions sent by the client in the TLS Hello.
565    */
566   std::string getSSLClientExts() const;
567
568   std::string getSSLClientSigAlgs() const;
569
570   /**
571    * Get the list of versions in the supported versions extension (used to
572    * negotiate TLS 1.3).
573    */
574   std::string getSSLClientSupportedVersions() const;
575
576   std::string getSSLAlertsReceived() const;
577
578   /**
579    * Get the list of shared ciphers between the server and the client.
580    * Works well for only SSLv2, not so good for SSLv3 or TLSv1.
581    */
582   void getSSLSharedCiphers(std::string& sharedCiphers) const;
583
584   /**
585    * Get the list of ciphers supported by the server in the server's
586    * preference order.
587    */
588   void getSSLServerCiphers(std::string& serverCiphers) const;
589
590   static int getSSLExDataIndex();
591   static AsyncSSLSocket* getFromSSL(const SSL *ssl);
592   static int bioWrite(BIO* b, const char* in, int inl);
593   static int bioRead(BIO* b, char* out, int outl);
594   void resetClientHelloParsing(SSL *ssl);
595   static void clientHelloParsingCallback(int write_p, int version,
596       int content_type, const void *buf, size_t len, SSL *ssl, void *arg);
597   static const char* getSSLServerNameFromSSL(SSL* ssl);
598
599   // For unit-tests
600   ssl::ClientHelloInfo* getClientHelloInfo() const {
601     return clientHelloInfo_.get();
602   }
603
604   /**
605    * Returns the time taken to complete a handshake.
606    */
607   virtual std::chrono::nanoseconds getHandshakeTime() const {
608     return handshakeEndTime_ - handshakeStartTime_;
609   }
610
611   void setMinWriteSize(size_t minWriteSize) {
612     minWriteSize_ = minWriteSize;
613   }
614
615   size_t getMinWriteSize() const {
616     return minWriteSize_;
617   }
618
619   void setReadCB(ReadCallback* callback) override;
620
621   /**
622    * Tries to enable the buffer movable experimental feature in openssl.
623    * This is not guaranteed to succeed in case openssl does not have
624    * the experimental feature built in.
625    */
626   void setBufferMovableEnabled(bool enabled);
627
628   /**
629    * Returns the peer certificate, or nullptr if no peer certificate received.
630    */
631   virtual ssl::X509UniquePtr getPeerCert() const override {
632     if (!ssl_) {
633       return nullptr;
634     }
635
636     X509* cert = SSL_get_peer_certificate(ssl_);
637     return ssl::X509UniquePtr(cert);
638   }
639
640   /**
641    * Force AsyncSSLSocket object to cache local and peer socket addresses.
642    * If called with "true" before connect() this function forces full local
643    * and remote socket addresses to be cached in the socket object and available
644    * through getLocalAddress()/getPeerAddress() methods even after the socket is
645    * closed.
646    */
647   void forceCacheAddrOnFailure(bool force) { cacheAddrOnFailure_ = force; }
648
649   const std::string& getServiceIdentity() const { return serviceIdentity_; }
650
651   void setServiceIdentity(std::string serviceIdentity) {
652     serviceIdentity_ = std::move(serviceIdentity);
653   }
654
655   void setCertCacheHit(bool hit) {
656     certCacheHit_ = hit;
657   }
658
659   bool getCertCacheHit() const {
660     return certCacheHit_;
661   }
662
663   bool sessionResumptionAttempted() const {
664     return sessionResumptionAttempted_;
665   }
666
667   /**
668    * Clears the ERR stack before invoking SSL methods.
669    * This is useful if unrelated code that runs in the same thread
670    * does not properly handle SSL error conditions, in which case
671    * it could cause SSL_* methods to fail with incorrect error codes.
672    */
673   void setClearOpenSSLErrors(bool clearErr) {
674     clearOpenSSLErrors_ = clearErr;
675   }
676
677  private:
678
679   void init();
680   void clearOpenSSLErrors();
681
682  protected:
683
684   /**
685    * Protected destructor.
686    *
687    * Users of AsyncSSLSocket must never delete it directly.  Instead, invoke
688    * destroy() instead.  (See the documentation in DelayedDestruction.h for
689    * more details.)
690    */
691   ~AsyncSSLSocket();
692
693   // Inherit event notification methods from AsyncSocket except
694   // the following.
695   void prepareReadBuffer(void** buf, size_t* buflen) override;
696   void handleRead() noexcept override;
697   void handleWrite() noexcept override;
698   void handleAccept() noexcept;
699   void handleConnect() noexcept override;
700
701   void invalidState(HandshakeCB* callback);
702   bool willBlock(int ret,
703                  int* sslErrorOut,
704                  unsigned long* errErrorOut) noexcept;
705
706   virtual void checkForImmediateRead() noexcept override;
707   // AsyncSocket calls this at the wrong time for SSL
708   void handleInitialReadWrite() noexcept override {}
709
710   WriteResult interpretSSLError(int rc, int error);
711   ReadResult performRead(void** buf, size_t* buflen, size_t* offset) override;
712   WriteResult performWrite(
713       const iovec* vec,
714       uint32_t count,
715       WriteFlags flags,
716       uint32_t* countWritten,
717       uint32_t* partialWritten) override;
718
719   ssize_t performWriteIovec(const iovec* vec, uint32_t count,
720                             WriteFlags flags, uint32_t* countWritten,
721                             uint32_t* partialWritten);
722
723   // This virtual wrapper around SSL_write exists solely for testing/mockability
724   virtual int sslWriteImpl(SSL *ssl, const void *buf, int n) {
725     clearOpenSSLErrors();
726     return SSL_write(ssl, buf, n);
727   }
728
729   /**
730    * Apply verification options passed to sslConn/sslAccept or those set
731    * in the underlying SSLContext object.
732    *
733    * @param ssl pointer to the SSL object on which verification options will be
734    * applied. If verifyPeer_ was explicitly set either via sslConn/sslAccept,
735    * those options override the settings in the underlying SSLContext.
736    */
737   void applyVerificationOptions(SSL * ssl);
738
739   /**
740    * Sets up SSL with a custom write bio which intercepts all writes.
741    *
742    * @return true, if succeeds and false if there is an error creating the bio.
743    */
744   bool setupSSLBio();
745
746   /**
747    * A SSL_write wrapper that understand EOR
748    *
749    * @param ssl: SSL* object
750    * @param buf: Buffer to be written
751    * @param n:   Number of bytes to be written
752    * @param eor: Does the last byte (buf[n-1]) have the app-last-byte?
753    * @return:    The number of app bytes successfully written to the socket
754    */
755   int eorAwareSSLWrite(SSL *ssl, const void *buf, int n, bool eor);
756
757   // Inherit error handling methods from AsyncSocket, plus the following.
758   void failHandshake(const char* fn, const AsyncSocketException& ex);
759
760   void invokeHandshakeErr(const AsyncSocketException& ex);
761   void invokeHandshakeCB();
762
763   void invokeConnectErr(const AsyncSocketException& ex) override;
764   void invokeConnectSuccess() override;
765   void scheduleConnectTimeout() override;
766
767   void cacheLocalPeerAddr();
768
769   void startSSLConnect();
770
771   static void sslInfoCallback(const SSL *ssl, int type, int val);
772
773   // Whether the current write to the socket should use MSG_MORE.
774   bool corkCurrentWrite_{false};
775   // SSL related members.
776   bool server_{false};
777   // Used to prevent client-initiated renegotiation.  Note that AsyncSSLSocket
778   // doesn't fully support renegotiation, so we could just fail all attempts
779   // to enforce this.  Once it is supported, we should make it an option
780   // to disable client-initiated renegotiation.
781   bool handshakeComplete_{false};
782   bool renegotiateAttempted_{false};
783   SSLStateEnum sslState_{STATE_UNINIT};
784   std::shared_ptr<folly::SSLContext> ctx_;
785   // Callback for SSL_accept() or SSL_connect()
786   HandshakeCB* handshakeCallback_{nullptr};
787   SSL* ssl_{nullptr};
788   SSL_SESSION *sslSession_{nullptr};
789   HandshakeTimeout handshakeTimeout_;
790   ConnectionTimeout connectionTimeout_;
791   // whether the SSL session was resumed using session ID or not
792   bool sessionIDResumed_{false};
793
794   // The app byte num that we are tracking for the MSG_EOR
795   // Only one app EOR byte can be tracked.
796   size_t appEorByteNo_{0};
797
798   // Try to avoid calling SSL_write() for buffers smaller than this.
799   // It doesn't take effect when it is 0.
800   size_t minWriteSize_{1500};
801
802   // When openssl is about to sendmsg() across the minEorRawBytesNo_,
803   // it will pass MSG_EOR to sendmsg().
804   size_t minEorRawByteNo_{0};
805 #if FOLLY_OPENSSL_HAS_SNI
806   std::shared_ptr<folly::SSLContext> handshakeCtx_;
807   std::string tlsextHostname_;
808 #endif
809
810   // a service identity that this socket/connection is associated with
811   std::string serviceIdentity_;
812
813   folly::SSLContext::SSLVerifyPeerEnum
814     verifyPeer_{folly::SSLContext::SSLVerifyPeerEnum::USE_CTX};
815
816   // Callback for SSL_CTX_set_verify()
817   static int sslVerifyCallback(int preverifyOk, X509_STORE_CTX* ctx);
818
819   bool parseClientHello_{false};
820   bool cacheAddrOnFailure_{false};
821   bool bufferMovableEnabled_{false};
822   bool certCacheHit_{false};
823   std::unique_ptr<ssl::ClientHelloInfo> clientHelloInfo_;
824   std::vector<std::pair<char, StringPiece>> alertsReceived_;
825
826   // Time taken to complete the ssl handshake.
827   std::chrono::steady_clock::time_point handshakeStartTime_;
828   std::chrono::steady_clock::time_point handshakeEndTime_;
829   std::chrono::milliseconds handshakeConnectTimeout_{0};
830   bool sessionResumptionAttempted_{false};
831
832   std::unique_ptr<IOBuf> preReceivedData_;
833   // Whether or not to clear the err stack before invocation of another
834   // SSL method
835   bool clearOpenSSLErrors_{false};
836 };
837
838 } // namespace