package pac

import (
	
	
	

	
	
	
	
	
)

// https://msdn.microsoft.com/en-us/library/cc237931.aspx

// CredentialsInfo implements https://msdn.microsoft.com/en-us/library/cc237953.aspx
type CredentialsInfo struct {
	Version                    uint32 // A 32-bit unsigned integer in little-endian format that defines the version. MUST be 0x00000000.
	EType                      uint32
	PACCredentialDataEncrypted []byte // Key usage number for encryption: KERB_NON_KERB_SALT (16)
	PACCredentialData          CredentialData
}

// Unmarshal bytes into the CredentialsInfo struct
func ( *CredentialsInfo) ( []byte,  types.EncryptionKey) ( error) {
	//The CredentialsInfo structure is a simple structure that is not NDR-encoded.
	 := mstypes.NewReader(bytes.NewReader())

	.Version,  = .Uint32()
	if  != nil {
		return
	}
	if .Version != 0 {
		 = errors.New("credentials info version is not zero")
		return
	}
	.EType,  = .Uint32()
	if  != nil {
		return
	}
	.PACCredentialDataEncrypted,  = .ReadBytes(len() - 8)
	if  != nil {
		 = fmt.Errorf("error reading PAC Credetials Data: %v", )
		return
	}

	 = .DecryptEncPart()
	if  != nil {
		 = fmt.Errorf("error decrypting PAC Credentials Data: %v", )
		return
	}
	return
}

// DecryptEncPart decrypts the encrypted part of the CredentialsInfo.
func ( *CredentialsInfo) ( types.EncryptionKey) error {
	if .KeyType != int32(.EType) {
		return fmt.Errorf("key provided is not the correct type. Type needed: %d, type provided: %d", .EType, .KeyType)
	}
	,  := crypto.DecryptMessage(.PACCredentialDataEncrypted, , keyusage.KERB_NON_KERB_SALT)
	if  != nil {
		return 
	}
	 = .PACCredentialData.Unmarshal()
	if  != nil {
		return 
	}
	return nil
}

// CredentialData implements https://msdn.microsoft.com/en-us/library/cc237952.aspx
type CredentialData struct {
	CredentialCount uint32
	Credentials     []SECPKGSupplementalCred // Size is the value of CredentialCount
}

// Unmarshal converts the bytes provided into a CredentialData type.
func ( *CredentialData) ( []byte) ( error) {
	 := ndr.NewDecoder(bytes.NewReader())
	 = .Decode()
	if  != nil {
		 = fmt.Errorf("error unmarshaling KerbValidationInfo: %v", )
	}
	return
}