Skip to content

Commit ecc3eea

Browse files
committed
Switch to PEM encoding for serialized identities
This change-set switches to PEM encoding for serialized identities in protocol messages to facilitate the integration with nodejs clients. Change-Id: I2d91f6977e0965cbf88d549c29d4fb68e70ae1be Signed-off-by: Alessandro Sorniotti <[email protected]>
1 parent 56fb71d commit ecc3eea

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

msp/bccspmsp.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,11 @@ func (msp *bccspmsp) DeserializeIdentity(serializedID []byte) (Identity, error)
248248
mspLogger.Infof("Obtaining identity")
249249

250250
// This MSP will always deserialize certs this way
251-
cert, err := x509.ParseCertificate(serializedID)
251+
bl, _ := pem.Decode(serializedID)
252+
if bl == nil {
253+
return nil, fmt.Errorf("Could not decode the PEM structure")
254+
}
255+
cert, err := x509.ParseCertificate(bl.Bytes)
252256
if err != nil {
253257
return nil, fmt.Errorf("ParseCertificate failed %s", err)
254258
}

msp/identities.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"crypto/x509"
2222
"fmt"
2323

24+
"encoding/pem"
25+
2426
"github.com/hyperledger/fabric/core/crypto/bccsp"
2527
"github.com/hyperledger/fabric/core/crypto/bccsp/factory"
2628
"github.com/hyperledger/fabric/core/crypto/bccsp/signer"
@@ -79,18 +81,23 @@ func (id *identity) VerifyAttributes(proof [][]byte, spec *AttributeProofSpec) (
7981

8082
func (id *identity) Serialize() ([]byte, error) {
8183
/*
82-
mspLogger.Infof("Serializing identity %s", id.id)
84+
mspLogger.Infof("Serializing identity %s", id.id)
8385
84-
// We serialize identities by prepending the MSPID and appending the ASN.1 DER content of the cert
85-
sId := SerializedIdentity{Mspid: id.id.Mspid, IdBytes: id.cert.Raw}
86-
idBytes, err := asn1.Marshal(sId)
87-
if err != nil {
88-
return nil, fmt.Errorf("Could not marshal a SerializedIdentity structure for identity %s, err %s", id.id, err)
89-
}
86+
// We serialize identities by prepending the MSPID and appending the ASN.1 DER content of the cert
87+
sId := SerializedIdentity{Mspid: id.id.Mspid, IdBytes: id.cert.Raw}
88+
idBytes, err := asn1.Marshal(sId)
89+
if err != nil {
90+
return nil, fmt.Errorf("Could not marshal a SerializedIdentity structure for identity %s, err %s", id.id, err)
91+
}
9092
91-
return idBytes, nil
93+
return idBytes, nil
9294
*/
93-
return id.cert.Raw, nil
95+
pb := &pem.Block{Bytes: id.cert.Raw}
96+
pemBytes := pem.EncodeToMemory(pb)
97+
if pemBytes == nil {
98+
return nil, fmt.Errorf("Encoding of identitiy failed")
99+
}
100+
return pemBytes, nil
94101
}
95102

96103
type signingidentity struct {

0 commit comments

Comments
 (0)