2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-23 09:10:58 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
2022-06-28 11:03:28 -04:00
|
|
|
"strconv"
|
2022-03-23 09:10:58 -04:00
|
|
|
"time"
|
|
|
|
|
2022-11-15 09:40:49 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/vtpm"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/crypto"
|
|
|
|
"github.com/edgelesssys/constellation/v2/verify/verifyproto"
|
2022-03-23 09:10:58 -04:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
"google.golang.org/grpc"
|
2022-06-28 11:03:28 -04:00
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2022-06-20 07:57:25 -04:00
|
|
|
"gopkg.in/yaml.v3"
|
2022-03-23 09:10:58 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-06-29 09:26:29 -04:00
|
|
|
coordIP = flag.String("constell-ip", "", "Public IP of the Constellation")
|
|
|
|
port = flag.String("constell-port", strconv.Itoa(constants.VerifyServiceNodePortGRPC), "NodePort of the Constellation's verification service")
|
|
|
|
export = flag.String("o", "", "Write PCRs, formatted as Go code, to file")
|
|
|
|
format = flag.String("format", "json", "Output format: json, yaml (default json)")
|
|
|
|
quiet = flag.Bool("q", false, "Set to disable output")
|
|
|
|
timeout = flag.Duration("timeout", 2*time.Minute, "Wait this duration for the verification service to become available")
|
2022-03-23 09:10:58 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
2022-06-29 09:26:29 -04:00
|
|
|
addr := net.JoinHostPort(*coordIP, *port)
|
2022-04-26 10:38:14 -04:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), *timeout)
|
2022-03-23 09:10:58 -04:00
|
|
|
defer cancel()
|
|
|
|
|
2022-06-28 11:03:28 -04:00
|
|
|
attDocRaw, err := getAttestation(ctx, addr)
|
2022-03-23 09:10:58 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-03-29 03:10:22 -04:00
|
|
|
pcrs, err := validatePCRAttDoc(attDocRaw)
|
2022-03-23 09:10:58 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !*quiet {
|
2022-06-20 07:57:25 -04:00
|
|
|
if err := printPCRs(os.Stdout, pcrs, *format); err != nil {
|
2022-03-23 09:10:58 -04:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if *export != "" {
|
|
|
|
if err := exportToFile(*export, pcrs, &afero.Afero{Fs: afero.NewOsFs()}); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-28 11:03:28 -04:00
|
|
|
// getAttestation connects to the Constellation verification service and returns its attestation document.
|
|
|
|
func getAttestation(ctx context.Context, addr string) ([]byte, error) {
|
2022-03-23 09:10:58 -04:00
|
|
|
conn, err := grpc.DialContext(
|
2022-06-28 11:03:28 -04:00
|
|
|
ctx, addr, grpc.WithTransportCredentials(insecure.NewCredentials()),
|
2022-03-23 09:10:58 -04:00
|
|
|
)
|
|
|
|
if err != nil {
|
2022-06-28 11:03:28 -04:00
|
|
|
return nil, fmt.Errorf("unable to connect to verification service: %w", err)
|
2022-03-23 09:10:58 -04:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2022-07-26 04:58:39 -04:00
|
|
|
nonce, err := crypto.GenerateRandomBytes(32)
|
2022-06-28 11:03:28 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-23 09:10:58 -04:00
|
|
|
|
2022-06-28 11:03:28 -04:00
|
|
|
client := verifyproto.NewAPIClient(conn)
|
|
|
|
res, err := client.GetAttestation(ctx, &verifyproto.GetAttestationRequest{Nonce: nonce, UserData: nonce})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-03-23 09:10:58 -04:00
|
|
|
}
|
2022-06-28 11:03:28 -04:00
|
|
|
return res.Attestation, nil
|
2022-03-23 09:10:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// validatePCRAttDoc parses and validates PCRs of an attestation document.
|
2022-11-15 09:40:49 -05:00
|
|
|
func validatePCRAttDoc(attDocRaw []byte) (measurements.M, error) {
|
2022-03-23 09:10:58 -04:00
|
|
|
attDoc := vtpm.AttestationDocument{}
|
|
|
|
if err := json.Unmarshal(attDocRaw, &attDoc); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if attDoc.Attestation == nil {
|
|
|
|
return nil, errors.New("empty attestation")
|
|
|
|
}
|
|
|
|
qIdx, err := vtpm.GetSHA256QuoteIndex(attDoc.Attestation.Quotes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for idx, pcr := range attDoc.Attestation.Quotes[qIdx].Pcrs.Pcrs {
|
|
|
|
if len(pcr) != 32 {
|
|
|
|
return nil, fmt.Errorf("incomplete PCR at index: %d", idx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return attDoc.Attestation.Quotes[qIdx].Pcrs.Pcrs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// printPCRs formates and prints PCRs to the given writer.
|
2022-06-20 07:57:25 -04:00
|
|
|
// format can be one of 'json' or 'yaml'. If it doesnt match defaults to 'json'.
|
2022-11-15 09:40:49 -05:00
|
|
|
func printPCRs(w io.Writer, pcrs measurements.M, format string) error {
|
2022-06-20 07:57:25 -04:00
|
|
|
switch format {
|
|
|
|
case "json":
|
|
|
|
return printPCRsJSON(w, pcrs)
|
|
|
|
case "yaml":
|
|
|
|
return printPCRsYAML(w, pcrs)
|
|
|
|
default:
|
|
|
|
return printPCRsJSON(w, pcrs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-15 09:40:49 -05:00
|
|
|
func printPCRsYAML(w io.Writer, pcrs measurements.M) error {
|
2022-06-20 07:57:25 -04:00
|
|
|
pcrYAML, err := yaml.Marshal(pcrs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "%s", string(pcrYAML))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-15 09:40:49 -05:00
|
|
|
func printPCRsJSON(w io.Writer, pcrs measurements.M) error {
|
2022-03-23 09:10:58 -04:00
|
|
|
pcrJSON, err := json.MarshalIndent(pcrs, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-20 07:57:25 -04:00
|
|
|
fmt.Fprintf(w, "%s", string(pcrJSON))
|
2022-03-23 09:10:58 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// exportToFile writes pcrs to a file, formatted to be valid Go code.
|
|
|
|
// Validity of the PCR map is not checked, and should be handled by the caller.
|
2022-11-15 09:40:49 -05:00
|
|
|
func exportToFile(path string, pcrs measurements.M, fs *afero.Afero) error {
|
2022-03-23 09:10:58 -04:00
|
|
|
goCode := `package pcrs
|
|
|
|
|
|
|
|
var pcrs = map[uint32][]byte{%s
|
|
|
|
}
|
|
|
|
`
|
|
|
|
pcrsFormatted := ""
|
|
|
|
for i := 0; i < len(pcrs); i++ {
|
|
|
|
pcrHex := fmt.Sprintf("%#02X", pcrs[uint32(i)][0])
|
|
|
|
for j := 1; j < len(pcrs[uint32(i)]); j++ {
|
|
|
|
pcrHex = fmt.Sprintf("%s, %#02X", pcrHex, pcrs[uint32(i)][j])
|
|
|
|
}
|
|
|
|
|
|
|
|
pcrsFormatted = pcrsFormatted + fmt.Sprintf("\n\t%d: {%s},", i, pcrHex)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fs.WriteFile(path, []byte(fmt.Sprintf(goCode, pcrsFormatted)), 0o644)
|
|
|
|
}
|