diff options
author | asynts <asynts@gmail.com> | 2020-08-25 15:11:15 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-08-25 16:22:14 +0200 |
commit | 10c6f062b3eb9da9e45c64a0483920a6f122fbbb (patch) | |
tree | c0fddfa9c23e25b5006bcbf3dd73f705b800ba09 /Libraries/LibTLS | |
parent | ecf6cbbd02c0c7456b0859a775af286b7473848b (diff) | |
download | serenity-10c6f062b3eb9da9e45c64a0483920a6f122fbbb.zip |
AK: Add Endian.h header to replace NetworkOrdered.h.
Diffstat (limited to 'Libraries/LibTLS')
-rw-r--r-- | Libraries/LibTLS/ClientHandshake.cpp | 16 | ||||
-rw-r--r-- | Libraries/LibTLS/Record.cpp | 12 | ||||
-rw-r--r-- | Libraries/LibTLS/TLSPacketBuilder.h | 6 | ||||
-rw-r--r-- | Libraries/LibTLS/TLSv12.cpp | 3 |
4 files changed, 22 insertions, 15 deletions
diff --git a/Libraries/LibTLS/ClientHandshake.cpp b/Libraries/LibTLS/ClientHandshake.cpp index 8b4bc28fa0..214fce237a 100644 --- a/Libraries/LibTLS/ClientHandshake.cpp +++ b/Libraries/LibTLS/ClientHandshake.cpp @@ -24,7 +24,9 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include <AK/Endian.h> #include <AK/Random.h> + #include <LibCore/Timer.h> #include <LibCrypto/ASN1/DER.h> #include <LibCrypto/PK/Code/EMSA_PSS.h> @@ -70,7 +72,7 @@ ssize_t TLSv12::handle_hello(const ByteBuffer& buffer, WritePacketStage& write_p dbg() << "not enough data for version"; return (i8)Error::NeedMoreData; } - auto version = (Version)convert_between_host_and_network(*(const u16*)buffer.offset_pointer(res)); + auto version = (Version)AK::convert_between_host_and_network_endian(*(const u16*)buffer.offset_pointer(res)); res += 2; if (!supports_version(version)) @@ -101,7 +103,7 @@ ssize_t TLSv12::handle_hello(const ByteBuffer& buffer, WritePacketStage& write_p dbg() << "not enough data for cipher suite listing"; return (i8)Error::NeedMoreData; } - auto cipher = (CipherSuite)convert_between_host_and_network(*(const u16*)buffer.offset_pointer(res)); + auto cipher = (CipherSuite)AK::convert_between_host_and_network_endian(*(const u16*)buffer.offset_pointer(res)); res += 2; if (!supports_cipher(cipher)) { m_context.cipher = CipherSuite::Invalid; @@ -140,9 +142,9 @@ ssize_t TLSv12::handle_hello(const ByteBuffer& buffer, WritePacketStage& write_p } while ((ssize_t)buffer.size() - res >= 4) { - auto extension_type = (HandshakeExtension)convert_between_host_and_network(*(const u16*)buffer.offset_pointer(res)); + auto extension_type = (HandshakeExtension)AK::convert_between_host_and_network_endian(*(const u16*)buffer.offset_pointer(res)); res += 2; - u16 extension_length = convert_between_host_and_network(*(const u16*)buffer.offset_pointer(res)); + u16 extension_length = AK::convert_between_host_and_network_endian(*(const u16*)buffer.offset_pointer(res)); res += 2; #ifdef TLS_DEBUG @@ -156,7 +158,7 @@ ssize_t TLSv12::handle_hello(const ByteBuffer& buffer, WritePacketStage& write_p // SNI if (extension_type == HandshakeExtension::ServerName) { - u16 sni_host_length = convert_between_host_and_network(*(const u16*)buffer.offset_pointer(res + 3)); + u16 sni_host_length = AK::convert_between_host_and_network_endian(*(const u16*)buffer.offset_pointer(res + 3)); if (buffer.size() - res - 5 < sni_host_length) { dbg() << "Not enough data for sni " << (buffer.size() - res - 5) << " < " << sni_host_length; return (i8)Error::NeedMoreData; @@ -168,7 +170,7 @@ ssize_t TLSv12::handle_hello(const ByteBuffer& buffer, WritePacketStage& write_p } } else if (extension_type == HandshakeExtension::ApplicationLayerProtocolNegotiation && m_context.alpn.size()) { if (buffer.size() - res > 2) { - auto alpn_length = convert_between_host_and_network(*(const u16*)buffer.offset_pointer(res)); + auto alpn_length = AK::convert_between_host_and_network_endian(*(const u16*)buffer.offset_pointer(res)); if (alpn_length && alpn_length <= extension_length - 2) { const u8* alpn = buffer.offset_pointer(res + 2); size_t alpn_position = 0; @@ -267,7 +269,7 @@ void TLSv12::build_random(PacketBuilder& builder) dbg() << "Server mode not supported"; return; } else { - *(u16*)random_bytes = convert_between_host_and_network((u16)Version::V12); + *(u16*)random_bytes = AK::convert_between_host_and_network_endian((u16)Version::V12); } m_context.premaster_key = ByteBuffer::copy(random_bytes, bytes); diff --git a/Libraries/LibTLS/Record.cpp b/Libraries/LibTLS/Record.cpp index 59cf3c0cbe..f061c5500a 100644 --- a/Libraries/LibTLS/Record.cpp +++ b/Libraries/LibTLS/Record.cpp @@ -24,6 +24,8 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include <AK/Endian.h> + #include <LibCore/Timer.h> #include <LibCrypto/ASN1/DER.h> #include <LibCrypto/PK/Code/EMSA_PSS.h> @@ -56,7 +58,7 @@ void TLSv12::write_packet(ByteBuffer& packet) void TLSv12::update_packet(ByteBuffer& packet) { u32 header_size = 5; - *(u16*)packet.offset_pointer(3) = convert_between_host_and_network((u16)(packet.size() - header_size)); + *(u16*)packet.offset_pointer(3) = AK::convert_between_host_and_network_endian((u16)(packet.size() - header_size)); if (packet[0] != (u8)MessageType::ChangeCipher) { if (packet[0] == (u8)MessageType::Handshake && packet.size() > header_size) { @@ -120,7 +122,7 @@ void TLSv12::update_packet(ByteBuffer& packet) // store the correct ciphertext length into the packet u16 ct_length = (u16)ct.size() - header_size; - *(u16*)ct.offset_pointer(header_size - 2) = convert_between_host_and_network(ct_length); + *(u16*)ct.offset_pointer(header_size - 2) = AK::convert_between_host_and_network_endian(ct_length); // replace the packet with the ciphertext packet = ct; @@ -137,7 +139,7 @@ void TLSv12::update_hash(const ByteBuffer& message) ByteBuffer TLSv12::hmac_message(const ReadonlyBytes& buf, const Optional<ReadonlyBytes> buf2, size_t mac_length, bool local) { - u64 sequence_number = convert_between_host_and_network(local ? m_context.local_sequence_number : m_context.remote_sequence_number); + u64 sequence_number = AK::convert_between_host_and_network_endian(local ? m_context.local_sequence_number : m_context.remote_sequence_number); ensure_hmac(mac_length, local); auto& hmac = local ? *m_hmac_local : *m_hmac_remote; #ifdef TLS_DEBUG @@ -185,7 +187,7 @@ ssize_t TLSv12::handle_message(const ByteBuffer& buffer) #endif buffer_position += 2; - auto length = convert_between_host_and_network(*(const u16*)buffer.offset_pointer(buffer_position)); + auto length = AK::convert_between_host_and_network_endian(*(const u16*)buffer.offset_pointer(buffer_position)); #ifdef TLS_DEBUG dbg() << "record length: " << length << " at offset: " << buffer_position; #endif @@ -238,7 +240,7 @@ ssize_t TLSv12::handle_message(const ByteBuffer& buffer) const u8* message_hmac = decrypted_span.offset(length); u8 temp_buf[5]; memcpy(temp_buf, buffer.offset_pointer(0), 3); - *(u16*)(temp_buf + 3) = convert_between_host_and_network(length); + *(u16*)(temp_buf + 3) = AK::convert_between_host_and_network_endian(length); auto hmac = hmac_message({ temp_buf, 5 }, decrypted_span.slice(0, length), mac_size); auto message_mac = ByteBuffer::wrap(const_cast<u8*>(message_hmac), mac_size); if (hmac != message_mac) { diff --git a/Libraries/LibTLS/TLSPacketBuilder.h b/Libraries/LibTLS/TLSPacketBuilder.h index b7f9bc26c2..b521f947a4 100644 --- a/Libraries/LibTLS/TLSPacketBuilder.h +++ b/Libraries/LibTLS/TLSPacketBuilder.h @@ -27,6 +27,7 @@ #pragma once #include <AK/ByteBuffer.h> +#include <AK/Endian.h> #include <AK/Types.h> namespace TLS { @@ -57,12 +58,12 @@ public: m_packet_data = ByteBuffer::create_uninitialized(size_hint + 16); m_current_length = 5; m_packet_data[0] = (u8)type; - *(u16*)m_packet_data.offset_pointer(1) = convert_between_host_and_network((u16)version); + *(u16*)m_packet_data.offset_pointer(1) = AK::convert_between_host_and_network_endian((u16)version); } inline void append(u16 value) { - value = convert_between_host_and_network(value); + value = AK::convert_between_host_and_network_endian(value); append((const u8*)&value, sizeof(value)); } inline void append(u8 value) @@ -115,4 +116,5 @@ private: ByteBuffer m_packet_data; size_t m_current_length; }; + } diff --git a/Libraries/LibTLS/TLSv12.cpp b/Libraries/LibTLS/TLSv12.cpp index 7924762b99..13bf311d6b 100644 --- a/Libraries/LibTLS/TLSv12.cpp +++ b/Libraries/LibTLS/TLSv12.cpp @@ -24,6 +24,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include <AK/Endian.h> #include <LibCore/DateTime.h> #include <LibCore/Timer.h> #include <LibCrypto/ASN1/DER.h> @@ -539,7 +540,7 @@ void TLSv12::consume(const ByteBuffer& record) dbg() << "message buffer length " << buffer_length; #endif while (buffer_length >= 5) { - auto length = convert_between_host_and_network(*(u16*)m_context.message_buffer.offset_pointer(index + size_offset)) + header_size; + auto length = AK::convert_between_host_and_network_endian(*(u16*)m_context.message_buffer.offset_pointer(index + size_offset)) + header_size; if (length > buffer_length) { #ifdef TLS_DEBUG dbg() << "Need more data: " << length << " | " << buffer_length; |