From ecc3eea3abcb59e3967bf65c90565c11dfeb016b Mon Sep 17 00:00:00 2001 From: Alessandro Sorniotti Date: Tue, 22 Nov 2016 09:15:41 +0100 Subject: [PATCH] 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 --- msp/bccspmsp.go | 6 +++++- msp/identities.go | 25 ++++++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/msp/bccspmsp.go b/msp/bccspmsp.go index 4df550d4b3d..2a8f1b53895 100644 --- a/msp/bccspmsp.go +++ b/msp/bccspmsp.go @@ -248,7 +248,11 @@ func (msp *bccspmsp) DeserializeIdentity(serializedID []byte) (Identity, error) mspLogger.Infof("Obtaining identity") // This MSP will always deserialize certs this way - cert, err := x509.ParseCertificate(serializedID) + bl, _ := pem.Decode(serializedID) + if bl == nil { + return nil, fmt.Errorf("Could not decode the PEM structure") + } + cert, err := x509.ParseCertificate(bl.Bytes) if err != nil { return nil, fmt.Errorf("ParseCertificate failed %s", err) } diff --git a/msp/identities.go b/msp/identities.go index 33842e9d469..a34b4148b6a 100644 --- a/msp/identities.go +++ b/msp/identities.go @@ -21,6 +21,8 @@ import ( "crypto/x509" "fmt" + "encoding/pem" + "github.com/hyperledger/fabric/core/crypto/bccsp" "github.com/hyperledger/fabric/core/crypto/bccsp/factory" "github.com/hyperledger/fabric/core/crypto/bccsp/signer" @@ -79,18 +81,23 @@ func (id *identity) VerifyAttributes(proof [][]byte, spec *AttributeProofSpec) ( func (id *identity) Serialize() ([]byte, error) { /* - mspLogger.Infof("Serializing identity %s", id.id) + mspLogger.Infof("Serializing identity %s", id.id) - // We serialize identities by prepending the MSPID and appending the ASN.1 DER content of the cert - sId := SerializedIdentity{Mspid: id.id.Mspid, IdBytes: id.cert.Raw} - idBytes, err := asn1.Marshal(sId) - if err != nil { - return nil, fmt.Errorf("Could not marshal a SerializedIdentity structure for identity %s, err %s", id.id, err) - } + // We serialize identities by prepending the MSPID and appending the ASN.1 DER content of the cert + sId := SerializedIdentity{Mspid: id.id.Mspid, IdBytes: id.cert.Raw} + idBytes, err := asn1.Marshal(sId) + if err != nil { + return nil, fmt.Errorf("Could not marshal a SerializedIdentity structure for identity %s, err %s", id.id, err) + } - return idBytes, nil + return idBytes, nil */ - return id.cert.Raw, nil + pb := &pem.Block{Bytes: id.cert.Raw} + pemBytes := pem.EncodeToMemory(pb) + if pemBytes == nil { + return nil, fmt.Errorf("Encoding of identitiy failed") + } + return pemBytes, nil } type signingidentity struct {