Skip to content

Commit ae18085

Browse files
adecaroyacovm
authored andcommitted
[FAB-2411] PKI-ID computation restructuring
This change-set does the following: 1. It addresses FAB-2411 (https://jira.hyperledger.org/browse/FAB-2411) by computing the PKI-ID of a peer identity as the hash of the concatenation of the corresponding serialized identity's fields. Tests have been updated to reflect the changes. Change-Id: If78ffca85bdf7811744c3b11043ef007db542186 Signed-off-by: Angelo De Caro <adc@zurich.ibm.com>
1 parent af23d64 commit ae18085

File tree

5 files changed

+84
-15
lines changed

5 files changed

+84
-15
lines changed

msp/mgmt/deserializer.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@ limitations under the License.
1717
package mgmt
1818

1919
import (
20+
"fmt"
21+
22+
"github.com/golang/protobuf/proto"
2023
"github.com/hyperledger/fabric/msp"
24+
mspproto "github.com/hyperledger/fabric/protos/msp"
2125
)
2226

2327
// DeserializersManager is a support interface to
2428
// access the local and channel deserializers
2529
type DeserializersManager interface {
30+
Deserialize(raw []byte) (*mspproto.SerializedIdentity, error)
2631

2732
// GetLocalMSPIdentifier returns the local MSP identifier
2833
GetLocalMSPIdentifier() string
@@ -41,6 +46,15 @@ func NewDeserializersManager() DeserializersManager {
4146

4247
type mspDeserializersManager struct{}
4348

49+
func (m *mspDeserializersManager) Deserialize(raw []byte) (*mspproto.SerializedIdentity, error) {
50+
sId := &mspproto.SerializedIdentity{}
51+
err := proto.Unmarshal(raw, sId)
52+
if err != nil {
53+
return nil, fmt.Errorf("Could not deserialize a SerializedIdentity, err %s", err)
54+
}
55+
return sId, nil
56+
}
57+
4458
func (m *mspDeserializersManager) GetLocalMSPIdentifier() string {
4559
id, _ := GetLocalMSP().GetIdentifier()
4660
return id

msp/mspimpl.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,17 @@ func (msp *bccspmsp) getIdentityFromConf(idBytes []byte) (Identity, bccsp.Key, e
101101
return nil, nil, fmt.Errorf("getIdentityFromBytes error: failed to import certitifacate's public key [%s]", err)
102102
}
103103

104-
return newIdentity(&IdentityIdentifier{
104+
// Use the hash of the identity's certificate as id in the IdentityIdentifier
105+
digest, err := factory.GetDefault().Hash(cert.Raw, &bccsp.SHA256Opts{})
106+
if err != nil {
107+
return nil, nil, fmt.Errorf("getIdentityFromConf failed hashing raw certificate to compute the id of the IdentityIdentifier [%s]", err)
108+
}
109+
110+
id := &IdentityIdentifier{
105111
Mspid: msp.name,
106-
Id: "IDENTITY"}, /* FIXME: not clear where we would get the identifier for this identity */
107-
cert, certPubK, msp), certPubK, nil
112+
Id: hex.EncodeToString(digest)}
113+
114+
return newIdentity(id, cert, certPubK, msp), certPubK, nil
108115
}
109116

110117
func (msp *bccspmsp) getSigningIdentityFromConf(sidInfo *m.SigningIdentityInfo) (SigningIdentity, error) {
@@ -141,10 +148,17 @@ func (msp *bccspmsp) getSigningIdentityFromConf(sidInfo *m.SigningIdentityInfo)
141148
return nil, fmt.Errorf("getIdentityFromBytes error: Failed initializing CryptoSigner, err %s", err)
142149
}
143150

144-
return newSigningIdentity(&IdentityIdentifier{
151+
// Use the hash of the identity's certificate as id in the IdentityIdentifier
152+
digest, err := factory.GetDefault().Hash(idPub.(*identity).cert.Raw, &bccsp.SHA256Opts{})
153+
if err != nil {
154+
return nil, fmt.Errorf("Failed hashing raw certificate to compute the id of the IdentityIdentifier [%s]", err)
155+
}
156+
157+
id := &IdentityIdentifier{
145158
Mspid: msp.name,
146-
Id: "DEFAULT"}, /* FIXME: not clear where we would get the identifier for this identity */
147-
idPub.(*identity).cert, idPub.(*identity).pk, peerSigner, msp), nil
159+
Id: hex.EncodeToString(digest)}
160+
161+
return newSigningIdentity(id, idPub.(*identity).cert, idPub.(*identity).pk, peerSigner, msp), nil
148162
}
149163

150164
/*
@@ -512,12 +526,19 @@ func (msp *bccspmsp) deserializeIdentityInternal(serializedIdentity []byte) (Ide
512526
// We can't do it yet because there is no standardized way
513527
// (yet) to encode the MSP ID into the x.509 body of a cert
514528

515-
id := &IdentityIdentifier{Mspid: msp.name,
516-
Id: "DEFAULT"} // TODO: where should this identifier be obtained from?
529+
// Use the hash of the identity's certificate as id in the IdentityIdentifier
530+
digest, err := factory.GetDefault().Hash(cert.Raw, &bccsp.SHA256Opts{})
531+
if err != nil {
532+
return nil, fmt.Errorf("Failed hashing raw certificate to compute the id of the IdentityIdentifier [%s]", err)
533+
}
534+
535+
id := &IdentityIdentifier{
536+
Mspid: msp.name,
537+
Id: hex.EncodeToString(digest)}
517538

518539
pub, err := msp.bccsp.KeyImport(cert, &bccsp.X509PublicKeyImportOpts{Temporary: true})
519540
if err != nil {
520-
return nil, fmt.Errorf("Failed to import certitifacateś public key [%s]", err)
541+
return nil, fmt.Errorf("Failed to import certitifacate's public key [%s]", err)
521542
}
522543

523544
return newIdentity(id, cert, pub, msp), nil

peer/gossip/mcs/mcs.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,22 @@ func (s *mspMessageCryptoService) GetPKIidOfCert(peerIdentity api.PeerIdentityTy
8888
return nil
8989
}
9090

91+
sid, err := s.deserializer.Deserialize(peerIdentity)
92+
if err != nil {
93+
logger.Errorf("Failed getting validated identity from peer identity [% x]: [%s]", peerIdentity, err)
94+
95+
return nil
96+
}
97+
98+
// concatenate msp-id and idbytes
99+
// idbytes is the low-level representation of an identity.
100+
// it is supposed to be already in its minimal representation
101+
102+
mspIdRaw := []byte(sid.Mspid)
103+
raw := append(mspIdRaw, sid.IdBytes...)
104+
91105
// Hash
92-
digest, err := factory.GetDefault().Hash(peerIdentity, &bccsp.SHA256Opts{})
106+
digest, err := factory.GetDefault().Hash(raw, &bccsp.SHA256Opts{})
93107
if err != nil {
94108
logger.Errorf("Failed computing digest of serialized identity [% x]: [%s]", peerIdentity, err)
95109

peer/gossip/mcs/mcs_test.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"fmt"
2121
"testing"
2222

23+
"reflect"
24+
2325
"github.com/golang/protobuf/proto"
2426
"github.com/hyperledger/fabric/bccsp"
2527
"github.com/hyperledger/fabric/bccsp/factory"
@@ -38,17 +40,31 @@ import (
3840
)
3941

4042
func TestPKIidOfCert(t *testing.T) {
41-
msgCryptoService := New(&MockChannelPolicyManagerGetter{}, localmsp.NewSigner(), mgmt.NewDeserializersManager())
43+
deserializersManager := &mockDeserializersManager{
44+
localDeserializer: &mockIdentityDeserializer{[]byte("Alice"), []byte("msg1")},
45+
}
46+
msgCryptoService := New(&mockChannelPolicyManagerGetter2{},
47+
&mockscrypto.LocalSigner{Identity: []byte("Alice")},
48+
deserializersManager,
49+
)
4250

4351
peerIdentity := []byte("Alice")
4452
pkid := msgCryptoService.GetPKIidOfCert(peerIdentity)
4553

4654
// Check pkid is not nil
4755
assert.NotNil(t, pkid, "PKID must be different from nil")
48-
// Check that pkid is the SHA2-256 of ithe peerIdentity
49-
digest, err := factory.GetDefault().Hash(peerIdentity, &bccsp.SHA256Opts{})
56+
// Check that pkid is correctly computed
57+
id, err := deserializersManager.Deserialize(peerIdentity)
58+
assert.NoError(t, err, "Failed getting validated identity from [% x]", []byte(peerIdentity))
59+
idRaw := append([]byte(id.Mspid), id.IdBytes...)
60+
assert.NoError(t, err, "Failed marshalling identity identifier [% x]: [%s]", peerIdentity, err)
61+
digest, err := factory.GetDefault().Hash(idRaw, &bccsp.SHA256Opts{})
5062
assert.NoError(t, err, "Failed computing digest of serialized identity [% x]", []byte(peerIdentity))
5163
assert.Equal(t, digest, []byte(pkid), "PKID must be the SHA2-256 of peerIdentity")
64+
65+
// The PKI-ID is calculated by concatenating the MspId with IdBytes. Ensure that additional fields haven't been introduced in the code
66+
v := reflect.Indirect(reflect.ValueOf(id))
67+
assert.Equal(t, 2, v.NumField())
5268
}
5369

5470
func TestPKIidOfNil(t *testing.T) {
@@ -87,7 +103,7 @@ func TestVerify(t *testing.T) {
87103
channelDeserializers: map[string]msp.IdentityDeserializer{
88104
"A": &mockIdentityDeserializer{[]byte("Bob"), []byte("msg2")},
89105
"B": &mockIdentityDeserializer{[]byte("Charlie"), []byte("msg3")},
90-
"C": &mockIdentityDeserializer{[]byte("Yacov"), []byte("msg4")},
106+
"C": &mockIdentityDeserializer{[]byte("Dave"), []byte("msg4")},
91107
},
92108
},
93109
)
@@ -107,7 +123,7 @@ func TestVerify(t *testing.T) {
107123

108124
sigma, err = msgCryptoService.Sign(msg)
109125
assert.NoError(t, err)
110-
err = msgCryptoService.Verify(api.PeerIdentityType("Yacov"), sigma, msg)
126+
err = msgCryptoService.Verify(api.PeerIdentityType("Dave"), sigma, msg)
111127
assert.Error(t, err)
112128
assert.Contains(t, fmt.Sprintf("%v", err), "Could not acquire policy manager")
113129
}

peer/gossip/mcs/mocks.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ type mockDeserializersManager struct {
8484
channelDeserializers map[string]msp.IdentityDeserializer
8585
}
8686

87+
func (m *mockDeserializersManager) Deserialize(raw []byte) (*mspproto.SerializedIdentity, error) {
88+
return &mspproto.SerializedIdentity{Mspid: "mock", IdBytes: raw}, nil
89+
}
90+
8791
func (m *mockDeserializersManager) GetLocalMSPIdentifier() string {
8892
return "mock"
8993
}

0 commit comments

Comments
 (0)