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

package handshake

import (
	

	
	
)

// MessageCertificateVerify provide explicit verification of a
// client certificate.
//
// https://tools.ietf.org/html/rfc5246#section-7.4.8
type MessageCertificateVerify struct {
	HashAlgorithm      hash.Algorithm
	SignatureAlgorithm signature.Algorithm
	Signature          []byte
}

const handshakeMessageCertificateVerifyMinLength = 4

// Type returns the Handshake Type.
func ( MessageCertificateVerify) () Type {
	return TypeCertificateVerify
}

// Marshal encodes the Handshake.
func ( *MessageCertificateVerify) () ([]byte, error) {
	 := make([]byte, 1+1+2+len(.Signature))

	[0] = byte(.HashAlgorithm)
	[1] = byte(.SignatureAlgorithm)
	binary.BigEndian.PutUint16([2:], uint16(len(.Signature))) //nolint:gosec // G115
	copy([4:], .Signature)

	return , nil
}

// Unmarshal populates the message from encoded data.
func ( *MessageCertificateVerify) ( []byte) error {
	if len() < handshakeMessageCertificateVerifyMinLength {
		return errBufferTooSmall
	}

	.HashAlgorithm = hash.Algorithm([0])
	if ,  := hash.Algorithms()[.HashAlgorithm]; ! {
		return errInvalidHashAlgorithm
	}

	.SignatureAlgorithm = signature.Algorithm([1])
	if ,  := signature.Algorithms()[.SignatureAlgorithm]; ! {
		return errInvalidSignatureAlgorithm
	}

	 := int(binary.BigEndian.Uint16([2:]))
	if ( + 4) != len() {
		return errBufferTooSmall
	}

	.Signature = append([]byte{}, [4:]...)

	return nil
}