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 protected: 51 virtual ~Client(); 52 }; 53 54 // The connection factory requires a client for yielding creation results 55 // asynchronously, as well as a task runner it can use to for running 56 // callbacks both on the factory and on created TlsConnection instances. 57 static std::unique_ptr<TlsConnectionFactory> CreateFactory( 58 Client* client, 59 TaskRunner* task_runner); 60 61 virtual ~TlsConnectionFactory(); 62 63 // Fires an OnConnected or OnConnectionFailed event. 64 virtual void Connect(const IPEndpoint& remote_address, 65 const TlsConnectOptions& options) = 0; 66 67 // Set the TlsCredentials used for listening for new connections. Currently, 68 // having different certificates on different address is not supported. This 69 // must be called before the first call to Listen. 70 virtual void SetListenCredentials(const TlsCredentials& credentials) = 0; 71 72 // Fires an OnAccepted or OnConnectionFailed event. 73 virtual void Listen(const IPEndpoint& local_address, 74 const TlsListenOptions& options) = 0; 75 76 protected: 77 TlsConnectionFactory(); 78 }; 79 80 } // namespace openscreen 81 82 #endif // PLATFORM_API_TLS_CONNECTION_FACTORY_H_ 83