• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 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 
5 #ifndef PLATFORM_API_TLS_CONNECTION_FACTORY_H_
6 #define PLATFORM_API_TLS_CONNECTION_FACTORY_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "platform/base/ip_address.h"
14 
15 namespace openscreen {
16 
17 class TaskRunner;
18 class TlsConnection;
19 struct TlsConnectOptions;
20 struct TlsCredentials;
21 struct TlsListenOptions;
22 
23 // We expect a single factory to be able to handle an arbitrary number of
24 // calls using the same client and task runner.
25 class TlsConnectionFactory {
26  public:
27   // Client callbacks are ran on the provided TaskRunner.
28   class Client {
29    public:
30     // Provides a new |connection| that resulted from listening on the local
31     // socket. |der_x509_peer_cert| is the DER-encoded X509 certificate from the
32     // peer if present, or empty if the peer didn't provide one.
33     virtual void OnAccepted(TlsConnectionFactory* factory,
34                             std::vector<uint8_t> der_x509_peer_cert,
35                             std::unique_ptr<TlsConnection> connection) = 0;
36 
37     // Provides a new |connection| that resulted from connecting to a remote
38     // endpoint. |der_x509_peer_cert| is the DER-encoded X509 certificate from
39     // the peer.
40     virtual void OnConnected(TlsConnectionFactory* factory,
41                              std::vector<uint8_t> der_x509_peer_cert,
42                              std::unique_ptr<TlsConnection> connection) = 0;
43 
44     virtual void OnConnectionFailed(TlsConnectionFactory* factory,
45                                     const IPEndpoint& remote_address) = 0;
46 
47     // Called when a non-recoverable error occurs.
48     virtual void OnError(TlsConnectionFactory* factory, Error error) = 0;
49   };
50 
51   // The connection factory requires a client for yielding creation results
52   // asynchronously, as well as a task runner it can use to for running
53   // callbacks both on the factory and on created TlsConnection instances.
54   static std::unique_ptr<TlsConnectionFactory> CreateFactory(
55       Client* client,
56       TaskRunner* task_runner);
57 
58   virtual ~TlsConnectionFactory();
59 
60   // Fires an OnConnected or OnConnectionFailed event.
61   virtual void Connect(const IPEndpoint& remote_address,
62                        const TlsConnectOptions& options) = 0;
63 
64   // Set the TlsCredentials used for listening for new connections. Currently,
65   // having different certificates on different address is not supported. This
66   // must be called before the first call to Listen.
67   virtual void SetListenCredentials(const TlsCredentials& credentials) = 0;
68 
69   // Fires an OnAccepted or OnConnectionFailed event.
70   virtual void Listen(const IPEndpoint& local_address,
71                       const TlsListenOptions& options) = 0;
72 
73  protected:
74   TlsConnectionFactory();
75 };
76 
77 }  // namespace openscreen
78 
79 #endif  // PLATFORM_API_TLS_CONNECTION_FACTORY_H_
80