summaryrefslogtreecommitdiffstats
path: root/remoting/protocol/channel_authenticator.h
blob: 4d2dc0591a8fbb09d0babe5598cd158b6993aad9 (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
99
100
101
// Copyright (c) 2011 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_CHANNEL_AUTHENTICATOR_H_
#define REMOTING_PROTOCOL_CHANNEL_AUTHENTICATOR_H_

#include <string>

#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "base/threading/non_thread_safe.h"
#include "net/base/completion_callback.h"

namespace net {
class DrainableIOBuffer;
class GrowableIOBuffer;
class SSLClientSocket;
class SSLServerSocket;
}  // namespace net

namespace remoting {
namespace protocol {

class ChannelAuthenticator : public base::NonThreadSafe {
 public:
  enum Result {
    SUCCESS,
    FAILURE,
  };

  typedef base::Callback<void(Result)> DoneCallback;

  ChannelAuthenticator() { }
  virtual ~ChannelAuthenticator() { }

  // Starts authentication of the |socket|. |done_callback| is called
  // when authentication is finished. Caller retains ownership of
  // |socket|. |shared_secret| is a shared secret that we use to
  // authenticate the channel.
  virtual void Authenticate(const std::string& shared_secret,
                            const DoneCallback& done_callback) = 0;

 private:
  DISALLOW_COPY_AND_ASSIGN(ChannelAuthenticator);
};

class HostChannelAuthenticator : public ChannelAuthenticator {
 public:
  HostChannelAuthenticator(net::SSLServerSocket* socket);
  virtual ~HostChannelAuthenticator();

  // ChannelAuthenticator overrides.
  virtual void Authenticate(const std::string& shared_secret,
                            const DoneCallback& done_callback) OVERRIDE;

 private:
  void DoAuthRead();
  void OnAuthBytesRead(int result);
  bool HandleAuthBytesRead(int result);
  bool VerifyAuthBytes(const std::string& received_auth_bytes);

  std::string auth_bytes_;
  net::SSLServerSocket* socket_;
  DoneCallback done_callback_;

  scoped_refptr<net::GrowableIOBuffer> auth_read_buf_;

  net::OldCompletionCallbackImpl<HostChannelAuthenticator> auth_read_callback_;

  DISALLOW_COPY_AND_ASSIGN(HostChannelAuthenticator);
};

class ClientChannelAuthenticator : public ChannelAuthenticator {
 public:
  ClientChannelAuthenticator(net::SSLClientSocket* socket);
  virtual ~ClientChannelAuthenticator();

  // ChannelAuthenticator overrides.
  virtual void Authenticate(const std::string& shared_secret,
                            const DoneCallback& done_callback);

 private:
  void DoAuthWrite();
  void OnAuthBytesWritten(int result);
  bool HandleAuthBytesWritten(int result);

  net::SSLClientSocket* socket_;
  DoneCallback done_callback_;

  scoped_refptr<net::DrainableIOBuffer> auth_write_buf_;

  net::OldCompletionCallbackImpl<ClientChannelAuthenticator> auth_write_callback_;

  DISALLOW_COPY_AND_ASSIGN(ClientChannelAuthenticator);
};

}  // namespace protocol
}  // namespace remoting

#endif  // REMOTING_PROTOCOL_CHANNEL_AUTHENTICATOR_H_