auth_util.cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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/protocol/auth_util.h"
  5. #include "base/base64.h"
  6. #include "base/logging.h"
  7. #include "base/strings/string_util.h"
  8. #include "crypto/hmac.h"
  9. #include "crypto/sha2.h"
  10. #include "net/base/net_errors.h"
  11. #include "net/socket/ssl_socket.h"
  12. namespace remoting {
  13. namespace protocol {
  14. const char kClientAuthSslExporterLabel[] =
  15. "EXPORTER-remoting-channel-auth-client";
  16. const char kHostAuthSslExporterLabel[] =
  17. "EXPORTER-remoting-channel-auth-host";
  18. const char kSslFakeHostName[] = "chromoting";
  19. std::string GetSharedSecretHash(const std::string& tag,
  20. const std::string& shared_secret) {
  21. crypto::HMAC response(crypto::HMAC::SHA256);
  22. if (!response.Init(tag)) {
  23. LOG(FATAL) << "HMAC::Init failed";
  24. }
  25. unsigned char out_bytes[kSharedSecretHashLength];
  26. if (!response.Sign(shared_secret, out_bytes, sizeof(out_bytes))) {
  27. LOG(FATAL) << "HMAC::Sign failed";
  28. }
  29. return std::string(out_bytes, out_bytes + sizeof(out_bytes));
  30. }
  31. // static
  32. std::string GetAuthBytes(net::SSLSocket* socket,
  33. const base::StringPiece& label,
  34. const base::StringPiece& shared_secret) {
  35. // Get keying material from SSL.
  36. unsigned char key_material[kAuthDigestLength];
  37. int export_result = socket->ExportKeyingMaterial(
  38. label, false, "", key_material, kAuthDigestLength);
  39. if (export_result != net::OK) {
  40. LOG(ERROR) << "Error fetching keying material: " << export_result;
  41. return std::string();
  42. }
  43. // Generate auth digest based on the keying material and shared secret.
  44. crypto::HMAC response(crypto::HMAC::SHA256);
  45. if (!response.Init(key_material, kAuthDigestLength)) {
  46. NOTREACHED() << "HMAC::Init failed";
  47. return std::string();
  48. }
  49. unsigned char out_bytes[kAuthDigestLength];
  50. if (!response.Sign(shared_secret, out_bytes, kAuthDigestLength)) {
  51. NOTREACHED() << "HMAC::Sign failed";
  52. return std::string();
  53. }
  54. return std::string(out_bytes, out_bytes + kAuthDigestLength);
  55. }
  56. } // namespace protocol
  57. } // namespace remoting