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
|
/*
* TLS Channels
* (C) 2011 Jack Lloyd
*
* Released under the terms of the Botan license
*/
#include <botan/tls_channel.h>
#include <botan/internal/tls_alerts.h>
#include <botan/internal/tls_state.h>
#include <botan/loadstor.h>
namespace Botan {
TLS_Channel::TLS_Channel(std::tr1::function<void (const byte[], size_t)> socket_output_fn,
std::tr1::function<void (const byte[], size_t, u16bit)> proc_fn,
std::tr1::function<void (const TLS_Session_Params&)> handshake_complete) :
proc_fn(proc_fn),
handshake_fn(handshake_complete),
writer(socket_output_fn),
state(0),
handshake_completed(false),
connection_closed(false)
{
}
TLS_Channel::~TLS_Channel()
{
close();
delete state;
state = 0;
}
Version_Code TLS_Channel::protocol_version() const
{
if(!handshake_completed)
throw std::logic_error("Version not known until handshake complete");
return writer.get_version();
}
size_t TLS_Channel::received_data(const byte buf[], size_t buf_size)
{
try
{
reader.add_input(buf, buf_size);
byte rec_type = CONNECTION_CLOSED;
SecureVector<byte> record;
while(!reader.currently_empty())
{
const size_t bytes_needed = reader.get_record(rec_type, record);
if(bytes_needed > 0)
return bytes_needed;
if(rec_type == APPLICATION_DATA)
{
if(handshake_completed)
{
/*
* OpenSSL among others sends empty records in versions
* before TLS v1.1 in order to randomize the IV of the
* following record. Avoid spurious callbacks.
*/
if(record.size() > 0)
proc_fn(&record[0], record.size(), NULL_ALERT);
}
else
{
throw Unexpected_Message("Application data before handshake done");
}
}
else if(rec_type == HANDSHAKE || rec_type == CHANGE_CIPHER_SPEC)
{
read_handshake(rec_type, record);
}
else if(rec_type == ALERT)
{
Alert alert_msg(record);
proc_fn(0, 0, alert_msg.type());
if(!connection_closed)
{
if(alert_msg.is_fatal() || alert_msg.type() == CLOSE_NOTIFY)
{
if(alert_msg.type() == CLOSE_NOTIFY)
alert(FATAL, CLOSE_NOTIFY);
else
alert(FATAL, NULL_ALERT);
}
}
}
else
throw Unexpected_Message("Unknown message type received");
}
return 0; // on a record boundary
}
catch(TLS_Exception& e)
{
alert(FATAL, e.type());
throw;
}
catch(Decoding_Error& e)
{
alert(FATAL, DECODE_ERROR);
throw;
}
catch(std::exception& e)
{
alert(FATAL, INTERNAL_ERROR);
throw;
}
}
/*
* Split up and process handshake messages
*/
void TLS_Channel::read_handshake(byte rec_type,
const MemoryRegion<byte>& rec_buf)
{
if(rec_type == HANDSHAKE)
{
if(!state)
state = new Handshake_State;
state->queue.write(&rec_buf[0], rec_buf.size());
}
while(true)
{
Handshake_Type type = HANDSHAKE_NONE;
SecureVector<byte> contents;
if(rec_type == HANDSHAKE)
{
if(state->queue.size() >= 4)
{
byte head[4] = { 0 };
state->queue.peek(head, 4);
const size_t length = make_u32bit(0, head[1], head[2], head[3]);
if(state->queue.size() >= length + 4)
{
type = static_cast<Handshake_Type>(head[0]);
contents.resize(length);
state->queue.read(head, 4);
state->queue.read(&contents[0], contents.size());
}
}
}
else if(rec_type == CHANGE_CIPHER_SPEC)
{
if(state->queue.size() == 0 && rec_buf.size() == 1 && rec_buf[0] == 1)
type = HANDSHAKE_CCS;
else
throw Decoding_Error("Malformed ChangeCipherSpec message");
}
else
throw Decoding_Error("Unknown message type in handshake processing");
if(type == HANDSHAKE_NONE)
break;
process_handshake_msg(type, contents);
if(type == HANDSHAKE_CCS || !state)
break;
}
}
void TLS_Channel::queue_for_sending(const byte buf[], size_t buf_size)
{
if(handshake_completed)
{
while(!pre_handshake_write_queue.end_of_data())
{
SecureVector<byte> q_buf(1024);
const size_t got = pre_handshake_write_queue.read(&q_buf[0], q_buf.size());
writer.send(APPLICATION_DATA, &q_buf[0], got);
}
writer.send(APPLICATION_DATA, buf, buf_size);
}
else
pre_handshake_write_queue.write(buf, buf_size);
}
void TLS_Channel::alert(Alert_Level alert_level, Alert_Type alert_code)
{
if(alert_code != NULL_ALERT && !connection_closed)
{
try
{
writer.alert(alert_level, alert_code);
}
catch(...) { /* swallow it */ }
}
if(!connection_closed &&
(alert_code == CLOSE_NOTIFY || alert_level == FATAL))
{
connection_closed = true;
delete state;
state = 0;
reader.reset();
writer.reset();
}
}
void TLS_Channel::Secure_Renegotiation_State::update(Client_Hello* client_hello)
{
if(initial_handshake)
{
secure_renegotiation = client_hello->secure_renegotiation();
}
else
{
if(secure_renegotiation != client_hello->secure_renegotiation())
throw TLS_Exception(HANDSHAKE_FAILURE,
"Client changed its mind about secure negotiation");
}
if(client_hello->secure_renegotiation())
{
const MemoryVector<byte>& data = client_hello->renegotiation_info();
if(initial_handshake)
{
if(!data.empty())
throw TLS_Exception(HANDSHAKE_FAILURE,
"Client sent renegotiation data on initial handshake");
}
else
{
if(data != for_client_hello())
throw TLS_Exception(HANDSHAKE_FAILURE,
"Client sent bad renegotiation data");
}
}
}
void TLS_Channel::Secure_Renegotiation_State::update(Server_Hello* server_hello)
{
if(initial_handshake)
{
/* If the client offered but server rejected, then this toggles
* secure_renegotiation to off
*/
secure_renegotiation = server_hello->secure_renegotiation();
}
else
{
if(secure_renegotiation != server_hello->secure_renegotiation())
throw TLS_Exception(HANDSHAKE_FAILURE,
"Server changed its mind about secure negotiation");
}
if(secure_renegotiation)
{
const MemoryVector<byte>& data = server_hello->renegotiation_info();
if(initial_handshake)
{
if(!data.empty())
throw TLS_Exception(HANDSHAKE_FAILURE,
"Server sent renegotiation data on initial handshake");
}
else
{
if(data != for_server_hello())
throw TLS_Exception(HANDSHAKE_FAILURE,
"Server sent bad renegotiation data");
}
}
initial_handshake = false;
}
void TLS_Channel::Secure_Renegotiation_State::update(Finished* client_finished,
Finished* server_finished)
{
client_verify = client_finished->verify_data();
server_verify = server_finished->verify_data();
}
}
|