2020-04-25 19:14:27 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Ali Mohammad Pur <ali.mpfard@gmail.com>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-25 19:14:27 +00:00
|
|
|
*/
|
|
|
|
|
2020-05-27 10:28:17 +00:00
|
|
|
#include <AK/Random.h>
|
2020-04-25 19:14:27 +00:00
|
|
|
#include <LibCrypto/ASN1/DER.h>
|
|
|
|
#include <LibCrypto/PK/Code/EMSA_PSS.h>
|
|
|
|
#include <LibTLS/TLSv12.h>
|
|
|
|
|
|
|
|
namespace TLS {
|
|
|
|
|
|
|
|
ByteBuffer TLSv12::build_hello()
|
|
|
|
{
|
2021-02-25 20:10:47 +00:00
|
|
|
fill_with_random(&m_context.local_random, 32);
|
2020-04-25 19:14:27 +00:00
|
|
|
|
2021-02-07 03:51:32 +00:00
|
|
|
auto packet_version = (u16)m_context.options.version;
|
|
|
|
auto version = (u16)m_context.options.version;
|
2020-04-25 19:14:27 +00:00
|
|
|
PacketBuilder builder { MessageType::Handshake, packet_version };
|
|
|
|
|
|
|
|
builder.append((u8)ClientHello);
|
|
|
|
|
|
|
|
// hello length (for later)
|
2020-05-27 10:28:17 +00:00
|
|
|
u8 dummy[3] = {};
|
2020-04-25 19:14:27 +00:00
|
|
|
builder.append(dummy, 3);
|
|
|
|
|
|
|
|
auto start_length = builder.length();
|
|
|
|
|
|
|
|
builder.append(version);
|
|
|
|
builder.append(m_context.local_random, sizeof(m_context.local_random));
|
|
|
|
|
|
|
|
builder.append(m_context.session_id_size);
|
|
|
|
if (m_context.session_id_size)
|
|
|
|
builder.append(m_context.session_id, m_context.session_id_size);
|
|
|
|
|
|
|
|
size_t extension_length = 0;
|
|
|
|
size_t alpn_length = 0;
|
|
|
|
size_t alpn_negotiated_length = 0;
|
|
|
|
|
|
|
|
// ALPN
|
|
|
|
if (!m_context.negotiated_alpn.is_null()) {
|
|
|
|
alpn_negotiated_length = m_context.negotiated_alpn.length();
|
|
|
|
alpn_length = alpn_negotiated_length + 1;
|
|
|
|
extension_length += alpn_length + 6;
|
|
|
|
} else if (m_context.alpn.size()) {
|
|
|
|
for (auto& alpn : m_context.alpn) {
|
|
|
|
size_t length = alpn.length();
|
|
|
|
alpn_length += length + 1;
|
|
|
|
}
|
|
|
|
if (alpn_length)
|
|
|
|
extension_length += alpn_length + 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ciphers
|
2021-02-07 03:51:32 +00:00
|
|
|
builder.append((u16)(m_context.options.usable_cipher_suites.size() * sizeof(u16)));
|
|
|
|
for (auto suite : m_context.options.usable_cipher_suites)
|
|
|
|
builder.append((u16)suite);
|
2020-04-25 19:14:27 +00:00
|
|
|
|
|
|
|
// we don't like compression
|
2021-02-07 03:51:32 +00:00
|
|
|
VERIFY(!m_context.options.use_compression);
|
2020-04-25 19:14:27 +00:00
|
|
|
builder.append((u8)1);
|
2021-02-07 03:51:32 +00:00
|
|
|
builder.append((u8)m_context.options.use_compression);
|
2020-04-25 19:14:27 +00:00
|
|
|
|
2021-02-07 03:51:32 +00:00
|
|
|
// set SNI if we have one, and the user hasn't explicitly asked us to omit it.
|
2020-04-25 19:14:27 +00:00
|
|
|
auto sni_length = 0;
|
2021-02-07 03:51:32 +00:00
|
|
|
if (!m_context.extensions.SNI.is_null() && m_context.options.use_sni)
|
2021-02-07 03:49:34 +00:00
|
|
|
sni_length = m_context.extensions.SNI.length();
|
2020-04-25 19:14:27 +00:00
|
|
|
|
|
|
|
if (sni_length)
|
|
|
|
extension_length += sni_length + 9;
|
|
|
|
|
|
|
|
builder.append((u16)extension_length);
|
|
|
|
|
|
|
|
if (sni_length) {
|
|
|
|
// SNI extension
|
|
|
|
builder.append((u16)HandshakeExtension::ServerName);
|
|
|
|
// extension length
|
|
|
|
builder.append((u16)(sni_length + 5));
|
|
|
|
// SNI length
|
|
|
|
builder.append((u16)(sni_length + 3));
|
|
|
|
// SNI type
|
|
|
|
builder.append((u8)0);
|
|
|
|
// SNI host length + value
|
|
|
|
builder.append((u16)sni_length);
|
2021-02-07 03:49:34 +00:00
|
|
|
builder.append((const u8*)m_context.extensions.SNI.characters(), sni_length);
|
2020-04-25 19:14:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (alpn_length) {
|
|
|
|
// TODO
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-04-25 19:14:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// set the "length" field of the packet
|
|
|
|
size_t remaining = builder.length() - start_length;
|
|
|
|
size_t payload_position = 6;
|
|
|
|
builder.set(payload_position, remaining / 0x10000);
|
|
|
|
remaining %= 0x10000;
|
|
|
|
builder.set(payload_position + 1, remaining / 0x100);
|
|
|
|
remaining %= 0x100;
|
|
|
|
builder.set(payload_position + 2, remaining);
|
|
|
|
|
|
|
|
auto packet = builder.build();
|
|
|
|
update_packet(packet);
|
|
|
|
|
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
|
|
|
ByteBuffer TLSv12::build_alert(bool critical, u8 code)
|
|
|
|
{
|
2021-02-07 03:51:32 +00:00
|
|
|
PacketBuilder builder(MessageType::Alert, (u16)m_context.options.version);
|
2020-04-28 14:27:01 +00:00
|
|
|
builder.append((u8)(critical ? AlertLevel::Critical : AlertLevel::Warning));
|
|
|
|
builder.append(code);
|
|
|
|
|
|
|
|
if (critical)
|
|
|
|
m_context.critical_error = code;
|
|
|
|
|
|
|
|
auto packet = builder.build();
|
|
|
|
update_packet(packet);
|
|
|
|
|
|
|
|
return packet;
|
2020-04-25 19:14:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ByteBuffer TLSv12::build_finished()
|
|
|
|
{
|
2021-02-07 03:51:32 +00:00
|
|
|
PacketBuilder builder { MessageType::Handshake, m_context.options.version, 12 + 64 };
|
2020-04-25 19:14:27 +00:00
|
|
|
builder.append((u8)HandshakeType::Finished);
|
|
|
|
|
|
|
|
u32 out_size = 12;
|
|
|
|
|
|
|
|
builder.append_u24(out_size);
|
|
|
|
|
|
|
|
u8 out[out_size];
|
2020-12-19 17:14:38 +00:00
|
|
|
auto outbuffer = Bytes { out, out_size };
|
2020-04-25 19:14:27 +00:00
|
|
|
auto dummy = ByteBuffer::create_zeroed(0);
|
|
|
|
|
|
|
|
auto digest = m_context.handshake_hash.digest();
|
2020-12-19 15:23:52 +00:00
|
|
|
auto hashbuf = ReadonlyBytes { digest.immutable_data(), m_context.handshake_hash.digest_size() };
|
2020-04-25 19:14:27 +00:00
|
|
|
pseudorandom_function(outbuffer, m_context.master_key, (const u8*)"client finished", 15, hashbuf, dummy);
|
|
|
|
|
2020-12-19 17:14:38 +00:00
|
|
|
builder.append(outbuffer);
|
2020-04-25 19:14:27 +00:00
|
|
|
auto packet = builder.build();
|
|
|
|
update_packet(packet);
|
|
|
|
|
|
|
|
return packet;
|
|
|
|
}
|
|
|
|
|
2020-05-30 15:23:07 +00:00
|
|
|
void TLSv12::alert(AlertLevel level, AlertDescription code)
|
|
|
|
{
|
|
|
|
auto the_alert = build_alert(level == AlertLevel::Critical, (u8)code);
|
|
|
|
write_packet(the_alert);
|
|
|
|
flush();
|
|
|
|
}
|
|
|
|
|
2020-04-25 19:14:27 +00:00
|
|
|
}
|