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

package handshake

import (
	
)

// MessageCertificate is a DTLS Handshake Message
// it can contain either a Client or Server Certificate
//
// https://tools.ietf.org/html/rfc5246#section-7.4.2
type MessageCertificate struct {
	Certificate [][]byte
}

// Type returns the Handshake Type
func ( MessageCertificate) () Type {
	return TypeCertificate
}

const (
	handshakeMessageCertificateLengthFieldSize = 3
)

// Marshal encodes the Handshake
func ( *MessageCertificate) () ([]byte, error) {
	 := make([]byte, handshakeMessageCertificateLengthFieldSize)

	for ,  := range .Certificate {
		// Certificate Length
		 = append(, make([]byte, handshakeMessageCertificateLengthFieldSize)...)
		util.PutBigEndianUint24([len()-handshakeMessageCertificateLengthFieldSize:], uint32(len()))

		// Certificate body
		 = append(, append([]byte{}, ...)...)
	}

	// Total Payload Size
	util.PutBigEndianUint24([0:], uint32(len([handshakeMessageCertificateLengthFieldSize:])))
	return , nil
}

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

	if  := int(util.BigEndianUint24()); +handshakeMessageCertificateLengthFieldSize != len() {
		return errLengthMismatch
	}

	 := handshakeMessageCertificateLengthFieldSize
	for  < len() {
		 := int(util.BigEndianUint24([:]))
		 += handshakeMessageCertificateLengthFieldSize

		if + > len() {
			return errLengthMismatch
		}

		.Certificate = append(.Certificate, append([]byte{}, [:+]...))
		 += 
	}

	return nil
}