// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef REMOTING_PROTOCOL_JINGLE_SESSION_H_ #define REMOTING_PROTOCOL_JINGLE_SESSION_H_ #include "base/memory/ref_counted.h" #include "crypto/rsa_private_key.h" #include "net/base/completion_callback.h" #include "remoting/protocol/session.h" #include "third_party/libjingle/source/talk/base/sigslot.h" #include "third_party/libjingle/source/talk/p2p/base/session.h" namespace jingle_glue { class PseudoTcpAdapter; class StreamSocketAdapter; class TransportChannelSocketAdapter; } // namespace jingle_glue namespace net { class CertVerifier; class ClientSocketFactory; class Socket; class StreamSocket; class X509Certificate; } // namespace net namespace remoting { namespace protocol { class JingleSessionManager; class SocketWrapper; // Implements protocol::Session that work over libjingle session (the // cricket::Session object is passed to Init() method). Created // by JingleSessionManager for incoming and outgoing connections. class JingleSession : public protocol::Session, public sigslot::has_slots<> { public: static const char kChromotingContentName[]; // Chromotocol Session interface. virtual void SetStateChangeCallback(StateChangeCallback* callback); virtual net::Socket* control_channel(); virtual net::Socket* event_channel(); virtual net::Socket* video_channel(); virtual net::Socket* video_rtp_channel(); virtual net::Socket* video_rtcp_channel(); virtual const std::string& jid(); virtual MessageLoop* message_loop(); virtual const CandidateSessionConfig* candidate_config(); virtual const SessionConfig* config(); virtual void set_config(const SessionConfig* config); virtual const std::string& initiator_token(); virtual void set_initiator_token(const std::string& initiator_token); virtual const std::string& receiver_token(); virtual void set_receiver_token(const std::string& receiver_token); virtual void Close(Task* closed_task); private: friend class JingleSessionManager; // Create a JingleSession used in client mode. A server certificate is // required. static JingleSession* CreateClientSession(JingleSessionManager* manager, const std::string& host_public_key); // Create a JingleSession used in server mode. A server certificate and // private key is provided. |key| is copied in the constructor. // // TODO(sergeyu): Remove |certificate| and |key| when we stop using TLS. static JingleSession* CreateServerSession( JingleSessionManager* manager, scoped_refptr certificate, crypto::RSAPrivateKey* key); // TODO(sergeyu): Change type of |peer_public_key| to RSAPublicKey. JingleSession(JingleSessionManager* jingle_session_manager, scoped_refptr local_cert, crypto::RSAPrivateKey* local_private_key, const std::string& peer_public_key); virtual ~JingleSession(); // Called by JingleSessionManager. void set_candidate_config(const CandidateSessionConfig* candidate_config); scoped_refptr local_certificate() const; void Init(cricket::Session* cricket_session); std::string GetEncryptedMasterKey() const; // Close all the channels and terminate the session. void CloseInternal(int result, bool failed); bool HasSession(cricket::Session* cricket_session); cricket::Session* ReleaseSession(); // Initialize the session configuration from a received connection response // stanza. bool InitializeConfigFromDescription( const cricket::SessionDescription* description); // Configures channels and calls InitializeSSL(). void InitializeChannels(); // Initialize PseudoTCP + SSL on each of the video, control and input // channels. The channels must have been created before this is called. bool InitializeSSL(); // Helper method to create and initialize PseudoTCP + SSL socket on // top of the provided |channel|. The resultant SSL socket is // written to |ssl_socket|. Return true if successful. bool EstablishSSLConnection(net::Socket* channel, scoped_ptr* ssl_socket); // Used for Session.SignalState sigslot. void OnSessionState(cricket::BaseSession* session, cricket::BaseSession::State state); // Used for Session.SignalError sigslot. void OnSessionError(cricket::BaseSession* session, cricket::BaseSession::Error error); void OnInitiate(); void OnAccept(); void OnTerminate(); void OnConnect(int result); // Called by SSL socket to notify connect event. void OnSSLConnect(int result); void SetState(State new_state); // JingleSessionManager that created this session. scoped_refptr jingle_session_manager_; // Certificates used for connection. Currently only receiving side // has a certificate. scoped_refptr local_cert_; scoped_refptr remote_cert_; // Private key used in SSL server sockets. scoped_ptr local_private_key_; // Public key of the peer. std::string peer_public_key_; // Master key used to derive ice keys for each ice // session. Generated on the client and sent to the host in the // session-initiate message (encrypted with the host's key). std::string master_key_; State state_; scoped_ptr state_change_callback_; bool closed_; bool closing_; // JID of the other side. Set when the connection is initialized, // and never changed after that. std::string jid_; // The corresponding libjingle session. cricket::Session* cricket_session_; scoped_ptr config_; std::string initiator_token_; std::string receiver_token_; // These data members are only set on the receiving side. scoped_ptr candidate_config_; // |control_channel_| holds a channel until SSL socket is // created. After that |control_ssl_socket_| owns the channel. The // same is the case fo |event_channel_| and |video_channel_|. cricket::TransportChannel* raw_control_channel_; scoped_ptr control_channel_; scoped_ptr control_ssl_socket_; cricket::TransportChannel* raw_event_channel_; scoped_ptr event_channel_; scoped_ptr event_ssl_socket_; cricket::TransportChannel* raw_video_channel_; scoped_ptr video_channel_; scoped_ptr video_ssl_socket_; // Count the number of SSL connections esblished. int ssl_connections_; // Used to verify the certificate received in SSLClientSocket. scoped_ptr cert_verifier_; cricket::TransportChannel* raw_video_rtp_channel_; scoped_ptr video_rtp_channel_; cricket::TransportChannel* raw_video_rtcp_channel_; scoped_ptr video_rtcp_channel_; // Callback called by the SSL layer. net::CompletionCallbackImpl connect_callback_; net::CompletionCallbackImpl ssl_connect_callback_; DISALLOW_COPY_AND_ASSIGN(JingleSession); }; } // namespace protocol } // namespace remoting #endif // REMOTING_PROTOCOL_JINGLE_SESSION_H_