| 1 |
package test |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/binary" |
| 5 |
"io" |
| 6 |
"sync" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/google/go-attestation/attest" |
| 10 |
"github.com/google/go-tpm-tools/simulator" |
| 11 |
"github.com/google/go-tpm/legacy/tpm2" |
| 12 |
gtpm2 "github.com/google/go-tpm/tpm2" |
| 13 |
"github.com/google/go-tpm/tpmutil" |
| 14 |
) |
| 15 |
|
| 16 |
// Only open the TPM device once. Reopening the device causes issues on Linux. |
| 17 |
var ( |
| 18 |
tpm io.ReadWriteCloser |
| 19 |
lock sync.Mutex |
| 20 |
) |
| 21 |
|
| 22 |
// PCR registers that are OK to use in tests (can be reset without reboot) |
| 23 |
var ( |
| 24 |
DebugPCR = 16 |
| 25 |
ApplicationPCR = 23 |
| 26 |
) |
| 27 |
|
| 28 |
type noClose struct { |
| 29 |
io.ReadWriter |
| 30 |
} |
| 31 |
|
| 32 |
func (n noClose) Close() error { |
| 33 |
return nil |
| 34 |
} |
| 35 |
|
| 36 |
type simulatedTpm struct { |
| 37 |
io.ReadWriteCloser |
| 38 |
eventLog []byte |
| 39 |
} |
| 40 |
|
| 41 |
func (s simulatedTpm) EventLog() ([]byte, error) { |
| 42 |
return s.eventLog, nil |
| 43 |
} |
| 44 |
|
| 45 |
// SkipOnUnsupportedAlg skips the test if the algorithm is not found in the TPM |
| 46 |
// capability. |
| 47 |
func SkipOnUnsupportedAlg(t testing.TB, rw io.ReadWriter, alg tpm2.Algorithm) { |
| 48 |
moreData := true |
| 49 |
for i := uint32(0); moreData; i++ { |
| 50 |
var err error |
| 51 |
var descs []interface{} |
| 52 |
descs, moreData, err = tpm2.GetCapability(rw, tpm2.CapabilityAlgs, 1, i) |
| 53 |
if err != nil { |
| 54 |
t.Fatalf("Could not get TPM algorithm capability: %v", err) |
| 55 |
} |
| 56 |
for _, desc := range descs { |
| 57 |
if desc.(tpm2.AlgorithmDescription).ID == alg { |
| 58 |
return |
| 59 |
} |
| 60 |
} |
| 61 |
if !moreData { |
| 62 |
break |
| 63 |
} |
| 64 |
} |
| 65 |
t.Skipf("Algorithm %v is not supported by the TPM", alg) |
| 66 |
} |
| 67 |
|
| 68 |
// GetTPM is a cross-platform testing helper function that retrives the |
| 69 |
// appropriate TPM device from the flags passed into "go test". |
| 70 |
// |
| 71 |
// If using a test TPM, this will also retrieve a test eventlog. In this case, |
| 72 |
// GetTPM extends the test event log's events into the test TPM. |
| 73 |
func GetTPM(tb testing.TB) io.ReadWriteCloser { |
| 74 |
tb.Helper() |
| 75 |
if !useRealTPM() { |
| 76 |
return GetSimulatorWithLog(tb, Rhel8EventLog) |
| 77 |
} |
| 78 |
|
| 79 |
lock.Lock() |
| 80 |
defer lock.Unlock() |
| 81 |
if tpm == nil { |
| 82 |
var err error |
| 83 |
if tpm, err = getRealTPM(); err != nil { |
| 84 |
tb.Fatalf("Failed to open TPM: %v", err) |
| 85 |
} |
| 86 |
} |
| 87 |
return noClose{tpm} |
| 88 |
} |
| 89 |
|
| 90 |
// SkipForRealTPM causes a test or benchmark to be skipped if we are not using |
| 91 |
// a test TPM. This lets us avoid clobbering important PCRs on a real machine. |
| 92 |
func SkipForRealTPM(tb testing.TB) { |
| 93 |
if useRealTPM() { |
| 94 |
tb.Skip("Running against a real TPM, Skipping Test") |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
// GetSimulatorWithLog returns a simulated TPM with PCRs that match the events |
| 99 |
// of the passed in eventlog. This allows for testing attestation flows. |
| 100 |
func GetSimulatorWithLog(tb testing.TB, eventLog []byte) io.ReadWriteCloser { |
| 101 |
simulator, err := simulator.Get() |
| 102 |
if err != nil { |
| 103 |
tb.Fatalf("Simulator initialization failed: %v", err) |
| 104 |
} |
| 105 |
// Make sure that whatever happens, we close the simulator |
| 106 |
tb.Cleanup(func() { |
| 107 |
if !simulator.IsClosed() { |
| 108 |
tb.Error("simulator was not properly closed") |
| 109 |
if err := simulator.Close(); err != nil { |
| 110 |
tb.Errorf("when closing simulator: %v", err) |
| 111 |
} |
| 112 |
} |
| 113 |
}) |
| 114 |
|
| 115 |
// Extend event log events on simulator TPM. |
| 116 |
simulateEventLogEvents(tb, simulator, eventLog) |
| 117 |
return simulatedTpm{simulator, eventLog} |
| 118 |
} |
| 119 |
|
| 120 |
// simulateEventLogEvents simulates the events in the test event log |
| 121 |
// "server/test/ubuntu-2104-event-log" by parsing the log |
| 122 |
// and manually extending the TPM. |
| 123 |
func simulateEventLogEvents(tb testing.TB, rw io.ReadWriter, eventLog []byte) { |
| 124 |
attestEventLog, err := attest.ParseEventLog(eventLog) |
| 125 |
if err != nil { |
| 126 |
tb.Fatalf("Failed to parse test event log: %v", err) |
| 127 |
} |
| 128 |
|
| 129 |
// TODO: The Ubuntu 21.04 event log also includes SHA384, but this is not yet |
| 130 |
// supported by go-attestation or go-tpm-tools. |
| 131 |
hashAlgs := map[tpm2.Algorithm]attest.HashAlg{ |
| 132 |
tpm2.AlgSHA1: attest.HashSHA1, |
| 133 |
tpm2.AlgSHA256: attest.HashSHA256, |
| 134 |
} |
| 135 |
|
| 136 |
for tpm2Alg, attestAlg := range hashAlgs { |
| 137 |
events := attestEventLog.Events(attestAlg) |
| 138 |
for _, event := range events { |
| 139 |
// EV_NO_ACTION |
| 140 |
if event.Type == 0x03 { |
| 141 |
continue |
| 142 |
} |
| 143 |
extendOnePcr(tb, rw, event.Index, tpm2Alg, event.Digest) |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
func extendOnePcr(tb testing.TB, rw io.ReadWriter, pcr int, hashAlg tpm2.Algorithm, hash []byte) { |
| 149 |
err := tpm2.PCRExtend(rw, tpmutil.Handle(pcr), hashAlg, hash, "") |
| 150 |
if err != nil { |
| 151 |
tb.Fatalf("PCRExtend failed: %v", err) |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
// CreateTpm2EventLog generates a sample event log that is based on gceConfidentialTechnology |
| 156 |
func CreateTpm2EventLog(gceConfidentialTechnologyEnum byte) []byte { |
| 157 |
pcr0 := uint32(0) |
| 158 |
algorithms := []gtpm2.TPMIAlgHash{gtpm2.TPMAlgSHA1, gtpm2.TPMAlgSHA256, gtpm2.TPMAlgSHA384} |
| 159 |
specEventInfo := []byte{ |
| 160 |
'S', 'p', 'e', 'c', ' ', 'I', 'D', ' ', 'E', 'v', 'e', 'n', 't', '0', '3', 0, |
| 161 |
0, 0, 0, 0, // platformClass |
| 162 |
0, // specVersionMinor, |
| 163 |
2, // specVersionMajor, |
| 164 |
0, // specErrata |
| 165 |
2, // uintnSize |
| 166 |
byte(len(algorithms)), 0, 0, 0} // NumberOfAlgorithms |
| 167 |
for _, alg := range algorithms { |
| 168 |
var algInfo [4]byte |
| 169 |
algo, _ := alg.Hash() |
| 170 |
binary.LittleEndian.PutUint16(algInfo[0:2], uint16(alg)) |
| 171 |
binary.LittleEndian.PutUint16(algInfo[2:4], uint16(algo.Size())) |
| 172 |
specEventInfo = append(specEventInfo, algInfo[:]...) |
| 173 |
} |
| 174 |
vendorInfoSize := byte(0) |
| 175 |
specEventInfo = append(specEventInfo, vendorInfoSize) |
| 176 |
|
| 177 |
specEventHeader := make([]byte, 32) |
| 178 |
evNoAction := uint32(0x03) |
| 179 |
binary.LittleEndian.PutUint32(specEventHeader[0:4], pcr0) |
| 180 |
binary.LittleEndian.PutUint32(specEventHeader[4:8], evNoAction) |
| 181 |
binary.LittleEndian.PutUint32(specEventHeader[28:32], uint32(len(specEventInfo))) |
| 182 |
specEvent := append(specEventHeader, specEventInfo...) |
| 183 |
|
| 184 |
// After the Spec ID Event, all events must use all the specified digest algorithms. |
| 185 |
extendHashes := func(buffer []byte, info []byte) []byte { |
| 186 |
var numberOfDigests [4]byte |
| 187 |
binary.LittleEndian.PutUint32(numberOfDigests[:], uint32(len(algorithms))) |
| 188 |
buffer = append(buffer, numberOfDigests[:]...) |
| 189 |
for _, alg := range algorithms { |
| 190 |
algo, _ := alg.Hash() |
| 191 |
digest := make([]byte, 2+algo.Size()) |
| 192 |
binary.LittleEndian.PutUint16(digest[0:2], uint16(alg)) |
| 193 |
h := algo.New() |
| 194 |
h.Write(info) |
| 195 |
copy(digest[2:], h.Sum(nil)) |
| 196 |
buffer = append(buffer, digest...) |
| 197 |
} |
| 198 |
return buffer |
| 199 |
} |
| 200 |
writeTpm2Event := func(buffer []byte, pcr uint32, eventType uint32, info []byte) []byte { |
| 201 |
header := make([]byte, 8) |
| 202 |
binary.LittleEndian.PutUint32(header[0:4], pcr) |
| 203 |
binary.LittleEndian.PutUint32(header[4:8], eventType) |
| 204 |
buffer = append(buffer, header...) |
| 205 |
|
| 206 |
buffer = extendHashes(buffer, info) |
| 207 |
|
| 208 |
var eventSize [4]byte |
| 209 |
binary.LittleEndian.PutUint32(eventSize[:], uint32(len(info))) |
| 210 |
buffer = append(buffer, eventSize[:]...) |
| 211 |
|
| 212 |
return append(buffer, info...) |
| 213 |
} |
| 214 |
evSCRTMversion := uint32(0x08) |
| 215 |
versionEventInfo := []byte{ |
| 216 |
'G', 0, 'C', 0, 'E', 0, ' ', 0, |
| 217 |
'V', 0, 'i', 0, 'r', 0, 't', 0, 'u', 0, 'a', 0, 'l', 0, ' ', 0, |
| 218 |
'F', 0, 'i', 0, 'r', 0, 'm', 0, 'w', 0, 'a', 0, 'r', 0, 'e', 0, ' ', 0, |
| 219 |
'v', 0, '1', 0, 0, 0} |
| 220 |
withVersionEvent := writeTpm2Event(specEvent, pcr0, evSCRTMversion, versionEventInfo) |
| 221 |
|
| 222 |
nonHostEventInfo := []byte{ |
| 223 |
'G', 'C', 'E', ' ', 'N', 'o', 'n', 'H', 'o', 's', 't', 'I', 'n', 'f', 'o', 0, |
| 224 |
gceConfidentialTechnologyEnum, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} |
| 225 |
evNonHostInfo := uint32(0x11) |
| 226 |
return writeTpm2Event(withVersionEvent, pcr0, evNonHostInfo, nonHostEventInfo) |
| 227 |
} |