aboutsummaryrefslogtreecommitdiffstats
path: root/src/tls/hello_verify.cpp
blob: c735d99871f1bd686ebab20de7221985fe86c4c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* DTLS Hello Verify Request
* (C) 2012 Jack Lloyd
*
* Released under the terms of the Botan license
*/

#include <botan/internal/tls_messages.h>
#include <botan/lookup.h>
#include <memory>

namespace Botan {

namespace TLS {

Hello_Verify_Request::Hello_Verify_Request(const std::vector<byte>& buf)
   {
   if(buf.size() < 3)
      throw Decoding_Error("Hello verify request too small");

   if(buf[0] != 254 || (buf[1] != 255 && buf[1] != 253))
      throw Decoding_Error("Unknown version from server in hello verify request");

   m_cookie.resize(buf.size() - 2);
   copy_mem(&m_cookie[0], &buf[2], buf.size() - 2);
   }

Hello_Verify_Request::Hello_Verify_Request(const std::vector<byte>& client_hello_bits,
                                           const std::string& client_identity,
                                           const SymmetricKey& secret_key)
   {
   std::unique_ptr<MessageAuthenticationCode> hmac(get_mac("HMAC(SHA-256)"));
   hmac->set_key(secret_key);

   hmac->update_be(client_hello_bits.size());
   hmac->update(client_hello_bits);
   hmac->update_be(client_identity.size());
   hmac->update(client_identity);

   m_cookie = unlock(hmac->final());
   }

std::vector<byte> Hello_Verify_Request::serialize() const
   {
   /* DTLS 1.2 server implementations SHOULD use DTLS version 1.0
      regardless of the version of TLS that is expected to be
      negotiated (RFC 6347, section 4.2.1)
   */

   Protocol_Version format_version(Protocol_Version::DTLS_V10);

   std::vector<byte> bits;
   bits.push_back(format_version.major_version());
   bits.push_back(format_version.minor_version());
   bits += m_cookie;
   return bits;
   }

}

}