-
Notifications
You must be signed in to change notification settings - Fork 14
/
evidence_handler.go
224 lines (178 loc) · 6.43 KB
/
evidence_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright 2021-2023 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package cca_ssd_platform
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/veraison/ccatoken"
"github.com/veraison/ear"
"github.com/veraison/psatoken"
"github.com/veraison/services/handler"
"github.com/veraison/services/log"
"github.com/veraison/services/proto"
"github.com/veraison/services/scheme/common"
"github.com/veraison/services/scheme/common/arm"
)
type EvidenceHandler struct{}
func (s EvidenceHandler) GetName() string {
return "cca-evidence-handler"
}
func (s EvidenceHandler) GetAttestationScheme() string {
return SchemeName
}
func (s EvidenceHandler) GetSupportedMediaTypes() []string {
return EvidenceMediaTypes
}
func (s EvidenceHandler) SynthKeysFromRefValue(
tenantID string,
refVal *handler.Endorsement,
) ([]string, error) {
return arm.SynthKeysFromRefValue(SchemeName, tenantID, refVal)
}
func (s EvidenceHandler) SynthKeysFromTrustAnchor(tenantID string, ta *handler.Endorsement) ([]string, error) {
return arm.SynthKeysFromTrustAnchors(SchemeName, tenantID, ta)
}
func (s EvidenceHandler) GetTrustAnchorID(token *proto.AttestationToken) (string, error) {
return arm.GetTrustAnchorID(SchemeName, token)
}
func (s EvidenceHandler) ExtractClaims(
token *proto.AttestationToken,
trustAnchor string,
) (*handler.ExtractedClaims, error) {
var ccaToken ccatoken.Evidence
if err := ccaToken.FromCBOR(token.Data); err != nil {
return nil, handler.BadEvidence(err)
}
var extracted handler.ExtractedClaims
platformClaimsSet, err := common.ClaimsToMap(ccaToken.PlatformClaims)
if err != nil {
return nil, handler.BadEvidence(fmt.Errorf(
"could not convert platform claims: %w", err))
}
realmClaimsSet, err := common.ClaimsToMap(ccaToken.RealmClaims)
if err != nil {
return nil, handler.BadEvidence(fmt.Errorf(
"could not convert realm claims: %w", err))
}
extracted.ClaimsSet = map[string]interface{}{
"platform": platformClaimsSet,
"realm": realmClaimsSet,
}
extracted.ReferenceID = arm.RefValLookupKey(
SchemeName,
token.TenantId,
arm.MustImplIDString(ccaToken.PlatformClaims),
)
log.Debugf("extracted Reference ID Key = %s", extracted.ReferenceID)
return &extracted, nil
}
// ValidateEvidenceIntegrity, decodes CCA collection and then invokes Verify API of ccatoken library
// which verifies the signature on the platform part of CCA collection, using supplied trust anchor
// and internally verifies the realm part of CCA token using realm public key extracted from
// realm token.
func (s EvidenceHandler) ValidateEvidenceIntegrity(
token *proto.AttestationToken,
trustAnchor string,
endorsementsStrings []string,
) error {
var (
ccaToken ccatoken.Evidence
)
if err := ccaToken.FromCBOR(token.Data); err != nil {
return handler.BadEvidence(err)
}
realmChallenge, err := ccaToken.RealmClaims.GetChallenge()
if err != nil {
return handler.BadEvidence(err)
}
// If the provided challenge was less than 64 bytes long, the RMM will
// zero-pad pad it when generating the attestation token, so do the
// same to the session nonce.
sessionNonce := make([]byte, 64)
copy(sessionNonce, token.Nonce)
if !bytes.Equal(realmChallenge, sessionNonce) {
return handler.BadEvidence(
"freshness: realm challenge (%s) does not match session nonce (%s)",
hex.EncodeToString(realmChallenge),
hex.EncodeToString(token.Nonce),
)
}
pk, err := arm.GetPublicKeyFromTA(SchemeName, trustAnchor)
if err != nil {
return fmt.Errorf("could not get public key from trust anchor: %w", err)
}
if err = ccaToken.Verify(pk); err != nil {
return handler.BadEvidence(err)
}
log.Debug("CCA platform token signature, realm token signature and cryptographic binding verified")
return nil
}
func (s EvidenceHandler) AppraiseEvidence(
ec *proto.EvidenceContext, endorsementsStrings []string,
) (*ear.AttestationResult, error) {
var endorsements []handler.Endorsement // nolint:prealloc
result := handler.CreateAttestationResult(SchemeName)
for i, e := range endorsementsStrings {
var endorsement handler.Endorsement
if err := json.Unmarshal([]byte(e), &endorsement); err != nil {
return nil, fmt.Errorf("could not decode endorsement at index %d: %w", i, err)
}
endorsements = append(endorsements, endorsement)
}
err := populateAttestationResult(result, ec.Evidence.AsMap(), endorsements)
// TO DO: Handle Unprocessed evidence when new Attestation Result interface
// is ready. Please see issue #105
return result, err
}
func populateAttestationResult(
result *ear.AttestationResult,
evidence map[string]interface{},
endorsements []handler.Endorsement,
) error {
claims, err := common.MapToClaims(evidence["platform"].(map[string]interface{}))
if err != nil {
return err
}
appraisal := result.Submods[SchemeName]
// once the signature on the token is verified, we can claim the HW is
// authentic
appraisal.TrustVector.Hardware = ear.GenuineHardwareClaim
rawLifeCycle, err := claims.GetSecurityLifeCycle()
if err != nil {
return handler.BadEvidence(err)
}
lifeCycle := psatoken.CcaLifeCycleToState(rawLifeCycle)
if lifeCycle == psatoken.CcaStateSecured ||
lifeCycle == psatoken.CcaStateNonCcaPlatformDebug {
appraisal.TrustVector.InstanceIdentity = ear.TrustworthyInstanceClaim
appraisal.TrustVector.RuntimeOpaque = ear.ApprovedRuntimeClaim
appraisal.TrustVector.StorageOpaque = ear.HwKeysEncryptedSecretsClaim
} else {
appraisal.TrustVector.InstanceIdentity = ear.UntrustworthyInstanceClaim
appraisal.TrustVector.RuntimeOpaque = ear.VisibleMemoryRuntimeClaim
appraisal.TrustVector.StorageOpaque = ear.UnencryptedSecretsClaim
}
swComps := arm.FilterRefVal(endorsements, "CCA_SSD_PLATFORM.sw-component")
match := arm.MatchSoftware(SchemeName, claims, swComps)
if match {
appraisal.TrustVector.Executables = ear.ApprovedRuntimeClaim
log.Debug("matchSoftware Success")
} else {
appraisal.TrustVector.Executables = ear.UnrecognizedRuntimeClaim
log.Debug("matchSoftware Failed")
}
platformConfig := arm.FilterRefVal(endorsements, "CCA_SSD_PLATFORM.platform-config")
match = arm.MatchPlatformConfig(SchemeName, claims, platformConfig)
if match {
appraisal.TrustVector.Configuration = ear.ApprovedConfigClaim
log.Debug("matchPlatformConfig Success")
} else {
appraisal.TrustVector.Configuration = ear.UnsafeConfigClaim
log.Debug("matchPlatformConfig Failed")
}
appraisal.UpdateStatusFromTrustVector()
appraisal.VeraisonAnnotatedEvidence = &evidence
return nil
}