spake2_authenticator.h 3.1 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2016 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.

#ifndef REMOTING_PROTOCOL_SPAKE2_AUTHENTICATOR_H_
#define REMOTING_PROTOCOL_SPAKE2_AUTHENTICATOR_H_

8
#include <memory>
9 10 11 12 13 14
#include <queue>
#include <string>

#include "base/compiler_specific.h"
#include "base/gtest_prod_util.h"
#include "base/macros.h"
15
#include "base/memory/ref_counted.h"
16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include "remoting/protocol/authenticator.h"

typedef struct spake2_ctx_st SPAKE2_CTX;

namespace remoting {

class RsaKeyPair;

namespace protocol {

// Authenticator that uses SPAKE2 implementation from BoringSSL. It
// implements SPAKE2 over Curve25519.
class Spake2Authenticator : public Authenticator {
 public:
30
  static std::unique_ptr<Authenticator> CreateForClient(
31 32 33 34 35
      const std::string& local_id,
      const std::string& remote_id,
      const std::string& shared_secret,
      State initial_state);

36
  static std::unique_ptr<Authenticator> CreateForHost(
37 38 39 40
      const std::string& local_id,
      const std::string& remote_id,
      const std::string& local_cert,
      scoped_refptr<RsaKeyPair> key_pair,
41
      const std::string& shared_secret,
42 43 44 45 46 47 48 49 50 51
      State initial_state);

  ~Spake2Authenticator() override;

  // Authenticator interface.
  State state() const override;
  bool started() const override;
  RejectionReason rejection_reason() const override;
  void ProcessMessage(const buzz::XmlElement* message,
                      const base::Closure& resume_callback) override;
52
  std::unique_ptr<buzz::XmlElement> GetNextMessage() override;
53
  const std::string& GetAuthKey() const override;
54 55
  std::unique_ptr<ChannelAuthenticator> CreateChannelAuthenticator()
      const override;
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 99 100 101

 private:
  FRIEND_TEST_ALL_PREFIXES(Spake2AuthenticatorTest, InvalidSecret);

  Spake2Authenticator(const std::string& local_id,
                      const std::string& remote_id,
                      const std::string& shared_secret,
                      bool is_host,
                      State initial_state);

  virtual void ProcessMessageInternal(const buzz::XmlElement* message);

  std::string CalculateVerificationHash(bool from_host,
                                        const std::string& local_id,
                                        const std::string& remote_id);

  const std::string local_id_;
  const std::string remote_id_;
  const std::string shared_secret_;
  const bool is_host_;

  // Used only for host authenticators.
  std::string local_cert_;
  scoped_refptr<RsaKeyPair> local_key_pair_;

  // Used only for client authenticators.
  std::string remote_cert_;

  // Used for both host and client authenticators.
  SPAKE2_CTX* spake2_context_;
  State state_;
  bool started_ = false;
  RejectionReason rejection_reason_ = INVALID_CREDENTIALS;
  std::string local_spake_message_;
  bool spake_message_sent_ = false;
  std::string outgoing_verification_hash_;
  std::string auth_key_;
  std::string expected_verification_hash_;

  DISALLOW_COPY_AND_ASSIGN(Spake2Authenticator);
};

}  // namespace protocol
}  // namespace remoting

#endif  // REMOTING_PROTOCOL_SPAKE2_AUTHENTICATOR_H_