Coverage Report

Created: 2022-06-23 06:44

/src/botan/src/lib/tls/tls12/msg_hello_verify.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* DTLS Hello Verify Request
3
* (C) 2012 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#include <botan/tls_messages.h>
9
#include <botan/mac.h>
10
11
namespace Botan::TLS {
12
13
Hello_Verify_Request::Hello_Verify_Request(const std::vector<uint8_t>& buf)
14
0
   {
15
0
   if(buf.size() < 3)
16
0
      throw Decoding_Error("Hello verify request too small");
17
18
0
   Protocol_Version version(buf[0], buf[1]);
19
20
0
   if(!version.is_datagram_protocol())
21
0
      {
22
0
      throw Decoding_Error("Unknown version from server in hello verify request");
23
0
      }
24
25
0
   if(static_cast<size_t>(buf[2]) + 3 != buf.size())
26
0
      throw Decoding_Error("Bad length in hello verify request");
27
28
0
   m_cookie.assign(buf.begin() + 3, buf.end());
29
0
   }
30
31
Hello_Verify_Request::Hello_Verify_Request(const std::vector<uint8_t>& client_hello_bits,
32
                                           const std::string& client_identity,
33
                                           const SymmetricKey& secret_key)
34
1.17k
   {
35
1.17k
   std::unique_ptr<MessageAuthenticationCode> hmac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
36
1.17k
   hmac->set_key(secret_key);
37
38
1.17k
   hmac->update_be(static_cast<uint64_t>(client_hello_bits.size()));
39
1.17k
   hmac->update(client_hello_bits);
40
1.17k
   hmac->update_be(static_cast<uint64_t>(client_identity.size()));
41
1.17k
   hmac->update(client_identity);
42
43
1.17k
   m_cookie.resize(hmac->output_length());
44
1.17k
   hmac->final(m_cookie.data());
45
1.17k
   }
46
47
std::vector<uint8_t> Hello_Verify_Request::serialize() const
48
1.17k
   {
49
   /* DTLS 1.2 server implementations SHOULD use DTLS version 1.0
50
      regardless of the version of TLS that is expected to be
51
      negotiated (RFC 6347, section 4.2.1)
52
   */
53
54
1.17k
   Protocol_Version format_version(254, 255); // DTLS 1.0
55
56
1.17k
   std::vector<uint8_t> bits;
57
1.17k
   bits.push_back(format_version.major_version());
58
1.17k
   bits.push_back(format_version.minor_version());
59
1.17k
   bits.push_back(static_cast<uint8_t>(m_cookie.size()));
60
1.17k
   bits += m_cookie;
61
1.17k
   return bits;
62
1.17k
   }
63
64
}