// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

package extension

import 

const (
	useExtendedMasterSecretHeaderSize = 4
)

// UseExtendedMasterSecret defines a TLS extension that contextually binds the
// master secret to a log of the full handshake that computes it, thus
// preventing MITM attacks.
type UseExtendedMasterSecret struct {
	Supported bool
}

// TypeValue returns the extension TypeValue.
func ( UseExtendedMasterSecret) () TypeValue {
	return UseExtendedMasterSecretTypeValue
}

// Marshal encodes the extension.
func ( *UseExtendedMasterSecret) () ([]byte, error) {
	if !.Supported {
		return []byte{}, nil
	}

	 := make([]byte, useExtendedMasterSecretHeaderSize)

	binary.BigEndian.PutUint16(, uint16(.TypeValue()))
	binary.BigEndian.PutUint16([2:], uint16(0)) // length

	return , nil
}

// Unmarshal populates the extension from encoded data.
func ( *UseExtendedMasterSecret) ( []byte) error {
	if len() < useExtendedMasterSecretHeaderSize {
		return errBufferTooSmall
	} else if TypeValue(binary.BigEndian.Uint16()) != .TypeValue() {
		return errInvalidExtensionType
	}

	.Supported = true

	return nil
}