Debian Codegraph @

cmd/seal.go

raw
1 package cmd
2
3 import (
4 "fmt"
5 "io"
6
7 "github.com/spf13/cobra"
8
9 "github.com/google/go-tpm-tools/client"
10 pb "github.com/google/go-tpm-tools/proto/tpm"
11 "github.com/google/go-tpm/legacy/tpm2"
12 )
13
14 var sealHashAlgo = tpm2.AlgSHA256
15
16 var sealCmd = &cobra.Command{
17 Use: "seal",
18 Short: "Seal some data to the TPM",
19 Long: `Encrypt the input data using the TPM
20
21 TPMs support a "sealing" operation that allows some secret data to be encrypted
22 by a particular TPM. This data can only be decrypted by the same TPM that did
23 the encryption.
24
25 Optionally (using the --pcrs flag), this decryption can be furthur restricted to
26 only work if certain Platform Control Registers (PCRs) are in the correct state.
27 This allows a key (i.e. a disk encryption key) to be bound to specific machine
28 state (like Secure Boot).`,
29 Args: cobra.NoArgs,
30 RunE: func(*cobra.Command, []string) error {
31 rwc, err := openTpm()
32 if err != nil {
33 return err
34 }
35 defer rwc.Close()
36
37 fmt.Fprintln(debugOutput(), "Loading SRK")
38 srk, err := getSRK(rwc)
39 if err != nil {
40 return err
41 }
42 defer srk.Close()
43
44 fmt.Fprintln(debugOutput(), "Reading sealed data")
45 secret, err := io.ReadAll(dataInput())
46 if err != nil {
47 return err
48 }
49
50 fmt.Fprintf(debugOutput(), "Sealing to PCRs: %v\n", pcrs)
51 opts := client.SealOpts{Current: tpm2.PCRSelection{
52 Hash: sealHashAlgo,
53 PCRs: pcrs}}
54 sealed, err := srk.Seal(secret, opts)
55 if err != nil {
56 return fmt.Errorf("sealing data: %w", err)
57 }
58
59 fmt.Fprintln(debugOutput(), "Writing sealed data")
60 var output []byte
61 if output, err = marshalOptions.Marshal(sealed); err != nil {
62 return err
63 }
64 if _, err = dataOutput().Write(output); err != nil {
65 return err
66 }
67 fmt.Fprintf(debugOutput(), "Sealed data to PCRs: %v\n", pcrs)
68 return nil
69 },
70 }
71
72 var unsealCmd = &cobra.Command{
73 Use: "unseal",
74 Short: "Unseal some data previously sealed to the TPM",
75 Long: `Decrypt the input data using the TPM
76
77 The opposite of "gotpm seal". This takes in some sealed input and decrypts it
78 using the TPM. This operation will fail if used on a different TPM, or if the
79 Platform Control Registers (PCRs) are in the incorrect state.
80
81 All the necessary data to decrypt the sealed input is present in the input blob.
82 We do not need to specify the PCRs used for unsealing.
83
84 We do support an optional "certification" process. A list of PCRs may be
85 provided with --pcrs, and the unwrapping will fail if the PCR values when
86 sealing differ from the current PCR values. This allows for verification of the
87 machine state when sealing took place.
88 `,
89 Args: cobra.NoArgs,
90 RunE: func(*cobra.Command, []string) error {
91 rwc, err := openTpm()
92 if err != nil {
93 return err
94 }
95 defer rwc.Close()
96
97 fmt.Fprintln(debugOutput(), "Reading sealed data")
98 data, err := io.ReadAll(dataInput())
99 if err != nil {
100 return err
101 }
102 var sealed pb.SealedBytes
103 if err := unmarshalOptions.Unmarshal(data, &sealed); err != nil {
104 return err
105 }
106
107 fmt.Fprintln(debugOutput(), "Loading SRK")
108 keyAlgo = tpm2.Algorithm(sealed.GetSrk())
109 srk, err := getSRK(rwc)
110 if err != nil {
111 return err
112 }
113 defer srk.Close()
114
115 fmt.Fprintln(debugOutput(), "Unsealing data")
116
117 opts := client.UnsealOpts{CertifyCurrent: tpm2.PCRSelection{
118 Hash: client.CertifyHashAlgTpm,
119 PCRs: pcrs}}
120 secret, err := srk.Unseal(&sealed, opts)
121 if err != nil {
122 return fmt.Errorf("unsealing data: %w", err)
123 }
124
125 fmt.Fprintln(debugOutput(), "Writing secret data")
126 if _, err := dataOutput().Write(secret); err != nil {
127 return fmt.Errorf("writing secret data: %w", err)
128 }
129 fmt.Fprintln(debugOutput(), "Unsealed data using TPM")
130 return nil
131 },
132 }
133
134 func init() {
135 RootCmd.AddCommand(sealCmd)
136 RootCmd.AddCommand(unsealCmd)
137 addInputFlag(sealCmd)
138 addInputFlag(unsealCmd)
139 addOutputFlag(sealCmd)
140 addOutputFlag(unsealCmd)
141 // PCRs and hash algorithm only used for sealing
142 addPCRsFlag(sealCmd)
143 addHashAlgoFlag(sealCmd, &sealHashAlgo)
144 addPCRsFlag(unsealCmd)
145 addPublicKeyAlgoFlag(sealCmd)
146 }