Debian Codegraph @

examples/tpm-keys/tpm-keys.go

raw
1 //go:build !windows
2
3 // Copyright (c) 2016, Kevin Walsh. 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 implements a program to clear key handles from a TPM.
18 package main
19
20 import (
21 "flag"
22 "fmt"
23 "os"
24
25 "github.com/google/go-tpm/tpm"
26 )
27
28 func main() {
29 var tpmname = flag.String("tpm", "/dev/tpm0", "The path to the TPM device to use")
30 var closekey = flag.Bool("close", false, "Close (unload) all existing key handles")
31 flag.Parse()
32
33 rwc, err := tpm.OpenTPM(*tpmname)
34 if err != nil {
35 fmt.Fprintf(os.Stderr, "Couldn't open the TPM file %s: %s\n", *tpmname, err)
36 return
37 }
38
39 handles, err := tpm.GetKeys(rwc)
40 if err != nil {
41 fmt.Fprintf(os.Stderr, "Couldn't enumerate loaded TPM keys: %s\n", err)
42 return
43 }
44
45 fmt.Printf("%d keys loaded in the TPM\n", len(handles))
46 for i, h := range handles {
47 fmt.Printf(" (%d) Key handle %d\n", i+1, h)
48 if *closekey {
49 if err = tpm.CloseKey(rwc, h); err != nil {
50 fmt.Fprintf(os.Stderr, "Couldn't close TPM key handle %d\n", h)
51 } else {
52 fmt.Printf(" Closed handle %d\n", h)
53 }
54 }
55 }
56
57 return
58 }