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
|
/*
* TLS Cipher Suite
* (C) 2004-2010,2012,2013 Jack Lloyd
*
* Released under the terms of the Botan license
*/
#include <botan/tls_ciphersuite.h>
#include <botan/libstate.h>
#include <botan/parsing.h>
#include <sstream>
#include <stdexcept>
namespace Botan {
namespace TLS {
namespace {
/*
* This way all work happens at the constuctor call, and we can
* rely on that happening only once in C++11.
*/
std::vector<Ciphersuite> gather_known_ciphersuites()
{
std::vector<Ciphersuite> ciphersuites;
for(size_t i = 0; i <= 0xFFFF; ++i)
{
Ciphersuite suite = Ciphersuite::by_id(i);
if(suite.valid())
ciphersuites.push_back(suite);
}
return ciphersuites;
}
}
const std::vector<Ciphersuite>& Ciphersuite::all_known_ciphersuites()
{
static std::vector<Ciphersuite> all_ciphersuites(gather_known_ciphersuites());
return all_ciphersuites;
}
Ciphersuite Ciphersuite::by_name(const std::string& name)
{
for(auto suite : all_known_ciphersuites())
{
if(suite.to_string() == name)
return suite;
}
return Ciphersuite(); // some unknown ciphersuite
}
bool Ciphersuite::is_scsv(u16bit suite)
{
// TODO: derive from IANA file in script
return (suite == 0x00FF || suite == 0x5600);
}
Ciphersuite::Ciphersuite(u16bit ciphersuite_code,
const char* sig_algo,
const char* kex_algo,
const char* cipher_algo,
size_t cipher_keylen,
size_t exp_nonce_bytes,
size_t imp_nonce_bytes,
const char* mac_algo,
size_t mac_keylen,
const char* prf_algo) :
m_ciphersuite_code(ciphersuite_code),
m_sig_algo(sig_algo),
m_kex_algo(kex_algo),
m_prf_algo(prf_algo),
m_cipher_algo(cipher_algo),
m_cipher_keylen(cipher_keylen),
m_explicit_nonce_bytes(exp_nonce_bytes),
m_implicit_nonce_bytes(imp_nonce_bytes),
m_mac_algo(mac_algo),
m_mac_keylen(mac_keylen)
{
}
bool Ciphersuite::psk_ciphersuite() const
{
return (kex_algo() == "PSK" ||
kex_algo() == "DHE_PSK" ||
kex_algo() == "ECDHE_PSK");
}
bool Ciphersuite::ecc_ciphersuite() const
{
return (sig_algo() == "ECDSA" || kex_algo() == "ECDH" || kex_algo() == "ECDHE_PSK");
}
bool Ciphersuite::valid() const
{
if(!m_cipher_keylen) // uninitialized object
return false;
Algorithm_Factory& af = global_state().algorithm_factory();
if(!af.prototype_hash_function(prf_algo()))
return false;
if(mac_algo() == "AEAD")
{
auto cipher_and_mode = split_on(cipher_algo(), '/');
BOTAN_ASSERT(cipher_and_mode.size() == 2, "Expected format for AEAD algo");
if(!af.prototype_block_cipher(cipher_and_mode[0]))
return false;
const auto mode = cipher_and_mode[1];
#if !defined(BOTAN_HAS_AEAD_CCM)
if(mode == "CCM" || mode == "CCM-8")
return false;
#endif
#if !defined(BOTAN_HAS_AEAD_GCM)
if(mode == "GCM")
return false;
#endif
#if !defined(BOTAN_HAS_AEAD_OCB)
if(mode == "OCB")
return false;
#endif
}
else
{
if(!af.prototype_block_cipher(cipher_algo()) &&
!af.prototype_stream_cipher(cipher_algo()))
return false;
if(!af.prototype_hash_function(mac_algo()))
return false;
}
if(kex_algo() == "SRP_SHA")
{
#if !defined(BOTAN_HAS_SRP6)
return false;
#endif
}
else if(kex_algo() == "ECDH" || kex_algo() == "ECDHE_PSK")
{
#if !defined(BOTAN_HAS_ECDH)
return false;
#endif
}
else if(kex_algo() == "DH" || kex_algo() == "DHE_PSK")
{
#if !defined(BOTAN_HAS_DIFFIE_HELLMAN)
return false;
#endif
}
if(sig_algo() == "DSA")
{
#if !defined(BOTAN_HAS_DSA)
return false;
#endif
}
else if(sig_algo() == "ECDSA")
{
#if !defined(BOTAN_HAS_ECDSA)
return false;
#endif
}
else if(sig_algo() == "RSA")
{
#if !defined(BOTAN_HAS_RSA)
return false;
#endif
}
return true;
}
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
out << kex_algo();
out << '_';
}
if(sig_algo() == "DSA")
out << "DSS_";
else if(sig_algo() != "")
out << sig_algo() << '_';
out << "WITH_";
if(cipher_algo() == "RC4")
{
out << "RC4_128_";
}
else
{
if(cipher_algo() == "3DES")
out << "3DES_EDE";
else if(cipher_algo().find("Camellia") == 0)
out << "CAMELLIA_" << std::to_string(8*cipher_keylen());
else
out << replace_chars(cipher_algo(), {'-', '/'}, '_');
if(cipher_algo().find("/") != std::string::npos)
out << "_"; // some explicit mode already included
else
out << "_CBC_";
}
if(mac_algo() == "SHA-1")
out << "SHA";
else if(mac_algo() == "AEAD")
out << erase_chars(prf_algo(), {'-'});
else
out << erase_chars(mac_algo(), {'-'});
return out.str();
}
}
}
|