Skip to content

Commit

Permalink
Add TPM.EKCertificates() method, it returns all certificates from TPM…
Browse files Browse the repository at this point in the history
…'s NVRAM
  • Loading branch information
zhsh committed Jun 22, 2023
1 parent 63dd90f commit 918de01
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
8 changes: 8 additions & 0 deletions attest/tpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
// Defined in "Registry of reserved TPM 2.0 handles and localities".
nvramRSACertIndex = 0x1c00002
nvramRSAEkNonceIndex = 0x1c00003
nvramECCCertIndex = 0x1c0000a

// Defined in "Registry of reserved TPM 2.0 handles and localities", and checked on a glinux machine.
commonSrkEquivalentHandle = 0x81000001
Expand Down Expand Up @@ -297,6 +298,7 @@ type tpmBase interface {
close() error
tpmVersion() TPMVersion
eks() ([]EK, error)
ekCertificates() ([]EK, error)
info() (*TPMInfo, error)

loadAK(opaqueBlob []byte) (*AK, error)
Expand Down Expand Up @@ -324,6 +326,12 @@ func (t *TPM) EKs() ([]EK, error) {
return t.tpm.eks()
}

// EKCertificates returns the endorsement key certificates burned-in to the platform.
// It is guaranteed that each EK.Certificate field will be populated.
func (t *TPM) EKCertificates() ([]EK, error) {
return t.tpm.ekCertificates()
}

// Info returns information about the TPM.
func (t *TPM) Info() (*TPMInfo, error) {
return t.tpm.info()
Expand Down
6 changes: 5 additions & 1 deletion attest/tpm12_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func readEKCertFromNVRAM12(ctx *tspi.Context) (*x509.Certificate, error) {
return ParseEKCertificate(ekCert)
}

func (t *trousersTPM) eks() ([]EK, error) {
func (t *trousersTPM) ekCertificates() ([]EK, error) {
cert, err := readEKCertFromNVRAM12(t.ctx)
if err != nil {
return nil, fmt.Errorf("readEKCertFromNVRAM failed: %v", err)
Expand All @@ -104,6 +104,10 @@ func (t *trousersTPM) eks() ([]EK, error) {
}, nil
}

func (t *trousersTPM) eks() ([]EK, error) {
return t.ekCertificates()
}

func (t *trousersTPM) newKey(*AK, *KeyConfig) (*Key, error) {
return nil, fmt.Errorf("not implemented")
}
Expand Down
12 changes: 12 additions & 0 deletions attest/tpm_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@ func (t *windowsTPM) info() (*TPMInfo, error) {
return &tInfo, nil
}

func (t *windowsTPM) ekCertificates() ([]EK, error) {
ekCerts, err := t.pcp.EKCerts()
if err != nil {
return nil, fmt.Errorf("could not read EKCerts: %v", err)
}
var eks []EK
for _, cert := range ekCerts {
eks = append(eks, EK{Certificate: cert, Public: cert.PublicKey})
}
return eks, nil
}

func (t *windowsTPM) eks() ([]EK, error) {
ekCerts, err := t.pcp.EKCerts()
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions attest/wrapped_tpm20.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ func (t *wrappedTPM20) getPrimaryKeyHandle(pHnd tpmutil.Handle) (tpmutil.Handle,
return pHnd, true, nil
}

func (t *wrappedTPM20) ekCertificates() ([]EK, error) {
var res []EK
if rsaCert, err := readEKCertFromNVRAM20(t.rwc, nvramRSACertIndex); err == nil {
res = append(res, EK{Public: crypto.PublicKey(rsaCert.PublicKey), Certificate: rsaCert})
}
if eccCert, err := readEKCertFromNVRAM20(t.rwc, nvramECCCertIndex); err == nil {
res = append(res, EK{Public: crypto.PublicKey(eccCert.PublicKey), Certificate: eccCert})
}
return res, nil
}

func (t *wrappedTPM20) eks() ([]EK, error) {
if cert, err := readEKCertFromNVRAM20(t.rwc, nvramRSACertIndex); err == nil {
return []EK{
Expand Down

0 comments on commit 918de01

Please sign in to comment.