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