Botan 3.6.1
Crypto and TLS for C&
tls_ciphersuite.h
Go to the documentation of this file.
1/*
2* TLS Cipher Suites
3* (C) 2004-2011,2012 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_TLS_CIPHER_SUITES_H_
9#define BOTAN_TLS_CIPHER_SUITES_H_
10
11#include <botan/tls_algos.h>
12#include <botan/tls_version.h>
13#include <botan/types.h>
14#include <optional>
15#include <string>
16#include <vector>
17
18namespace Botan::TLS {
19
20/**
21* Ciphersuite Information
22*/
24 public:
25 /**
26 * Convert an SSL/TLS ciphersuite to algorithm fields
27 * @param suite the ciphersuite code number
28 * @return ciphersuite object or std::nullopt if it is unknown to the library
29 */
30 static std::optional<Ciphersuite> by_id(uint16_t suite);
31
32 /**
33 * Convert an SSL/TLS ciphersuite name to algorithm fields
34 * @param name the IANA name for the desired ciphersuite
35 * @return ciphersuite object or std::nullopt if it is unknown to the library
36 */
37 static std::optional<Ciphersuite> from_name(std::string_view name);
38
39 /**
40 * Returns true iff this suite is a known SCSV
41 */
42 static bool is_scsv(uint16_t suite);
43
44 /**
45 * Generate a static list of all known ciphersuites and return it.
46 *
47 * @return list of all known ciphersuites
48 */
49 static const std::vector<Ciphersuite>& all_known_ciphersuites();
50
51 /**
52 * Formats the ciphersuite back to an RFC-style ciphersuite string
53 *
54 * e.g "RSA_WITH_RC4_128_SHA" or "ECDHE_RSA_WITH_AES_128_GCM_SHA256"
55 * @return RFC ciphersuite string identifier
56 */
57 std::string to_string() const { return (!m_iana_id) ? "unknown cipher suite" : m_iana_id; }
58
59 /**
60 * @return ciphersuite number
61 */
62 uint16_t ciphersuite_code() const { return m_ciphersuite_code; }
63
64 /**
65 * @return true if this is a PSK ciphersuite
66 */
67 bool psk_ciphersuite() const;
68
69 /**
70 * @return true if this is an ECC ciphersuite
71 */
72 bool ecc_ciphersuite() const;
73
74 /**
75 * @return true if this suite uses a CBC cipher
76 */
77 bool cbc_ciphersuite() const;
78
79 /**
80 * @return true if this suite uses a AEAD cipher
81 */
82 bool aead_ciphersuite() const;
83
84 bool signature_used() const;
85
86 /**
87 * @return key exchange algorithm used by this ciphersuite
88 */
89 std::string kex_algo() const { return kex_method_to_string(kex_method()); }
90
91 Kex_Algo kex_method() const { return m_kex_algo; }
92
93 /**
94 * @return signature algorithm used by this ciphersuite
95 */
96 std::string sig_algo() const { return auth_method_to_string(auth_method()); }
97
98 Auth_Method auth_method() const { return m_auth_method; }
99
100 /**
101 * @return symmetric cipher algorithm used by this ciphersuite
102 */
103 std::string cipher_algo() const { return m_cipher_algo; }
104
105 /**
106 * @return message authentication algorithm used by this ciphersuite
107 */
108 std::string mac_algo() const { return m_mac_algo; }
109
110 std::string prf_algo() const { return kdf_algo_to_string(m_prf_algo); }
111
112 /**
113 * @return cipher key length used by this ciphersuite
114 */
115 size_t cipher_keylen() const { return m_cipher_keylen; }
116
117 size_t nonce_bytes_from_handshake() const;
118
119 size_t nonce_bytes_from_record(Protocol_Version version) const;
120
121 Nonce_Format nonce_format() const { return m_nonce_format; }
122
123 size_t mac_keylen() const { return m_mac_keylen; }
124
125 /**
126 * @return true if this is a valid/known ciphersuite
127 */
128 bool valid() const { return m_usable; }
129
130 bool usable_in_version(Protocol_Version version) const;
131
132 bool operator<(const Ciphersuite& o) const { return ciphersuite_code() < o.ciphersuite_code(); }
133
134 bool operator<(const uint16_t c) const { return ciphersuite_code() < c; }
135
136 private:
137 bool is_usable() const;
138
139 Ciphersuite(uint16_t ciphersuite_code,
140 const char* iana_id,
141 Auth_Method auth_method,
142 Kex_Algo kex_algo,
143 const char* cipher_algo,
144 size_t cipher_keylen,
145 const char* mac_algo,
146 size_t mac_keylen,
147 KDF_Algo prf_algo,
148 Nonce_Format nonce_format) :
149 m_ciphersuite_code(ciphersuite_code),
150 m_iana_id(iana_id),
151 m_auth_method(auth_method),
152 m_kex_algo(kex_algo),
153 m_prf_algo(prf_algo),
154 m_nonce_format(nonce_format),
155 m_cipher_algo(cipher_algo),
156 m_mac_algo(mac_algo),
157 m_cipher_keylen(cipher_keylen),
158 m_mac_keylen(mac_keylen) {
159 m_usable = is_usable();
160 }
161
162 uint16_t m_ciphersuite_code = 0;
163
164 /*
165 All of these const char* strings are references to compile time
166 constants in tls_suite_info.cpp
167 */
168 const char* m_iana_id;
169
170 Auth_Method m_auth_method;
171 Kex_Algo m_kex_algo;
172 KDF_Algo m_prf_algo;
173 Nonce_Format m_nonce_format;
174
175 const char* m_cipher_algo;
176 const char* m_mac_algo;
177
178 size_t m_cipher_keylen;
179 size_t m_mac_keylen;
180
181 bool m_usable = false;
182};
183
184} // namespace Botan::TLS
185
186#endif
uint16_t ciphersuite_code() const
bool operator<(const uint16_t c) const
Auth_Method auth_method() const
Nonce_Format nonce_format() const
std::string to_string() const
Kex_Algo kex_method() const
std::string kex_algo() const
std::string mac_algo() const
std::string sig_algo() const
std::string prf_algo() const
bool operator<(const Ciphersuite &o) const
std::string cipher_algo() const
std::string name
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
std::string kdf_algo_to_string(KDF_Algo algo)
Definition tls_algos.cpp:15
std::string kex_method_to_string(Kex_Algo method)
Definition tls_algos.cpp:28
std::string auth_method_to_string(Auth_Method method)