Debian Codegraph @

examples/tpm-sign/sign.go

raw
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 signAction() {
30 var tpmname = flag.String("tpm", "/dev/tpm0", "The path to the TPM device to use")
31 var keyblobPath = flag.String("keyblob", "keyblob", "Input path of the keyblob to use")
32 var signaturePath = flag.String("signature", "sig.data", "Output path of the signature")
33 var hashAlgArg = flag.String("hash", "SHA256", "Hash algorithm to use when generating the signature")
34 var dataPath = flag.String("data", "", "Path to the data that will be signed. If empty or omitted, the data will be read from stdin.")
35 flag.CommandLine.Parse(os.Args[2:])
36
37 hashAlg, ok := hashNames[*hashAlgArg]
38 if !ok {
39 fmt.Fprintf(os.Stderr, "Invalid hash algorithm: %s\n", *hashAlgArg)
40 return
41 }
42
43 rwc, err := tpm.OpenTPM(*tpmname)
44 if err != nil {
45 fmt.Fprintf(os.Stderr, "Couldn't open the TPM file %s: %s\n", *tpmname, err)
46 return
47 }
48 defer rwc.Close()
49
50 // Compute the auth values as needed.
51 var srkAuth [20]byte
52 srkInput := os.Getenv(srkAuthEnvVar)
53 if srkInput != "" {
54 sa := sha1.Sum([]byte(srkInput))
55 copy(srkAuth[:], sa[:])
56 }
57
58 var usageAuth [20]byte
59 usageInput := os.Getenv(usageAuthEnvVar)
60 if usageInput != "" {
61 ua := sha1.Sum([]byte(usageInput))
62 copy(usageAuth[:], ua[:])
63 }
64
65 var migrationAuth [20]byte
66 migrationInput := os.Getenv(migrationAuthEnvVar)
67 if migrationInput != "" {
68 ma := sha1.Sum([]byte(migrationInput))
69 copy(migrationAuth[:], ma[:])
70 }
71
72 keyblob, err := os.ReadFile(*keyblobPath)
73 if err != nil {
74 fmt.Fprintf(os.Stderr, "Error reading keyblob file: %s\n", err)
75 return
76 }
77 keyHandle, err := tpm.LoadKey2(rwc, keyblob, srkAuth[:])
78 if err != nil {
79 fmt.Fprintf(os.Stderr, "Could not load keyblob: %s\n", err)
80 return
81 }
82 defer tpm.CloseKey(rwc, keyHandle)
83
84 var data []byte
85 if *dataPath == "" {
86 data, err = io.ReadAll(os.Stdin)
87 } else {
88 data, err = os.ReadFile(*dataPath)
89 }
90 if err != nil {
91 fmt.Fprintf(os.Stderr, "Error reading input data: %s\n", err)
92 return
93 }
94
95 hash := hashAlg.New()
96 if _, err = hash.Write(data); err != nil {
97 fmt.Fprintf(os.Stderr, "Error building hash of data: %s\n", err)
98 return
99 }
100 hashed := hash.Sum(nil)
101 signature, err := tpm.Sign(rwc, usageAuth[:], keyHandle, hashAlg, hashed[:])
102 if err != nil {
103 fmt.Fprintf(os.Stderr, "Could not perform sign operation: %s\n", err)
104 return
105 }
106 fmt.Printf("Writing signature to %s\n", *signaturePath)
107 if err = os.WriteFile(*signaturePath, signature, 0644); err != nil {
108 fmt.Fprintf(os.Stderr, "Unable to write signature to file: %s\n", err)
109 return
110 }
111 }