Chromium Code Reviews| 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/simple_host_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/simple_host_channel_authenticator.h" | |
| 13 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" | |
| 14 | |
| 15 using buzz::QName; | |
| 16 using buzz::XmlElement; | |
| 17 | |
| 18 namespace remoting { | |
| 19 namespace protocol { | |
| 20 | |
| 21 namespace { | |
| 22 const char kAuthenticationTag[] = "authentication"; | |
| 23 const char kAuthTokenTag[] = "auth-token"; | |
| 24 const char kCertificateTag[] = "certificate"; | |
| 25 } // namespace | |
| 26 | |
| 27 SimpleHostAuthenticator::SimpleHostAuthenticator( | |
| 28 const std::string& local_cert, | |
| 29 crypto::RSAPrivateKey* local_private_key, | |
| 30 const std::string& shared_secret, | |
| 31 const std::string& remote_jid) | |
| 32 : local_cert_(local_cert), | |
| 33 local_private_key_(local_private_key), | |
| 34 shared_secret_(shared_secret), | |
| 35 remote_jid_(remote_jid), | |
| 36 state_(WAITING_MESSAGE) { | |
| 37 } | |
| 38 | |
| 39 SimpleHostAuthenticator::~SimpleHostAuthenticator() { | |
| 40 } | |
| 41 | |
| 42 Authenticator::State SimpleHostAuthenticator::state() const { | |
| 43 return state_; | |
| 44 } | |
| 45 | |
| 46 void SimpleHostAuthenticator::ProcessMessage(const XmlElement* message) { | |
| 47 DCHECK_EQ(state_, WAITING_MESSAGE); | |
| 48 | |
| 49 std::string auth_token = | |
| 50 message->TextNamed(buzz::QName(kChromotingXmlNamespace, kAuthTokenTag)); | |
| 51 | |
| 52 if (!protocol::VerifySupportAuthToken( | |
| 53 remote_jid_, shared_secret_, auth_token)) { | |
| 54 state_ = REJECTED; | |
| 55 } else { | |
| 56 state_ = MESSAGE_READY; | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 XmlElement* SimpleHostAuthenticator::GetNextMessage() { | |
| 61 DCHECK_EQ(state_, MESSAGE_READY); | |
| 62 | |
| 63 XmlElement* message = new XmlElement( | |
| 64 QName(kChromotingXmlNamespace, kAuthenticationTag)); | |
|
Wez
2011/11/22 22:58:05
nit: Create |message| lower down, where it's actua
Sergey Ulanov
2011/11/23 02:02:25
We create XML tree here, and I think it's easier t
| |
| 65 | |
| 66 buzz::XmlElement* certificate_tag = new XmlElement( | |
| 67 buzz::QName(kChromotingXmlNamespace, kCertificateTag)); | |
| 68 std::string base64_cert; | |
| 69 if (!base::Base64Encode(local_cert_, &base64_cert)) { | |
| 70 LOG(DFATAL) << "Cannot perform base64 encode on certificate"; | |
| 71 } | |
| 72 certificate_tag->SetBodyText(base64_cert); | |
| 73 message->AddElement(certificate_tag); | |
| 74 | |
| 75 state_ = ACCEPTED; | |
| 76 return message; | |
| 77 } | |
| 78 | |
| 79 ChannelAuthenticator* | |
| 80 SimpleHostAuthenticator::CreateChannelAuthenticator() const { | |
| 81 DCHECK_EQ(state_, ACCEPTED); | |
| 82 return new SimpleHostChannelAuthenticator( | |
| 83 local_cert_, local_private_key_, shared_secret_); | |
| 84 }; | |
| 85 | |
| 86 SimpleHostAuthenticatorFactory::SimpleHostAuthenticatorFactory( | |
| 87 const std::string& local_cert, | |
| 88 crypto::RSAPrivateKey* local_private_key, | |
| 89 const std::string& shared_secret) | |
| 90 : local_cert_(local_cert), | |
| 91 shared_secret_(shared_secret) { | |
| 92 DCHECK(local_private_key); | |
| 93 | |
| 94 // TODO(hclam): Need a better way to clone a key. | |
|
Wez
2011/11/22 22:58:05
nit: We should just create a bug for that, referen
Sergey Ulanov
2011/11/23 02:02:25
Opened crbug.com/105220
| |
| 95 std::vector<uint8> key_bytes; | |
| 96 CHECK(local_private_key->ExportPrivateKey(&key_bytes)); | |
| 97 local_private_key_.reset( | |
| 98 crypto::RSAPrivateKey::CreateFromPrivateKeyInfo(key_bytes)); | |
| 99 CHECK(local_private_key_.get()); | |
| 100 } | |
| 101 | |
| 102 SimpleHostAuthenticatorFactory::~SimpleHostAuthenticatorFactory() { | |
| 103 } | |
| 104 | |
| 105 Authenticator* SimpleHostAuthenticatorFactory::CreateAuthenticator( | |
| 106 const std::string& remote_jid, | |
| 107 const buzz::XmlElement* first_message) { | |
| 108 return new SimpleHostAuthenticator(local_cert_, local_private_key_.get(), | |
| 109 shared_secret_, remote_jid); | |
| 110 } | |
| 111 | |
| 112 } // namespace remoting | |
| 113 } // namespace protocol | |
| OLD | NEW |