@@ -680,11 +680,14 @@ func (msp *bccspmsp) getCertificationChainForBCCSPIdentity(id *identity) ([]*x50
680
680
return nil , errors .New ("A CA certificate cannot be used directly by this MSP" )
681
681
}
682
682
683
- return msp .getValidationChain (id .cert )
683
+ return msp .getValidationChain (id .cert , false )
684
684
}
685
685
686
686
func (msp * bccspmsp ) getUniqueValidationChain (cert * x509.Certificate ) ([]* x509.Certificate , error ) {
687
687
// ask golang to validate the cert for us based on the options that we've built at setup time
688
+ if msp .opts == nil {
689
+ return nil , fmt .Errorf ("The supplied identity has no verify options" )
690
+ }
688
691
validationChains , err := cert .Verify (* (msp .opts ))
689
692
if err != nil {
690
693
return nil , fmt .Errorf ("The supplied identity is not valid, Verify() returned %s" , err )
@@ -700,7 +703,7 @@ func (msp *bccspmsp) getUniqueValidationChain(cert *x509.Certificate) ([]*x509.C
700
703
return validationChains [0 ], nil
701
704
}
702
705
703
- func (msp * bccspmsp ) getValidationChain (cert * x509.Certificate ) ([]* x509.Certificate , error ) {
706
+ func (msp * bccspmsp ) getValidationChain (cert * x509.Certificate , isIntermediateChain bool ) ([]* x509.Certificate , error ) {
704
707
validationChain , err := msp .getUniqueValidationChain (cert )
705
708
if err != nil {
706
709
return nil , fmt .Errorf ("Failed getting validation chain %s" , err )
@@ -712,10 +715,14 @@ func (msp *bccspmsp) getValidationChain(cert *x509.Certificate) ([]*x509.Certifi
712
715
}
713
716
714
717
// check that the parent is a leaf of the certification tree
715
- if msp .certificationTreeInternalNodesMap [string (validationChain [1 ].Raw )] {
718
+ // if validating an intermediate chain, the first certificate will the parent
719
+ parentPosition := 1
720
+ if isIntermediateChain {
721
+ parentPosition = 0
722
+ }
723
+ if msp .certificationTreeInternalNodesMap [string (validationChain [parentPosition ].Raw )] {
716
724
return nil , fmt .Errorf ("Invalid validation chain. Parent certificate should be a leaf of the certification tree [%v]." , cert .Raw )
717
725
}
718
-
719
726
return validationChain , nil
720
727
}
721
728
@@ -753,14 +760,21 @@ func (msp *bccspmsp) getCertificationChainIdentifierFromChain(chain []*x509.Cert
753
760
func (msp * bccspmsp ) setupOUs (conf m.FabricMSPConfig ) error {
754
761
msp .ouIdentifiers = make (map [string ][][]byte )
755
762
for _ , ou := range conf .OrganizationalUnitIdentifiers {
756
- // 1. check that it registered in msp.rootCerts or msp.intermediateCerts
763
+
764
+ // 1. check that certificate is registered in msp.rootCerts or msp.intermediateCerts
757
765
cert , err := msp .getCertFromPem (ou .Certificate )
758
766
if err != nil {
759
767
return fmt .Errorf ("Failed getting certificate for [%v]: [%s]" , ou , err )
760
768
}
761
769
770
+ // 2. Sanitize it to ensure like for like comparison
771
+ cert , err = msp .sanitizeCert (cert )
772
+ if err != nil {
773
+ return fmt .Errorf ("sanitizeCert failed %s" , err )
774
+ }
775
+
762
776
found := false
763
- root := true
777
+ root := false
764
778
// Search among root certificates
765
779
for _ , v := range msp .rootCerts {
766
780
if v .(* identity ).cert .Equal (cert ) {
@@ -783,19 +797,19 @@ func (msp *bccspmsp) setupOUs(conf m.FabricMSPConfig) error {
783
797
return fmt .Errorf ("Failed adding OU. Certificate [%v] not in root or intermediate certs." , ou .Certificate )
784
798
}
785
799
786
- // 2 . get the certification path for it
800
+ // 3 . get the certification path for it
787
801
var certifiersIdentitifer []byte
788
802
var chain []* x509.Certificate
789
803
if root {
790
804
chain = []* x509.Certificate {cert }
791
805
} else {
792
- chain , err = msp .getValidationChain (cert )
806
+ chain , err = msp .getValidationChain (cert , true )
793
807
if err != nil {
794
808
return fmt .Errorf ("Failed computing validation chain for [%v]. [%s]" , cert , err )
795
809
}
796
810
}
797
811
798
- // 3 . compute the hash of the certification path
812
+ // 4 . compute the hash of the certification path
799
813
certifiersIdentitifer , err = msp .getCertificationChainIdentifierFromChain (chain )
800
814
if err != nil {
801
815
return fmt .Errorf ("Failed computing Certifiers Identifier for [%v]. [%s]" , ou .Certificate , err )
@@ -969,6 +983,9 @@ func (msp *bccspmsp) validateIdentityOUs(id *identity) error {
969
983
}
970
984
971
985
if ! found {
986
+ if len (id .GetOrganizationalUnits ()) == 0 {
987
+ return fmt .Errorf ("The identity certificate does not contain an Organizational Unit (OU)" )
988
+ }
972
989
return fmt .Errorf ("None of the identity's organizational units [%v] are in MSP %s" , id .GetOrganizationalUnits (), msp .name )
973
990
}
974
991
}
0 commit comments