Skip to content

Commit 28f38e7

Browse files
shjalarouming
authored andcommitted
tpmmgr/tpm : move tests from tpmmgr to tpm
Adds tpm_test.go and moves tests that are local to tpm from tpmmgr to it. Signed-off-by: Shahriyar Jalayeri <[email protected]>
1 parent a0413e3 commit 28f38e7

File tree

3 files changed

+181
-168
lines changed

3 files changed

+181
-168
lines changed

pkg/pillar/cmd/tpmmgr/tpmmgr_test.go

Lines changed: 0 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,15 @@
66
package tpmmgr
77

88
import (
9-
"bytes"
10-
"crypto/sha256"
119
"crypto/x509"
1210
"encoding/pem"
1311
"fmt"
1412
"io/ioutil"
1513
"os"
16-
"reflect"
17-
"strings"
1814
"testing"
1915
"time"
2016

21-
"github.com/google/go-tpm/tpm2"
22-
"github.com/google/go-tpm/tpmutil"
2317
etpm "github.com/lf-edge/eve/pkg/pillar/evetpm"
24-
fileutils "github.com/lf-edge/eve/pkg/pillar/utils/file"
2518
)
2619

2720
const ecdhCertPem = `
@@ -208,140 +201,3 @@ func TestVerifyEdgeNodeCerts(t *testing.T) {
208201
return
209202
}
210203
}
211-
212-
func TestSealUnseal(t *testing.T) {
213-
_, err := os.Stat(etpm.TpmDevicePath)
214-
if err != nil {
215-
t.Skip("TPM is not available, skipping the test.")
216-
}
217-
218-
dataToSeal := []byte("secret")
219-
if err := etpm.SealDiskKey(dataToSeal, etpm.DiskKeySealingPCRs); err != nil {
220-
t.Errorf("Seal operation failed with err: %v", err)
221-
return
222-
}
223-
unsealedData, err := etpm.UnsealDiskKey(etpm.DiskKeySealingPCRs)
224-
if err != nil {
225-
t.Errorf("Unseal operation failed with err: %v", err)
226-
return
227-
}
228-
if !reflect.DeepEqual(dataToSeal, unsealedData) {
229-
t.Errorf("Seal/Unseal operation failed, want %v, but got %v", dataToSeal, unsealedData)
230-
}
231-
}
232-
233-
func TestSealUnsealMismatchReport(t *testing.T) {
234-
_, err := os.Stat(etpm.TpmDevicePath)
235-
if err != nil {
236-
t.Skip("TPM is not available, skipping the test.")
237-
}
238-
239-
rw, err := tpm2.OpenTPM(etpm.TpmDevicePath)
240-
if err != nil {
241-
t.Errorf("OpenTPM failed with err: %v", err)
242-
return
243-
}
244-
defer rw.Close()
245-
246-
dataToSeal := []byte("secret")
247-
if err := etpm.SealDiskKey(dataToSeal, etpm.DiskKeySealingPCRs); err != nil {
248-
t.Errorf("Seal operation failed with err: %v", err)
249-
return
250-
}
251-
252-
pcrIndexes := [3]int{1, 7, 8}
253-
pcrValue := bytes.Repeat([]byte{0xF}, sha256.Size)
254-
for _, pcr := range pcrIndexes {
255-
if err = tpm2.PCRExtend(rw, tpmutil.Handle(pcr), tpm2.AlgSHA256, pcrValue, ""); err != nil {
256-
t.Errorf("Failed to extend PCR %d: %s", pcr, err)
257-
return
258-
}
259-
}
260-
261-
_, err = etpm.UnsealDiskKey(etpm.DiskKeySealingPCRs)
262-
if err == nil {
263-
t.Errorf("Expected error from UnsealDiskKey, got nil")
264-
return
265-
}
266-
267-
if !strings.Contains(err.Error(), "[1 7 8]") {
268-
t.Errorf("UnsealDiskKey expected to report mismatching PCR indexes, got : %v", err)
269-
return
270-
}
271-
}
272-
273-
func TestSealUnsealTpmEventLogCollect(t *testing.T) {
274-
_, err := os.Stat(etpm.TpmDevicePath)
275-
if err != nil {
276-
t.Skip("TPM is not available, skipping the test.")
277-
}
278-
279-
rw, err := tpm2.OpenTPM(etpm.TpmDevicePath)
280-
if err != nil {
281-
t.Errorf("OpenTPM failed with err: %v", err)
282-
return
283-
}
284-
defer rw.Close()
285-
286-
// this should write the save the first event log
287-
dataToSeal := []byte("secret")
288-
if err := etpm.SealDiskKey(dataToSeal, etpm.DiskKeySealingPCRs); err != nil {
289-
t.Errorf("Seal operation failed with err: %v", err)
290-
return
291-
}
292-
293-
// this won't write to event log, but still triggers saving it on unseal.
294-
pcrValue := bytes.Repeat([]byte{0xF}, sha256.Size)
295-
if err = tpm2.PCRExtend(rw, tpmutil.Handle(1), tpm2.AlgSHA256, pcrValue, ""); err != nil {
296-
t.Errorf("Failed to extend PCR[1]: %v", err)
297-
return
298-
}
299-
300-
// this should fail and result in saving the second tpm event log
301-
_, err = etpm.UnsealDiskKey(etpm.DiskKeySealingPCRs)
302-
if err == nil {
303-
t.Errorf("Expected error from UnsealDiskKey, got nil")
304-
return
305-
}
306-
307-
// just check for tpm0
308-
sealSuccess := fmt.Sprintf(etpm.TpmEvtLogSavePattern, etpm.MeasurementLogSealSuccess, 0)
309-
sealFail := fmt.Sprintf(etpm.TpmEvtLogSavePattern, etpm.MeasurementLogUnsealFail, 0)
310-
if !fileutils.FileExists(nil, sealSuccess) {
311-
t.Errorf("TPM event log \"%s\" not found, Expected to be saved", sealSuccess)
312-
return
313-
}
314-
if !fileutils.FileExists(nil, sealFail) {
315-
t.Errorf("TPM event log \"%s\" not found, Expected to be saved", sealFail)
316-
return
317-
}
318-
319-
// this should trigger collecting previous tpm event logs
320-
if err := etpm.SealDiskKey(dataToSeal, etpm.DiskKeySealingPCRs); err != nil {
321-
t.Errorf("Seal operation failed with err: %v", err)
322-
return
323-
}
324-
325-
// current event log should exist
326-
if !fileutils.FileExists(nil, sealSuccess) {
327-
t.Errorf("TPM event log \"%s\" not found, Expected to be saved", sealSuccess)
328-
return
329-
}
330-
// this shouldn't exist because SealDiskKey will do a clean up
331-
if fileutils.FileExists(nil, sealFail) {
332-
t.Errorf("TPM event log \"%s\" found, Expected to not exist", sealFail)
333-
return
334-
}
335-
336-
// collected event logs both should exist
337-
prevSealSuccess := fmt.Sprintf(etpm.TpmEvtLogCollectPattern, sealSuccess)
338-
prevSealFail := fmt.Sprintf(etpm.TpmEvtLogCollectPattern, sealFail)
339-
if !fileutils.FileExists(nil, prevSealSuccess) {
340-
t.Errorf("TPM event log \"%s\" not found, Expected to be collected", prevSealSuccess)
341-
return
342-
}
343-
if !fileutils.FileExists(nil, prevSealFail) {
344-
t.Errorf("TPM event log \"%s\" not found, Expected to be collected", prevSealFail)
345-
return
346-
}
347-
}

pkg/pillar/evetpm/tpm.go

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,17 @@ const (
7575
EmptyPassword = ""
7676
vaultKeyLength = 32 //Bytes
7777

78-
// TpmSavedDiskSealingPcrs is the file that holds a copy of PCR values
78+
// savedSealingPcrsFile is the file that holds a copy of PCR values
7979
// at the time of generating and sealing the disk key into the TPM.
80-
TpmSavedDiskSealingPcrs = types.PersistStatusDir + "/sealingpcrs"
80+
savedSealingPcrsFile = types.PersistStatusDir + "/sealingpcrs"
8181

82-
// MeasurementLogSealSuccess is files that holds a copy of event log at the time
82+
// measurementLogSealSuccess is files that holds a copy of event log at the time
8383
// of generating/sealing the disk key into the TPM.
84-
MeasurementLogSealSuccess = types.PersistStatusDir + "/tpm_measurement_seal_success"
84+
measurementLogSealSuccess = types.PersistStatusDir + "/tpm_measurement_seal_success"
8585

86-
// MeasurementLogUnsealFail is files that holds a copy of event log at the time EVE
86+
// measurementLogUnsealFail is files that holds a copy of event log at the time EVE
8787
// fails to unseal the vault key from TPM.
88-
MeasurementLogUnsealFail = types.PersistStatusDir + "/tpm_measurement_unseal_fail"
89-
90-
// TpmEvtLogSavePattern contains the pattern used to save event log files,
91-
// considering the case when multiple tpm devices are present.
92-
TpmEvtLogSavePattern = "%s-tpm%d"
93-
94-
// TpmEvtLogCollectPattern contains the pattern used to collect event log files.
95-
TpmEvtLogCollectPattern = "%s-backup"
88+
measurementLogUnsealFail = types.PersistStatusDir + "/tpm_measurement_unseal_fail"
9689

9790
// measurementLogFile is a kernel exposed variable that contains the
9891
// TPM measurements and events log.
@@ -653,7 +646,7 @@ func SealDiskKey(key []byte, pcrSel tpm2.PCRSelection) error {
653646
}
654647

655648
// save a snapshot of current PCR values
656-
if err := saveDiskKeySealingPCRs(TpmSavedDiskSealingPcrs); err != nil {
649+
if err := saveDiskKeySealingPCRs(savedSealingPcrsFile); err != nil {
657650
return fmt.Errorf("saving snapshot of sealing PCRs failed: %w", err)
658651
}
659652

@@ -673,7 +666,7 @@ func SealDiskKey(key []byte, pcrSel tpm2.PCRSelection) error {
673666
}
674667

675668
// save a copy of the current measurement log
676-
if err := copyMeasurementLog(MeasurementLogSealSuccess); err != nil {
669+
if err := copyMeasurementLog(measurementLogSealSuccess); err != nil {
677670
return fmt.Errorf("copying current TPM measurement log failed: %w", err)
678671
}
679672

@@ -735,14 +728,14 @@ func UnsealDiskKey(pcrSel tpm2.PCRSelection) ([]byte, error) {
735728
// We get here mostly because of RCPolicyFail error, so first try to save
736729
// a copy of TPM measurement log, it comes handy for diagnosing the issue.
737730
evtLogStat := "copied (failed unseal) TPM measurement log"
738-
if errEvtLog := copyMeasurementLog(MeasurementLogUnsealFail); errEvtLog != nil {
731+
if errEvtLog := copyMeasurementLog(measurementLogUnsealFail); errEvtLog != nil {
739732
// just report the failure, still give findMismatchingPCRs a chance so
740733
// we can at least have some partial information about why unseal failed.
741734
evtLogStat = fmt.Sprintf("copying (failed unseal) TPM measurement log failed: %v", errEvtLog)
742735
}
743736

744737
// try to find out the mismatching PCR index
745-
mismatch, errPcrMiss := findMismatchingPCRs(TpmSavedDiskSealingPcrs)
738+
mismatch, errPcrMiss := findMismatchingPCRs(savedSealingPcrsFile)
746739
if errPcrMiss != nil {
747740
return nil, fmt.Errorf("UnsealWithSession failed: %w, %s, finding mismatching PCR failed: %v", err, evtLogStat, errPcrMiss)
748741
}
@@ -857,6 +850,14 @@ func pcrBankSHA256EnabledHelper() bool {
857850
return err == nil
858851
}
859852

853+
func getLogCopyPath(destination string, tpmIndex int) string {
854+
return fmt.Sprintf("%s-tpm%d", destination, tpmIndex)
855+
}
856+
857+
func getLogBackupPath(destination string) string {
858+
return fmt.Sprintf("%s-backup", destination)
859+
}
860+
860861
func getMappedTpmsPath() ([]string, error) {
861862
paths, err := filepath.Glob(syfsTpmDir)
862863
if err != nil {
@@ -902,11 +903,11 @@ func backupCopiedMeasurementLogs() error {
902903

903904
leftToBackup := counted
904905
for i := 0; i < counted; i++ {
905-
sealSuccessPath := fmt.Sprintf(TpmEvtLogSavePattern, MeasurementLogSealSuccess, i)
906-
unsealFailPath := fmt.Sprintf(TpmEvtLogSavePattern, MeasurementLogUnsealFail, i)
906+
sealSuccessPath := getLogCopyPath(measurementLogSealSuccess, i)
907+
unsealFailPath := getLogCopyPath(measurementLogUnsealFail, i)
907908
if fileutils.FileExists(nil, sealSuccessPath) && fileutils.FileExists(nil, unsealFailPath) {
908-
sealSuccessBackupPath := fmt.Sprintf(TpmEvtLogCollectPattern, sealSuccessPath)
909-
unsealFailBackupPath := fmt.Sprintf(TpmEvtLogCollectPattern, unsealFailPath)
909+
sealSuccessBackupPath := getLogBackupPath(sealSuccessPath)
910+
unsealFailBackupPath := getLogBackupPath(unsealFailPath)
910911
if err := os.Rename(sealSuccessPath, sealSuccessBackupPath); err != nil {
911912
fmt.Fprintf(os.Stderr, "failed to backup tpm%d \"seal success\" previously copied measurement log: %v", i, err)
912913
continue
@@ -935,8 +936,8 @@ func removeCopiedMeasurementLogs() error {
935936
}
936937

937938
for i := 0; i < counted; i++ {
938-
sealSuccessPath := fmt.Sprintf(TpmEvtLogSavePattern, MeasurementLogSealSuccess, i)
939-
unsealFailPath := fmt.Sprintf(TpmEvtLogSavePattern, MeasurementLogUnsealFail, i)
939+
sealSuccessPath := getLogCopyPath(measurementLogSealSuccess, i)
940+
unsealFailPath := getLogCopyPath(measurementLogUnsealFail, i)
940941
_ = os.Remove(sealSuccessPath)
941942
_ = os.Remove(unsealFailPath)
942943
}
@@ -958,7 +959,7 @@ func copyMeasurementLog(dstPath string) error {
958959
continue
959960
}
960961

961-
copyPath := fmt.Sprintf(TpmEvtLogSavePattern, dstPath, i)
962+
copyPath := getLogCopyPath(dstPath, i)
962963
err = fileutils.WriteRename(copyPath, measurementLogContent)
963964
if err != nil {
964965
fmt.Fprintf(os.Stderr, "failed to copy stored measurement log file: %v", err)

0 commit comments

Comments
 (0)