| 1 |
package cmd |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"os" |
| 8 |
"strconv" |
| 9 |
"strings" |
| 10 |
|
| 11 |
"github.com/google/go-tpm-tools/client" |
| 12 |
"github.com/google/go-tpm/legacy/tpm2" |
| 13 |
"github.com/spf13/cobra" |
| 14 |
"google.golang.org/protobuf/proto" |
| 15 |
) |
| 16 |
|
| 17 |
var ( |
| 18 |
output string |
| 19 |
input string |
| 20 |
nvIndex uint32 |
| 21 |
nonce []byte |
| 22 |
teeNonce []byte |
| 23 |
keyAlgo = tpm2.AlgRSA |
| 24 |
pcrs []int |
| 25 |
format string |
| 26 |
asAddress string |
| 27 |
audience string |
| 28 |
eventLog string |
| 29 |
cloudLog bool |
| 30 |
customNonce []string |
| 31 |
) |
| 32 |
|
| 33 |
type pcrsFlag struct { |
| 34 |
value *[]int |
| 35 |
} |
| 36 |
|
| 37 |
func (f *pcrsFlag) Set(val string) error { |
| 38 |
for _, d := range strings.Split(val, ",") { |
| 39 |
pcr, err := strconv.Atoi(d) |
| 40 |
if err != nil { |
| 41 |
return err |
| 42 |
} |
| 43 |
if pcr < 0 || pcr >= client.NumPCRs { |
| 44 |
return errors.New("pcr out of range") |
| 45 |
} |
| 46 |
*f.value = append(*f.value, pcr) |
| 47 |
} |
| 48 |
return nil |
| 49 |
} |
| 50 |
|
| 51 |
func (f *pcrsFlag) Type() string { |
| 52 |
return "pcrs" |
| 53 |
} |
| 54 |
|
| 55 |
func (f *pcrsFlag) String() string { |
| 56 |
if len(*f.value) == 0 { |
| 57 |
return "" |
| 58 |
} |
| 59 |
var b strings.Builder |
| 60 |
fmt.Fprintf(&b, "%d", (*f.value)[0]) |
| 61 |
for _, pcr := range (*f.value)[1:] { |
| 62 |
fmt.Fprintf(&b, ",%d", pcr) |
| 63 |
} |
| 64 |
return b.String() |
| 65 |
} |
| 66 |
|
| 67 |
var algos = map[tpm2.Algorithm]string{ |
| 68 |
tpm2.AlgUnknown: "", |
| 69 |
tpm2.AlgRSA: "rsa", |
| 70 |
tpm2.AlgECC: "ecc", |
| 71 |
tpm2.AlgSHA1: "sha1", |
| 72 |
tpm2.AlgSHA256: "sha256", |
| 73 |
tpm2.AlgSHA384: "sha384", |
| 74 |
tpm2.AlgSHA512: "sha512", |
| 75 |
} |
| 76 |
|
| 77 |
type algoFlag struct { |
| 78 |
value *tpm2.Algorithm |
| 79 |
allowed []tpm2.Algorithm |
| 80 |
} |
| 81 |
|
| 82 |
func (f *algoFlag) Set(val string) error { |
| 83 |
present := false |
| 84 |
for _, algo := range f.allowed { |
| 85 |
if algos[algo] == val { |
| 86 |
*f.value = algo |
| 87 |
present = true |
| 88 |
} |
| 89 |
} |
| 90 |
if !present { |
| 91 |
return errors.New("unknown algorithm") |
| 92 |
} |
| 93 |
return nil |
| 94 |
} |
| 95 |
|
| 96 |
func (f *algoFlag) Type() string { |
| 97 |
return "algo" |
| 98 |
} |
| 99 |
|
| 100 |
func (f *algoFlag) String() string { |
| 101 |
return algos[*f.value] |
| 102 |
} |
| 103 |
|
| 104 |
// Allowed gives a string list of the permitted algorithm values for this flag. |
| 105 |
func (f *algoFlag) Allowed() string { |
| 106 |
out := make([]string, len(f.allowed)) |
| 107 |
for i, a := range f.allowed { |
| 108 |
out[i] = algos[a] |
| 109 |
} |
| 110 |
return strings.Join(out, ", ") |
| 111 |
} |
| 112 |
|
| 113 |
// Disable the "help" subcommand (and just use the -h/--help flags). |
| 114 |
// This should be called on all commands with subcommands. |
| 115 |
// See https://github.com/spf13/cobra/issues/587 for why this is needed. |
| 116 |
func hideHelp(cmd *cobra.Command) { |
| 117 |
cmd.SetHelpCommand(&cobra.Command{Hidden: true}) |
| 118 |
} |
| 119 |
|
| 120 |
// Lets this command specify an output file, for use with dataOutput(). |
| 121 |
func addOutputFlag(cmd *cobra.Command) { |
| 122 |
cmd.PersistentFlags().StringVar(&output, "output", "", |
| 123 |
"output file (defaults to stdout)") |
| 124 |
} |
| 125 |
|
| 126 |
// Lets this command specify an input file, for use with dataInput(). |
| 127 |
func addInputFlag(cmd *cobra.Command) { |
| 128 |
cmd.PersistentFlags().StringVar(&input, "input", "", |
| 129 |
"input file (defaults to stdin)") |
| 130 |
} |
| 131 |
|
| 132 |
// Lets this command specify an Attestation Server Address. |
| 133 |
func addAsAddressFlag(cmd *cobra.Command) { |
| 134 |
cmd.PersistentFlags().StringVar(&asAddress, "verifier-endpoint", "https://confidentialcomputing.googleapis.com", |
| 135 |
"the attestation verifier endpoint used to retrieve an attestation claims token") |
| 136 |
} |
| 137 |
|
| 138 |
// Lets this command enable Cloud logging. |
| 139 |
func addCloudLoggingFlag(cmd *cobra.Command) { |
| 140 |
cmd.Flags().BoolVar(&cloudLog, "cloud-log", false, "logs the attestation and token to Cloud Logging for auditing purposes. Requires the audience flag.") |
| 141 |
} |
| 142 |
|
| 143 |
// Lets this command specify custom audience field of the attestation token. |
| 144 |
func addAudienceFlag(cmd *cobra.Command) { |
| 145 |
cmd.PersistentFlags().StringVar(&audience, "audience", "", |
| 146 |
"the audience field in the claims token. Cannot be sts.googleapis.com.") |
| 147 |
} |
| 148 |
|
| 149 |
// Lets this command specify custom nonce field of the attestation token. |
| 150 |
func addCustomNonceFlag(cmd *cobra.Command) { |
| 151 |
cmd.PersistentFlags().StringArrayVar(&customNonce, "custom-nonce", nil, |
| 152 |
"the custom nonce field in the claims token. use this flag multiple times to add multiple custom nonces.") |
| 153 |
} |
| 154 |
|
| 155 |
// Lets this command specify event log path. |
| 156 |
func addEventLogFlag(cmd *cobra.Command) { |
| 157 |
cmd.PersistentFlags().StringVar(&eventLog, "event-log", "/sys/kernel/security/tpm0/binary_bios_measurements", "specifies the event log file path.") |
| 158 |
} |
| 159 |
|
| 160 |
// Lets this command specify an NVDATA index, for use with nvIndex. |
| 161 |
func addIndexFlag(cmd *cobra.Command) { |
| 162 |
cmd.PersistentFlags().Uint32Var(&nvIndex, "index", 0, |
| 163 |
"NVDATA index, cannot be 0") |
| 164 |
} |
| 165 |
|
| 166 |
// Lets this command specify some number of PCR arguments, check if in range. |
| 167 |
func addPCRsFlag(cmd *cobra.Command) { |
| 168 |
cmd.PersistentFlags().Var(&pcrsFlag{&pcrs}, "pcrs", "comma separated list of PCR numbers") |
| 169 |
} |
| 170 |
|
| 171 |
// Lets this command specify the public key algorithm. |
| 172 |
func addPublicKeyAlgoFlag(cmd *cobra.Command) { |
| 173 |
f := algoFlag{&keyAlgo, []tpm2.Algorithm{tpm2.AlgRSA, tpm2.AlgECC}} |
| 174 |
cmd.PersistentFlags().Var(&f, "algo", "public key algorithm: "+f.Allowed()) |
| 175 |
} |
| 176 |
|
| 177 |
func addHashAlgoFlag(cmd *cobra.Command, hashAlgo *tpm2.Algorithm) { |
| 178 |
f := algoFlag{hashAlgo, []tpm2.Algorithm{tpm2.AlgSHA1, tpm2.AlgSHA256, tpm2.AlgSHA384, tpm2.AlgSHA512}} |
| 179 |
cmd.PersistentFlags().Var(&f, "hash-algo", "hash algorithm: "+f.Allowed()) |
| 180 |
} |
| 181 |
|
| 182 |
func addNonceFlag(cmd *cobra.Command) { |
| 183 |
cmd.PersistentFlags().BytesHexVar(&nonce, "nonce", []byte{}, "hex encoded nonce for vTPM attestation, cannot be empty") |
| 184 |
} |
| 185 |
|
| 186 |
// Lets this command specify the type of output file (binary or txt) |
| 187 |
func addFormatFlag(cmd *cobra.Command) { |
| 188 |
cmd.PersistentFlags().StringVar(&format, "format", "binarypb", "type of output file where attestation report stored <binarypb|textproto>") |
| 189 |
} |
| 190 |
|
| 191 |
func addTeeNonceflag(cmd *cobra.Command) { |
| 192 |
cmd.PersistentFlags().BytesHexVar(&teeNonce, "tee-nonce", []byte{}, "hex encoded teenonce for hardware attestation, can be empty") |
| 193 |
} |
| 194 |
|
| 195 |
// alwaysError implements io.ReadWriter by always returning an error |
| 196 |
type alwaysError struct { |
| 197 |
error |
| 198 |
} |
| 199 |
|
| 200 |
func (ae alwaysError) Write([]byte) (int, error) { |
| 201 |
return 0, ae.error |
| 202 |
} |
| 203 |
|
| 204 |
func (ae alwaysError) Read(_ []byte) (n int, err error) { |
| 205 |
return 0, ae.error |
| 206 |
} |
| 207 |
|
| 208 |
// Handle to output data file. If there is an issue opening the file, the Writer |
| 209 |
// returned will return the error upon any call to Write() |
| 210 |
func dataOutput() io.Writer { |
| 211 |
if output == "" { |
| 212 |
return os.Stdout |
| 213 |
} |
| 214 |
|
| 215 |
file, err := os.Create(output) |
| 216 |
if err != nil { |
| 217 |
return alwaysError{err} |
| 218 |
} |
| 219 |
return file |
| 220 |
} |
| 221 |
|
| 222 |
func openForWrite(path string) io.Writer { |
| 223 |
if path == "" { |
| 224 |
return os.Stdout |
| 225 |
} |
| 226 |
|
| 227 |
file, err := os.Create(path) |
| 228 |
if err != nil { |
| 229 |
return alwaysError{err} |
| 230 |
} |
| 231 |
return file |
| 232 |
} |
| 233 |
|
| 234 |
func writeProtoToOutput(message proto.Message) error { |
| 235 |
var out []byte |
| 236 |
var err error |
| 237 |
switch format { |
| 238 |
case "binarypb": |
| 239 |
out, err = proto.Marshal(message) |
| 240 |
if err != nil { |
| 241 |
return fmt.Errorf("failed to marshal proto: %v", message) |
| 242 |
} |
| 243 |
case "textproto": |
| 244 |
out = []byte(marshalOptions.Format(message)) |
| 245 |
default: |
| 246 |
return fmt.Errorf("format should be either binarypb or textproto") |
| 247 |
} |
| 248 |
if _, err := dataOutput().Write(out); err != nil { |
| 249 |
return fmt.Errorf("failed to write attestation report: %v", err) |
| 250 |
} |
| 251 |
return nil |
| 252 |
} |
| 253 |
|
| 254 |
// Handle to input data file. If there is an issue opening the file, the Reader |
| 255 |
// returned will return the error upon any call to Read() |
| 256 |
func dataInput() io.Reader { |
| 257 |
if input == "" { |
| 258 |
return os.Stdin |
| 259 |
} |
| 260 |
|
| 261 |
file, err := os.Open(input) |
| 262 |
if err != nil { |
| 263 |
return alwaysError{err} |
| 264 |
} |
| 265 |
return file |
| 266 |
} |
| 267 |
|
| 268 |
var errMustSpecifyPath = errors.New("must specify path to read file") |
| 269 |
|
| 270 |
func readBytes(path string) ([]byte, error) { |
| 271 |
if path == "" { |
| 272 |
return nil, errMustSpecifyPath |
| 273 |
} |
| 274 |
file, err := os.Open(path) |
| 275 |
if err != nil { |
| 276 |
return nil, err |
| 277 |
} |
| 278 |
bytes, err := io.ReadAll(file) |
| 279 |
if err != nil { |
| 280 |
return nil, fmt.Errorf("failed to read proto: %v", err) |
| 281 |
} |
| 282 |
return bytes, nil |
| 283 |
} |
| 284 |
|
| 285 |
// Reads binarypb file from path |
| 286 |
func readProtoFromPath(path string, message proto.Message) error { |
| 287 |
requestBytes, err := readBytes(path) |
| 288 |
if err != nil { |
| 289 |
return fmt.Errorf("failed to read proto: %v", err) |
| 290 |
} |
| 291 |
|
| 292 |
err = proto.Unmarshal(requestBytes, message) |
| 293 |
if err != nil { |
| 294 |
return fmt.Errorf("failed to unmarshal proto: %v", err) |
| 295 |
} |
| 296 |
return nil |
| 297 |
} |
| 298 |
|
| 299 |
// Load SRK based on tpm2.Algorithm set in the global flag vars. |
| 300 |
func getSRK(rwc io.ReadWriter) (*client.Key, error) { |
| 301 |
switch keyAlgo { |
| 302 |
case tpm2.AlgRSA: |
| 303 |
return client.StorageRootKeyRSA(rwc) |
| 304 |
case tpm2.AlgECC: |
| 305 |
return client.StorageRootKeyECC(rwc) |
| 306 |
default: |
| 307 |
panic("unexpected keyAlgo") |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
// Load EK based on tpm2.Algorithm set in the global flag vars. |
| 312 |
func getEK(rwc io.ReadWriter) (*client.Key, error) { |
| 313 |
switch keyAlgo { |
| 314 |
case tpm2.AlgRSA: |
| 315 |
return client.EndorsementKeyRSA(rwc) |
| 316 |
case tpm2.AlgECC: |
| 317 |
return client.EndorsementKeyECC(rwc) |
| 318 |
default: |
| 319 |
panic("unexpected keyAlgo") |
| 320 |
} |
| 321 |
} |