pin_hash.cc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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
  3. // found in the LICENSE file.
  4. #include "remoting/host/pin_hash.h"
  5. #include "base/base64.h"
  6. #include "base/logging.h"
  7. #include "remoting/protocol/auth_util.h"
  8. #include "remoting/protocol/me2me_host_authenticator_factory.h"
  9. namespace remoting {
  10. bool ParsePinHashFromConfig(const std::string& value,
  11. const std::string& host_id,
  12. std::string* pin_hash_out) {
  13. size_t separator = value.find(':');
  14. if (separator == std::string::npos)
  15. return false;
  16. if (!base::Base64Decode(value.substr(separator + 1), pin_hash_out))
  17. return false;
  18. std::string function_name = value.substr(0, separator);
  19. if (function_name == "plain") {
  20. *pin_hash_out = protocol::GetSharedSecretHash(host_id, *pin_hash_out);
  21. return true;
  22. } else if (function_name == "hmac") {
  23. return true;
  24. }
  25. pin_hash_out->clear();
  26. return false;
  27. }
  28. std::string MakeHostPinHash(const std::string& host_id,
  29. const std::string& pin) {
  30. std::string hash = protocol::GetSharedSecretHash(host_id, pin);
  31. std::string hash_base64;
  32. base::Base64Encode(hash, &hash_base64);
  33. return "hmac:" + hash_base64;
  34. }
  35. bool VerifyHostPinHash(const std::string& hash,
  36. const std::string& host_id,
  37. const std::string& pin) {
  38. std::string hash_parsed;
  39. if (!ParsePinHashFromConfig(hash, host_id, &hash_parsed)) {
  40. LOG(FATAL) << "Failed to parse PIN hash.";
  41. return false;
  42. }
  43. std::string hash_calculated = protocol::GetSharedSecretHash(host_id, pin);
  44. return hash_calculated == hash_parsed;
  45. }
  46. } // namespace remoting