OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "remoting/protocol/pairing_client_authenticator.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "remoting/base/constants.h" | |
10 #include "remoting/base/rsa_key_pair.h" | |
11 #include "remoting/protocol/authentication_method.h" | |
12 #include "remoting/protocol/channel_authenticator.h" | |
13 #include "remoting/protocol/v2_authenticator.h" | |
14 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" | |
15 | |
16 namespace remoting { | |
17 namespace protocol { | |
18 | |
19 namespace { | |
20 // These definitions must be kept in sync with pairing_host_authenticator.cc. | |
21 const buzz::StaticQName kPairingInfoTag = | |
22 { kChromotingXmlNamespace, "pairing-info" }; | |
23 const buzz::StaticQName kClientIdAttribute = | |
24 { "", "client-id" }; | |
25 const buzz::StaticQName kPairingFailedTag = | |
26 { kChromotingXmlNamespace, "pairing-failed" }; | |
27 const buzz::StaticQName kPairingErrorAttribute = | |
28 { "", "error" }; | |
29 } | |
30 | |
31 PairingClientAuthenticator::PairingClientAuthenticator( | |
32 const std::string& client_id, | |
33 const std::string& paired_secret, | |
34 const FetchSecretCallback& fetch_pin_callback, | |
35 const std::string& authentication_tag) | |
36 : pairing_state_(MESSAGE_READY), | |
37 client_id_(client_id), | |
38 paired_secret_(paired_secret), | |
39 fetch_pin_callback_(fetch_pin_callback), | |
40 authentication_tag_(authentication_tag), | |
41 pairing_failed_(false), | |
42 weak_factory_(this) { | |
43 } | |
44 | |
45 Authenticator::State PairingClientAuthenticator::state() const { | |
46 if (pairing_state_ == ACCEPTED || pairing_state_ == REJECTED) { | |
47 DCHECK(v2_authenticator_); | |
48 return v2_authenticator_->state(); | |
49 } else { | |
50 return pairing_state_; | |
51 } | |
52 } | |
53 | |
54 Authenticator::RejectionReason | |
55 PairingClientAuthenticator::rejection_reason() const { | |
56 DCHECK(v2_authenticator_); | |
57 return v2_authenticator_->rejection_reason(); | |
58 } | |
59 | |
60 void PairingClientAuthenticator::ProcessMessage( | |
61 const buzz::XmlElement* message, | |
62 const base::Closure& resume_callback) { | |
63 DCHECK_EQ(state(), WAITING_MESSAGE); | |
64 | |
65 if (v2_authenticator_) { | |
rmsousa
2013/05/16 20:11:02
This isn't really necessary, the else case below a
Jamie
2013/05/16 21:09:10
I realized the same thing when I was implementing
| |
66 v2_authenticator_->ProcessMessage(message, resume_callback); | |
67 return; | |
68 } | |
69 | |
70 const buzz::XmlElement* pairing_failed_tag = | |
71 message->FirstNamed(kPairingFailedTag); | |
72 | |
73 if (pairing_failed_tag) { | |
74 // If pairing failed, prompt the user for the PIN and try again. | |
75 std::string error = pairing_failed_tag->Attr(kPairingErrorAttribute); | |
76 LOG(INFO) << "Pairing failed: " << error; | |
77 pairing_state_ = PROCESSING_MESSAGE; | |
78 SecretFetchedCallback callback = base::Bind( | |
79 &PairingClientAuthenticator::CreateV2AuthenticatorWithPIN, | |
80 weak_factory_.GetWeakPtr(), message, resume_callback); | |
rmsousa
2013/05/16 20:11:02
Strange, I think passing a naked pointer here shou
Jamie
2013/05/16 21:09:10
Done.
| |
81 fetch_pin_callback_.Run(callback); | |
82 return; | |
83 | |
84 } else { | |
85 // If it's not a pairing error message, create the V2 authenticator | |
86 // backed by the paired secret if it doesn't already exist, and let | |
87 // it process the message. | |
88 if (!v2_authenticator_) { | |
89 pairing_state_ = ACCEPTED; | |
90 v2_authenticator_ = V2Authenticator::CreateForClient( | |
91 paired_secret_, WAITING_MESSAGE); | |
92 } | |
93 v2_authenticator_->ProcessMessage(message, resume_callback); | |
94 } | |
95 } | |
96 | |
97 scoped_ptr<buzz::XmlElement> PairingClientAuthenticator::GetNextMessage() { | |
98 DCHECK_EQ(state(), MESSAGE_READY); | |
99 | |
100 // If the initial message has not yet been sent, return it now. | |
101 if (pairing_state_ == MESSAGE_READY) { | |
102 scoped_ptr<buzz::XmlElement> result = CreateEmptyAuthenticatorMessage(); | |
103 buzz::XmlElement* pairing_tag = new buzz::XmlElement(kPairingInfoTag); | |
104 pairing_tag->AddAttr(kClientIdAttribute, client_id_); | |
105 result->AddElement(pairing_tag); | |
106 pairing_state_ = WAITING_MESSAGE; | |
107 return result.Pass(); | |
108 } | |
109 | |
110 // In all other cases, defer to the underlying authenticator. | |
111 DCHECK(v2_authenticator_); | |
112 return v2_authenticator_->GetNextMessage(); | |
113 } | |
114 | |
115 scoped_ptr<ChannelAuthenticator> | |
116 PairingClientAuthenticator::CreateChannelAuthenticator() const { | |
117 return v2_authenticator_->CreateChannelAuthenticator(); | |
118 } | |
119 | |
120 void PairingClientAuthenticator::CreateV2AuthenticatorWithPIN( | |
121 const buzz::XmlElement* message, | |
122 const base::Closure& resume_callback, | |
123 const std::string& pin) { | |
124 pairing_state_ = REJECTED; | |
125 v2_authenticator_ = V2Authenticator::CreateForClient( | |
126 AuthenticationMethod::ApplyHashFunction( | |
127 AuthenticationMethod::HMAC_SHA256, | |
128 authentication_tag_, pin), | |
129 WAITING_MESSAGE); | |
130 ProcessMessage(message, resume_callback); | |
131 } | |
132 | |
133 } // namespace protocol | |
134 } // namespace remoting | |
OLD | NEW |