OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/v1_authenticator.h" |
| 6 |
| 7 #include "base/base64.h" |
| 8 #include "base/logging.h" |
| 9 #include "crypto/rsa_private_key.h" |
| 10 #include "remoting/base/constants.h" |
| 11 #include "remoting/protocol/auth_util.h" |
| 12 #include "remoting/protocol/v1_client_channel_authenticator.h" |
| 13 #include "remoting/protocol/v1_host_channel_authenticator.h" |
| 14 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" |
| 15 |
| 16 using buzz::QName; |
| 17 using buzz::XmlElement; |
| 18 |
| 19 namespace remoting { |
| 20 namespace protocol { |
| 21 |
| 22 namespace { |
| 23 const char kAuthenticationTag[] = "authentication"; |
| 24 const char kAuthTokenTag[] = "auth-token"; |
| 25 const char kCertificateTag[] = "certificate"; |
| 26 } // namespace |
| 27 |
| 28 V1ClientAuthenticator::V1ClientAuthenticator( |
| 29 const std::string& local_jid, |
| 30 const std::string& shared_secret) |
| 31 : local_jid_(local_jid), |
| 32 shared_secret_(shared_secret), |
| 33 state_(MESSAGE_READY) { |
| 34 } |
| 35 |
| 36 V1ClientAuthenticator::~V1ClientAuthenticator() { |
| 37 } |
| 38 |
| 39 Authenticator::State V1ClientAuthenticator::state() const { |
| 40 return state_; |
| 41 } |
| 42 |
| 43 void V1ClientAuthenticator::ProcessMessage(const XmlElement* message) { |
| 44 DCHECK_EQ(state_, WAITING_MESSAGE); |
| 45 |
| 46 // Parse the certificate. |
| 47 const XmlElement* cert_tag = |
| 48 message->FirstNamed(QName(kChromotingXmlNamespace, kCertificateTag)); |
| 49 if (cert_tag) { |
| 50 std::string base64_cert = cert_tag->BodyText(); |
| 51 if (!base::Base64Decode(base64_cert, &remote_cert_)) { |
| 52 LOG(ERROR) << "Failed to decode certificate received from the peer."; |
| 53 remote_cert_.clear(); |
| 54 } |
| 55 } |
| 56 |
| 57 if (remote_cert_.empty()) { |
| 58 state_ = REJECTED; |
| 59 } else { |
| 60 state_ = ACCEPTED; |
| 61 } |
| 62 } |
| 63 |
| 64 XmlElement* V1ClientAuthenticator::GetNextMessage() { |
| 65 DCHECK_EQ(state_, MESSAGE_READY); |
| 66 |
| 67 XmlElement* authentication_tag = new XmlElement( |
| 68 QName(kChromotingXmlNamespace, kAuthenticationTag)); |
| 69 |
| 70 std::string token = |
| 71 protocol::GenerateSupportAuthToken(local_jid_, shared_secret_); |
| 72 |
| 73 XmlElement* auth_token_tag = new XmlElement( |
| 74 QName(kChromotingXmlNamespace, kAuthTokenTag)); |
| 75 auth_token_tag->SetBodyText(token); |
| 76 authentication_tag->AddElement(auth_token_tag); |
| 77 |
| 78 state_ = WAITING_MESSAGE; |
| 79 return authentication_tag; |
| 80 } |
| 81 |
| 82 ChannelAuthenticator* |
| 83 V1ClientAuthenticator::CreateChannelAuthenticator() const { |
| 84 DCHECK_EQ(state_, ACCEPTED); |
| 85 return new V1ClientChannelAuthenticator(remote_cert_, shared_secret_); |
| 86 }; |
| 87 |
| 88 V1HostAuthenticator::V1HostAuthenticator( |
| 89 const std::string& local_cert, |
| 90 crypto::RSAPrivateKey* local_private_key, |
| 91 const std::string& shared_secret, |
| 92 const std::string& remote_jid) |
| 93 : local_cert_(local_cert), |
| 94 local_private_key_(local_private_key), |
| 95 shared_secret_(shared_secret), |
| 96 remote_jid_(remote_jid), |
| 97 state_(WAITING_MESSAGE) { |
| 98 } |
| 99 |
| 100 V1HostAuthenticator::~V1HostAuthenticator() { |
| 101 } |
| 102 |
| 103 Authenticator::State V1HostAuthenticator::state() const { |
| 104 return state_; |
| 105 } |
| 106 |
| 107 void V1HostAuthenticator::ProcessMessage(const XmlElement* message) { |
| 108 DCHECK_EQ(state_, WAITING_MESSAGE); |
| 109 |
| 110 std::string auth_token = |
| 111 message->TextNamed(buzz::QName(kChromotingXmlNamespace, kAuthTokenTag)); |
| 112 |
| 113 if (!protocol::VerifySupportAuthToken( |
| 114 remote_jid_, shared_secret_, auth_token)) { |
| 115 state_ = REJECTED; |
| 116 } else { |
| 117 state_ = MESSAGE_READY; |
| 118 } |
| 119 } |
| 120 |
| 121 XmlElement* V1HostAuthenticator::GetNextMessage() { |
| 122 DCHECK_EQ(state_, MESSAGE_READY); |
| 123 |
| 124 XmlElement* message = new XmlElement( |
| 125 QName(kChromotingXmlNamespace, kAuthenticationTag)); |
| 126 |
| 127 buzz::XmlElement* certificate_tag = new XmlElement( |
| 128 buzz::QName(kChromotingXmlNamespace, kCertificateTag)); |
| 129 std::string base64_cert; |
| 130 if (!base::Base64Encode(local_cert_, &base64_cert)) { |
| 131 LOG(DFATAL) << "Cannot perform base64 encode on certificate"; |
| 132 } |
| 133 certificate_tag->SetBodyText(base64_cert); |
| 134 message->AddElement(certificate_tag); |
| 135 |
| 136 state_ = ACCEPTED; |
| 137 return message; |
| 138 } |
| 139 |
| 140 ChannelAuthenticator* |
| 141 V1HostAuthenticator::CreateChannelAuthenticator() const { |
| 142 DCHECK_EQ(state_, ACCEPTED); |
| 143 return new V1HostChannelAuthenticator( |
| 144 local_cert_, local_private_key_, shared_secret_); |
| 145 }; |
| 146 |
| 147 V1HostAuthenticatorFactory::V1HostAuthenticatorFactory( |
| 148 const std::string& local_cert, |
| 149 crypto::RSAPrivateKey* local_private_key, |
| 150 const std::string& shared_secret) |
| 151 : local_cert_(local_cert), |
| 152 shared_secret_(shared_secret) { |
| 153 DCHECK(local_private_key); |
| 154 |
| 155 // TODO(hclam): Need a better way to clone a key. See crbug.com/105220 . |
| 156 std::vector<uint8> key_bytes; |
| 157 CHECK(local_private_key->ExportPrivateKey(&key_bytes)); |
| 158 local_private_key_.reset( |
| 159 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_bytes)); |
| 160 CHECK(local_private_key_.get()); |
| 161 } |
| 162 |
| 163 V1HostAuthenticatorFactory::~V1HostAuthenticatorFactory() { |
| 164 } |
| 165 |
| 166 Authenticator* V1HostAuthenticatorFactory::CreateAuthenticator( |
| 167 const std::string& remote_jid, |
| 168 const buzz::XmlElement* first_message) { |
| 169 return new V1HostAuthenticator(local_cert_, local_private_key_.get(), |
| 170 shared_secret_, remote_jid); |
| 171 } |
| 172 |
| 173 } // namespace remoting |
| 174 } // namespace protocol |
OLD | NEW |