1919package didx509
2020
2121import (
22+ "bytes"
2223 "crypto/x509"
2324 "errors"
2425 "fmt"
@@ -47,8 +48,8 @@ const (
4748
4849var (
4950
50- // ErrX509ChainMissing is returned when the x509 root certificate chain is not present in the metadata.
51- ErrX509ChainMissing = errors .New ("x509 rootCert chain is missing " )
51+ // ErrX509ChainMissing indicates that no x5c header was found in the provided metadata.
52+ ErrX509ChainMissing = errors .New ("no x5c header found " )
5253
5354 // ErrNoCertsInHeaders indicates that no x5t or x5t#S256 header was found in the provided metadata.
5455 ErrNoCertsInHeaders = errors .New ("no x5t or x5t#S256 header found" )
@@ -76,11 +77,14 @@ type X509DidPolicy struct {
7677 Value string
7778}
7879
79- // X509DidReference represents a reference for an X.509 Decentralized Identifier (DID) including method, root certificate, and policies .
80+ // X509DidReference represents a reference for an X.509 Decentralized Identifier (DID).
8081type X509DidReference struct {
81- Method HashAlgorithm
82- RootCertRef string
83- Policies []X509DidPolicy
82+ // Method specifies the hash algorithm that was used to generate CAFingerprint from the raw DER bytes of the CA certificate.
83+ Method HashAlgorithm
84+ // CAFingerprint is the fingerprint of the CA certificate.
85+ CAFingerprint string
86+ // Policies contain the fields that are included in the did:x509, which must be validated against the certificates.
87+ Policies []X509DidPolicy
8488}
8589
8690// Resolve resolves a DID document given its identifier and corresponding metadata.
@@ -107,14 +111,17 @@ func (r Resolver) Resolve(id did.DID, metadata *resolver.ResolveMetadata) (*did.
107111 if err != nil {
108112 return nil , nil , fmt .Errorf ("did:x509 x5c certificate parsing: %w" , err )
109113 }
110- _ , err = findCertificateByHash (chain , ref .RootCertRef , ref .Method )
114+ caFingerprintCert , err : = findCertificateByHash (chain , ref .CAFingerprint , ref .Method )
111115 if err != nil {
112116 return nil , nil , err
113117 }
114118 validationCert , err := findValidationCertificate (metadata , chain )
115119 if err != nil {
116120 return nil , nil , err
117121 }
122+ if bytes .Equal (caFingerprintCert .Raw , validationCert .Raw ) {
123+ return nil , nil , fmt .Errorf ("did:x509 ca-fingerprint refers to leaf certificate, must be either root or intermediate CA certificate" )
124+ }
118125
119126 // Validate certificate chain, checking signatures and whether the chain is complete
120127 var chainWithoutLeaf []* x509.Certificate
@@ -225,7 +232,7 @@ func parseX509Did(id did.DID) (*X509DidReference, error) {
225232 return nil , ErrDidVersion
226233 }
227234 ref .Method = HashAlgorithm (didParts [1 ])
228- ref .RootCertRef = didParts [2 ]
235+ ref .CAFingerprint = didParts [2 ]
229236
230237 for _ , policyString := range policyStrings {
231238 policyFragments := strings .Split (policyString , ":" )
0 commit comments