Botan 3.12.0
Crypto and TLS for C&
msg_hello_verify.cpp
Go to the documentation of this file.
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
10#include <botan/mac.h>
11#include <botan/internal/tls_reader.h>
12
13namespace Botan::TLS {
14
15Hello_Verify_Request::Hello_Verify_Request(const std::vector<uint8_t>& buf) {
16 if(buf.size() < 3) {
17 throw Decoding_Error("Hello verify request too small");
18 }
19
20 const Protocol_Version version(buf[0], buf[1]);
21
22 if(!version.is_datagram_protocol()) {
23 throw Decoding_Error("Unknown version from server in hello verify request");
24 }
25
26 if(static_cast<size_t>(buf[2]) + 3 != buf.size()) {
27 throw Decoding_Error("Bad length in hello verify request");
28 }
29
30 m_cookie.assign(buf.begin() + 3, buf.end());
31}
32
33Hello_Verify_Request::Hello_Verify_Request(const std::vector<uint8_t>& client_hello_bits,
34 std::string_view client_identity,
35 const SymmetricKey& secret_key) {
36 auto hmac = MessageAuthenticationCode::create_or_throw("HMAC(SHA-256)");
37 hmac->set_key(secret_key);
38
39 hmac->update_be(static_cast<uint64_t>(client_hello_bits.size()));
40 hmac->update(client_hello_bits);
41 hmac->update_be(static_cast<uint64_t>(client_identity.size()));
42 hmac->update(client_identity);
43
44 m_cookie.resize(hmac->output_length());
45 hmac->final(m_cookie.data());
46}
47
48std::vector<uint8_t> Hello_Verify_Request::serialize() const {
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 const Protocol_Version format_version(254, 255); // DTLS 1.0
55
56 std::vector<uint8_t> bits;
57 bits.push_back(format_version.major_version());
58 bits.push_back(format_version.minor_version());
59 append_tls_length_value(bits, m_cookie, 1);
60 return bits;
61}
62
63} // namespace Botan::TLS
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:147
Hello_Verify_Request(const std::vector< uint8_t > &buf)
std::vector< uint8_t > serialize() const override
uint8_t major_version() const
Definition tls_version.h:84
uint8_t minor_version() const
Definition tls_version.h:89
void append_tls_length_value(std::vector< uint8_t, Alloc > &buf, const T *vals, size_t vals_size, size_t tag_size)
Definition tls_reader.h:177
OctetString SymmetricKey
Definition symkey.h:140