| 1 |
package cmd |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto" |
| 5 |
"crypto/x509" |
| 6 |
"encoding/pem" |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
|
| 10 |
"github.com/google/go-tpm-tools/client" |
| 11 |
"github.com/google/go-tpm/legacy/tpm2" |
| 12 |
directtpm2 "github.com/google/go-tpm/tpm2" |
| 13 |
"github.com/google/go-tpm/tpmutil" |
| 14 |
"github.com/spf13/cobra" |
| 15 |
) |
| 16 |
|
| 17 |
var keyFormat string |
| 18 |
|
| 19 |
func addKeyFormatFlag(cmd *cobra.Command) { |
| 20 |
cmd.PersistentFlags().StringVar(&keyFormat, "key-format", "pem", "type of format for the outputted key, defaults to pem, but can also specify tpmt-public") |
| 21 |
} |
| 22 |
|
| 23 |
var hierarchyNames = map[string]tpmutil.Handle{ |
| 24 |
"endorsement": tpm2.HandleEndorsement, |
| 25 |
"owner": tpm2.HandleOwner, |
| 26 |
"platform": tpm2.HandlePlatform, |
| 27 |
"null": tpm2.HandleNull, |
| 28 |
} |
| 29 |
|
| 30 |
var pubkeyCmd = &cobra.Command{ |
| 31 |
Use: "pubkey <endorsement | owner | platform | null>", |
| 32 |
Short: "Retrieve a public key from the TPM", |
| 33 |
Long: `Get the PEM-formatted public component of a TPM's primary key |
| 34 |
|
| 35 |
A TPM can create a primary asymmetric key in one of 4 hierarchies: |
| 36 |
endorsement - used for remote attestation, privacy sensitive |
| 37 |
owner - used for local signing/encryption, reset on TPM2_Clear |
| 38 |
platform - rarely used |
| 39 |
null - all keys are ephemeral, reset on every boot |
| 40 |
|
| 41 |
Furthermore, this key is based on a template containing parameters like |
| 42 |
algorithms and key sizes. By default, this command uses a standard template |
| 43 |
defined in the TPM2 spec. If --index is provided, the template is read from |
| 44 |
NVDATA instead (and --algo is ignored).`, |
| 45 |
ValidArgs: func() []string { |
| 46 |
// The keys from the hierarchyNames map are our valid arguments |
| 47 |
keys := make([]string, len(hierarchyNames)) |
| 48 |
for k := range hierarchyNames { |
| 49 |
keys = append(keys, k) |
| 50 |
} |
| 51 |
return keys |
| 52 |
}(), |
| 53 |
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), |
| 54 |
RunE: func(_ *cobra.Command, args []string) error { |
| 55 |
rwc, err := openTpm() |
| 56 |
if err != nil { |
| 57 |
return err |
| 58 |
} |
| 59 |
defer rwc.Close() |
| 60 |
|
| 61 |
key, err := getKey(rwc, hierarchyNames[args[0]], keyAlgo) |
| 62 |
if err != nil { |
| 63 |
return err |
| 64 |
} |
| 65 |
defer key.Close() |
| 66 |
|
| 67 |
if keyFormat == "pem" { |
| 68 |
return writeKey(key.PublicKey()) |
| 69 |
} |
| 70 |
if keyFormat == "tpmt-public" { |
| 71 |
encoded, err := key.PublicArea().Encode() |
| 72 |
if err != nil { |
| 73 |
return fmt.Errorf("failed to encode public area: %v", err) |
| 74 |
} |
| 75 |
_, err = dataOutput().Write(encoded) |
| 76 |
if err != nil { |
| 77 |
return fmt.Errorf("failed to write key: %v", err) |
| 78 |
} |
| 79 |
return nil |
| 80 |
} |
| 81 |
return fmt.Errorf("key format must be either pem or tpmt-public") |
| 82 |
|
| 83 |
}, |
| 84 |
} |
| 85 |
|
| 86 |
func init() { |
| 87 |
RootCmd.AddCommand(pubkeyCmd) |
| 88 |
addIndexFlag(pubkeyCmd) |
| 89 |
addOutputFlag(pubkeyCmd) |
| 90 |
addPublicKeyAlgoFlag(pubkeyCmd) |
| 91 |
addKeyFormatFlag(pubkeyCmd) |
| 92 |
} |
| 93 |
|
| 94 |
func getKey(rw io.ReadWriter, hierarchy tpmutil.Handle, _ tpm2.Algorithm) (*client.Key, error) { |
| 95 |
fmt.Fprintf(debugOutput(), "Using hierarchy 0x%x\n", hierarchy) |
| 96 |
if nvIndex != 0 { |
| 97 |
fmt.Fprintf(debugOutput(), "Reading from NVDATA index %d\n", nvIndex) |
| 98 |
return client.KeyFromNvIndex(rw, hierarchy, nvIndex) |
| 99 |
} |
| 100 |
|
| 101 |
switch hierarchy { |
| 102 |
case tpm2.HandleEndorsement: |
| 103 |
return getEK(rw) |
| 104 |
case tpm2.HandleOwner: |
| 105 |
return getSRK(rw) |
| 106 |
default: |
| 107 |
return nil, fmt.Errorf("there is no default key for the given hierarchy: 0x%x", hierarchy) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
func writeKey(pubKey crypto.PublicKey) error { |
| 112 |
fmt.Fprintf(debugOutput(), "Got key: %+v\n", pubKey) |
| 113 |
asn1Bytes, err := x509.MarshalPKIXPublicKey(pubKey) |
| 114 |
if err != nil { |
| 115 |
return err |
| 116 |
} |
| 117 |
|
| 118 |
return pem.Encode(dataOutput(), &pem.Block{ |
| 119 |
Type: "PUBLIC KEY", |
| 120 |
Bytes: asn1Bytes, |
| 121 |
}) |
| 122 |
} |
| 123 |
|
| 124 |
func readTPMTPublic(rw io.Reader) (*directtpm2.TPMTPublic, error) { |
| 125 |
data, err := io.ReadAll(rw) |
| 126 |
if err != nil { |
| 127 |
return nil, fmt.Errorf("failed to read public key: %v", err) |
| 128 |
} |
| 129 |
tPublic, err := directtpm2.Unmarshal[directtpm2.TPMTPublic](data) |
| 130 |
if err != nil { |
| 131 |
return nil, fmt.Errorf("failed to unmarshal public area: %v", err) |
| 132 |
} |
| 133 |
return tPublic, nil |
| 134 |
} |