| 1 |
package client |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"math" |
| 7 |
|
| 8 |
"github.com/google/go-tpm/legacy/tpm2" |
| 9 |
"github.com/google/go-tpm/tpmutil" |
| 10 |
) |
| 11 |
|
| 12 |
// Reserved Handles from "TCG TPM v2.0 Provisioning Guidance" - v1r1 - Table 2 |
| 13 |
const ( |
| 14 |
EKReservedHandle = tpmutil.Handle(0x81010001) |
| 15 |
EKECCReservedHandle = tpmutil.Handle(0x81010002) |
| 16 |
SRKReservedHandle = tpmutil.Handle(0x81000001) |
| 17 |
SRKECCReservedHandle = tpmutil.Handle(0x81000002) |
| 18 |
) |
| 19 |
|
| 20 |
// From "TCG EK Credential Profile", v2.3r2 Section 2.2.1.4 |
| 21 |
const ( |
| 22 |
// RSA 2048 EK Cert. |
| 23 |
EKCertNVIndexRSA uint32 = 0x01c00002 |
| 24 |
// ECC P256 EK Cert. |
| 25 |
EKCertNVIndexECC uint32 = 0x01c0000a |
| 26 |
) |
| 27 |
|
| 28 |
// Picked available handles from TPM 2.0 Handles and Localities 2.3.1 - Table 11 |
| 29 |
// go-tpm-tools will use handles in the range from 0x81008F00 to 0x81008FFF |
| 30 |
const ( |
| 31 |
DefaultAKECCHandle = tpmutil.Handle(0x81008F00) |
| 32 |
DefaultAKRSAHandle = tpmutil.Handle(0x81008F01) |
| 33 |
) |
| 34 |
|
| 35 |
// GCE Attestation Key NV Indices |
| 36 |
const ( |
| 37 |
// RSA 2048 AK. |
| 38 |
GceAKCertNVIndexRSA uint32 = 0x01c10000 |
| 39 |
GceAKTemplateNVIndexRSA uint32 = 0x01c10001 |
| 40 |
// ECC P256 AK. |
| 41 |
GceAKCertNVIndexECC uint32 = 0x01c10002 |
| 42 |
GceAKTemplateNVIndexECC uint32 = 0x01c10003 |
| 43 |
) |
| 44 |
|
| 45 |
func isHierarchy(h tpmutil.Handle) bool { |
| 46 |
return h == tpm2.HandleOwner || h == tpm2.HandleEndorsement || |
| 47 |
h == tpm2.HandlePlatform || h == tpm2.HandleNull |
| 48 |
} |
| 49 |
|
| 50 |
// Handles returns a slice of tpmutil.Handle objects of all handles within |
| 51 |
// the TPM rw of type handleType. |
| 52 |
func Handles(rw io.ReadWriter, handleType tpm2.HandleType) ([]tpmutil.Handle, error) { |
| 53 |
// Handle type is determined by the most-significant octet (MSO) of the property. |
| 54 |
property := uint32(handleType) << 24 |
| 55 |
|
| 56 |
vals, moreData, err := tpm2.GetCapability(rw, tpm2.CapabilityHandles, math.MaxUint32, property) |
| 57 |
if err != nil { |
| 58 |
return nil, err |
| 59 |
} |
| 60 |
if moreData { |
| 61 |
return nil, fmt.Errorf("tpm2.GetCapability() should never return moreData==true for tpm2.CapabilityHandles") |
| 62 |
} |
| 63 |
handles := make([]tpmutil.Handle, len(vals)) |
| 64 |
for i, v := range vals { |
| 65 |
handle, ok := v.(tpmutil.Handle) |
| 66 |
if !ok { |
| 67 |
return nil, fmt.Errorf("unable to assert type tpmutil.Handle of value %#v", v) |
| 68 |
} |
| 69 |
handles[i] = handle |
| 70 |
} |
| 71 |
return handles, nil |
| 72 |
} |