2022-08-31 14:10:49 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package snp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/vtpm"
|
2023-03-29 03:30:13 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/variant"
|
2023-03-21 07:46:49 -04:00
|
|
|
"github.com/edgelesssys/go-azguestattestation/maa"
|
2022-08-31 14:10:49 -04:00
|
|
|
tpmclient "github.com/google/go-tpm-tools/client"
|
|
|
|
)
|
|
|
|
|
2023-03-21 07:46:49 -04:00
|
|
|
const tpmAkIdx = 0x81000003
|
2022-08-31 14:10:49 -04:00
|
|
|
|
|
|
|
// Issuer for Azure TPM attestation.
|
|
|
|
type Issuer struct {
|
2023-03-29 03:30:13 -04:00
|
|
|
variant.AzureSEVSNP
|
2022-08-31 14:10:49 -04:00
|
|
|
*vtpm.Issuer
|
2023-03-21 07:46:49 -04:00
|
|
|
|
|
|
|
imds imdsAPI
|
|
|
|
maa maaTokenCreator
|
2022-08-31 14:10:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewIssuer initializes a new Azure Issuer.
|
2023-02-28 10:34:18 -05:00
|
|
|
func NewIssuer(log vtpm.AttestationLogger) *Issuer {
|
2023-03-21 07:46:49 -04:00
|
|
|
i := &Issuer{
|
|
|
|
imds: newIMDSClient(),
|
|
|
|
maa: newMAAClient(),
|
2022-08-31 14:10:49 -04:00
|
|
|
}
|
|
|
|
|
2023-03-21 07:46:49 -04:00
|
|
|
i.Issuer = vtpm.NewIssuer(
|
|
|
|
vtpm.OpenVTPM,
|
|
|
|
getAttestationKey,
|
|
|
|
i.getInstanceInfo,
|
|
|
|
log,
|
|
|
|
)
|
|
|
|
return i
|
2022-08-31 14:10:49 -04:00
|
|
|
}
|
|
|
|
|
2023-03-21 07:46:49 -04:00
|
|
|
func (i *Issuer) getInstanceInfo(ctx context.Context, tpm io.ReadWriteCloser, userData []byte) ([]byte, error) {
|
|
|
|
params, err := i.maa.newParameters(ctx, userData, tpm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting system parameters: %w", err)
|
|
|
|
}
|
2022-08-31 14:10:49 -04:00
|
|
|
|
2023-03-21 07:46:49 -04:00
|
|
|
var maaToken string
|
2022-08-31 14:10:49 -04:00
|
|
|
|
2023-03-21 07:46:49 -04:00
|
|
|
maaURL, err := i.imds.getMAAURL(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("retrieving MAA URL from IMDS API: %w", err)
|
|
|
|
}
|
|
|
|
if maaURL != "" {
|
|
|
|
maaToken, err = i.maa.createToken(ctx, tpm, maaURL, userData, params)
|
2022-08-31 14:10:49 -04:00
|
|
|
if err != nil {
|
2023-03-21 07:46:49 -04:00
|
|
|
return nil, fmt.Errorf("creating MAA token: %w", err)
|
2022-08-31 14:10:49 -04:00
|
|
|
}
|
2023-03-21 07:46:49 -04:00
|
|
|
}
|
2022-08-31 14:10:49 -04:00
|
|
|
|
2023-03-21 07:46:49 -04:00
|
|
|
instanceInfo := azureInstanceInfo{
|
|
|
|
Vcek: params.VcekCert,
|
|
|
|
CertChain: params.VcekChain,
|
|
|
|
AttestationReport: params.SNPReport,
|
|
|
|
RuntimeData: params.RuntimeData,
|
|
|
|
MAAToken: maaToken,
|
2022-08-31 14:10:49 -04:00
|
|
|
}
|
2023-03-21 07:46:49 -04:00
|
|
|
statement, err := json.Marshal(instanceInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("marshalling AzureInstanceInfo: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return statement, nil
|
2022-08-31 14:10:49 -04:00
|
|
|
}
|
|
|
|
|
2022-10-27 05:04:23 -04:00
|
|
|
// getAttestationKey reads the attestation key put into the TPM during early boot.
|
2022-08-31 14:10:49 -04:00
|
|
|
func getAttestationKey(tpm io.ReadWriter) (*tpmclient.Key, error) {
|
2022-10-04 10:44:44 -04:00
|
|
|
ak, err := tpmclient.LoadCachedKey(tpm, tpmAkIdx)
|
2022-08-31 14:10:49 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("reading HCL attestation key from TPM: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ak, nil
|
|
|
|
}
|
|
|
|
|
2023-03-21 07:46:49 -04:00
|
|
|
type imdsAPI interface {
|
|
|
|
getMAAURL(ctx context.Context) (string, error)
|
2022-08-31 14:10:49 -04:00
|
|
|
}
|
|
|
|
|
2023-03-21 07:46:49 -04:00
|
|
|
type maaTokenCreator interface {
|
|
|
|
newParameters(context.Context, []byte, io.ReadWriter) (maa.Parameters, error)
|
|
|
|
createToken(context.Context, io.ReadWriter, string, []byte, maa.Parameters) (string, error)
|
2022-08-31 14:10:49 -04:00
|
|
|
}
|