Skip to content

Commit bf30af4

Browse files
committed
FAB-2430 Compare certs' ASN.1 bytes
This change set makes sure that byte-by-byte comparison of certificates is performed on the ASN.1 raw encoding (which is guaranteed to be deterministic) as opposed to its protobuf serialization (which isn't). Change-Id: Id9ab15faccd7aa30e5f15ce1e2cf976e1b85cb01 Signed-off-by: Alessandro Sorniotti <[email protected]>
1 parent d778d0a commit bf30af4

File tree

2 files changed

+59
-20
lines changed

2 files changed

+59
-20
lines changed

msp/msp_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,57 @@ func TestAdminPolicyPrincipalFails(t *testing.T) {
309309
assert.Error(t, err)
310310
}
311311

312+
func TestIdentityPolicyPrincipal(t *testing.T) {
313+
id, err := localMsp.GetDefaultSigningIdentity()
314+
assert.NoError(t, err)
315+
316+
idSerialized, err := id.Serialize()
317+
assert.NoError(t, err)
318+
319+
principal := &msp.MSPPrincipal{
320+
PrincipalClassification: msp.MSPPrincipal_IDENTITY,
321+
Principal: idSerialized}
322+
323+
err = id.SatisfiesPrincipal(principal)
324+
assert.NoError(t, err)
325+
}
326+
327+
const othercert = `-----BEGIN CERTIFICATE-----
328+
MIIDAzCCAqigAwIBAgIBAjAKBggqhkjOPQQDAjBsMQswCQYDVQQGEwJHQjEQMA4G
329+
A1UECAwHRW5nbGFuZDEOMAwGA1UECgwFQmFyMTkxDjAMBgNVBAsMBUJhcjE5MQ4w
330+
DAYDVQQDDAVCYXIxOTEbMBkGCSqGSIb3DQEJARYMQmFyMTktY2xpZW50MB4XDTE3
331+
MDIwOTE2MDcxMFoXDTE4MDIxOTE2MDcxMFowfDELMAkGA1UEBhMCR0IxEDAOBgNV
332+
BAgMB0VuZ2xhbmQxEDAOBgNVBAcMB0lwc3dpY2gxDjAMBgNVBAoMBUJhcjE5MQ4w
333+
DAYDVQQLDAVCYXIxOTEOMAwGA1UEAwwFQmFyMTkxGTAXBgkqhkiG9w0BCQEWCkJh
334+
cjE5LXBlZXIwWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAQlRSnAyD+ND6qmaRV7
335+
AS/BPJKX5dZt3gBe1v/RewOpc1zJeXQNWACAk0ae3mv5u9l0HxI6TXJIAQSwJACu
336+
Rqsyo4IBKTCCASUwCQYDVR0TBAIwADARBglghkgBhvhCAQEEBAMCBkAwMwYJYIZI
337+
AYb4QgENBCYWJE9wZW5TU0wgR2VuZXJhdGVkIFNlcnZlciBDZXJ0aWZpY2F0ZTAd
338+
BgNVHQ4EFgQUwHzbLJQMaWd1cpHdkSaEFxdKB1owgYsGA1UdIwSBgzCBgIAUYxFe
339+
+cXOD5iQ223bZNdOuKCRiTKhZaRjMGExCzAJBgNVBAYTAkdCMRAwDgYDVQQIDAdF
340+
bmdsYW5kMRAwDgYDVQQHDAdJcHN3aWNoMQ4wDAYDVQQKDAVCYXIxOTEOMAwGA1UE
341+
CwwFQmFyMTkxDjAMBgNVBAMMBUJhcjE5ggEBMA4GA1UdDwEB/wQEAwIFoDATBgNV
342+
HSUEDDAKBggrBgEFBQcDATAKBggqhkjOPQQDAgNJADBGAiEAuMq65lOaie4705Ol
343+
Ow52DjbaO2YuIxK2auBCqNIu0gECIQCDoKdUQ/sa+9Ah1mzneE6iz/f/YFVWo4EP
344+
HeamPGiDTQ==
345+
-----END CERTIFICATE-----
346+
`
347+
348+
func TestIdentityPolicyPrincipalFails(t *testing.T) {
349+
id, err := localMsp.GetDefaultSigningIdentity()
350+
assert.NoError(t, err)
351+
352+
sid, err := NewSerializedIdentity("DEFAULT", []byte(othercert))
353+
assert.NoError(t, err)
354+
355+
principal := &msp.MSPPrincipal{
356+
PrincipalClassification: msp.MSPPrincipal_IDENTITY,
357+
Principal: sid}
358+
359+
err = id.SatisfiesPrincipal(principal)
360+
assert.Error(t, err)
361+
}
362+
312363
var conf *msp.MSPConfig
313364
var localMsp MSP
314365
var mspMgr MSPManager

msp/mspimpl.go

+8-20
Original file line numberDiff line numberDiff line change
@@ -551,19 +551,8 @@ func (msp *bccspmsp) SatisfiesPrincipal(id Identity, principal *m.MSPPrincipal)
551551
case m.MSPRole_ADMIN:
552552
// in the case of admin, we check that the
553553
// id is exactly one of our admins
554-
idBytes, err := id.Serialize()
555-
if err != nil {
556-
return fmt.Errorf("Could not serialize this identity instance, err %s", err)
557-
}
558-
559554
for _, admincert := range msp.admins {
560-
adBytes, err := admincert.Serialize()
561-
if err != nil {
562-
return fmt.Errorf("Could not serialize admin cert, err %s", err)
563-
}
564-
565-
rv := bytes.Compare(idBytes, adBytes)
566-
if rv == 0 {
555+
if bytes.Equal(id.(*identity).cert.Raw, admincert.(*identity).cert.Raw) {
567556
return nil
568557
}
569558
}
@@ -572,20 +561,19 @@ func (msp *bccspmsp) SatisfiesPrincipal(id Identity, principal *m.MSPPrincipal)
572561
default:
573562
return fmt.Errorf("Invalid MSP role type %d", int32(mspRole.Role))
574563
}
575-
// in this case we have to serialize this instance
576-
// and compare it byte-by-byte with Principal
577564
case m.MSPPrincipal_IDENTITY:
578-
idBytes, err := id.Serialize()
565+
// in this case we have to deserialize the principal's identity
566+
// and compare it byte-by-byte with our cert
567+
principalId, err := msp.DeserializeIdentity(principal.Principal)
579568
if err != nil {
580-
return fmt.Errorf("Could not serialize this identity instance, err %s", err)
569+
return fmt.Errorf("Invalid identity principal, not a certificate. Error %s", err)
581570
}
582571

583-
rv := bytes.Compare(idBytes, principal.Principal)
584-
if rv == 0 {
572+
if bytes.Equal(id.(*identity).cert.Raw, principalId.(*identity).cert.Raw) {
585573
return nil
586-
} else {
587-
return errors.New("The identities do not match")
588574
}
575+
576+
return errors.New("The identities do not match")
589577
case m.MSPPrincipal_ORGANIZATION_UNIT:
590578
// Principal contains the OrganizationUnit
591579
OU := &m.OrganizationUnit{}

0 commit comments

Comments
 (0)