1 // Copyright (c) 2013 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 QUICHE_QUIC_CORE_CRYPTO_KEY_EXCHANGE_H_ 6 #define QUICHE_QUIC_CORE_CRYPTO_KEY_EXCHANGE_H_ 7 8 #include <memory> 9 #include <string> 10 11 #include "absl/strings/string_view.h" 12 #include "quiche/quic/core/crypto/crypto_protocol.h" 13 #include "quiche/quic/core/crypto/quic_random.h" 14 #include "quiche/quic/platform/api/quic_export.h" 15 16 namespace quic { 17 18 // Interface for a Diffie-Hellman key exchange with an asynchronous interface. 19 // This allows for implementations which hold the private key locally, as well 20 // as ones which make an RPC to an external key-exchange service. 21 class QUICHE_EXPORT AsynchronousKeyExchange { 22 public: 23 virtual ~AsynchronousKeyExchange() = default; 24 25 // Callback base class for receiving the results of an async call to 26 // CalculateSharedKeys. 27 class QUICHE_EXPORT Callback { 28 public: 29 Callback() = default; 30 virtual ~Callback() = default; 31 32 // Invoked upon completion of CalculateSharedKeysAsync. 33 // 34 // |ok| indicates whether the operation completed successfully. If false, 35 // then the value pointed to by |shared_key| passed in to 36 // CalculateSharedKeyAsync is undefined. 37 virtual void Run(bool ok) = 0; 38 39 private: 40 Callback(const Callback&) = delete; 41 Callback& operator=(const Callback&) = delete; 42 }; 43 44 // CalculateSharedKey computes the shared key between a private key which is 45 // conceptually owned by this object (though it may not be physically located 46 // in this process) and a public value from the peer. Callers should expect 47 // that |callback| might be invoked synchronously. Results will be written 48 // into |*shared_key|. 49 virtual void CalculateSharedKeyAsync( 50 absl::string_view peer_public_value, std::string* shared_key, 51 std::unique_ptr<Callback> callback) const = 0; 52 53 // Tag indicating the key-exchange algorithm this object will use. 54 virtual QuicTag type() const = 0; 55 }; 56 57 // Interface for a Diffie-Hellman key exchange with both synchronous and 58 // asynchronous interfaces. Only implementations which hold the private key 59 // locally should implement this interface. 60 class QUICHE_EXPORT SynchronousKeyExchange : public AsynchronousKeyExchange { 61 public: 62 virtual ~SynchronousKeyExchange() = default; 63 64 // AyncKeyExchange API. Note that this method is marked 'final.' Subclasses 65 // should implement CalculateSharedKeySync only. CalculateSharedKeyAsync(absl::string_view peer_public_value,std::string * shared_key,std::unique_ptr<Callback> callback)66 void CalculateSharedKeyAsync(absl::string_view peer_public_value, 67 std::string* shared_key, 68 std::unique_ptr<Callback> callback) const final { 69 const bool ok = CalculateSharedKeySync(peer_public_value, shared_key); 70 callback->Run(ok); 71 } 72 73 // CalculateSharedKey computes the shared key between a local private key and 74 // a public value from the peer. Results will be written into |*shared_key|. 75 virtual bool CalculateSharedKeySync(absl::string_view peer_public_value, 76 std::string* shared_key) const = 0; 77 78 // public_value returns the local public key which can be sent to a peer in 79 // order to complete a key exchange. The returned absl::string_view is 80 // a reference to a member of this object and is only valid for as long as it 81 // exists. 82 virtual absl::string_view public_value() const = 0; 83 }; 84 85 // Create a SynchronousKeyExchange object which will use a keypair generated 86 // from |private_key|, and a key-exchange algorithm specified by |type|, which 87 // must be one of {kC255, kC256}. Returns nullptr if |private_key| or |type| is 88 // invalid. 89 std::unique_ptr<SynchronousKeyExchange> CreateLocalSynchronousKeyExchange( 90 QuicTag type, absl::string_view private_key); 91 92 // Create a SynchronousKeyExchange object which will use a keypair generated 93 // from |rand|, and a key-exchange algorithm specified by |type|, which must be 94 // one of {kC255, kC256}. Returns nullptr if |type| is invalid. 95 std::unique_ptr<SynchronousKeyExchange> CreateLocalSynchronousKeyExchange( 96 QuicTag type, QuicRandom* rand); 97 98 } // namespace quic 99 100 #endif // QUICHE_QUIC_CORE_CRYPTO_KEY_EXCHANGE_H_ 101