| 1 |
package cmd |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
|
| 7 |
"github.com/google/go-tpm-tools/client" |
| 8 |
"github.com/google/go-tpm-tools/internal" |
| 9 |
"github.com/google/go-tpm/legacy/tpm2" |
| 10 |
"github.com/google/go-tpm/tpmutil" |
| 11 |
"github.com/spf13/cobra" |
| 12 |
) |
| 13 |
|
| 14 |
var readCmd = &cobra.Command{ |
| 15 |
Use: "read <pcr>", |
| 16 |
Short: "Read from the TPM", |
| 17 |
Long: `Read from the TPM`, |
| 18 |
Args: cobra.NoArgs, |
| 19 |
} |
| 20 |
|
| 21 |
var pcrHashAlgo = tpm2.AlgUnknown |
| 22 |
|
| 23 |
var pcrCmd = &cobra.Command{ |
| 24 |
Use: "pcr", |
| 25 |
Short: "Read PCRs from the TPM", |
| 26 |
Long: `Read PCRs from the TPM |
| 27 |
|
| 28 |
Based on --hash-algo and --pcrs flags, read the contents of the TPM's PCRs. |
| 29 |
|
| 30 |
If --hash-algo is not provided, all banks of PCRs will be read. |
| 31 |
If --pcrs is not provided, all PCRs are read for that hash algorithm.`, |
| 32 |
Args: cobra.NoArgs, |
| 33 |
RunE: func(*cobra.Command, []string) error { |
| 34 |
rwc, err := openTpm() |
| 35 |
if err != nil { |
| 36 |
return err |
| 37 |
} |
| 38 |
defer rwc.Close() |
| 39 |
|
| 40 |
if pcrHashAlgo != tpm2.AlgUnknown { |
| 41 |
sel := tpm2.PCRSelection{Hash: pcrHashAlgo, PCRs: pcrs} |
| 42 |
if len(sel.PCRs) == 0 { |
| 43 |
sel = client.FullPcrSel(sel.Hash) |
| 44 |
} |
| 45 |
|
| 46 |
fmt.Fprintf(debugOutput(), "Reading %v PCRs (%v)\n", sel.Hash, sel.PCRs) |
| 47 |
pcrs, err := client.ReadPCRs(rwc, sel) |
| 48 |
if err != nil { |
| 49 |
return err |
| 50 |
} |
| 51 |
return internal.FormatPCRs(dataOutput(), pcrs) |
| 52 |
} |
| 53 |
if len(pcrs) != 0 { |
| 54 |
return errors.New("--hash-algo must be used with --pcrs") |
| 55 |
} |
| 56 |
|
| 57 |
fmt.Fprintln(debugOutput(), "Reading all PCRs") |
| 58 |
banks, err := client.ReadAllPCRs(rwc) |
| 59 |
if err != nil { |
| 60 |
return err |
| 61 |
} |
| 62 |
|
| 63 |
for _, bank := range banks { |
| 64 |
if err = internal.FormatPCRs(dataOutput(), bank); err != nil { |
| 65 |
return err |
| 66 |
} |
| 67 |
} |
| 68 |
return nil |
| 69 |
}, |
| 70 |
} |
| 71 |
|
| 72 |
var nvReadCmd = &cobra.Command{ |
| 73 |
Use: "nvdata", |
| 74 |
Short: "Read TPM NVData", |
| 75 |
Long: `Read NVData at a particular NVIndex |
| 76 |
|
| 77 |
Based on the --index flag, this reads all of the NVData present at that NVIndex. |
| 78 |
The read is authenticated with the owner hierarchy and an empty password.`, |
| 79 |
Args: cobra.NoArgs, |
| 80 |
RunE: func(*cobra.Command, []string) error { |
| 81 |
rwc, err := openTpm() |
| 82 |
if err != nil { |
| 83 |
return err |
| 84 |
} |
| 85 |
defer rwc.Close() |
| 86 |
|
| 87 |
data, err := tpm2.NVReadEx(rwc, tpmutil.Handle(nvIndex), tpm2.HandleOwner, "", 0) |
| 88 |
if err != nil { |
| 89 |
return err |
| 90 |
} |
| 91 |
if _, err := dataOutput().Write(data); err != nil { |
| 92 |
return fmt.Errorf("cannot output NVData: %w", err) |
| 93 |
} |
| 94 |
return nil |
| 95 |
}, |
| 96 |
} |
| 97 |
|
| 98 |
func init() { |
| 99 |
RootCmd.AddCommand(readCmd) |
| 100 |
readCmd.AddCommand(pcrCmd) |
| 101 |
readCmd.AddCommand(nvReadCmd) |
| 102 |
addOutputFlag(pcrCmd) |
| 103 |
addPCRsFlag(pcrCmd) |
| 104 |
addHashAlgoFlag(pcrCmd, &pcrHashAlgo) |
| 105 |
addIndexFlag(nvReadCmd) |
| 106 |
nvReadCmd.MarkPersistentFlagRequired("index") |
| 107 |
addOutputFlag(nvReadCmd) |
| 108 |
} |