1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:37:35 +00:00

AK: Add Endian.h header to replace NetworkOrdered.h.

This commit is contained in:
asynts 2020-08-25 15:11:15 +02:00 committed by Andreas Kling
parent ecf6cbbd02
commit 10c6f062b3
20 changed files with 195 additions and 106 deletions

View file

@ -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);

View file

@ -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) {

View file

@ -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;
};
}

View file

@ -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;