2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-08-30 06:28:15 -04:00
|
|
|
// Verify verifies an MAA JWT and prints the SNP ID key digest on success.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-09-12 08:39:31 -04:00
|
|
|
"context"
|
2022-08-30 06:28:15 -04:00
|
|
|
"crypto/x509"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2023-06-01 07:55:46 -04:00
|
|
|
"flag"
|
2022-08-30 06:28:15 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2023-06-07 10:16:32 -04:00
|
|
|
configapi "github.com/edgelesssys/constellation/v2/internal/api/attestationconfigapi"
|
2022-08-30 06:28:15 -04:00
|
|
|
"gopkg.in/square/go-jose.v2"
|
|
|
|
"gopkg.in/square/go-jose.v2/jwt"
|
|
|
|
)
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// IsolationTEE describes an Azure SNP TEE.
|
2022-09-21 04:47:38 -04:00
|
|
|
type IsolationTEE struct {
|
|
|
|
IDKeyDigest string `json:"x-ms-sevsnpvm-idkeydigest"`
|
|
|
|
TEESvn int `json:"x-ms-sevsnpvm-tee-svn"`
|
|
|
|
SNPFwSvn int `json:"x-ms-sevsnpvm-snpfw-svn"`
|
|
|
|
MicrocodeSvn int `json:"x-ms-sevsnpvm-microcode-svn"`
|
|
|
|
BootloaderSvn int `json:"x-ms-sevsnpvm-bootloader-svn"`
|
|
|
|
GuestSvn int `json:"x-ms-sevsnpvm-guestsvn"`
|
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// PrintSVNs prints the relevant Security Version Numbers (SVNs).
|
2022-09-21 04:47:38 -04:00
|
|
|
func (i *IsolationTEE) PrintSVNs() {
|
|
|
|
fmt.Println("\tTEE SVN:", i.TEESvn)
|
|
|
|
fmt.Println("\tSNP FW SVN:", i.SNPFwSvn)
|
|
|
|
fmt.Println("\tMicrocode SVN:", i.MicrocodeSvn)
|
|
|
|
fmt.Println("\tBootloader SVN:", i.BootloaderSvn)
|
|
|
|
fmt.Println("\tGuest SVN:", i.GuestSvn)
|
|
|
|
}
|
|
|
|
|
2022-08-30 06:28:15 -04:00
|
|
|
func main() {
|
2023-06-01 07:55:46 -04:00
|
|
|
configAPIExportPath := flag.String("export-path", "azure-sev-snp-version.json", "Path to the exported config API file.")
|
2023-06-02 06:10:22 -04:00
|
|
|
maaJWT := flag.String("report", "", "MAA JWT report to verify")
|
2023-06-01 07:55:46 -04:00
|
|
|
flag.Parse()
|
2023-06-02 06:10:22 -04:00
|
|
|
if *maaJWT == "" {
|
|
|
|
fmt.Println("Must provide --report")
|
2022-08-30 06:28:15 -04:00
|
|
|
return
|
|
|
|
}
|
2023-06-02 06:10:22 -04:00
|
|
|
report, err := getTEEReport(*maaJWT)
|
2022-08-30 06:28:15 -04:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-09-21 04:47:38 -04:00
|
|
|
fmt.Println("Successfully validated ID key digest:", report.IDKeyDigest)
|
|
|
|
fmt.Println("Currently reported SVNs:")
|
|
|
|
report.PrintSVNs()
|
2023-06-01 07:55:46 -04:00
|
|
|
|
|
|
|
if *configAPIExportPath != "" {
|
|
|
|
configAPIVersion := convertToConfigAPIFile(report)
|
|
|
|
if err := exportToJSONFile(configAPIVersion, *configAPIExportPath); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fmt.Println("Successfully exported config API file to:", configAPIExportPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertToConfigAPIFile(i IsolationTEE) configapi.AzureSEVSNPVersion {
|
|
|
|
return configapi.AzureSEVSNPVersion{
|
|
|
|
Bootloader: uint8(i.BootloaderSvn),
|
|
|
|
TEE: uint8(i.TEESvn),
|
|
|
|
SNP: uint8(i.SNPFwSvn),
|
|
|
|
Microcode: uint8(i.MicrocodeSvn),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func exportToJSONFile(configAPIVersion configapi.AzureSEVSNPVersion, configAPIExportPath string) error {
|
|
|
|
data, err := json.Marshal(configAPIVersion)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.WriteFile(configAPIExportPath, data, 0o644)
|
2022-08-30 06:28:15 -04:00
|
|
|
}
|
|
|
|
|
2022-09-21 04:47:38 -04:00
|
|
|
func getTEEReport(rawToken string) (IsolationTEE, error) {
|
2022-08-30 06:28:15 -04:00
|
|
|
// Parse token.
|
|
|
|
token, err := jwt.ParseSigned(rawToken)
|
|
|
|
if err != nil {
|
2022-09-21 04:47:38 -04:00
|
|
|
return IsolationTEE{}, err
|
2022-08-30 06:28:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get JSON Web Key set.
|
2022-09-12 08:39:31 -04:00
|
|
|
keySetBytes, err := httpGet(context.Background(), "https://sharedeus.eus.attest.azure.net/certs")
|
2022-08-30 06:28:15 -04:00
|
|
|
if err != nil {
|
2022-09-21 04:47:38 -04:00
|
|
|
return IsolationTEE{}, err
|
2022-08-30 06:28:15 -04:00
|
|
|
}
|
|
|
|
keySet, err := parseKeySet(keySetBytes)
|
|
|
|
if err != nil {
|
2022-09-21 04:47:38 -04:00
|
|
|
return IsolationTEE{}, err
|
2022-08-30 06:28:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get claims. Private claims contain ID Key digest.
|
|
|
|
|
|
|
|
var publicClaims jwt.Claims
|
|
|
|
|
|
|
|
var privateClaims struct {
|
2022-09-21 04:47:38 -04:00
|
|
|
IsolationTEE IsolationTEE `json:"x-ms-isolation-tee"`
|
2022-08-30 06:28:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := token.Claims(&keySet, &publicClaims, &privateClaims); err != nil {
|
2022-09-21 04:47:38 -04:00
|
|
|
return IsolationTEE{}, err
|
2022-08-30 06:28:15 -04:00
|
|
|
}
|
|
|
|
if err := publicClaims.Validate(jwt.Expected{Time: time.Now()}); err != nil {
|
2022-09-21 04:47:38 -04:00
|
|
|
return IsolationTEE{}, err
|
2022-08-30 06:28:15 -04:00
|
|
|
}
|
|
|
|
|
2022-09-21 04:47:38 -04:00
|
|
|
return privateClaims.IsolationTEE, nil
|
2022-08-30 06:28:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseKeySet(keySetBytes []byte) (jose.JSONWebKeySet, error) {
|
|
|
|
var rawKeySet struct {
|
|
|
|
Keys []struct {
|
|
|
|
X5c []string
|
|
|
|
Kid string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(keySetBytes, &rawKeySet); err != nil {
|
|
|
|
return jose.JSONWebKeySet{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var keySet jose.JSONWebKeySet
|
|
|
|
for _, key := range rawKeySet.Keys {
|
|
|
|
rawCert, _ := base64.StdEncoding.DecodeString(key.X5c[0])
|
|
|
|
cert, err := x509.ParseCertificate(rawCert)
|
|
|
|
if err != nil {
|
|
|
|
return jose.JSONWebKeySet{}, err
|
|
|
|
}
|
|
|
|
keySet.Keys = append(keySet.Keys, jose.JSONWebKey{KeyID: key.Kid, Key: cert.PublicKey})
|
|
|
|
}
|
|
|
|
|
|
|
|
return keySet, nil
|
|
|
|
}
|
|
|
|
|
2022-09-12 08:39:31 -04:00
|
|
|
func httpGet(ctx context.Context, url string) ([]byte, error) {
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
2022-08-30 06:28:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, errors.New(resp.Status)
|
|
|
|
}
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return body, nil
|
|
|
|
}
|