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

package dtls

import (
	

	
	
	
	
	
)

func flight5bParse( context.Context,  flightConn,  *State,  *handshakeCache,  *handshakeConfig) (flightVal, *alert.Alert, error) {
	, ,  := .fullPullMap(.handshakeRecvSequence-1, .cipherSuite,
		handshakeCachePullRule{handshake.TypeFinished, .initialEpoch + 1, false, 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 flight5b.
	return flight5b, nil, nil
}

func flight5bGenerate( flightConn,  *State,  *handshakeCache,  *handshakeConfig) ([]*packet, *alert.Alert, error) { //nolint:gocognit
	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.TypeFinished, .initialEpoch + 1, false, false},
		)

		var  error
		.localVerifyData,  = prf.VerifyDataClient(.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,
					},
				},
			},
			shouldEncrypt:            true,
			resetLocalSequenceNumber: true,
		})

	return , nil, nil
}