Debian Codegraph @

client/import_certify.go

raw
1 package client
2
3 import (
4 "fmt"
5
6 tpb "github.com/google/go-tpm-tools/proto/tpm"
7 "github.com/google/go-tpm/tpm2"
8 "github.com/google/go-tpm/tpm2/transport"
9 )
10
11 // This file aims to implement the attester side of https://trustedcomputinggroup.org/wp-content/uploads/EK-Based-Key-Attestation-with-TPM-Firmware-Version-V1-RC1_9July2025.pdf#page=8
12 // For reference: https://github.com/TrustedComputingGroup/tpm-fw-attestation-reference-code
13
14 func ekResponse(tpm transport.TPM) (*tpm2.CreatePrimaryResponse, error) {
15 // SVSM currently only supports attesting an RSA EK.
16 // We may parameterize this later for more options.
17 return tpm2.CreatePrimary{
18 PrimaryHandle: tpm2.TPMRHEndorsement,
19 InPublic: tpm2.New2B(tpm2.RSAEKTemplate),
20 }.Execute(tpm)
21 }
22
23 func makeAK(tpm transport.TPM, keyAlgo tpm2.TPMAlgID) (*tpm2.CreatePrimaryResponse, error) {
24 var public []byte
25 var err error
26 switch keyAlgo {
27 case tpm2.TPMAlgECC:
28 public, err = AKTemplateECC().Encode()
29 case tpm2.TPMAlgRSA:
30 public, err = AKTemplateRSA().Encode()
31 default:
32 return nil, fmt.Errorf("unsupported keyAlgo %v", keyAlgo)
33 }
34 if err != nil {
35 return nil, fmt.Errorf("failed to create AK: %w", err)
36 }
37 cp, err := tpm2.CreatePrimary{
38 PrimaryHandle: tpm2.TPMRHOwner,
39 InPublic: tpm2.BytesAs2B[tpm2.TPMTPublic](public),
40 }.Execute(tpm)
41 if err != nil {
42 return nil, err
43 }
44 return cp, nil
45 }
46
47 // CreateCertifiedAKBlob creates an AK and certifies it, thus solving the TPM registration challenge.
48 func CreateCertifiedAKBlob(tpm transport.TPM, req *tpb.ImportBlob, keyAlgo tpm2.TPMAlgID) (*tpb.CertifiedBlob, error) {
49 ek, err := ekResponse(tpm)
50 if err != nil {
51 return nil, fmt.Errorf("failed to create RSA EK: %w", err)
52 }
53
54 // Import the restricted HMAC key.
55 imported, err := tpm2.Import{
56 ParentHandle: tpm2.AuthHandle{
57 Handle: ek.ObjectHandle,
58 Name: ek.Name,
59 Auth: tpm2.Policy(tpm2.TPMAlgSHA256, 32, ekPolicy),
60 },
61 ObjectPublic: tpm2.BytesAs2B[tpm2.TPMTPublic](req.GetPublicArea()),
62 Duplicate: tpm2.TPM2BPrivate{Buffer: req.GetDuplicate()},
63 InSymSeed: tpm2.TPM2BEncryptedSecret{Buffer: req.GetEncryptedSeed()},
64 }.Execute(tpm)
65 if err != nil {
66 tpm2.FlushContext{
67 FlushHandle: ek.ObjectHandle,
68 }.Execute(tpm)
69 return nil, fmt.Errorf("failed to import blob: %w", err)
70 }
71
72 // Load the imported HMAC key.
73 loaded, err := tpm2.Load{
74 ParentHandle: tpm2.AuthHandle{
75 Handle: ek.ObjectHandle,
76 Name: ek.Name,
77 Auth: tpm2.Policy(tpm2.TPMAlgSHA256, 32, ekPolicy),
78 },
79 InPublic: tpm2.BytesAs2B[tpm2.TPMTPublic](req.GetPublicArea()),
80 InPrivate: imported.OutPrivate,
81 }.Execute(tpm)
82 // Flush before checking error and potentially early returning since we need to flush in both situations.
83 tpm2.FlushContext{
84 FlushHandle: ek.ObjectHandle,
85 }.Execute(tpm)
86 if err != nil {
87 return nil, fmt.Errorf("failed to load HMAC: %w", err)
88 }
89
90 defer tpm2.FlushContext{
91 FlushHandle: loaded.ObjectHandle,
92 }.Execute(tpm)
93
94 ak, err := makeAK(tpm, keyAlgo)
95 if err != nil {
96 return nil, err
97 }
98 defer tpm2.FlushContext{
99 FlushHandle: ak.ObjectHandle,
100 }.Execute(tpm)
101
102 // Certify a newly created AK.
103 certified, err := tpm2.Certify{
104 ObjectHandle: tpm2.NamedHandle{
105 Handle: ak.ObjectHandle,
106 Name: ak.Name,
107 },
108 SignHandle: tpm2.NamedHandle{
109 Handle: loaded.ObjectHandle,
110 Name: loaded.Name,
111 },
112 }.Execute(tpm)
113 if err != nil {
114 return nil, fmt.Errorf("failed to certify blob: %w", err)
115 }
116
117 return &tpb.CertifiedBlob{
118 PubArea: ak.OutPublic.Bytes(),
119 CertifyInfo: certified.CertifyInfo.Bytes(),
120 RawSig: tpm2.Marshal(certified.Signature),
121 }, nil
122 }
123
124 func ekPolicy(t transport.TPM, handle tpm2.TPMISHPolicy, nonceTPM tpm2.TPM2BNonce) error {
125 cmd := tpm2.PolicySecret{
126 AuthHandle: tpm2.TPMRHEndorsement,
127 PolicySession: handle,
128 NonceTPM: nonceTPM,
129 }
130 _, err := cmd.Execute(t)
131 return err
132 }