aboutsummaryrefslogtreecommitdiffstats
path: root/src/tls/finished.cpp
blob: 140a3cb59da46e508ca6b7c4c86a980bdac4bc99 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Finished Message
* (C) 2004-2006,2012 Jack Lloyd
*
* Released under the terms of the Botan license
*/

#include <botan/internal/tls_messages.h>
#include <botan/prf_tls.h>
#include <botan/hmac.h>
#include <botan/sha2_32.h>
#include <memory>

namespace Botan {

namespace TLS {

namespace {

KDF* choose_tls_prf(Protocol_Version version)
   {
   if(version == Protocol_Version::TLS_V10 || version == Protocol_Version::TLS_V11)
      return new TLS_PRF;
   else if(version == Protocol_Version::TLS_V12)
      return new TLS_12_PRF(new HMAC(new SHA_256)); // might depend on ciphersuite
   else
      throw TLS_Exception(Alert::PROTOCOL_VERSION,
                          "Unknown version for PRF");
   }

/*
* Compute the verify_data
*/
MemoryVector<byte> finished_compute_verify(Handshake_State* state,
                                           Connection_Side side)
   {
   if(state->version == Protocol_Version::SSL_V3)
      {
      const byte SSL_CLIENT_LABEL[] = { 0x43, 0x4C, 0x4E, 0x54 };
      const byte SSL_SERVER_LABEL[] = { 0x53, 0x52, 0x56, 0x52 };

      Handshake_Hash hash = state->hash; // don't modify state

      MemoryVector<byte> ssl3_finished;

      if(side == CLIENT)
         hash.update(SSL_CLIENT_LABEL, sizeof(SSL_CLIENT_LABEL));
      else
         hash.update(SSL_SERVER_LABEL, sizeof(SSL_SERVER_LABEL));

      return hash.final_ssl3(state->keys.master_secret());
      }
   else
      {
      const byte TLS_CLIENT_LABEL[] = {
         0x63, 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x66, 0x69, 0x6E, 0x69,
         0x73, 0x68, 0x65, 0x64 };

      const byte TLS_SERVER_LABEL[] = {
         0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x66, 0x69, 0x6E, 0x69,
         0x73, 0x68, 0x65, 0x64 };

      std::auto_ptr<KDF> prf(choose_tls_prf(state->version));

      MemoryVector<byte> input;
      if(side == CLIENT)
         input += std::make_pair(TLS_CLIENT_LABEL, sizeof(TLS_CLIENT_LABEL));
      else
         input += std::make_pair(TLS_SERVER_LABEL, sizeof(TLS_SERVER_LABEL));

      input += state->hash.final(state->version);

      return prf->derive_key(12, state->keys.master_secret(), input);
      }
   }

}

/*
* Create a new Finished message
*/
Finished::Finished(Record_Writer& writer,
                   Handshake_State* state,
                   Connection_Side side)
   {
   verification_data = finished_compute_verify(state, side);
   send(writer, state->hash);
   }

/*
* Serialize a Finished message
*/
MemoryVector<byte> Finished::serialize() const
   {
   return verification_data;
   }

/*
* Deserialize a Finished message
*/
Finished::Finished(const MemoryRegion<byte>& buf)
   {
   verification_data = buf;
   }

/*
* Verify a Finished message
*/
bool Finished::verify(Handshake_State* state,
                      Connection_Side side)
   {
   return (verification_data == finished_compute_verify(state, side));
   }

}

}