Botan 3.12.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 bool signature_used() const;
90
91 /**
92 * @return true if this ciphersuite requires the server to present
93 * a certificate. True for both signature-authenticated suites and
94 * static RSA key exchange (which uses the server's RSA cert for
95 * key transport).
96 */
97 bool is_certificate_required() const;
98
99 /**
100 * @return key exchange algorithm used by this ciphersuite
101 */
102 std::string kex_algo() const { return kex_method_to_string(kex_method()); }
103
104 Kex_Algo kex_method() const { return m_kex_algo; }
105
106 /**
107 * @return signature algorithm used by this ciphersuite
108 */
109 std::string sig_algo() const { return auth_method_to_string(auth_method()); }
110
111 Auth_Method auth_method() const { return m_auth_method; }
112
113 /**
114 * @return symmetric cipher algorithm used by this ciphersuite
115 */
116 std::string cipher_algo() const { return m_cipher_algo; }
117
118 /**
119 * @return message authentication algorithm used by this ciphersuite
120 */
121 std::string mac_algo() const { return m_mac_algo; }
122
123 std::string prf_algo() const { return kdf_algo_to_string(m_prf_algo); }
124
125 /**
126 * @return cipher key length used by this ciphersuite
127 */
128 size_t cipher_keylen() const { return m_cipher_keylen; }
129
130 size_t nonce_bytes_from_handshake() const;
131
132 size_t nonce_bytes_from_record(Protocol_Version version) const;
133
134 Nonce_Format nonce_format() const { return m_nonce_format; }
135
136 size_t mac_keylen() const { return m_mac_keylen; }
137
138 /**
139 * @return true if this is a valid/known ciphersuite
140 */
141 bool valid() const { return m_usable; }
142
143 bool usable_in_version(Protocol_Version version) const;
144
145 bool operator<(const Ciphersuite& o) const { return ciphersuite_code() < o.ciphersuite_code(); }
146
147 bool operator<(const uint16_t c) const { return ciphersuite_code() < c; }
148
149 private:
150 static bool is_known_usable(uint16_t code);
151
152 Ciphersuite(uint16_t ciphersuite_code,
153 const char* iana_id,
154 Auth_Method auth_method,
155 Kex_Algo kex_algo,
156 const char* cipher_algo,
157 size_t cipher_keylen,
158 const char* mac_algo,
159 size_t mac_keylen,
160 KDF_Algo prf_algo,
161 Nonce_Format nonce_format) :
162 m_ciphersuite_code(ciphersuite_code),
163 m_iana_id(iana_id),
164 m_auth_method(auth_method),
165 m_kex_algo(kex_algo),
166 m_prf_algo(prf_algo),
167 m_nonce_format(nonce_format),
168 m_cipher_algo(cipher_algo),
169 m_mac_algo(mac_algo),
170 m_cipher_keylen(cipher_keylen),
171 m_mac_keylen(mac_keylen),
172 m_usable(is_known_usable(ciphersuite_code)) {}
173
174 uint16_t m_ciphersuite_code = 0;
175
176 /*
177 All of these const char* strings are references to compile time
178 constants in tls_suite_info.cpp
179 */
180 const char* m_iana_id;
181
182 Auth_Method m_auth_method;
183 Kex_Algo m_kex_algo;
184 KDF_Algo m_prf_algo;
185 Nonce_Format m_nonce_format;
186
187 const char* m_cipher_algo;
188 const char* m_mac_algo;
189
190 size_t m_cipher_keylen;
191 size_t m_mac_keylen;
192
193 bool m_usable = false;
194};
195
196} // namespace Botan::TLS
197
198#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)