Debian Codegraph @

examples/tpm-sign/generate.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 "crypto/x509"
22 "flag"
23 "fmt"
24 "os"
25 "strconv"
26 "strings"
27
28 "github.com/google/go-tpm/tpm"
29 )
30
31 func generateAction() {
32 var tpmname = flag.String("tpm", "/dev/tpm0", "The path to the TPM device to use")
33 var keyblobPath = flag.String("keyblob", "keyblob", "Output path of the generated keyblob")
34 var pubKeyPath = flag.String("public-key", "publickey", "Output path of the generated keyblob's public key")
35 var pcrsStr = flag.String("pcrs", "", "A comma-separated list of PCR numbers against which the generated key will be bound. If blank, it will not be bound to any PCR values.")
36 flag.CommandLine.Parse(os.Args[2:])
37
38 var pcrs []int
39 if *pcrsStr != "" {
40 for _, pcr := range strings.Split(*pcrsStr, ",") {
41 pcrNum, err := strconv.Atoi(pcr)
42 if err != nil {
43 fmt.Fprintf(os.Stderr, "Bad value in pcrs argument: %s\n", pcr)
44 return
45 }
46 pcrs = append(pcrs, pcrNum)
47 }
48 }
49
50 rwc, err := tpm.OpenTPM(*tpmname)
51 if err != nil {
52 fmt.Fprintf(os.Stderr, "Couldn't open the TPM file %s: %s\n", *tpmname, err)
53 return
54 }
55 defer rwc.Close()
56
57 // Compute the auth values as needed.
58 var srkAuth [20]byte
59 srkInput := os.Getenv(srkAuthEnvVar)
60 if srkInput != "" {
61 sa := sha1.Sum([]byte(srkInput))
62 copy(srkAuth[:], sa[:])
63 }
64
65 var usageAuth [20]byte
66 usageInput := os.Getenv(usageAuthEnvVar)
67 if usageInput != "" {
68 ua := sha1.Sum([]byte(usageInput))
69 copy(usageAuth[:], ua[:])
70 }
71
72 var migrationAuth [20]byte
73 migrationInput := os.Getenv(migrationAuthEnvVar)
74 if migrationInput != "" {
75 ma := sha1.Sum([]byte(migrationInput))
76 copy(migrationAuth[:], ma[:])
77 }
78
79 keyblob, err := tpm.CreateWrapKey(rwc, srkAuth[:], usageAuth, migrationAuth, pcrs)
80 if err != nil {
81 fmt.Fprintf(os.Stderr, "Couldn't make a new signing key: %s\n", err)
82 return
83 }
84 fmt.Printf("Writing keyblob to %s\n", *keyblobPath)
85 if err = os.WriteFile(*keyblobPath, keyblob, 0644); err != nil {
86 fmt.Fprintf(os.Stderr, "Error writing keyblob file: %s\n", err)
87 return
88 }
89
90 pubKey, err := tpm.UnmarshalRSAPublicKey(keyblob)
91 if err != nil {
92 fmt.Fprintf(os.Stderr, "Could not get public key: %s\n", err)
93 return
94 }
95
96 pubKeyBytes, err := x509.MarshalPKIXPublicKey(pubKey)
97 if err != nil {
98 fmt.Fprintf(os.Stderr, "Could not marshal public key: %s\n", err)
99 return
100 }
101 fmt.Printf("Writing public key to %s\n", *pubKeyPath)
102 if err = os.WriteFile(*pubKeyPath, pubKeyBytes, 0644); err != nil {
103 fmt.Fprintf(os.Stderr, "Error writing public key file: %s\n", err)
104 return
105 }
106 }