-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathpcs.go
More file actions
487 lines (440 loc) · 18.3 KB
/
pcs.go
File metadata and controls
487 lines (440 loc) · 18.3 KB
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
// Copyright 2023 Google LLC
//
// 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 pcs defines values specified for the Intel's Provisioning Certification Service
package pcs
import (
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/hex"
"errors"
"fmt"
"net/http"
"strconv"
"time"
)
const (
pckCertExtensionSize = 6
sgxExtensionMinSize = 4
tcbExtensionSize = 18
ppidSize = 16
cpuSvnSize = 16
fmspcSize = 6
pceIDSize = 2
tcbComponentSize = 16
// sgxPckCrlIssuerChainHeaderKey retrieves the issuer chain from the Intel PCS API:
// https://api.portal.trustedservices.intel.com/content/documentation.html#pcs-revocation-v4
sgxPckCrlIssuerChainHeaderKey = "SGX-PCK-CRL-Issuer-Chain"
// sgxQeIdentityIssuerChainHeaderKey retrieves the issuer chain from the Intel PCS API:
// https://api.portal.trustedservices.intel.com/content/documentation.html#pcs-enclave-identity-v4
sgxQeIdentityIssuerChainHeaderKey = "SGX-Enclave-Identity-Issuer-Chain"
// tcbInfoIssuerChainHeaderKey retrieves the issuer chain from the Intel PCS API:
// https://api.portal.trustedservices.intel.com/content/documentation.html#pcs-tcb-info-tdx-v4
tcbInfoIssuerChainHeaderKey = "TCB-Info-Issuer-Chain"
// SgxBaseURL is the base URL for fetching SGX related info from the Intel PCS API.
SgxBaseURL = "https://api.trustedservices.intel.com/sgx/certification/v4"
// TdxBaseURL is the base URL for fetching TDX related info from the Intel PCS API.
TdxBaseURL = "https://api.trustedservices.intel.com/tdx/certification/v4"
)
var (
sgxTcbComponentOidPrefix = []int{1, 2, 840, 113741, 1, 13, 1, 2}
// SgxPckCrlIssuerChainPhrase conforms to the canonicalized header key format used by Go's net/http package.
SgxPckCrlIssuerChainPhrase = http.CanonicalHeaderKey(sgxPckCrlIssuerChainHeaderKey)
// SgxQeIdentityIssuerChainPhrase conforms to the canonicalized header key format used by Go's net/http package.
SgxQeIdentityIssuerChainPhrase = http.CanonicalHeaderKey(sgxQeIdentityIssuerChainHeaderKey)
// TcbInfoIssuerChainPhrase conforms to the canonicalized header key format used by Go's net/http package.
TcbInfoIssuerChainPhrase = http.CanonicalHeaderKey(tcbInfoIssuerChainHeaderKey)
// OidSgxExtension is the x509v3 extension for PCK certificate's SGX Extension.
OidSgxExtension = asn1.ObjectIdentifier([]int{1, 2, 840, 113741, 1, 13, 1})
// OidPPID is the x509v3 extension for PCK certificate's SGX Extensions PPID value.
OidPPID = asn1.ObjectIdentifier([]int{1, 2, 840, 113741, 1, 13, 1, 1})
// OidTCB is the x509v3 extension for PCK certificate's SGX Extensions TCB struct.
OidTCB = asn1.ObjectIdentifier([]int{1, 2, 840, 113741, 1, 13, 1, 2})
// OidPCESvn is the x509v3 extension for PCK certificate's SGX Extensions PCESVN component in TCB struct.
OidPCESvn = asn1.ObjectIdentifier([]int{1, 2, 840, 113741, 1, 13, 1, 2, 17})
// OidCPUSvn is the x509v3 extension for PCK certificate's SGX Extensions CPUSVN component in TCB struct.
OidCPUSvn = asn1.ObjectIdentifier([]int{1, 2, 840, 113741, 1, 13, 1, 2, 18})
// OidPCEID is the x509v3 extension for PCK certificate's SGX Extensions PCEID value.
OidPCEID = asn1.ObjectIdentifier([]int{1, 2, 840, 113741, 1, 13, 1, 3})
// OidFMSPC is the x509v3 extension for PCK certificate's SGX Extensions FMSPC value.
OidFMSPC = asn1.ObjectIdentifier([]int{1, 2, 840, 113741, 1, 13, 1, 4})
// ErrPckExtInvalid error returned when parsing PCK certificate's extension returns leftover bytes
ErrPckExtInvalid = errors.New("unexpected leftover bytes for PCK certificate's extension")
// ErrTcbExtInvalid error returned when parsing of TCB in SGX Extension returns leftover bytes
ErrTcbExtInvalid = errors.New("unexpected leftover bytes for TCB extension inside SGX extension field")
// ErrTcbCompInvalid error returned when parsing of TCB components in SGX Extension returns leftover bytes
ErrTcbCompInvalid = errors.New("unexpected leftover bytes for TCB components in TCB Extension inside SGX extension field")
// ErrSgxExtInvalid error returned when parsing SGX extensions returns leftover bytes
ErrSgxExtInvalid = errors.New("unexpected leftover bytes when parsing SGX extensions")
)
// TdxTcbInfo struct is used to map response from tcbInfo PCS API Service
type TdxTcbInfo struct {
TcbInfo TcbInfo `json:"tcbInfo"`
Signature string `json:"signature"`
}
// TcbInfo struct is used to map response from tcbInfo field
type TcbInfo struct {
ID string `json:"id"`
Version byte `json:"version"`
IssueDate time.Time `json:"issueDate"`
NextUpdate time.Time `json:"nextUpdate"`
Fmspc string `json:"fmspc"`
PceID string `json:"pceId"`
TcbType byte `json:"tcbType"`
TcbEvaluationDataNumber int `json:"tcbEvaluationDataNumber"`
TdxModule TdxModule `json:"tdxModule"`
TdxModuleIdentities []TdxModuleIdentity `json:"tdxModuleIdentities"`
TcbLevels []TcbLevel `json:"tcbLevels"`
}
// TdxModule struct is used to map response from tcbInfo for tdxModule field
type TdxModule struct {
Mrsigner HexBytes `json:"mrsigner"`
Attributes HexBytes `json:"attributes"`
AttributesMask HexBytes `json:"attributesMask"`
}
// TdxModuleIdentity struct is used to map response from tcbInfo for TdxModuleIdentity field
type TdxModuleIdentity struct {
ID string `json:"id"`
Mrsigner HexBytes `json:"mrsigner"`
Attributes HexBytes `json:"attributes"`
AttributesMask HexBytes `json:"attributesMask"`
TcbLevels []TcbLevel `json:"tcbLevels"`
}
// TcbLevel struct is used to map TCB Level field
type TcbLevel struct {
Tcb Tcb `json:"tcb"`
TcbDate string `json:"tcbDate"`
TcbStatus TcbComponentStatus `json:"tcbStatus"`
AdvisoryIDs []string `json:"advisoryIDs"`
}
// Tcb struct is used to map TCB field
type Tcb struct {
SgxTcbcomponents []TcbComponent `json:"sgxtcbcomponents"`
Pcesvn uint16 `json:"pcesvn"`
TdxTcbcomponents []TcbComponent `json:"tdxtcbcomponents"`
Isvsvn uint32 `json:"isvsvn"`
}
// TcbComponent struct is used to map sgx/tdx tcb components
type TcbComponent struct {
Svn byte `json:"svn"`
Category string `json:"category"`
Type string `json:"type"`
}
// QeIdentity struct is used to map response from enclaveIdentity PCS API Call
type QeIdentity struct {
EnclaveIdentity EnclaveIdentity `json:"enclaveIdentity"`
Signature string `json:"signature"`
}
// EnclaveIdentity struct is used to map enclave identity field
type EnclaveIdentity struct {
ID string `json:"id"`
Version byte `json:"version"`
IssueDate time.Time `json:"issueDate"`
NextUpdate time.Time `json:"nextUpdate"`
TcbEvaluationDataNumber int `json:"tcbEvaluationDataNumber"`
Miscselect HexBytes `json:"miscselect"`
MiscselectMask HexBytes `json:"miscselectMask"`
Attributes HexBytes `json:"attributes"`
AttributesMask HexBytes `json:"attributesMask"`
Mrsigner HexBytes `json:"mrsigner"`
IsvProdID uint16 `json:"isvprodid"`
TcbLevels []TcbLevel `json:"tcbLevels"`
}
// PckCertTCB represents struct that store information related to TCB components
type PckCertTCB struct {
PCESvn uint16
CPUSvn []byte
CPUSvnComponents []byte
}
// PckExtensions represents the information stored in the x509 extensions of a PCK certificate which
// will be required for verification
type PckExtensions struct {
PPID string
TCB PckCertTCB
PCEID string
FMSPC string
}
// HexBytes struct contains hex decoded string to bytes value
type HexBytes struct {
Bytes []byte
}
// UnmarshalJSON for hex bytes converts hex encoded string to bytes
func (hb *HexBytes) UnmarshalJSON(s []byte) error {
unquoted, err := strconv.Unquote(string(s))
if err != nil {
return err
}
val, err := hex.DecodeString(unquoted)
if err != nil {
return err
}
hb.Bytes = val
return nil
}
// TcbComponentStatus represents the status of corresponding TCB field
type TcbComponentStatus string
const (
// TcbComponentStatusUpToDate denotes tcb status as UpToDate
TcbComponentStatusUpToDate TcbComponentStatus = "UpToDate"
// TcbComponentStatusSwHardeningNeeded denotes tcb status as SWHardeningNeeded
TcbComponentStatusSwHardeningNeeded TcbComponentStatus = "SWHardeningNeeded"
// TcbComponentStatusConfigurationNeeded denotes tcb status as ConfigurationNeeded
TcbComponentStatusConfigurationNeeded TcbComponentStatus = "ConfigurationNeeded"
// TcbComponentStatusConfigurationAndSWHardeningNeeded denotes tcb status as ConfigurationAndSWHardeningNeeded
TcbComponentStatusConfigurationAndSWHardeningNeeded TcbComponentStatus = "ConfigurationAndSWHardeningNeeded"
// TcbComponentStatusOutOfDate denotes tcb status as OutOfDate
TcbComponentStatusOutOfDate TcbComponentStatus = "OutOfDate"
// TcbComponentStatusOutOfDateConfigurationNeeded denotes tcb status as OutOfDateConfigurationNeeded
TcbComponentStatusOutOfDateConfigurationNeeded TcbComponentStatus = "OutOfDateConfigurationNeeded"
// TcbComponentStatusRevoked denotes tcb status as Revoked
TcbComponentStatusRevoked TcbComponentStatus = "Revoked"
// TcbComponentStatusRelaunchAdvisedConfigurationNeeded denotes tcb status as RelaunchAdvisedConfigurationNeeded
TcbComponentStatusRelaunchAdvisedConfigurationNeeded TcbComponentStatus = "RelaunchAdvisedConfigurationNeeded"
)
// UnmarshalJSON for TcbComponentStatus maps tcb status to corresponding valid strings
func (st *TcbComponentStatus) UnmarshalJSON(s []byte) error {
unquotedStatus, err := strconv.Unquote(string(s))
if err != nil {
return err
}
val := TcbComponentStatus(unquotedStatus)
switch val {
case TcbComponentStatusUpToDate, TcbComponentStatusSwHardeningNeeded, TcbComponentStatusConfigurationNeeded,
TcbComponentStatusConfigurationAndSWHardeningNeeded, TcbComponentStatusOutOfDate, TcbComponentStatusOutOfDateConfigurationNeeded, TcbComponentStatusRevoked:
*st = val
default:
return fmt.Errorf("unexpected tcb status found: %q", val)
}
return nil
}
func sgxTcbComponentOid(component int) asn1.ObjectIdentifier {
return asn1.ObjectIdentifier(append(sgxTcbComponentOidPrefix, component))
}
func asn1U8(ext *pkix.AttributeTypeAndValue, field string, out *byte) error {
if ext == nil {
return fmt.Errorf("no extension for field %s", field)
}
val, ok := ext.Value.(int64)
if !ok {
return fmt.Errorf("%s extension is of type %T, expected int64", field, ext.Value)
}
if val < 0 || val > 255 {
return fmt.Errorf("int value for field %s isn't a byte: %d", field, val)
}
*out = byte(val)
return nil
}
func asn1U16(ext *pkix.AttributeTypeAndValue, field string, out *uint16) error {
if ext == nil {
return fmt.Errorf("no extension for field %s", field)
}
val, ok := ext.Value.(int64)
if !ok {
return fmt.Errorf("%s extension is of type %T, expected int64", field, ext.Value)
}
if val < 0 || val > 65535 {
return fmt.Errorf("int value for field %s isn't a uint16: %d", field, val)
}
*out = uint16(val)
return nil
}
func asn1OctetString(ext *pkix.Extension, field string, size int) ([]byte, error) {
if ext == nil {
return nil, fmt.Errorf("no extension for field %s", field)
}
if len(ext.Value) == size {
return ext.Value, nil
}
var octet []byte
rest, err := asn1.Unmarshal(ext.Value, &octet)
if err != nil {
return nil, fmt.Errorf("could not parse %v extension as an octet string %v (value %v): %v", field, *ext, ext.Value, err)
}
if len(rest) != 0 {
return nil, fmt.Errorf("expected leftover bytes in extension value for field %v", field)
}
// Check the expected length.
if size >= 0 && len(octet) != size {
return nil, fmt.Errorf("%v extension's value size is %d, expected %d", field, len(octet), size)
}
return octet, nil
}
func extractTcbExtension(tcbExtension []asn1.RawValue, tcb *PckCertTCB) error {
tcbComponents := make([]byte, tcbComponentSize)
for _, ext := range tcbExtension {
var tcbValue pkix.AttributeTypeAndValue
rest, err := asn1.Unmarshal(ext.FullBytes, &tcbValue)
if err != nil {
return fmt.Errorf("could not parse TCB component inside the TCB extension in the PCK certificate: %v", err)
}
if len(rest) != 0 {
return ErrTcbCompInvalid
}
for i := 0; i < tcbComponentSize; i++ {
tcbComponentOid := sgxTcbComponentOid(i + 1)
if tcbValue.Type.Equal(tcbComponentOid) {
phrase := fmt.Sprintf("sgxTcbComponent%d", i+1)
var val byte
if err := asn1U8(&tcbValue, phrase, &val); err != nil {
return err
}
tcbComponents[i] = val
break
}
}
if tcbValue.Type.Equal(OidPCESvn) {
if err := asn1U16(&tcbValue, "PCESvn", &tcb.PCESvn); err != nil {
return err
}
}
if tcbValue.Type.Equal(OidCPUSvn) {
val, ok := tcbValue.Value.([]byte)
if !ok {
return fmt.Errorf("CPUSVN component in TCB extension is of type %T, expected []byte", tcbValue.Value)
}
if len(tcbValue.Value.([]byte)) != cpuSvnSize {
return fmt.Errorf("CPUSVN component in TCB extension is of size %d, expected %d", len(tcbValue.Value.([]byte)), cpuSvnSize)
}
tcb.CPUSvn = val
}
}
tcb.CPUSvnComponents = tcbComponents
return nil
}
func extractAsn1SequenceTcbExtension(ext asn1.RawValue) (*PckCertTCB, error) {
tcb := &PckCertTCB{}
var sExtension []asn1.RawValue
rest, err := asn1.Unmarshal(ext.FullBytes, &sExtension)
if err != nil {
return nil, fmt.Errorf("could not parse TCB extension present inside the SGX extension in PCK certificate: %v", err)
}
if len(rest) != 0 {
return nil, ErrTcbExtInvalid
}
if len(sExtension) != 2 {
return nil, fmt.Errorf("TCB extension when unmarshalled is of size %d, expected 2", len(sExtension))
}
var tcbExtension []asn1.RawValue
rest, err = asn1.Unmarshal(sExtension[1].FullBytes, &tcbExtension)
if err != nil {
return nil, fmt.Errorf("could not parse TCB components present inside the SGX extension in PCK certificate: %v", err)
}
if len(rest) != 0 {
return nil, ErrTcbCompInvalid
}
if len(tcbExtension) != tcbExtensionSize {
return nil, fmt.Errorf("TCB extension is of size %d, expected %d", len(tcbExtension), tcbExtensionSize)
}
if err := extractTcbExtension(tcbExtension, tcb); err != nil {
return nil, err
}
return tcb, nil
}
func extractAsn1OctetStringExtension(name string, extension asn1.RawValue, size int) (string, error) {
var sExtension pkix.Extension
rest, err := asn1.Unmarshal(extension.FullBytes, &sExtension)
if err != nil {
return "", fmt.Errorf("could not parse %s present inside the SGX extension in PCK certificate: %v", name, err)
}
if len(rest) != 0 {
return "", fmt.Errorf("unexpected leftover bytes for %s extension inside SGX extension field", name)
}
val, err := asn1OctetString(&sExtension, name, size)
if err != nil {
return "", err
}
return hex.EncodeToString(val), nil
}
func extractSgxExtensions(extensions []asn1.RawValue) (*PckExtensions, error) {
pckExtension := &PckExtensions{}
if len(extensions) < sgxExtensionMinSize {
return nil, fmt.Errorf("SGX Extension has length %d. It should have a minimum length of %d", len(extensions), sgxExtensionMinSize)
}
for i, ext := range extensions {
var sExtension pkix.AttributeTypeAndValue
rest, err := asn1.Unmarshal(ext.FullBytes, &sExtension)
if err != nil {
return nil, fmt.Errorf("could not parse SGX extension's in PCK certificate: %v", err)
}
if len(rest) != 0 {
return nil, ErrSgxExtInvalid
}
if sExtension.Type.Equal(OidPPID) {
pckExtension.PPID, err = extractAsn1OctetStringExtension("PPID", extensions[i], ppidSize)
if err != nil {
return nil, err
}
}
if sExtension.Type.Equal(OidTCB) {
tcb, err := extractAsn1SequenceTcbExtension(extensions[i])
if err != nil {
return nil, err
}
pckExtension.TCB = *tcb
}
if sExtension.Type.Equal(OidPCEID) {
pckExtension.PCEID, err = extractAsn1OctetStringExtension("PCEID", extensions[i], pceIDSize)
if err != nil {
return nil, err
}
}
if sExtension.Type.Equal(OidFMSPC) {
pckExtension.FMSPC, err = extractAsn1OctetStringExtension("FMSPC", extensions[i], fmspcSize)
if err != nil {
return nil, err
}
}
}
return pckExtension, nil
}
func findMatchingExtension(extns []pkix.Extension, oid asn1.ObjectIdentifier) (*pkix.Extension, error) {
for _, ext := range extns {
if ext.Id.Equal(oid) {
return &ext, nil
}
}
return nil, fmt.Errorf("unable to find extension with OID %v in PCK Certificate", oid)
}
// PckCertificateExtensions returns only those x509v3 extensions from the PCK certificate into a
// struct type which will be required in verification purpose.
func PckCertificateExtensions(cert *x509.Certificate) (*PckExtensions, error) {
if len(cert.Extensions) != pckCertExtensionSize {
return nil, fmt.Errorf("PCK certificate extensions length found %d. Expected %d", len(cert.Extensions), pckCertExtensionSize)
}
sgxExt, err := findMatchingExtension(cert.Extensions, OidSgxExtension)
if err != nil {
return nil, fmt.Errorf("could not find SGX extension present in the PCK certificate: %v", err)
}
var sgxExtensions []asn1.RawValue
rest, err := asn1.Unmarshal(sgxExt.Value, &sgxExtensions)
if err != nil {
return nil, fmt.Errorf("could not parse SGX extension present in the PCK certificate: %v", err)
}
if len(rest) != 0 {
return nil, ErrPckExtInvalid
}
return extractSgxExtensions(sgxExtensions)
}
// PckCrlURL returns the Intel PCS URL for retrieving PCK CRL
func PckCrlURL(ca string) string {
return fmt.Sprintf("%s/pckcrl?ca=%s&encoding=der", SgxBaseURL, ca)
}
// TcbInfoURL returns the Intel PCS URL for retrieving TCB Info
func TcbInfoURL(fmspc string) string {
return fmt.Sprintf("%s/tcb?fmspc=%s", TdxBaseURL, fmspc)
}
// QeIdentityURL returns the Intel PCS URL for retrieving QE identity
func QeIdentityURL() string {
return fmt.Sprintf("%s/qe/identity", TdxBaseURL)
}