aboutsummaryrefslogtreecommitdiffstats
path: root/src/tls/tls_record.cpp
blob: 58aa5bfc9642e478d98135b009adefd8129d7671 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* TLS Record Handling
* (C) 2012 Jack Lloyd
*
* Released under the terms of the Botan license
*/

#include <botan/tls_record.h>
#include <botan/internal/tls_session_key.h>
#include <botan/libstate.h>
#include <botan/internal/rounding.h>
#include <botan/internal/assert.h>
#include <botan/internal/xor_buf.h>

namespace Botan {

namespace TLS {

Connection_Cipher_State::Connection_Cipher_State(
   Protocol_Version version,
   Connection_Side side,
   const Ciphersuite& suite,
   const Session_Keys& keys)
   {
   SymmetricKey mac_key, cipher_key;
   InitializationVector iv;

   if(side == CLIENT)
      {
      cipher_key = keys.client_cipher_key();
      iv = keys.client_iv();
      mac_key = keys.client_mac_key();
      }
   else
      {
      cipher_key = keys.server_cipher_key();
      iv = keys.server_iv();
      mac_key = keys.server_mac_key();
      }

   const std::string cipher_algo = suite.cipher_algo();
   const std::string mac_algo = suite.mac_algo();

   Algorithm_Factory& af = global_state().algorithm_factory();

   if(const BlockCipher* bc = af.prototype_block_cipher(cipher_algo))
      {
      m_block_cipher.reset(bc->clone());
      m_block_cipher->set_key(cipher_key);
      m_block_cipher_cbc_state = iv.bits_of();
      m_block_size = bc->block_size();

      if(version.supports_explicit_cbc_ivs())
         m_iv_size = m_block_size;
      else
         m_iv_size = 0;
      }
   else if(const StreamCipher* sc = af.prototype_stream_cipher(cipher_algo))
      {
      m_stream_cipher.reset(sc->clone());
      m_stream_cipher->set_key(cipher_key);
      m_block_size = 0;
      m_iv_size = 0;
      }
   else
      throw Invalid_Argument("Unknown TLS cipher " + cipher_algo);

   if(version == Protocol_Version::SSL_V3)
      m_mac.reset(af.make_mac("SSL3-MAC(" + mac_algo + ")"));
   else
      m_mac.reset(af.make_mac("HMAC(" + mac_algo + ")"));

   m_mac->set_key(mac_key);
   }

size_t write_record(std::vector<byte>& output,
                    byte msg_type, const byte msg[], size_t msg_length,
                    u64bit msg_sequence_number,
                    Protocol_Version version,
                    Connection_Cipher_State* cipherstate,
                    RandomNumberGenerator& rng)
   {
   BOTAN_ASSERT(output.size() >= TLS_HEADER_SIZE + msg_length,
                "Write buffer is big enough");

   output[0] = msg_type;
   output[1] = version.major_version();
   output[2] = version.minor_version();

   if(!cipherstate) // initial unencrypted handshake records
      {
      output[3] = get_byte<u16bit>(0, msg_length);
      output[4] = get_byte<u16bit>(1, msg_length);

      copy_mem(&output[TLS_HEADER_SIZE], msg, msg_length);

      return (TLS_HEADER_SIZE + msg_length);
      }

   cipherstate->mac()->update_be(msg_sequence_number);
   cipherstate->mac()->update(msg_type);

   if(version != Protocol_Version::SSL_V3)
      {
      cipherstate->mac()->update(version.major_version());
      cipherstate->mac()->update(version.minor_version());
      }

   cipherstate->mac()->update(get_byte<u16bit>(0, msg_length));
   cipherstate->mac()->update(get_byte<u16bit>(1, msg_length));
   cipherstate->mac()->update(msg, msg_length);

   const size_t block_size = cipherstate->block_size();
   const size_t iv_size = cipherstate->iv_size();
   const size_t mac_size = cipherstate->mac_size();

   const size_t buf_size = round_up(
      iv_size + msg_length + mac_size + (block_size ? 1 : 0),
      block_size);

   if(buf_size >= MAX_CIPHERTEXT_SIZE)
      throw Internal_Error("Record_Writer: Record is too big");

   BOTAN_ASSERT(output.size() >= TLS_HEADER_SIZE + MAX_CIPHERTEXT_SIZE,
                "Write buffer is big enough");

   output[3] = get_byte<u16bit>(0, buf_size);
   output[4] = get_byte<u16bit>(1, buf_size);

   byte* buf_write_ptr = &output[TLS_HEADER_SIZE];

   if(iv_size)
      {
      rng.randomize(buf_write_ptr, iv_size);
      buf_write_ptr += iv_size;
      }

   copy_mem(buf_write_ptr, msg, msg_length);
   buf_write_ptr += msg_length;

   cipherstate->mac()->final(buf_write_ptr);
   buf_write_ptr += mac_size;

   if(block_size)
      {
      const size_t pad_val =
         buf_size - (iv_size + msg_length + mac_size + 1);

      for(size_t i = 0; i != pad_val + 1; ++i)
         {
         *buf_write_ptr = pad_val;
         buf_write_ptr += 1;
         }
      }

   if(buf_size > MAX_CIPHERTEXT_SIZE)
      throw Internal_Error("Produced ciphertext larger than protocol allows");

   if(StreamCipher* sc = cipherstate->stream_cipher())
      {
      sc->cipher1(&output[TLS_HEADER_SIZE], buf_size);
      }
   else if(BlockCipher* bc = cipherstate->block_cipher())
      {
      secure_vector<byte>& cbc_state = cipherstate->cbc_state();

      BOTAN_ASSERT(buf_size % block_size == 0,
                   "Buffer is an even multiple of block size");

      byte* buf = &output[TLS_HEADER_SIZE];

      const size_t blocks = buf_size / block_size;

      xor_buf(&buf[0], &cbc_state[0], block_size);
      bc->encrypt(&buf[0]);

      for(size_t i = 1; i <= blocks; ++i)
         {
         xor_buf(&buf[block_size*i], &buf[block_size*(i-1)], block_size);
         bc->encrypt(&buf[block_size*i]);
         }

      cbc_state.assign(&buf[block_size*(blocks-1)],
                       &buf[block_size*blocks]);
      }
   else
      throw Internal_Error("NULL cipher not supported");

   return (TLS_HEADER_SIZE + buf_size);
   }

}

}