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 #include "remoting/host/pin_hash.h" | 5 #include "remoting/host/pin_hash.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "remoting/protocol/authentication_method.h" | 9 #include "remoting/protocol/authentication_method.h" |
10 #include "remoting/protocol/me2me_host_authenticator_factory.h" | 10 #include "remoting/protocol/me2me_host_authenticator_factory.h" |
11 | 11 |
12 namespace remoting { | 12 namespace remoting { |
13 | 13 |
| 14 bool ParsePinHashFromConfig(const std::string& value, |
| 15 const std::string& host_id, |
| 16 std::string* pin_hash_out) { |
| 17 size_t separator = value.find(':'); |
| 18 if (separator == std::string::npos) |
| 19 return false; |
| 20 |
| 21 if (!base::Base64Decode(value.substr(separator + 1), pin_hash_out)) |
| 22 return false; |
| 23 |
| 24 std::string function_name = value.substr(0, separator); |
| 25 if (function_name == "plain") { |
| 26 *pin_hash_out = protocol::ApplySharedSecretHashFunction( |
| 27 protocol::HashFunction::HMAC_SHA256, host_id, *pin_hash_out); |
| 28 return true; |
| 29 } else if (function_name == "hmac") { |
| 30 return true; |
| 31 } |
| 32 |
| 33 pin_hash_out->clear(); |
| 34 return false; |
| 35 } |
| 36 |
14 std::string MakeHostPinHash(const std::string& host_id, | 37 std::string MakeHostPinHash(const std::string& host_id, |
15 const std::string& pin) { | 38 const std::string& pin) { |
16 std::string hash = protocol::AuthenticationMethod::ApplyHashFunction( | 39 std::string hash = protocol::ApplySharedSecretHashFunction( |
17 protocol::AuthenticationMethod::HMAC_SHA256, host_id, pin); | 40 protocol::HashFunction::HMAC_SHA256, host_id, pin); |
18 std::string hash_base64; | 41 std::string hash_base64; |
19 base::Base64Encode(hash, &hash_base64); | 42 base::Base64Encode(hash, &hash_base64); |
20 return "hmac:" + hash_base64; | 43 return "hmac:" + hash_base64; |
21 } | 44 } |
22 | 45 |
23 bool VerifyHostPinHash(const std::string& hash, | 46 bool VerifyHostPinHash(const std::string& hash, |
24 const std::string& host_id, | 47 const std::string& host_id, |
25 const std::string& pin) { | 48 const std::string& pin) { |
26 remoting::protocol::SharedSecretHash hash_parsed; | 49 std::string hash_parsed; |
27 if (!hash_parsed.Parse(hash)) { | 50 if (!ParsePinHashFromConfig(hash, host_id, &hash_parsed)) { |
28 LOG(FATAL) << "Invalid hash."; | 51 LOG(FATAL) << "Failed to parse PIN hash."; |
29 return false; | 52 return false; |
30 } | 53 } |
31 std::string hash_calculated = | 54 std::string hash_calculated = protocol::ApplySharedSecretHashFunction( |
32 remoting::protocol::AuthenticationMethod::ApplyHashFunction( | 55 protocol::HashFunction::HMAC_SHA256, host_id, pin); |
33 hash_parsed.hash_function, host_id, pin); | 56 return hash_calculated == hash_parsed; |
34 return hash_calculated == hash_parsed.value; | |
35 } | 57 } |
36 | 58 |
37 } // namespace remoting | 59 } // namespace remoting |
OLD | NEW |