aboutsummaryrefslogtreecommitdiffstats
path: root/src/ssl/tls_suites.cpp
blob: cf6bd45e5f13a93f3f4159c51c44df8b1e0fc8b5 (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
/**
* TLS Cipher Suites
* (C) 2004-2010 Jack Lloyd
*
* Released under the terms of the Botan license
*/

#include <botan/tls_suites.h>
#include <botan/tls_exceptn.h>

namespace Botan {

namespace {

/**
* Convert an SSL/TLS ciphersuite to algorithm fields
*/
TLS_Ciphersuite_Algos lookup_ciphersuite(u16bit suite)
   {
   if(suite == TLS_RSA_WITH_RC4_128_MD5)
      return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA |
                                   TLS_ALGO_KEYEXCH_NOKEX |
                                   TLS_ALGO_MAC_MD5 |
                                   TLS_ALGO_CIPHER_RC4_128);

   if(suite == TLS_RSA_WITH_RC4_128_SHA)
      return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA |
                                   TLS_ALGO_KEYEXCH_NOKEX |
                                   TLS_ALGO_MAC_SHA1 |
                                   TLS_ALGO_CIPHER_RC4_128);

   if(suite == TLS_RSA_WITH_3DES_EDE_CBC_SHA)
      return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA |
                                   TLS_ALGO_KEYEXCH_NOKEX |
                                   TLS_ALGO_MAC_SHA1 |
                                   TLS_ALGO_CIPHER_3DES_CBC);

   if(suite == TLS_RSA_WITH_AES_128_CBC_SHA)
      return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA |
                                   TLS_ALGO_KEYEXCH_NOKEX |
                                   TLS_ALGO_MAC_SHA1 |
                                   TLS_ALGO_CIPHER_AES128_CBC);

   if(suite == TLS_RSA_WITH_AES_256_CBC_SHA)
      return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA |
                                   TLS_ALGO_KEYEXCH_NOKEX |
                                   TLS_ALGO_MAC_SHA1 |
                                   TLS_ALGO_CIPHER_AES256_CBC);

   if(suite == TLS_RSA_WITH_AES_128_CBC_SHA256)
      return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA |
                                   TLS_ALGO_KEYEXCH_NOKEX |
                                   TLS_ALGO_MAC_SHA256 |
                                   TLS_ALGO_CIPHER_AES128_CBC);

   if(suite == TLS_RSA_WITH_AES_256_CBC_SHA256)
      return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA |
                                   TLS_ALGO_KEYEXCH_NOKEX |
                                   TLS_ALGO_MAC_SHA256 |
                                   TLS_ALGO_CIPHER_AES256_CBC);

   if(suite == TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA)
      return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_DSA |
                                   TLS_ALGO_KEYEXCH_DH |
                                   TLS_ALGO_MAC_SHA1 |
                                   TLS_ALGO_CIPHER_3DES_CBC);

   if(suite == TLS_DHE_DSS_WITH_AES_128_CBC_SHA)
      return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_DSA |
                                   TLS_ALGO_KEYEXCH_DH |
                                   TLS_ALGO_MAC_SHA1 |
                                   TLS_ALGO_CIPHER_AES128_CBC);

   if(suite == TLS_DHE_DSS_WITH_AES_256_CBC_SHA)
      return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_DSA |
                                   TLS_ALGO_KEYEXCH_DH |
                                   TLS_ALGO_MAC_SHA1 |
                                   TLS_ALGO_CIPHER_AES256_CBC);

   if(suite == TLS_DHE_DSS_WITH_AES_128_CBC_SHA256)
      return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_DSA |
                                   TLS_ALGO_KEYEXCH_DH |
                                   TLS_ALGO_MAC_SHA256 |
                                   TLS_ALGO_CIPHER_AES128_CBC);

   if(suite == TLS_DHE_DSS_WITH_AES_256_CBC_SHA256)
      return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_DSA |
                                   TLS_ALGO_KEYEXCH_DH |
                                   TLS_ALGO_MAC_SHA256 |
                                   TLS_ALGO_CIPHER_AES256_CBC);

   if(suite == TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA)
      return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA |
                                   TLS_ALGO_KEYEXCH_DH |
                                   TLS_ALGO_MAC_SHA1 |
                                   TLS_ALGO_CIPHER_3DES_CBC);

   if(suite == TLS_DHE_RSA_WITH_AES_128_CBC_SHA)
      return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA |
                                   TLS_ALGO_KEYEXCH_DH |
                                   TLS_ALGO_MAC_SHA1 |
                                   TLS_ALGO_CIPHER_AES128_CBC);

   if(suite == TLS_DHE_RSA_WITH_AES_256_CBC_SHA)
      return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA |
                                   TLS_ALGO_KEYEXCH_DH |
                                   TLS_ALGO_MAC_SHA1 |
                                   TLS_ALGO_CIPHER_AES256_CBC);

   if(suite == TLS_DHE_RSA_WITH_AES_128_CBC_SHA256)
      return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA |
                                   TLS_ALGO_KEYEXCH_DH |
                                   TLS_ALGO_MAC_SHA256 |
                                   TLS_ALGO_CIPHER_AES128_CBC);

   if(suite == TLS_DHE_RSA_WITH_AES_256_CBC_SHA256)
      return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_RSA |
                                   TLS_ALGO_KEYEXCH_DH |
                                   TLS_ALGO_MAC_SHA256 |
                                   TLS_ALGO_CIPHER_AES256_CBC);

   if(suite == TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256)
      return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_ECDSA |
                                   TLS_ALGO_KEYEXCH_ECDH |
                                   TLS_ALGO_MAC_SHA256 |
                                   TLS_ALGO_CIPHER_AES128_CBC);

   if(suite == TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384)
      return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_ECDSA |
                                   TLS_ALGO_KEYEXCH_ECDH |
                                   TLS_ALGO_MAC_SHA384 |
                                   TLS_ALGO_CIPHER_AES256_CBC);

   if(suite == TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256)
      return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_ECDSA |
                                   TLS_ALGO_KEYEXCH_ECDH |
                                   TLS_ALGO_MAC_SHA256 |
                                   TLS_ALGO_CIPHER_AES128_CBC);

   if(suite == TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384)
      return TLS_Ciphersuite_Algos(TLS_ALGO_SIGNER_ECDSA |
                                   TLS_ALGO_KEYEXCH_ECDH |
                                   TLS_ALGO_MAC_SHA384 |
                                   TLS_ALGO_CIPHER_AES256_CBC);

   return TLS_Ciphersuite_Algos(0);
   }

std::pair<std::string, u32bit> cipher_code_to_name(TLS_Ciphersuite_Algos algo)
   {
   if((algo & TLS_ALGO_CIPHER_MASK) == TLS_ALGO_CIPHER_RC4_128)
      return std::make_pair("RC4", 128);

   if((algo & TLS_ALGO_CIPHER_MASK) == TLS_ALGO_CIPHER_3DES_CBC)
      return std::make_pair("3DES", 24);

   if((algo & TLS_ALGO_CIPHER_MASK) == TLS_ALGO_CIPHER_AES128_CBC)
      return std::make_pair("AES-128", 16);

   if((algo & TLS_ALGO_CIPHER_MASK) == TLS_ALGO_CIPHER_AES256_CBC)
      return std::make_pair("AES-256", 32);

   if((algo & TLS_ALGO_CIPHER_MASK) == TLS_ALGO_CIPHER_SEED_CBC)
      return std::make_pair("SEED", 16);

   throw TLS_Exception(INTERNAL_ERROR,
                       "CipherSuite: Unknown cipher type " + to_string(algo));
   }

std::string mac_code_to_name(TLS_Ciphersuite_Algos algo)
   {
   if((algo & TLS_ALGO_MAC_MASK) == TLS_ALGO_MAC_MD5)
      return "MD5";

   if((algo & TLS_ALGO_MAC_MASK) == TLS_ALGO_MAC_SHA1)
      return "SHA-1";

   if((algo & TLS_ALGO_MAC_MASK) == TLS_ALGO_MAC_SHA256)
      return "SHA-256";

   if((algo & TLS_ALGO_MAC_MASK) == TLS_ALGO_MAC_SHA384)
      return "SHA-384";

   throw TLS_Exception(INTERNAL_ERROR,
                       "CipherSuite: Unknown MAC type " + to_string(algo));
   }

}

/**
* CipherSuite Constructor
*/
CipherSuite::CipherSuite(u16bit suite_code)
   {
   if(suite_code == 0)
      return;

   TLS_Ciphersuite_Algos algos = lookup_ciphersuite(suite_code);

   if(algos == 0)
      throw Invalid_Argument("Unknown ciphersuite: " + to_string(suite_code));

   sig_algo = TLS_Ciphersuite_Algos(algos & TLS_ALGO_SIGNER_MASK);

   kex_algo = TLS_Ciphersuite_Algos(algos & TLS_ALGO_KEYEXCH_MASK);

   std::pair<std::string, u32bit> cipher_info = cipher_code_to_name(algos);

   cipher = cipher_info.first;
   cipher_key_length = cipher_info.second;

   mac = mac_code_to_name(algos);
   }

}