@@ -368,23 +368,6 @@ func (msp *bccspmsp) Setup(conf1 *m.MSPConfig) error {
368
368
msp .admins [i ] = id
369
369
}
370
370
371
- // ensure that our CAs are properly formed
372
- for _ , cert := range append (append ([]Identity {}, msp .rootCerts ... ), msp .intermediateCerts ... ) {
373
- if ! isCACert (cert .(* identity ).cert ) {
374
- return fmt .Errorf ("CA Certificate did not have the Subject Key Identifier extension, (SN: %s)" , cert .(* identity ).cert .SerialNumber )
375
- }
376
- }
377
-
378
- // setup the signer (if present)
379
- if conf .SigningIdentity != nil {
380
- sid , err := msp .getSigningIdentityFromConf (conf .SigningIdentity )
381
- if err != nil {
382
- return err
383
- }
384
-
385
- msp .signer = sid
386
- }
387
-
388
371
// setup the CRL (if present)
389
372
msp .CRL = make ([]* pkix.CertificateList , len (conf .RevocationList ))
390
373
for i , crlbytes := range conf .RevocationList {
@@ -401,6 +384,27 @@ func (msp *bccspmsp) Setup(conf1 *m.MSPConfig) error {
401
384
msp .CRL [i ] = crl
402
385
}
403
386
387
+ // ensure that our CAs are properly formed and that they are valid
388
+ for _ , id := range append (append ([]Identity {}, msp .rootCerts ... ), msp .intermediateCerts ... ) {
389
+ if ! isCACert (id .(* identity ).cert ) {
390
+ return fmt .Errorf ("CA Certificate did not have the Subject Key Identifier extension, (SN: %s)" , id .(* identity ).cert .SerialNumber )
391
+ }
392
+
393
+ if err := msp .validateCAIdentity (id .(* identity )); err != nil {
394
+ return fmt .Errorf ("CA Certificate is not valid, (SN: %s) [%s]" , id .(* identity ).cert .SerialNumber , err )
395
+ }
396
+ }
397
+
398
+ // setup the signer (if present)
399
+ if conf .SigningIdentity != nil {
400
+ sid , err := msp .getSigningIdentityFromConf (conf .SigningIdentity )
401
+ if err != nil {
402
+ return err
403
+ }
404
+
405
+ msp .signer = sid
406
+ }
407
+
404
408
// setup the OUs
405
409
if err := msp .setupOUs (conf ); err != nil {
406
410
return err
@@ -461,82 +465,7 @@ func (msp *bccspmsp) Validate(id Identity) error {
461
465
// this is how I can validate it given the
462
466
// root of trust this MSP has
463
467
case * identity :
464
- validationChain , err := msp .getCertificationChainForBCCSPIdentity (id )
465
- if err != nil {
466
- return fmt .Errorf ("Could not obtain certification chain, err %s" , err )
467
- }
468
-
469
- // here we know that the identity is valid; now we have to check whether it has been revoked
470
-
471
- // identify the SKI of the CA that signed this cert
472
- SKI , err := getSubjectKeyIdentifierFromCert (validationChain [1 ])
473
- if err != nil {
474
- return fmt .Errorf ("Could not obtain Subject Key Identifier for signer cert, err %s" , err )
475
- }
476
-
477
- // check whether one of the CRLs we have has this cert's
478
- // SKI as its AuthorityKeyIdentifier
479
- for _ , crl := range msp .CRL {
480
- aki , err := getAuthorityKeyIdentifierFromCrl (crl )
481
- if err != nil {
482
- return fmt .Errorf ("Could not obtain Authority Key Identifier for crl, err %s" , err )
483
- }
484
-
485
- // check if the SKI of the cert that signed us matches the AKI of any of the CRLs
486
- if bytes .Equal (aki , SKI ) {
487
- // we have a CRL, check whether the serial number is revoked
488
- for _ , rc := range crl .TBSCertList .RevokedCertificates {
489
- if rc .SerialNumber .Cmp (id .cert .SerialNumber ) == 0 {
490
- // We have found a CRL whose AKI matches the SKI of
491
- // the CA (root or intermediate) that signed the
492
- // certificate that is under validation. As a
493
- // precaution, we verify that said CA is also the
494
- // signer of this CRL.
495
- err = validationChain [1 ].CheckCRLSignature (crl )
496
- if err != nil {
497
- // the CA cert that signed the certificate
498
- // that is under validation did not sign the
499
- // candidate CRL - skip
500
- mspLogger .Warningf ("Invalid signature over the identified CRL, error %s" , err )
501
- continue
502
- }
503
-
504
- // A CRL also includes a time of revocation so that
505
- // the CA can say "this cert is to be revoked starting
506
- // from this time"; however here we just assume that
507
- // revocation applies instantaneously from the time
508
- // the MSP config is committed and used so we will not
509
- // make use of that field
510
- return errors .New ("The certificate has been revoked" )
511
- }
512
- }
513
- }
514
- }
515
-
516
- // Check that the identity's OUs are compatible with those recognized by this MSP,
517
- // meaning that the intersection is not empty.
518
- if len (msp .ouIdentifiers ) > 0 {
519
- found := false
520
-
521
- for _ , OU := range id .GetOrganizationalUnits () {
522
- certificationIDs , exists := msp .ouIdentifiers [OU .OrganizationalUnitIdentifier ]
523
-
524
- if exists {
525
- for _ , certificationID := range certificationIDs {
526
- if bytes .Equal (certificationID , OU .CertifiersIdentifier ) {
527
- found = true
528
- break
529
- }
530
- }
531
- }
532
- }
533
-
534
- if ! found {
535
- return fmt .Errorf ("None of the identity's organizational units [%v] are in MSP %s" , id .GetOrganizationalUnits (), msp .name )
536
- }
537
- }
538
-
539
- return nil
468
+ return msp .validateIdentity (id )
540
469
default :
541
470
return fmt .Errorf ("Identity type not recognized" )
542
471
}
@@ -899,3 +828,117 @@ func (msp *bccspmsp) sanitizeCert(cert *x509.Certificate) (*x509.Certificate, er
899
828
}
900
829
return cert , nil
901
830
}
831
+
832
+ func (msp * bccspmsp ) validateIdentity (id * identity ) error {
833
+ validationChain , err := msp .getCertificationChainForBCCSPIdentity (id )
834
+ if err != nil {
835
+ return fmt .Errorf ("Could not obtain certification chain, err %s" , err )
836
+ }
837
+
838
+ err = msp .validateIdentityAgainstChain (id , validationChain )
839
+ if err != nil {
840
+ return fmt .Errorf ("Could not validate identity against certification chain, err %s" , err )
841
+ }
842
+
843
+ err = msp .validateIdentityOUs (id )
844
+ if err != nil {
845
+ return fmt .Errorf ("Could not validate identity's OUs, err %s" , err )
846
+ }
847
+
848
+ return nil
849
+ }
850
+
851
+ func (msp * bccspmsp ) validateCAIdentity (id * identity ) error {
852
+ if ! id .cert .IsCA {
853
+ return errors .New ("Only CA identities can be validated" )
854
+ }
855
+
856
+ validationChain , err := msp .getUniqueValidationChain (id .cert )
857
+ if err != nil {
858
+ return fmt .Errorf ("Could not obtain certification chain, err %s" , err )
859
+ }
860
+ if len (validationChain ) == 1 {
861
+ // validationChain[0] is the root CA certificate
862
+ return nil
863
+ }
864
+
865
+ return msp .validateIdentityAgainstChain (id , validationChain )
866
+ }
867
+
868
+ func (msp * bccspmsp ) validateIdentityAgainstChain (id * identity , validationChain []* x509.Certificate ) error {
869
+ // here we know that the identity is valid; now we have to check whether it has been revoked
870
+
871
+ // identify the SKI of the CA that signed this cert
872
+ SKI , err := getSubjectKeyIdentifierFromCert (validationChain [1 ])
873
+ if err != nil {
874
+ return fmt .Errorf ("Could not obtain Subject Key Identifier for signer cert, err %s" , err )
875
+ }
876
+
877
+ // check whether one of the CRLs we have has this cert's
878
+ // SKI as its AuthorityKeyIdentifier
879
+ for _ , crl := range msp .CRL {
880
+ aki , err := getAuthorityKeyIdentifierFromCrl (crl )
881
+ if err != nil {
882
+ return fmt .Errorf ("Could not obtain Authority Key Identifier for crl, err %s" , err )
883
+ }
884
+
885
+ // check if the SKI of the cert that signed us matches the AKI of any of the CRLs
886
+ if bytes .Equal (aki , SKI ) {
887
+ // we have a CRL, check whether the serial number is revoked
888
+ for _ , rc := range crl .TBSCertList .RevokedCertificates {
889
+ if rc .SerialNumber .Cmp (id .cert .SerialNumber ) == 0 {
890
+ // We have found a CRL whose AKI matches the SKI of
891
+ // the CA (root or intermediate) that signed the
892
+ // certificate that is under validation. As a
893
+ // precaution, we verify that said CA is also the
894
+ // signer of this CRL.
895
+ err = validationChain [1 ].CheckCRLSignature (crl )
896
+ if err != nil {
897
+ // the CA cert that signed the certificate
898
+ // that is under validation did not sign the
899
+ // candidate CRL - skip
900
+ mspLogger .Warningf ("Invalid signature over the identified CRL, error %s" , err )
901
+ continue
902
+ }
903
+
904
+ // A CRL also includes a time of revocation so that
905
+ // the CA can say "this cert is to be revoked starting
906
+ // from this time"; however here we just assume that
907
+ // revocation applies instantaneously from the time
908
+ // the MSP config is committed and used so we will not
909
+ // make use of that field
910
+ return errors .New ("The certificate has been revoked" )
911
+ }
912
+ }
913
+ }
914
+ }
915
+
916
+ return nil
917
+ }
918
+
919
+ func (msp * bccspmsp ) validateIdentityOUs (id * identity ) error {
920
+ // Check that the identity's OUs are compatible with those recognized by this MSP,
921
+ // meaning that the intersection is not empty.
922
+ if len (msp .ouIdentifiers ) > 0 {
923
+ found := false
924
+
925
+ for _ , OU := range id .GetOrganizationalUnits () {
926
+ certificationIDs , exists := msp .ouIdentifiers [OU .OrganizationalUnitIdentifier ]
927
+
928
+ if exists {
929
+ for _ , certificationID := range certificationIDs {
930
+ if bytes .Equal (certificationID , OU .CertifiersIdentifier ) {
931
+ found = true
932
+ break
933
+ }
934
+ }
935
+ }
936
+ }
937
+
938
+ if ! found {
939
+ return fmt .Errorf ("None of the identity's organizational units [%v] are in MSP %s" , id .GetOrganizationalUnits (), msp .name )
940
+ }
941
+ }
942
+
943
+ return nil
944
+ }
0 commit comments