proxygen
AeadTicketCipher.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #pragma once
10 
14 
15 namespace fizz {
16 namespace server {
17 
18 template <typename AeadType, typename CodecType, typename HkdfType>
20  public:
27  explicit AeadTicketCipher(std::string pskContext)
29  {CodecType::Label.toString(), pskContext})) {}
30 
32  : tokenCipher_(std::vector<std::string>({CodecType::Label.toString()})) {}
33 
39  bool setTicketSecrets(const std::vector<folly::ByteRange>& ticketSecrets) {
40  return tokenCipher_.setSecrets(ticketSecrets);
41  }
42 
44  context_ = context;
45  }
46 
47  void setValidity(std::chrono::seconds validity) {
48  validity_ = validity;
49  }
50 
52  ResumptionState resState) const override {
53  auto encoded = CodecType::encode(std::move(resState));
54  auto ticket = tokenCipher_.encrypt(std::move(encoded));
55  if (!ticket) {
56  return folly::none;
57  } else {
58  return std::make_pair(std::move(*ticket), validity_);
59  }
60  }
61 
63  std::unique_ptr<folly::IOBuf> encryptedTicket) const override {
64  auto plaintext = tokenCipher_.decrypt(std::move(encryptedTicket));
65  if (plaintext) {
66  try {
67  auto decoded = CodecType::decode(std::move(*plaintext), context_);
68  return std::make_pair(PskType::Resumption, std::move(decoded));
69  } catch (const std::exception& ex) {
70  VLOG(6) << "Failed to decode ticket, ex=" << ex.what();
71  }
72  }
73 
74  return std::make_pair(PskType::Rejected, folly::none);
75  }
76 
77  private:
79 
80  std::chrono::seconds validity_{std::chrono::hours(1)};
81 
82  const FizzServerContext* context_ = nullptr;
83 };
84 } // namespace server
85 } // namespace fizz
unique_ptr< IOBuf > encode(vector< HPACKHeader > &headers, HPACKEncoder &encoder)
AeadTokenCipher< AeadType, HkdfType > tokenCipher_
context
Definition: CMakeCache.txt:563
TokenBindingMessage decode(folly::io::Cursor &cursor)
Definition: Types.cpp:132
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
static constexpr StringPiece ticket
bool setTicketSecrets(const std::vector< folly::ByteRange > &ticketSecrets)
Definition: Actions.h:16
Definition: Traits.h:588
folly::Future< folly::Optional< std::pair< Buf, std::chrono::seconds > > > encrypt(ResumptionState resState) const override
AeadTicketCipher(std::string pskContext)
const FizzServerContext * context_
folly::Future< std::pair< PskType, folly::Optional< ResumptionState > > > decrypt(std::unique_ptr< folly::IOBuf > encryptedTicket) const override
const char * string
Definition: Conv.cpp:212
std::chrono::seconds validity_
void setValidity(std::chrono::seconds validity)
void setContext(const FizzServerContext *context)
constexpr None none
Definition: Optional.h:87