blob: 4c4cfbbac2f8f02879177957eefc371ff808c390 [file] [log] [blame]
nharperfbaacc02020-04-24 17:30:22 -07001// Copyright (c) 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
QUICHE team5be974e2020-12-29 18:35:24 -05005#include "quic/test_tools/test_ticket_crypter.h"
nharperfbaacc02020-04-24 17:30:22 -07006
7#include <cstring>
8
vasilvv035fe3d2020-10-20 08:38:37 -07009#include "absl/base/macros.h"
QUICHE team5be974e2020-12-29 18:35:24 -050010#include "quic/core/crypto/quic_random.h"
nharperfbaacc02020-04-24 17:30:22 -070011
12namespace quic {
13namespace test {
14
15namespace {
16
17// A TicketCrypter implementation is supposed to encrypt and decrypt session
18// tickets. However, the only requirement that is needed of a test
19// implementation is that calling Decrypt(Encrypt(input), callback) results in
20// callback being called with input. (The output of Encrypt must also not exceed
21// the overhead specified by MaxOverhead.) This test implementation encrypts
22// tickets by prepending kTicketPrefix to generate the ciphertext. The decrypt
23// function checks that the prefix is present and strips it; otherwise it
24// returns an empty vector to signal failure.
25constexpr char kTicketPrefix[] = "TEST TICKET";
26
27} // namespace
28
nharper732883c2020-10-27 11:41:41 -070029TestTicketCrypter::TestTicketCrypter()
30 : ticket_prefix_(ABSL_ARRAYSIZE(kTicketPrefix) + 16) {
31 memcpy(ticket_prefix_.data(), kTicketPrefix, ABSL_ARRAYSIZE(kTicketPrefix));
32 QuicRandom::GetInstance()->RandBytes(
33 ticket_prefix_.data() + ABSL_ARRAYSIZE(kTicketPrefix), 16);
34}
35
nharperfbaacc02020-04-24 17:30:22 -070036size_t TestTicketCrypter::MaxOverhead() {
nharper732883c2020-10-27 11:41:41 -070037 return ticket_prefix_.size();
nharperfbaacc02020-04-24 17:30:22 -070038}
39
vasilvv5f225b02020-10-08 11:49:09 -040040std::vector<uint8_t> TestTicketCrypter::Encrypt(absl::string_view in) {
nharper732883c2020-10-27 11:41:41 -070041 size_t prefix_len = ticket_prefix_.size();
nharperfbaacc02020-04-24 17:30:22 -070042 std::vector<uint8_t> out(prefix_len + in.size());
nharper732883c2020-10-27 11:41:41 -070043 memcpy(out.data(), ticket_prefix_.data(), prefix_len);
nharperfbaacc02020-04-24 17:30:22 -070044 memcpy(out.data() + prefix_len, in.data(), in.size());
45 return out;
46}
47
vasilvv5f225b02020-10-08 11:49:09 -040048std::vector<uint8_t> TestTicketCrypter::Decrypt(absl::string_view in) {
nharper732883c2020-10-27 11:41:41 -070049 size_t prefix_len = ticket_prefix_.size();
nharperfbaacc02020-04-24 17:30:22 -070050 if (fail_decrypt_ || in.size() < prefix_len ||
nharper732883c2020-10-27 11:41:41 -070051 memcmp(ticket_prefix_.data(), in.data(), prefix_len) != 0) {
nharperfbaacc02020-04-24 17:30:22 -070052 return std::vector<uint8_t>();
53 }
54 return std::vector<uint8_t>(in.begin() + prefix_len, in.end());
55}
56
57void TestTicketCrypter::Decrypt(
vasilvv5f225b02020-10-08 11:49:09 -040058 absl::string_view in,
nharperfbaacc02020-04-24 17:30:22 -070059 std::unique_ptr<ProofSource::DecryptCallback> callback) {
60 auto decrypted_ticket = Decrypt(in);
61 if (run_async_) {
62 pending_callbacks_.push_back({std::move(callback), decrypted_ticket});
63 } else {
64 callback->Run(decrypted_ticket);
65 }
66}
67
68void TestTicketCrypter::SetRunCallbacksAsync(bool run_async) {
69 run_async_ = run_async;
70}
71
72size_t TestTicketCrypter::NumPendingCallbacks() {
73 return pending_callbacks_.size();
74}
75
76void TestTicketCrypter::RunPendingCallback(size_t n) {
77 const PendingCallback& callback = pending_callbacks_[n];
78 callback.callback->Run(callback.decrypted_ticket);
79}
80
81} // namespace test
82} // namespace quic