TLSv12.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include "Certificate.h"
  8. #include <AK/IPv4Address.h>
  9. #include <AK/Queue.h>
  10. #include <AK/WeakPtr.h>
  11. #include <LibCore/Notifier.h>
  12. #include <LibCore/Socket.h>
  13. #include <LibCore/Timer.h>
  14. #include <LibCrypto/Authentication/HMAC.h>
  15. #include <LibCrypto/BigInt/UnsignedBigInteger.h>
  16. #include <LibCrypto/Cipher/AES.h>
  17. #include <LibCrypto/Curves/EllipticCurve.h>
  18. #include <LibCrypto/Hash/HashManager.h>
  19. #include <LibCrypto/PK/RSA.h>
  20. #include <LibTLS/CipherSuite.h>
  21. #include <LibTLS/TLSPacketBuilder.h>
  22. namespace TLS {
  23. inline void print_buffer(ReadonlyBytes buffer)
  24. {
  25. dbgln("{:hex-dump}", buffer);
  26. }
  27. inline void print_buffer(ByteBuffer const& buffer)
  28. {
  29. print_buffer(buffer.bytes());
  30. }
  31. inline void print_buffer(u8 const* buffer, size_t size)
  32. {
  33. print_buffer(ReadonlyBytes { buffer, size });
  34. }
  35. class Socket;
  36. enum class Error : i8 {
  37. NoError = 0,
  38. UnknownError = -1,
  39. BrokenPacket = -2,
  40. NotUnderstood = -3,
  41. NoCommonCipher = -5,
  42. UnexpectedMessage = -6,
  43. CloseConnection = -7,
  44. CompressionNotSupported = -8,
  45. NotVerified = -9,
  46. NotSafe = -10,
  47. IntegrityCheckFailed = -11,
  48. ErrorAlert = -12,
  49. BrokenConnection = -13,
  50. BadCertificate = -14,
  51. UnsupportedCertificate = -15,
  52. NoRenegotiation = -16,
  53. FeatureNotSupported = -17,
  54. DecryptionFailed = -20,
  55. NeedMoreData = -21,
  56. TimedOut = -22,
  57. OutOfMemory = -23,
  58. };
  59. enum class WritePacketStage {
  60. Initial = 0,
  61. ClientHandshake = 1,
  62. ServerHandshake = 2,
  63. Finished = 3,
  64. };
  65. enum class ConnectionStatus {
  66. Disconnected,
  67. Negotiating,
  68. KeyExchange,
  69. Renegotiating,
  70. Established,
  71. };
  72. enum ClientVerificationStaus {
  73. Verified,
  74. VerificationNeeded,
  75. };
  76. // Note for the 16 iv length instead of 8:
  77. // 4 bytes of fixed IV, 8 random (nonce) bytes, 4 bytes for counter
  78. // GCM specifically asks us to transmit only the nonce, the counter is zero
  79. // and the fixed IV is derived from the premaster key.
  80. //
  81. // The cipher suite list below is ordered based on the recommendations from Mozilla.
  82. // When changing the supported cipher suites, please consult the webpage below for
  83. // the preferred order.
  84. //
  85. // https://wiki.mozilla.org/Security/Server_Side_TLS
  86. #define ENUMERATE_CIPHERS(C) \
  87. C(true, CipherSuite::TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, KeyExchangeAlgorithm::ECDHE_ECDSA, CipherAlgorithm::AES_128_GCM, Crypto::Hash::SHA256, 8, true) \
  88. C(true, CipherSuite::TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, KeyExchangeAlgorithm::ECDHE_RSA, CipherAlgorithm::AES_128_GCM, Crypto::Hash::SHA256, 8, true) \
  89. C(true, CipherSuite::TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, KeyExchangeAlgorithm::ECDHE_ECDSA, CipherAlgorithm::AES_256_GCM, Crypto::Hash::SHA384, 8, true) \
  90. C(true, CipherSuite::TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, KeyExchangeAlgorithm::ECDHE_RSA, CipherAlgorithm::AES_256_GCM, Crypto::Hash::SHA384, 8, true) \
  91. C(true, CipherSuite::TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, KeyExchangeAlgorithm::DHE_RSA, CipherAlgorithm::AES_128_GCM, Crypto::Hash::SHA256, 8, true) \
  92. C(true, CipherSuite::TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, KeyExchangeAlgorithm::DHE_RSA, CipherAlgorithm::AES_256_GCM, Crypto::Hash::SHA384, 8, true) \
  93. C(true, CipherSuite::TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, KeyExchangeAlgorithm::ECDHE_ECDSA, CipherAlgorithm::AES_128_CBC, Crypto::Hash::SHA1, 16, false) \
  94. C(true, CipherSuite::TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, KeyExchangeAlgorithm::ECDHE_RSA, CipherAlgorithm::AES_128_CBC, Crypto::Hash::SHA1, 16, false) \
  95. C(true, CipherSuite::TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, KeyExchangeAlgorithm::ECDHE_ECDSA, CipherAlgorithm::AES_256_CBC, Crypto::Hash::SHA1, 16, false) \
  96. C(true, CipherSuite::TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, KeyExchangeAlgorithm::ECDHE_RSA, CipherAlgorithm::AES_256_CBC, Crypto::Hash::SHA1, 16, false) \
  97. C(true, CipherSuite::TLS_RSA_WITH_AES_128_GCM_SHA256, KeyExchangeAlgorithm::RSA, CipherAlgorithm::AES_128_GCM, Crypto::Hash::SHA256, 8, true) \
  98. C(true, CipherSuite::TLS_RSA_WITH_AES_256_GCM_SHA384, KeyExchangeAlgorithm::RSA, CipherAlgorithm::AES_256_GCM, Crypto::Hash::SHA384, 8, true) \
  99. C(true, CipherSuite::TLS_RSA_WITH_AES_128_CBC_SHA256, KeyExchangeAlgorithm::RSA, CipherAlgorithm::AES_128_CBC, Crypto::Hash::SHA256, 16, false) \
  100. C(true, CipherSuite::TLS_RSA_WITH_AES_256_CBC_SHA256, KeyExchangeAlgorithm::RSA, CipherAlgorithm::AES_256_CBC, Crypto::Hash::SHA256, 16, false) \
  101. C(true, CipherSuite::TLS_RSA_WITH_AES_128_CBC_SHA, KeyExchangeAlgorithm::RSA, CipherAlgorithm::AES_128_CBC, Crypto::Hash::SHA1, 16, false) \
  102. C(true, CipherSuite::TLS_RSA_WITH_AES_256_CBC_SHA, KeyExchangeAlgorithm::RSA, CipherAlgorithm::AES_256_CBC, Crypto::Hash::SHA1, 16, false)
  103. constexpr KeyExchangeAlgorithm get_key_exchange_algorithm(CipherSuite suite)
  104. {
  105. switch (suite) {
  106. #define C(is_supported, suite, key_exchange, cipher, hash, iv_size, is_aead) \
  107. case suite: \
  108. return key_exchange;
  109. ENUMERATE_CIPHERS(C)
  110. #undef C
  111. default:
  112. return KeyExchangeAlgorithm::Invalid;
  113. }
  114. }
  115. constexpr CipherAlgorithm get_cipher_algorithm(CipherSuite suite)
  116. {
  117. switch (suite) {
  118. #define C(is_supported, suite, key_exchange, cipher, hash, iv_size, is_aead) \
  119. case suite: \
  120. return cipher;
  121. ENUMERATE_CIPHERS(C)
  122. #undef C
  123. default:
  124. return CipherAlgorithm::Invalid;
  125. }
  126. }
  127. struct Options {
  128. static Vector<CipherSuite> default_usable_cipher_suites()
  129. {
  130. Vector<CipherSuite> cipher_suites;
  131. #define C(is_supported, suite, key_exchange, cipher, hash, iv_size, is_aead) \
  132. if constexpr (is_supported) \
  133. cipher_suites.empend(suite);
  134. ENUMERATE_CIPHERS(C)
  135. #undef C
  136. return cipher_suites;
  137. }
  138. Vector<CipherSuite> usable_cipher_suites = default_usable_cipher_suites();
  139. #define OPTION_WITH_DEFAULTS(typ, name, ...) \
  140. static typ default_##name() \
  141. { \
  142. return typ { __VA_ARGS__ }; \
  143. } \
  144. typ name = default_##name(); \
  145. Options& set_##name(typ new_value)& \
  146. { \
  147. name = move(new_value); \
  148. return *this; \
  149. } \
  150. Options&& set_##name(typ new_value)&& \
  151. { \
  152. name = move(new_value); \
  153. return move(*this); \
  154. }
  155. OPTION_WITH_DEFAULTS(ProtocolVersion, version, ProtocolVersion::VERSION_1_2)
  156. OPTION_WITH_DEFAULTS(Vector<SignatureAndHashAlgorithm>, supported_signature_algorithms,
  157. { HashAlgorithm::SHA512, SignatureAlgorithm::RSA },
  158. { HashAlgorithm::SHA384, SignatureAlgorithm::RSA },
  159. { HashAlgorithm::SHA256, SignatureAlgorithm::RSA },
  160. { HashAlgorithm::SHA1, SignatureAlgorithm::RSA },
  161. { HashAlgorithm::SHA256, SignatureAlgorithm::ECDSA },
  162. { HashAlgorithm::SHA384, SignatureAlgorithm::ECDSA },
  163. { HashAlgorithm::INTRINSIC, SignatureAlgorithm::ED25519 });
  164. OPTION_WITH_DEFAULTS(Vector<SupportedGroup>, elliptic_curves,
  165. SupportedGroup::X25519,
  166. SupportedGroup::SECP256R1,
  167. SupportedGroup::SECP384R1,
  168. SupportedGroup::X448)
  169. OPTION_WITH_DEFAULTS(Vector<ECPointFormat>, supported_ec_point_formats, ECPointFormat::UNCOMPRESSED)
  170. OPTION_WITH_DEFAULTS(bool, use_sni, true)
  171. OPTION_WITH_DEFAULTS(bool, use_compression, false)
  172. OPTION_WITH_DEFAULTS(bool, validate_certificates, true)
  173. OPTION_WITH_DEFAULTS(bool, allow_self_signed_certificates, false)
  174. OPTION_WITH_DEFAULTS(Optional<Vector<Certificate>>, root_certificates, )
  175. OPTION_WITH_DEFAULTS(Function<void(AlertDescription)>, alert_handler, [](auto) {})
  176. OPTION_WITH_DEFAULTS(Function<void()>, finish_callback, [] {})
  177. OPTION_WITH_DEFAULTS(Function<Vector<Certificate>()>, certificate_provider, [] { return Vector<Certificate> {}; })
  178. OPTION_WITH_DEFAULTS(bool, enable_extended_master_secret, true)
  179. #undef OPTION_WITH_DEFAULTS
  180. };
  181. class SegmentedBuffer {
  182. public:
  183. [[nodiscard]] size_t size() const { return m_size; }
  184. [[nodiscard]] bool is_empty() const { return m_size == 0; }
  185. void transfer(Bytes dest, size_t size)
  186. {
  187. VERIFY(size <= dest.size());
  188. size_t transferred = 0;
  189. while (transferred < size) {
  190. auto& buffer = m_buffers.head();
  191. size_t to_transfer = min(buffer.size() - m_offset_into_current_buffer, size - transferred);
  192. memcpy(dest.offset(transferred), buffer.data() + m_offset_into_current_buffer, to_transfer);
  193. transferred += to_transfer;
  194. m_offset_into_current_buffer += to_transfer;
  195. if (m_offset_into_current_buffer >= buffer.size()) {
  196. m_buffers.dequeue();
  197. m_offset_into_current_buffer = 0;
  198. }
  199. m_size -= to_transfer;
  200. }
  201. }
  202. AK::ErrorOr<void> try_append(ReadonlyBytes data)
  203. {
  204. if (Checked<size_t>::addition_would_overflow(m_size, data.size()))
  205. return AK::Error::from_errno(EOVERFLOW);
  206. m_size += data.size();
  207. m_buffers.enqueue(TRY(ByteBuffer::copy(data)));
  208. return {};
  209. }
  210. private:
  211. size_t m_size { 0 };
  212. Queue<ByteBuffer> m_buffers;
  213. size_t m_offset_into_current_buffer { 0 };
  214. };
  215. struct Context {
  216. bool verify_chain(StringView host) const;
  217. bool verify_certificate_pair(Certificate const& subject, Certificate const& issuer) const;
  218. Options options;
  219. u8 remote_random[32];
  220. u8 local_random[32];
  221. u8 session_id[32];
  222. u8 session_id_size { 0 };
  223. CipherSuite cipher;
  224. bool is_server { false };
  225. Vector<Certificate> certificates;
  226. Certificate private_key;
  227. Vector<Certificate> client_certificates;
  228. ByteBuffer master_key;
  229. ByteBuffer premaster_key;
  230. u8 cipher_spec_set { 0 };
  231. struct {
  232. int created { 0 };
  233. u8 remote_mac[32];
  234. u8 local_mac[32];
  235. u8 local_iv[16];
  236. u8 remote_iv[16];
  237. u8 local_aead_iv[4];
  238. u8 remote_aead_iv[4];
  239. } crypto;
  240. Crypto::Hash::Manager handshake_hash;
  241. ByteBuffer message_buffer;
  242. u64 remote_sequence_number { 0 };
  243. u64 local_sequence_number { 0 };
  244. ConnectionStatus connection_status { ConnectionStatus::Disconnected };
  245. u8 critical_error { 0 };
  246. Error error_code { Error::NoError };
  247. ByteBuffer tls_buffer;
  248. SegmentedBuffer application_buffer;
  249. bool is_child { false };
  250. struct {
  251. // Server Name Indicator
  252. ByteString SNI; // I hate your existence
  253. bool extended_master_secret { false };
  254. } extensions;
  255. u8 request_client_certificate { 0 };
  256. ByteBuffer cached_handshake;
  257. ClientVerificationStaus client_verified { Verified };
  258. bool connection_finished { false };
  259. bool close_notify { false };
  260. bool has_invoked_finish_or_error_callback { false };
  261. // message flags
  262. u8 handshake_messages[11] { 0 };
  263. ByteBuffer user_data;
  264. HashMap<ByteString, Certificate> root_certificates;
  265. Vector<ByteString> alpn;
  266. StringView negotiated_alpn;
  267. size_t send_retries { 0 };
  268. time_t handshake_initiation_timestamp { 0 };
  269. struct {
  270. ByteBuffer p;
  271. ByteBuffer g;
  272. ByteBuffer Ys;
  273. } server_diffie_hellman_params;
  274. OwnPtr<Crypto::Curves::EllipticCurve> server_key_exchange_curve;
  275. };
  276. class TLSv12 final : public Core::Socket {
  277. private:
  278. Core::Socket& underlying_stream()
  279. {
  280. return *m_stream.visit([&](auto& stream) -> Core::Socket* { return stream; });
  281. }
  282. Core::Socket const& underlying_stream() const
  283. {
  284. return *m_stream.visit([&](auto& stream) -> Core::Socket const* { return stream; });
  285. }
  286. public:
  287. /// Reads into a buffer, with the maximum size being the size of the buffer.
  288. /// The amount of bytes read can be smaller than the size of the buffer.
  289. /// Returns either the bytes that were read, or an errno in the case of
  290. /// failure.
  291. virtual ErrorOr<Bytes> read_some(Bytes) override;
  292. /// Tries to write the entire contents of the buffer. It is possible for
  293. /// less than the full buffer to be written. Returns either the amount of
  294. /// bytes written into the stream, or an errno in the case of failure.
  295. virtual ErrorOr<size_t> write_some(ReadonlyBytes) override;
  296. virtual bool is_eof() const override { return m_context.application_buffer.is_empty() && (m_context.connection_finished || underlying_stream().is_eof()); }
  297. virtual bool is_open() const override { return is_established(); }
  298. virtual void close() override;
  299. virtual ErrorOr<size_t> pending_bytes() const override { return m_context.application_buffer.size(); }
  300. virtual ErrorOr<bool> can_read_without_blocking(int = 0) const override { return !m_context.application_buffer.is_empty(); }
  301. virtual ErrorOr<void> set_blocking(bool block) override
  302. {
  303. VERIFY(!block);
  304. return {};
  305. }
  306. virtual ErrorOr<void> set_close_on_exec(bool enabled) override { return underlying_stream().set_close_on_exec(enabled); }
  307. virtual void set_notifications_enabled(bool enabled) override { underlying_stream().set_notifications_enabled(enabled); }
  308. static ErrorOr<NonnullOwnPtr<TLSv12>> connect(ByteString const& host, u16 port, Options = {});
  309. static ErrorOr<NonnullOwnPtr<TLSv12>> connect(ByteString const& host, Core::Socket& underlying_stream, Options = {});
  310. using StreamVariantType = Variant<OwnPtr<Core::Socket>, Core::Socket*>;
  311. explicit TLSv12(StreamVariantType, Options);
  312. bool is_established() const { return m_context.connection_status == ConnectionStatus::Established; }
  313. void set_sni(StringView sni)
  314. {
  315. if (m_context.is_server || m_context.critical_error || m_context.connection_status != ConnectionStatus::Disconnected) {
  316. dbgln("invalid state for set_sni");
  317. return;
  318. }
  319. m_context.extensions.SNI = sni;
  320. }
  321. void set_root_certificates(Vector<Certificate>);
  322. static Vector<Certificate> parse_pem_certificate(ReadonlyBytes certificate_pem_buffer, ReadonlyBytes key_pem_buffer);
  323. StringView alpn() const { return m_context.negotiated_alpn; }
  324. bool supports_cipher(CipherSuite suite) const
  325. {
  326. switch (suite) {
  327. #define C(is_supported, suite, key_exchange, cipher, hash, iv_size, is_aead) \
  328. case suite: \
  329. return is_supported;
  330. ENUMERATE_CIPHERS(C)
  331. #undef C
  332. default:
  333. return false;
  334. }
  335. }
  336. bool supports_version(ProtocolVersion v) const
  337. {
  338. return v == ProtocolVersion::VERSION_1_2;
  339. }
  340. void alert(AlertLevel, AlertDescription);
  341. Function<void(AlertDescription)> on_tls_error;
  342. Function<void()> on_tls_finished;
  343. Function<void(TLSv12&)> on_tls_certificate_request;
  344. Function<void()> on_connected;
  345. private:
  346. void setup_connection();
  347. void consume(ReadonlyBytes record);
  348. ByteBuffer hmac_message(ReadonlyBytes buf, Optional<ReadonlyBytes> const buf2, size_t mac_length, bool local = false);
  349. void ensure_hmac(size_t digest_size, bool local);
  350. void update_packet(ByteBuffer& packet);
  351. void update_hash(ReadonlyBytes in, size_t header_size);
  352. void write_packet(ByteBuffer& packet, bool immediately = false);
  353. ByteBuffer build_client_key_exchange();
  354. ByteBuffer build_server_key_exchange();
  355. ByteBuffer build_hello();
  356. ByteBuffer build_handshake_finished();
  357. ByteBuffer build_certificate();
  358. ByteBuffer build_alert(bool critical, u8 code);
  359. ByteBuffer build_change_cipher_spec();
  360. void build_rsa_pre_master_secret(PacketBuilder&);
  361. void build_dhe_rsa_pre_master_secret(PacketBuilder&);
  362. void build_ecdhe_rsa_pre_master_secret(PacketBuilder&);
  363. ErrorOr<bool> flush();
  364. void write_into_socket();
  365. ErrorOr<void> read_from_socket();
  366. bool check_connection_state(bool read);
  367. void notify_client_for_app_data();
  368. ssize_t handle_server_hello(ReadonlyBytes, WritePacketStage&);
  369. ssize_t handle_handshake_finished(ReadonlyBytes, WritePacketStage&);
  370. ssize_t handle_certificate(ReadonlyBytes);
  371. ssize_t handle_server_key_exchange(ReadonlyBytes);
  372. ssize_t handle_dhe_rsa_server_key_exchange(ReadonlyBytes);
  373. ssize_t handle_ecdhe_server_key_exchange(ReadonlyBytes, u8& server_public_key_length);
  374. ssize_t handle_ecdhe_rsa_server_key_exchange(ReadonlyBytes);
  375. ssize_t handle_ecdhe_ecdsa_server_key_exchange(ReadonlyBytes);
  376. ssize_t handle_server_hello_done(ReadonlyBytes);
  377. ssize_t handle_certificate_verify(ReadonlyBytes);
  378. ssize_t handle_handshake_payload(ReadonlyBytes);
  379. ssize_t handle_message(ReadonlyBytes);
  380. void pseudorandom_function(Bytes output, ReadonlyBytes secret, u8 const* label, size_t label_length, ReadonlyBytes seed, ReadonlyBytes seed_b);
  381. ssize_t verify_rsa_server_key_exchange(ReadonlyBytes server_key_info_buffer, ReadonlyBytes signature_buffer);
  382. ssize_t verify_ecdsa_server_key_exchange(ReadonlyBytes server_key_info_buffer, ReadonlyBytes signature_buffer);
  383. size_t key_length() const
  384. {
  385. switch (m_context.cipher) {
  386. #define C(is_supported, suite, key_exchange, cipher, hash, iv_size, is_aead) \
  387. case suite: \
  388. return cipher_key_size(cipher) / 8;
  389. ENUMERATE_CIPHERS(C)
  390. #undef C
  391. default:
  392. return 128 / 8;
  393. }
  394. }
  395. size_t mac_length() const
  396. {
  397. switch (m_context.cipher) {
  398. #define C(is_supported, suite, key_exchange, cipher, hash, iv_size, is_aead) \
  399. case suite: \
  400. return hash ::digest_size();
  401. ENUMERATE_CIPHERS(C)
  402. #undef C
  403. default:
  404. return Crypto::Hash::SHA256::digest_size();
  405. }
  406. }
  407. Crypto::Hash::HashKind hmac_hash() const
  408. {
  409. switch (mac_length()) {
  410. case Crypto::Hash::SHA512::DigestSize:
  411. return Crypto::Hash::HashKind::SHA512;
  412. case Crypto::Hash::SHA384::DigestSize:
  413. return Crypto::Hash::HashKind::SHA384;
  414. case Crypto::Hash::SHA256::DigestSize:
  415. case Crypto::Hash::SHA1::DigestSize:
  416. default:
  417. return Crypto::Hash::HashKind::SHA256;
  418. }
  419. }
  420. size_t iv_length() const
  421. {
  422. switch (m_context.cipher) {
  423. #define C(is_supported, suite, key_exchange, cipher, hash, iv_size, is_aead) \
  424. case suite: \
  425. return iv_size;
  426. ENUMERATE_CIPHERS(C)
  427. #undef C
  428. default:
  429. return 16;
  430. }
  431. }
  432. bool is_aead() const
  433. {
  434. switch (m_context.cipher) {
  435. #define C(is_supported, suite, key_exchange, cipher, hash, iv_size, is_aead) \
  436. case suite: \
  437. return is_aead;
  438. ENUMERATE_CIPHERS(C)
  439. #undef C
  440. default:
  441. return false;
  442. }
  443. }
  444. bool expand_key();
  445. bool compute_master_secret_from_pre_master_secret(size_t length);
  446. void try_disambiguate_error() const;
  447. bool m_eof { false };
  448. StreamVariantType m_stream;
  449. Context m_context;
  450. OwnPtr<Crypto::Authentication::HMAC<Crypto::Hash::Manager>> m_hmac_local;
  451. OwnPtr<Crypto::Authentication::HMAC<Crypto::Hash::Manager>> m_hmac_remote;
  452. using CipherVariant = Variant<
  453. Empty,
  454. Crypto::Cipher::AESCipher::CBCMode,
  455. Crypto::Cipher::AESCipher::GCMMode>;
  456. CipherVariant m_cipher_local {};
  457. CipherVariant m_cipher_remote {};
  458. bool m_has_scheduled_write_flush { false };
  459. bool m_has_scheduled_app_data_flush { false };
  460. i32 m_max_wait_time_for_handshake_in_seconds { 10 };
  461. RefPtr<Core::Timer> m_handshake_timeout_timer;
  462. };
  463. }