| 1 |
package cmd |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
|
| 6 |
"github.com/google/go-tpm-tools/client" |
| 7 |
tpb "github.com/google/go-tpm-tools/proto/tpm" |
| 8 |
"github.com/google/go-tpm-tools/server" |
| 9 |
"github.com/google/go-tpm/tpm2" |
| 10 |
"github.com/google/go-tpm/tpm2/transport" |
| 11 |
"github.com/spf13/cobra" |
| 12 |
) |
| 13 |
|
| 14 |
// This file implements TPM registration which for now is only used for SVSM e-vTPMs. |
| 15 |
// It uses https://trustedcomputinggroup.org/wp-content/uploads/EK-Based-Key-Attestation-with-TPM-Firmware-Version-V1-RC1_9July2025.pdf#page=8 |
| 16 |
// which we call as import certify. |
| 17 |
// ActivateCredential is not implemented yet. |
| 18 |
|
| 19 |
var registerCmd = &cobra.Command{ |
| 20 |
Use: "register", |
| 21 |
Short: "Register an SVSM TPM AK", |
| 22 |
Long: "Given an EK, we'll register a corresponding AK and prove that it's on the same TPM as the EK.", |
| 23 |
Args: cobra.NoArgs, |
| 24 |
} |
| 25 |
|
| 26 |
var ( |
| 27 |
secretOut string |
| 28 |
secretIn string |
| 29 |
) |
| 30 |
|
| 31 |
func addSecretInputFlag(cmd *cobra.Command) { |
| 32 |
cmd.PersistentFlags().StringVar(&secretIn, "secret-input", "", |
| 33 |
"specifies path to read the secret that was generated from create-challenge") |
| 34 |
} |
| 35 |
|
| 36 |
func addSecretOutputFlag(cmd *cobra.Command) { |
| 37 |
cmd.PersistentFlags().StringVar(&secretOut, "secret-output", "", |
| 38 |
"specifies path to write the secret to") |
| 39 |
} |
| 40 |
|
| 41 |
var createChallengeCmd = &cobra.Command{ |
| 42 |
Use: "create-challenge", |
| 43 |
Short: "Create a challenge for the client to register the EK", |
| 44 |
Long: `Given an EK public key in TPM2 wire format, create a challenge for the client |
| 45 |
to register the EK. and save the ChallengeSecret but don't share it with the |
| 46 |
client.`, |
| 47 |
Args: cobra.NoArgs, |
| 48 |
RunE: func(_ *cobra.Command, _ []string) error { |
| 49 |
ekpub, err := readTPMTPublic(dataInput()) |
| 50 |
if err != nil { |
| 51 |
return err |
| 52 |
} |
| 53 |
|
| 54 |
challenge, secret, err := server.CreateRestrictedHMACBlob(ekpub) |
| 55 |
if err != nil { |
| 56 |
return fmt.Errorf("could not create challenge: %s", err) |
| 57 |
} |
| 58 |
writeProtoToOutput(challenge) |
| 59 |
_, err = openForWrite(secretOut).Write(secret) |
| 60 |
if err != nil { |
| 61 |
return fmt.Errorf("could not write secret: %s", err) |
| 62 |
} |
| 63 |
|
| 64 |
return nil |
| 65 |
}, |
| 66 |
} |
| 67 |
|
| 68 |
var solveChallengeCmd = &cobra.Command{ |
| 69 |
Use: "solve-challenge", |
| 70 |
Short: "Solve a challenge from the TPM EK", |
| 71 |
Args: cobra.NoArgs, |
| 72 |
RunE: func(_ *cobra.Command, _ []string) error { |
| 73 |
challenge := &tpb.ImportBlob{} |
| 74 |
err := readProtoFromPath(input, challenge) |
| 75 |
if err != nil { |
| 76 |
return fmt.Errorf("could not read challenge: %s", err) |
| 77 |
} |
| 78 |
|
| 79 |
rwc, err := openImpl() |
| 80 |
if err != nil { |
| 81 |
return fmt.Errorf("could not open TPM: %s", err) |
| 82 |
} |
| 83 |
defer rwc.Close() |
| 84 |
tpm := transport.FromReadWriter(rwc) |
| 85 |
solved, err := client.CreateCertifiedAKBlob(tpm, challenge, tpm2.TPMAlgID(keyAlgo)) |
| 86 |
if err != nil { |
| 87 |
return fmt.Errorf("could not solve challenge: %s", err) |
| 88 |
} |
| 89 |
writeProtoToOutput(solved) |
| 90 |
return nil |
| 91 |
}, |
| 92 |
} |
| 93 |
|
| 94 |
var verifyChallengeCmd = &cobra.Command{ |
| 95 |
Use: "verify-challenge", |
| 96 |
Short: "Verify a challenge for the SVSM TPM's EK and its binding to the SNP attestation report", |
| 97 |
Args: cobra.NoArgs, |
| 98 |
RunE: func(_ *cobra.Command, _ []string) error { |
| 99 |
blob := &tpb.CertifiedBlob{} |
| 100 |
err := readProtoFromPath(input, blob) |
| 101 |
if err != nil { |
| 102 |
return err |
| 103 |
} |
| 104 |
|
| 105 |
if secretIn == "" { |
| 106 |
return fmt.Errorf("secret-input must be specified") |
| 107 |
} |
| 108 |
secret, err := readBytes(secretIn) |
| 109 |
if err != nil { |
| 110 |
return fmt.Errorf("could not read secret: %s", err) |
| 111 |
} |
| 112 |
|
| 113 |
err = server.VerifyCertifiedAKBlob(blob, secret) |
| 114 |
if err != nil { |
| 115 |
return fmt.Errorf("could not verify attestation: %s", err) |
| 116 |
} |
| 117 |
return nil |
| 118 |
}, |
| 119 |
} |
| 120 |
|
| 121 |
func init() { |
| 122 |
RootCmd.AddCommand(registerCmd) |
| 123 |
challengeCommands := []*cobra.Command{ |
| 124 |
createChallengeCmd, |
| 125 |
solveChallengeCmd, |
| 126 |
verifyChallengeCmd, |
| 127 |
} |
| 128 |
for _, cmd := range challengeCommands { |
| 129 |
registerCmd.AddCommand(cmd) |
| 130 |
addOutputFlag(cmd) |
| 131 |
addFormatFlag(cmd) |
| 132 |
addInputFlag(cmd) |
| 133 |
} |
| 134 |
|
| 135 |
addSecretOutputFlag(createChallengeCmd) |
| 136 |
addSecretInputFlag(verifyChallengeCmd) |
| 137 |
addPublicKeyAlgoFlag(solveChallengeCmd) |
| 138 |
} |