Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 5d6ca32

Browse files
Import cert sanitation fix from fabric main repo (#287)
Signed-off-by: Artem Barger <artem@bargr.net> Co-authored-by: Andrew Coleman <andrew_coleman@uk.ibm.com>
1 parent 32aa92a commit 5d6ca32

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

internal/github.com/hyperledger/fabric/msp/cert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ func sanitizeECDSASignedCert(cert *x509.Certificate, parentCert *x509.Certificat
107107

108108
// 2. Change the signature
109109
newCert.SignatureValue = asn1.BitString{Bytes: expectedSig, BitLength: len(expectedSig) * 8}
110+
newCert.Raw = nil
110111

111112
// 3. marshal again newCert. Raw must be nil
112-
newCert.Raw = nil
113113
newRaw, err := asn1.Marshal(newCert)
114114
if err != nil {
115115
return nil, errors.Wrap(err, "marshalling of the certificate failed")

internal/github.com/hyperledger/fabric/msp/mspimpl.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type validateIdentityOUsFuncType func(id *identity) error
3434
// satisfiesPrincipalInternalFuncType is the prototype of the function to check if principals are satisfied
3535
type satisfiesPrincipalInternalFuncType func(id Identity, principal *m.MSPPrincipal) error
3636

37-
//setupAdminInternalFuncType is a prototype of the function to setup the admins
37+
// setupAdminInternalFuncType is a prototype of the function to setup the admins
3838
type setupAdminInternalFuncType func(conf *m.FabricMSPConfig) error
3939

4040
// This is an instantiation of an MSP that
@@ -778,28 +778,42 @@ func (msp *bccspmsp) getCertificationChainIdentifierFromChain(chain []*x509.Cert
778778
// do have signatures in Low-S. If this is not the case, the certificate
779779
// is regenerated to have a Low-S signature.
780780
func (msp *bccspmsp) sanitizeCert(cert *x509.Certificate) (*x509.Certificate, error) {
781+
var err error
781782
if isECDSASignedCert(cert) {
783+
isRootCACert := false
784+
validityOpts := msp.getValidityOptsForCert(cert)
785+
if cert.IsCA && cert.CheckSignatureFrom(cert) == nil {
786+
// this is a root CA we can already sanitize it
787+
cert, err = sanitizeECDSASignedCert(cert, cert)
788+
if err != nil {
789+
return nil, err
790+
}
791+
isRootCACert = true
792+
validityOpts.Roots = x509.NewCertPool()
793+
validityOpts.Roots.AddCert(cert)
794+
}
782795
// Lookup for a parent certificate to perform the sanitization
783-
var parentCert *x509.Certificate
784-
chain, err := msp.getUniqueValidationChain(cert, msp.getValidityOptsForCert(cert))
796+
// run cert validation at any rate, if this is a root CA
797+
// we will validate already sanitized cert
798+
chain, err := msp.getUniqueValidationChain(cert, validityOpts)
785799
if err != nil {
786800
return nil, err
787801
}
788802

789-
// at this point, cert might be a root CA certificate
790-
// or an intermediate CA certificate
791-
if cert.IsCA && len(chain) == 1 {
792-
// cert is a root CA certificate
793-
parentCert = cert
794-
} else {
795-
parentCert = chain[1]
803+
// once we finish validation and this is already
804+
// sanitized certificate, there is no need to
805+
// sanitize it once again hence we can just return it
806+
if isRootCACert {
807+
return cert, nil
796808
}
797809

810+
// ok, this is no a root CA cert, and now we
811+
// then we have chain of certs and can get parent
812+
// to sanitize the cert whenever it's intermediate or leaf certificate
813+
parentCert := chain[1]
814+
798815
// Sanitize
799-
cert, err = sanitizeECDSASignedCert(cert, parentCert)
800-
if err != nil {
801-
return nil, err
802-
}
816+
return sanitizeECDSASignedCert(cert, parentCert)
803817
}
804818
return cert, nil
805819
}

0 commit comments

Comments
 (0)