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.
5 #include "remoting/protocol/authentication_method.h"
7 #include "base/base64.h"
8 #include "base/logging.h"
9 #include "crypto/hmac.h"
10 #include "remoting/protocol/auth_util.h"
16 AuthenticationMethod AuthenticationMethod::Invalid() {
17 return AuthenticationMethod();
21 AuthenticationMethod AuthenticationMethod::Spake2(HashFunction hash_function) {
22 return AuthenticationMethod(SPAKE2, hash_function);
26 AuthenticationMethod AuthenticationMethod::Spake2Pair() {
27 return AuthenticationMethod(SPAKE2_PAIR, HMAC_SHA256);
31 AuthenticationMethod AuthenticationMethod::ThirdParty() {
32 return AuthenticationMethod(THIRD_PARTY, NONE);
36 AuthenticationMethod AuthenticationMethod::FromString(
37 const std::string& value) {
38 if (value == "spake2_pair") {
40 } else if (value == "spake2_plain") {
42 } else if (value == "spake2_hmac") {
43 return Spake2(HMAC_SHA256);
44 } else if (value == "third_party") {
47 return AuthenticationMethod::Invalid();
52 std::string AuthenticationMethod::ApplyHashFunction(
53 HashFunction hash_function,
54 const std::string& tag,
55 const std::string& shared_secret) {
56 switch (hash_function) {
62 crypto::HMAC response(crypto::HMAC::SHA256);
63 if (!response.Init(tag)) {
64 LOG(FATAL) << "HMAC::Init failed";
67 unsigned char out_bytes[kSharedSecretHashLength];
68 if (!response.Sign(shared_secret, out_bytes, sizeof(out_bytes))) {
69 LOG(FATAL) << "HMAC::Sign failed";
72 return std::string(out_bytes, out_bytes + sizeof(out_bytes));
80 AuthenticationMethod::AuthenticationMethod()
82 hash_function_(NONE) {
85 AuthenticationMethod::AuthenticationMethod(MethodType type,
86 HashFunction hash_function)
88 hash_function_(hash_function) {
89 DCHECK_NE(type_, INVALID);
92 AuthenticationMethod::HashFunction AuthenticationMethod::hash_function() const {
94 return hash_function_;
97 const std::string AuthenticationMethod::ToString() const {
106 return "spake2_pair";
109 switch (hash_function_) {
111 return "spake2_plain";
113 return "spake2_hmac";
118 return "third_party";
124 bool AuthenticationMethod::operator ==(
125 const AuthenticationMethod& other) const {
126 return type_ == other.type_ &&
127 hash_function_ == other.hash_function_;
130 bool SharedSecretHash::Parse(const std::string& as_string) {
131 size_t separator = as_string.find(':');
132 if (separator == std::string::npos)
135 std::string function_name = as_string.substr(0, separator);
136 if (function_name == "plain") {
137 hash_function = AuthenticationMethod::NONE;
138 } else if (function_name == "hmac") {
139 hash_function = AuthenticationMethod::HMAC_SHA256;
144 if (!base::Base64Decode(as_string.substr(separator + 1), &value)) {
151 } // namespace protocol
152 } // namespace remoting