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

package srtp

const labelExtractorDtlsSrtp = "EXTRACTOR-dtls_srtp"

// KeyingMaterialExporter allows package SRTP to extract keying material.
type KeyingMaterialExporter interface {
	ExportKeyingMaterial(label string, context []byte, length int) ([]byte, error)
}

// ExtractSessionKeysFromDTLS allows setting the Config SessionKeys by
// extracting them from DTLS. This behavior is defined in RFC5764:
// https://tools.ietf.org/html/rfc5764
func ( *Config) ( KeyingMaterialExporter,  bool) error {
	,  := .Profile.KeyLen()
	if  != nil {
		return 
	}

	,  := .Profile.SaltLen()
	if  != nil {
		return 
	}

	,  := .ExportKeyingMaterial(labelExtractorDtlsSrtp, nil, (*2)+(*2))
	if  != nil {
		return 
	}

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

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

	 = append(, [:+]...)
	 += 

	 = append(, [:+]...)

	if  {
		.Keys.LocalMasterKey = [0:]
		.Keys.LocalMasterSalt = [:]
		.Keys.RemoteMasterKey = [0:]
		.Keys.RemoteMasterSalt = [:]

		return nil
	}

	.Keys.LocalMasterKey = [0:]
	.Keys.LocalMasterSalt = [:]
	.Keys.RemoteMasterKey = [0:]
	.Keys.RemoteMasterSalt = [:]

	return nil
}