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
|
|
|
|
|
2022-10-27 05:04:23 -04:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"log"
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-10-27 05:04:23 -04:00
|
|
|
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/vtpm"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/oid"
|
|
|
|
|
|
|
|
"github.com/google/go-tpm-tools/client"
|
|
|
|
tpmclient "github.com/google/go-tpm-tools/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Issuer for AWS TPM attestation.
|
2022-03-22 11:03:15 -04:00
|
|
|
type Issuer struct {
|
|
|
|
oid.AWS
|
2022-10-27 05:04:23 -04:00
|
|
|
*vtpm.Issuer
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIssuer() *Issuer {
|
|
|
|
return &Issuer{
|
|
|
|
Issuer: vtpm.NewIssuer(
|
|
|
|
vtpm.OpenVTPM,
|
|
|
|
getAttestationKey,
|
|
|
|
getInstanceInfo(imds.New(imds.Options{})),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getAttestationKey returns a new attestation key.
|
|
|
|
func getAttestationKey(tpm io.ReadWriter) (*tpmclient.Key, error) {
|
|
|
|
tpmAk, err := client.AttestationKeyRSA(tpm)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error creating RSA Endorsement key!")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tpmAk, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getInstanceInfo returns information about the current instance using the aws Metadata SDK.
|
|
|
|
// The returned bytes will be written into the attestation document.
|
|
|
|
func getInstanceInfo(client awsMetaData) func(tpm io.ReadWriteCloser) ([]byte, error) {
|
|
|
|
return func(io.ReadWriteCloser) ([]byte, error) {
|
|
|
|
ec2InstanceIdentityOutput, err := client.GetInstanceIdentityDocument(context.Background(), &imds.GetInstanceIdentityDocumentInput{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("unable to fetch instance identity document")
|
|
|
|
}
|
|
|
|
return json.Marshal(ec2InstanceIdentityOutput.InstanceIdentityDocument)
|
|
|
|
}
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-10-27 05:04:23 -04:00
|
|
|
type awsMetaData interface {
|
|
|
|
GetInstanceIdentityDocument(context.Context, *imds.GetInstanceIdentityDocumentInput, ...func(*imds.Options)) (*imds.GetInstanceIdentityDocumentOutput, error)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|