blob: d547a25bf9ec9aafe84d713d75b9b96f471473f5 [file] [log] [blame]
nharperb57e9d32020-05-05 12:43:17 -07001// Copyright 2020 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_TOOLS_SIMPLE_TICKET_CRYPTER_H_
6#define QUICHE_QUIC_TOOLS_SIMPLE_TICKET_CRYPTER_H_
7
8#include "third_party/boringssl/src/include/openssl/aead.h"
QUICHE team5be974e2020-12-29 18:35:24 -05009#include "quic/core/crypto/proof_source.h"
10#include "quic/core/quic_clock.h"
11#include "quic/core/quic_time.h"
nharperb57e9d32020-05-05 12:43:17 -070012
13namespace quic {
14
15// SimpleTicketCrypter implements the QUIC ProofSource::TicketCrypter interface.
16// It generates a random key at startup and every 7 days it rotates the key,
17// keeping track of the previous key used to facilitate decrypting older
18// tickets. This implementation is not suitable for server setups where multiple
19// servers need to share keys.
nharper37d11a02020-05-08 16:55:40 -070020class QUIC_NO_EXPORT SimpleTicketCrypter
nharperb57e9d32020-05-05 12:43:17 -070021 : public quic::ProofSource::TicketCrypter {
22 public:
23 explicit SimpleTicketCrypter(QuicClock* clock);
24 ~SimpleTicketCrypter() override;
25
26 size_t MaxOverhead() override;
vasilvv6c9e9c32020-10-08 08:16:57 -070027 std::vector<uint8_t> Encrypt(absl::string_view in) override;
nharperb57e9d32020-05-05 12:43:17 -070028 void Decrypt(
vasilvv6c9e9c32020-10-08 08:16:57 -070029 absl::string_view in,
nharperb57e9d32020-05-05 12:43:17 -070030 std::unique_ptr<quic::ProofSource::DecryptCallback> callback) override;
31
32 private:
vasilvv6c9e9c32020-10-08 08:16:57 -070033 std::vector<uint8_t> Decrypt(absl::string_view in);
nharperb57e9d32020-05-05 12:43:17 -070034
35 void MaybeRotateKeys();
36
37 static constexpr size_t kKeySize = 16;
38
39 struct Key {
40 uint8_t key[kKeySize];
41 bssl::ScopedEVP_AEAD_CTX aead_ctx;
42 QuicTime expiration = QuicTime::Zero();
43 };
44
45 std::unique_ptr<Key> NewKey();
46
47 std::unique_ptr<Key> current_key_;
48 std::unique_ptr<Key> previous_key_;
49 uint8_t key_epoch_ = 0;
50 QuicClock* clock_;
51};
52
53} // namespace quic
54
55#endif // QUICHE_QUIC_TOOLS_SIMPLE_TICKET_CRYPTER_H_