proxygen
CookieCipher.cpp
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 
10 
12 #include <fizz/record/Extensions.h>
13 #include <fizz/server/Negotiator.h>
14 
15 namespace fizz {
16 namespace server {
17 
22  Buf cookie) {
23  Buf encodedHelloRetryRequest;
24 
28  hrr.cipher_suite = cipher;
29 
30  ServerSupportedVersions versionExt;
31  versionExt.selected_version = version;
32  hrr.extensions.push_back(encodeExtension(std::move(versionExt)));
33 
34  if (group) {
36  keyShare.selected_group = *group;
37  hrr.extensions.push_back(encodeExtension(std::move(keyShare)));
38  }
39 
40  Cookie cookieExt;
41  cookieExt.cookie = std::move(cookie);
42  hrr.extensions.push_back(encodeExtension(std::move(cookieExt)));
43 
44  return encodeHandshake(std::move(hrr));
45 }
46 
48  const std::vector<NamedGroup>& supportedGroups,
49  const ClientHello& chlo) {
50  auto groupsExt = getExtension<SupportedGroups>(chlo.extensions);
51  if (!groupsExt) {
52  return folly::none;
53  }
54 
55  // Group is negotiated solely based on supported groups, without considering
56  // which shares were sent.
57  auto negotiatedGroup =
58  negotiate(supportedGroups, groupsExt->named_group_list);
59  if (!negotiatedGroup) {
60  // We will deal with any supported group mismatch at the full handshake.
61  return folly::none;
62  }
63 
64  auto clientShares = getExtension<ClientKeyShare>(chlo.extensions);
65  if (!clientShares) {
66  throw std::runtime_error("supported_groups without key_share");
67  }
68 
69  for (const auto& share : clientShares->client_shares) {
70  if (share.group == *negotiatedGroup) {
71  // We already have the right key share.
72  return folly::none;
73  }
74  }
75 
76  return negotiatedGroup;
77 }
78 
80  const Factory& factory,
81  const std::vector<ProtocolVersion>& supportedVersions,
82  const std::vector<std::vector<CipherSuite>>& supportedCiphers,
83  const std::vector<NamedGroup>& supportedGroups,
84  const ClientHello& chlo,
85  Buf appToken) {
86  auto clientVersions = getExtension<SupportedVersions>(chlo.extensions);
87  if (!clientVersions) {
88  throw std::runtime_error("no supported versions");
89  }
90  auto version = negotiate(supportedVersions, clientVersions->versions);
91  if (!version) {
92  throw std::runtime_error("version mismatch");
93  }
94 
95  auto cipher = negotiate(supportedCiphers, chlo.cipher_suites);
96  if (!cipher) {
97  throw std::runtime_error("cipher mismatch");
98  }
99 
100  auto group = getHrrGroup(supportedGroups, chlo);
101 
103  state.version = *version;
104  state.cipher = *cipher;
105  state.group = group;
106  state.appToken = std::move(appToken);
107 
108  auto handshakeContext = factory.makeHandshakeContext(*cipher);
109  handshakeContext->appendToTranscript(*chlo.originalEncoding);
110  state.chloHash = handshakeContext->getHandshakeContext();
111 
112  return state;
113 }
114 } // namespace server
115 } // namespace fizz
std::vector< Extension > extensions
Definition: Types.h:218
Buf getStatelessHelloRetryRequest(ProtocolVersion version, CipherSuite cipher, folly::Optional< NamedGroup > group, Buf cookie)
Buf encodeHandshake(T &&handshakeMsg)
Definition: Types-inl.h:515
static const std::string chlo
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
PUSHMI_INLINE_VAR constexpr detail::share_fn< TN... > share
Definition: share.h:53
CookieState getCookieState(const Factory &factory, const std::vector< ProtocolVersion > &supportedVersions, const std::vector< std::vector< CipherSuite >> &supportedCiphers, const std::vector< NamedGroup > &supportedGroups, const ClientHello &chlo, Buf appToken)
folly::Optional< T > negotiate(const std::vector< std::vector< T >> &serverPref, const std::vector< T > &clientPref)
Definition: Negotiator.h:22
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
CipherSuite
Definition: Types.h:153
static folly::Optional< NamedGroup > getHrrGroup(const std::vector< NamedGroup > &supportedGroups, const ClientHello &chlo)
CipherSuite cipher
ProtocolVersion
Definition: Types.h:24
ProtocolVersion version
ProtocolVersion selected_version
Definition: Extensions.h:99
Definition: Actions.h:16
virtual std::unique_ptr< HandshakeContext > makeHandshakeContext(CipherSuite cipher) const
Definition: Factory.h:76
std::vector< Extension > extensions
Definition: Types.h:193
std::vector< CipherSuite > cipher_suites
Definition: Types.h:191
ProtocolVersion legacy_version
Definition: Types.h:210
ProtocolVersion version
Definition: CookieCipher.h:18
Optional< NamedGroup > group
folly::Optional< Buf > originalEncoding
Definition: Types.h:92
std::unique_ptr< folly::IOBuf > Buf
Definition: Types.h:22
CipherSuite cipher_suite
Definition: Types.h:216
folly::Optional< NamedGroup > group
Definition: CookieCipher.h:20
Extension encodeExtension(const TokenBindingParameters &params)
Definition: Types.cpp:113
state
Definition: http_parser.c:272
constexpr None none
Definition: Optional.h:87