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"
|
|
|
|
"encoding/base64"
|
2022-08-29 10:41:09 -04:00
|
|
|
"encoding/hex"
|
2022-04-19 11:02:02 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/atls"
|
2022-10-21 11:09:10 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/aws"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/azure/snp"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/azure/trustedlaunch"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/gcp"
|
2022-11-15 09:40:49 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/qemu"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/vtpm"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
|
|
|
"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
|
|
|
// Validator validates Platform Configuration Registers (PCRs).
|
2022-08-09 08:04:40 -04:00
|
|
|
type Validator struct {
|
2022-08-29 10:41:09 -04:00
|
|
|
provider cloudprovider.Provider
|
2022-11-15 09:40:49 -05:00
|
|
|
pcrs measurements.M
|
2022-08-29 10:41:09 -04:00
|
|
|
enforcedPCRs []uint32
|
|
|
|
idkeydigest []byte
|
2022-10-05 09:02:46 -04:00
|
|
|
enforceIDKeyDigest bool
|
2022-08-31 14:10:49 -04:00
|
|
|
azureCVM bool
|
2022-08-29 10:41:09 -04:00
|
|
|
validator atls.Validator
|
2022-04-19 11:02:02 -04:00
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// NewValidator creates a new Validator.
|
2022-10-21 11:09:10 -04:00
|
|
|
func NewValidator(provider cloudprovider.Provider, conf *config.Config) (*Validator, error) {
|
2022-08-09 08:04:40 -04:00
|
|
|
v := Validator{}
|
2022-04-27 05:17:41 -04:00
|
|
|
if provider == cloudprovider.Unknown {
|
|
|
|
return nil, errors.New("unknown cloud provider")
|
|
|
|
}
|
|
|
|
v.provider = provider
|
2022-10-21 11:09:10 -04:00
|
|
|
if err := v.setPCRs(conf); err != nil {
|
2022-04-27 05:17:41 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-29 10:41:09 -04:00
|
|
|
|
|
|
|
if v.provider == cloudprovider.Azure {
|
2022-10-21 11:09:10 -04:00
|
|
|
v.azureCVM = *conf.Provider.Azure.ConfidentialVM
|
2022-08-31 14:10:49 -04:00
|
|
|
if v.azureCVM {
|
2022-10-21 11:09:10 -04:00
|
|
|
idkeydigest, err := hex.DecodeString(conf.Provider.Azure.IDKeyDigest)
|
2022-08-31 14:10:49 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("bad config: decoding idkeydigest from config: %w", err)
|
|
|
|
}
|
2022-10-21 11:09:10 -04:00
|
|
|
v.enforceIDKeyDigest = *conf.Provider.Azure.EnforceIDKeyDigest
|
2022-08-31 14:10:49 -04:00
|
|
|
v.idkeydigest = idkeydigest
|
2022-08-29 10:41:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 05:17:41 -04:00
|
|
|
return &v, nil
|
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// UpdateInitPCRs sets the owner and cluster PCR values.
|
2022-08-09 08:04:40 -04:00
|
|
|
func (v *Validator) UpdateInitPCRs(ownerID, clusterID string) error {
|
2022-04-27 05:17:41 -04:00
|
|
|
if err := v.updatePCR(uint32(vtpm.PCRIndexOwnerID), ownerID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return v.updatePCR(uint32(vtpm.PCRIndexClusterID), clusterID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// updatePCR adds a new entry to the pcr map of v, or removes the key if the input is an empty string.
|
|
|
|
//
|
|
|
|
// When adding, the input is first decoded from base64.
|
|
|
|
// We then calculate the expected PCR by hashing the input using SHA256,
|
|
|
|
// appending expected PCR for initialization, and then hashing once more.
|
2022-08-09 08:04:40 -04:00
|
|
|
func (v *Validator) updatePCR(pcrIndex uint32, encoded string) error {
|
2022-04-27 05:17:41 -04:00
|
|
|
if encoded == "" {
|
|
|
|
delete(v.pcrs, pcrIndex)
|
2022-08-12 09:59:45 -04:00
|
|
|
|
|
|
|
// remove enforced PCR if it exists
|
|
|
|
for i, enforcedIdx := range v.enforcedPCRs {
|
|
|
|
if enforcedIdx == pcrIndex {
|
|
|
|
v.enforcedPCRs[i] = v.enforcedPCRs[len(v.enforcedPCRs)-1]
|
|
|
|
v.enforcedPCRs = v.enforcedPCRs[:len(v.enforcedPCRs)-1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 05:17:41 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
decoded, err := base64.StdEncoding.DecodeString(encoded)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("input [%s] is not base64 encoded: %w", encoded, err)
|
|
|
|
}
|
|
|
|
// new_pcr_value := hash(old_pcr_value || data_to_extend)
|
|
|
|
// 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)
|
|
|
|
expectedPcr := sha256.Sum256(append(v.pcrs[pcrIndex], hashedInput[:]...))
|
|
|
|
v.pcrs[pcrIndex] = expectedPcr[:]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-09 08:04:40 -04:00
|
|
|
func (v *Validator) setPCRs(config *config.Config) error {
|
2022-04-27 05:17:41 -04:00
|
|
|
switch v.provider {
|
2022-10-21 06:24:18 -04:00
|
|
|
case cloudprovider.AWS:
|
|
|
|
awsPCRs := config.Provider.AWS.Measurements
|
|
|
|
enforcedPCRs := config.Provider.AWS.EnforcedMeasurements
|
|
|
|
if err := v.checkPCRs(awsPCRs, enforcedPCRs); err != nil {
|
2022-04-27 05:17:41 -04:00
|
|
|
return err
|
2022-04-19 11:02:02 -04:00
|
|
|
}
|
2022-08-12 09:59:45 -04:00
|
|
|
v.enforcedPCRs = enforcedPCRs
|
2022-10-21 06:24:18 -04:00
|
|
|
v.pcrs = awsPCRs
|
2022-04-19 11:02:02 -04:00
|
|
|
case cloudprovider.Azure:
|
2022-05-16 12:54:25 -04:00
|
|
|
azurePCRs := config.Provider.Azure.Measurements
|
2022-08-12 09:59:45 -04:00
|
|
|
enforcedPCRs := config.Provider.Azure.EnforcedMeasurements
|
|
|
|
if err := v.checkPCRs(azurePCRs, enforcedPCRs); err != nil {
|
2022-04-27 05:17:41 -04:00
|
|
|
return err
|
2022-04-19 11:02:02 -04:00
|
|
|
}
|
2022-08-12 09:59:45 -04:00
|
|
|
v.enforcedPCRs = enforcedPCRs
|
2022-04-27 05:17:41 -04:00
|
|
|
v.pcrs = azurePCRs
|
2022-10-21 06:24:18 -04:00
|
|
|
case cloudprovider.GCP:
|
|
|
|
gcpPCRs := config.Provider.GCP.Measurements
|
|
|
|
enforcedPCRs := config.Provider.GCP.EnforcedMeasurements
|
|
|
|
if err := v.checkPCRs(gcpPCRs, enforcedPCRs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
v.enforcedPCRs = enforcedPCRs
|
|
|
|
v.pcrs = gcpPCRs
|
2022-05-02 04:54:54 -04:00
|
|
|
case cloudprovider.QEMU:
|
2022-05-16 12:54:25 -04:00
|
|
|
qemuPCRs := config.Provider.QEMU.Measurements
|
2022-08-12 09:59:45 -04:00
|
|
|
enforcedPCRs := config.Provider.QEMU.EnforcedMeasurements
|
|
|
|
if err := v.checkPCRs(qemuPCRs, enforcedPCRs); err != nil {
|
2022-05-02 04:54:54 -04:00
|
|
|
return err
|
|
|
|
}
|
2022-08-12 09:59:45 -04:00
|
|
|
v.enforcedPCRs = enforcedPCRs
|
2022-05-02 04:54:54 -04:00
|
|
|
v.pcrs = qemuPCRs
|
2022-04-19 11:02:02 -04:00
|
|
|
}
|
2022-04-27 05:17:41 -04:00
|
|
|
return nil
|
2022-04-19 11:02:02 -04:00
|
|
|
}
|
|
|
|
|
2022-08-09 08:04:40 -04:00
|
|
|
// V returns the validator as atls.Validator.
|
2022-08-12 09:59:45 -04:00
|
|
|
func (v *Validator) V(cmd *cobra.Command) atls.Validator {
|
|
|
|
v.updateValidator(cmd)
|
2022-08-09 08:04:40 -04:00
|
|
|
return v.validator
|
2022-04-19 11:02:02 -04:00
|
|
|
}
|
|
|
|
|
2022-08-09 08:04:40 -04:00
|
|
|
// PCRS returns the validator's PCR map.
|
2022-11-15 09:40:49 -05:00
|
|
|
func (v *Validator) PCRS() measurements.M {
|
2022-08-09 08:04:40 -04:00
|
|
|
return v.pcrs
|
|
|
|
}
|
|
|
|
|
2022-08-12 09:59:45 -04:00
|
|
|
func (v *Validator) updateValidator(cmd *cobra.Command) {
|
2022-08-29 10:41:09 -04:00
|
|
|
log := warnLogger{cmd: cmd}
|
2022-04-27 05:17:41 -04:00
|
|
|
switch v.provider {
|
|
|
|
case cloudprovider.GCP:
|
2022-08-29 10:41:09 -04:00
|
|
|
v.validator = gcp.NewValidator(v.pcrs, v.enforcedPCRs, log)
|
2022-04-27 05:17:41 -04:00
|
|
|
case cloudprovider.Azure:
|
2022-08-31 14:10:49 -04:00
|
|
|
if v.azureCVM {
|
2022-10-05 09:02:46 -04:00
|
|
|
v.validator = snp.NewValidator(v.pcrs, v.enforcedPCRs, v.idkeydigest, v.enforceIDKeyDigest, log)
|
2022-08-31 14:10:49 -04:00
|
|
|
} else {
|
|
|
|
v.validator = trustedlaunch.NewValidator(v.pcrs, v.enforcedPCRs, log)
|
|
|
|
}
|
2022-10-21 11:09:10 -04:00
|
|
|
case cloudprovider.AWS:
|
|
|
|
v.validator = aws.NewValidator(v.pcrs, v.enforcedPCRs, log)
|
2022-05-02 04:54:54 -04:00
|
|
|
case cloudprovider.QEMU:
|
2022-08-29 10:41:09 -04:00
|
|
|
v.validator = qemu.NewValidator(v.pcrs, v.enforcedPCRs, log)
|
2022-04-27 05:17:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-15 09:40:49 -05:00
|
|
|
func (v *Validator) checkPCRs(pcrs measurements.M, enforcedPCRs []uint32) error {
|
2022-04-27 05:17:41 -04:00
|
|
|
if len(pcrs) == 0 {
|
|
|
|
return errors.New("no PCR values provided")
|
|
|
|
}
|
|
|
|
for k, v := range pcrs {
|
|
|
|
if len(v) != 32 {
|
|
|
|
return fmt.Errorf("bad config: PCR[%d]: expected length: %d, but got: %d", k, 32, len(v))
|
|
|
|
}
|
2022-04-19 11:02:02 -04:00
|
|
|
}
|
2022-08-12 09:59:45 -04:00
|
|
|
for _, v := range enforcedPCRs {
|
|
|
|
if _, ok := pcrs[v]; !ok {
|
|
|
|
return fmt.Errorf("bad config: PCR[%d] is enforced, but no expected measurement is provided", v)
|
|
|
|
}
|
|
|
|
}
|
2022-04-27 05:17:41 -04:00
|
|
|
return nil
|
2022-04-19 11:02:02 -04:00
|
|
|
}
|
|
|
|
|
2022-08-12 09:59:45 -04:00
|
|
|
// warnLogger implements logging of warnings for validators.
|
|
|
|
type warnLogger struct {
|
|
|
|
cmd *cobra.Command
|
2022-07-20 10:44:41 -04:00
|
|
|
}
|
|
|
|
|
2022-10-14 10:29:21 -04:00
|
|
|
// Infof is a no-op since we don't want extra info messages when using the CLI.
|
2022-10-25 09:51:23 -04:00
|
|
|
func (wl warnLogger) Infof(format string, args ...any) {}
|
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
|
|
|
}
|