noxtls 0.2.10

TLS/DTLS protocol and connection state machine for the noxtls Rust stack.
Documentation
// Copyright (c) 2019-2026, Argenox Technologies LLC
// All rights reserved.
//
// SPDX-License-Identifier: GPL-2.0-only OR LicenseRef-Argenox-Commercial-License
//
// This file is part of the NoxTLS Library.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by the
// Free Software Foundation; version 2 of the License.
//
// Alternatively, this file may be used under the terms of a commercial
// license from Argenox Technologies LLC.
//
// See `noxtls/LICENSE` and `noxtls/LICENSE.md` in this repository for full details.
// CONTACT: info@argenox.com

//! TLS wire helpers: handshake payload splitting and re-exports of record framing from `noxtls-io`.

use crate::internal_alloc::Vec;
use noxtls_core::{Error, Result};

pub use noxtls_io::{TlsRecordDeframer, TLS_MAX_RECORD_PAYLOAD_LEN, TLS_RECORD_HEADER_LEN};

/// Splits one TLS `Handshake` inner payload into individual handshake messages.
///
/// Each message has the form `type(1) || length(3) || body(length)`.
///
/// # Arguments
///
/// * `payload` — Decrypted inner handshake bytes (for example from TLS 1.3 `Handshake` inner content).
///
/// # Returns
///
/// On success, a vector of complete handshake message byte vectors in wire order.
///
/// # Errors
///
/// Returns [`noxtls_core::Error::ParseFailure`] when the payload is truncated or malformed.
///
/// # Panics
///
/// This function does not panic.
pub fn split_tls13_handshake_payload(payload: &[u8]) -> Result<Vec<Vec<u8>>> {
    const HANDSHAKE_HEADER_LEN: usize = 4;
    let mut cursor = 0_usize;
    let mut messages = Vec::new();
    while cursor < payload.len() {
        if payload.len().saturating_sub(cursor) < HANDSHAKE_HEADER_LEN {
            return Err(Error::ParseFailure(
                "truncated tls handshake header in inner handshake payload",
            ));
        }
        let message_len = ((payload[cursor + 1] as usize) << 16)
            | ((payload[cursor + 2] as usize) << 8)
            | payload[cursor + 3] as usize;
        let full_len = HANDSHAKE_HEADER_LEN.saturating_add(message_len);
        if payload.len().saturating_sub(cursor) < full_len {
            return Err(Error::ParseFailure(
                "truncated tls handshake message body in inner handshake payload",
            ));
        }
        messages.push(payload[cursor..cursor + full_len].to_vec());
        cursor = cursor.saturating_add(full_len);
    }
    Ok(messages)
}