Debian Codegraph @

attributecert/attributecert.go

raw
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Copyright 2019 Google, LLC.
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5
6 // Based on earlier versions of https://go.dev/src/crypto/x509/x509.go
7 // LICENSE file from https://go.dev/LICENSE follows:
8
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are
11 // met:
12
13 // * Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 // * Redistributions in binary form must reproduce the above
16 // copyright notice, this list of conditions and the following disclaimer
17 // in the documentation and/or other materials provided with the
18 // distribution.
19 // * Neither the name of Google LLC nor the names of its
20 // contributors may be used to endorse or promote products derived from
21 // this software without specific prior written permission.
22
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35 // Package attributecert parses X.509-encoded attribute certificates.
36 package attributecert
37
38 import (
39 "bytes"
40 "crypto"
41 "crypto/x509"
42 "crypto/x509/pkix"
43 "encoding/asn1"
44 "errors"
45 "fmt"
46 "math/big"
47 "time"
48
49 "github.com/google/go-attestation/oid"
50 )
51
52 var (
53 oidExtensionAuthorityKeyIdentifier = []int{2, 5, 29, 35}
54 oidAuthorityInfoAccess = []int{1, 3, 6, 1, 5, 5, 7, 1, 1}
55 oidCpsCertificatePolicy = []int{1, 3, 6, 1, 5, 5, 7, 2, 1}
56 oidAuthorityInfoAccessOcsp = []int{1, 3, 6, 1, 5, 5, 7, 48, 1}
57 oidAuthorityInfoAccessIssuers = []int{1, 3, 6, 1, 5, 5, 7, 48, 2}
58 oidTcgCertificatePolicy = []int{1, 2, 840, 113741, 1, 5, 2, 4}
59 oidAttributeUserNotice = []int{1, 3, 6, 1, 5, 5, 7, 2, 2}
60 oidTcgPlatformManufacturerStrV1 = []int{2, 23, 133, 2, 4}
61 oidTcgPlatformModelV1 = []int{2, 23, 133, 2, 5}
62 oidTcgPlatformVersionV1 = []int{2, 23, 133, 2, 6}
63 )
64
65 var (
66 oidSignatureSHA1WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 5}
67 oidSignatureSHA256WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 11}
68 oidSignatureSHA384WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 12}
69 oidSignatureSHA512WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 13}
70 oidSignatureRSAPSS = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 10}
71 oidSignatureECDSAWithSHA256 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 2}
72 oidSignatureECDSAWithSHA384 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 3}
73 oidSignatureECDSAWithSHA512 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 4}
74 oidSignatureEd25519 = asn1.ObjectIdentifier{1, 3, 101, 112}
75
76 oidSHA256 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 1}
77 oidSHA384 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 2}
78 oidSHA512 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 3}
79
80 oidMGF1 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 8}
81 )
82
83 type signatureAlgorithmDetail struct {
84 algo x509.SignatureAlgorithm
85 name string
86 oid asn1.ObjectIdentifier
87 params asn1.RawValue
88 pubKeyAlgo x509.PublicKeyAlgorithm
89 hash crypto.Hash
90 isRSAPSS bool
91 }
92
93 // Shamelessly copied from stdlib x509.go
94 var signatureAlgorithmDetails = []signatureAlgorithmDetail{
95 // SHA1WithRSA is deprecated, but we still support it for legacy reasons.
96 // See testdata/Intel_nuc_pc2.cer for example.
97 // TODO: Remove SHA1WithRSA once we no longer need to support it.
98 {x509.SHA1WithRSA, "SHA1-RSA", oidSignatureSHA1WithRSA, asn1.NullRawValue, x509.RSA, crypto.SHA1, false},
99 {x509.SHA256WithRSA, "SHA256-RSA", oidSignatureSHA256WithRSA, asn1.NullRawValue, x509.RSA, crypto.SHA256, false},
100 {x509.SHA384WithRSA, "SHA384-RSA", oidSignatureSHA384WithRSA, asn1.NullRawValue, x509.RSA, crypto.SHA384, false},
101 {x509.SHA512WithRSA, "SHA512-RSA", oidSignatureSHA512WithRSA, asn1.NullRawValue, x509.RSA, crypto.SHA512, false},
102 {x509.SHA256WithRSAPSS, "SHA256-RSAPSS", oidSignatureRSAPSS, pssParametersSHA256, x509.RSA, crypto.SHA256, true},
103 {x509.SHA384WithRSAPSS, "SHA384-RSAPSS", oidSignatureRSAPSS, pssParametersSHA384, x509.RSA, crypto.SHA384, true},
104 {x509.SHA512WithRSAPSS, "SHA512-RSAPSS", oidSignatureRSAPSS, pssParametersSHA512, x509.RSA, crypto.SHA512, true},
105 {x509.ECDSAWithSHA256, "ECDSA-SHA256", oidSignatureECDSAWithSHA256, emptyRawValue, x509.ECDSA, crypto.SHA256, false},
106 {x509.ECDSAWithSHA384, "ECDSA-SHA384", oidSignatureECDSAWithSHA384, emptyRawValue, x509.ECDSA, crypto.SHA384, false},
107 {x509.ECDSAWithSHA512, "ECDSA-SHA512", oidSignatureECDSAWithSHA512, emptyRawValue, x509.ECDSA, crypto.SHA512, false},
108 {x509.PureEd25519, "Ed25519", oidSignatureEd25519, emptyRawValue, x509.Ed25519, crypto.Hash(0) /* no pre-hashing */, false},
109 }
110
111 var emptyRawValue = asn1.RawValue{}
112
113 // DER encoded RSA PSS parameters for the
114 // SHA256, SHA384, and SHA512 hashes as defined in RFC 3447, Appendix A.2.3.
115 // The parameters contain the following values:
116 // - hashAlgorithm contains the associated hash identifier with NULL parameters
117 // - maskGenAlgorithm always contains the default mgf1SHA1 identifier
118 // - saltLength contains the length of the associated hash
119 // - trailerField always contains the default trailerFieldBC value
120 var (
121 pssParametersSHA256 = asn1.RawValue{FullBytes: []byte{48, 52, 160, 15, 48, 13, 6, 9, 96, 134, 72, 1, 101, 3, 4, 2, 1, 5, 0, 161, 28, 48, 26, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 8, 48, 13, 6, 9, 96, 134, 72, 1, 101, 3, 4, 2, 1, 5, 0, 162, 3, 2, 1, 32}}
122 pssParametersSHA384 = asn1.RawValue{FullBytes: []byte{48, 52, 160, 15, 48, 13, 6, 9, 96, 134, 72, 1, 101, 3, 4, 2, 2, 5, 0, 161, 28, 48, 26, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 8, 48, 13, 6, 9, 96, 134, 72, 1, 101, 3, 4, 2, 2, 5, 0, 162, 3, 2, 1, 48}}
123 pssParametersSHA512 = asn1.RawValue{FullBytes: []byte{48, 52, 160, 15, 48, 13, 6, 9, 96, 134, 72, 1, 101, 3, 4, 2, 3, 5, 0, 161, 28, 48, 26, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 8, 48, 13, 6, 9, 96, 134, 72, 1, 101, 3, 4, 2, 3, 5, 0, 162, 3, 2, 1, 64}}
124 )
125
126 // pssParameters reflects the parameters in an AlgorithmIdentifier that
127 // specifies RSA PSS. See RFC 3447, Appendix A.2.3.
128 type pssParameters struct {
129 // The following three fields are not marked as
130 // optional because the default values specify SHA-1,
131 // which is no longer suitable for use in signatures.
132 Hash pkix.AlgorithmIdentifier `asn1:"explicit,tag:0"`
133 MGF pkix.AlgorithmIdentifier `asn1:"explicit,tag:1"`
134 SaltLength int `asn1:"explicit,tag:2"`
135 TrailerField int `asn1:"optional,explicit,tag:3,default:1"`
136 }
137
138 func getSignatureAlgorithmFromAI(ai pkix.AlgorithmIdentifier) x509.SignatureAlgorithm {
139 if ai.Algorithm.Equal(oidSignatureEd25519) {
140 // RFC 8410, Section 3
141 // > For all of the OIDs, the parameters MUST be absent.
142 if len(ai.Parameters.FullBytes) != 0 {
143 return x509.UnknownSignatureAlgorithm
144 }
145 }
146
147 if !ai.Algorithm.Equal(oidSignatureRSAPSS) {
148 for _, details := range signatureAlgorithmDetails {
149 if ai.Algorithm.Equal(details.oid) {
150 return details.algo
151 }
152 }
153 return x509.UnknownSignatureAlgorithm
154 }
155
156 // RSA PSS is special because it encodes important parameters
157 // in the Parameters.
158
159 var params pssParameters
160 if _, err := asn1.Unmarshal(ai.Parameters.FullBytes, &params); err != nil {
161 return x509.UnknownSignatureAlgorithm
162 }
163
164 var mgf1HashFunc pkix.AlgorithmIdentifier
165 if _, err := asn1.Unmarshal(params.MGF.Parameters.FullBytes, &mgf1HashFunc); err != nil {
166 return x509.UnknownSignatureAlgorithm
167 }
168
169 // PSS is greatly overburdened with options. This code forces them into
170 // three buckets by requiring that the MGF1 hash function always match the
171 // message hash function (as recommended in RFC 3447, Section 8.1), that the
172 // salt length matches the hash length, and that the trailer field has the
173 // default value.
174 if (len(params.Hash.Parameters.FullBytes) != 0 && !bytes.Equal(params.Hash.Parameters.FullBytes, asn1.NullBytes)) ||
175 !params.MGF.Algorithm.Equal(oidMGF1) ||
176 !mgf1HashFunc.Algorithm.Equal(params.Hash.Algorithm) ||
177 (len(mgf1HashFunc.Parameters.FullBytes) != 0 && !bytes.Equal(mgf1HashFunc.Parameters.FullBytes, asn1.NullBytes)) ||
178 params.TrailerField != 1 {
179 return x509.UnknownSignatureAlgorithm
180 }
181
182 switch {
183 case params.Hash.Algorithm.Equal(oidSHA256) && params.SaltLength == 32:
184 return x509.SHA256WithRSAPSS
185 case params.Hash.Algorithm.Equal(oidSHA384) && params.SaltLength == 48:
186 return x509.SHA384WithRSAPSS
187 case params.Hash.Algorithm.Equal(oidSHA512) && params.SaltLength == 64:
188 return x509.SHA512WithRSAPSS
189 }
190
191 return x509.UnknownSignatureAlgorithm
192 }
193
194 // RFC 5280 4.2.2.1
195 type authorityInfoAccess struct {
196 Method asn1.ObjectIdentifier
197 Location asn1.RawValue
198 }
199
200 // RFC 5280 4.2.1.1
201 type authKeyID struct {
202 ID []byte `asn1:"optional,tag:0"`
203 IssuerName asn1.RawValue `asn1:"set,optional,tag:1"`
204 SerialNumber *big.Int `asn1:"optional,tag:2"`
205 }
206
207 // RFC 5280 4.2.1.4
208 type cpsPolicy struct {
209 ID asn1.ObjectIdentifier
210 Value string
211 }
212
213 // RFC 5280 4.2.1.4
214 type policyInformation struct {
215 Raw asn1.RawContent
216 ID asn1.ObjectIdentifier
217 Policy asn1.RawValue
218 }
219
220 // RFC 5280 4.1.2.5
221 type validity struct {
222 NotBefore, NotAfter time.Time
223 }
224
225 // RFC 5280 4.2.1.4
226 type noticeReference struct {
227 Organization string
228 NoticeNumbers []int
229 }
230
231 // RFC 5280 4.2.1.4
232 type userNotice struct {
233 NoticeRef noticeReference `asn1:"optional"`
234 ExplicitText string `asn1:"optional"`
235 }
236
237 // RFC 5755 4.1
238 type objectDigestInfo struct {
239 DigestedObjectType asn1.Enumerated
240 OtherObjectTypeID asn1.ObjectIdentifier `asn1:"optional"`
241 DigestAlgorithm pkix.AlgorithmIdentifier
242 ObjectDigest asn1.BitString
243 }
244
245 // RFC 5755 4.1
246 type attCertIssuer struct {
247 IssuerName asn1.RawValue `asn1:"set,optional"`
248 BaseCertificateID issuerSerial `asn1:"optional,tag:0"`
249 ObjectDigestInfo objectDigestInfo `asn1:"optional,tag:1"`
250 }
251
252 // RFC 5755 4.1
253 type issuerSerial struct {
254 Raw asn1.RawContent
255 Issuer asn1.RawValue
256 Serial *big.Int
257 IssuerUID asn1.BitString `asn1:"optional"`
258 }
259
260 // RFC 5755 4.1
261 type holder struct {
262 Raw asn1.RawContent
263 BaseCertificateID issuerSerial `asn1:"optional,tag:0"`
264 EntityName pkix.Extension `asn1:"optional,tag:1"`
265 ObjectDigestInfo objectDigestInfo `asn1:"optional,tag:2"`
266 }
267
268 // RFC 5755 4.1
269 type attribute struct {
270 ID asn1.ObjectIdentifier
271 RawValues []asn1.RawValue `asn1:"set"`
272 }
273
274 // RFC 5755 4.1
275 type tbsAttributeCertificate struct {
276 Raw asn1.RawContent
277 Version int
278 Holder holder
279 Issuer attCertIssuer `asn1:"tag:0"`
280 SignatureAlgorithm pkix.AlgorithmIdentifier
281 SerialNumber *big.Int
282 Validity validity
283 Attributes []attribute
284 IssuerUniqueID asn1.BitString `asn1:"optional"`
285 Extensions []pkix.Extension `asn1:"optional"`
286 }
287
288 type attributeCertificate struct {
289 Raw asn1.RawContent
290 TBSAttributeCertificate tbsAttributeCertificate
291 SignatureAlgorithm pkix.AlgorithmIdentifier
292 SignatureValue asn1.BitString
293 }
294
295 type Certholder struct {
296 Issuer pkix.Name
297 Serial *big.Int
298 }
299
300 type Component struct {
301 Manufacturer string
302 Model string
303 Serial string
304 Revision string
305 ManufacturerID int64
306 FieldReplaceable bool
307 Addresses []ComponentAddress
308 }
309
310 type AttributeCertificate struct {
311 Raw []byte // Complete ASN.1 DER content (certificate, signature algorithm and signature).
312 RawTBSAttributeCertificate []byte // Certificate part of raw ASN.1 DER content.
313
314 Signature []byte
315 SignatureAlgorithm x509.SignatureAlgorithm
316
317 Version int
318 SerialNumber *big.Int
319 Holder Certholder
320 Issuer pkix.Name
321 Subject pkix.Name
322 NotBefore, NotAfter time.Time // Validity bounds.
323 TCGPlatformSpecification TCGPlatformSpecification
324 TBBSecurityAssertions TBBSecurityAssertions
325 PlatformManufacturer string
326 PlatformModel string
327 PlatformVersion string
328 PlatformSerial string
329 CredentialSpecification string
330 UserNotice userNotice
331 Components []Component
332 Properties []Property
333 PropertiesURI string
334 }
335
336 // ParseAttributeCertificate parses a single attribute certificate from the
337 // given ASN.1 DER data.
338 func ParseAttributeCertificate(asn1Data []byte) (*AttributeCertificate, error) {
339 var cert attributeCertificate
340
341 rest, err := asn1.Unmarshal(asn1Data, &cert)
342 if err != nil {
343 return nil, err
344 }
345 if len(rest) != 0 {
346 return nil, asn1.SyntaxError{Msg: "attributecert: trailing data"}
347 }
348
349 return parseAttributeCertificate(&cert)
350 }
351
352 type PlatformDataSequence []PlatformDataSET
353 type PlatformDataSET []pkix.AttributeTypeAndValue
354
355 type TCGData struct {
356 ID asn1.ObjectIdentifier
357 Data string
358 }
359
360 type TCGDirectoryEntry struct {
361 ID asn1.ObjectIdentifier
362 Data asn1.RawValue
363 }
364
365 type TCGSpecificationVersion struct {
366 MajorVersion int
367 MinorVersion int
368 Revision int
369 }
370
371 type TCGPlatformSpecification struct {
372 Version TCGSpecificationVersion
373 }
374
375 type TCGCredentialSpecification struct {
376 Version TCGSpecificationVersion
377 }
378
379 type TCGCredentialType struct {
380 CertificateType asn1.ObjectIdentifier
381 }
382
383 type FipsLevel struct {
384 Version string
385 Level asn1.Enumerated
386 Plus bool `asn1:"optional,default=false"`
387 }
388
389 type CommonCriteriaMeasures struct {
390 Version string
391 AssuranceLevel asn1.Enumerated
392 EvaluationStatus asn1.Enumerated
393 Plus bool
394 StrengthOfFunction asn1.Enumerated `asn1:"optional,tag:0"`
395 ProfileOid asn1.ObjectIdentifier `asn1:"optional,tag:1"`
396 ProfileURI string `asn1:"optional,tag:2"`
397 TargetOid asn1.ObjectIdentifier `asn1:"optional,tag:3"`
398 TargetURI asn1.ObjectIdentifier `asn1:"optional,tag:4"`
399 }
400
401 type TBBSecurityAssertions struct {
402 Version int `asn1:"optional,default=0"`
403 CcInfo CommonCriteriaMeasures `asn1:"optional,tag:0"`
404 FipsLevel FipsLevel `asn1:"optional,tag:1"`
405 RtmType asn1.Enumerated `asn1:"optional,tag:2"`
406 Iso9000Certified bool `asn1:"optional,default=false"`
407 Iso9000URI string `asn1:"optional"`
408 }
409
410 // Certificates with this information in the SDA region appear to fail to
411 // tag the optional fields
412 type CommonCriteriaMeasures_sda struct {
413 Version string
414 AssuranceLevel asn1.Enumerated
415 EvaluationStatus asn1.Enumerated
416 Plus bool `asn1:"optional,default=false"`
417 StrengthOfFunction asn1.Enumerated `asn1:"optional"`
418 ProfileOid asn1.ObjectIdentifier `asn1:"optional"`
419 ProfileURI string `asn1:"optional"`
420 TargetOid asn1.ObjectIdentifier `asn1:"optional"`
421 TargetURI asn1.ObjectIdentifier `asn1:"optional"`
422 }
423
424 type TBBSecurityAssertions_sda struct {
425 Version int
426 CcInfo CommonCriteriaMeasures_sda `asn1:"optional"`
427 FipsLevel FipsLevel `asn1:"optional"`
428 RtmType asn1.Enumerated `asn1:"optional"`
429 Iso9000Certified bool `asn1:"optional"`
430 Iso9000URI string `asn1:"optional"`
431 }
432
433 type Property struct {
434 PropertyName string
435 PropertyValue string
436 Status asn1.Enumerated `asn1:"optional,tag:0"`
437 }
438
439 type AttributeCertificateIdentifier struct {
440 HashAlgorithm pkix.AlgorithmIdentifier
441 HashOverSignatureValue string
442 }
443
444 type CertificateIdentifier struct {
445 AttributeCertIdentifier AttributeCertificateIdentifier `asn1:"optional,tag:0"`
446 GenericCertIdientifier issuerSerial `asn1:"optional,tag:1"`
447 }
448
449 type ComponentAddress struct {
450 AddressType asn1.ObjectIdentifier
451 AddressValue string
452 }
453
454 type ComponentClass struct {
455 ComponentClassRegistry asn1.ObjectIdentifier
456 ComponentClassValue []byte
457 }
458
459 type ComponentIdentifierV2 struct {
460 ComponentClass ComponentClass
461 ComponentManufacturer string
462 ComponentModel string
463 ComponentSerial string `asn1:"optional,utf8,tag:0"`
464 ComponentRevision string `asn1:"optional,utf8,tag:1"`
465 ComponentManufacturerID int64 `asn1:"optional,tag:2"`
466 FieldReplaceable bool `asn1:"optional,tag:3"`
467 ComponentAddresses []ComponentAddress `asn1:"optional,tag:4"`
468 ComponentPlatformCert CertificateIdentifier `asn1:"optional,tag:5"`
469 ComponentPlatformCertURI string `asn1:"optional,tag:6"`
470 Status asn1.Enumerated `asn1:"optional,tag:7"`
471 }
472
473 type URIReference struct {
474 UniformResourceIdentifier string
475 HashAlgorithm pkix.AlgorithmIdentifier `asn1:"optional"`
476 HashValue string `asn1:"optional"`
477 }
478
479 type PlatformConfigurationV2 struct {
480 ComponentIdentifiers []ComponentIdentifierV2 `asn1:"optional,tag:0"`
481 ComponentIdentifiersURI URIReference `asn1:"optional,tag:1"`
482 PlatformProperties []Property `asn1:"optional,tag:2"`
483 PlatformPropertiesURI URIReference `asn1:"optional,tag:3"`
484 }
485
486 type PlatformConfigurationV2Workaround struct {
487 ComponentIdentifiers []ComponentIdentifierV2 `asn1:"optional,tag:0"`
488 ComponentIdentifiersURI URIReference `asn1:"optional,tag:1"`
489 PlatformProperty Property `asn1:"optional,tag:2"`
490 PlatformPropertiesURI URIReference `asn1:"optional,tag:3"`
491 }
492
493 type ComponentIdentifierV1 struct {
494 ComponentClass []byte `asn1:"optional"`
495 ComponentManufacturer string
496 ComponentModel string
497 ComponentSerial string `asn1:"optional,utf8,tag:0"`
498 ComponentRevision string `asn1:"optional,utf8,tag:1"`
499 ComponentManufacturerID int64 `asn1:"optional,tag:2"`
500 FieldReplaceable bool `asn1:"optional,tag:3"`
501 ComponentAddresses []ComponentAddress `asn1:"optional,tag:4"`
502 }
503
504 type PlatformConfigurationV1 struct {
505 ComponentIdentifiers []ComponentIdentifierV1 `asn1:"optional,tag:0"`
506 PlatformProperties []Property `asn1:"optional,tag:1"`
507 PlatformPropertiesURI URIReference `asn1:"optional,tag:2"`
508 }
509
510 func unmarshalSAN(v asn1.RawValue) ([]pkix.AttributeTypeAndValue, error) {
511 switch v.Tag {
512 case asn1.TagSet:
513 var e pkix.AttributeTypeAndValue
514 if _, err := asn1.Unmarshal(v.Bytes, &e); err != nil {
515 return nil, err
516 }
517 return []pkix.AttributeTypeAndValue{e}, nil
518 case asn1.TagOctetString:
519 var attributes []pkix.AttributeTypeAndValue
520 var platformData PlatformDataSequence
521 rest, err := asn1.Unmarshal(v.Bytes, &platformData)
522 if err != nil {
523 return nil, err
524 }
525 if len(rest) != 0 {
526 return nil, errors.New("attributecert: trailing data after X.509 subject")
527 }
528 for _, e := range platformData {
529 for _, e2 := range e {
530 attributes = append(attributes, e2)
531 }
532 }
533 return attributes, nil
534 default:
535 return nil, fmt.Errorf("attributecert: unexpected SAN type %v", v.Tag)
536 }
537 }
538
539 func parseAttributeCertificate(in *attributeCertificate) (*AttributeCertificate, error) {
540 out := &AttributeCertificate{
541 Raw: in.Raw,
542 RawTBSAttributeCertificate: in.TBSAttributeCertificate.Raw,
543 Signature: in.SignatureValue.RightAlign(),
544 SignatureAlgorithm: getSignatureAlgorithmFromAI(in.TBSAttributeCertificate.SignatureAlgorithm),
545 Version: in.TBSAttributeCertificate.Version + 1,
546 SerialNumber: in.TBSAttributeCertificate.SerialNumber,
547 }
548
549 var v asn1.RawValue
550 if _, err := asn1.Unmarshal(in.TBSAttributeCertificate.Issuer.IssuerName.Bytes, &v); err != nil {
551 return nil, err
552 }
553
554 var issuer pkix.RDNSequence
555 if rest, err := asn1.Unmarshal(v.Bytes, &issuer); err != nil {
556 return nil, err
557 } else if len(rest) != 0 {
558 return nil, errors.New("attributecert: trailing data after X.509 subject")
559 }
560
561 out.Issuer.FillFromRDNSequence(&issuer)
562 if _, err := asn1.Unmarshal(in.TBSAttributeCertificate.Holder.BaseCertificateID.Issuer.Bytes, &v); err != nil {
563 return nil, err
564 }
565
566 var holder pkix.RDNSequence
567 if rest, err := asn1.Unmarshal(v.Bytes, &holder); err != nil {
568 return nil, err
569 } else if len(rest) != 0 {
570 return nil, errors.New("attributecert: trailing data after X.509 subject")
571 }
572
573 out.Holder.Issuer.FillFromRDNSequence(&holder)
574 out.Holder.Serial = in.TBSAttributeCertificate.Holder.BaseCertificateID.Serial
575
576 out.NotBefore = in.TBSAttributeCertificate.Validity.NotBefore
577 out.NotAfter = in.TBSAttributeCertificate.Validity.NotAfter
578
579 for _, attribute := range in.TBSAttributeCertificate.Attributes {
580 switch {
581 case attribute.ID.Equal(oidAttributeUserNotice):
582 if _, err := asn1.Unmarshal(attribute.RawValues[0].FullBytes, &out.UserNotice); err != nil {
583 return nil, err
584 }
585 case attribute.ID.Equal(oid.TCGPlatformSpecification):
586 if _, err := asn1.Unmarshal(attribute.RawValues[0].FullBytes, &out.TCGPlatformSpecification); err != nil {
587 return nil, err
588 }
589 case attribute.ID.Equal(oid.TBBSecurityAssertions):
590 if _, err := asn1.Unmarshal(attribute.RawValues[0].FullBytes, &out.TBBSecurityAssertions); err != nil {
591 return nil, err
592 }
593 case attribute.ID.Equal(oid.TCGCredentialSpecification):
594 var credentialSpecification TCGCredentialSpecification
595 if _, err := asn1.Unmarshal(attribute.RawValues[0].FullBytes, &credentialSpecification); err != nil {
596 var credentialSpecification TCGSpecificationVersion
597 if _, err := asn1.Unmarshal(attribute.RawValues[0].FullBytes, &credentialSpecification); err != nil {
598 return nil, err
599 }
600 }
601 case attribute.ID.Equal(oid.TCGCredentialType):
602 var credentialType TCGCredentialType
603 if _, err := asn1.Unmarshal(attribute.RawValues[0].FullBytes, &credentialType); err != nil {
604 return nil, err
605 }
606 case attribute.ID.Equal(oid.PlatformConfigurationV1):
607 var platformConfiguration PlatformConfigurationV1
608 if _, err := asn1.Unmarshal(attribute.RawValues[0].FullBytes, &platformConfiguration); err != nil {
609 return nil, err
610 }
611 for _, component := range platformConfiguration.ComponentIdentifiers {
612 t := Component{
613 Manufacturer: component.ComponentManufacturer,
614 Model: component.ComponentModel,
615 Serial: component.ComponentSerial,
616 Revision: component.ComponentRevision,
617 ManufacturerID: component.ComponentManufacturerID,
618 FieldReplaceable: component.FieldReplaceable,
619 Addresses: component.ComponentAddresses,
620 }
621 out.Components = append(out.Components, t)
622 }
623 out.Properties = platformConfiguration.PlatformProperties
624 out.PropertiesURI = platformConfiguration.PlatformPropertiesURI.UniformResourceIdentifier
625 case attribute.ID.Equal(oid.PlatformConfigurationV2):
626 var platformConfiguration PlatformConfigurationV2
627 if _, err := asn1.Unmarshal(attribute.RawValues[0].FullBytes, &platformConfiguration); err != nil {
628 var workaround PlatformConfigurationV2Workaround
629 if _, err := asn1.Unmarshal(attribute.RawValues[0].FullBytes, &workaround); err != nil {
630 return nil, err
631 }
632 platformConfiguration.ComponentIdentifiers = workaround.ComponentIdentifiers
633 platformConfiguration.ComponentIdentifiersURI = workaround.ComponentIdentifiersURI
634 platformConfiguration.PlatformProperties = append(platformConfiguration.PlatformProperties, workaround.PlatformProperty)
635 platformConfiguration.PlatformPropertiesURI = workaround.PlatformPropertiesURI
636 }
637 for _, component := range platformConfiguration.ComponentIdentifiers {
638 t := Component{
639 Manufacturer: component.ComponentManufacturer,
640 Model: component.ComponentModel,
641 Serial: component.ComponentSerial,
642 Revision: component.ComponentRevision,
643 ManufacturerID: component.ComponentManufacturerID,
644 FieldReplaceable: component.FieldReplaceable,
645 Addresses: component.ComponentAddresses,
646 }
647 out.Components = append(out.Components, t)
648 }
649 out.Properties = platformConfiguration.PlatformProperties
650 out.PropertiesURI = platformConfiguration.PlatformPropertiesURI.UniformResourceIdentifier
651 case attribute.ID.Equal(oid.PlatformConfigURI):
652 var platformConfigurationURI URIReference
653 if _, err := asn1.Unmarshal(attribute.RawValues[0].FullBytes, &platformConfigurationURI); err != nil {
654 return nil, err
655 }
656 default:
657 return nil, fmt.Errorf("attributecert: unknown attribute %v", attribute.ID)
658 }
659 }
660
661 for _, extension := range in.TBSAttributeCertificate.Extensions {
662 switch {
663 case extension.Id.Equal(oid.SubjectAltName):
664 var seq asn1.RawValue
665 rest, err := asn1.Unmarshal(extension.Value, &seq)
666 if err != nil {
667 return nil, err
668 }
669 if len(rest) != 0 {
670 return nil, errors.New("attributecert: trailing data after X.509 extension")
671 }
672 rest = seq.Bytes
673 for len(rest) > 0 {
674 var v asn1.RawValue
675 rest, err = asn1.Unmarshal(rest, &v)
676 if err != nil {
677 return nil, err
678 }
679 tcgdata, err := unmarshalSAN(v)
680 if err != nil {
681 return nil, fmt.Errorf("attributecert: failed to unmarshal SAN: %v", err)
682 }
683 for _, e := range tcgdata {
684 switch {
685 case e.Type.Equal(oidTcgPlatformManufacturerStrV1):
686 out.PlatformManufacturer = e.Value.(string)
687 case e.Type.Equal(oidTcgPlatformModelV1):
688 out.PlatformModel = e.Value.(string)
689 case e.Type.Equal(oidTcgPlatformVersionV1):
690 out.PlatformVersion = e.Value.(string)
691 case e.Type.Equal(oid.TCGCredentialSpecification):
692 // This OID appears to be misused in this context
693 out.PlatformSerial = e.Value.(string)
694 case e.Type.Equal(oid.PlatformManufacturerStr):
695 out.PlatformManufacturer = e.Value.(string)
696 case e.Type.Equal(oid.PlatformManufacturerID):
697 // We can't parse these out at present
698 case e.Type.Equal(oid.PlatformModel):
699 out.PlatformModel = e.Value.(string)
700 case e.Type.Equal(oid.PlatformVersion):
701 out.PlatformVersion = e.Value.(string)
702 case e.Type.Equal(oid.PlatformSerial):
703 out.PlatformSerial = e.Value.(string)
704 default:
705 return nil, fmt.Errorf("attributecert: unhandled attribute: %v", e.Type)
706 }
707 }
708 }
709
710 case extension.Id.Equal(oid.SubjectDirectoryAttributes):
711 var seq asn1.RawValue
712 rest, err := asn1.Unmarshal(extension.Value, &seq)
713 if err != nil {
714 return nil, err
715 }
716 if len(rest) != 0 {
717 return nil, errors.New("attributecert: trailing data after X.509 extension")
718 }
719 rest = seq.Bytes
720 for len(rest) > 0 {
721 var e TCGDirectoryEntry
722 rest, err = asn1.Unmarshal(rest, &e)
723 if err != nil {
724 return nil, err
725 }
726 switch {
727 case e.ID.Equal(oid.TCGPlatformSpecification):
728 var platformSpecification TCGPlatformSpecification
729 _, err := asn1.Unmarshal(e.Data.Bytes, &platformSpecification)
730 if err != nil {
731 return nil, err
732 }
733 out.TCGPlatformSpecification = platformSpecification
734 case e.ID.Equal(oid.TBBSecurityAssertions):
735 var securityAssertions TBBSecurityAssertions_sda
736 _, err := asn1.Unmarshal(e.Data.Bytes, &securityAssertions)
737 if err != nil {
738 return nil, err
739 }
740 out.TBBSecurityAssertions.Version = securityAssertions.Version
741 out.TBBSecurityAssertions.CcInfo = CommonCriteriaMeasures(securityAssertions.CcInfo)
742 out.TBBSecurityAssertions.FipsLevel = securityAssertions.FipsLevel
743 out.TBBSecurityAssertions.RtmType = securityAssertions.RtmType
744 out.TBBSecurityAssertions.Iso9000Certified = securityAssertions.Iso9000Certified
745 out.TBBSecurityAssertions.Iso9000URI = securityAssertions.Iso9000URI
746 default:
747 return nil, fmt.Errorf("attributecert: unhandled TCG directory attribute: %v", e.ID)
748 }
749 }
750
751 case extension.Id.Equal(oid.CertificatePolicies):
752 var policies []policyInformation
753 _, err := asn1.Unmarshal(extension.Value, &policies)
754 if err != nil {
755 return nil, err
756 }
757 for _, policy := range policies {
758 if policy.ID.Equal(oidTcgCertificatePolicy) {
759 var subpolicies []policyInformation
760 _, err := asn1.Unmarshal(policy.Policy.FullBytes, &subpolicies)
761 if err != nil {
762 return nil, err
763 }
764 for _, subpolicy := range subpolicies {
765 switch {
766 case subpolicy.ID.Equal(oidCpsCertificatePolicy):
767 var cpsPolicy cpsPolicy
768 _, err := asn1.Unmarshal(subpolicy.Raw, &cpsPolicy)
769 if err != nil {
770 return nil, err
771 }
772 case subpolicy.ID.Equal(oidAttributeUserNotice):
773 var userNotice string
774 _, err := asn1.Unmarshal(subpolicy.Policy.Bytes, &userNotice)
775 if err != nil {
776 return nil, err
777 }
778 default:
779 return nil, fmt.Errorf("attributecert: unhandled certificate policy: %v", subpolicy.ID)
780 }
781 }
782 }
783 }
784
785 case extension.Id.Equal(oidExtensionAuthorityKeyIdentifier):
786 var a authKeyID
787 _, err := asn1.Unmarshal(extension.Value, &a)
788 if err != nil {
789 return nil, err
790 }
791
792 case extension.Id.Equal(oidAuthorityInfoAccess):
793 var aia []authorityInfoAccess
794 _, err := asn1.Unmarshal(extension.Value, &aia)
795 if err != nil {
796 return nil, err
797 }
798 for _, v := range aia {
799 if v.Method.Equal(oidAuthorityInfoAccessOcsp) {
800 // TODO
801 } else if v.Method.Equal(oidAuthorityInfoAccessIssuers) {
802 // TODO
803 } else {
804 return nil, fmt.Errorf("attributecert: unhandled Authority Info Access type %v", v.Method)
805 }
806 }
807
808 default:
809 return nil, fmt.Errorf("attributecert: unknown extension ID %v", extension.Id)
810 }
811 }
812
813 return out, nil
814 }
815
816 // CheckSignatureFrom verifies that the signature on c is a valid signature
817 // from parent.
818 func (c *AttributeCertificate) CheckSignatureFrom(parent *x509.Certificate) error {
819 if parent.KeyUsage != 0 && parent.KeyUsage&x509.KeyUsageCertSign == 0 {
820 return x509.ConstraintViolationError{}
821 }
822
823 if parent.PublicKeyAlgorithm == x509.UnknownPublicKeyAlgorithm {
824 return x509.ErrUnsupportedAlgorithm
825 }
826
827 // TODO(agl): don't ignore the path length constraint.
828
829 return parent.CheckSignature(c.SignatureAlgorithm, c.RawTBSAttributeCertificate, c.Signature)
830 }