proxygen
fizz::KeyScheduler Class Reference

#include <KeyScheduler.h>

Inheritance diagram for fizz::KeyScheduler:
fizz::LoggingKeyScheduler fizz::test::MockKeyScheduler

Classes

struct  AppTrafficSecret
 
struct  EarlySecret
 
struct  HandshakeSecret
 
struct  MasterSecret
 

Public Member Functions

 KeyScheduler (std::unique_ptr< KeyDerivation > deriver)
 
virtual ~KeyScheduler ()=default
 
virtual void deriveEarlySecret (folly::ByteRange psk)
 
virtual void deriveHandshakeSecret ()
 
virtual void deriveHandshakeSecret (folly::ByteRange ecdhe)
 
virtual void deriveMasterSecret ()
 
virtual void deriveAppTrafficSecrets (folly::ByteRange transcript)
 
virtual void clearMasterSecret ()
 
virtual uint32_t clientKeyUpdate ()
 
virtual uint32_t serverKeyUpdate ()
 
virtual std::vector< uint8_tgetSecret (EarlySecrets s, folly::ByteRange transcript) const
 
virtual std::vector< uint8_tgetSecret (HandshakeSecrets s, folly::ByteRange transcript) const
 
virtual std::vector< uint8_tgetSecret (MasterSecrets s, folly::ByteRange transcript) const
 
virtual std::vector< uint8_tgetSecret (AppTrafficSecrets s) const
 
virtual TrafficKey getTrafficKey (folly::ByteRange trafficSecret, size_t keyLength, size_t ivLength) const
 
virtual Buf getResumptionSecret (folly::ByteRange resumptionMasterSecret, folly::ByteRange ticketNonce) const
 

Private Attributes

folly::Optional< boost::variant< EarlySecret, HandshakeSecret, MasterSecret > > secret_
 
folly::Optional< AppTrafficSecretappTrafficSecret_
 
std::unique_ptr< KeyDerivationderiver_
 

Detailed Description

Keeps track of the TLS 1.3 key derivation schedule.

Definition at line 33 of file KeyScheduler.h.

Constructor & Destructor Documentation

fizz::KeyScheduler::KeyScheduler ( std::unique_ptr< KeyDerivation deriver)
inlineexplicit

Definition at line 35 of file KeyScheduler.h.

References s, and uint32_t.

36  : deriver_(std::move(deriver)) {}
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::unique_ptr< KeyDerivation > deriver_
Definition: KeyScheduler.h:134
virtual fizz::KeyScheduler::~KeyScheduler ( )
virtualdefault

Member Function Documentation

void fizz::KeyScheduler::clearMasterSecret ( )
virtual

Clears the master secret. Must be in master secret state.

Definition at line 85 of file KeyScheduler.cpp.

References folly::none.

Referenced by fizz::sm::generateTicket(), and fizz::sm::handleCertMsg().

85  {
86  boost::get<MasterSecret>(*secret_);
87  secret_ = folly::none;
88 }
constexpr None none
Definition: Optional.h:87
uint32_t fizz::KeyScheduler::clientKeyUpdate ( )
virtual

Performs a key update on the client traffic key. Traffic secrets must be derived.

Definition at line 90 of file KeyScheduler.cpp.

References appTrafficSecret_, folly::IOBuf::create(), deriver_, kTrafficKeyUpdate, and folly::range().

Referenced by fizz::sm::generateTicket(), and fizz::sm::getMaxEarlyDataSize().

90  {
91  auto& appTrafficSecret = *appTrafficSecret_;
92  auto buf = deriver_->expandLabel(
93  folly::range(appTrafficSecret.client),
96  deriver_->hashLength());
97  buf->coalesce();
98  appTrafficSecret.client = std::vector<uint8_t>(buf->data(), buf->tail());
99  return ++appTrafficSecret.clientGeneration;
100 }
folly::Optional< AppTrafficSecret > appTrafficSecret_
Definition: KeyScheduler.h:132
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
std::unique_ptr< KeyDerivation > deriver_
Definition: KeyScheduler.h:134
static constexpr StringPiece kTrafficKeyUpdate
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
void fizz::KeyScheduler::deriveAppTrafficSecrets ( folly::ByteRange  transcript)
virtual

Derives the app traffic secrets given the handshake context. Must be in master secret state. Note that this does not clear the master secret.

Definition at line 75 of file KeyScheduler.cpp.

References appTrafficSecret_, fizz::KeyScheduler::AppTrafficSecret::client, deriver_, kClientAppTraffic, kServerAppTraffic, folly::gen::move, folly::range(), and fizz::KeyScheduler::AppTrafficSecret::server.

Referenced by fizz::sm::handleCertMsg().

75  {
76  auto& masterSecret = boost::get<MasterSecret>(*secret_);
77  AppTrafficSecret trafficSecret;
78  trafficSecret.client = deriver_->deriveSecret(
79  folly::range(masterSecret.secret), kClientAppTraffic, transcript);
80  trafficSecret.server = deriver_->deriveSecret(
81  folly::range(masterSecret.secret), kServerAppTraffic, transcript);
82  appTrafficSecret_ = std::move(trafficSecret);
83 }
folly::Optional< AppTrafficSecret > appTrafficSecret_
Definition: KeyScheduler.h:132
static constexpr StringPiece kServerAppTraffic
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
static constexpr StringPiece kClientAppTraffic
std::unique_ptr< KeyDerivation > deriver_
Definition: KeyScheduler.h:134
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
void fizz::KeyScheduler::deriveEarlySecret ( folly::ByteRange  psk)
virtual

Derives the early secret. Must be in uninitialized state.

Definition at line 32 of file KeyScheduler.cpp.

References deriver_, folly::range(), and secret_.

Referenced by fizz::sm::encodeAndAddBinders().

32  {
33  if (secret_) {
34  throw std::runtime_error("secret already set");
35  }
36 
37  auto zeros = std::vector<uint8_t>(deriver_->hashLength(), 0);
38  secret_ = EarlySecret{deriver_->hkdfExtract(folly::range(zeros), psk)};
39 }
folly::Optional< boost::variant< EarlySecret, HandshakeSecret, MasterSecret > > secret_
Definition: KeyScheduler.h:131
std::unique_ptr< KeyDerivation > deriver_
Definition: KeyScheduler.h:134
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
void fizz::KeyScheduler::deriveHandshakeSecret ( )
virtual

Derives the master secert. Must be in early secret state.

Definition at line 41 of file KeyScheduler.cpp.

References deriver_, kDerivedSecret, and folly::range().

Referenced by fizz::sm::doKex().

41  {
42  auto& earlySecret = boost::get<EarlySecret>(*secret_);
43  auto zeros = std::vector<uint8_t>(deriver_->hashLength(), 0);
44  auto preSecret = deriver_->deriveSecret(
45  folly::range(earlySecret.secret), kDerivedSecret, deriver_->blankHash());
46  secret_ = HandshakeSecret{
47  deriver_->hkdfExtract(folly::range(preSecret), folly::range(zeros))};
48 }
std::unique_ptr< KeyDerivation > deriver_
Definition: KeyScheduler.h:134
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
static constexpr StringPiece kDerivedSecret
void fizz::KeyScheduler::deriveHandshakeSecret ( folly::ByteRange  ecdhe)
virtual

Derives the master secret with a DH secret. Must be in uninitialized or early secret state.

Definition at line 50 of file KeyScheduler.cpp.

References deriver_, kDerivedSecret, folly::range(), and secret_.

50  {
51  if (!secret_) {
52  auto zeros = std::vector<uint8_t>(deriver_->hashLength(), 0);
53  secret_ = EarlySecret{
54  deriver_->hkdfExtract(folly::range(zeros), folly::range(zeros))};
55  }
56 
57  auto& earlySecret = boost::get<EarlySecret>(*secret_);
58  auto preSecret = deriver_->deriveSecret(
59  folly::range(earlySecret.secret), kDerivedSecret, deriver_->blankHash());
60  secret_ =
61  HandshakeSecret{deriver_->hkdfExtract(folly::range(preSecret), ecdhe)};
62 }
folly::Optional< boost::variant< EarlySecret, HandshakeSecret, MasterSecret > > secret_
Definition: KeyScheduler.h:131
std::unique_ptr< KeyDerivation > deriver_
Definition: KeyScheduler.h:134
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
static constexpr StringPiece kDerivedSecret
void fizz::KeyScheduler::deriveMasterSecret ( )
virtual

Derives the master secert. Must be in handshake secret state.

Definition at line 64 of file KeyScheduler.cpp.

References deriver_, kDerivedSecret, and folly::range().

Referenced by fizz::sm::handleCertMsg().

64  {
65  auto zeros = std::vector<uint8_t>(deriver_->hashLength(), 0);
66  auto& handshakeSecret = boost::get<HandshakeSecret>(*secret_);
67  auto preSecret = deriver_->deriveSecret(
68  folly::range(handshakeSecret.secret),
70  deriver_->blankHash());
71  secret_ = MasterSecret{
72  deriver_->hkdfExtract(folly::range(preSecret), folly::range(zeros))};
73 }
std::unique_ptr< KeyDerivation > deriver_
Definition: KeyScheduler.h:134
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
static constexpr StringPiece kDerivedSecret
Buf fizz::KeyScheduler::getResumptionSecret ( folly::ByteRange  resumptionMasterSecret,
folly::ByteRange  ticketNonce 
) const
virtual

Derive a resumption secret with a particular ticket nonce. Does not require being in master secret state.

Definition at line 204 of file KeyScheduler.cpp.

References deriver_, kResumption, and folly::IOBuf::wrapBuffer().

Referenced by fizz::sm::generateTicket(), and fizz::sm::getMaxEarlyDataSize().

206  {
207  return deriver_->expandLabel(
208  resumptionMasterSecret,
209  kResumption,
210  folly::IOBuf::wrapBuffer(ticketNonce),
211  deriver_->hashLength());
212 }
static std::unique_ptr< IOBuf > wrapBuffer(const void *buf, std::size_t capacity)
Definition: IOBuf.cpp:353
std::unique_ptr< KeyDerivation > deriver_
Definition: KeyScheduler.h:134
static constexpr StringPiece kResumption
std::vector< uint8_t > fizz::KeyScheduler::getSecret ( EarlySecrets  s,
folly::ByteRange  transcript 
) const
virtual

Retreive a secret from the scheduler. Must be in the appropriate state.

Reimplemented in fizz::LoggingKeyScheduler.

Definition at line 114 of file KeyScheduler.cpp.

References fizz::ClientEarlyTraffic, deriver_, fizz::EarlyExporter, fizz::ExternalPskBinder, kClientEarlyTraffic, kEarlyExporter, kExternalPskBinder, kResumptionPskBinder, fizz::test::label, folly::range(), and fizz::ResumptionPskBinder.

Referenced by fizz::sm::encodeAndAddBinders(), fizz::sm::generateTicket(), fizz::sm::getMaxEarlyDataSize(), fizz::LoggingKeyScheduler::getSecret(), and fizz::sm::handleCertMsg().

116  {
118  switch (s) {
120  label = kExternalPskBinder;
121  break;
123  label = kResumptionPskBinder;
124  break;
126  label = kClientEarlyTraffic;
127  break;
129  label = kEarlyExporter;
130  break;
131  default:
132  LOG(FATAL) << "unknown secret";
133  }
134 
135  auto& earlySecret = boost::get<EarlySecret>(*secret_);
136  return deriver_->deriveSecret(
137  folly::range(earlySecret.secret), label, transcript);
138 }
static constexpr StringPiece kExternalPskBinder
static constexpr StringPiece kEarlyExporter
std::unique_ptr< KeyDerivation > deriver_
Definition: KeyScheduler.h:134
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
static constexpr StringPiece kResumptionPskBinder
static set< string > s
static constexpr StringPiece kClientEarlyTraffic
Range< const char * > StringPiece
StringPiece label
std::vector< uint8_t > fizz::KeyScheduler::getSecret ( HandshakeSecrets  s,
folly::ByteRange  transcript 
) const
virtual

Reimplemented in fizz::LoggingKeyScheduler.

Definition at line 140 of file KeyScheduler.cpp.

References fizz::ClientHandshakeTraffic, deriver_, kClientHandshakeTraffic, kServerHandshakeTraffic, fizz::test::label, folly::range(), and fizz::ServerHandshakeTraffic.

142  {
144  switch (s) {
146  label = kClientHandshakeTraffic;
147  break;
149  label = kServerHandshakeTraffic;
150  break;
151  default:
152  LOG(FATAL) << "unknown secret";
153  }
154 
155  auto& handshakeSecret = boost::get<HandshakeSecret>(*secret_);
156  return deriver_->deriveSecret(
157  folly::range(handshakeSecret.secret), label, transcript);
158 }
static constexpr StringPiece kClientHandshakeTraffic
std::unique_ptr< KeyDerivation > deriver_
Definition: KeyScheduler.h:134
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
static set< string > s
Range< const char * > StringPiece
StringPiece label
static constexpr StringPiece kServerHandshakeTraffic
std::vector< uint8_t > fizz::KeyScheduler::getSecret ( MasterSecrets  s,
folly::ByteRange  transcript 
) const
virtual

Definition at line 160 of file KeyScheduler.cpp.

References deriver_, fizz::ExporterMaster, kExporterMaster, kResumptionMaster, fizz::test::label, folly::range(), and fizz::ResumptionMaster.

162  {
164  switch (s) {
166  label = kExporterMaster;
167  break;
169  label = kResumptionMaster;
170  break;
171  default:
172  LOG(FATAL) << "unknown secret";
173  }
174 
175  auto& masterSecret = boost::get<MasterSecret>(*secret_);
176  return deriver_->deriveSecret(
177  folly::range(masterSecret.secret), label, transcript);
178 }
static constexpr StringPiece kExporterMaster
std::unique_ptr< KeyDerivation > deriver_
Definition: KeyScheduler.h:134
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
static constexpr StringPiece kResumptionMaster
static set< string > s
Range< const char * > StringPiece
StringPiece label
std::vector< uint8_t > fizz::KeyScheduler::getSecret ( AppTrafficSecrets  s) const
virtual

Reimplemented in fizz::LoggingKeyScheduler.

Definition at line 180 of file KeyScheduler.cpp.

References appTrafficSecret_, fizz::ClientAppTraffic, and fizz::ServerAppTraffic.

180  {
181  auto& appTrafficSecret = *appTrafficSecret_;
182  switch (s) {
184  return appTrafficSecret.client;
186  return appTrafficSecret.server;
187  default:
188  LOG(FATAL) << "unknown secret";
189  }
190 }
folly::Optional< AppTrafficSecret > appTrafficSecret_
Definition: KeyScheduler.h:132
static set< string > s
TrafficKey fizz::KeyScheduler::getTrafficKey ( folly::ByteRange  trafficSecret,
size_t  keyLength,
size_t  ivLength 
) const
virtual

Derive a traffic key and iv from a traffic secret.

Definition at line 192 of file KeyScheduler.cpp.

References folly::IOBuf::create(), deriver_, fizz::TrafficKey::iv, fizz::TrafficKey::key, kTrafficIv, and kTrafficKey.

Referenced by fizz::Protocol::setAead().

195  {
196  TrafficKey trafficKey;
197  trafficKey.key = deriver_->expandLabel(
198  trafficSecret, kTrafficKey, folly::IOBuf::create(0), keyLength);
199  trafficKey.iv = deriver_->expandLabel(
200  trafficSecret, kTrafficIv, folly::IOBuf::create(0), ivLength);
201  return trafficKey;
202 }
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
std::unique_ptr< KeyDerivation > deriver_
Definition: KeyScheduler.h:134
static constexpr StringPiece kTrafficKey
static constexpr StringPiece kTrafficIv
uint32_t fizz::KeyScheduler::serverKeyUpdate ( )
virtual

Performs a key update on the server traffic key. Traffic secrets must be derived.

Definition at line 102 of file KeyScheduler.cpp.

References appTrafficSecret_, folly::IOBuf::create(), deriver_, kTrafficKeyUpdate, and folly::range().

Referenced by fizz::sm::generateTicket(), and fizz::sm::getMaxEarlyDataSize().

102  {
103  auto& appTrafficSecret = *appTrafficSecret_;
104  auto buf = deriver_->expandLabel(
105  folly::range(appTrafficSecret.server),
108  deriver_->hashLength());
109  buf->coalesce();
110  appTrafficSecret.server = std::vector<uint8_t>(buf->data(), buf->tail());
111  return ++appTrafficSecret.serverGeneration;
112 }
folly::Optional< AppTrafficSecret > appTrafficSecret_
Definition: KeyScheduler.h:132
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
std::unique_ptr< KeyDerivation > deriver_
Definition: KeyScheduler.h:134
static constexpr StringPiece kTrafficKeyUpdate
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114

Member Data Documentation

folly::Optional<AppTrafficSecret> fizz::KeyScheduler::appTrafficSecret_
private
folly::Optional<boost::variant<EarlySecret, HandshakeSecret, MasterSecret> > fizz::KeyScheduler::secret_
private

Definition at line 131 of file KeyScheduler.h.

Referenced by deriveEarlySecret(), and deriveHandshakeSecret().


The documentation for this class was generated from the following files: