summaryrefslogtreecommitdiffstats
path: root/remoting/protocol/authentication_method.h
blob: 6b94a06cce0153165cf3205686d613ae490204ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// AuthenticationMethod represents an authentication algorithm and its
// configuration. It knows how to parse and format authentication
// method names.
// Currently the following methods are supported:
//   v1_token - deprecated V1 authentication mechanism,
//   spake2_plain - SPAKE2 without hashing applied to the password.
//   spake2_hmac - SPAKE2 with HMAC hashing of the password.

#ifndef REMOTING_PROTOCOL_AUTHENTICATION_METHOD_H_
#define REMOTING_PROTOCOL_AUTHENTICATION_METHOD_H_

#include <string>

#include "base/memory/scoped_ptr.h"

namespace remoting {
namespace protocol {

class Authenticator;

class AuthenticationMethod {
 public:
  enum Version {
    // Legacy authentication mechanism.
    // TODO(sergeyu): Should be removed when we finished switching to
    // the new version (at which point this enum may be removed).
    // crbug.com/110483
    VERSION_1,

    // The new SPAKE2-based authentication.
    VERSION_2,
  };

  enum HashFunction {
    NONE,
    HMAC_SHA256,
  };

  // Constructors for various authentication methods.
  static AuthenticationMethod Invalid();
  static AuthenticationMethod V1Token();
  static AuthenticationMethod Spake2(HashFunction hash_function);

  // Parses a string that defines an authentication method. Returns an
  // invalid value if the string is invalid.
  static AuthenticationMethod FromString(const std::string& value);

  // Applies the specified hash function to |shared_secret| with the
  // specified |tag| as a key.
  static std::string ApplyHashFunction(HashFunction hash_function,
                                       const std::string& tag,
                                       const std::string& shared_secret);

  // Returns true
  bool is_valid() const { return !invalid_; }

  // Following methods are valid only when is_valid() returns true.

  // Version of the authentication protocol.
  Version version() const ;

  // Hash function applied to the shared secret on both ends.
  HashFunction hash_function() const;

  // Returns string representation of the value stored in this object.
  const std::string ToString() const;

  // Creates client authenticator using the specified parameters.
  scoped_ptr<Authenticator> CreateAuthenticator(
      const std::string& local_jid,
      const std::string& tag,
      const std::string& shared_secret) const;

  // Comparison operators so that std::find() can be used with
  // collections of this class.
  bool operator ==(const AuthenticationMethod& other) const;
  bool operator !=(const AuthenticationMethod& other) const {
    return !(*this == other);
  }

 private:
  AuthenticationMethod();
  AuthenticationMethod(Version version,
                       HashFunction hash_function);

  bool invalid_;
  Version version_;
  HashFunction hash_function_;
};

}  // namespace protocol
}  // namespace remoting

#endif  // REMOTING_PROTOCOL_AUTHENTICATION_METHOD_H_