• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5230455705

10 Jun 2023 02:30PM UTC coverage: 91.715% (-0.03%) from 91.746%
5230455705

push

github

randombit
Merge GH #3584 Change clang-format AllowShortFunctionsOnASingleLine config from All to Inline

77182 of 84154 relevant lines covered (91.72%)

11975295.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.83
/src/lib/tls/tls13/tls_transcript_hash_13.cpp
1
/*
2
* TLS transcript hash implementation for TLS 1.3
3
* (C) 2022 Jack Lloyd
4
*     2022 Hannes Rantzsch, René Meusel - neXenio GmbH
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8

9
#include <botan/internal/tls_transcript_hash_13.h>
10

11
#include <botan/tls_exceptn.h>
12
#include <botan/tls_messages.h>
13
#include <botan/internal/tls_reader.h>
14

15
#include <utility>
16

17
namespace Botan::TLS {
18

19
Transcript_Hash_State::Transcript_Hash_State(std::string_view algo_spec) {
132✔
20
   set_algorithm(algo_spec);
132✔
21
}
132✔
22

23
Transcript_Hash_State::Transcript_Hash_State(const Transcript_Hash_State& other) :
102✔
24
      m_hash((other.m_hash != nullptr) ? other.m_hash->copy_state() : nullptr),
102✔
25
      m_unprocessed_transcript(other.m_unprocessed_transcript),
102✔
26
      m_current(other.m_current),
102✔
27
      m_previous(other.m_previous),
102✔
28
      m_truncated(other.m_truncated) {}
102✔
29

30
Transcript_Hash_State Transcript_Hash_State::recreate_after_hello_retry_request(
71✔
31
   std::string_view algo_spec, const Transcript_Hash_State& prev_transcript_hash_state) {
32
   // make sure that we have seen exactly 'client_hello' and 'hello_retry_request'
33
   // before re-creating the transcript hash state
34
   BOTAN_STATE_CHECK(prev_transcript_hash_state.m_hash == nullptr);
71✔
35
   BOTAN_STATE_CHECK(prev_transcript_hash_state.m_unprocessed_transcript.size() == 2);
71✔
36

37
   Transcript_Hash_State ths(algo_spec);
71✔
38

39
   const auto& client_hello_1 = prev_transcript_hash_state.m_unprocessed_transcript.front();
71✔
40
   const auto& hello_retry_request = prev_transcript_hash_state.m_unprocessed_transcript.back();
71✔
41

42
   const size_t hash_length = ths.m_hash->output_length();
71✔
43
   BOTAN_ASSERT_NOMSG(hash_length < 256);
71✔
44

45
   // RFC 8446 4.4.1
46
   //    [...], when the server responds to a ClientHello with a HelloRetryRequest,
47
   //    the value of ClientHello1 is replaced with a special synthetic handshake
48
   //    message of handshake type "message_hash" [(0xFE)] containing:
49
   std::vector<uint8_t> message_hash;
71✔
50
   message_hash.reserve(4 + hash_length);
71✔
51
   message_hash.push_back(0xFE /* message type 'message_hash' RFC 8446 4. */);
71✔
52
   message_hash.push_back(0x00);
71✔
53
   message_hash.push_back(0x00);
71✔
54
   message_hash.push_back(static_cast<uint8_t>(hash_length));
71✔
55
   message_hash += ths.m_hash->process(client_hello_1);
142✔
56

57
   ths.update(message_hash);
71✔
58
   ths.update(hello_retry_request);
71✔
59

60
   return ths;
71✔
61
}
71✔
62

63
namespace {
64

65
// TODO: This is a massive code duplication of the client hello parsing code,
66
//       as well as basic parsing of extensions. We should resolve this.
67
//
68
// Ad-hoc idea: When parsing the production objects, we could keep markers into
69
//              the original buffer. E.g. the PSK extensions would keep its off-
70
//              set into the entire client hello buffer. Using that offset we
71
//              could quickly identify the offset of the binders list slice the
72
//              buffer without re-parsing it.
73
//
74
// Finds the truncation offset in a serialization of Client Hello as defined in
75
// RFC 8446 4.2.11.2 used for the calculation of PSK binder MACs.
76
size_t find_client_hello_truncation_mark(std::span<const uint8_t> client_hello) {
888✔
77
   TLS_Data_Reader reader("Client Hello Truncation", client_hello);
888✔
78

79
   // handshake message type
80
   BOTAN_ASSERT_NOMSG(reader.get_byte() == static_cast<uint8_t>(Handshake_Type::ClientHello));
888✔
81

82
   // message length
83
   reader.discard_next(3);
888✔
84

85
   // legacy version
86
   reader.discard_next(2);
888✔
87

88
   // random
89
   reader.discard_next(32);
888✔
90

91
   // session ID
92
   const auto session_id_length = reader.get_byte();
888✔
93
   reader.discard_next(session_id_length);
888✔
94

95
   // TODO: DTLS contains a hello_cookie in this location
96
   //       Currently we don't support DTLS 1.3
97

98
   // cipher suites
99
   const auto ciphersuites_length = reader.get_uint16_t();
888✔
100
   reader.discard_next(ciphersuites_length);
888✔
101

102
   // compression methods
103
   const auto compression_methods_length = reader.get_byte();
888✔
104
   reader.discard_next(compression_methods_length);
888✔
105

106
   // extensions
107
   const auto extensions_length = reader.get_uint16_t();
888✔
108
   const auto extensions_offset = reader.read_so_far();
888✔
109
   while(reader.has_remaining() && reader.read_so_far() - extensions_offset < extensions_length) {
11,243✔
110
      const auto ext_type = static_cast<Extension_Code>(reader.get_uint16_t());
10,622✔
111
      const auto ext_length = reader.get_uint16_t();
10,622✔
112

113
      // skip over all extensions, finding the PSK extension to be truncated
114
      if(ext_type != Extension_Code::PresharedKey) {
10,622✔
115
         reader.discard_next(ext_length);
10,355✔
116
         continue;
10,355✔
117
      }
118

119
      // PSK identities list
120
      const auto identities_length = reader.get_uint16_t();
267✔
121
      reader.discard_next(identities_length);
267✔
122

123
      // check that only the binders are left in the buffer...
124
      const auto binders_length = reader.peek_uint16_t();
267✔
125
      if(binders_length != reader.remaining_bytes() - 2 /* binders_length */) {
267✔
126
         throw TLS_Exception(Alert::IllegalParameter,
×
127
                             "Failed to truncate Client Hello that doesn't end on the PSK binders list");
×
128
      }
129

130
      // the reader now points to the truncation point
131
      break;
132
   }
133

134
   // if no PSK extension was found, this will point to the end of the buffer
135
   return reader.read_so_far();
888✔
136
}
137

138
std::vector<uint8_t> read_hash_state(std::unique_ptr<HashFunction>& hash) {
5,572✔
139
   // Botan does not support finalizing a HashFunction without resetting
140
   // the internal state of the hash. Hence we first copy the internal
141
   // state and then finalize the transient HashFunction.
142
   return hash->copy_state()->final_stdvec();
11,144✔
143
}
144

145
}  // namespace
146

147
void Transcript_Hash_State::update(std::span<const uint8_t> serialized_message_s) {
8,163✔
148
   auto serialized_message = serialized_message_s.data();
8,163✔
149
   auto serialized_message_length = serialized_message_s.size();
8,163✔
150
   if(m_hash != nullptr) {
8,163✔
151
      auto truncation_mark = serialized_message_length;
5,305✔
152

153
      // Check whether we should generate a truncated hash for supporting PSK
154
      // binder calculation or verification. See RFC 8446 4.2.11.2.
155
      if(serialized_message_length > 0 && *serialized_message == static_cast<uint8_t>(Handshake_Type::ClientHello)) {
5,305✔
156
         truncation_mark = find_client_hello_truncation_mark(serialized_message_s);
888✔
157
      }
158

159
      if(truncation_mark < serialized_message_length) {
5,305✔
160
         m_hash->update(serialized_message, truncation_mark);
267✔
161
         m_truncated = read_hash_state(m_hash);
267✔
162
         m_hash->update(serialized_message + truncation_mark, serialized_message_length - truncation_mark);
267✔
163
      } else {
164
         m_truncated.clear();
5,038✔
165
         m_hash->update(serialized_message, serialized_message_length);
5,038✔
166
      }
167

168
      m_previous = std::exchange(m_current, read_hash_state(m_hash));
5,305✔
169
   } else {
170
      m_unprocessed_transcript.push_back(
5,716✔
171
         std::vector(serialized_message, serialized_message + serialized_message_length));
5,716✔
172
   }
173
}
8,163✔
174

175
const Transcript_Hash& Transcript_Hash_State::current() const {
2,943✔
176
   BOTAN_STATE_CHECK(!m_current.empty());
2,943✔
177
   return m_current;
2,930✔
178
}
179

180
const Transcript_Hash& Transcript_Hash_State::previous() const {
1,077✔
181
   BOTAN_STATE_CHECK(!m_previous.empty());
1,077✔
182
   return m_previous;
1,073✔
183
}
184

185
const Transcript_Hash& Transcript_Hash_State::truncated() const {
188✔
186
   BOTAN_STATE_CHECK(!m_truncated.empty());
188✔
187
   return m_truncated;
187✔
188
}
189

190
void Transcript_Hash_State::set_algorithm(std::string_view algo_spec) {
1,009✔
191
   BOTAN_STATE_CHECK(m_hash == nullptr || m_hash->name() == algo_spec);
1,139✔
192
   if(m_hash != nullptr) {
1,007✔
193
      return;
194
   }
195

196
   m_hash = HashFunction::create_or_throw(algo_spec);
944✔
197
   for(const auto& msg : m_unprocessed_transcript) {
2,153✔
198
      update(msg);
1,209✔
199
   }
200
   m_unprocessed_transcript.clear();
944✔
201
}
202

203
Transcript_Hash_State Transcript_Hash_State::clone() const {
102✔
204
   return *this;
102✔
205
}
206

207
}  // namespace Botan::TLS
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc