This commit is contained in:
J. Nick Koston 2025-07-19 15:06:42 -10:00
parent 7e3027d9bd
commit 0046e67727
No known key found for this signature in database
2 changed files with 18 additions and 18 deletions

View File

@ -299,6 +299,19 @@ APIError APINoiseFrameHelper::init() {
state_ = State::CLIENT_HELLO; state_ = State::CLIENT_HELLO;
return APIError::OK; return APIError::OK;
} }
// Helper for handling handshake frame errors
APIError APINoiseFrameHelper::handle_handshake_frame_error_(APIError aerr) {
if (aerr == APIError::BAD_INDICATOR) {
send_explicit_handshake_reject_("Bad indicator byte");
return aerr;
}
if (aerr == APIError::BAD_HANDSHAKE_PACKET_LEN) {
send_explicit_handshake_reject_("Bad handshake packet len");
return aerr;
}
return aerr;
}
/// Run through handshake messages (if in that phase) /// Run through handshake messages (if in that phase)
APIError APINoiseFrameHelper::loop() { APIError APINoiseFrameHelper::loop() {
// During handshake phase, process as many actions as possible until we can't progress // During handshake phase, process as many actions as possible until we can't progress
@ -423,16 +436,9 @@ APIError APINoiseFrameHelper::state_action_() {
// waiting for client hello // waiting for client hello
ParsedFrame frame; ParsedFrame frame;
aerr = try_read_frame_(&frame); aerr = try_read_frame_(&frame);
if (aerr == APIError::BAD_INDICATOR) { if (aerr != APIError::OK) {
send_explicit_handshake_reject_("Bad indicator byte"); return handle_handshake_frame_error_(aerr);
return aerr;
} }
if (aerr == APIError::BAD_HANDSHAKE_PACKET_LEN) {
send_explicit_handshake_reject_("Bad handshake packet len");
return aerr;
}
if (aerr != APIError::OK)
return aerr;
// ignore contents, may be used in future for flags // ignore contents, may be used in future for flags
// Reserve space for: existing prologue + 2 size bytes + frame data // Reserve space for: existing prologue + 2 size bytes + frame data
prologue_.reserve(prologue_.size() + 2 + frame.msg.size()); prologue_.reserve(prologue_.size() + 2 + frame.msg.size());
@ -478,16 +484,9 @@ APIError APINoiseFrameHelper::state_action_() {
// waiting for handshake msg // waiting for handshake msg
ParsedFrame frame; ParsedFrame frame;
aerr = try_read_frame_(&frame); aerr = try_read_frame_(&frame);
if (aerr == APIError::BAD_INDICATOR) { if (aerr != APIError::OK) {
send_explicit_handshake_reject_("Bad indicator byte"); return handle_handshake_frame_error_(aerr);
return aerr;
} }
if (aerr == APIError::BAD_HANDSHAKE_PACKET_LEN) {
send_explicit_handshake_reject_("Bad handshake packet len");
return aerr;
}
if (aerr != APIError::OK)
return aerr;
if (frame.msg.empty()) { if (frame.msg.empty()) {
send_explicit_handshake_reject_("Empty handshake message"); send_explicit_handshake_reject_("Empty handshake message");

View File

@ -212,6 +212,7 @@ class APINoiseFrameHelper : public APIFrameHelper {
APIError init_handshake_(); APIError init_handshake_();
APIError check_handshake_finished_(); APIError check_handshake_finished_();
void send_explicit_handshake_reject_(const std::string &reason); void send_explicit_handshake_reject_(const std::string &reason);
APIError handle_handshake_frame_error_(APIError aerr);
// Pointers first (4 bytes each) // Pointers first (4 bytes each)
NoiseHandshakeState *handshake_{nullptr}; NoiseHandshakeState *handshake_{nullptr};