Botan 3.13.0
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*/
23class BOTAN_PUBLIC_API(2, 0) Ciphersuite final {
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 == nullptr) ? "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 NULL cipher
81 */
82 bool null_ciphersuite() const;
83
84 /**
85 * @return true if this suite uses a AEAD cipher
86 */
87 bool aead_ciphersuite() const;
88
89 /**
90 * @return true if this suite uses a short (less than 128 bit)
91 * authentication tag, for instance one of the _CCM_8 suites.
92 */
93 bool uses_short_authentication_tag() const;
94
95 bool signature_used() const;
96
97 /**
98 * @return true if this ciphersuite requires the server to present
99 * a certificate. True for both signature-authenticated suites and
100 * static RSA key exchange (which uses the server's RSA cert for
101 * key transport).
102 */
103 bool is_certificate_required() const;
104
105 /**
106 * @return key exchange algorithm used by this ciphersuite
107 */
108 std::string kex_algo() const { return kex_method_to_string(kex_method()); }
109
110 Kex_Algo kex_method() const { return m_kex_algo; }
111
112 /**
113 * @return signature algorithm used by this ciphersuite
114 */
115 std::string sig_algo() const { return auth_method_to_string(auth_method()); }
116
117 Auth_Method auth_method() const { return m_auth_method; }
118
119 /**
120 * @return symmetric cipher algorithm used by this ciphersuite
121 */
122 std::string cipher_algo() const { return m_cipher_algo; }
123
124 /**
125 * @return message authentication algorithm used by this ciphersuite
126 */
127 std::string mac_algo() const { return m_mac_algo; }
128
129 std::string prf_algo() const { return kdf_algo_to_string(m_prf_algo); }
130
131 /**
132 * @return cipher key length used by this ciphersuite
133 */
134 size_t cipher_keylen() const { return m_cipher_keylen; }
135
136 size_t nonce_bytes_from_handshake() const;
137
138 size_t nonce_bytes_from_record(Protocol_Version version) const;
139
140 Nonce_Format nonce_format() const { return m_nonce_format; }
141
142 size_t mac_keylen() const { return m_mac_keylen; }
143
144 /**
145 * @return true if this is a valid/known ciphersuite
146 */
147 bool valid() const { return m_usable; }
148
149 bool usable_in_version(Protocol_Version version) const;
150
151 bool operator<(const Ciphersuite& o) const { return ciphersuite_code() < o.ciphersuite_code(); }
152
153 bool operator<(const uint16_t c) const { return ciphersuite_code() < c; }
154
155 private:
156 static bool is_known_usable(uint16_t code);
157
158 Ciphersuite(uint16_t ciphersuite_code,
159 const char* iana_id,
160 Auth_Method auth_method,
161 Kex_Algo kex_algo,
162 const char* cipher_algo,
163 size_t cipher_keylen,
164 const char* mac_algo,
165 size_t mac_keylen,
166 KDF_Algo prf_algo,
167 Nonce_Format nonce_format) :
168 m_ciphersuite_code(ciphersuite_code),
169 m_iana_id(iana_id),
170 m_auth_method(auth_method),
171 m_kex_algo(kex_algo),
172 m_prf_algo(prf_algo),
173 m_nonce_format(nonce_format),
174 m_cipher_algo(cipher_algo),
175 m_mac_algo(mac_algo),
176 m_cipher_keylen(cipher_keylen),
177 m_mac_keylen(mac_keylen),
178 m_usable(is_known_usable(ciphersuite_code)) {}
179
180 uint16_t m_ciphersuite_code = 0;
181
182 /*
183 All of these const char* strings are references to compile time
184 constants in tls_suite_info.cpp
185 */
186 const char* m_iana_id;
187
188 Auth_Method m_auth_method;
189 Kex_Algo m_kex_algo;
190 KDF_Algo m_prf_algo;
191 Nonce_Format m_nonce_format;
192
193 const char* m_cipher_algo;
194 const char* m_mac_algo;
195
196 size_t m_cipher_keylen;
197 size_t m_mac_keylen;
198
199 bool m_usable = false;
200};
201
202} // namespace Botan::TLS
203
204#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
uint16_t ciphersuite_code() const
bool operator<(const uint16_t c) const
static const std::vector< Ciphersuite > & all_known_ciphersuites()
Auth_Method auth_method() const
Nonce_Format nonce_format() const
std::string to_string() const
static std::optional< Ciphersuite > from_name(std::string_view name)
static bool is_scsv(uint16_t suite)
static std::optional< Ciphersuite > by_id(uint16_t suite)
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 kdf_algo_to_string(KDF_Algo algo)
Definition tls_algos.cpp:17
std::string kex_method_to_string(Kex_Algo method)
Definition tls_algos.cpp:30
std::string auth_method_to_string(Auth_Method method)