aboutsummaryrefslogtreecommitdiffstats
path: root/src/tls/tls_ciphersuite.cpp
blob: 247948464bb87722b748bcd9a0721cf5bed4fa51 (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
/*
* TLS Cipher Suites
* (C) 2004-2010,2012 Jack Lloyd
*
* Released under the terms of the Botan license
*/

#include <botan/tls_ciphersuite.h>
#include <botan/tls_magic.h>
#include <botan/parsing.h>
#include <sstream>
#include <stdexcept>

namespace Botan {

namespace TLS {

/**
* Convert an SSL/TLS ciphersuite to algorithm fields
*/
Ciphersuite Ciphersuite::by_id(u16bit suite)
   {
   switch(static_cast<Ciphersuite_Code>(suite))
      {
      // RSA ciphersuites

      case TLS_RSA_WITH_AES_128_CBC_SHA:
         return Ciphersuite("RSA", "RSA", "SHA-1", "AES-128", 16);

      case TLS_RSA_WITH_AES_256_CBC_SHA:
         return Ciphersuite("RSA", "RSA", "SHA-1", "AES-256", 32);

      case TLS_RSA_WITH_AES_128_CBC_SHA256:
         return Ciphersuite("RSA", "RSA", "SHA-256", "AES-128", 16);

      case TLS_RSA_WITH_AES_256_CBC_SHA256:
         return Ciphersuite("RSA", "RSA", "SHA-256", "AES-256", 32);

      case TLS_RSA_WITH_3DES_EDE_CBC_SHA:
         return Ciphersuite("RSA", "RSA", "SHA-1", "3DES", 24);

      case TLS_RSA_WITH_RC4_128_SHA:
         return Ciphersuite("RSA", "RSA", "SHA-1", "ARC4", 16);

      case TLS_RSA_WITH_RC4_128_MD5:
         return Ciphersuite("RSA", "RSA", "MD5", "ARC4", 16);

      case TLS_RSA_WITH_CAMELLIA_128_CBC_SHA:
         return Ciphersuite("RSA", "RSA", "SHA-1", "Camellia", 16);

      case TLS_RSA_WITH_CAMELLIA_256_CBC_SHA:
         return Ciphersuite("RSA", "RSA", "SHA-1", "Camellia", 32);

      case TLS_RSA_WITH_SEED_CBC_SHA:
         return Ciphersuite("RSA", "RSA", "SHA-1", "SEED", 16);

#if defined(BOTAN_HAS_IDEA)
      case TLS_RSA_WITH_IDEA_CBC_SHA:
         return Ciphersuite("RSA", "RSA", "SHA-1", "IDEA", 16);
#endif

      // DH/DSS ciphersuites

      case TLS_DHE_DSS_WITH_AES_128_CBC_SHA:
         return Ciphersuite("DSA", "DH", "SHA-1", "AES-128", 16);

      case TLS_DHE_DSS_WITH_AES_256_CBC_SHA:
         return Ciphersuite("DSA", "DH", "SHA-1", "AES-256", 32);

      case TLS_DHE_DSS_WITH_AES_128_CBC_SHA256:
         return Ciphersuite("DSA", "DH", "SHA-256", "AES-128", 16);

      case TLS_DHE_DSS_WITH_AES_256_CBC_SHA256:
         return Ciphersuite("DSA", "DH", "SHA-256", "AES-256", 32);

      case TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA:
         return Ciphersuite("DSA", "DH", "SHA-1", "3DES", 24);

      case TLS_DHE_DSS_WITH_RC4_128_SHA:
         return Ciphersuite("DSA", "DH", "SHA-1", "ARC4", 16);

      case TLS_DHE_DSS_WITH_CAMELLIA_128_CBC_SHA:
         return Ciphersuite("DSA", "DH", "SHA-1", "Camellia", 16);

      case TLS_DHE_DSS_WITH_CAMELLIA_256_CBC_SHA:
         return Ciphersuite("DSA", "DH", "SHA-1", "Camellia", 32);

      case TLS_DHE_DSS_WITH_SEED_CBC_SHA:
         return Ciphersuite("DSA", "DH", "SHA-1", "SEED", 16);

      // DH/RSA ciphersuites

      case TLS_DHE_RSA_WITH_AES_128_CBC_SHA:
         return Ciphersuite("RSA", "DH", "SHA-1", "AES-128", 16);

      case TLS_DHE_RSA_WITH_AES_256_CBC_SHA:
         return Ciphersuite("RSA", "DH", "SHA-1", "AES-256", 32);

      case TLS_DHE_RSA_WITH_AES_128_CBC_SHA256:
         return Ciphersuite("RSA", "DH", "SHA-256", "AES-128", 16);

      case TLS_DHE_RSA_WITH_AES_256_CBC_SHA256:
         return Ciphersuite("RSA", "DH", "SHA-256", "AES-256", 32);

      case TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA:
         return Ciphersuite("RSA", "DH", "SHA-1", "3DES", 24);

      case TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA:
         return Ciphersuite("RSA", "DH", "SHA-1", "Camellia", 16);

      case TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA:
         return Ciphersuite("RSA", "DH", "SHA-1", "Camellia", 32);

      case TLS_DHE_RSA_WITH_SEED_CBC_SHA:
         return Ciphersuite("RSA", "DH", "SHA-1", "SEED", 16);

      // ECDH/RSA ciphersuites
      case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA:
         return Ciphersuite("RSA", "ECDH", "SHA-1", "AES-128", 16);

      case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA:
         return Ciphersuite("RSA", "ECDH", "SHA-1", "AES-256", 32);

      case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256:
         return Ciphersuite("RSA", "ECDH", "SHA-256", "AES-128", 16);

      case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384:
         return Ciphersuite("RSA", "ECDH", "SHA-384", "AES-256", 32);

      case TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA:
         return Ciphersuite("RSA", "ECDH", "SHA-1", "3DES", 24);

      case TLS_ECDHE_RSA_WITH_RC4_128_SHA:
         return Ciphersuite("RSA", "ECDH", "SHA-1", "ARC4", 16);

      // ECDH/ECDSA ciphersuites

      case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA:
         return Ciphersuite("ECDSA", "ECDH", "SHA-1", "AES-128", 16);

      case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA:
         return Ciphersuite("ECDSA", "ECDH", "SHA-1", "AES-256", 32);

      case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256:
         return Ciphersuite("ECDSA", "ECDH", "SHA-256", "AES-128", 16);

      case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384:
         return Ciphersuite("ECDSA", "ECDH", "SHA-384", "AES-256", 32);

      case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA:
         return Ciphersuite("ECDSA", "ECDH", "SHA-1", "ARC4", 16);

      case TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA:
         return Ciphersuite("ECDSA", "ECDH", "SHA-1", "3DES", 24);

      // PSK ciphersuites

      case TLS_PSK_WITH_RC4_128_SHA:
         return Ciphersuite("", "PSK", "SHA-1", "ARC4", 16);

      case TLS_PSK_WITH_3DES_EDE_CBC_SHA:
         return Ciphersuite("", "PSK", "SHA-1", "3DES", 24);

      case TLS_PSK_WITH_AES_128_CBC_SHA:
         return Ciphersuite("", "PSK", "SHA-1", "AES-128", 16);

      case TLS_PSK_WITH_AES_128_CBC_SHA256:
         return Ciphersuite("", "PSK", "SHA-256", "AES-128", 16);

      case TLS_PSK_WITH_AES_256_CBC_SHA:
         return Ciphersuite("", "PSK", "SHA-1", "AES-256", 32);

      case TLS_PSK_WITH_AES_256_CBC_SHA384:
         return Ciphersuite("", "PSK", "SHA-384", "AES-256", 32);

      // PSK+DH ciphersuites

      case TLS_DHE_PSK_WITH_RC4_128_SHA:
         return Ciphersuite("", "DHE_PSK", "SHA-1", "ARC4", 16);

      case TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA:
         return Ciphersuite("", "DHE_PSK", "SHA-1", "3DES", 24);

      case TLS_DHE_PSK_WITH_AES_128_CBC_SHA:
         return Ciphersuite("", "DHE_PSK", "SHA-1", "AES-128", 16);

      case TLS_DHE_PSK_WITH_AES_128_CBC_SHA256:
         return Ciphersuite("", "DHE_PSK", "SHA-256", "AES-128", 16);

      case TLS_DHE_PSK_WITH_AES_256_CBC_SHA:
         return Ciphersuite("", "DHE_PSK", "SHA-1", "AES-256", 32);

      case TLS_DHE_PSK_WITH_AES_256_CBC_SHA384:
         return Ciphersuite("", "DHE_PSK", "SHA-384", "AES-256", 32);

      // PSK+ECDH ciphersuites

      case TLS_ECDHE_PSK_WITH_RC4_128_SHA:
         return Ciphersuite("", "ECDHE_PSK", "SHA-1", "ARC4", 16);

      case TLS_ECDHE_PSK_WITH_3DES_EDE_CBC_SHA:
         return Ciphersuite("", "ECDHE_PSK", "SHA-1", "3DES", 24);

      case TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA:
         return Ciphersuite("", "ECDHE_PSK", "SHA-1", "AES-128", 16);

      case TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256:
         return Ciphersuite("", "ECDHE_PSK", "SHA-256", "AES-128", 16);

      case TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA:
         return Ciphersuite("", "ECDHE_PSK", "SHA-1", "AES-256", 32);

      case TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA384:
         return Ciphersuite("", "ECDHE_PSK", "SHA-384", "AES-256", 32);

      // SRP ciphersuites

      case TLS_SRP_SHA_WITH_AES_128_CBC_SHA:
         return Ciphersuite("", "SRP", "SHA-1", "AES-128", 16);

      case TLS_SRP_SHA_WITH_AES_256_CBC_SHA:
         return Ciphersuite("", "SRP", "SHA-1", "AES-256", 32);

      case TLS_SRP_SHA_WITH_3DES_EDE_CBC_SHA:
         return Ciphersuite("", "SRP", "SHA-1", "3DES", 24);

      // SRP/RSA ciphersuites

      case TLS_SRP_SHA_RSA_WITH_AES_128_CBC_SHA:
         return Ciphersuite("RSA", "SRP", "SHA-1", "AES-128", 16);

      case TLS_SRP_SHA_RSA_WITH_AES_256_CBC_SHA:
         return Ciphersuite("RSA", "SRP", "SHA-1", "AES-256", 32);

      case TLS_SRP_SHA_RSA_WITH_3DES_EDE_CBC_SHA:
         return Ciphersuite("RSA", "SRP", "SHA-1", "3DES", 24);

      // SRP/DSA ciphersuites

      case TLS_SRP_SHA_DSS_WITH_AES_128_CBC_SHA:
         return Ciphersuite("DSA", "SRP", "SHA-1", "AES-128", 16);

      case TLS_SRP_SHA_DSS_WITH_AES_256_CBC_SHA:
         return Ciphersuite("DSA", "SRP", "SHA-1", "AES-256", 32);

      case TLS_SRP_SHA_DSS_WITH_3DES_EDE_CBC_SHA:
         return Ciphersuite("DSA", "SRP", "SHA-1", "3DES", 24);

      // Signaling ciphersuite values

      case TLS_EMPTY_RENEGOTIATION_INFO_SCSV:
         return Ciphersuite();
      }

   return Ciphersuite(); // some unknown ciphersuite
   }

Ciphersuite Ciphersuite::by_name(const std::string& name)
   {
   for(size_t i = 0; i != 65536; ++i)
      {
      Ciphersuite suite = Ciphersuite::by_id(i);

      if(!suite.valid())
         continue; // not a ciphersuite we know, skip

      if(suite.to_string() == name)
         return suite;
      }

   return Ciphersuite(); // some unknown ciphersuite
   }

std::string Ciphersuite::to_string() const
   {
   if(m_cipher_keylen == 0)
      throw std::runtime_error("Ciphersuite::to_string - no value set");

   std::ostringstream out;

   out << "TLS_";

   if(kex_algo() != "RSA")
      {
      if(kex_algo() == "DH")
         out << "DHE";
      else if(kex_algo() == "ECDH")
         out << "ECDHE";
      else if(kex_algo() == "SRP")
         out << "SRP_SHA";
      else
         out << kex_algo();

      out << '_';
      }

   if(sig_algo() == "DSA")
      out << "DSS_";
   else if(sig_algo() != "")
      out << sig_algo() << '_';

   out << "WITH_";

   if(cipher_algo() == "ARC4")
      {
      out << "RC4_128_";
      }
   else
      {
      if(cipher_algo() == "3DES")
         out << "3DES_EDE";
      else if(cipher_algo() == "Camellia")
         out << "CAMELLIA_" << Botan::to_string(8*cipher_keylen());
      else
         out << replace_char(cipher_algo(), '-', '_');

      out << "_CBC_";
      }

   if(mac_algo() == "SHA-1")
      out << "SHA";
   else if(mac_algo() == "SHA-256")
      out << "SHA256";
   else if(mac_algo() == "SHA-384")
      out << "SHA384";
   else
      out << mac_algo();

   return out.str();
   }

Ciphersuite::Ciphersuite(const std::string& sig_algo,
                         const std::string& kex_algo,
                         const std::string& mac_algo,
                         const std::string& cipher_algo,
                         size_t cipher_algo_keylen) :
   m_sig_algo(sig_algo),
   m_kex_algo(kex_algo),
   m_mac_algo(mac_algo),
   m_cipher_algo(cipher_algo),
   m_cipher_keylen(cipher_algo_keylen)
   {
   }

}

}