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
|
/*
* TLS Messages
* (C) 2004-2010 Jack Lloyd
*
* Released under the terms of the Botan license
*/
#ifndef BOTAN_TLS_MESSAGES_H__
#define BOTAN_TLS_MESSAGES_H__
#include <botan/internal/tls_handshake_hash.h>
#include <botan/tls_policy.h>
#include <botan/tls_magic.h>
#include <botan/tls_suites.h>
#include <botan/bigint.h>
#include <botan/pkcs8.h>
#include <botan/x509cert.h>
#include <vector>
namespace Botan {
class Record_Writer;
class Record_Reader;
/**
* TLS Handshake Message Base Class
*/
class HandshakeMessage
{
public:
void send(Record_Writer&, TLS_Handshake_Hash&) const;
virtual Handshake_Type type() const = 0;
virtual ~HandshakeMessage() {}
private:
HandshakeMessage& operator=(const HandshakeMessage&) { return (*this); }
virtual MemoryVector<byte> serialize() const = 0;
virtual void deserialize(const MemoryRegion<byte>&) = 0;
};
/**
* Client Hello Message
*/
class Client_Hello : public HandshakeMessage
{
public:
Handshake_Type type() const { return CLIENT_HELLO; }
Version_Code version() const { return c_version; }
const MemoryVector<byte>& session_id() const { return sess_id; }
std::vector<byte> session_id_vector() const
{
std::vector<byte> v;
v.insert(v.begin(), &sess_id[0], &sess_id[sess_id.size()]);
return v;
}
std::vector<u16bit> ciphersuites() const { return suites; }
std::vector<byte> compression_methods() const { return comp_methods; }
const MemoryVector<byte>& random() const { return c_random; }
std::string sni_hostname() const { return requested_hostname; }
std::string srp_identifier() const { return requested_srp_id; }
bool offered_suite(u16bit) const;
Client_Hello(RandomNumberGenerator& rng,
Record_Writer&, const TLS_Policy&, TLS_Handshake_Hash&);
Client_Hello(const MemoryRegion<byte>& buf,
Handshake_Type type)
{
if(type == CLIENT_HELLO)
deserialize(buf);
else
deserialize_sslv2(buf);
}
private:
MemoryVector<byte> serialize() const;
void deserialize(const MemoryRegion<byte>&);
void deserialize_sslv2(const MemoryRegion<byte>&);
Version_Code c_version;
MemoryVector<byte> sess_id, c_random;
std::vector<u16bit> suites;
std::vector<byte> comp_methods;
std::string requested_hostname;
std::string requested_srp_id;
};
/**
* Client Key Exchange Message
*/
class Client_Key_Exchange : public HandshakeMessage
{
public:
Handshake_Type type() const { return CLIENT_KEX; }
SecureVector<byte> pre_master_secret() const;
SecureVector<byte> pre_master_secret(RandomNumberGenerator& rng,
const Private_Key* key,
Version_Code version);
Client_Key_Exchange(RandomNumberGenerator& rng,
Record_Writer& output,
TLS_Handshake_Hash& hash,
const Public_Key* my_key,
Version_Code using_version,
Version_Code pref_version);
Client_Key_Exchange(const MemoryRegion<byte>& buf,
const CipherSuite& suite,
Version_Code using_version);
private:
MemoryVector<byte> serialize() const;
void deserialize(const MemoryRegion<byte>&);
SecureVector<byte> key_material, pre_master;
bool include_length;
};
/**
* Certificate Message
*/
class Certificate : public HandshakeMessage
{
public:
Handshake_Type type() const { return CERTIFICATE; }
const std::vector<X509_Certificate>& cert_chain() const { return certs; }
size_t count() const { return certs.size(); }
bool empty() const { return certs.empty(); }
Certificate(Record_Writer&, const std::vector<X509_Certificate>&,
TLS_Handshake_Hash&);
Certificate(const MemoryRegion<byte>& buf) { deserialize(buf); }
private:
MemoryVector<byte> serialize() const;
void deserialize(const MemoryRegion<byte>&);
std::vector<X509_Certificate> certs;
};
/**
* Certificate Request Message
*/
class Certificate_Req : public HandshakeMessage
{
public:
Handshake_Type type() const { return CERTIFICATE_REQUEST; }
std::vector<Certificate_Type> acceptable_types() const { return types; }
std::vector<X509_DN> acceptable_CAs() const { return names; }
Certificate_Req(Record_Writer& writer,
TLS_Handshake_Hash& hash,
const std::vector<X509_Certificate>& allowed_cas,
const std::vector<Certificate_Type>& types =
std::vector<Certificate_Type>());
Certificate_Req(const MemoryRegion<byte>& buf) { deserialize(buf); }
private:
MemoryVector<byte> serialize() const;
void deserialize(const MemoryRegion<byte>&);
std::vector<X509_DN> names;
std::vector<Certificate_Type> types;
};
/**
* Certificate Verify Message
*/
class Certificate_Verify : public HandshakeMessage
{
public:
Handshake_Type type() const { return CERTIFICATE_VERIFY; }
/**
* Check the signature on a certificate verify message
* @param cert the purported certificate
* @param hash the running handshake message hash
* @param version the version number we negotiated
* @param master_secret the session key (only used if version is SSL_V3)
*/
bool verify(const X509_Certificate& cert,
TLS_Handshake_Hash& hash,
Version_Code version,
const SecureVector<byte>& master_secret);
Certificate_Verify(RandomNumberGenerator& rng,
Record_Writer& writer,
TLS_Handshake_Hash& hash,
const Private_Key* key);
Certificate_Verify(const MemoryRegion<byte>& buf) { deserialize(buf); }
private:
MemoryVector<byte> serialize() const;
void deserialize(const MemoryRegion<byte>&);
MemoryVector<byte> signature;
};
/**
* Finished Message
*/
class Finished : public HandshakeMessage
{
public:
Handshake_Type type() const { return FINISHED; }
bool verify(const MemoryRegion<byte>&, Version_Code,
const TLS_Handshake_Hash&, Connection_Side);
Finished(Record_Writer&, Version_Code, Connection_Side,
const MemoryRegion<byte>&, TLS_Handshake_Hash&);
Finished(const MemoryRegion<byte>& buf) { deserialize(buf); }
private:
MemoryVector<byte> serialize() const;
void deserialize(const MemoryRegion<byte>&);
MemoryVector<byte> compute_verify(const MemoryRegion<byte>&,
TLS_Handshake_Hash, Connection_Side,
Version_Code);
Connection_Side side;
MemoryVector<byte> verification_data;
};
/**
* Hello Request Message
*/
class Hello_Request : public HandshakeMessage
{
public:
Handshake_Type type() const { return HELLO_REQUEST; }
Hello_Request(Record_Writer&);
Hello_Request(const MemoryRegion<byte>& buf) { deserialize(buf); }
private:
MemoryVector<byte> serialize() const;
void deserialize(const MemoryRegion<byte>&);
};
/**
* Server Hello Message
*/
class Server_Hello : public HandshakeMessage
{
public:
Handshake_Type type() const { return SERVER_HELLO; }
Version_Code version() { return s_version; }
const MemoryVector<byte>& session_id() const { return sess_id; }
u16bit ciphersuite() const { return suite; }
byte compression_method() const { return comp_method; }
std::vector<byte> session_id_vector() const
{
std::vector<byte> v;
v.insert(v.begin(), &sess_id[0], &sess_id[sess_id.size()]);
return v;
}
const MemoryVector<byte>& random() const { return s_random; }
Server_Hello(RandomNumberGenerator& rng,
Record_Writer& writer,
const TLS_Policy& policies,
const std::vector<X509_Certificate>& certs,
const Client_Hello& other,
const MemoryRegion<byte>& session_id,
Version_Code version,
TLS_Handshake_Hash& hash);
Server_Hello(RandomNumberGenerator& rng,
Record_Writer& writer,
const MemoryRegion<byte>& session_id,
u16bit ciphersuite,
byte compression,
Version_Code ver,
TLS_Handshake_Hash& hash);
Server_Hello(const MemoryRegion<byte>& buf) { deserialize(buf); }
private:
MemoryVector<byte> serialize() const;
void deserialize(const MemoryRegion<byte>&);
Version_Code s_version;
MemoryVector<byte> sess_id, s_random;
u16bit suite;
byte comp_method;
};
/**
* Server Key Exchange Message
*/
class Server_Key_Exchange : public HandshakeMessage
{
public:
Handshake_Type type() const { return SERVER_KEX; }
Public_Key* key() const;
bool verify(const X509_Certificate&, const MemoryRegion<byte>&,
const MemoryRegion<byte>&) const;
Server_Key_Exchange(RandomNumberGenerator& rng,
Record_Writer&, const Public_Key*,
const Private_Key*, const MemoryRegion<byte>&,
const MemoryRegion<byte>&, TLS_Handshake_Hash&);
Server_Key_Exchange(const MemoryRegion<byte>& buf) { deserialize(buf); }
private:
MemoryVector<byte> serialize() const;
MemoryVector<byte> serialize_params() const;
void deserialize(const MemoryRegion<byte>&);
std::vector<BigInt> params;
MemoryVector<byte> signature;
};
/**
* Server Hello Done Message
*/
class Server_Hello_Done : public HandshakeMessage
{
public:
Handshake_Type type() const { return SERVER_HELLO_DONE; }
Server_Hello_Done(Record_Writer&, TLS_Handshake_Hash&);
Server_Hello_Done(const MemoryRegion<byte>& buf) { deserialize(buf); }
private:
MemoryVector<byte> serialize() const;
void deserialize(const MemoryRegion<byte>&);
};
}
#endif
|