2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
package vtpm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2022-08-12 09:59:45 -04:00
|
|
|
"fmt"
|
2022-03-22 11:03:15 -04:00
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
tpmclient "github.com/google/go-tpm-tools/client"
|
|
|
|
"github.com/google/go-tpm-tools/proto/attest"
|
|
|
|
"github.com/google/go-tpm-tools/proto/tpm"
|
|
|
|
"github.com/google/go-tpm/tpm2"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-03-09 09:23:42 -05:00
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/simulator"
|
|
|
|
tpmsim "github.com/edgelesssys/constellation/v2/internal/attestation/simulator"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type simTPMWithEventLog struct {
|
2023-03-09 09:23:42 -05:00
|
|
|
io.ReadWriteCloser
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func newSimTPMWithEventLog() (io.ReadWriteCloser, error) {
|
2023-03-09 09:23:42 -05:00
|
|
|
tpmSim, err := simulator.OpenSimulatedTPM()
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &simTPMWithEventLog{tpmSim}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EventLog overrides the default event log getter.
|
|
|
|
func (s simTPMWithEventLog) EventLog() ([]byte, error) {
|
|
|
|
// event log header for successful parsing of event log
|
|
|
|
header := []byte{
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x00, 0x00, 0x00, 0x53, 0x70, 0x65,
|
|
|
|
0x63, 0x20, 0x49, 0x44, 0x20, 0x45, 0x76, 0x65, 0x6E, 0x74, 0x30, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x02, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x14, 0x00, 0x0B, 0x00, 0x20, 0x00, 0x00,
|
|
|
|
}
|
|
|
|
return header, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fakeGetInstanceInfo(tpm io.ReadWriteCloser) ([]byte, error) {
|
|
|
|
return []byte("unit-test"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidate(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
2023-03-06 03:15:52 -05:00
|
|
|
fakeValidateCVM := func(AttestationDocument, *attest.MachineState) error { return nil }
|
2022-08-12 09:59:45 -04:00
|
|
|
fakeGetTrustedKey := func(aKPub, instanceInfo []byte) (crypto.PublicKey, error) {
|
|
|
|
pubArea, err := tpm2.DecodePublic(aKPub)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return pubArea.Key()
|
|
|
|
}
|
|
|
|
|
2022-11-24 04:57:58 -05:00
|
|
|
testExpectedPCRs := measurements.M{
|
2023-02-12 11:26:54 -05:00
|
|
|
0: measurements.WithAllBytes(0x00, false),
|
|
|
|
1: measurements.WithAllBytes(0x00, false),
|
|
|
|
uint32(measurements.PCRIndexClusterID): measurements.WithAllBytes(0x00, false),
|
2022-08-12 09:59:45 -04:00
|
|
|
}
|
2022-10-14 10:29:21 -04:00
|
|
|
warnLog := &testAttestationLogger{}
|
2022-08-12 09:59:45 -04:00
|
|
|
|
2023-02-12 11:26:54 -05:00
|
|
|
tpmOpen, tpmCloser := tpmsim.NewSimulatedTPMOpenFunc()
|
|
|
|
defer tpmCloser.Close()
|
|
|
|
|
2023-02-28 10:34:18 -05:00
|
|
|
issuer := NewIssuer(tpmOpen, tpmclient.AttestationKeyRSA, fakeGetInstanceInfo, logger.NewTest(t))
|
2023-02-12 11:33:33 -05:00
|
|
|
validator := NewValidator(testExpectedPCRs, fakeGetTrustedKey, fakeValidateCVM, nil)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
nonce := []byte{1, 2, 3, 4}
|
|
|
|
challenge := []byte("Constellation")
|
|
|
|
|
|
|
|
attDocRaw, err := issuer.Issue(challenge, nonce)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
var attDoc AttestationDocument
|
|
|
|
err = json.Unmarshal(attDocRaw, &attDoc)
|
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(challenge, attDoc.UserData)
|
|
|
|
|
|
|
|
// valid test
|
|
|
|
out, err := validator.Validate(attDocRaw, nonce)
|
|
|
|
require.NoError(err)
|
|
|
|
require.Equal(challenge, out)
|
|
|
|
|
2023-02-12 11:26:54 -05:00
|
|
|
// validation must fail after bootstrapping (change of enforced PCR)
|
|
|
|
require.NoError(MarkNodeAsBootstrapped(tpmOpen, []byte{2}))
|
|
|
|
attDocBootstrappedRaw, err := issuer.Issue(challenge, nonce)
|
|
|
|
require.NoError(err)
|
|
|
|
_, err = validator.Validate(attDocBootstrappedRaw, nonce)
|
|
|
|
require.Error(err)
|
|
|
|
|
|
|
|
// userData must be bound to PCR state
|
|
|
|
attDocBootstrappedRaw, err = issuer.Issue([]byte{2, 3}, nonce)
|
|
|
|
require.NoError(err)
|
|
|
|
var attDocBootstrapped AttestationDocument
|
|
|
|
require.NoError(json.Unmarshal(attDocBootstrappedRaw, &attDocBootstrapped))
|
|
|
|
attDocBootstrapped.Attestation = attDoc.Attestation
|
|
|
|
attDocBootstrappedRaw, err = json.Marshal(attDocBootstrapped)
|
|
|
|
require.NoError(err)
|
|
|
|
_, err = validator.Validate(attDocBootstrappedRaw, nonce)
|
|
|
|
require.Error(err)
|
|
|
|
|
2022-11-24 04:57:58 -05:00
|
|
|
expectedPCRs := measurements.M{
|
|
|
|
0: measurements.WithAllBytes(0x00, true),
|
|
|
|
1: measurements.WithAllBytes(0x00, true),
|
|
|
|
2: measurements.Measurement{
|
|
|
|
Expected: [32]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20},
|
|
|
|
WarnOnly: true,
|
|
|
|
},
|
|
|
|
3: measurements.Measurement{
|
|
|
|
Expected: [32]byte{0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40},
|
|
|
|
WarnOnly: true,
|
|
|
|
},
|
|
|
|
4: measurements.Measurement{
|
|
|
|
Expected: [32]byte{0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60},
|
|
|
|
WarnOnly: true,
|
|
|
|
},
|
|
|
|
5: measurements.Measurement{
|
|
|
|
Expected: [32]byte{0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80},
|
|
|
|
WarnOnly: true,
|
|
|
|
},
|
2022-08-12 09:59:45 -04:00
|
|
|
}
|
|
|
|
warningValidator := NewValidator(
|
|
|
|
expectedPCRs,
|
|
|
|
fakeGetTrustedKey,
|
|
|
|
fakeValidateCVM,
|
2022-08-29 10:41:09 -04:00
|
|
|
warnLog,
|
2022-08-12 09:59:45 -04:00
|
|
|
)
|
|
|
|
out, err = warningValidator.Validate(attDocRaw, nonce)
|
|
|
|
require.NoError(err)
|
|
|
|
assert.Equal(t, challenge, out)
|
2022-11-24 04:57:58 -05:00
|
|
|
assert.Len(t, warnLog.warnings, 4)
|
2022-08-12 09:59:45 -04:00
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
validator *Validator
|
|
|
|
attDoc []byte
|
|
|
|
nonce []byte
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
2023-02-12 11:26:54 -05:00
|
|
|
"valid": {
|
2023-02-12 11:33:33 -05:00
|
|
|
validator: NewValidator(testExpectedPCRs, fakeGetTrustedKey, fakeValidateCVM, warnLog),
|
2023-02-12 11:26:54 -05:00
|
|
|
attDoc: mustMarshalAttestation(attDoc, require),
|
|
|
|
nonce: nonce,
|
|
|
|
},
|
2022-03-22 11:03:15 -04:00
|
|
|
"invalid nonce": {
|
2023-02-12 11:33:33 -05:00
|
|
|
validator: NewValidator(testExpectedPCRs, fakeGetTrustedKey, fakeValidateCVM, warnLog),
|
2022-04-26 10:54:05 -04:00
|
|
|
attDoc: mustMarshalAttestation(attDoc, require),
|
|
|
|
nonce: []byte{4, 3, 2, 1},
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"invalid signature": {
|
2023-02-12 11:33:33 -05:00
|
|
|
validator: NewValidator(testExpectedPCRs, fakeGetTrustedKey, fakeValidateCVM, warnLog),
|
2022-03-22 11:03:15 -04:00
|
|
|
attDoc: mustMarshalAttestation(AttestationDocument{
|
2023-02-12 11:26:54 -05:00
|
|
|
Attestation: attDoc.Attestation,
|
|
|
|
InstanceInfo: attDoc.InstanceInfo,
|
|
|
|
UserData: []byte("wrong data"),
|
2022-03-22 11:03:15 -04:00
|
|
|
}, require),
|
2022-04-26 10:54:05 -04:00
|
|
|
nonce: nonce,
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"untrusted attestation public key": {
|
|
|
|
validator: NewValidator(
|
2022-08-12 09:59:45 -04:00
|
|
|
testExpectedPCRs,
|
2022-03-22 11:03:15 -04:00
|
|
|
func(akPub, instanceInfo []byte) (crypto.PublicKey, error) {
|
|
|
|
return nil, errors.New("untrusted")
|
|
|
|
},
|
2023-02-12 11:33:33 -05:00
|
|
|
fakeValidateCVM, warnLog),
|
2022-04-26 10:54:05 -04:00
|
|
|
attDoc: mustMarshalAttestation(attDoc, require),
|
|
|
|
nonce: nonce,
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"not a CVM": {
|
|
|
|
validator: NewValidator(
|
2022-08-12 09:59:45 -04:00
|
|
|
testExpectedPCRs,
|
2022-03-22 11:03:15 -04:00
|
|
|
fakeGetTrustedKey,
|
2023-03-06 03:15:52 -05:00
|
|
|
func(AttestationDocument, *attest.MachineState) error {
|
2022-03-22 11:03:15 -04:00
|
|
|
return errors.New("untrusted")
|
|
|
|
},
|
2023-02-12 11:33:33 -05:00
|
|
|
warnLog),
|
2022-04-26 10:54:05 -04:00
|
|
|
attDoc: mustMarshalAttestation(attDoc, require),
|
|
|
|
nonce: nonce,
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"untrusted PCRs": {
|
|
|
|
validator: NewValidator(
|
2022-11-24 04:57:58 -05:00
|
|
|
measurements.M{
|
|
|
|
0: measurements.Measurement{
|
|
|
|
Expected: [32]byte{0xFF},
|
|
|
|
WarnOnly: false,
|
|
|
|
},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
fakeGetTrustedKey,
|
|
|
|
fakeValidateCVM,
|
2023-02-12 11:33:33 -05:00
|
|
|
warnLog),
|
2022-04-26 10:54:05 -04:00
|
|
|
attDoc: mustMarshalAttestation(attDoc, require),
|
|
|
|
nonce: nonce,
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"no sha256 quote": {
|
2023-02-12 11:33:33 -05:00
|
|
|
validator: NewValidator(testExpectedPCRs, fakeGetTrustedKey, fakeValidateCVM, warnLog),
|
2022-03-22 11:03:15 -04:00
|
|
|
attDoc: mustMarshalAttestation(AttestationDocument{
|
|
|
|
Attestation: &attest.Attestation{
|
|
|
|
AkPub: attDoc.Attestation.AkPub,
|
|
|
|
Quotes: []*tpm.Quote{
|
|
|
|
attDoc.Attestation.Quotes[2],
|
|
|
|
},
|
|
|
|
EventLog: attDoc.Attestation.EventLog,
|
|
|
|
InstanceInfo: attDoc.Attestation.InstanceInfo,
|
|
|
|
},
|
2023-02-12 11:26:54 -05:00
|
|
|
InstanceInfo: attDoc.InstanceInfo,
|
|
|
|
UserData: attDoc.UserData,
|
2022-03-22 11:03:15 -04:00
|
|
|
}, require),
|
2022-04-26 10:54:05 -04:00
|
|
|
nonce: nonce,
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"invalid attestation document": {
|
2023-02-12 11:33:33 -05:00
|
|
|
validator: NewValidator(testExpectedPCRs, fakeGetTrustedKey, fakeValidateCVM, warnLog),
|
2022-04-26 10:54:05 -04:00
|
|
|
attDoc: []byte("invalid attestation"),
|
|
|
|
nonce: nonce,
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
_, err = tc.validator.Validate(tc.attDoc, tc.nonce)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustMarshalAttestation(attDoc AttestationDocument, require *require.Assertions) []byte {
|
|
|
|
out, err := json.Marshal(attDoc)
|
|
|
|
require.NoError(err)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFailIssuer(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
issuer *Issuer
|
|
|
|
userData []byte
|
|
|
|
nonce []byte
|
|
|
|
}{
|
|
|
|
"fail openTPM": {
|
|
|
|
issuer: NewIssuer(
|
|
|
|
func() (io.ReadWriteCloser, error) {
|
|
|
|
return nil, errors.New("failure")
|
|
|
|
},
|
|
|
|
tpmclient.AttestationKeyRSA,
|
|
|
|
fakeGetInstanceInfo,
|
2023-02-28 10:34:18 -05:00
|
|
|
nil,
|
2022-03-22 11:03:15 -04:00
|
|
|
),
|
|
|
|
userData: []byte("Constellation"),
|
|
|
|
nonce: []byte{1, 2, 3, 4},
|
|
|
|
},
|
|
|
|
"fail getAttestationKey": {
|
|
|
|
issuer: NewIssuer(
|
|
|
|
newSimTPMWithEventLog,
|
|
|
|
func(tpm io.ReadWriter) (*tpmclient.Key, error) {
|
|
|
|
return nil, errors.New("failure")
|
|
|
|
},
|
|
|
|
fakeGetInstanceInfo,
|
2023-02-28 10:34:18 -05:00
|
|
|
nil,
|
2022-03-22 11:03:15 -04:00
|
|
|
),
|
|
|
|
userData: []byte("Constellation"),
|
|
|
|
nonce: []byte{1, 2, 3, 4},
|
|
|
|
},
|
|
|
|
"fail Attest": {
|
|
|
|
issuer: NewIssuer(
|
|
|
|
newSimTPMWithEventLog,
|
|
|
|
func(tpm io.ReadWriter) (*tpmclient.Key, error) {
|
|
|
|
return &tpmclient.Key{}, nil
|
|
|
|
},
|
|
|
|
fakeGetInstanceInfo,
|
2023-02-28 10:34:18 -05:00
|
|
|
nil,
|
2022-03-22 11:03:15 -04:00
|
|
|
),
|
|
|
|
userData: []byte("Constellation"),
|
|
|
|
nonce: []byte{1, 2, 3, 4},
|
|
|
|
},
|
|
|
|
"fail getInstanceInfo": {
|
|
|
|
issuer: NewIssuer(
|
|
|
|
newSimTPMWithEventLog,
|
|
|
|
tpmclient.AttestationKeyRSA,
|
|
|
|
func(io.ReadWriteCloser) ([]byte, error) { return nil, errors.New("failure") },
|
2023-02-28 10:34:18 -05:00
|
|
|
nil,
|
2022-03-22 11:03:15 -04:00
|
|
|
),
|
|
|
|
userData: []byte("Constellation"),
|
|
|
|
nonce: []byte{1, 2, 3, 4},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2023-02-28 10:34:18 -05:00
|
|
|
tc.issuer.log = logger.NewTest(t)
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
_, err := tc.issuer.Issue(tc.userData, tc.nonce)
|
|
|
|
assert.Error(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetSHA256QuoteIndex(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
quotes []*tpm.Quote
|
|
|
|
wantIdx int
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"idx 0 is valid": {
|
|
|
|
quotes: []*tpm.Quote{
|
|
|
|
{
|
|
|
|
Pcrs: &tpm.PCRs{
|
|
|
|
Hash: tpm.HashAlgo_SHA256,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantIdx: 0,
|
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"idx 1 is valid": {
|
|
|
|
quotes: []*tpm.Quote{
|
|
|
|
{
|
|
|
|
Pcrs: &tpm.PCRs{
|
|
|
|
Hash: tpm.HashAlgo_SHA1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Pcrs: &tpm.PCRs{
|
|
|
|
Hash: tpm.HashAlgo_SHA256,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantIdx: 1,
|
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"no quotes": {
|
2022-04-26 10:54:05 -04:00
|
|
|
quotes: nil,
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"quotes is nil": {
|
2022-04-26 10:54:05 -04:00
|
|
|
quotes: make([]*tpm.Quote, 2),
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"pcrs is nil": {
|
|
|
|
quotes: []*tpm.Quote{
|
|
|
|
{
|
|
|
|
Pcrs: &tpm.PCRs{
|
|
|
|
Hash: tpm.HashAlgo_SHA1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Pcrs: nil,
|
|
|
|
},
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
idx, err := GetSHA256QuoteIndex(tc.quotes)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
assert.Equal(0, idx)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantIdx, idx)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 04:57:58 -05:00
|
|
|
func TestGetSelectedMeasurements(t *testing.T) {
|
2022-03-22 11:03:15 -04:00
|
|
|
testCases := map[string]struct {
|
|
|
|
openFunc TPMOpenFunc
|
|
|
|
pcrSelection tpm2.PCRSelection
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"error": {
|
|
|
|
openFunc: func() (io.ReadWriteCloser, error) { return nil, errors.New("error") },
|
|
|
|
pcrSelection: tpm2.PCRSelection{
|
|
|
|
Hash: tpm2.AlgSHA256,
|
|
|
|
PCRs: []int{0, 1, 2},
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"3 PCRs": {
|
2023-03-09 09:23:42 -05:00
|
|
|
openFunc: simulator.OpenSimulatedTPM,
|
2022-03-22 11:03:15 -04:00
|
|
|
pcrSelection: tpm2.PCRSelection{
|
|
|
|
Hash: tpm2.AlgSHA256,
|
|
|
|
PCRs: []int{0, 1, 2},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"Azure PCRS": {
|
2023-03-09 09:23:42 -05:00
|
|
|
openFunc: simulator.OpenSimulatedTPM,
|
2022-03-22 11:03:15 -04:00
|
|
|
pcrSelection: AzurePCRSelection,
|
|
|
|
},
|
|
|
|
"GCP PCRs": {
|
2023-03-09 09:23:42 -05:00
|
|
|
openFunc: simulator.OpenSimulatedTPM,
|
2022-03-22 11:03:15 -04:00
|
|
|
pcrSelection: GCPPCRSelection,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-11-24 04:57:58 -05:00
|
|
|
pcrs, err := GetSelectedMeasurements(tc.openFunc, tc.pcrSelection)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
2022-11-24 04:57:58 -05:00
|
|
|
return
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-11-24 04:57:58 -05:00
|
|
|
require.NoError(err)
|
|
|
|
assert.Len(pcrs, len(tc.pcrSelection.PCRs))
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-08-12 09:59:45 -04:00
|
|
|
|
2022-10-14 10:29:21 -04:00
|
|
|
type testAttestationLogger struct {
|
|
|
|
infos []string
|
2022-08-12 09:59:45 -04:00
|
|
|
warnings []string
|
|
|
|
}
|
|
|
|
|
2022-10-25 09:51:23 -04:00
|
|
|
func (w *testAttestationLogger) Infof(format string, args ...any) {
|
2022-10-14 10:29:21 -04:00
|
|
|
w.infos = append(w.infos, fmt.Sprintf(format, args...))
|
|
|
|
}
|
|
|
|
|
2022-10-25 09:51:23 -04:00
|
|
|
func (w *testAttestationLogger) Warnf(format string, args ...any) {
|
2022-08-12 09:59:45 -04:00
|
|
|
w.warnings = append(w.warnings, fmt.Sprintf(format, args...))
|
|
|
|
}
|