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