Line data Source code
1 : /* 2 : * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #ifndef SRC_IO_SSL_SESSION_H_ 6 : #define SRC_IO_SSL_SESSION_H_ 7 : 8 : #include <mutex> 9 : 10 : #include "io/tcp_session.h" 11 : #include "io/ssl_server.h" 12 : 13 : class SslSession; 14 : typedef boost::intrusive_ptr<SslSession> SslSessionPtr; 15 : typedef boost::function<void(SslSessionPtr, 16 : const boost::system::error_code& error)> SslHandShakeCallbackHandler; 17 : 18 : class SslSession : public TcpSession { 19 : public: 20 : typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> SslSocket; 21 : 22 : // SslSession constructor takes ownership of socket. 23 : SslSession(SslServer *server, SslSocket *socket, 24 : bool async_read_ready = true); 25 : 26 : virtual Socket *socket() const; 27 : 28 : // Trigger delayed SslHandShake 29 : void TriggerSslHandShake(SslHandShakeCallbackHandler); 30 : 31 : // Additional states to determine the trigger of SSL handshake 32 1240706 : bool IsSslDisabled() { 33 1240706 : return (!ssl_enabled_); 34 : } 35 : 36 3185 : bool IsSslHandShakeDelayed() { 37 3185 : return ssl_handshake_delayed_; 38 : } 39 : 40 : bool IsSslHandShakeSuccess() { 41 : std::scoped_lock lock(mutex_); 42 : return ssl_handshake_success_; 43 : } 44 : 45 2934247 : bool IsSslHandShakeSuccessLocked() { 46 2934247 : return ssl_handshake_success_; 47 : } 48 : 49 892158 : bool IsSslHandShakeInProgress() { 50 892158 : std::scoped_lock lock(mutex_); 51 892158 : return ssl_handshake_in_progress_; 52 892157 : } 53 : 54 2912 : void SetSslHandShakeInProgress(bool state) { 55 2912 : std::scoped_lock lock(mutex_); 56 2912 : ssl_handshake_in_progress_ = state; 57 2912 : } 58 : 59 : static bool IsSocketErrorHard(const boost::system::error_code &ec); 60 : protected: 61 : virtual ~SslSession(); 62 : 63 : private: 64 : class SslReader; 65 : friend class SslServer; 66 : 67 : // SslSession do actual ssl socket read for data in this context with 68 : // session mutex held, to avoid concurrent read and write operations 69 : // on same socket. 70 : size_t ReadSome(boost::asio::mutable_buffer buffer, 71 : boost::system::error_code *error); 72 : void AsyncWrite(const uint8_t *data, std::size_t size); 73 : 74 : static void TriggerSslHandShakeInternal(SslSessionPtr ptr, 75 : SslHandShakeCallbackHandler cb); 76 : 77 : virtual Task* CreateReaderTask(boost::asio::mutable_buffer, size_t); 78 : 79 : static void SslHandShakeCallback(SslHandShakeCallbackHandler cb, 80 : SslSessionPtr, const boost::system::error_code &error); 81 : 82 2919 : void SetSslHandShakeSuccess() { 83 2919 : std::scoped_lock lock(mutex_); 84 2919 : ssl_handshake_success_ = true; 85 2919 : } 86 : 87 25 : void SetSslHandShakeFailure() { 88 25 : std::scoped_lock lock(mutex_); 89 25 : ssl_handshake_success_ = false; 90 25 : } 91 : virtual size_t GetReadBufferSize() const; 92 : virtual void AsyncReadSome(); 93 : 94 : boost::scoped_ptr<SslSocket> ssl_socket_; 95 : 96 : /**************** protected by mutex_ *************************/ 97 : bool ssl_handshake_in_progress_; // ssl handshake ongoing 98 : bool ssl_handshake_success_; // ssl handshake success 99 : /**************** end protected by mutex_ *********************/ 100 : 101 : /**************** config knobs ********************************/ 102 : bool ssl_enabled_; // default true 103 : bool ssl_handshake_delayed_; // default false 104 : /**************************************************************/ 105 : 106 : size_t ssl_last_read_len_; // data len of the last read done 107 : 108 : DISALLOW_COPY_AND_ASSIGN(SslSession); 109 : }; 110 : 111 : #endif // SRC_IO_SSL_SESSION_H_