diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2021-05-14 09:22:56 +0430 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-05-14 08:39:29 +0100 |
commit | df515e1d850339f408ab5eb7f4713d989ffb4534 (patch) | |
tree | 0f26bd76fba48a95cd29b841d2a80e99e1be0576 /Userland/Libraries/LibTLS/Record.cpp | |
parent | bfd4c7a16cd35452bc5d1331f05d8bd713b37d14 (diff) | |
download | serenity-df515e1d850339f408ab5eb7f4713d989ffb4534.zip |
LibCrypto+LibTLS: Avoid unaligned reads and writes
This adds an `AK::ByteReader` to help with that so we don't duplicate
the logic all over the place.
No more `*(const u16*)` and `*(const u32*)` for anyone.
This should help a little with #7060.
Diffstat (limited to 'Userland/Libraries/LibTLS/Record.cpp')
-rw-r--r-- | Userland/Libraries/LibTLS/Record.cpp | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/Userland/Libraries/LibTLS/Record.cpp b/Userland/Libraries/LibTLS/Record.cpp index ae1a96d650..3994b70640 100644 --- a/Userland/Libraries/LibTLS/Record.cpp +++ b/Userland/Libraries/LibTLS/Record.cpp @@ -34,7 +34,7 @@ void TLSv12::write_packet(ByteBuffer& packet) void TLSv12::update_packet(ByteBuffer& packet) { u32 header_size = 5; - *(u16*)packet.offset_pointer(3) = AK::convert_between_host_and_network_endian((u16)(packet.size() - header_size)); + ByteReader::store(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) { @@ -159,7 +159,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) = AK::convert_between_host_and_network_endian(ct_length); + ByteReader::store(ct.offset_pointer(header_size - 2), AK::convert_between_host_and_network_endian(ct_length)); // replace the packet with the ciphertext packet = ct; @@ -222,13 +222,14 @@ ssize_t TLSv12::handle_message(ReadonlyBytes buffer) // FIXME: Read the version and verify it if constexpr (TLS_DEBUG) { - auto version = (Version) * (const u16*)buffer.offset_pointer(buffer_position); + auto version = ByteReader::load16(buffer.offset_pointer(buffer_position)); dbgln("type={}, version={}", (u8)type, (u16)version); } buffer_position += 2; - auto length = AK::convert_between_host_and_network_endian(*(const u16*)buffer.offset_pointer(buffer_position)); + auto length = AK::convert_between_host_and_network_endian(ByteReader::load16(buffer.offset_pointer(buffer_position))); + dbgln_if(TLS_DEBUG, "record length: {} at offset: {}", length, buffer_position); buffer_position += 2; |