OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef REMOTING_PROTOCOL_NEGOTIATING_AUTHENTICATOR_H_ | 5 #ifndef REMOTING_PROTOCOL_NEGOTIATING_AUTHENTICATOR_H_ |
6 #define REMOTING_PROTOCOL_NEGOTIATING_AUTHENTICATOR_H_ | 6 #define REMOTING_PROTOCOL_NEGOTIATING_AUTHENTICATOR_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" |
14 #include "remoting/protocol/authenticator.h" | 15 #include "remoting/protocol/authenticator.h" |
15 #include "remoting/protocol/authentication_method.h" | 16 #include "remoting/protocol/authentication_method.h" |
16 | 17 |
17 namespace remoting { | 18 namespace remoting { |
18 | 19 |
19 class RsaKeyPair; | 20 class RsaKeyPair; |
20 | 21 |
21 namespace protocol { | 22 namespace protocol { |
22 | 23 |
| 24 typedef base::Callback<void(const std::string& secret)> SecretFetchedCallback; |
| 25 typedef base::Callback<void( |
| 26 const SecretFetchedCallback& secret_fetched_callback)> FetchSecretCallback; |
| 27 |
| 28 // This class provides a meta-authenticator that allows clients and hosts that |
| 29 // support multiple authentication methods to negotiate a method to use. |
| 30 // |
| 31 // The typical flow is: |
| 32 // * Client sends a message to host with its supported methods. |
| 33 // (clients may additionally pick a method and send its first message). |
| 34 // * Host picks a method and sends its first message (if any). |
| 35 // (if a message for that method was sent by the client, it is processed). |
| 36 // * Client creates the authenticator selected by the host. If the method |
| 37 // starts with a message from the host, it is processed. |
| 38 // * Client and host exchange messages until the authentication is ACCEPTED or |
| 39 // REJECTED. |
| 40 // |
| 41 // The details: |
| 42 // * CreateAuthenticator() may be asynchronous (i.e. require user interaction |
| 43 // to determine initial parameters, like PIN). This happens inside |
| 44 // ProcessMessage, so to the outside this behaves like any asynchronous |
| 45 // message processing. Internally, CreateAuthenticator() receives a |
| 46 // callback, that will resume the authentication once the authenticator is |
| 47 // created. If there is already a message to be processed by the new |
| 48 // authenticator, this callback includes a call to the underlying |
| 49 // ProcessMessage(). |
| 50 // * Some authentication methods may have a specific starting direction (e.g. |
| 51 // host always sends the first message), while others are versatile (e.g. |
| 52 // SPAKE, where either side can send the first message). When an |
| 53 // authenticator is created, it is given a preferred initial state, which |
| 54 // the authenticator may ignore. |
| 55 // * If the new authenticator state doesn't match the preferred one, |
| 56 // the NegotiatingAuthenticator deals with that, by sending an empty |
| 57 // <authenticator> stanza if the method has no message to send, and |
| 58 // ignoring such empty messages on the receiving end. |
| 59 // * The client may optimistically pick a method on its first message (assuming |
| 60 // it doesn't require user interaction to start). If the host doesn't |
| 61 // support that method, it will just discard that message, and choose |
| 62 // another method from the client's supported methods list. |
| 63 // * The host never sends its own supported methods back to the client, so once |
| 64 // the host picks a method from the client's list, it's final. |
| 65 // * Any change in this class must maintain compatibility between any version |
| 66 // mix of webapp, client plugin and host, for both Me2Me and IT2Me. |
23 class NegotiatingAuthenticator : public Authenticator { | 67 class NegotiatingAuthenticator : public Authenticator { |
24 public: | 68 public: |
25 virtual ~NegotiatingAuthenticator(); | 69 virtual ~NegotiatingAuthenticator(); |
26 | 70 |
27 static bool IsNegotiableMessage(const buzz::XmlElement* message); | |
28 | |
29 // Creates a client authenticator for the given methods. | 71 // Creates a client authenticator for the given methods. |
30 static scoped_ptr<Authenticator> CreateForClient( | 72 static scoped_ptr<Authenticator> CreateForClient( |
31 const std::string& authentication_tag, | 73 const std::string& authentication_tag, |
32 const std::string& shared_secret, | 74 const FetchSecretCallback& fetch_secret_callback, |
33 const std::vector<AuthenticationMethod>& methods); | 75 const std::vector<AuthenticationMethod>& methods); |
34 | 76 |
35 // Creates a host authenticator, using a fixed shared secret/PIN hash. | 77 // Creates a host authenticator, using a fixed shared secret/PIN hash. |
36 static scoped_ptr<Authenticator> CreateForHost( | 78 static scoped_ptr<Authenticator> CreateForHost( |
37 const std::string& local_cert, | 79 const std::string& local_cert, |
38 scoped_refptr<RsaKeyPair> key_pair, | 80 scoped_refptr<RsaKeyPair> key_pair, |
39 const std::string& shared_secret_hash, | 81 const std::string& shared_secret_hash, |
40 AuthenticationMethod::HashFunction hash_function); | 82 AuthenticationMethod::HashFunction hash_function); |
41 | 83 |
42 // Authenticator interface. | 84 // Authenticator interface. |
43 virtual State state() const OVERRIDE; | 85 virtual State state() const OVERRIDE; |
44 virtual RejectionReason rejection_reason() const OVERRIDE; | 86 virtual RejectionReason rejection_reason() const OVERRIDE; |
45 virtual void ProcessMessage(const buzz::XmlElement* message, | 87 virtual void ProcessMessage(const buzz::XmlElement* message, |
46 const base::Closure& resume_callback) OVERRIDE; | 88 const base::Closure& resume_callback) OVERRIDE; |
47 virtual scoped_ptr<buzz::XmlElement> GetNextMessage() OVERRIDE; | 89 virtual scoped_ptr<buzz::XmlElement> GetNextMessage() OVERRIDE; |
48 virtual scoped_ptr<ChannelAuthenticator> | 90 virtual scoped_ptr<ChannelAuthenticator> |
49 CreateChannelAuthenticator() const OVERRIDE; | 91 CreateChannelAuthenticator() const OVERRIDE; |
50 | 92 |
51 private: | 93 private: |
52 NegotiatingAuthenticator(Authenticator::State initial_state); | 94 explicit NegotiatingAuthenticator(Authenticator::State initial_state); |
53 | 95 |
54 void AddMethod(const AuthenticationMethod& method); | 96 void AddMethod(const AuthenticationMethod& method); |
55 void CreateAuthenticator(State initial_state); | 97 |
| 98 // (Asynchronously) creates an authenticator, and stores it in |
| 99 // |current_authenticator_|. Authenticators that can be started in either |
| 100 // state will be created in |preferred_initial_state|. |
| 101 // |resume_callback| is called after |current_authenticator_| is set. |
| 102 void CreateAuthenticator(Authenticator::State preferred_initial_state, |
| 103 const base::Closure& resume_callback); |
| 104 |
| 105 // Calls |current_authenticator_| to process |message|, passing the supplied |
| 106 // |resume_callback|. |
| 107 void ProcessMessageInternal(const buzz::XmlElement* message, |
| 108 const base::Closure& resume_callback); |
| 109 |
| 110 // Updates |state_| to reflect the current underlying authenticator state. |
| 111 // |resume_callback| is called after the state is updated. |
56 void UpdateState(const base::Closure& resume_callback); | 112 void UpdateState(const base::Closure& resume_callback); |
57 | 113 |
| 114 // Creates a V2Authenticator in state |initial_state| with the given |
| 115 // |shared_secret|, then runs |resume_callback|. |
| 116 void CreateV2AuthenticatorWithSecret( |
| 117 Authenticator::State initial_state, |
| 118 const base::Closure& resume_callback, |
| 119 const std::string& shared_secret); |
| 120 |
58 bool is_host_side() const; | 121 bool is_host_side() const; |
59 | 122 |
60 // Used only for host authenticators. | 123 // Used only for host authenticators. |
61 std::string local_cert_; | 124 std::string local_cert_; |
62 scoped_refptr<RsaKeyPair> local_key_pair_; | 125 scoped_refptr<RsaKeyPair> local_key_pair_; |
63 std::string shared_secret_hash_; | 126 std::string shared_secret_hash_; |
64 | 127 |
65 // Used only for client authenticators. | 128 // Used only for client authenticators. |
66 std::string authentication_tag_; | 129 std::string authentication_tag_; |
67 std::string shared_secret_; | 130 FetchSecretCallback fetch_secret_callback_; |
68 | 131 |
69 // Used for both host and client authenticators. | 132 // Used for both host and client authenticators. |
70 std::vector<AuthenticationMethod> methods_; | 133 std::vector<AuthenticationMethod> methods_; |
71 AuthenticationMethod current_method_; | 134 AuthenticationMethod current_method_; |
72 scoped_ptr<Authenticator> current_authenticator_; | 135 scoped_ptr<Authenticator> current_authenticator_; |
73 State state_; | 136 State state_; |
74 RejectionReason rejection_reason_; | 137 RejectionReason rejection_reason_; |
75 | 138 |
| 139 base::WeakPtrFactory<NegotiatingAuthenticator> weak_factory_; |
| 140 |
76 DISALLOW_COPY_AND_ASSIGN(NegotiatingAuthenticator); | 141 DISALLOW_COPY_AND_ASSIGN(NegotiatingAuthenticator); |
77 }; | 142 }; |
78 | 143 |
79 } // namespace protocol | 144 } // namespace protocol |
80 } // namespace remoting | 145 } // namespace remoting |
81 | 146 |
82 #endif // REMOTING_PROTOCOL_NEGOTIATING_AUTHENTICATOR_H_ | 147 #endif // REMOTING_PROTOCOL_NEGOTIATING_AUTHENTICATOR_H_ |
OLD | NEW |