2023-10-30 07:31:42 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Package SNP provides types shared by SNP-based attestation implementations.
|
|
|
|
// It ensures all issuers provide the same types to the verify command.
|
|
|
|
package snp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2024-04-29 08:38:34 -04:00
|
|
|
"errors"
|
2023-10-30 07:31:42 -04:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation"
|
|
|
|
"github.com/google/go-sev-guest/abi"
|
2024-05-16 09:48:44 -04:00
|
|
|
"github.com/google/go-sev-guest/client"
|
2023-10-30 07:31:42 -04:00
|
|
|
"github.com/google/go-sev-guest/kds"
|
|
|
|
spb "github.com/google/go-sev-guest/proto/sevsnp"
|
|
|
|
"github.com/google/go-sev-guest/verify/trust"
|
2024-04-16 12:13:47 -04:00
|
|
|
"github.com/google/go-tpm-tools/proto/attest"
|
2023-10-30 07:31:42 -04:00
|
|
|
)
|
|
|
|
|
2024-04-29 08:38:34 -04:00
|
|
|
var errNoPemBlocks = errors.New("no PEM blocks found")
|
|
|
|
|
2023-11-07 06:17:08 -05:00
|
|
|
// Product returns the SEV product info currently supported by Constellation's SNP attestation.
|
|
|
|
func Product() *spb.SevProduct {
|
|
|
|
// sevProduct is the product info of the SEV platform as reported through CPUID[EAX=1].
|
|
|
|
// It may become necessary in the future to differentiate among CSP vendors.
|
|
|
|
return &spb.SevProduct{Name: spb.SevProduct_SEV_PRODUCT_MILAN, Stepping: 0} // Milan-B0
|
|
|
|
}
|
|
|
|
|
2024-05-16 09:48:44 -04:00
|
|
|
// GetExtendedReport retrieves the extended SNP report from the CVM.
|
|
|
|
func GetExtendedReport(reportData [64]byte) (report, certChain []byte, err error) {
|
|
|
|
qp, err := client.GetLeveledQuoteProvider()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("getting quote provider: %w", err)
|
|
|
|
}
|
|
|
|
quote, err := qp.GetRawQuoteAtLevel(reportData, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("getting extended report: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the report and certificate chain from the quote.
|
|
|
|
report = quote
|
|
|
|
if len(quote) > abi.ReportSize {
|
|
|
|
report = quote[:abi.ReportSize]
|
|
|
|
certChain = quote[abi.ReportSize:]
|
|
|
|
}
|
|
|
|
return report, certChain, nil
|
|
|
|
}
|
|
|
|
|
2023-11-07 09:19:31 -05:00
|
|
|
// InstanceInfo contains the necessary information to establish trust in a SNP CVM.
|
2023-10-30 07:31:42 -04:00
|
|
|
type InstanceInfo struct {
|
2023-11-06 08:22:44 -05:00
|
|
|
// ReportSigner is the PEM-encoded certificate used to validate the attestation report's signature.
|
2023-11-07 09:19:31 -05:00
|
|
|
ReportSigner []byte
|
|
|
|
// CertChain is the PEM-encoded certificate chain for the attestation report (ASK+ARK).
|
|
|
|
// Intermediate key that validates the ReportSigner and root key.
|
2023-10-30 07:31:42 -04:00
|
|
|
CertChain []byte
|
|
|
|
// AttestationReport is the attestation report from the vTPM (NVRAM) of the CVM.
|
|
|
|
AttestationReport []byte
|
2023-11-07 09:19:31 -05:00
|
|
|
Azure *AzureInstanceInfo
|
2024-04-16 12:13:47 -04:00
|
|
|
GCP *attest.GCEInstanceInfo
|
2023-11-07 09:19:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// AzureInstanceInfo contains Azure specific information related to SNP attestation.
|
|
|
|
type AzureInstanceInfo struct {
|
2023-10-30 07:31:42 -04:00
|
|
|
// RuntimeData is the Azure runtime data from the vTPM (NVRAM) of the CVM.
|
|
|
|
RuntimeData []byte
|
|
|
|
// MAAToken is the token of the MAA for the attestation report, used as a fallback
|
|
|
|
// if the IDKeyDigest cannot be verified.
|
|
|
|
MAAToken string
|
|
|
|
}
|
|
|
|
|
2023-11-06 08:22:44 -05:00
|
|
|
// addReportSigner parses the reportSigner certificate (VCEK/VLEK) from a and adds it to the attestation proto att.
|
|
|
|
// If reportSigner is empty and a VLEK is required, an error is returned.
|
|
|
|
// If reportSigner is empty and a VCEK is required, the VCEK is retrieved from AMD KDS.
|
|
|
|
func (a *InstanceInfo) addReportSigner(att *spb.Attestation, report *spb.Report, productName string, getter trust.HTTPSGetter, logger attestation.Logger) (abi.ReportSigner, error) {
|
|
|
|
// If the VCEK certificate is present, parse it and format it.
|
|
|
|
reportSigner, err := a.ParseReportSigner()
|
|
|
|
if err != nil {
|
2024-02-08 09:20:01 -05:00
|
|
|
logger.Warn(fmt.Sprintf("Error parsing report signer: %v", err))
|
2023-11-06 08:22:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
signerInfo, err := abi.ParseSignerInfo(report.GetSignerInfo())
|
|
|
|
if err != nil {
|
|
|
|
return abi.NoneReportSigner, fmt.Errorf("parsing signer info: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch signerInfo.SigningKey {
|
|
|
|
case abi.VlekReportSigner:
|
|
|
|
if reportSigner == nil {
|
|
|
|
return abi.NoneReportSigner, fmt.Errorf("VLEK certificate required but not present")
|
|
|
|
}
|
|
|
|
att.CertificateChain.VlekCert = reportSigner.Raw
|
|
|
|
|
|
|
|
case abi.VcekReportSigner:
|
|
|
|
var vcekData []byte
|
|
|
|
|
|
|
|
// If no VCEK is present, fetch it from AMD.
|
|
|
|
if reportSigner == nil {
|
2024-02-08 09:20:01 -05:00
|
|
|
logger.Info("VCEK certificate not present, falling back to retrieving it from AMD KDS")
|
2023-11-06 08:22:44 -05:00
|
|
|
vcekURL := kds.VCEKCertURL(productName, report.GetChipId(), kds.TCBVersion(report.GetReportedTcb()))
|
|
|
|
vcekData, err = getter.Get(vcekURL)
|
|
|
|
if err != nil {
|
|
|
|
return abi.NoneReportSigner, fmt.Errorf("retrieving VCEK certificate from AMD KDS: %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vcekData = reportSigner.Raw
|
|
|
|
}
|
|
|
|
|
|
|
|
att.CertificateChain.VcekCert = vcekData
|
|
|
|
}
|
|
|
|
|
|
|
|
return signerInfo.SigningKey, nil
|
|
|
|
}
|
|
|
|
|
2023-10-30 07:31:42 -04:00
|
|
|
// AttestationWithCerts returns a formatted version of the attestation report and its certificates from the instanceInfo.
|
|
|
|
// Certificates are retrieved in the following precedence:
|
2024-04-16 12:13:47 -04:00
|
|
|
// 1. ASK from issuer. On Azure: THIM. One AWS: not prefilled. (Go to option 2) On GCP: prefilled.
|
2023-10-30 07:31:42 -04:00
|
|
|
// 2. ASK or ARK from fallbackCerts.
|
|
|
|
// 3. ASK or ARK from AMD KDS.
|
2023-11-06 08:22:44 -05:00
|
|
|
func (a *InstanceInfo) AttestationWithCerts(getter trust.HTTPSGetter,
|
|
|
|
fallbackCerts CertificateChain, logger attestation.Logger,
|
2023-10-30 07:31:42 -04:00
|
|
|
) (*spb.Attestation, error) {
|
|
|
|
report, err := abi.ReportToProto(a.AttestationReport)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("converting report to proto: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-05-16 09:48:44 -04:00
|
|
|
productName := kds.ProductLine(Product())
|
2023-10-30 07:31:42 -04:00
|
|
|
|
|
|
|
att := &spb.Attestation{
|
|
|
|
Report: report,
|
|
|
|
CertificateChain: &spb.CertificateChain{},
|
2023-11-07 06:17:08 -05:00
|
|
|
Product: Product(),
|
2023-10-30 07:31:42 -04:00
|
|
|
}
|
|
|
|
|
2023-11-06 08:22:44 -05:00
|
|
|
// Add VCEK/VLEK to attestation object.
|
|
|
|
signingInfo, err := a.addReportSigner(att, report, productName, getter, logger)
|
2023-10-30 07:31:42 -04:00
|
|
|
if err != nil {
|
2023-11-06 08:22:44 -05:00
|
|
|
return nil, fmt.Errorf("adding report signer: %w", err)
|
2023-10-30 07:31:42 -04:00
|
|
|
}
|
|
|
|
|
2024-04-16 12:13:47 -04:00
|
|
|
// If a certificate chain was pre-fetched by the Issuer, parse it and format it.
|
|
|
|
// Make sure to only use the ask, since using an ark from the Issuer would invalidate security guarantees.
|
|
|
|
ask, _, err := a.ParseCertChain()
|
2024-04-29 08:38:34 -04:00
|
|
|
if err != nil && !errors.Is(err, errNoPemBlocks) {
|
2024-02-08 09:20:01 -05:00
|
|
|
logger.Warn(fmt.Sprintf("Error parsing certificate chain: %v", err))
|
2023-10-30 07:31:42 -04:00
|
|
|
}
|
|
|
|
if ask != nil {
|
2024-04-16 12:13:47 -04:00
|
|
|
logger.Info("Using ASK certificate from pre-fetched certificate chain")
|
2023-10-30 07:31:42 -04:00
|
|
|
att.CertificateChain.AskCert = ask.Raw
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a cached ASK or an ARK from the Constellation config is present, use it.
|
|
|
|
if att.CertificateChain.AskCert == nil && fallbackCerts.ask != nil {
|
2024-02-08 09:20:01 -05:00
|
|
|
logger.Info("Using cached ASK certificate")
|
2023-10-30 07:31:42 -04:00
|
|
|
att.CertificateChain.AskCert = fallbackCerts.ask.Raw
|
|
|
|
}
|
2024-04-16 12:13:47 -04:00
|
|
|
if fallbackCerts.ark != nil {
|
|
|
|
logger.Info("Using cached ARK certificate")
|
2023-10-30 07:31:42 -04:00
|
|
|
att.CertificateChain.ArkCert = fallbackCerts.ark.Raw
|
|
|
|
}
|
2024-04-16 12:13:47 -04:00
|
|
|
|
|
|
|
// Otherwise, retrieve missing certificates from AMD KDS.
|
2023-10-30 07:31:42 -04:00
|
|
|
if att.CertificateChain.AskCert == nil || att.CertificateChain.ArkCert == nil {
|
2024-02-08 09:20:01 -05:00
|
|
|
logger.Info(fmt.Sprintf(
|
2023-10-30 07:31:42 -04:00
|
|
|
"Certificate chain not fully present (ARK present: %t, ASK present: %t), falling back to retrieving it from AMD KDS",
|
|
|
|
(att.CertificateChain.ArkCert != nil),
|
|
|
|
(att.CertificateChain.AskCert != nil),
|
2024-02-08 09:20:01 -05:00
|
|
|
))
|
2023-11-06 08:22:44 -05:00
|
|
|
kdsCertChain, err := trust.GetProductChain(productName, signingInfo, getter)
|
2023-10-30 07:31:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("retrieving certificate chain from AMD KDS: %w", err)
|
|
|
|
}
|
|
|
|
if att.CertificateChain.AskCert == nil && kdsCertChain.Ask != nil {
|
2024-02-08 09:20:01 -05:00
|
|
|
logger.Info("Using ASK certificate from AMD KDS")
|
2023-10-30 07:31:42 -04:00
|
|
|
att.CertificateChain.AskCert = kdsCertChain.Ask.Raw
|
|
|
|
}
|
|
|
|
if att.CertificateChain.ArkCert == nil && kdsCertChain.Ask != nil {
|
2024-02-08 09:20:01 -05:00
|
|
|
logger.Info("Using ARK certificate from AMD KDS")
|
2023-10-30 07:31:42 -04:00
|
|
|
att.CertificateChain.ArkCert = kdsCertChain.Ark.Raw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return att, nil
|
|
|
|
}
|
|
|
|
|
2023-11-07 09:19:31 -05:00
|
|
|
// CertificateChain stores an AMD signing key (ASK) and AMD root key (ARK) certificate.
|
2023-10-30 07:31:42 -04:00
|
|
|
type CertificateChain struct {
|
|
|
|
ask *x509.Certificate
|
|
|
|
ark *x509.Certificate
|
|
|
|
}
|
|
|
|
|
2023-11-07 09:19:31 -05:00
|
|
|
// NewCertificateChain returns a new CertificateChain with the given ASK and ARK certificates.
|
2023-10-30 07:31:42 -04:00
|
|
|
func NewCertificateChain(ask, ark *x509.Certificate) CertificateChain {
|
|
|
|
return CertificateChain{
|
|
|
|
ask: ask,
|
|
|
|
ark: ark,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseCertChain parses the certificate chain from the instanceInfo into x509-formatted ASK and ARK certificates.
|
|
|
|
// If less than 2 certificates are present, only the present certificate is returned.
|
|
|
|
// If more than 2 certificates are present, an error is returned.
|
|
|
|
func (a *InstanceInfo) ParseCertChain() (ask, ark *x509.Certificate, retErr error) {
|
|
|
|
rest := bytes.TrimSpace(a.CertChain)
|
|
|
|
|
|
|
|
i := 1
|
|
|
|
var block *pem.Block
|
|
|
|
for block, rest = pem.Decode(rest); block != nil; block, rest = pem.Decode(rest) {
|
|
|
|
if i > 2 {
|
|
|
|
retErr = fmt.Errorf("parse certificate %d: more than 2 certificates in chain", i)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if block.Type != "CERTIFICATE" {
|
|
|
|
retErr = fmt.Errorf("parse certificate %d: expected PEM block type 'CERTIFICATE', got '%s'", i, block.Type)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := x509.ParseCertificate(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
retErr = fmt.Errorf("parse certificate %d: %w", i, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://www.amd.com/content/dam/amd/en/documents/epyc-technical-docs/specifications/57230.pdf
|
|
|
|
// Table 6 and 7
|
|
|
|
switch cert.Subject.CommonName {
|
2023-11-06 08:22:44 -05:00
|
|
|
case "SEV-Milan", "SEV-VLEK-Milan":
|
2023-10-30 07:31:42 -04:00
|
|
|
ask = cert
|
|
|
|
case "ARK-Milan":
|
|
|
|
ark = cert
|
|
|
|
default:
|
|
|
|
retErr = fmt.Errorf("parse certificate %d: unexpected subject CN %s", i, cert.Subject.CommonName)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case i == 1:
|
2024-04-29 08:38:34 -04:00
|
|
|
retErr = errNoPemBlocks
|
2023-10-30 07:31:42 -04:00
|
|
|
case len(rest) != 0:
|
|
|
|
retErr = fmt.Errorf("remaining PEM block is not a valid certificate: %s", rest)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-06 08:22:44 -05:00
|
|
|
// ParseReportSigner parses the VCEK/VLEK certificate from the instanceInfo into an x509-formatted certificate.
|
|
|
|
// If no certificate is present, nil is returned.
|
|
|
|
func (a *InstanceInfo) ParseReportSigner() (*x509.Certificate, error) {
|
2023-11-07 09:19:31 -05:00
|
|
|
newlinesTrimmed := bytes.TrimSpace(a.ReportSigner)
|
2023-10-30 07:31:42 -04:00
|
|
|
if len(newlinesTrimmed) == 0 {
|
|
|
|
// VCEK is not present.
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
block, rest := pem.Decode(newlinesTrimmed)
|
|
|
|
if block == nil {
|
|
|
|
return nil, fmt.Errorf("no PEM blocks found")
|
|
|
|
}
|
|
|
|
if len(rest) != 0 {
|
|
|
|
return nil, fmt.Errorf("received more data than expected")
|
|
|
|
}
|
|
|
|
if block.Type != "CERTIFICATE" {
|
|
|
|
return nil, fmt.Errorf("expected PEM block type 'CERTIFICATE', got '%s'", block.Type)
|
|
|
|
}
|
|
|
|
|
2023-11-06 08:22:44 -05:00
|
|
|
reportSigner, err := x509.ParseCertificate(block.Bytes)
|
2023-10-30 07:31:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parsing VCEK certificate: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-11-06 08:22:44 -05:00
|
|
|
return reportSigner, nil
|
2023-10-30 07:31:42 -04:00
|
|
|
}
|