Skip to content

Commit

Permalink
WIP handshake layer
Browse files Browse the repository at this point in the history
Co-authored-by: Hannes Rantzsch <hannes.rantzsch@nexenio.com>
  • Loading branch information
reneme and Hannes Rantzsch committed Feb 17, 2022
1 parent 7f0b2a7 commit c803641
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
31 changes: 28 additions & 3 deletions src/lib/tls/tls13/tls_handshake_layer_13.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "botan/assert.h"
#include <botan/tls_alert.h>
#include <botan/tls_exceptn.h>

#include <botan/internal/tls_handshake_layer_13.h>
#include <botan/internal/tls_reader.h>

Expand Down Expand Up @@ -34,8 +36,31 @@ Handshake_Layer::ReadResult<Handshake_Layer::Handshake_Message_13> Handshake_Lay
if(reader.remaining_bytes() < msg_len)
return BytesNeeded(msg_len - reader.remaining_bytes());

BOTAN_ASSERT_NOMSG(type == Handshake_Type::CLIENT_HELLO);
return Client_Hello_13(reader.get_fixed<uint8_t>(msg_len));
switch (type) {
case CLIENT_HELLO:
return Client_Hello_13(reader.get_fixed<uint8_t>(msg_len));
case SERVER_HELLO:
return Server_Hello_13(reader.get_fixed<uint8_t>(msg_len));
case NEW_SESSION_TICKET:
return New_Session_Ticket(reader.get_fixed<uint8_t>(msg_len));
// case END_OF_EARLY_DATA:
// return End_Of_Early_Data(reader.get_fixed<uint8_t>(msg_len));
case ENCRYPTED_EXTENSIONS:
return Handshake_Message_13(Encrypted_Extensions(reader.get_fixed<uint8_t>(msg_len)));
// case CERTIFICATE:
// return Certificate_13(reader.get_fixed<uint8_t>(msg_len));
// case CERTIFICATE_REQUEST:
// return Certificate_Req_13(reader.get_fixed<uint8_t>(msg_len));
case CERTIFICATE_VERIFY:
return Certificate_Verify_13(reader.get_fixed<uint8_t>(msg_len));
case FINISHED:
return Finished(reader.get_fixed<uint8_t>(msg_len));
// case KEY_UPDATE:
// return Key_Update(reader.get_fixed<uint8_t>(msg_len));

default:
throw TLS_Exception(Alert::UNEXPECTED_MESSAGE, "unexpected handshake message received");
}
}

}
1 change: 0 additions & 1 deletion src/lib/tls/tls_messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,6 @@ class BOTAN_UNSTABLE_API Encrypted_Extensions final : public Handshake_Message
public:
explicit Encrypted_Extensions(const std::vector<uint8_t>& buf);

~Encrypted_Extensions() override = default;
Handshake_Type type() const override { return Handshake_Type::ENCRYPTED_EXTENSIONS; }

const Extensions& extensions() const { return m_extensions; }
Expand Down
24 changes: 23 additions & 1 deletion src/tests/test_tls_handshake_layer_13.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ std::vector<Test::Result> read_full_handshake_messages()
"04 03 05 03 06 03 02 03 08 04 08 05 08 06 04 01 05 01 06 01 02"
"01 04 02 05 02 06 02 02 02 00 2d 00 02 01 01 00 1c 00 02 40 01");

const auto server_hello_message = Botan::hex_decode(
"02 00 00 56 03 03 a6"
"af 06 a4 12 18 60 dc 5e 6e 60 24 9c d3 4c 95 93 0c 8a c5 cb 14"
"34 da c1 55 77 2e d3 e2 69 28 00 13 01 00 00 2e 00 33 00 24 00"
"1d 00 20 c9 82 88 76 11 20 95 fe 66 76 2b db f7 c6 72 e1 56 d6"
"cc 25 3b 83 3d f1 dd 69 b1 b0 4e 75 1f 0f 00 2b 00 02 03 04");

return
{
CHECK("read incomplete header", [&](auto& result)
Expand All @@ -88,20 +95,35 @@ std::vector<Test::Result> read_full_handshake_messages()
hl.copy_data({0x00, 0x01, 0x02});
result.test_eq("needs one more byte", get_bytes_needed(result, hl.next_message()), 1);
}),
CHECK("read incomplete client hello", [&](auto& result)
{
Handshake_Layer hl;
const std::vector<uint8_t> partial_client_hello_message(
client_hello_message.cbegin(), client_hello_message.cend() - 15);
hl.copy_data(partial_client_hello_message);
result.test_eq("needs 15 more bytes", get_bytes_needed(result, hl.next_message()), 15);
}),
CHECK("read client hello", [&](auto& result)
{
Handshake_Layer hl;
hl.copy_data(client_hello_message);
result.confirm("is a client hello", has_message<Client_Hello_13>(result, hl.next_message()));
}),
CHECK("read server hello", [&](auto& result)
{
Handshake_Layer hl;
hl.copy_data(server_hello_message);
result.confirm("is a server hello", has_message<Server_Hello_13>(result, hl.next_message()));
}),

};
}

std::vector<Test::Result> prepare_message()
{
return
{
CHECK("", [&](auto& result)
CHECK("", [&](auto&)
{
}),
};
Expand Down

0 comments on commit c803641

Please sign in to comment.