proxygen
TicketCodec-inl.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 namespace fizz {
10 namespace server {
11 
12 template <CertificateStorage Storage>
14 
15 template <CertificateStorage Storage>
17  Buf selfIdentity = folly::IOBuf::create(0);
18  if (resState.serverCert) {
19  selfIdentity = folly::IOBuf::copyBuffer(resState.serverCert->getIdentity());
20  }
21 
22  uint64_t ticketIssueTime = std::chrono::duration_cast<std::chrono::seconds>(
23  resState.ticketIssueTime.time_since_epoch())
24  .count();
25 
26  auto buf = folly::IOBuf::create(60);
27  folly::io::Appender appender(buf.get(), 60);
28 
29  fizz::detail::write(resState.version, appender);
30  fizz::detail::write(resState.cipher, appender);
31  fizz::detail::writeBuf<uint16_t>(resState.resumptionSecret, appender);
32  fizz::detail::writeBuf<uint16_t>(selfIdentity, appender);
33  appendClientCertificate(Storage, resState.clientCert, appender);
34  fizz::detail::write(resState.ticketAgeAdd, appender);
35  fizz::detail::write(ticketIssueTime, appender);
36  if (resState.alpn) {
37  auto alpnBuf = folly::IOBuf::copyBuffer(*resState.alpn);
38  fizz::detail::writeBuf<uint8_t>(alpnBuf, appender);
39  } else {
40  fizz::detail::writeBuf<uint8_t>(nullptr, appender);
41  }
42  fizz::detail::writeBuf<uint16_t>(resState.appToken, appender);
43  return buf;
44 }
45 
46 template <CertificateStorage Storage>
48  Buf encoded,
49  const FizzServerContext* context) {
50  folly::io::Cursor cursor(encoded.get());
51 
52  ResumptionState resState;
53  fizz::detail::read(resState.version, cursor);
54  fizz::detail::read(resState.cipher, cursor);
55  fizz::detail::readBuf<uint16_t>(resState.resumptionSecret, cursor);
56  Buf selfIdentity;
57  fizz::detail::readBuf<uint16_t>(selfIdentity, cursor);
58 
59  resState.clientCert = readClientCertificate(cursor);
60 
61  fizz::detail::read(resState.ticketAgeAdd, cursor);
62  uint64_t seconds;
63  fizz::detail::read(seconds, cursor);
64  Buf alpnBuf;
65  fizz::detail::readBuf<uint8_t>(alpnBuf, cursor);
66  if (!alpnBuf->empty()) {
67  resState.alpn = alpnBuf->moveToFbString().toStdString();
68  }
69 
70  resState.ticketIssueTime = std::chrono::time_point<std::chrono::system_clock>(
71  std::chrono::seconds(seconds));
72  if (context) {
73  resState.serverCert =
74  context->getCert(selfIdentity->moveToFbString().toStdString());
75  }
76  if (cursor.isAtEnd()) {
77  return resState;
78  }
79  fizz::detail::readBuf<uint16_t>(resState.appToken, cursor);
80 
81  return resState;
82 }
83 } // namespace server
84 } // namespace fizz
folly::Optional< std::pair< std::shared_ptr< SelfCert >, SignatureScheme > > getCert(const folly::Optional< std::string > &sni, const std::vector< SignatureScheme > &peerSigSchemes) const
void write(const T &in, folly::io::Appender &appender)
Definition: Types-inl.h:112
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
static ResumptionState decode(Buf encoded, const FizzServerContext *context)
context
Definition: CMakeCache.txt:563
folly::Optional< std::string > alpn
std::chrono::system_clock::time_point ticketIssueTime
std::shared_ptr< const Cert > readClientCertificate(folly::io::Cursor &cursor)
Definition: TicketCodec.cpp:47
size_t read(T &out, folly::io::Cursor &cursor)
Definition: Types-inl.h:258
Definition: Actions.h:16
static constexpr folly::StringPiece Label
Definition: TicketCodec.h:41
int * count
std::unique_ptr< folly::IOBuf > Buf
Definition: Types.h:22
void appendClientCertificate(CertificateStorage storage, const std::shared_ptr< const Cert > &cert, folly::io::Appender &appender)
Definition: TicketCodec.cpp:26
std::shared_ptr< const Cert > serverCert
static std::unique_ptr< IOBuf > copyBuffer(const void *buf, std::size_t size, std::size_t headroom=0, std::size_t minTailroom=0)
Definition: IOBuf.h:1587
std::shared_ptr< const Cert > clientCert
static Buf encode(ResumptionState state)