Botan  2.9.0
Crypto and TLS for C++11
tls_algos.cpp
Go to the documentation of this file.
1 /*
2 * (C) 2017 Jack Lloyd
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6 
7 #include <botan/tls_algos.h>
8 #include <botan/exceptn.h>
9 
10 namespace Botan {
11 
12 namespace TLS {
13 
14 std::string kdf_algo_to_string(KDF_Algo algo)
15  {
16  switch(algo)
17  {
18  case KDF_Algo::SHA_1:
19  return "SHA-1";
20  case KDF_Algo::SHA_256:
21  return "SHA-256";
22  case KDF_Algo::SHA_384:
23  return "SHA-384";
24  }
25 
26  throw Invalid_State("kdf_algo_to_string unknown enum value");
27  }
28 
29 std::string kex_method_to_string(Kex_Algo method)
30  {
31  switch(method)
32  {
34  return "RSA";
35  case Kex_Algo::DH:
36  return "DH";
37  case Kex_Algo::ECDH:
38  return "ECDH";
39  case Kex_Algo::CECPQ1:
40  return "CECPQ1";
41  case Kex_Algo::SRP_SHA:
42  return "SRP_SHA";
43  case Kex_Algo::PSK:
44  return "PSK";
45  case Kex_Algo::DHE_PSK:
46  return "DHE_PSK";
48  return "ECDHE_PSK";
49  }
50 
51  throw Invalid_State("kex_method_to_string unknown enum value");
52  }
53 
54 Kex_Algo kex_method_from_string(const std::string& str)
55  {
56  if(str == "RSA")
57  return Kex_Algo::STATIC_RSA;
58 
59  if(str == "DH")
60  return Kex_Algo::DH;
61 
62  if(str == "ECDH")
63  return Kex_Algo::ECDH;
64 
65  if(str == "CECPQ1")
66  return Kex_Algo::CECPQ1;
67 
68  if(str == "SRP_SHA")
69  return Kex_Algo::SRP_SHA;
70 
71  if(str == "PSK")
72  return Kex_Algo::PSK;
73 
74  if(str == "DHE_PSK")
75  return Kex_Algo::DHE_PSK;
76 
77  if(str == "ECDHE_PSK")
78  return Kex_Algo::ECDHE_PSK;
79 
80  throw Invalid_Argument("Unknown kex method " + str);
81  }
82 
84  {
85  switch(method)
86  {
87  case Auth_Method::RSA:
88  return "RSA";
89  case Auth_Method::DSA:
90  return "DSA";
91  case Auth_Method::ECDSA:
92  return "ECDSA";
94  return "IMPLICIT";
96  return "ANONYMOUS";
97  }
98 
99  throw Invalid_State("auth_method_to_string unknown enum value");
100  }
101 
102 Auth_Method auth_method_from_string(const std::string& str)
103  {
104  if(str == "RSA")
105  return Auth_Method::RSA;
106  if(str == "DSA")
107  return Auth_Method::DSA;
108  if(str == "ECDSA")
109  return Auth_Method::ECDSA;
110  if(str == "IMPLICIT")
111  return Auth_Method::IMPLICIT;
112  if(str == "ANONYMOUS" || str == "")
113  return Auth_Method::ANONYMOUS;
114 
115  throw Invalid_Argument("Bad signature method " + str);
116  }
117 
119  {
120  uint16_t group_id = static_cast<uint16_t>(group);
121  return (group_id >= 256 && group_id < 512);
122  }
123 
124 Group_Params group_param_from_string(const std::string& group_name)
125  {
126  if(group_name == "secp256r1")
128  if(group_name == "secp384r1")
130  if(group_name == "secp521r1")
132  if(group_name == "brainpool256r1")
134  if(group_name == "brainpool384r1")
136  if(group_name == "brainpool512r1")
138  if(group_name == "x25519")
139  return Group_Params::X25519;
140 
141  if(group_name == "ffdhe/ietf/2048")
143  if(group_name == "ffdhe/ietf/3072")
145  if(group_name == "ffdhe/ietf/4096")
147  if(group_name == "ffdhe/ietf/6144")
149  if(group_name == "ffdhe/ietf/8192")
151 
152  return Group_Params::NONE; // unknown
153  }
154 
156  {
157  switch(group)
158  {
160  return "secp256r1";
162  return "secp384r1";
164  return "secp521r1";
166  return "brainpool256r1";
168  return "brainpool384r1";
170  return "brainpool512r1";
172  return "x25519";
173 
175  return "ffdhe/ietf/2048";
177  return "ffdhe/ietf/3072";
179  return "ffdhe/ietf/4096";
181  return "ffdhe/ietf/6144";
183  return "ffdhe/ietf/8192";
184 
185  default:
186  return "";
187  }
188  }
189 
190 
192  {
193  switch(scheme)
194  {
198  return "SHA-1";
199 
204  return "SHA-256";
205 
210  return "SHA-384";
211 
216  return "SHA-512";
217 
220  return "Pure";
221 
223  return "";
224  }
225 
226  throw Invalid_State("hash_function_of_scheme: Unknown signature algorithm enum");
227  }
228 
229 const std::vector<Signature_Scheme>& all_signature_schemes()
230  {
231  /*
232  * This is ordered in some approximate order of preference
233  */
234  static const std::vector<Signature_Scheme> all_schemes = {
235  //Signature_Scheme::EDDSA_448,
236  //Signature_Scheme::EDDSA_25519,
237 
241 
245 
249 
253 
257  };
258 
259  return all_schemes;
260  }
261 
263  {
264  switch(scheme)
265  {
273 
278 
283  return true;
284 
285  default:
286  return false;
287  }
288 
289  }
290 
292  {
293  switch(scheme)
294  {
302  return "RSA";
303 
308  return "DSA";
309 
314  return "ECDSA";
315 
317  return "Ed25519";
318 
320  return "Ed448";
321 
323  return "";
324  }
325 
326  throw Invalid_State("signature_algorithm_of_scheme: Unknown signature algorithm enum");
327  }
328 
330  {
331  switch(scheme)
332  {
334  return "RSA_PKCS1_SHA1";
336  return "RSA_PKCS1_SHA256";
338  return "RSA_PKCS1_SHA384";
340  return "RSA_PKCS1_SHA512";
341 
343  return "DSA_SHA1";
345  return "DSA_SHA256";
347  return "DSA_SHA384";
349  return "DSA_SHA512";
350 
352  return "ECDSA_SHA1";
354  return "ECDSA_SHA256";
356  return "ECDSA_SHA384";
358  return "ECDSA_SHA512";
359 
361  return "RSA_PSS_SHA256";
363  return "RSA_PSS_SHA384";
365  return "RSA_PSS_SHA512";
366 
368  return "EDDSA_25519";
370  return "EDDSA_448";
371 
373  return "";
374  }
375 
376  throw Invalid_State("sig_scheme_to_string: Unknown signature algorithm enum");
377  }
378 
380  {
381  switch(scheme)
382  {
384  return "EMSA_PKCS1(SHA-1)";
386  return "EMSA_PKCS1(SHA-256)";
388  return "EMSA_PKCS1(SHA-384)";
390  return "EMSA_PKCS1(SHA-512)";
391 
394  return "EMSA1(SHA-1)";
397  return "EMSA1(SHA-256)";
400  return "EMSA1(SHA-384)";
403  return "EMSA1(SHA-512)";
404 
406  return "PSSR(SHA-256,MGF1,32)";
408  return "PSSR(SHA-384,MGF1,48)";
410  return "PSSR(SHA-512,MGF1,64)";
411 
413  return "Pure";
415  return "Pure";
416 
418  return "";
419  }
420 
421  throw Invalid_State("padding_string_for_scheme: Unknown signature algorithm enum");
422  }
423 
424 }
425 
426 }
Auth_Method auth_method_from_string(const std::string &str)
Definition: tls_algos.cpp:102
std::string padding_string_for_scheme(Signature_Scheme scheme)
Definition: tls_algos.cpp:379
std::string sig_scheme_to_string(Signature_Scheme scheme)
Definition: tls_algos.cpp:329
std::string hash_function_of_scheme(Signature_Scheme scheme)
Definition: tls_algos.cpp:191
bool group_param_is_dh(Group_Params group)
Definition: tls_algos.cpp:118
Signature_Scheme
Definition: tls_algos.h:84
std::string group_param_to_string(Group_Params group)
Definition: tls_algos.cpp:155
std::string kex_method_to_string(Kex_Algo method)
Definition: tls_algos.cpp:29
Definition: alg_id.cpp:13
const std::vector< Signature_Scheme > & all_signature_schemes()
Definition: tls_algos.cpp:229
std::string auth_method_to_string(Auth_Method method)
Definition: tls_algos.cpp:83
std::string signature_algorithm_of_scheme(Signature_Scheme scheme)
Definition: tls_algos.cpp:291
std::string kdf_algo_to_string(KDF_Algo algo)
Definition: tls_algos.cpp:14
bool signature_scheme_is_known(Signature_Scheme scheme)
Definition: tls_algos.cpp:262
Group_Params group_param_from_string(const std::string &group_name)
Definition: tls_algos.cpp:124
Kex_Algo kex_method_from_string(const std::string &str)
Definition: tls_algos.cpp:54