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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
/*
* TLS Extensions
* (C) 2011,2012,2016 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#ifndef BOTAN_TLS_EXTENSIONS_H__
#define BOTAN_TLS_EXTENSIONS_H__
#include <botan/secmem.h>
#include <botan/tls_magic.h>
#include <vector>
#include <string>
#include <map>
#include <set>
namespace Botan {
namespace TLS {
class TLS_Data_Reader;
enum Handshake_Extension_Type {
TLSEXT_SERVER_NAME_INDICATION = 0,
TLSEXT_MAX_FRAGMENT_LENGTH = 1,
TLSEXT_CLIENT_CERT_URL = 2,
TLSEXT_TRUSTED_CA_KEYS = 3,
TLSEXT_TRUNCATED_HMAC = 4,
TLSEXT_CERTIFICATE_TYPES = 9,
TLSEXT_USABLE_ELLIPTIC_CURVES = 10,
TLSEXT_EC_POINT_FORMATS = 11,
TLSEXT_SRP_IDENTIFIER = 12,
TLSEXT_SIGNATURE_ALGORITHMS = 13,
TLSEXT_USE_SRTP = 14,
TLSEXT_HEARTBEAT_SUPPORT = 15,
TLSEXT_ALPN = 16,
TLSEXT_EXTENDED_MASTER_SECRET = 23,
TLSEXT_SESSION_TICKET = 35,
TLSEXT_SAFE_RENEGOTIATION = 65281,
};
/**
* Base class representing a TLS extension of some kind
*/
class Extension
{
public:
/**
* @return code number of the extension
*/
virtual Handshake_Extension_Type type() const = 0;
/**
* @return serialized binary for the extension
*/
virtual std::vector<byte> serialize() const = 0;
/**
* @return if we should encode this extension or not
*/
virtual bool empty() const = 0;
virtual ~Extension() {}
};
/**
* Server Name Indicator extension (RFC 3546)
*/
class Server_Name_Indicator final : public Extension
{
public:
static Handshake_Extension_Type static_type()
{ return TLSEXT_SERVER_NAME_INDICATION; }
Handshake_Extension_Type type() const override { return static_type(); }
Server_Name_Indicator(const std::string& host_name) :
m_sni_host_name(host_name) {}
Server_Name_Indicator(TLS_Data_Reader& reader,
u16bit extension_size);
std::string host_name() const { return m_sni_host_name; }
std::vector<byte> serialize() const override;
bool empty() const override { return m_sni_host_name.empty(); }
private:
std::string m_sni_host_name;
};
/**
* SRP identifier extension (RFC 5054)
*/
class SRP_Identifier final : public Extension
{
public:
static Handshake_Extension_Type static_type()
{ return TLSEXT_SRP_IDENTIFIER; }
Handshake_Extension_Type type() const override { return static_type(); }
SRP_Identifier(const std::string& identifier) :
m_srp_identifier(identifier) {}
SRP_Identifier(TLS_Data_Reader& reader,
u16bit extension_size);
std::string identifier() const { return m_srp_identifier; }
std::vector<byte> serialize() const override;
bool empty() const override { return m_srp_identifier.empty(); }
private:
std::string m_srp_identifier;
};
/**
* Renegotiation Indication Extension (RFC 5746)
*/
class Renegotiation_Extension final : public Extension
{
public:
static Handshake_Extension_Type static_type()
{ return TLSEXT_SAFE_RENEGOTIATION; }
Handshake_Extension_Type type() const override { return static_type(); }
Renegotiation_Extension() {}
Renegotiation_Extension(const std::vector<byte>& bits) :
m_reneg_data(bits) {}
Renegotiation_Extension(TLS_Data_Reader& reader,
u16bit extension_size);
const std::vector<byte>& renegotiation_info() const
{ return m_reneg_data; }
std::vector<byte> serialize() const override;
bool empty() const override { return false; } // always send this
private:
std::vector<byte> m_reneg_data;
};
/**
* Maximum Fragment Length Negotiation Extension (RFC 4366 sec 3.2)
*/
class Maximum_Fragment_Length final : public Extension
{
public:
static Handshake_Extension_Type static_type()
{ return TLSEXT_MAX_FRAGMENT_LENGTH; }
Handshake_Extension_Type type() const override { return static_type(); }
bool empty() const override { return false; }
size_t fragment_size() const { return m_max_fragment; }
std::vector<byte> serialize() const override;
/**
* @param max_fragment specifies what maximum fragment size to
* advertise. Currently must be one of 512, 1024, 2048, or
* 4096.
*/
Maximum_Fragment_Length(size_t max_fragment) :
m_max_fragment(max_fragment) {}
Maximum_Fragment_Length(TLS_Data_Reader& reader,
u16bit extension_size);
private:
size_t m_max_fragment;
};
/**
* ALPN (RFC 7301)
*/
class Application_Layer_Protocol_Notification final : public Extension
{
public:
static Handshake_Extension_Type static_type() { return TLSEXT_ALPN; }
Handshake_Extension_Type type() const override { return static_type(); }
const std::vector<std::string>& protocols() const { return m_protocols; }
const std::string& single_protocol() const;
/**
* Single protocol, used by server
*/
Application_Layer_Protocol_Notification(const std::string& protocol) :
m_protocols(1, protocol) {}
/**
* List of protocols, used by client
*/
Application_Layer_Protocol_Notification(const std::vector<std::string>& protocols) :
m_protocols(protocols) {}
Application_Layer_Protocol_Notification(TLS_Data_Reader& reader,
u16bit extension_size);
std::vector<byte> serialize() const override;
bool empty() const override { return m_protocols.empty(); }
private:
std::vector<std::string> m_protocols;
};
/**
* Session Ticket Extension (RFC 5077)
*/
class Session_Ticket final : public Extension
{
public:
static Handshake_Extension_Type static_type()
{ return TLSEXT_SESSION_TICKET; }
Handshake_Extension_Type type() const override { return static_type(); }
/**
* @return contents of the session ticket
*/
const std::vector<byte>& contents() const { return m_ticket; }
/**
* Create empty extension, used by both client and server
*/
Session_Ticket() {}
/**
* Extension with ticket, used by client
*/
Session_Ticket(const std::vector<byte>& session_ticket) :
m_ticket(session_ticket) {}
/**
* Deserialize a session ticket
*/
Session_Ticket(TLS_Data_Reader& reader, u16bit extension_size);
std::vector<byte> serialize() const override { return m_ticket; }
bool empty() const override { return false; }
private:
std::vector<byte> m_ticket;
};
/**
* Supported Elliptic Curves Extension (RFC 4492)
*/
class Supported_Elliptic_Curves final : public Extension
{
public:
static Handshake_Extension_Type static_type()
{ return TLSEXT_USABLE_ELLIPTIC_CURVES; }
Handshake_Extension_Type type() const override { return static_type(); }
static std::string curve_id_to_name(u16bit id);
static u16bit name_to_curve_id(const std::string& name);
const std::vector<std::string>& curves() const { return m_curves; }
std::vector<byte> serialize() const override;
Supported_Elliptic_Curves(const std::vector<std::string>& curves) :
m_curves(curves) {}
Supported_Elliptic_Curves(TLS_Data_Reader& reader,
u16bit extension_size);
bool empty() const override { return m_curves.empty(); }
private:
std::vector<std::string> m_curves;
};
/**
* Signature Algorithms Extension for TLS 1.2 (RFC 5246)
*/
class Signature_Algorithms final : public Extension
{
public:
static Handshake_Extension_Type static_type()
{ return TLSEXT_SIGNATURE_ALGORITHMS; }
Handshake_Extension_Type type() const override { return static_type(); }
static std::string hash_algo_name(byte code);
static byte hash_algo_code(const std::string& name);
static std::string sig_algo_name(byte code);
static byte sig_algo_code(const std::string& name);
std::vector<std::pair<std::string, std::string> >
supported_signature_algorthms() const
{
return m_supported_algos;
}
std::vector<byte> serialize() const override;
bool empty() const override { return false; }
Signature_Algorithms(const std::vector<std::string>& hashes,
const std::vector<std::string>& sig_algos);
Signature_Algorithms(const std::vector<std::pair<std::string, std::string> >& algos) :
m_supported_algos(algos) {}
Signature_Algorithms(TLS_Data_Reader& reader,
u16bit extension_size);
private:
std::vector<std::pair<std::string, std::string> > m_supported_algos;
};
/**
* Used to indicate SRTP algorithms for DTLS (RFC 5764)
*/
class SRTP_Protection_Profiles final : public Extension
{
public:
static Handshake_Extension_Type static_type()
{ return TLSEXT_USE_SRTP; }
Handshake_Extension_Type type() const override { return static_type(); }
const std::vector<u16bit>& profiles() const { return m_pp; }
std::vector<byte> serialize() const override;
bool empty() const override { return m_pp.empty(); }
SRTP_Protection_Profiles(const std::vector<u16bit>& pp) : m_pp(pp) {}
SRTP_Protection_Profiles(u16bit pp) : m_pp(1, pp) {}
SRTP_Protection_Profiles(TLS_Data_Reader& reader, u16bit extension_size);
private:
std::vector<u16bit> m_pp;
};
/**
* Extended Master Secret Extension (RFC 7627)
*/
class Extended_Master_Secret final : public Extension
{
public:
static Handshake_Extension_Type static_type()
{ return TLSEXT_EXTENDED_MASTER_SECRET; }
Handshake_Extension_Type type() const override { return static_type(); }
std::vector<byte> serialize() const override;
bool empty() const override { return false; }
Extended_Master_Secret() {}
Extended_Master_Secret(TLS_Data_Reader& reader, u16bit extension_size);
};
/**
* Represents a block of extensions in a hello message
*/
class Extensions
{
public:
std::set<Handshake_Extension_Type> extension_types() const;
template<typename T>
T* get() const
{
Handshake_Extension_Type type = T::static_type();
auto i = m_extensions.find(type);
if(i != m_extensions.end())
return dynamic_cast<T*>(i->second.get());
return nullptr;
}
template<typename T>
bool has() const
{
return get<T>() != nullptr;
}
void add(Extension* extn)
{
m_extensions[extn->type()].reset(extn);
}
std::vector<byte> serialize() const;
void deserialize(TLS_Data_Reader& reader);
Extensions() {}
Extensions(TLS_Data_Reader& reader) { deserialize(reader); }
private:
Extensions(const Extensions&) {}
Extensions& operator=(const Extensions&) { return (*this); }
std::map<Handshake_Extension_Type, std::unique_ptr<Extension>> m_extensions;
};
}
}
#endif
|