Skip to content

Commit

Permalink
LibTLS: Even more ByteBuffer -> Span conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Dec 19, 2020
1 parent f82b0a7 commit e517505
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 23 deletions.
4 changes: 1 addition & 3 deletions Libraries/LibGemini/GeminiJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ void GeminiJob::read_while_data_available(Function<IterationDecision()> read)

void GeminiJob::set_certificate(String certificate, String private_key)
{
if (!m_socket->add_client_key(
ByteBuffer::wrap(const_cast<char*>(certificate.characters()), certificate.length()),
ByteBuffer::wrap(const_cast<char*>(private_key.characters()), private_key.length()))) {
if (!m_socket->add_client_key(certificate.bytes(), private_key.bytes())) {
dbg() << "LibGemini: Failed to set a client certificate";
// FIXME: Do something about this failure
ASSERT_NOT_REACHED();
Expand Down
5 changes: 1 addition & 4 deletions Libraries/LibHTTP/HttpsJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ void HttpsJob::shutdown()

void HttpsJob::set_certificate(String certificate, String private_key)
{
if (!m_socket->add_client_key(
ByteBuffer::wrap(const_cast<char*>(certificate.characters()), certificate.length()),
ByteBuffer::wrap(const_cast<char*>(private_key.characters()), private_key.length()))) {

if (!m_socket->add_client_key(certificate.bytes(), private_key.bytes())) {
dbg() << "LibHTTP: Failed to set a client certificate";
// FIXME: Do something about this failure
ASSERT_NOT_REACHED();
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibTLS/ClientHandshake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ ssize_t TLSv12::handle_hello(ReadonlyBytes buffer, WritePacketStage& write_packe
m_context.session_id_size = session_length;
#ifdef TLS_DEBUG
dbg() << "Remote session ID:";
print_buffer(ByteBuffer::wrap(m_context.session_id, session_length));
print_buffer(ReadonlyBytes { m_context.session_id, session_length });
#endif
} else {
m_context.session_id_size = 0;
Expand Down
16 changes: 8 additions & 8 deletions Libraries/LibTLS/Exchange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ bool TLSv12::expand_key()
key_buffer,
m_context.master_key,
(const u8*)"key expansion", 13,
ByteBuffer::wrap(m_context.remote_random, 32),
ByteBuffer::wrap(m_context.local_random, 32));
ReadonlyBytes { m_context.remote_random, sizeof(m_context.remote_random) },
ReadonlyBytes { m_context.local_random, sizeof(m_context.local_random) });

size_t offset = 0;
if (is_aead) {
Expand Down Expand Up @@ -93,14 +93,14 @@ bool TLSv12::expand_key()
memcpy(m_context.crypto.local_aead_iv, client_iv, iv_size);
memcpy(m_context.crypto.remote_aead_iv, server_iv, iv_size);

m_aes_local.gcm = make<Crypto::Cipher::AESCipher::GCMMode>(ByteBuffer::wrap(client_key, key_size), key_size * 8, Crypto::Cipher::Intent::Encryption, Crypto::Cipher::PaddingMode::RFC5246);
m_aes_remote.gcm = make<Crypto::Cipher::AESCipher::GCMMode>(ByteBuffer::wrap(server_key, key_size), key_size * 8, Crypto::Cipher::Intent::Decryption, Crypto::Cipher::PaddingMode::RFC5246);
m_aes_local.gcm = make<Crypto::Cipher::AESCipher::GCMMode>(ReadonlyBytes { client_key, key_size }, key_size * 8, Crypto::Cipher::Intent::Encryption, Crypto::Cipher::PaddingMode::RFC5246);
m_aes_remote.gcm = make<Crypto::Cipher::AESCipher::GCMMode>(ReadonlyBytes { server_key, key_size }, key_size * 8, Crypto::Cipher::Intent::Decryption, Crypto::Cipher::PaddingMode::RFC5246);
} else {
memcpy(m_context.crypto.local_iv, client_iv, iv_size);
memcpy(m_context.crypto.remote_iv, server_iv, iv_size);

m_aes_local.cbc = make<Crypto::Cipher::AESCipher::CBCMode>(ByteBuffer::wrap(client_key, key_size), key_size * 8, Crypto::Cipher::Intent::Encryption, Crypto::Cipher::PaddingMode::RFC5246);
m_aes_remote.cbc = make<Crypto::Cipher::AESCipher::CBCMode>(ByteBuffer::wrap(server_key, key_size), key_size * 8, Crypto::Cipher::Intent::Decryption, Crypto::Cipher::PaddingMode::RFC5246);
m_aes_local.cbc = make<Crypto::Cipher::AESCipher::CBCMode>(ReadonlyBytes { client_key, key_size }, key_size * 8, Crypto::Cipher::Intent::Encryption, Crypto::Cipher::PaddingMode::RFC5246);
m_aes_remote.cbc = make<Crypto::Cipher::AESCipher::CBCMode>(ReadonlyBytes { server_key, key_size }, key_size * 8, Crypto::Cipher::Intent::Decryption, Crypto::Cipher::PaddingMode::RFC5246);
}

m_context.crypto.created = 1;
Expand Down Expand Up @@ -167,8 +167,8 @@ bool TLSv12::compute_master_secret(size_t length)
m_context.master_key,
m_context.premaster_key,
(const u8*)"master secret", 13,
ByteBuffer::wrap(m_context.local_random, 32),
ByteBuffer::wrap(m_context.remote_random, 32));
ReadonlyBytes { m_context.local_random, sizeof(m_context.local_random) },
ReadonlyBytes { m_context.remote_random, sizeof(m_context.remote_random) });

m_context.premaster_key.clear();
#ifdef TLS_DEBUG
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibTLS/Handshake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ ByteBuffer TLSv12::build_finished()
auto dummy = ByteBuffer::create_zeroed(0);

auto digest = m_context.handshake_hash.digest();
auto hashbuf = ByteBuffer::wrap(const_cast<u8*>(digest.immutable_data()), m_context.handshake_hash.digest_size());
auto hashbuf = ReadonlyBytes { digest.immutable_data(), m_context.handshake_hash.digest_size() };
pseudorandom_function(outbuffer, m_context.master_key, (const u8*)"client finished", 15, hashbuf, dummy);

builder.append(outbuffer.bytes());
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibTLS/Record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ ssize_t TLSv12::handle_message(ReadonlyBytes buffer)
memcpy(temp_buf, buffer.offset_pointer(0), 3);
*(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);
auto message_mac = ReadonlyBytes { message_hmac, mac_size };
if (hmac != message_mac) {
dbg() << "integrity check failed (mac length " << mac_size << ")";
dbg() << "mac received:";
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibTLS/TLSv12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ static ssize_t _parse_asn1(const Context& context, Certificate& cert, const u8*
cert.SAN.append(alt_name);
}
}
// print_buffer(ByteBuffer::wrap(const_cast<u8*>(buffer) + position, length));
// print_buffer(ReadonlyBytes { buffer + position, length });
break;
case 0x03:
if (_asn1_is_field_present(fields, Constants::pk_id)) {
Expand Down
8 changes: 4 additions & 4 deletions Userland/test-crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ static void aes_cbc(const char* message, size_t len)

if (encrypting) {
Crypto::Cipher::AESCipher::CBCMode cipher(
ByteBuffer::wrap(const_cast<char*>(secret_key), strlen(secret_key)),
StringView(secret_key).bytes(),
key_bits,
Crypto::Cipher::Intent::Encryption);

Expand All @@ -218,7 +218,7 @@ static void aes_cbc(const char* message, size_t len)
print_buffer(enc_span, Crypto::Cipher::AESCipher::block_size());
} else {
Crypto::Cipher::AESCipher::CBCMode cipher(
ByteBuffer::wrap(const_cast<char*>(secret_key), strlen(secret_key)),
StringView(secret_key).bytes(),
key_bits,
Crypto::Cipher::Intent::Decryption);
auto dec = cipher.create_aligned_buffer(buffer.size());
Expand Down Expand Up @@ -2038,8 +2038,8 @@ static void tls_test_client_hello()
FAIL(write(0) failed);
loop.quit(0);
}
auto* the_server = (const u8*)(server ?: DEFAULT_SERVER);
if (!tls.write(ByteBuffer::wrap(const_cast<u8*>(the_server), strlen((const char*)the_server)))) {
auto* the_server = server ?: DEFAULT_SERVER;
if (!tls.write(StringView(the_server).bytes())) {
FAIL(write(1) failed);
loop.quit(0);
}
Expand Down

0 comments on commit e517505

Please sign in to comment.