LibTLS: Show enum value instead of underlying value where possible

This commit is contained in:
stelar7 2023-04-14 00:45:32 +02:00 committed by Sam Atkins
parent 5853d9642a
commit 9059694216
Notes: sideshowbarker 2024-07-17 00:16:31 +09:00
4 changed files with 93 additions and 10 deletions

View file

@ -704,6 +704,89 @@ enum class AlertDescription : u8 {
#undef _ENUM_KEY
#undef _ENUM_KEY_VALUE
constexpr static StringView enum_to_string(CipherSuite descriptor)
{
#define _ENUM_KEY_VALUE(name, value) \
case CipherSuite::name: \
return #name##sv;
switch (descriptor) {
__ENUM_CIPHER_SUITES
}
return "Unknown"sv;
#undef _ENUM_KEY_VALUE
}
constexpr static StringView enum_to_string(ExtensionType descriptor)
{
#define _ENUM_KEY_VALUE(name, value) \
case ExtensionType::name: \
return #name##sv;
switch (descriptor) {
__ENUM_EXTENSION_TYPES
}
return "Unknown"sv;
#undef _ENUM_KEY_VALUE
}
constexpr static StringView enum_to_string(ContentType descriptor)
{
#define _ENUM_KEY_VALUE(name, value) \
case ContentType::name: \
return #name##sv;
switch (descriptor) {
__ENUM_CONTENT_TYPES
}
return "Unknown"sv;
#undef _ENUM_KEY_VALUE
}
constexpr static StringView enum_to_string(ProtocolVersion descriptor)
{
#define _ENUM_KEY_VALUE(name, value) \
case ProtocolVersion::name: \
return #name##sv;
switch (descriptor) {
__ENUM_PROTOCOL_VERSIONS
}
return "Unknown"sv;
#undef _ENUM_KEY_VALUE
}
constexpr static StringView enum_to_string(HandshakeType descriptor)
{
#define _ENUM_KEY_VALUE(name, value) \
case HandshakeType::name: \
return #name##sv;
switch (descriptor) {
__ENUM_HANDSHAKE_TYPES
}
return "Unknown"sv;
#undef _ENUM_KEY_VALUE
}
constexpr static StringView enum_to_string(SignatureAlgorithm descriptor)
{
#define _ENUM_KEY_VALUE(name, value) \
case SignatureAlgorithm::name: \
return #name##sv;
switch (descriptor) {
__ENUM_SIGNATURE_ALGORITHM
}
return "Unknown"sv;
#undef _ENUM_KEY_VALUE
}
constexpr static StringView enum_to_string(AlertDescription descriptor)
{
#define _ENUM_KEY_VALUE(name, value) \

View file

@ -412,7 +412,7 @@ ssize_t TLSv12::handle_handshake_payload(ReadonlyBytes vbuffer)
}
break;
default:
dbgln("message type not understood: {}", to_underlying(type));
dbgln("message type not understood: {}", enum_to_string(type));
return (i8)Error::NotUnderstood;
}

View file

@ -85,7 +85,7 @@ ssize_t TLSv12::handle_server_hello(ReadonlyBytes buffer, WritePacketStage& writ
return (i8)Error::NoCommonCipher;
}
m_context.cipher = cipher;
dbgln_if(TLS_DEBUG, "Cipher: {}", (u16)cipher);
dbgln_if(TLS_DEBUG, "Cipher: {}", enum_to_string(cipher));
// Simplification: We only support handshake hash functions via HMAC
m_context.handshake_hash.initialize(hmac_hash());
@ -116,7 +116,7 @@ ssize_t TLSv12::handle_server_hello(ReadonlyBytes buffer, WritePacketStage& writ
u16 extension_length = AK::convert_between_host_and_network_endian(ByteReader::load16(buffer.offset_pointer(res)));
res += 2;
dbgln_if(TLS_DEBUG, "Extension {} with length {}", (u16)extension_type, extension_length);
dbgln_if(TLS_DEBUG, "Extension {} with length {}", enum_to_string(extension_type), extension_length);
if (buffer.size() - res < extension_length)
return (i8)Error::NeedMoreData;
@ -188,7 +188,7 @@ ssize_t TLSv12::handle_server_hello(ReadonlyBytes buffer, WritePacketStage& writ
// that the server supports uncompressed points.
res += extension_length;
} else {
dbgln("Encountered unknown extension {} with length {}", (u16)extension_type, extension_length);
dbgln("Encountered unknown extension {} with length {}", enum_to_string(extension_type), extension_length);
res += extension_length;
}
}
@ -346,9 +346,9 @@ ssize_t TLSv12::handle_ecdhe_rsa_server_key_exchange(ReadonlyBytes buffer)
ssize_t TLSv12::verify_rsa_server_key_exchange(ReadonlyBytes server_key_info_buffer, ReadonlyBytes signature_buffer)
{
auto signature_hash = signature_buffer[0];
auto signature_algorithm = signature_buffer[1];
if (signature_algorithm != (u8)SignatureAlgorithm::RSA) {
dbgln("verify_rsa_server_key_exchange failed: Signature algorithm is not RSA, instead {}", signature_algorithm);
auto signature_algorithm = static_cast<SignatureAlgorithm>(signature_buffer[1]);
if (signature_algorithm != SignatureAlgorithm::RSA) {
dbgln("verify_rsa_server_key_exchange failed: Signature algorithm is not RSA, instead {}", enum_to_string(signature_algorithm));
return (i8)Error::NotUnderstood;
}

View file

@ -325,8 +325,8 @@ ssize_t TLSv12::handle_message(ReadonlyBytes buffer)
// FIXME: Read the version and verify it
if constexpr (TLS_DEBUG) {
auto version = ByteReader::load16(buffer.offset_pointer(buffer_position));
dbgln("type={}, version={}", (u8)type, (u16)version);
auto version = static_cast<ProtocolVersion>(ByteReader::load16(buffer.offset_pointer(buffer_position)));
dbgln("type={}, version={}", enum_to_string(type), enum_to_string(version));
}
buffer_position += 2;
@ -341,7 +341,7 @@ ssize_t TLSv12::handle_message(ReadonlyBytes buffer)
return (i8)Error::NeedMoreData;
}
dbgln_if(TLS_DEBUG, "message type: {}, length: {}", (u8)type, length);
dbgln_if(TLS_DEBUG, "message type: {}, length: {}", enum_to_string(type), length);
auto plain = buffer.slice(buffer_position, buffer.size() - buffer_position);
ByteBuffer decrypted;