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
|
/*
* TLS Extensions
* (C) 2011 Jack Lloyd
*
* Released under the terms of the Botan license
*/
#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>
namespace Botan {
class TLS_Data_Reader;
/**
* Base class representing a TLS extension of some kind
*/
class TLS_Extension
{
public:
virtual TLS_Handshake_Extension_Type type() const = 0;
virtual MemoryVector<byte> serialize() const = 0;
virtual bool empty() const = 0;
virtual ~TLS_Extension() {}
};
/**
* Server Name Indicator extension (RFC 3546)
*/
class Server_Name_Indicator : public TLS_Extension
{
public:
static TLS_Handshake_Extension_Type static_type()
{ return TLSEXT_SERVER_NAME_INDICATION; }
TLS_Handshake_Extension_Type type() const { return static_type(); }
Server_Name_Indicator(const std::string& host_name) :
sni_host_name(host_name) {}
Server_Name_Indicator(TLS_Data_Reader& reader,
u16bit extension_size);
std::string host_name() const { return sni_host_name; }
MemoryVector<byte> serialize() const;
bool empty() const { return sni_host_name == ""; }
private:
std::string sni_host_name;
};
/**
* SRP identifier extension (RFC 5054)
*/
class SRP_Identifier : public TLS_Extension
{
public:
static TLS_Handshake_Extension_Type static_type()
{ return TLSEXT_SRP_IDENTIFIER; }
TLS_Handshake_Extension_Type type() const { return static_type(); }
SRP_Identifier(const std::string& identifier) :
srp_identifier(identifier) {}
SRP_Identifier(TLS_Data_Reader& reader,
u16bit extension_size);
std::string identifier() const { return srp_identifier; }
MemoryVector<byte> serialize() const;
bool empty() const { return srp_identifier == ""; }
private:
std::string srp_identifier;
};
/**
* Renegotiation Indication Extension (RFC 5746)
*/
class Renegotation_Extension : public TLS_Extension
{
public:
static TLS_Handshake_Extension_Type static_type()
{ return TLSEXT_SAFE_RENEGOTIATION; }
TLS_Handshake_Extension_Type type() const { return static_type(); }
Renegotation_Extension() {}
Renegotation_Extension(const MemoryRegion<byte>& bits) :
reneg_data(bits) {}
Renegotation_Extension(TLS_Data_Reader& reader,
u16bit extension_size);
const MemoryVector<byte>& renegotiation_info() const
{ return reneg_data; }
MemoryVector<byte> serialize() const;
bool empty() const { return false; } // always send this
private:
MemoryVector<byte> reneg_data;
};
/**
* Maximum Fragment Length Negotiation Extension (RFC 4366 sec 3.2)
*/
class Maximum_Fragment_Length : public TLS_Extension
{
public:
static TLS_Handshake_Extension_Type static_type()
{ return TLSEXT_MAX_FRAGMENT_LENGTH; }
TLS_Handshake_Extension_Type type() const { return static_type(); }
bool empty() const { return val != 0; }
size_t fragment_size() const;
MemoryVector<byte> serialize() const
{
return MemoryVector<byte>(&val, 1);
}
/**
* @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);
Maximum_Fragment_Length(TLS_Data_Reader& reader,
u16bit extension_size);
private:
byte val;
};
/**
* Next Protocol Negotiation
* http://technotes.googlecode.com/git/nextprotoneg.html
*
* This implementation requires the semantics defined in the Google
* spec (implemented in Chromium); the internet draft leaves the format
* unspecified.
*/
class Next_Protocol_Notification : public TLS_Extension
{
public:
static TLS_Handshake_Extension_Type static_type()
{ return TLSEXT_NEXT_PROTOCOL; }
TLS_Handshake_Extension_Type type() const { return static_type(); }
const std::vector<std::string>& protocols() const
{ return m_protocols; }
/**
* Empty extension, used by client
*/
Next_Protocol_Notification() {}
/**
* List of protocols, used by server
*/
Next_Protocol_Notification(const std::vector<std::string>& protocols) :
m_protocols(protocols) {}
Next_Protocol_Notification(TLS_Data_Reader& reader,
u16bit extension_size);
MemoryVector<byte> serialize() const;
bool empty() const { return false; }
private:
std::vector<std::string> m_protocols;
};
/**
* Supported Elliptic Curves Extension (RFC 4492)
*/
class Supported_Elliptic_Curves : public TLS_Extension
{
public:
static TLS_Handshake_Extension_Type static_type()
{ return TLSEXT_USABLE_ELLIPTIC_CURVES; }
TLS_Handshake_Extension_Type type() const { return static_type(); }
const std::vector<std::string>& curves() const { return m_curves; }
MemoryVector<byte> serialize() const;
Supported_Elliptic_Curves(); // default values, for client
Supported_Elliptic_Curves(TLS_Data_Reader& reader,
u16bit extension_size);
bool empty() const { return m_curves.empty(); }
private:
std::vector<std::string> m_curves;
};
/**
* Signature Algorithms Extension for TLS 1.2 (RFC 5246)
*/
class Signature_Algorithms : public TLS_Extension
{
public:
static TLS_Handshake_Extension_Type static_type()
{ return TLSEXT_SIGNATURE_ALGORITHMS; }
TLS_Handshake_Extension_Type type() const { 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;
}
MemoryVector<byte> serialize() const;
bool empty() const { return false; }
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;
};
/**
* Represents a block of extensions in a hello message
*/
class TLS_Extensions
{
public:
template<typename T>
T* get() const
{
TLS_Handshake_Extension_Type type = T::static_type();
std::map<TLS_Handshake_Extension_Type, TLS_Extension*>::const_iterator i =
extensions.find(type);
if(i != extensions.end())
return dynamic_cast<T*>(i->second);
return 0;
}
void add(TLS_Extension* extn)
{
delete extensions[extn->type()]; // or hard error if already exists?
extensions[extn->type()] = extn;
}
MemoryVector<byte> serialize() const;
TLS_Extensions() {}
TLS_Extensions(TLS_Data_Reader& reader); // deserialize
~TLS_Extensions();
private:
TLS_Extensions(const TLS_Extensions&) {}
TLS_Extensions& operator=(const TLS_Extensions&) { return (*this); }
std::map<TLS_Handshake_Extension_Type, TLS_Extension*> extensions;
};
}
#endif
|