Botan  2.7.0
Crypto and TLS for C++11
tls_extensions.h
Go to the documentation of this file.
1 /*
2 * TLS Extensions
3 * (C) 2011,2012,2016,2018 Jack Lloyd
4 * (C) 2016 Juraj Somorovsky
5 * (C) 2016 Matthias Gierlings
6 *
7 * Botan is released under the Simplified BSD License (see license.txt)
8 */
9 
10 #ifndef BOTAN_TLS_EXTENSIONS_H_
11 #define BOTAN_TLS_EXTENSIONS_H_
12 
13 #include <botan/tls_algos.h>
14 #include <botan/secmem.h>
15 #include <botan/x509_dn.h>
16 #include <vector>
17 #include <string>
18 #include <map>
19 #include <set>
20 
21 namespace Botan {
22 
23 namespace TLS {
24 
25 class TLS_Data_Reader;
26 
30 
38 
41 
43 
45 };
46 
47 /**
48 * Base class representing a TLS extension of some kind
49 */
51  {
52  public:
53  /**
54  * @return code number of the extension
55  */
56  virtual Handshake_Extension_Type type() const = 0;
57 
58  /**
59  * @return serialized binary for the extension
60  */
61  virtual std::vector<uint8_t> serialize() const = 0;
62 
63  /**
64  * @return if we should encode this extension or not
65  */
66  virtual bool empty() const = 0;
67 
68  virtual ~Extension() = default;
69  };
70 
71 /**
72 * Server Name Indicator extension (RFC 3546)
73 */
74 class Server_Name_Indicator final : public Extension
75  {
76  public:
79 
80  Handshake_Extension_Type type() const override { return static_type(); }
81 
82  explicit Server_Name_Indicator(const std::string& host_name) :
83  m_sni_host_name(host_name) {}
84 
86  uint16_t extension_size);
87 
88  std::string host_name() const { return m_sni_host_name; }
89 
90  std::vector<uint8_t> serialize() const override;
91 
92  bool empty() const override { return m_sni_host_name.empty(); }
93  private:
94  std::string m_sni_host_name;
95  };
96 
97 #if defined(BOTAN_HAS_SRP6)
98 /**
99 * SRP identifier extension (RFC 5054)
100 */
101 class SRP_Identifier final : public Extension
102  {
103  public:
104  static Handshake_Extension_Type static_type()
105  { return TLSEXT_SRP_IDENTIFIER; }
106 
107  Handshake_Extension_Type type() const override { return static_type(); }
108 
109  explicit SRP_Identifier(const std::string& identifier) :
110  m_srp_identifier(identifier) {}
111 
112  SRP_Identifier(TLS_Data_Reader& reader,
113  uint16_t extension_size);
114 
115  std::string identifier() const { return m_srp_identifier; }
116 
117  std::vector<uint8_t> serialize() const override;
118 
119  bool empty() const override { return m_srp_identifier.empty(); }
120  private:
121  std::string m_srp_identifier;
122  };
123 #endif
124 
125 /**
126 * Renegotiation Indication Extension (RFC 5746)
127 */
128 class Renegotiation_Extension final : public Extension
129  {
130  public:
132  { return TLSEXT_SAFE_RENEGOTIATION; }
133 
134  Handshake_Extension_Type type() const override { return static_type(); }
135 
136  Renegotiation_Extension() = default;
137 
138  explicit Renegotiation_Extension(const std::vector<uint8_t>& bits) :
139  m_reneg_data(bits) {}
140 
142  uint16_t extension_size);
143 
144  const std::vector<uint8_t>& renegotiation_info() const
145  { return m_reneg_data; }
146 
147  std::vector<uint8_t> serialize() const override;
148 
149  bool empty() const override { return false; } // always send this
150  private:
151  std::vector<uint8_t> m_reneg_data;
152  };
153 
154 /**
155 * ALPN (RFC 7301)
156 */
158  {
159  public:
161 
162  Handshake_Extension_Type type() const override { return static_type(); }
163 
164  const std::vector<std::string>& protocols() const { return m_protocols; }
165 
166  const std::string& single_protocol() const;
167 
168  /**
169  * Single protocol, used by server
170  */
171  explicit Application_Layer_Protocol_Notification(const std::string& protocol) :
172  m_protocols(1, protocol) {}
173 
174  /**
175  * List of protocols, used by client
176  */
177  explicit Application_Layer_Protocol_Notification(const std::vector<std::string>& protocols) :
178  m_protocols(protocols) {}
179 
181  uint16_t extension_size);
182 
183  std::vector<uint8_t> serialize() const override;
184 
185  bool empty() const override { return m_protocols.empty(); }
186  private:
187  std::vector<std::string> m_protocols;
188  };
189 
190 /**
191 * Session Ticket Extension (RFC 5077)
192 */
193 class Session_Ticket final : public Extension
194  {
195  public:
197  { return TLSEXT_SESSION_TICKET; }
198 
199  Handshake_Extension_Type type() const override { return static_type(); }
200 
201  /**
202  * @return contents of the session ticket
203  */
204  const std::vector<uint8_t>& contents() const { return m_ticket; }
205 
206  /**
207  * Create empty extension, used by both client and server
208  */
209  Session_Ticket() = default;
210 
211  /**
212  * Extension with ticket, used by client
213  */
214  explicit Session_Ticket(const std::vector<uint8_t>& session_ticket) :
215  m_ticket(session_ticket) {}
216 
217  /**
218  * Deserialize a session ticket
219  */
220  Session_Ticket(TLS_Data_Reader& reader, uint16_t extension_size);
221 
222  std::vector<uint8_t> serialize() const override { return m_ticket; }
223 
224  bool empty() const override { return false; }
225  private:
226  std::vector<uint8_t> m_ticket;
227  };
228 
229 
230 /**
231 * Supported Groups Extension (RFC 7919)
232 */
233 class Supported_Groups final : public Extension
234  {
235  public:
237  { return TLSEXT_SUPPORTED_GROUPS; }
238 
239  Handshake_Extension_Type type() const override { return static_type(); }
240 
241  std::vector<Group_Params> ec_groups() const;
242  std::vector<Group_Params> dh_groups() const;
243 
244  std::vector<uint8_t> serialize() const override;
245 
246  explicit Supported_Groups(const std::vector<Group_Params>& groups);
247 
249  uint16_t extension_size);
250 
251  bool empty() const override { return m_groups.empty(); }
252  private:
253  std::vector<Group_Params> m_groups;
254  };
255 
256 // previously Supported Elliptic Curves Extension (RFC 4492)
257 //using Supported_Elliptic_Curves = Supported_Groups;
258 
259 /**
260 * Supported Point Formats Extension (RFC 4492)
261 */
262 class Supported_Point_Formats final : public Extension
263  {
264  public:
265  enum ECPointFormat : uint8_t {
268  ANSIX962_COMPRESSED_CHAR2 = 2, // don't support these curves
269  };
270 
272  { return TLSEXT_EC_POINT_FORMATS; }
273 
274  Handshake_Extension_Type type() const override { return static_type(); }
275 
276  std::vector<uint8_t> serialize() const override;
277 
278  explicit Supported_Point_Formats(bool prefer_compressed) :
279  m_prefers_compressed(prefer_compressed) {}
280 
282  uint16_t extension_size);
283 
284  bool empty() const override { return false; }
285 
286  bool prefers_compressed() { return m_prefers_compressed; }
287 
288  private:
289  bool m_prefers_compressed = false;
290  };
291 
292 /**
293 * Signature Algorithms Extension for TLS 1.2 (RFC 5246)
294 */
295 class Signature_Algorithms final : public Extension
296  {
297  public:
299  { return TLSEXT_SIGNATURE_ALGORITHMS; }
300 
301  Handshake_Extension_Type type() const override { return static_type(); }
302 
303  const std::vector<Signature_Scheme>& supported_schemes() const { return m_schemes; }
304 
305  std::vector<uint8_t> serialize() const override;
306 
307  bool empty() const override { return m_schemes.empty(); }
308 
309  explicit Signature_Algorithms(const std::vector<Signature_Scheme>& schemes) :
310  m_schemes(schemes) {}
311 
313  uint16_t extension_size);
314  private:
315  std::vector<Signature_Scheme> m_schemes;
316  };
317 
318 /**
319 * Used to indicate SRTP algorithms for DTLS (RFC 5764)
320 */
322  {
323  public:
325  { return TLSEXT_USE_SRTP; }
326 
327  Handshake_Extension_Type type() const override { return static_type(); }
328 
329  const std::vector<uint16_t>& profiles() const { return m_pp; }
330 
331  std::vector<uint8_t> serialize() const override;
332 
333  bool empty() const override { return m_pp.empty(); }
334 
335  explicit SRTP_Protection_Profiles(const std::vector<uint16_t>& pp) : m_pp(pp) {}
336 
337  explicit SRTP_Protection_Profiles(uint16_t pp) : m_pp(1, pp) {}
338 
339  SRTP_Protection_Profiles(TLS_Data_Reader& reader, uint16_t extension_size);
340  private:
341  std::vector<uint16_t> m_pp;
342  };
343 
344 /**
345 * Extended Master Secret Extension (RFC 7627)
346 */
347 class Extended_Master_Secret final : public Extension
348  {
349  public:
352 
353  Handshake_Extension_Type type() const override { return static_type(); }
354 
355  std::vector<uint8_t> serialize() const override;
356 
357  bool empty() const override { return false; }
358 
359  Extended_Master_Secret() = default;
360 
361  Extended_Master_Secret(TLS_Data_Reader& reader, uint16_t extension_size);
362  };
363 
364 /**
365 * Encrypt-then-MAC Extension (RFC 7366)
366 */
367 class Encrypt_then_MAC final : public Extension
368  {
369  public:
371  { return TLSEXT_ENCRYPT_THEN_MAC; }
372 
373  Handshake_Extension_Type type() const override { return static_type(); }
374 
375  std::vector<uint8_t> serialize() const override;
376 
377  bool empty() const override { return false; }
378 
379  Encrypt_then_MAC() = default;
380 
381  Encrypt_then_MAC(TLS_Data_Reader& reader, uint16_t extension_size);
382  };
383 
384 /**
385 * Certificate Status Request (RFC 6066)
386 */
388  {
389  public:
391  { return TLSEXT_CERT_STATUS_REQUEST; }
392 
393  Handshake_Extension_Type type() const override { return static_type(); }
394 
395  std::vector<uint8_t> serialize() const override;
396 
397  bool empty() const override { return false; }
398 
399  // Server generated version: empty
401 
402  // Client version, both lists can be empty
403  Certificate_Status_Request(const std::vector<X509_DN>& ocsp_responder_ids,
404  const std::vector<std::vector<uint8_t>>& ocsp_key_ids);
405 
406  Certificate_Status_Request(TLS_Data_Reader& reader, uint16_t extension_size);
407  private:
408  std::vector<X509_DN> m_ocsp_names;
409  std::vector<std::vector<uint8_t>> m_ocsp_keys;
410  std::vector<uint8_t> m_extension_bytes;
411  bool m_server_side;
412  };
413 
414 /**
415 * Unknown extensions are deserialized as this type
416 */
418  {
419  public:
421  TLS_Data_Reader& reader,
422  uint16_t extension_size);
423 
424  std::vector<uint8_t> serialize() const override; // always fails
425 
426  const std::vector<uint8_t>& value() { return m_value; }
427 
428  bool empty() const override { return false; }
429 
430  Handshake_Extension_Type type() const override { return m_type; }
431 
432  private:
434  std::vector<uint8_t> m_value;
435 
436  };
437 
438 /**
439 * Represents a block of extensions in a hello message
440 */
442  {
443  public:
444  std::set<Handshake_Extension_Type> extension_types() const;
445 
446  template<typename T>
447  T* get() const
448  {
449  return dynamic_cast<T*>(get(T::static_type()));
450  }
451 
452  template<typename T>
453  bool has() const
454  {
455  return get<T>() != nullptr;
456  }
457 
458  void add(Extension* extn)
459  {
460  m_extensions[extn->type()].reset(extn);
461  }
462 
464  {
465  auto i = m_extensions.find(type);
466 
467  if(i != m_extensions.end())
468  return i->second.get();
469  return nullptr;
470  }
471 
472  std::vector<uint8_t> serialize() const;
473 
474  void deserialize(TLS_Data_Reader& reader);
475 
476  /**
477  * Remvoe an extension from this extensions object, if it exists.
478  * Returns true if the extension existed (and thus is now removed),
479  * otherwise false (the extension wasn't set in the first place).
480  */
481  bool remove_extension(Handshake_Extension_Type typ);
482 
483  Extensions() = default;
484 
485  explicit Extensions(TLS_Data_Reader& reader) { deserialize(reader); }
486 
487  private:
488  Extensions(const Extensions&) = delete;
489  Extensions& operator=(const Extensions&) = delete;
490 
491  std::map<Handshake_Extension_Type, std::unique_ptr<Extension>> m_extensions;
492  };
493 
494 }
495 
496 }
497 
498 #endif
#define BOTAN_UNSTABLE_API
Definition: compiler.h:34
std::vector< uint8_t > serialize() const override
std::vector< uint8_t > serialize() const override
static Handshake_Extension_Type static_type()
const std::vector< Signature_Scheme > & supported_schemes() const
static Handshake_Extension_Type static_type()
Extensions(TLS_Data_Reader &reader)
Handshake_Extension_Type type() const override
std::vector< uint8_t > serialize() const override
Server_Name_Indicator(const std::string &host_name)
Handshake_Extension_Type type() const override
Handshake_Extension_Type type() const override
void add(Extension *extn)
std::vector< uint8_t > serialize() const override
const std::vector< uint16_t > & profiles() const
static Handshake_Extension_Type static_type()
Handshake_Extension_Type type() const override
Handshake_Extension_Type type() const override
MechanismType type
Signature_Algorithms(const std::vector< Signature_Scheme > &schemes)
std::vector< uint8_t > serialize() const override
bool empty() const override
std::vector< uint8_t > serialize() const override
Handshake_Extension_Type type() const override
Handshake_Extension_Type type() const override
virtual Handshake_Extension_Type type() const =0
std::vector< uint8_t > serialize() const override
static Handshake_Extension_Type static_type()
Application_Layer_Protocol_Notification(const std::string &protocol)
const std::vector< uint8_t > & renegotiation_info() const
static Handshake_Extension_Type static_type()
std::vector< uint8_t > serialize() const override
static Handshake_Extension_Type static_type()
static Handshake_Extension_Type static_type()
Definition: alg_id.cpp:13
const std::vector< uint8_t > & value()
Session_Ticket(const std::vector< uint8_t > &session_ticket)
const std::vector< uint8_t > & contents() const
static Handshake_Extension_Type static_type()
std::vector< Group_Params > ec_groups() const
Handshake_Extension_Type type() const override
std::vector< uint8_t > serialize() const override
Handshake_Extension_Type type() const override
bool empty() const override
std::vector< uint8_t > serialize() const override
bool empty() const override
SRTP_Protection_Profiles(const std::vector< uint16_t > &pp)
fe T
Definition: ge.cpp:37
const std::vector< std::string > & protocols() const
static Handshake_Extension_Type static_type()
Handshake_Extension_Type type() const override
std::vector< Group_Params > dh_groups() const
Handshake_Extension_Type type() const override
Supported_Groups(const std::vector< Group_Params > &groups)
std::vector< uint8_t > serialize() const override
Handshake_Extension_Type type() const override
static Handshake_Extension_Type static_type()
Supported_Point_Formats(bool prefer_compressed)
bool empty() const override
static Handshake_Extension_Type static_type()
Application_Layer_Protocol_Notification(const std::vector< std::string > &protocols)
Renegotiation_Extension(const std::vector< uint8_t > &bits)