Botan  1.10.9
tls_suites.cpp
Go to the documentation of this file.
1 /*
2 * TLS Cipher Suites
3 * (C) 2004-2010 Jack Lloyd
4 *
5 * Released under the terms of the Botan license
6 */
7 
8 #include <botan/tls_suites.h>
9 #include <botan/tls_exceptn.h>
10 
11 namespace Botan {
12 
13 /**
14 * Convert an SSL/TLS ciphersuite to algorithm fields
15 */
17  {
18  if(suite == TLS_RSA_WITH_RC4_128_MD5)
23 
24  if(suite == TLS_RSA_WITH_RC4_128_SHA)
29 
35 
36  if(suite == TLS_RSA_WITH_AES_128_CBC_SHA)
41 
42  if(suite == TLS_RSA_WITH_AES_256_CBC_SHA)
47 
48  if(suite == TLS_RSA_WITH_SEED_CBC_SHA)
53 
59 
65 
71 
77 
83 
89 
95 
101 
107 
113 
114  if(suite == TLS_DHE_DSS_WITH_SEED_CBC_SHA)
119 
125 
131 
137 
143 
149 
155 
161 
167 
173 
174  if(suite == TLS_ECDHE_RSA_WITH_RC4_128_SHA)
179 
185 
191 
197 
203 
209 
210  return TLS_Ciphersuite_Algos(0);
211  }
212 
213 namespace {
214 
215 std::pair<std::string, size_t> cipher_code_to_name(TLS_Ciphersuite_Algos algo)
216  {
218  return std::make_pair("ARC4", 16);
219 
220  if((algo & TLS_ALGO_CIPHER_MASK) == TLS_ALGO_CIPHER_3DES_CBC)
221  return std::make_pair("3DES", 24);
222 
223  if((algo & TLS_ALGO_CIPHER_MASK) == TLS_ALGO_CIPHER_AES128_CBC)
224  return std::make_pair("AES-128", 16);
225 
226  if((algo & TLS_ALGO_CIPHER_MASK) == TLS_ALGO_CIPHER_AES256_CBC)
227  return std::make_pair("AES-256", 32);
228 
229  if((algo & TLS_ALGO_CIPHER_MASK) == TLS_ALGO_CIPHER_SEED_CBC)
230  return std::make_pair("SEED", 16);
231 
233  "CipherSuite: Unknown cipher type " + to_string(algo));
234  }
235 
236 std::string mac_code_to_name(TLS_Ciphersuite_Algos algo)
237  {
238  if((algo & TLS_ALGO_MAC_MASK) == TLS_ALGO_MAC_MD5)
239  return "MD5";
240 
241  if((algo & TLS_ALGO_MAC_MASK) == TLS_ALGO_MAC_SHA1)
242  return "SHA-1";
243 
244  if((algo & TLS_ALGO_MAC_MASK) == TLS_ALGO_MAC_SHA256)
245  return "SHA-256";
246 
247  if((algo & TLS_ALGO_MAC_MASK) == TLS_ALGO_MAC_SHA384)
248  return "SHA-384";
249 
250  throw TLS_Exception(INTERNAL_ERROR,
251  "CipherSuite: Unknown MAC type " + to_string(algo));
252  }
253 
254 }
255 
256 /**
257 * CipherSuite Constructor
258 */
260  {
261  if(suite_code == 0)
262  return;
263 
264  TLS_Ciphersuite_Algos algos = lookup_ciphersuite(suite_code);
265 
266  if(algos == 0)
267  throw Invalid_Argument("Unknown ciphersuite: " + to_string(suite_code));
268 
269  sig_algo = TLS_Ciphersuite_Algos(algos & TLS_ALGO_SIGNER_MASK);
270 
271  kex_algo = TLS_Ciphersuite_Algos(algos & TLS_ALGO_KEYEXCH_MASK);
272 
273  std::pair<std::string, size_t> cipher_info = cipher_code_to_name(algos);
274 
275  cipher = cipher_info.first;
276  cipher_key_length = cipher_info.second;
277 
278  mac = mac_code_to_name(algos);
279  }
280 
281 }
static TLS_Ciphersuite_Algos lookup_ciphersuite(u16bit suite)
Definition: tls_suites.cpp:16
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
unsigned short u16bit
Definition: types.h:27
std::string to_string(u64bit n, size_t min_len)
Definition: parsing.cpp:42
TLS_Ciphersuite_Algos
Definition: tls_magic.h:145
CipherSuite(u16bit=0)
Definition: tls_suites.cpp:259