2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-06-09 09:41:02 -04:00
|
|
|
package nitrotpm
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-10-27 05:04:23 -04:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2023-06-09 09:41:02 -04:00
|
|
|
"fmt"
|
2022-10-27 05:04:23 -04:00
|
|
|
"io"
|
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"
|
2023-03-08 08:13:57 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation"
|
2023-06-09 09:41:02 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/variant"
|
2022-10-27 05:04:23 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/vtpm"
|
|
|
|
|
|
|
|
"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 {
|
2023-03-29 03:30:13 -04:00
|
|
|
variant.AWSNitroTPM
|
2022-10-27 05:04:23 -04:00
|
|
|
*vtpm.Issuer
|
|
|
|
}
|
|
|
|
|
2023-06-09 09:41:02 -04:00
|
|
|
// NewIssuer creates a TPM based issuer for AWS.
|
2023-03-08 08:13:57 -05:00
|
|
|
func NewIssuer(log attestation.Logger) *Issuer {
|
2022-10-27 05:04:23 -04:00
|
|
|
return &Issuer{
|
|
|
|
Issuer: vtpm.NewIssuer(
|
|
|
|
vtpm.OpenVTPM,
|
|
|
|
getAttestationKey,
|
|
|
|
getInstanceInfo(imds.New(imds.Options{})),
|
2023-02-28 10:34:18 -05:00
|
|
|
log,
|
2022-10-27 05:04:23 -04:00
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getAttestationKey returns a new attestation key.
|
|
|
|
func getAttestationKey(tpm io.ReadWriter) (*tpmclient.Key, error) {
|
|
|
|
tpmAk, err := client.AttestationKeyRSA(tpm)
|
|
|
|
if err != nil {
|
2023-06-09 09:41:02 -04:00
|
|
|
return nil, fmt.Errorf("error creating RSA Endorsement key: %w", err)
|
2022-10-27 05:04:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2023-03-21 07:46:49 -04:00
|
|
|
func getInstanceInfo(client awsMetaData) func(context.Context, io.ReadWriteCloser, []byte) ([]byte, error) {
|
2023-06-09 09:41:02 -04:00
|
|
|
return func(ctx context.Context, _ io.ReadWriteCloser, _ []byte) ([]byte, error) {
|
|
|
|
ec2InstanceIdentityOutput, err := client.GetInstanceIdentityDocument(ctx, &imds.GetInstanceIdentityDocumentInput{})
|
2022-10-27 05:04:23 -04:00
|
|
|
if err != nil {
|
2023-06-09 09:41:02 -04:00
|
|
|
return nil, fmt.Errorf("fetching instance identity document: %w", err)
|
2022-10-27 05:04:23 -04:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|