• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 QUIC_EXPORT_PRIVATE 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 QUIC_EXPORT_PRIVATE 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 QUIC_EXPORT_PRIVATE SynchronousKeyExchange
61     : public AsynchronousKeyExchange {
62  public:
63   virtual ~SynchronousKeyExchange() = default;
64 
65   // AyncKeyExchange API.  Note that this method is marked 'final.'  Subclasses
66   // should implement CalculateSharedKeySync only.
CalculateSharedKeyAsync(absl::string_view peer_public_value,std::string * shared_key,std::unique_ptr<Callback> callback)67   void CalculateSharedKeyAsync(absl::string_view peer_public_value,
68                                std::string* shared_key,
69                                std::unique_ptr<Callback> callback) const final {
70     const bool ok = CalculateSharedKeySync(peer_public_value, shared_key);
71     callback->Run(ok);
72   }
73 
74   // CalculateSharedKey computes the shared key between a local private key and
75   // a public value from the peer.  Results will be written into |*shared_key|.
76   virtual bool CalculateSharedKeySync(absl::string_view peer_public_value,
77                                       std::string* shared_key) const = 0;
78 
79   // public_value returns the local public key which can be sent to a peer in
80   // order to complete a key exchange. The returned absl::string_view is
81   // a reference to a member of this object and is only valid for as long as it
82   // exists.
83   virtual absl::string_view public_value() const = 0;
84 };
85 
86 // Create a SynchronousKeyExchange object which will use a keypair generated
87 // from |private_key|, and a key-exchange algorithm specified by |type|, which
88 // must be one of {kC255, kC256}.  Returns nullptr if |private_key| or |type| is
89 // invalid.
90 std::unique_ptr<SynchronousKeyExchange> CreateLocalSynchronousKeyExchange(
91     QuicTag type, absl::string_view private_key);
92 
93 // Create a SynchronousKeyExchange object which will use a keypair generated
94 // from |rand|, and a key-exchange algorithm specified by |type|, which must be
95 // one of {kC255, kC256}.  Returns nullptr if |type| is invalid.
96 std::unique_ptr<SynchronousKeyExchange> CreateLocalSynchronousKeyExchange(
97     QuicTag type, QuicRandom* rand);
98 
99 }  // namespace quic
100 
101 #endif  // QUICHE_QUIC_CORE_CRYPTO_KEY_EXCHANGE_H_
102