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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
/*
* TLS Record Reading
* (C) 2004-2012 Jack Lloyd
*
* Released under the terms of the Botan license
*/
#include <botan/tls_record.h>
#include <botan/lookup.h>
#include <botan/loadstor.h>
#include <botan/internal/tls_session_key.h>
#include <botan/internal/rounding.h>
#include <botan/internal/assert.h>
namespace Botan {
Record_Reader::Record_Reader()
{
m_mac = 0;
reset();
set_maximum_fragment_size(0);
// A single record is never larger than this
m_readbuf.resize(MAX_CIPHERTEXT_SIZE);
}
/*
* Reset the state
*/
void Record_Reader::reset()
{
m_cipher.reset();
delete m_mac;
m_mac = 0;
zeroise(m_readbuf);
m_readbuf_pos = 0;
m_mac_size = 0;
m_block_size = 0;
m_iv_size = 0;
m_major = m_minor = 0;
m_seq_no = 0;
set_maximum_fragment_size(0);
}
void Record_Reader::set_maximum_fragment_size(size_t max_fragment)
{
if(max_fragment == 0)
m_max_fragment = MAX_PLAINTEXT_SIZE;
else
m_max_fragment = clamp(max_fragment, 128, MAX_PLAINTEXT_SIZE);
}
/*
* Set the version to use
*/
void Record_Reader::set_version(Version_Code version)
{
if(version != SSL_V3 && version != TLS_V10 && version != TLS_V11)
throw Invalid_Argument("Record_Reader: Invalid protocol version");
m_major = (version >> 8) & 0xFF;
m_minor = (version & 0xFF);
}
/*
* Get the version in use
*/
Version_Code Record_Reader::get_version() const
{
return static_cast<Version_Code>(
(static_cast<u16bit>(m_major) << 8) | m_minor);
}
/*
* Set the keys for reading
*/
void Record_Reader::activate(const TLS_Cipher_Suite& suite,
const SessionKeys& keys,
Connection_Side side)
{
m_cipher.reset();
delete m_mac;
m_mac = 0;
m_seq_no = 0;
SymmetricKey mac_key, cipher_key;
InitializationVector iv;
if(side == CLIENT)
{
cipher_key = keys.server_cipher_key();
iv = keys.server_iv();
mac_key = keys.server_mac_key();
}
else
{
cipher_key = keys.client_cipher_key();
iv = keys.client_iv();
mac_key = keys.client_mac_key();
}
const std::string cipher_algo = suite.cipher_algo();
const std::string mac_algo = suite.mac_algo();
if(have_block_cipher(cipher_algo))
{
m_cipher.append(get_cipher(
cipher_algo + "/CBC/NoPadding",
cipher_key, iv, DECRYPTION)
);
m_block_size = block_size_of(cipher_algo);
if(m_major > 3 || (m_major == 3 && m_minor >= 2))
m_iv_size = m_block_size;
else
m_iv_size = 0;
}
else if(have_stream_cipher(cipher_algo))
{
m_cipher.append(get_cipher(cipher_algo, cipher_key, DECRYPTION));
m_block_size = 0;
m_iv_size = 0;
}
else
throw Invalid_Argument("Record_Reader: Unknown cipher " + cipher_algo);
if(have_hash(mac_algo))
{
Algorithm_Factory& af = global_state().algorithm_factory();
if(m_major == 3 && m_minor == 0)
m_mac = af.make_mac("SSL3-MAC(" + mac_algo + ")");
else
m_mac = af.make_mac("HMAC(" + mac_algo + ")");
m_mac->set_key(mac_key);
m_mac_size = m_mac->output_length();
}
else
throw Invalid_Argument("Record_Reader: Unknown hash " + mac_algo);
}
void Record_Reader::consume_input(const byte*& input,
size_t& input_size,
size_t& input_consumed,
size_t desired)
{
const size_t space_available = (m_readbuf.size() - m_readbuf_pos);
const size_t taken = std::min(input_size, desired);
if(taken > space_available)
throw TLS_Exception(RECORD_OVERFLOW,
"Record is larger than allowed maximum size");
copy_mem(&m_readbuf[m_readbuf_pos], input, taken);
m_readbuf_pos += taken;
input_consumed += taken;
input_size -= taken;
input += taken;
}
/*
* Retrieve the next record
*/
size_t Record_Reader::add_input(const byte input_array[], size_t input_size,
size_t& input_consumed,
byte& msg_type,
MemoryVector<byte>& msg)
{
const byte* input = &input_array[0];
input_consumed = 0;
const size_t HEADER_SIZE = 5;
if(m_readbuf_pos < HEADER_SIZE) // header incomplete?
{
consume_input(input, input_size, input_consumed, HEADER_SIZE - m_readbuf_pos);
if(m_readbuf_pos < HEADER_SIZE)
return (HEADER_SIZE - m_readbuf_pos); // header still incomplete
BOTAN_ASSERT_EQUAL(m_readbuf_pos, HEADER_SIZE,
"Buffer error in SSL header");
}
// SSLv2-format client hello?
if(m_readbuf[0] & 0x80 && m_readbuf[2] == 1 && m_readbuf[3] >= 3)
{
size_t record_len = make_u16bit(m_readbuf[0], m_readbuf[1]) & 0x7FFF;
consume_input(input, input_size, input_consumed, (record_len + 2) - m_readbuf_pos);
if(m_readbuf_pos < (record_len + 2))
return ((record_len + 2) - m_readbuf_pos);
BOTAN_ASSERT_EQUAL(m_readbuf_pos, (record_len + 2),
"Buffer error in SSLv2 hello");
msg_type = HANDSHAKE;
msg.resize(record_len + 4);
// Fake v3-style handshake message wrapper
msg[0] = CLIENT_HELLO_SSLV2;
msg[1] = 0;
msg[2] = m_readbuf[0] & 0x7F;
msg[3] = m_readbuf[1];
copy_mem(&msg[4], &m_readbuf[2], m_readbuf_pos - 2);
m_readbuf_pos = 0;
return 0;
}
if(m_readbuf[0] != CHANGE_CIPHER_SPEC &&
m_readbuf[0] != ALERT &&
m_readbuf[0] != HANDSHAKE &&
m_readbuf[0] != APPLICATION_DATA)
{
throw TLS_Exception(UNEXPECTED_MESSAGE,
"Record_Reader: Unknown record type");
}
const u16bit version = make_u16bit(m_readbuf[1], m_readbuf[2]);
const u16bit record_len = make_u16bit(m_readbuf[3], m_readbuf[4]);
if(m_major && (m_readbuf[1] != m_major || m_readbuf[2] != m_minor))
throw TLS_Exception(PROTOCOL_VERSION,
"Record_Reader: Got unexpected version");
if(record_len > MAX_CIPHERTEXT_SIZE)
throw TLS_Exception(RECORD_OVERFLOW,
"Got message that exceeds maximum size");
consume_input(input, input_size, input_consumed,
(HEADER_SIZE + record_len) - m_readbuf_pos);
if(m_readbuf_pos < (HEADER_SIZE + record_len))
return ((HEADER_SIZE + record_len) - m_readbuf_pos);
BOTAN_ASSERT_EQUAL(HEADER_SIZE + record_len, m_readbuf_pos,
"Bad buffer handling in record body");
// Null mac means no encryption either, only valid during handshake
if(m_mac_size == 0)
{
if(m_readbuf[0] != CHANGE_CIPHER_SPEC &&
m_readbuf[0] != ALERT &&
m_readbuf[0] != HANDSHAKE)
{
throw TLS_Exception(DECODE_ERROR, "Invalid msg type received during handshake");
}
msg_type = m_readbuf[0];
msg.resize(record_len);
copy_mem(&msg[0], &m_readbuf[HEADER_SIZE], record_len);
m_readbuf_pos = 0;
return 0; // got a full record
}
// Otherwise, decrypt, check MAC, return plaintext
// FIXME: process in-place
m_cipher.process_msg(&m_readbuf[HEADER_SIZE], record_len);
size_t got_back = m_cipher.read(&m_readbuf[HEADER_SIZE], record_len, Pipe::LAST_MESSAGE);
BOTAN_ASSERT_EQUAL(got_back, record_len, "Cipher didn't decrypt full amount");
size_t pad_size = 0;
if(m_block_size)
{
byte pad_value = m_readbuf[HEADER_SIZE + (record_len-1)];
pad_size = pad_value + 1;
/*
* Check the padding; if it is wrong, then say we have 0 bytes of
* padding, which should ensure that the MAC check below does not
* suceed. This hides a timing channel.
*
* This particular countermeasure is recommended in the TLS 1.2
* spec (RFC 5246) in section 6.2.3.2
*/
if(version == SSL_V3)
{
if(pad_value > m_block_size)
pad_size = 0;
}
else
{
bool padding_good = true;
for(size_t i = 0; i != pad_size; ++i)
if(m_readbuf[HEADER_SIZE + (record_len-i-1)] != pad_value)
padding_good = false;
if(!padding_good)
pad_size = 0;
}
}
if(record_len < m_mac_size + pad_size + m_iv_size)
throw Decoding_Error("Record_Reader: Record truncated");
const u16bit plain_length = record_len - (m_mac_size + pad_size + m_iv_size);
if(plain_length > m_max_fragment)
throw TLS_Exception(RECORD_OVERFLOW, "Plaintext record is too large");
m_mac->update_be(m_seq_no);
m_mac->update(m_readbuf[0]); // msg_type
if(version != SSL_V3)
for(size_t i = 0; i != 2; ++i)
m_mac->update(get_byte(i, version));
m_mac->update_be(plain_length);
m_mac->update(&m_readbuf[HEADER_SIZE + m_iv_size], plain_length);
++m_seq_no;
MemoryVector<byte> computed_mac = m_mac->final();
if(computed_mac.size() != m_mac_size)
throw TLS_Exception(INTERNAL_ERROR,
"MAC produced value of unexpected size");
const size_t mac_offset = record_len - (m_mac_size + pad_size);
if(!same_mem(&m_readbuf[HEADER_SIZE + mac_offset], &computed_mac[0], m_mac_size))
throw TLS_Exception(BAD_RECORD_MAC, "Record_Reader: MAC failure");
msg_type = m_readbuf[0];
msg.resize(plain_length);
copy_mem(&msg[0], &m_readbuf[HEADER_SIZE + m_iv_size], plain_length);
m_readbuf_pos = 0;
return 0;
}
}
|