2023-12-05 10:16:50 -05:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/api/attestationconfigapi"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/idkeydigest"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/variant"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
2024-06-12 04:24:16 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/encoding"
|
2023-12-05 10:16:50 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// naming schema:
|
|
|
|
// convertFromTf<type> : convert a terraform struct to a constellation struct
|
|
|
|
// convertToTf<type> : convert a constellation struct to a terraform struct
|
|
|
|
// terraform struct: used to parse the terraform state
|
|
|
|
// constellation struct: used to call the constellation API
|
|
|
|
|
|
|
|
// convertFromTfAttestationCfg converts the related terraform struct to a constellation attestation config.
|
2023-12-18 04:15:54 -05:00
|
|
|
func convertFromTfAttestationCfg(tfAttestation attestationAttribute, attestationVariant variant.Variant) (config.AttestationCfg, error) {
|
2023-12-05 10:16:50 -05:00
|
|
|
c11nMeasurements := make(measurements.M)
|
|
|
|
for strIdx, v := range tfAttestation.Measurements {
|
|
|
|
idx, err := strconv.ParseUint(strIdx, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
expectedBt, err := hex.DecodeString(v.Expected)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var valOption measurements.MeasurementValidationOption
|
|
|
|
switch v.WarnOnly {
|
|
|
|
case true:
|
|
|
|
valOption = measurements.WarnOnly
|
|
|
|
case false:
|
|
|
|
valOption = measurements.Enforce
|
|
|
|
}
|
|
|
|
c11nMeasurements[uint32(idx)] = measurements.Measurement{
|
|
|
|
Expected: expectedBt,
|
|
|
|
ValidationOpt: valOption,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var attestationConfig config.AttestationCfg
|
|
|
|
switch attestationVariant {
|
|
|
|
case variant.AzureSEVSNP{}:
|
|
|
|
firmwareCfg, err := convertFromTfFirmwareCfg(tfAttestation.AzureSNPFirmwareSignerConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("converting firmware signer config: %w", err)
|
|
|
|
}
|
2023-12-11 09:55:44 -05:00
|
|
|
|
2023-12-05 10:16:50 -05:00
|
|
|
var rootKey config.Certificate
|
|
|
|
if err := json.Unmarshal([]byte(tfAttestation.AMDRootKey), &rootKey); err != nil {
|
|
|
|
return nil, fmt.Errorf("unmarshalling root key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
attestationConfig = &config.AzureSEVSNP{
|
|
|
|
Measurements: c11nMeasurements,
|
|
|
|
BootloaderVersion: newVersion(tfAttestation.BootloaderVersion),
|
|
|
|
TEEVersion: newVersion(tfAttestation.TEEVersion),
|
|
|
|
SNPVersion: newVersion(tfAttestation.SNPVersion),
|
|
|
|
MicrocodeVersion: newVersion(tfAttestation.MicrocodeVersion),
|
|
|
|
FirmwareSignerConfig: firmwareCfg,
|
|
|
|
AMDRootKey: rootKey,
|
|
|
|
}
|
2023-12-11 09:55:44 -05:00
|
|
|
case variant.AWSSEVSNP{}:
|
|
|
|
var rootKey config.Certificate
|
|
|
|
if err := json.Unmarshal([]byte(tfAttestation.AMDRootKey), &rootKey); err != nil {
|
|
|
|
return nil, fmt.Errorf("unmarshalling root key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
attestationConfig = &config.AWSSEVSNP{
|
|
|
|
Measurements: c11nMeasurements,
|
|
|
|
BootloaderVersion: newVersion(tfAttestation.BootloaderVersion),
|
|
|
|
TEEVersion: newVersion(tfAttestation.TEEVersion),
|
|
|
|
SNPVersion: newVersion(tfAttestation.SNPVersion),
|
|
|
|
MicrocodeVersion: newVersion(tfAttestation.MicrocodeVersion),
|
|
|
|
AMDRootKey: rootKey,
|
|
|
|
}
|
2024-01-26 09:46:21 -05:00
|
|
|
case variant.AzureTDX{}:
|
|
|
|
var rootKey config.Certificate
|
|
|
|
if err := json.Unmarshal([]byte(tfAttestation.TDX.IntelRootKey), &rootKey); err != nil {
|
|
|
|
return nil, fmt.Errorf("unmarshalling root key: %w", err)
|
|
|
|
}
|
|
|
|
teeTCBSVN, err := hex.DecodeString(tfAttestation.TDX.TEETCBSVN)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("decoding tee_tcb_svn: %w", err)
|
|
|
|
}
|
|
|
|
qeVendorID, err := hex.DecodeString(tfAttestation.TDX.QEVendorID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("decoding qe_vendor_id: %w", err)
|
|
|
|
}
|
|
|
|
mrSeam, err := hex.DecodeString(tfAttestation.TDX.MRSeam)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("decoding mr_seam: %w", err)
|
|
|
|
}
|
|
|
|
xfam, err := hex.DecodeString(tfAttestation.TDX.XFAM)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("decoding xfam: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
attestationConfig = &config.AzureTDX{
|
|
|
|
Measurements: c11nMeasurements,
|
2024-06-12 04:24:16 -04:00
|
|
|
QESVN: newVersion(tfAttestation.TDX.QESVN),
|
|
|
|
PCESVN: newVersion(tfAttestation.TDX.PCESVN),
|
|
|
|
TEETCBSVN: newVersion(encoding.HexBytes(teeTCBSVN)),
|
|
|
|
QEVendorID: newVersion(encoding.HexBytes(qeVendorID)),
|
2024-01-26 09:46:21 -05:00
|
|
|
MRSeam: mrSeam,
|
2024-06-12 04:24:16 -04:00
|
|
|
XFAM: newVersion(encoding.HexBytes(xfam)),
|
2024-01-26 09:46:21 -05:00
|
|
|
IntelRootKey: rootKey,
|
|
|
|
}
|
2023-12-11 09:55:44 -05:00
|
|
|
case variant.GCPSEVES{}:
|
|
|
|
attestationConfig = &config.GCPSEVES{
|
|
|
|
Measurements: c11nMeasurements,
|
|
|
|
}
|
2024-04-16 12:13:47 -04:00
|
|
|
case variant.GCPSEVSNP{}:
|
|
|
|
attestationConfig = &config.GCPSEVSNP{
|
|
|
|
Measurements: c11nMeasurements,
|
|
|
|
}
|
2024-03-06 14:48:40 -05:00
|
|
|
case variant.QEMUVTPM{}:
|
|
|
|
attestationConfig = &config.QEMUVTPM{
|
|
|
|
Measurements: c11nMeasurements,
|
|
|
|
}
|
2023-12-11 09:55:44 -05:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown attestation variant: %s", attestationVariant)
|
2023-12-05 10:16:50 -05:00
|
|
|
}
|
|
|
|
return attestationConfig, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertToTfAttestationCfg converts the constellation attestation config to the related terraform structs.
|
2024-06-12 10:30:03 -04:00
|
|
|
func convertToTfAttestation(attVar variant.Variant, latestVersions attestationconfigapi.Entry) (tfAttestation attestationAttribute, err error) {
|
2023-12-18 04:15:54 -05:00
|
|
|
tfAttestation = attestationAttribute{
|
2024-06-12 04:24:16 -04:00
|
|
|
Variant: attVar.String(),
|
2023-12-05 10:16:50 -05:00
|
|
|
}
|
2023-12-11 09:55:44 -05:00
|
|
|
|
|
|
|
switch attVar {
|
|
|
|
case variant.AWSSEVSNP{}:
|
2023-12-05 10:16:50 -05:00
|
|
|
certStr, err := certAsString(config.DefaultForAWSSEVSNP().AMDRootKey)
|
|
|
|
if err != nil {
|
|
|
|
return tfAttestation, err
|
|
|
|
}
|
|
|
|
tfAttestation.AMDRootKey = certStr
|
2024-06-12 04:24:16 -04:00
|
|
|
tfAttestation.BootloaderVersion = latestVersions.Bootloader
|
|
|
|
tfAttestation.TEEVersion = latestVersions.TEE
|
|
|
|
tfAttestation.SNPVersion = latestVersions.SNP
|
|
|
|
tfAttestation.MicrocodeVersion = latestVersions.Microcode
|
2023-12-05 10:16:50 -05:00
|
|
|
|
2024-04-16 12:13:47 -04:00
|
|
|
case variant.GCPSEVSNP{}:
|
|
|
|
certStr, err := certAsString(config.DefaultForGCPSEVSNP().AMDRootKey)
|
|
|
|
if err != nil {
|
|
|
|
return tfAttestation, err
|
|
|
|
}
|
|
|
|
tfAttestation.AMDRootKey = certStr
|
2024-06-12 04:24:16 -04:00
|
|
|
tfAttestation.BootloaderVersion = latestVersions.Bootloader
|
|
|
|
tfAttestation.TEEVersion = latestVersions.TEE
|
|
|
|
tfAttestation.SNPVersion = latestVersions.SNP
|
|
|
|
tfAttestation.MicrocodeVersion = latestVersions.Microcode
|
2024-04-16 12:13:47 -04:00
|
|
|
|
2023-12-11 09:55:44 -05:00
|
|
|
case variant.AzureSEVSNP{}:
|
|
|
|
certStr, err := certAsString(config.DefaultForAzureSEVSNP().AMDRootKey)
|
2023-12-05 10:16:50 -05:00
|
|
|
if err != nil {
|
|
|
|
return tfAttestation, err
|
|
|
|
}
|
|
|
|
tfAttestation.AMDRootKey = certStr
|
2024-06-12 04:24:16 -04:00
|
|
|
tfAttestation.BootloaderVersion = latestVersions.Bootloader
|
|
|
|
tfAttestation.TEEVersion = latestVersions.TEE
|
|
|
|
tfAttestation.SNPVersion = latestVersions.SNP
|
|
|
|
tfAttestation.MicrocodeVersion = latestVersions.Microcode
|
2023-12-05 10:16:50 -05:00
|
|
|
|
|
|
|
firmwareCfg := config.DefaultForAzureSEVSNP().FirmwareSignerConfig
|
|
|
|
tfFirmwareCfg, err := convertToTfFirmwareCfg(firmwareCfg)
|
|
|
|
if err != nil {
|
|
|
|
return tfAttestation, err
|
|
|
|
}
|
|
|
|
tfAttestation.AzureSNPFirmwareSignerConfig = tfFirmwareCfg
|
2024-06-12 04:24:16 -04:00
|
|
|
|
2024-01-26 09:46:21 -05:00
|
|
|
case variant.AzureTDX{}:
|
2024-06-12 04:24:16 -04:00
|
|
|
certStr, err := certAsString(config.DefaultForAzureTDX().IntelRootKey)
|
2024-01-26 09:46:21 -05:00
|
|
|
if err != nil {
|
|
|
|
return tfAttestation, err
|
|
|
|
}
|
2024-06-12 04:24:16 -04:00
|
|
|
tfAttestation.TDX.IntelRootKey = certStr
|
|
|
|
tfAttestation.TDX.PCESVN = latestVersions.PCESVN
|
|
|
|
tfAttestation.TDX.QESVN = latestVersions.QESVN
|
|
|
|
tfAttestation.TDX.TEETCBSVN = hex.EncodeToString(latestVersions.TEETCBSVN[:])
|
|
|
|
tfAttestation.TDX.QEVendorID = hex.EncodeToString(latestVersions.QEVendorID[:])
|
|
|
|
tfAttestation.TDX.XFAM = hex.EncodeToString(latestVersions.XFAM[:])
|
2024-01-26 09:46:21 -05:00
|
|
|
|
2024-03-06 14:48:40 -05:00
|
|
|
case variant.GCPSEVES{}, variant.QEMUVTPM{}:
|
2023-12-11 09:55:44 -05:00
|
|
|
// no additional fields
|
|
|
|
default:
|
|
|
|
return tfAttestation, fmt.Errorf("unknown attestation variant: %s", attVar)
|
2023-12-05 10:16:50 -05:00
|
|
|
}
|
|
|
|
return tfAttestation, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func certAsString(cert config.Certificate) (string, error) {
|
|
|
|
certBytes, err := cert.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(certBytes), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertToTfFirmwareCfg converts the constellation firmware config to the terraform struct.
|
2023-12-18 04:15:54 -05:00
|
|
|
func convertToTfFirmwareCfg(firmwareCfg config.SNPFirmwareSignerConfig) (azureSnpFirmwareSignerConfigAttribute, error) {
|
2023-12-05 10:16:50 -05:00
|
|
|
keyDigestAny, err := firmwareCfg.AcceptedKeyDigests.MarshalYAML()
|
|
|
|
if err != nil {
|
2023-12-18 04:15:54 -05:00
|
|
|
return azureSnpFirmwareSignerConfigAttribute{}, err
|
2023-12-05 10:16:50 -05:00
|
|
|
}
|
|
|
|
keyDigest, ok := keyDigestAny.([]string)
|
|
|
|
if !ok {
|
2023-12-18 04:15:54 -05:00
|
|
|
return azureSnpFirmwareSignerConfigAttribute{}, fmt.Errorf("reading Accepted Key Digests: could not convert %T to []string", keyDigestAny)
|
2023-12-05 10:16:50 -05:00
|
|
|
}
|
2023-12-18 04:15:54 -05:00
|
|
|
return azureSnpFirmwareSignerConfigAttribute{
|
2023-12-05 10:16:50 -05:00
|
|
|
AcceptedKeyDigests: keyDigest,
|
|
|
|
EnforcementPolicy: firmwareCfg.EnforcementPolicy.String(),
|
|
|
|
MAAURL: firmwareCfg.MAAURL,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertFromTfFirmwareCfg converts the terraform struct to a constellation firmware config.
|
2023-12-18 04:15:54 -05:00
|
|
|
func convertFromTfFirmwareCfg(tfFirmwareCfg azureSnpFirmwareSignerConfigAttribute) (config.SNPFirmwareSignerConfig, error) {
|
2023-12-05 10:16:50 -05:00
|
|
|
keyDigests, err := idkeydigest.UnmarshalHexString(tfFirmwareCfg.AcceptedKeyDigests)
|
|
|
|
if err != nil {
|
|
|
|
return config.SNPFirmwareSignerConfig{}, err
|
|
|
|
}
|
|
|
|
return config.SNPFirmwareSignerConfig{
|
|
|
|
AcceptedKeyDigests: keyDigests,
|
|
|
|
EnforcementPolicy: idkeydigest.EnforcePolicyFromString(tfFirmwareCfg.EnforcementPolicy),
|
|
|
|
MAAURL: tfFirmwareCfg.MAAURL,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertToTfMeasurements converts the constellation measurements to the terraform struct.
|
2023-12-18 04:15:54 -05:00
|
|
|
func convertToTfMeasurements(m measurements.M) map[string]measurementAttribute {
|
|
|
|
tfMeasurements := map[string]measurementAttribute{}
|
2023-12-05 10:16:50 -05:00
|
|
|
for key, value := range m {
|
|
|
|
keyStr := strconv.FormatUint(uint64(key), 10)
|
2023-12-18 04:15:54 -05:00
|
|
|
tfMeasurements[keyStr] = measurementAttribute{
|
2023-12-05 10:16:50 -05:00
|
|
|
Expected: hex.EncodeToString(value.Expected),
|
|
|
|
WarnOnly: bool(value.ValidationOpt),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tfMeasurements
|
|
|
|
}
|
|
|
|
|
2024-06-12 04:24:16 -04:00
|
|
|
func newVersion[T uint8 | uint16 | encoding.HexBytes](v T) config.AttestationVersion[T] {
|
|
|
|
return config.AttestationVersion[T]{
|
2023-12-05 10:16:50 -05:00
|
|
|
Value: v,
|
|
|
|
}
|
|
|
|
}
|