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

package dtls

import (
	

	
	
	
	
	
)

func flight6Parse(
	 context.Context,
	 flightConn,
	 *State,
	 *handshakeCache,
	 *handshakeConfig,
) (flightVal, *alert.Alert, error) {
	, ,  := .fullPullMap(.handshakeRecvSequence-1, .cipherSuite,
		handshakeCachePullRule{handshake.TypeFinished, .initialEpoch + 1, true, false},
	)
	if ! {
		// No valid message received. Keep reading
		return 0, nil, nil
	}

	if _,  = [handshake.TypeFinished].(*handshake.MessageFinished); ! {
		return 0, &alert.Alert{Level: alert.Fatal, Description: alert.InternalError}, nil
	}

	// Other party may re-transmit the last flight. Keep state to be flight6.
	return flight6, nil, nil
}

func flight6Generate(
	 flightConn,
	 *State,
	 *handshakeCache,
	 *handshakeConfig,
) ([]*packet, *alert.Alert, error) {
	var  []*packet

	 = append(,
		&packet{
			record: &recordlayer.RecordLayer{
				Header: recordlayer.Header{
					Version: protocol.Version1_2,
				},
				Content: &protocol.ChangeCipherSpec{},
			},
		})

	if len(.localVerifyData) == 0 {
		 := .pullAndMerge(
			handshakeCachePullRule{handshake.TypeClientHello, .initialEpoch, true, false},
			handshakeCachePullRule{handshake.TypeServerHello, .initialEpoch, false, false},
			handshakeCachePullRule{handshake.TypeCertificate, .initialEpoch, false, false},
			handshakeCachePullRule{handshake.TypeServerKeyExchange, .initialEpoch, false, false},
			handshakeCachePullRule{handshake.TypeCertificateRequest, .initialEpoch, false, false},
			handshakeCachePullRule{handshake.TypeServerHelloDone, .initialEpoch, false, false},
			handshakeCachePullRule{handshake.TypeCertificate, .initialEpoch, true, false},
			handshakeCachePullRule{handshake.TypeClientKeyExchange, .initialEpoch, true, false},
			handshakeCachePullRule{handshake.TypeCertificateVerify, .initialEpoch, true, false},
			handshakeCachePullRule{handshake.TypeFinished, .initialEpoch + 1, true, false},
		)

		var  error
		.localVerifyData,  = prf.VerifyDataServer(.masterSecret, , .cipherSuite.HashFunc())
		if  != nil {
			return nil, &alert.Alert{Level: alert.Fatal, Description: alert.InternalError}, 
		}
	}

	 = append(,
		&packet{
			record: &recordlayer.RecordLayer{
				Header: recordlayer.Header{
					Version: protocol.Version1_2,
					Epoch:   1,
				},
				Content: &handshake.Handshake{
					Message: &handshake.MessageFinished{
						VerifyData: .localVerifyData,
					},
				},
			},
			shouldWrapCID:            len(.remoteConnectionID) > 0,
			shouldEncrypt:            true,
			resetLocalSequenceNumber: true,
		},
	)

	return , nil, nil
}