Skip to content

Commit bcd9c64

Browse files
committed
[FAB-3039] Checking Identity's OUs
This change-set does the following: 1. It allows the default MSP implementation to carry information about the OUs to be supported. 2. When an Identity is validated, it is checked that the identity's OUs are compatible with those set at the MSP, meaning that their intersection is not empty. If the MSP does not define any required OU then the check is not performed. Change-Id: If5a59c60f25ee5f40bea4d831ea2d051c24d9f05 Signed-off-by: Angelo De Caro <[email protected]>
1 parent 441b308 commit bcd9c64

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

msp/msp_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"fmt"
2525

2626
"github.com/golang/protobuf/proto"
27+
"github.com/hyperledger/fabric/bccsp"
2728
"github.com/hyperledger/fabric/protos/msp"
2829
"github.com/stretchr/testify/assert"
2930
)
@@ -259,6 +260,24 @@ func TestGetOU(t *testing.T) {
259260
assert.Equal(t, "COP", id.GetOrganizationalUnits()[0].OrganizationalUnitIdentifier)
260261
}
261262

263+
func TestCertificationIdentifierComputation(t *testing.T) {
264+
id, err := localMsp.GetDefaultSigningIdentity()
265+
assert.NoError(t, err)
266+
267+
chain, err := localMsp.(*bccspmsp).getCertificationChain(id.GetPublicVersion())
268+
assert.NoError(t, err)
269+
270+
// Hash the chain
271+
hf, err := localMsp.(*bccspmsp).bccsp.GetHash(&bccsp.SHA256Opts{})
272+
assert.NoError(t, err)
273+
for i := 0; i < len(chain); i++ {
274+
hf.Write(chain[i].Raw)
275+
}
276+
sum := hf.Sum(nil)
277+
278+
assert.Equal(t, sum, id.GetOrganizationalUnits()[0].CertifiersIdentifier)
279+
}
280+
262281
func TestOUPolicyPrincipal(t *testing.T) {
263282
id, err := localMsp.GetDefaultSigningIdentity()
264283
assert.NoError(t, err)
@@ -368,6 +387,39 @@ func TestIdentityPolicyPrincipal(t *testing.T) {
368387
assert.NoError(t, err)
369388
}
370389

390+
func TestMSPOus(t *testing.T) {
391+
// Set the OUIdentifiers
392+
backup := localMsp.(*bccspmsp).ouIdentifiers
393+
defer func() { localMsp.(*bccspmsp).ouIdentifiers = backup }()
394+
395+
id, err := localMsp.GetDefaultSigningIdentity()
396+
assert.NoError(t, err)
397+
398+
localMsp.(*bccspmsp).ouIdentifiers = []*msp.FabricOUIdentifier{
399+
&msp.FabricOUIdentifier{
400+
OrganizationalUnitIdentifier: "COP",
401+
CertifiersIdentifier: id.GetOrganizationalUnits()[0].CertifiersIdentifier,
402+
},
403+
}
404+
assert.NoError(t, localMsp.Validate(id.GetPublicVersion()))
405+
406+
localMsp.(*bccspmsp).ouIdentifiers = []*msp.FabricOUIdentifier{
407+
&msp.FabricOUIdentifier{
408+
OrganizationalUnitIdentifier: "COP2",
409+
CertifiersIdentifier: id.GetOrganizationalUnits()[0].CertifiersIdentifier,
410+
},
411+
}
412+
assert.Error(t, localMsp.Validate(id.GetPublicVersion()))
413+
414+
localMsp.(*bccspmsp).ouIdentifiers = []*msp.FabricOUIdentifier{
415+
&msp.FabricOUIdentifier{
416+
OrganizationalUnitIdentifier: "COP",
417+
CertifiersIdentifier: []byte{0, 1, 2, 3, 4},
418+
},
419+
}
420+
assert.Error(t, localMsp.Validate(id.GetPublicVersion()))
421+
}
422+
371423
const othercert = `-----BEGIN CERTIFICATE-----
372424
MIIDAzCCAqigAwIBAgIBAjAKBggqhkjOPQQDAjBsMQswCQYDVQQGEwJHQjEQMA4G
373425
A1UECAwHRW5nbGFuZDEOMAwGA1UECgwFQmFyMTkxDjAMBgNVBAsMBUJhcjE5MQ4w

msp/mspimpl.go

+33
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ type bccspmsp struct {
6161

6262
// list of certificate revocation lists
6363
CRL []*pkix.CertificateList
64+
65+
// list of OUs
66+
ouIdentifiers []*m.FabricOUIdentifier
6467
}
6568

6669
// NewBccspMsp returns an MSP instance backed up by a BCCSP
@@ -341,6 +344,15 @@ func (msp *bccspmsp) Setup(conf1 *m.MSPConfig) error {
341344
msp.CRL[i] = crl
342345
}
343346

347+
// setup the OUs
348+
msp.ouIdentifiers = make([]*m.FabricOUIdentifier, len(conf.OrganizationalUnitIdentifiers))
349+
for i, ou := range conf.OrganizationalUnitIdentifiers {
350+
msp.ouIdentifiers[i] = &m.FabricOUIdentifier{
351+
CertifiersIdentifier: ou.CertifiersIdentifier,
352+
OrganizationalUnitIdentifier: ou.OrganizationalUnitIdentifier,
353+
}
354+
}
355+
344356
return nil
345357
}
346358

@@ -448,6 +460,27 @@ func (msp *bccspmsp) Validate(id Identity) error {
448460
}
449461
}
450462

463+
// Check that the identity's OUs are compatible with those recognized by this MSP,
464+
// meaning that the intersection is not empty.
465+
if len(msp.ouIdentifiers) > 0 {
466+
found := false
467+
for _, ou := range msp.ouIdentifiers {
468+
for _, OU := range id.GetOrganizationalUnits() {
469+
if bytes.Equal(ou.CertifiersIdentifier, OU.CertifiersIdentifier) &&
470+
ou.OrganizationalUnitIdentifier == OU.OrganizationalUnitIdentifier {
471+
found = true
472+
break
473+
}
474+
}
475+
if found {
476+
break
477+
}
478+
}
479+
if !found {
480+
return fmt.Errorf("None of the identity's organizational units [%v] are in MSP %s", id.GetOrganizationalUnits(), msp.name)
481+
}
482+
}
483+
451484
return nil
452485
default:
453486
return fmt.Errorf("Identity type not recognized")

0 commit comments

Comments
 (0)