2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2022-10-27 05:04:23 -04:00
|
|
|
"context"
|
|
|
|
"crypto"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2022-11-15 09:40:49 -05:00
|
|
|
awsConfig "github.com/aws/aws-sdk-go-v2/config"
|
2022-10-27 05:04:23 -04:00
|
|
|
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/ec2"
|
2022-11-15 09:40:49 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
|
2022-10-27 05:04:23 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/vtpm"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/oid"
|
2022-10-27 05:04:23 -04:00
|
|
|
"github.com/google/go-tpm/tpm2"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
2022-10-27 05:04:23 -04:00
|
|
|
// Validator for AWS TPM attestation.
|
2022-03-22 11:03:15 -04:00
|
|
|
type Validator struct {
|
|
|
|
oid.AWS
|
2022-10-27 05:04:23 -04:00
|
|
|
*vtpm.Validator
|
2022-11-02 10:19:13 -04:00
|
|
|
getDescribeClient func(context.Context, string) (awsMetadataAPI, error)
|
2022-10-27 05:04:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewValidator create a new Validator structure and returns it.
|
2022-11-24 04:57:58 -05:00
|
|
|
func NewValidator(pcrs measurements.M, log vtpm.AttestationLogger) *Validator {
|
2022-10-27 05:04:23 -04:00
|
|
|
v := &Validator{}
|
|
|
|
v.Validator = vtpm.NewValidator(
|
|
|
|
pcrs,
|
|
|
|
getTrustedKey,
|
|
|
|
v.tpmEnabled,
|
|
|
|
vtpm.VerifyPKCS1v15,
|
|
|
|
log,
|
|
|
|
)
|
|
|
|
v.getDescribeClient = getEC2Client
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
// getTrustedKeys return the public area of the provides attestation key.
|
|
|
|
// Normally, here the trust of this key should be verified, but currently AWS does not provide this feature.
|
|
|
|
func getTrustedKey(akPub []byte, instanceInfo []byte) (crypto.PublicKey, error) {
|
|
|
|
// Copied from https://github.com/edgelesssys/constellation/blob/main/internal/attestation/qemu/validator.go
|
|
|
|
pubArea, err := tpm2.DecodePublic(akPub)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pubArea.Key()
|
|
|
|
}
|
|
|
|
|
|
|
|
// tpmEnabled verifies if the virtual machine has the tpm2.0 feature enabled.
|
|
|
|
func (v *Validator) tpmEnabled(attestation vtpm.AttestationDocument) error {
|
|
|
|
// https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/verify-nitrotpm-support-on-ami.html
|
|
|
|
// 1. Get the vm's ami (from IdentiTyDocument.imageId)
|
|
|
|
// 2. Check the value of key "TpmSupport": {"Value": "v2.0"}"
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
idDocument := imds.InstanceIdentityDocument{}
|
2022-11-02 10:19:13 -04:00
|
|
|
err := json.Unmarshal(attestation.InstanceInfo, &idDocument)
|
2022-10-27 05:04:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
imageID := idDocument.ImageID
|
|
|
|
|
2022-11-02 10:19:13 -04:00
|
|
|
client, err := v.getDescribeClient(ctx, idDocument.Region)
|
2022-10-27 05:04:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Currently, there seems to be a problem with retrieving image attributes directly.
|
|
|
|
// Alternatively, parse it from the general output.
|
|
|
|
imageOutput, err := client.DescribeImages(ctx, &ec2.DescribeImagesInput{ImageIds: []string{imageID}})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if imageOutput.Images[0].TpmSupport == "v2.0" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("iam image %s does not support TPM v2.0", imageID)
|
|
|
|
}
|
|
|
|
|
2022-11-02 10:19:13 -04:00
|
|
|
func getEC2Client(ctx context.Context, region string) (awsMetadataAPI, error) {
|
2022-11-15 09:40:49 -05:00
|
|
|
client, err := awsConfig.LoadDefaultConfig(ctx, awsConfig.WithRegion(region))
|
2022-10-27 05:04:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ec2.NewFromConfig(client), nil
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-10-27 05:04:23 -04:00
|
|
|
type awsMetadataAPI interface {
|
|
|
|
DescribeImages(ctx context.Context, params *ec2.DescribeImagesInput, optFns ...func(*ec2.Options)) (*ec2.DescribeImagesOutput, error)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|