2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-04-19 11:02:02 -04:00
|
|
|
package cloudcmd
|
|
|
|
|
|
|
|
import (
|
2022-04-27 05:17:41 -04:00
|
|
|
"crypto/sha256"
|
2023-03-22 09:56:51 -04:00
|
|
|
"crypto/sha512"
|
2022-04-27 05:17:41 -04:00
|
|
|
"encoding/base64"
|
2022-08-29 10:41:09 -04:00
|
|
|
"encoding/hex"
|
2022-04-19 11:02:02 -04:00
|
|
|
"fmt"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/atls"
|
2023-03-14 06:46:27 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/choose"
|
2022-11-15 09:40:49 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
|
2023-06-09 09:41:02 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/variant"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
2022-08-12 09:59:45 -04:00
|
|
|
"github.com/spf13/cobra"
|
2022-04-19 11:02:02 -04:00
|
|
|
)
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// NewValidator creates a new Validator.
|
2023-05-03 05:11:53 -04:00
|
|
|
func NewValidator(cmd *cobra.Command, config config.AttestationCfg, log debugLog) (atls.Validator, error) {
|
|
|
|
return choose.Validator(config, warnLogger{cmd: cmd, log: log})
|
2022-04-27 05:17:41 -04:00
|
|
|
}
|
|
|
|
|
2023-03-22 08:40:02 -04:00
|
|
|
// UpdateInitMeasurements sets the owner and cluster measurement values.
|
|
|
|
func UpdateInitMeasurements(config config.AttestationCfg, ownerID, clusterID string) error {
|
2023-05-03 05:11:53 -04:00
|
|
|
m := config.GetMeasurements()
|
2023-03-22 09:56:51 -04:00
|
|
|
|
|
|
|
switch config.GetVariant() {
|
2023-06-09 09:41:02 -04:00
|
|
|
case variant.AWSNitroTPM{}, variant.AWSSEVSNP{}, variant.AzureTrustedLaunch{}, variant.AzureSEVSNP{}, variant.GCPSEVES{}, variant.QEMUVTPM{}:
|
2023-03-22 09:56:51 -04:00
|
|
|
if err := updateMeasurementTPM(m, uint32(measurements.PCRIndexOwnerID), ownerID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return updateMeasurementTPM(m, uint32(measurements.PCRIndexClusterID), clusterID)
|
|
|
|
case variant.QEMUTDX{}:
|
|
|
|
// Measuring ownerID is currently not implemented for Constellation
|
|
|
|
// Since adding support for measuring ownerID to TDX would require additional code changes,
|
|
|
|
// the current implementation does not support it, but can be changed if we decide to add support in the future
|
|
|
|
return updateMeasurementTDX(m, uint32(measurements.TDXIndexClusterID), clusterID)
|
|
|
|
default:
|
2023-03-22 11:53:01 -04:00
|
|
|
return fmt.Errorf("selecting attestation variant: unknown attestation variant")
|
2022-04-27 05:17:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 09:56:51 -04:00
|
|
|
func updateMeasurementTDX(m measurements.M, measurementIdx uint32, encoded string) error {
|
2022-04-27 05:17:41 -04:00
|
|
|
if encoded == "" {
|
2023-03-22 08:40:02 -04:00
|
|
|
delete(m, measurementIdx)
|
2022-04-27 05:17:41 -04:00
|
|
|
return nil
|
|
|
|
}
|
2023-03-22 09:56:51 -04:00
|
|
|
decoded, err := decodeMeasurement(encoded)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-24 04:57:58 -05:00
|
|
|
|
2023-03-22 09:56:51 -04:00
|
|
|
// new_measurement_value := hash(old_measurement_value || data_to_extend)
|
|
|
|
// Since we use the DG.MR.RTMR.EXTEND call to extend the register, data_to_extend is the hash of our input
|
|
|
|
hashedInput := sha512.Sum384(decoded)
|
|
|
|
oldExpected := m[measurementIdx].Expected
|
|
|
|
expectedMeasurementSum := sha512.Sum384(append(oldExpected[:], hashedInput[:]...))
|
|
|
|
m[measurementIdx] = measurements.Measurement{
|
|
|
|
Expected: expectedMeasurementSum[:],
|
|
|
|
ValidationOpt: m[measurementIdx].ValidationOpt,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateMeasurementTPM(m measurements.M, measurementIdx uint32, encoded string) error {
|
|
|
|
if encoded == "" {
|
|
|
|
delete(m, measurementIdx)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
decoded, err := decodeMeasurement(encoded)
|
2022-04-27 05:17:41 -04:00
|
|
|
if err != nil {
|
2023-03-22 09:56:51 -04:00
|
|
|
return err
|
2022-04-27 05:17:41 -04:00
|
|
|
}
|
2023-03-22 09:56:51 -04:00
|
|
|
|
|
|
|
// new_pcr_value := hash(old_pcr_value || data_to_extend)
|
2022-04-27 05:17:41 -04:00
|
|
|
// Since we use the TPM2_PCR_Event call to extend the PCR, data_to_extend is the hash of our input
|
|
|
|
hashedInput := sha256.Sum256(decoded)
|
2023-03-22 08:40:02 -04:00
|
|
|
oldExpected := m[measurementIdx].Expected
|
|
|
|
expectedMeasurement := sha256.Sum256(append(oldExpected[:], hashedInput[:]...))
|
|
|
|
m[measurementIdx] = measurements.Measurement{
|
|
|
|
Expected: expectedMeasurement[:],
|
|
|
|
ValidationOpt: m[measurementIdx].ValidationOpt,
|
2022-11-24 04:57:58 -05:00
|
|
|
}
|
2022-04-27 05:17:41 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-22 09:56:51 -04:00
|
|
|
func decodeMeasurement(encoded string) ([]byte, error) {
|
|
|
|
decoded, err := hex.DecodeString(encoded)
|
|
|
|
if err != nil {
|
|
|
|
hexErr := err
|
|
|
|
decoded, err = base64.StdEncoding.DecodeString(encoded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("input [%s] could neither be hex decoded (%w) nor base64 decoded (%w)", encoded, hexErr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return decoded, nil
|
|
|
|
}
|
|
|
|
|
2022-08-12 09:59:45 -04:00
|
|
|
// warnLogger implements logging of warnings for validators.
|
|
|
|
type warnLogger struct {
|
|
|
|
cmd *cobra.Command
|
2023-03-03 10:50:25 -05:00
|
|
|
log debugLog
|
2022-07-20 10:44:41 -04:00
|
|
|
}
|
|
|
|
|
2023-03-03 10:50:25 -05:00
|
|
|
// Infof messages are reduced to debug messages, since we don't want
|
|
|
|
// the extra info when using the CLI without setting the debug flag.
|
|
|
|
func (wl warnLogger) Infof(fmtStr string, args ...any) {
|
|
|
|
wl.log.Debugf(fmtStr, args...)
|
|
|
|
}
|
2022-10-14 10:29:21 -04:00
|
|
|
|
2022-08-12 09:59:45 -04:00
|
|
|
// Warnf prints a formatted warning from the validator.
|
2022-10-25 09:51:23 -04:00
|
|
|
func (wl warnLogger) Warnf(fmtStr string, args ...any) {
|
2022-08-12 09:59:45 -04:00
|
|
|
wl.cmd.PrintErrf("Warning: %s\n", fmt.Sprintf(fmtStr, args...))
|
2022-04-19 11:02:02 -04:00
|
|
|
}
|
2023-03-30 10:13:14 -04:00
|
|
|
|
|
|
|
type debugLog interface {
|
|
|
|
Debugf(format string, args ...any)
|
|
|
|
Sync()
|
|
|
|
}
|