| 1 |
//go:build !windows |
| 2 |
|
| 3 |
// Copyright (c) 2018, Ian Haken. All rights reserved. |
| 4 |
// |
| 5 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
// you may not use this file except in compliance with the License. |
| 7 |
// You may obtain a copy of the License at |
| 8 |
// |
| 9 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
// |
| 11 |
// Unless required by applicable law or agreed to in writing, software |
| 12 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
// See the License for the specific language governing permissions and |
| 15 |
// limitations under the License. |
| 16 |
|
| 17 |
package main |
| 18 |
|
| 19 |
import ( |
| 20 |
"crypto/sha1" |
| 21 |
"flag" |
| 22 |
"fmt" |
| 23 |
"io" |
| 24 |
"os" |
| 25 |
|
| 26 |
"github.com/google/go-tpm/tpm" |
| 27 |
) |
| 28 |
|
| 29 |
func extendPcrAction() { |
| 30 |
var tpmname = flag.String("tpm", "/dev/tpm0", "The path to the TPM device to use") |
| 31 |
var pcrNum = flag.Int("pcr", 16, "PCR number to extend") |
| 32 |
var reset = flag.Bool("reset", false, "Reset the PCR rather than extending it") |
| 33 |
var dataPath = flag.String("data", "", "Path to the data that will be used to extend the PCR. If empty or omitted, the data will be read from stdin.") |
| 34 |
flag.CommandLine.Parse(os.Args[2:]) |
| 35 |
|
| 36 |
rwc, err := tpm.OpenTPM(*tpmname) |
| 37 |
if err != nil { |
| 38 |
fmt.Fprintf(os.Stderr, "Couldn't open the TPM file %s: %s\n", *tpmname, err) |
| 39 |
return |
| 40 |
} |
| 41 |
defer rwc.Close() |
| 42 |
|
| 43 |
if *reset { |
| 44 |
if err = tpm.PcrReset(rwc, []int{*pcrNum}); err != nil { |
| 45 |
fmt.Fprintf(os.Stderr, "Unable to reset PCR: %s\n", err) |
| 46 |
return |
| 47 |
} |
| 48 |
} else { |
| 49 |
var data []byte |
| 50 |
if *dataPath == "" { |
| 51 |
data, err = io.ReadAll(os.Stdin) |
| 52 |
} else { |
| 53 |
data, err = os.ReadFile(*dataPath) |
| 54 |
} |
| 55 |
if err != nil { |
| 56 |
fmt.Fprintf(os.Stderr, "Unable to read input: %s\n", err) |
| 57 |
return |
| 58 |
} |
| 59 |
if _, err = tpm.PcrExtend(rwc, uint32(*pcrNum), sha1.Sum(data)); err != nil { |
| 60 |
fmt.Fprintf(os.Stderr, "Error extending PCR: %s\n", err) |
| 61 |
return |
| 62 |
} |
| 63 |
} |
| 64 |
} |