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_host_authenticator.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "remoting/base/constants.h" |
| 9 #include "remoting/base/rsa_key_pair.h" |
| 10 #include "remoting/protocol/channel_authenticator.h" |
| 11 #include "remoting/protocol/pairing_registry.h" |
| 12 #include "remoting/protocol/v2_authenticator.h" |
| 13 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
| 14 |
| 15 namespace remoting { |
| 16 namespace protocol { |
| 17 |
| 18 namespace { |
| 19 // These definitions must be kept in sync with pairing_client_authenticator.cc. |
| 20 const buzz::StaticQName kPairingInfoTag = |
| 21 { kChromotingXmlNamespace, "pairing-info" }; |
| 22 const buzz::StaticQName kClientIdAttribute = |
| 23 { "", "client-id" }; |
| 24 const buzz::StaticQName kPairingFailedTag = |
| 25 { kChromotingXmlNamespace, "pairing-failed" }; |
| 26 const buzz::StaticQName kPairingErrorAttribute = |
| 27 { "", "error" }; |
| 28 } |
| 29 |
| 30 PairingHostAuthenticator::PairingHostAuthenticator( |
| 31 scoped_refptr<PairingRegistry> pairing_registry, |
| 32 const std::string& local_cert, |
| 33 scoped_refptr<RsaKeyPair> key_pair, |
| 34 const std::string& pin, |
| 35 State initial_state) |
| 36 : pairing_registry_(pairing_registry), |
| 37 local_cert_(local_cert), |
| 38 key_pair_(key_pair), |
| 39 pin_(pin), |
| 40 protocol_error_(false) { |
| 41 // If the client didn't specify an initial message, use the PIN as the shared |
| 42 // secret. If it did, the authenticator will be created in ProcessMessage with |
| 43 // the appropriate paired secret from the pairing registry. |
| 44 if (initial_state != WAITING_MESSAGE) { |
| 45 DCHECK_EQ(initial_state, MESSAGE_READY); |
| 46 CreateV2AuthenticatorWithPIN(); |
| 47 } |
| 48 } |
| 49 |
| 50 Authenticator::State PairingHostAuthenticator::state() const { |
| 51 if (protocol_error_) { |
| 52 return REJECTED; |
| 53 } else if (v2_authenticator_) { |
| 54 return v2_authenticator_->state(); |
| 55 } else { |
| 56 return WAITING_MESSAGE; |
| 57 } |
| 58 } |
| 59 |
| 60 Authenticator::RejectionReason |
| 61 PairingHostAuthenticator::rejection_reason() const { |
| 62 DCHECK(v2_authenticator_); |
| 63 return v2_authenticator_->rejection_reason(); |
| 64 } |
| 65 |
| 66 void PairingHostAuthenticator::ProcessMessage( |
| 67 const buzz::XmlElement* message, |
| 68 const base::Closure& resume_callback) { |
| 69 DCHECK_EQ(state(), WAITING_MESSAGE); |
| 70 |
| 71 // If there's already an underlying authenticator, defer to it. |
| 72 if (v2_authenticator_) { |
| 73 DCHECK_EQ(v2_authenticator_->state(), WAITING_MESSAGE); |
| 74 v2_authenticator_->ProcessMessage(message, resume_callback); |
| 75 return; |
| 76 } |
| 77 |
| 78 // If not, then create one based on the contents of the first message. |
| 79 std::string client_id; |
| 80 const buzz::XmlElement* pairing_tag = message->FirstNamed(kPairingInfoTag); |
| 81 if (pairing_tag) { |
| 82 client_id = pairing_tag->Attr(kClientIdAttribute); |
| 83 if (client_id.empty()) { |
| 84 LOG(ERROR) << "No client id specified."; |
| 85 protocol_error_ = true; |
| 86 resume_callback.Run(); |
| 87 return; |
| 88 } |
| 89 } |
| 90 |
| 91 std::string paired_secret = pairing_registry_->GetSecret(client_id); |
| 92 |
| 93 if (paired_secret.empty()) { |
| 94 LOG(INFO) << "Unknown client id"; |
| 95 error_message_ = "unknown-client-id"; |
| 96 CreateV2AuthenticatorWithPIN(); |
| 97 resume_callback.Run(); |
| 98 return; |
| 99 } |
| 100 |
| 101 v2_authenticator_ = V2Authenticator::CreateForHost( |
| 102 local_cert_, key_pair_, paired_secret, MESSAGE_READY); |
| 103 resume_callback.Run(); |
| 104 } |
| 105 |
| 106 scoped_ptr<buzz::XmlElement> PairingHostAuthenticator::GetNextMessage() { |
| 107 DCHECK_EQ(state(), MESSAGE_READY); |
| 108 |
| 109 DCHECK(v2_authenticator_); |
| 110 scoped_ptr<buzz::XmlElement> result = v2_authenticator_->GetNextMessage(); |
| 111 |
| 112 if (!error_message_.empty()) { |
| 113 buzz::XmlElement* pairing_failed_tag = |
| 114 new buzz::XmlElement(kPairingFailedTag); |
| 115 pairing_failed_tag->AddAttr(kPairingErrorAttribute, error_message_); |
| 116 result->AddElement(pairing_failed_tag); |
| 117 error_message_.clear(); |
| 118 } |
| 119 |
| 120 return result.Pass(); |
| 121 } |
| 122 |
| 123 scoped_ptr<ChannelAuthenticator> |
| 124 PairingHostAuthenticator::CreateChannelAuthenticator() const { |
| 125 DCHECK(v2_authenticator_); |
| 126 return v2_authenticator_->CreateChannelAuthenticator(); |
| 127 } |
| 128 |
| 129 void PairingHostAuthenticator::CreateV2AuthenticatorWithPIN() { |
| 130 DCHECK(!v2_authenticator_); |
| 131 v2_authenticator_ = V2Authenticator::CreateForHost( |
| 132 local_cert_, key_pair_, pin_, MESSAGE_READY); |
| 133 } |
| 134 |
| 135 } // namespace protocol |
| 136 } // namespace remoting |
OLD | NEW |