aboutsummaryrefslogtreecommitdiffstats
path: root/src/tls/tls_extensions.cpp
blob: d414a979d9c40983633acd6ca7720129a7781e59 (plain)
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
/*
* TLS Extensions
* (C) 2011,2012 Jack Lloyd
*
* Released under the terms of the Botan license
*/

#include <botan/internal/tls_extensions.h>
#include <botan/internal/tls_reader.h>
#include <botan/tls_exceptn.h>

namespace Botan {

namespace {

TLS_Extension* make_extension(TLS_Data_Reader& reader,
                              u16bit code,
                              u16bit size)
   {
   if(code == TLSEXT_SERVER_NAME_INDICATION)
      return new Server_Name_Indicator(reader, size);
   else if(code == TLSEXT_MAX_FRAGMENT_LENGTH)
      return new Maximum_Fragment_Length(reader, size);
   else if(code == TLSEXT_SRP_IDENTIFIER)
      return new SRP_Identifier(reader, size);
   else if(code == TLSEXT_SAFE_RENEGOTIATION)
      return new Renegotation_Extension(reader, size);
   else if(code == TLSEXT_SIGNATURE_ALGORITHMS)
      return new Signature_Algorithms(reader, size);
   else if(code == TLSEXT_NEXT_PROTOCOL)
      return new Next_Protocol_Notification(reader, size);
   else
      return 0; // not known
   }

}

TLS_Extensions::TLS_Extensions(TLS_Data_Reader& reader)
   {
   if(reader.has_remaining())
      {
      const u16bit all_extn_size = reader.get_u16bit();

      if(reader.remaining_bytes() != all_extn_size)
         throw Decoding_Error("Bad extension size");

      while(reader.has_remaining())
         {
         const u16bit extension_code = reader.get_u16bit();
         const u16bit extension_size = reader.get_u16bit();

         TLS_Extension* extn = make_extension(reader,
                                              extension_code,
                                              extension_size);

         if(extn)
            extensions.push_back(extn);
         else // unknown/unhandled extension
            reader.discard_next(extension_size);
         }
      }
   }

MemoryVector<byte> TLS_Extensions::serialize() const
   {
   MemoryVector<byte> buf(2); // 2 bytes for length field

   for(size_t i = 0; i != extensions.size(); ++i)
      {
      if(extensions[i]->empty())
         continue;

      const u16bit extn_code = extensions[i]->type();

      MemoryVector<byte> extn_val = extensions[i]->serialize();

      buf.push_back(get_byte(0, extn_code));
      buf.push_back(get_byte(1, extn_code));

      buf.push_back(get_byte<u16bit>(0, extn_val.size()));
      buf.push_back(get_byte<u16bit>(1, extn_val.size()));

      buf += extn_val;
      }

   const u16bit extn_size = buf.size() - 2;

   buf[0] = get_byte(0, extn_size);
   buf[1] = get_byte(1, extn_size);

   // avoid sending a completely empty extensions block
   if(buf.size() == 2)
      return MemoryVector<byte>();

   return buf;
   }

TLS_Extensions::~TLS_Extensions()
   {
   for(size_t i = 0; i != extensions.size(); ++i)
      delete extensions[i];
   extensions.clear();
   }

Server_Name_Indicator::Server_Name_Indicator(TLS_Data_Reader& reader,
                                             u16bit extension_size)
   {
   /*
   * This is used by the server to confirm that it knew the name
   */
   if(extension_size == 0)
      return;

   u16bit name_bytes = reader.get_u16bit();

   if(name_bytes + 2 != extension_size)
      throw Decoding_Error("Bad encoding of SNI extension");

   while(name_bytes)
      {
      byte name_type = reader.get_byte();
      name_bytes--;

      if(name_type == 0) // DNS
         {
         sni_host_name = reader.get_string(2, 1, 65535);
         name_bytes -= (2 + sni_host_name.size());
         }
      else // some other unknown name type
         {
         reader.discard_next(name_bytes);
         name_bytes = 0;
         }
      }
   }

MemoryVector<byte> Server_Name_Indicator::serialize() const
   {
   MemoryVector<byte> buf;

   size_t name_len = sni_host_name.size();

   buf.push_back(get_byte<u16bit>(0, name_len+3));
   buf.push_back(get_byte<u16bit>(1, name_len+3));
   buf.push_back(0); // DNS

   buf.push_back(get_byte<u16bit>(0, name_len));
   buf.push_back(get_byte<u16bit>(1, name_len));

   buf += std::make_pair(
      reinterpret_cast<const byte*>(sni_host_name.data()),
      sni_host_name.size());

   return buf;
   }

SRP_Identifier::SRP_Identifier(TLS_Data_Reader& reader,
                               u16bit extension_size)
   {
   srp_identifier = reader.get_string(1, 1, 255);

   if(srp_identifier.size() + 1 != extension_size)
      throw Decoding_Error("Bad encoding for SRP identifier extension");
   }

MemoryVector<byte> SRP_Identifier::serialize() const
   {
   MemoryVector<byte> buf;

   const byte* srp_bytes =
      reinterpret_cast<const byte*>(srp_identifier.data());

   append_tls_length_value(buf, srp_bytes, srp_identifier.size(), 1);

   return buf;
   }

Renegotation_Extension::Renegotation_Extension(TLS_Data_Reader& reader,
                                               u16bit extension_size)
   {
   reneg_data = reader.get_range<byte>(1, 0, 255);

   if(reneg_data.size() + 1 != extension_size)
      throw Decoding_Error("Bad encoding for secure renegotiation extn");
   }

MemoryVector<byte> Renegotation_Extension::serialize() const
   {
   MemoryVector<byte> buf;
   append_tls_length_value(buf, reneg_data, 1);
   return buf;
   }

size_t Maximum_Fragment_Length::fragment_size() const
   {
   switch(val)
      {
      case 1:
         return 512;
      case 2:
         return 1024;
      case 3:
         return 2048;
      case 4:
         return 4096;
      default:
         throw TLS_Exception(ILLEGAL_PARAMETER,
                             "Bad value in maximum fragment extension");
      }
   }

Maximum_Fragment_Length::Maximum_Fragment_Length(size_t max_fragment)
   {
   if(max_fragment == 512)
      val = 1;
   else if(max_fragment == 1024)
      val = 2;
   else if(max_fragment == 2048)
      val = 3;
   else if(max_fragment == 4096)
      val = 4;
   else
      throw std::invalid_argument("Bad setting " + to_string(max_fragment) +
                                  " for maximum fragment size");
   }

Maximum_Fragment_Length::Maximum_Fragment_Length(TLS_Data_Reader& reader,
                                                 u16bit extension_size)
   {
   if(extension_size != 1)
      throw Decoding_Error("Bad size for maximum fragment extension");
   val = reader.get_byte();
   }

Next_Protocol_Notification::Next_Protocol_Notification(TLS_Data_Reader& reader,
                                                       u16bit extension_size)
   {
   if(extension_size == 0)
      return; // empty extension

   size_t bytes_remaining = extension_size;

   while(bytes_remaining)
      {
      const std::string p = reader.get_string(1, 0, 255);

      if(bytes_remaining < p.size() + 1)
         throw Decoding_Error("Bad encoding for next protocol extension");

      bytes_remaining -= (p.size() + 1);

      m_protocols.push_back(p);
      }
   }

MemoryVector<byte> Next_Protocol_Notification::serialize() const
   {
   MemoryVector<byte> buf;

   for(size_t i = 0; i != m_protocols.size(); ++i)
      {
      const std::string p = m_protocols[i];

      if(p != "")
         append_tls_length_value(buf,
                                 reinterpret_cast<const byte*>(p.data()),
                                 p.size(),
                                 1);
      }

   return buf;
   }

TLS_Ciphersuite_Algos Signature_Algorithms::hash_algo_code(byte code)
   {
   switch(code)
      {
      case 1:
         return TLS_ALGO_HASH_MD5;
      case 2:
         return TLS_ALGO_HASH_SHA1;
      case 3:
         return TLS_ALGO_HASH_SHA224;
      case 4:
         return TLS_ALGO_HASH_SHA256;
      case 5:
         return TLS_ALGO_HASH_SHA384;
      case 6:
         return TLS_ALGO_HASH_SHA512;
      default:
         return TLS_ALGO_UNKNOWN;
      }
   }

byte Signature_Algorithms::hash_algo_code(TLS_Ciphersuite_Algos code)
   {
   switch(code)
      {
      case TLS_ALGO_HASH_MD5:
         return 1;
      case TLS_ALGO_HASH_SHA1:
         return 2;
      case TLS_ALGO_HASH_SHA224:
         return 3;
      case TLS_ALGO_HASH_SHA256:
         return 4;
      case TLS_ALGO_HASH_SHA384:
         return 5;
      case TLS_ALGO_HASH_SHA512:
         return 6;
      default:
         throw Algorithm_Not_Found("Unknown hash ID for signature_algorithms");
      }
   }

TLS_Ciphersuite_Algos Signature_Algorithms::sig_algo_code(byte code)
   {
   switch(code)
      {
      case 1:
         return TLS_ALGO_SIGNER_RSA;
      case 2:
         return TLS_ALGO_SIGNER_DSA;
      case 3:
         return TLS_ALGO_SIGNER_ECDSA;
      default:
         return TLS_ALGO_UNKNOWN;
      }
   }

byte Signature_Algorithms::sig_algo_code(TLS_Ciphersuite_Algos code)
   {
   switch(code)
      {
      case TLS_ALGO_SIGNER_RSA:
         return 1;
      case TLS_ALGO_SIGNER_DSA:
         return 2;
      case TLS_ALGO_SIGNER_ECDSA:
         return 3;
      default:
         throw Algorithm_Not_Found("Unknown sig ID for signature_algorithms");
      }
   }

MemoryVector<byte> Signature_Algorithms::serialize() const
   {
   MemoryVector<byte> buf(2);

   for(size_t i = 0; i != m_supported_algos.size(); ++i)
      {
      buf.push_back(hash_algo_code(m_supported_algos[i].first));
      buf.push_back(sig_algo_code(m_supported_algos[i].second));
      }

   buf[0] = get_byte<u16bit>(0, buf.size()-2);
   buf[1] = get_byte<u16bit>(1, buf.size()-2);

   return buf;
   }

Signature_Algorithms::Signature_Algorithms()
   {
   /*
   Declare we support everything except MD5 for RSA, and SHA-1 with DSA.
   We prefer hashes strongest (SHA-512) to weakest (SHA-1).
   */

   m_supported_algos.push_back(std::make_pair(TLS_ALGO_HASH_SHA512,
                                              TLS_ALGO_SIGNER_RSA));

   m_supported_algos.push_back(std::make_pair(TLS_ALGO_HASH_SHA384,
                                              TLS_ALGO_SIGNER_RSA));

   m_supported_algos.push_back(std::make_pair(TLS_ALGO_HASH_SHA256,
                                              TLS_ALGO_SIGNER_RSA));

   m_supported_algos.push_back(std::make_pair(TLS_ALGO_HASH_SHA224,
                                              TLS_ALGO_SIGNER_RSA));

   m_supported_algos.push_back(std::make_pair(TLS_ALGO_HASH_SHA1,
                                              TLS_ALGO_SIGNER_RSA));

   m_supported_algos.push_back(std::make_pair(TLS_ALGO_HASH_SHA1,
                                              TLS_ALGO_SIGNER_DSA));
   }

Signature_Algorithms::Signature_Algorithms(TLS_Data_Reader& reader,
                                           u16bit extension_size)
   {
   u16bit len = reader.get_u16bit();

   if(len + 2 != extension_size)
      throw Decoding_Error("Bad encoding on signature algorithms extension");

   while(len)
      {
      TLS_Ciphersuite_Algos hash_code = hash_algo_code(reader.get_byte());
      TLS_Ciphersuite_Algos sig_code = sig_algo_code(reader.get_byte());

      // If not something we know, ignore completely
      if(hash_code == TLS_ALGO_UNKNOWN || sig_code == TLS_ALGO_UNKNOWN)
         continue;

      m_supported_algos.push_back(std::make_pair(hash_code, sig_code));

      len -= 2;
      }
   }

}