-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcertlib_proofs.go
3894 lines (3496 loc) · 122 KB
/
certlib_proofs.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2021-22, VMware Inc, and the Certifier Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package certlib
import (
"bytes"
"crypto/ecdsa"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/sha512"
"crypto/x509"
"errors"
"fmt"
certprotos "github.com/vmware-research/certifier-framework-for-confidential-computing/certifier_service/certprotos"
gramineverify "github.com/vmware-research/certifier-framework-for-confidential-computing/certifier_service/gramineverify"
isletverify "github.com/vmware-research/certifier-framework-for-confidential-computing/certifier_service/isletverify"
oeverify "github.com/vmware-research/certifier-framework-for-confidential-computing/certifier_service/oeverify"
"google.golang.org/protobuf/proto"
"math/big"
"os"
)
func InitAxiom(pk certprotos.KeyMessage, ps *certprotos.ProvedStatements) bool {
// add pk is-trusted to proved statenments
ke := MakeKeyEntity(&pk)
ist := "is-trusted"
vc := MakeUnaryVseClause(ke, &ist)
ps.Proved = append(ps.Proved, vc)
return true
}
func testSign(PK1 *ecdsa.PublicKey) {
fmt.Printf("\n***testSign\n")
keyFile := "emulated_keystone_key.bin"
serializedKey, err := os.ReadFile(keyFile)
if err != nil {
fmt.Printf("testSign: Can't read key file\n")
return
}
privateKey := certprotos.KeyMessage{}
err = proto.Unmarshal(serializedKey, &privateKey)
if err != nil {
fmt.Printf("testSign: Can't deserialize key\n")
return
}
pK, PK, err := GetEccKeysFromInternal(&privateKey)
if err != nil || PK == nil || pK == nil {
fmt.Printf("testSign: Can't convertkey\n")
return
}
toHash := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9}
hashed := sha256.Sum256(toHash)
signed, err := ecdsa.SignASN1(rand.Reader, pK, hashed[:])
if err != nil {
fmt.Printf("testSign: Can't sign\n")
return
}
fmt.Printf("\nhashed: ")
PrintBytes(hashed[:])
fmt.Printf("\nsigned: ")
PrintBytes(signed[:])
fmt.Printf("\n")
if ecdsa.VerifyASN1(PK, hashed[:], signed[:]) {
fmt.Printf("testSign: Verify succeeded (1)\n")
} else {
fmt.Printf("testSign: Verify failed (1)\n")
}
if ecdsa.VerifyASN1(PK1, hashed[:], signed[:]) {
fmt.Printf("testSign: Verify succeeded (2)\n")
} else {
fmt.Printf("testSign: Verify failed (2)\n")
}
fmt.Printf("\n")
}
/*
This is policy pool management. When evidence comes in, this selects the subset of
the original policy to use in the proof.
InitPolicyPool puts policy first in AllPolicy
PlatformKeyStatements is the list of policy statements about platform keys
MeasurementsStatements is the list of policy statements about programs (measurements)
PlatformFeatureStatements is a list of policy about platform policy
After pool is initialized, use GetRelevantPlatformKeyPolicy, GetRelevantMeasurementPolicy
and PlatformFeatureStatements to retrieve the policies relevant to the specified
EvidencePackage when constructing proofs. Each must return the single relevant
policy statement of the named type needed in the constructed proof
*/
type PolicyPool struct {
Initialized bool
// Contains all the policy statements
AllPolicy *certprotos.ProvedStatements
// Contains platform key policy statements
PlatformKeyPolicy *certprotos.ProvedStatements
// Contains trusted measurement statements
MeasurementPolicy *certprotos.ProvedStatements
// Contains platform features statements
PlatformFeaturePolicy *certprotos.ProvedStatements
}
// policyKey says platformKey is-trusted-for-attestation
func isPlatformKeyStatement(vse *certprotos.VseClause) bool {
if vse.Clause == nil {
return false
}
if vse.Clause.Subject == nil {
return false
}
if vse.Clause.Subject.EntityType == nil {
return false
}
if vse.Clause.Verb == nil {
return false
}
if vse.Clause.Subject.GetEntityType() == "key" && vse.Clause.GetVerb() == "is-trusted-for-attestation" {
return true
}
return false
}
// policyKey says platform has-trusted-platform-property
func isPlatformFeatureStatement(vse *certprotos.VseClause) bool {
if vse.Clause == nil {
return false
}
if vse.Clause.Subject == nil {
return false
}
if vse.Clause.Subject.EntityType == nil {
return false
}
if vse.Clause.Verb == nil {
return false
}
if vse.Clause.Subject.GetEntityType() == "platform" && vse.Clause.GetVerb() == "has-trusted-platform-property" {
return true
}
return false
}
// policyKey says measurement is-trusted
func isPlatformMeasurementStatement(vse *certprotos.VseClause) bool {
if vse.Clause == nil {
return false
}
if vse.Clause.Subject == nil {
return false
}
if vse.Clause.Subject.EntityType == nil {
return false
}
if vse.Clause.Verb == nil {
return false
}
if vse.Clause.Subject.GetEntityType() == "measurement" && vse.Clause.GetVerb() == "is-trusted" {
return true
}
return false
}
func InitPolicyPool(pool *PolicyPool, original *certprotos.ProvedStatements) bool {
if pool == nil {
fmt.Printf("InitPolicyPool: pool is nil\n")
return false
}
if original == nil {
fmt.Printf("InitPolicyPool: original policy is nil\n")
return false
}
pool.AllPolicy = &certprotos.ProvedStatements{}
pool.PlatformKeyPolicy = &certprotos.ProvedStatements{}
pool.MeasurementPolicy = &certprotos.ProvedStatements{}
pool.PlatformFeaturePolicy = &certprotos.ProvedStatements{}
if pool.AllPolicy == nil || pool.PlatformKeyPolicy == nil ||
pool.MeasurementPolicy == nil || pool.PlatformFeaturePolicy == nil {
fmt.Printf("InitPolicyPool: Some proved statements structures are nil\n")
return false
}
pool.Initialized = false
for i := 0; i < len(original.Proved); i++ {
from := original.Proved[i]
pool.AllPolicy.Proved = append(pool.AllPolicy.Proved, from)
// to := proto.Clone(from).(*certprotos.VseClause)
if isPlatformKeyStatement(from) {
pool.PlatformKeyPolicy.Proved = append(pool.PlatformKeyPolicy.Proved, from)
}
if isPlatformFeatureStatement(from) {
pool.PlatformFeaturePolicy.Proved = append(pool.PlatformFeaturePolicy.Proved, from)
}
if isPlatformMeasurementStatement(from) {
pool.MeasurementPolicy.Proved = append(pool.MeasurementPolicy.Proved, from)
}
}
pool.Initialized = true
return true
}
// Returns the single policy statement naming the relevant platform key policy
// statement for a this evidence package
func GetRelevantPlatformKeyPolicy(pool *PolicyPool, evType string,
evp *certprotos.EvidencePackage) *certprotos.VseClause {
// find the platform key needed from evp and the corresponding policy rule
ev_list := evp.FactAssertion
if ev_list == nil {
return nil
}
var platSubject *certprotos.EntityMessage = nil
// find platformKey says attestationKey is-trusted-for-attestation
fmt.Printf("GetRelevantPlatformKeyPolicy: %d evidence statements\n", len(ev_list))
for i := 0; i < len(ev_list); i++ {
ev := ev_list[i]
/* Debug
fmt.Printf("%d: GetRelevantPlatformKeyPolicy: evidence\n", i)
PrintEvidence(ev)
fmt.Printf("\n")
*/
if ev == nil {
continue
}
if ev.GetEvidenceType() == "signed-claim" {
signedClaimMsg := certprotos.SignedClaimMessage{}
err := proto.Unmarshal(ev.SerializedEvidence, &signedClaimMsg)
if err != nil {
continue
}
claimMsg := certprotos.ClaimMessage{}
err = proto.Unmarshal(signedClaimMsg.SerializedClaimMessage, &claimMsg)
if err != nil {
continue
}
if claimMsg.GetClaimFormat() != "vse-clause" {
continue
}
cl := certprotos.VseClause{}
err = proto.Unmarshal(claimMsg.SerializedClaim, &cl)
if err != nil {
continue
}
/* Debug
fmt.Printf("%d: Clause\n", i)
PrintVseClause(&cl)
fmt.Printf("\n")
*/
if cl.GetVerb() != "says" || cl.Clause == nil {
continue
}
if cl.Clause.Subject == nil || cl.Clause.Verb == nil || cl.Clause.GetVerb() != "is-trusted-for-attestation" {
continue
}
platSubject = cl.Subject
if platSubject == nil || platSubject.GetEntityType() != "key" {
fmt.Printf("GetRelevantPlatformKeyPolicy: wrong entity\n")
return nil
}
break
} else if ev.GetEvidenceType() == "cert" {
platCert := Asn1ToX509(ev.SerializedEvidence)
if platCert == nil {
fmt.Printf("GetRelevantPlatformKeyPolicy: cant convert cert to x509\n")
continue
}
platKey := GetSubjectKey(platCert)
if platKey == nil {
fmt.Printf("GetRelevantPlatformKeyPolicy: cant get subject key from cert\n")
continue
}
platSubject = MakeKeyEntity(platKey)
if platSubject == nil || platSubject.GetEntityType() != "key" {
fmt.Printf("GetRelevantPlatformKeyPolicy: wrong entity\n")
return nil
}
break
} else {
continue
}
}
if platSubject == nil {
fmt.Printf("GetRelevantPlatformKeyPolicy: no match\n")
return nil
}
// Find rule that says policyKey says platSubject is-trusted-for-attestation and return it
for i := 0; i < len(pool.PlatformKeyPolicy.Proved); i++ {
cl := pool.PlatformKeyPolicy.Proved[i]
if cl == nil {
continue
}
if cl.Clause == nil || cl.Clause.Subject == nil {
continue
}
if SameEntity(platSubject, cl.Clause.Subject) {
return cl
}
}
return nil
}
func getVseMeasurementFromAttestation(evBuf []byte) []byte {
sr := certprotos.SignedReport{}
err := proto.Unmarshal(evBuf, &sr)
if err != nil {
fmt.Printf("getVseMeasurementFromAttestation: Can't unmarshal signed report\n")
return nil
}
info := certprotos.VseAttestationReportInfo{}
err = proto.Unmarshal(sr.GetReport(), &info)
if err != nil {
fmt.Printf("getVseMeasurementFromAttestation: Can't unmarshal info\n")
return nil
}
return info.VerifiedMeasurement
}
func getSevMeasurementFromAttestation(evBuf []byte) []byte {
var am certprotos.SevAttestationMessage
err := proto.Unmarshal(evBuf, &am)
if err != nil {
fmt.Printf("getSevMeasurementFromAttestation: Can't unmarshal SevAttestationMessage\n")
return nil
}
return GetMeasurementFromSevAttest(am.ReportedAttestation)
}
func getGramineMeasurementFromAttestation(evBuf []byte) []byte {
succeeded, _, m, err := VerifyGramineAttestation(evBuf)
if !succeeded || err != nil {
fmt.Printf("getGramineMeasurementFromAttestation: Can't verify gramine evidence\n")
return nil
}
return m
}
func getOeMeasurementFromAttestation(prevEvidence *certprotos.Evidence,
curEvidence *certprotos.Evidence) []byte {
var serializedUD, m []byte
var err error
if prevEvidence != nil {
serializedUD, m, err = oeverify.OEHostVerifyEvidence(curEvidence.SerializedEvidence, prevEvidence.SerializedEvidence, false)
} else {
// No endorsement presented
serializedUD, m, err = oeverify.OEHostVerifyEvidence(curEvidence.SerializedEvidence, nil, false)
}
if err != nil || serializedUD == nil || m == nil {
return nil
}
return m
}
func getKeystoneMeasurementFromAttestation(evBuf []byte) []byte {
var am certprotos.KeystoneAttestationMessage
err := proto.Unmarshal(evBuf, &am)
if err != nil {
fmt.Printf("getKeystoneMeasurementFromAttestation: Can't unmarshal KeystoneAttestationMessage\n")
return nil
}
ptr := am.ReportedAttestation
return ptr[0:32]
}
func getIsletMeasurementFromAttestation(evBuf []byte) []byte {
var am certprotos.IsletAttestationMessage
err := proto.Unmarshal(evBuf, &am)
if err != nil {
fmt.Printf("getIsletMeasurementFromAttestation: Can't unmarshal IsletAttestationMessage\n")
return nil
}
m, err := isletverify.IsletVerify(am.WhatWasSaid, am.ReportedAttestation)
if err != nil {
fmt.Printf("getIsletMeasurementFromAttestation: IsletVerify() failed\n")
return nil
}
return m
}
// Returns the single policy statement naming the relevant measurement policy
// statement for a this evidence package
func GetRelevantMeasurementPolicy(pool *PolicyPool, evType string,
evp *certprotos.EvidencePackage) *certprotos.VseClause {
ev_list := evp.FactAssertion
if ev_list == nil {
return nil
}
// find attestation and get measurement
var measurement []byte = nil
for i := 0; i < len(ev_list); i++ {
ev := ev_list[i]
if ev == nil {
continue
}
if ev.GetEvidenceType() == "signed-claim" {
continue
} else if ev.GetEvidenceType() == "pem-cert-chain" {
continue
} else if ev.GetEvidenceType() == "cert" {
continue
} else if ev.GetEvidenceType() == "signed-vse-attestation-report" {
measurement = getVseMeasurementFromAttestation(ev.SerializedEvidence)
break
} else if ev.GetEvidenceType() == "sev-attestation" {
measurement = getSevMeasurementFromAttestation(ev.SerializedEvidence)
break
} else if ev.GetEvidenceType() == "islet-attestation" {
measurement = getIsletMeasurementFromAttestation(ev.SerializedEvidence)
break
} else if ev.GetEvidenceType() == "keystone-attestation" {
measurement = getKeystoneMeasurementFromAttestation(ev.SerializedEvidence)
break
} else if ev.GetEvidenceType() == "gramine-attestation" {
measurement = getGramineMeasurementFromAttestation(ev.SerializedEvidence)
break
} else if ev.GetEvidenceType() == "oe-attestation-report" {
if i < 1 || ev_list[i-1].GetEvidenceType() != "pem-cert-chain" {
measurement = getOeMeasurementFromAttestation(nil, ev_list[i])
} else {
measurement = getOeMeasurementFromAttestation(ev_list[i-1], ev_list[i])
}
break
} else {
continue
}
}
if measurement == nil {
fmt.Printf("GetRelevantMeasurementPolicy: no evidence measurement\n")
return nil
}
// look for policyKey says Measurement[] is-trusted
for i := 0; i < len(pool.MeasurementPolicy.Proved); i++ {
s := pool.MeasurementPolicy.Proved[i]
if s == nil || s.Verb == nil || s.GetVerb() != "says" {
continue
}
cl := s.Clause
if cl == nil || cl.Subject == nil || cl.Verb == nil {
continue
}
if cl.Subject.GetEntityType() != "measurement" || cl.GetVerb() != "is-trusted" {
continue
}
if bytes.Equal(measurement, cl.Subject.Measurement) {
return s
}
}
return nil
}
// Returns the single policy statement naming the relevant trusted-platform
// policy statement for a this evidence package
func GetRelevantPlatformFeaturePolicy(pool *PolicyPool, evType string,
evp *certprotos.EvidencePackage) *certprotos.VseClause {
ev_list := evp.FactAssertion
if ev_list == nil {
return nil
}
var platform *certprotos.EntityMessage = nil
// Find "attestationKey says environment(platform, measurement) is-environment"
for i := 0; i < len(ev_list); i++ {
ev := ev_list[i]
if ev == nil {
continue
}
if ev.GetEvidenceType() != "sev-attestation" {
continue
}
var am certprotos.SevAttestationMessage
err := proto.Unmarshal(ev.SerializedEvidence, &am)
if err != nil {
fmt.Printf("GetRelevantPlatformFeaturePolicy: Can't unmarshal SevAttestationMessage\n")
return nil
}
plat := GetPlatformFromSevAttest(am.ReportedAttestation)
if plat != nil {
platform = MakePlatformEntity(plat)
break
}
}
if platform == nil {
return nil
}
/* Debug
fmt.Printf("found platform\n")
PrintEntity(platform)
fmt.Printf("\n")
*/
// look for policyKey says platform has-trusted-platform-property and match properties
for i := 0; i < len(pool.PlatformFeaturePolicy.Proved); i++ {
s := pool.PlatformFeaturePolicy.Proved[i]
if s == nil {
continue
}
cl := s.Clause
/* Debug
fmt.Printf("Clause for plat:\n")
PrintVseClause(cl)
fmt.Printf("\n")
*/
if cl == nil || cl.Subject == nil || cl.Verb == nil {
continue
}
if cl.Subject.GetEntityType() != "platform" || cl.GetVerb() != "has-trusted-platform-property" {
continue
}
return s
}
return nil
}
// Filtered OePolicy should be
// 00: "policyKey is-trusted"
// 01: "Key[rsa, policyKey, f2663e9ca042fcd261ab051b3a4e3ac83d79afdd] says
// Key[rsa, VSE, cbfced04cfc0f1f55df8cbe437c3aba79af1657a] is-trusted-for-attestation"
// 02: "policyKey says measurement is-trusted"
func FilterOePolicy(policyKey *certprotos.KeyMessage, evp *certprotos.EvidencePackage,
policyPool *PolicyPool) *certprotos.ProvedStatements {
/* Debug
fmt.Printf("Incoming evidence for Oe\n")
PrintEvidencePackage(evp, true)
fmt.Printf("\nOriginal Platform Policy:\n")
for i := 0; i < len(policyPool.PlatformKeyPolicy.Proved); i++ {
cl := policyPool.PlatformKeyPolicy.Proved[i]
PrintVseClause(cl)
fmt.Printf("\n")
}
fmt.Printf("\n")
fmt.Printf("\nOriginal Measurement Policy:\n")
for i := 0; i < len(policyPool.MeasurementPolicy.Proved); i++ {
cl := policyPool.MeasurementPolicy.Proved[i]
PrintVseClause(cl)
fmt.Printf("\n")
}
fmt.Printf("\n\n")
*/
filtered := &certprotos.ProvedStatements{}
// policyKey is-trusted
from := policyPool.AllPolicy.Proved[0]
to := proto.Clone(from).(*certprotos.VseClause)
filtered.Proved = append(filtered.Proved, to)
// This should be passed in
evType := "oe-evidence"
// Oe (like keystone and islet) does not always include a platform
// certificate in the evidence.
from = GetRelevantPlatformKeyPolicy(policyPool, evType, evp)
if from != nil {
to = proto.Clone(from).(*certprotos.VseClause)
filtered.Proved = append(filtered.Proved, to)
}
from = GetRelevantMeasurementPolicy(policyPool, evType, evp)
if from == nil {
return nil
}
to = proto.Clone(from).(*certprotos.VseClause)
filtered.Proved = append(filtered.Proved, to)
return filtered
}
// Filtered Policy should be
// 0: "policyKey is-trusted"
// 1: "policyKey says platformKey is-trusted-for-attestation"
// 2: "policyKey says measurement is-trusted"
func FilterInternalPolicy(policyKey *certprotos.KeyMessage, evp *certprotos.EvidencePackage,
policyPool *PolicyPool) *certprotos.ProvedStatements {
filtered := &certprotos.ProvedStatements{}
// policyKey is-trusted
from := policyPool.AllPolicy.Proved[0]
to := proto.Clone(from).(*certprotos.VseClause)
filtered.Proved = append(filtered.Proved, to)
// This should be passed in
evType := "vse-attestation-package"
from = GetRelevantPlatformKeyPolicy(policyPool, evType, evp)
if from == nil {
fmt.Printf("FilterInternalPolicy: Can't get relevant platform key\n")
return nil
}
to = proto.Clone(from).(*certprotos.VseClause)
filtered.Proved = append(filtered.Proved, to)
from = GetRelevantMeasurementPolicy(policyPool, evType, evp)
if from == nil {
fmt.Printf("FilterInternalPolicy: Can't get relevant measurement\n")
return nil
}
to = proto.Clone(from).(*certprotos.VseClause)
filtered.Proved = append(filtered.Proved, to)
return filtered
}
// Filtered Policy should be
// 00 Key[rsa, policyKey, f91d6331b1fd99b3fa8641fd16dcd4c272a92b8a] is-trusted
// 01 Key[rsa, policyKey, f91d6331b1fd99b3fa8641fd16dcd4c272a92b8a] says
// Key[rsa, ARKKey, c36d3343d69d9d8000d32d0979adff876e98ec79] is-trusted-for-attestation
// 02 Key[rsa, policyKey, f91d6331b1fd99b3fa8641fd16dcd4c272a92b8a] says
// Measurement[010203040506070801020304050607080102030405060708010203040506070801020304050607080102030405060708] is-trusted
// 03 Key[rsa, policyKey, f91d6331b1fd99b3fa8641fd16dcd4c272a92b8a] says
// platform[amd-sev-snp, debug: no, migrate: no, api-major: >=0, api-minor: >=0, key-share: no,
// tcb-version: >=0] has-trusted-platform-property
func FilterSevPolicy(policyKey *certprotos.KeyMessage, evp *certprotos.EvidencePackage,
policyPool *PolicyPool) *certprotos.ProvedStatements {
filtered := &certprotos.ProvedStatements{}
// policyKey is-trusted
from := policyPool.AllPolicy.Proved[0]
to := proto.Clone(from).(*certprotos.VseClause)
filtered.Proved = append(filtered.Proved, to)
// This should be passed in
evType := "sev-evidence"
// policyKey says platformKey is-trusted-for-attestation
from = GetRelevantPlatformKeyPolicy(policyPool, evType, evp)
if from == nil {
fmt.Printf("FilterSevPolicy: Can't get relavent platform key\n")
return nil
}
to = proto.Clone(from).(*certprotos.VseClause)
filtered.Proved = append(filtered.Proved, to)
// policyKey says measurement is-trusted-for-attestation
from = GetRelevantMeasurementPolicy(policyPool, evType, evp)
if from == nil {
fmt.Printf("FilterSevPolicy: Can't get relavent measurement\n")
return nil
}
to = proto.Clone(from).(*certprotos.VseClause)
filtered.Proved = append(filtered.Proved, to)
// policyKey says platform has-trusted-platform-policy
from = GetRelevantPlatformFeaturePolicy(policyPool, evType, evp)
if from == nil {
fmt.Printf("FilterSevPolicy: Can't get relavent platform features\n")
return nil
}
to = proto.Clone(from).(*certprotos.VseClause)
filtered.Proved = append(filtered.Proved, to)
return filtered
}
func InitPolicy(publicPolicyKey *certprotos.KeyMessage, signedPolicy *certprotos.SignedClaimSequence,
alreadyProved *certprotos.ProvedStatements) bool {
if publicPolicyKey == nil {
fmt.Printf("Policy key empty\n")
return false
}
for i := 0; i < len(signedPolicy.Claims); i++ {
sc := signedPolicy.Claims[i]
if !VerifySignedClaim(sc, publicPolicyKey) {
fmt.Printf("Can't verify signature\n")
return false
}
cm := &certprotos.ClaimMessage{}
err := proto.Unmarshal(sc.SerializedClaimMessage, cm)
if err != nil {
fmt.Printf("Can't unmarshal claim\n")
return false
}
if cm.GetClaimFormat() != "vse-clause" {
fmt.Printf("Not vse claim\n")
return false
}
vse := &certprotos.VseClause{}
err = proto.Unmarshal(cm.SerializedClaim, vse)
if err != nil {
fmt.Printf("Can't unmarshal vse claim\n")
return false
}
alreadyProved.Proved = append(alreadyProved.Proved, vse)
}
return true
}
func InitProvedStatements(pk certprotos.KeyMessage, evidenceList []*certprotos.Evidence,
ps *certprotos.ProvedStatements) bool {
seenList := new(CertSeenList)
seenList.maxSize = 30
seenList.size = 0
// Debug
fmt.Printf("\nInitProvedStatements %d assertions\n", len(evidenceList))
for i := 0; i < len(evidenceList); i++ {
ev := evidenceList[i]
if ev.GetEvidenceType() == "signed-claim" {
signedClaim := certprotos.SignedClaimMessage{}
err := proto.Unmarshal(ev.SerializedEvidence, &signedClaim)
if err != nil {
fmt.Printf("InitProvedStatements: Can't unmarshal serialized claim\n")
return false
}
k := signedClaim.SigningKey
tcl := certprotos.VseClause{}
if VerifySignedAssertion(signedClaim, k, &tcl) {
// make sure the saying key in tcl is the same key that signed it
if tcl.GetVerb() == "says" && tcl.GetSubject().GetEntityType() == "key" {
if SameKey(k, tcl.GetSubject().GetKey()) {
ps.Proved = append(ps.Proved, &tcl)
}
}
}
} else if ev.GetEvidenceType() == "pem-cert-chain" {
// nothing to do
} else if ev.GetEvidenceType() == "gramine-attestation" {
succeeded, serializedUD, m, err := VerifyGramineAttestation(ev.SerializedEvidence)
if !succeeded || err != nil {
fmt.Printf("InitProvedStatements: Can't verify gramine evidence\n")
return false
}
// get enclave key from ud
ud := certprotos.AttestationUserData{}
err = proto.Unmarshal(serializedUD, &ud)
if err != nil {
fmt.Printf("InitProvedStatements: Can't unmarshal user data\n")
return false
}
cl := ConstructGramineClaim(ud.EnclaveKey, m)
if cl == nil {
fmt.Printf("InitProvedStatements: ConstructGramineClaim failed\n")
return false
}
ps.Proved = append(ps.Proved, cl)
} else if ev.GetEvidenceType() == "oe-attestation-report" {
// call oeVerify here and construct the statement:
// enclave-key speaks-for measurement
// from the return values. Then add it to proved statements
// Ignore SGX TCB level check for now
var serializedUD, m []byte
var err error
if i < 1 || evidenceList[i-1].GetEvidenceType() != "pem-cert-chain" {
// No endorsement presented
serializedUD, m, err = oeverify.OEHostVerifyEvidence(evidenceList[i].SerializedEvidence,
nil, false)
} else {
serializedUD, m, err = oeverify.OEHostVerifyEvidence(evidenceList[i].SerializedEvidence,
evidenceList[i-1].SerializedEvidence, false)
}
if err != nil || serializedUD == nil || m == nil {
return false
}
ud := certprotos.AttestationUserData{}
err = proto.Unmarshal(serializedUD, &ud)
if err != nil {
return false
}
// Get platform key from pem file
var cl *certprotos.VseClause
if i >= 1 {
stripped := StripPemHeaderAndTrailer(string(evidenceList[i-1].SerializedEvidence))
if stripped == nil {
fmt.Printf("InitProvedStatements: Bad PEM\n")
return false
}
k := KeyFromPemFormat(*stripped)
cl = ConstructOESpeaksForStatement(k, ud.EnclaveKey, m)
} else {
cl = ConstructOESpeaksForStatement(nil, ud.EnclaveKey, m)
}
if cl == nil {
fmt.Printf("InitProvedStatements: ConstructEnclaveKeySpeaksForMeasurement failed\n")
return false
}
ps.Proved = append(ps.Proved, cl)
} else if ev.GetEvidenceType() == "islet-attestation" {
n := 1
if ps.Proved[n] == nil || ps.Proved[n].Clause == nil ||
ps.Proved[n].Clause.Subject == nil {
fmt.Printf("InitProvedStatements: Can't get attestKey key (1)\n")
return false
}
attestKeyVerifyKeyEnt := ps.Proved[n].Clause.Subject
if attestKeyVerifyKeyEnt == nil {
fmt.Printf("InitProvedStatements: Can't get attestKey key (2)\n")
return false
}
if attestKeyVerifyKeyEnt.GetEntityType() != "key" {
fmt.Printf("InitProvedStatements: Can't get attestKey key (3)\n")
return false
}
attestKey := attestKeyVerifyKeyEnt.Key
if attestKey == nil {
fmt.Printf("InitProvedStatements: Can't get attestKey key (4)\n")
return false
}
m := VerifyIsletAttestation(ev.SerializedEvidence, attestKey)
if m == nil {
fmt.Printf("InitProvedStatements: VerifyIsletAttestation failed\n")
return false
}
var am certprotos.IsletAttestationMessage
err := proto.Unmarshal(ev.SerializedEvidence, &am)
if err != nil {
fmt.Printf("InitProvedStatements: Can't unmarshal IsletAttestationMessage\n")
return false
}
var ud certprotos.AttestationUserData
err = proto.Unmarshal(am.WhatWasSaid, &ud)
if err != nil {
fmt.Printf("InitProvedStatements: Can't unmarshal AttestationUserData\n")
return false
}
if ud.EnclaveKey == nil {
fmt.Printf("InitProvedStatements: No enclaveKey\n")
return false
}
if am.ReportedAttestation == nil {
fmt.Printf("InitProvedStatements: No reported attestation\n")
return false
}
mEnt := MakeMeasurementEntity(m)
c2 := ConstructIsletSpeaksForMeasurementStatement(attestKey, ud.EnclaveKey, mEnt)
if c2 == nil {
fmt.Printf("InitProvedStatements: ConstructIsletSpeaksForMeasurementStatement failed\n")
return false
}
ps.Proved = append(ps.Proved, c2)
} else if ev.GetEvidenceType() == "keystone-attestation" {
n := 1
if ps.Proved[n] == nil || ps.Proved[n].Clause == nil ||
ps.Proved[n].Clause.Subject == nil {
fmt.Printf("InitProvedStatements: Can't get attestKey key (1)\n")
return false
}
attestKeyVerifyKeyEnt := ps.Proved[n].Clause.Subject
if attestKeyVerifyKeyEnt == nil {
fmt.Printf("InitProvedStatements: Can't get attestKey key (2)\n")
return false
}
if attestKeyVerifyKeyEnt.GetEntityType() != "key" {
fmt.Printf("InitProvedStatements: Can't get attestKey key (3)\n")
return false
}
attestKey := attestKeyVerifyKeyEnt.Key
if attestKey == nil {
fmt.Printf("InitProvedStatements: Can't get attestKey key (4)\n")
return false
}
m := VerifyKeystoneAttestation(ev.SerializedEvidence, attestKey)
if m == nil {
fmt.Printf("InitProvedStatements: VerifyKeystoneAttestation failed\n")
return false
}
var am certprotos.KeystoneAttestationMessage
err := proto.Unmarshal(ev.SerializedEvidence, &am)
if err != nil {
fmt.Printf("InitProvedStatements: Can't unmarshal KeystoneAttestationMessage\n")
return false
}
var ud certprotos.AttestationUserData
err = proto.Unmarshal(am.WhatWasSaid, &ud)
if err != nil {
fmt.Printf("InitProvedStatements: Can't unmarshal UserData\n")
return false
}
if ud.EnclaveKey == nil {
fmt.Printf("InitProvedStatements: No enclaveKey\n")
return false
}
if am.ReportedAttestation == nil {
fmt.Printf("InitProvedStatements: No reported attestation\n")
return false
}
mEnt := MakeMeasurementEntity(m)
c2 := ConstructKeystoneSpeaksForMeasurementStatement(attestKey, ud.EnclaveKey, mEnt)
if c2 == nil {
fmt.Printf("InitProvedStatements: ConstructKeystoneSpeaksForMeasurementStatement failed\n")
return false
}
ps.Proved = append(ps.Proved, c2)
} else if ev.GetEvidenceType() == "sev-attestation" {
// get the key from ps
n := len(ps.Proved) - 1
if n < 0 {
fmt.Printf("InitProvedStatements: sev evidence is at wrong position\n")
return false
}
if ps.Proved[n] == nil || ps.Proved[n].Clause == nil ||
ps.Proved[n].Clause.Subject == nil {
fmt.Printf("InitProvedStatements: Can't get vcek key (1)\n")
return false
}
vcekVerifyKeyEnt := ps.Proved[n].Clause.Subject
if vcekVerifyKeyEnt == nil {
fmt.Printf("InitProvedStatements: Can't get vcek key (2)\n")
return false
}
if vcekVerifyKeyEnt.GetEntityType() != "key" {
fmt.Printf("InitProvedStatements: Can't get vcek key (3)\n")
return false
}
vcekKey := vcekVerifyKeyEnt.Key
if vcekKey == nil {
fmt.Printf("InitProvedStatements: Can't get vcek key (4)\n")
return false
}
m := VerifySevAttestation(ev.SerializedEvidence, vcekKey)
if m == nil {
fmt.Printf("InitProvedStatements: VerifySevAttestation failed\n")
return false
}
var am certprotos.SevAttestationMessage
err := proto.Unmarshal(ev.SerializedEvidence, &am)
if err != nil {
fmt.Printf("InitProvedStatements: Can't unmarshal SevAttestationMessage\n")
return false
}
var ud certprotos.AttestationUserData
err = proto.Unmarshal(am.WhatWasSaid, &ud)
if err != nil {
fmt.Printf("InitProvedStatements: Can't unmarshal UserData\n")
return false
}
if ud.EnclaveKey == nil {
fmt.Printf("InitProvedStatements: No enclaveKey\n")
return false
}
if am.ReportedAttestation == nil {
fmt.Printf("InitProvedStatements: No reported attestation\n")
return false
}
c1 := ConstructSevIsEnvironmentStatement(vcekKey, am.ReportedAttestation)
if c1 == nil {
fmt.Printf("InitProvedStatements: ConstructSevIsEnvironmentStatement failed\n")
return false
}
ps.Proved = append(ps.Proved, c1)
if c1.Clause == nil || c1.Clause.Subject == nil {
fmt.Printf("InitProvedStatements: can't get environment\n")
return false
}
env := c1.Clause.Subject
c2 := ConstructSevSpeaksForEnvironmentStatement(vcekKey, ud.EnclaveKey, env)
if c2 == nil {
fmt.Printf("InitProvedStatements: ConstructSevSpeaksForEnvironmentStatement failed\n")
return false
}
ps.Proved = append(ps.Proved, c2)
} else if ev.GetEvidenceType() == "cert" {
// A cert always means "the signing-key says the subject-key is-trusted-for-attestation"
// construct vse statement.