Skip to content

Commit f2d52f5

Browse files
yacovmmastersingh24
authored andcommitted
[FAB-5713] properly log x509 certs
Whenever new identity instances are created while the msp log module is configured with DEBUG level - the peer outputs gibberish such as: California1^V0^T^F^CU^D^G^S^MSan Francisco1^_0^]^F^CU^D^C^S^Vpe ^]^O^A^A�^D^D^C^B^G�0^L^F^CU^]^S^A^A�^D^B0^@0+^F^CU^]#^D$0"� m5��� ^�4^Pn$^U)c�z^L^M0 This not only makes it useless, but also might make text parsing utilities not work properly when parsing log files. With this, it logs: 2017-08-10 15:32:52.262 UTC [msp/identity] newIdentity -> DEBU 034 Creating identity instance for cert -----BEGIN CERTIFICATE----- MIICGTCCAb+gAwIBAgIQf9Nof+8cN6zuUYM/pHibLjAKBggqhkjOPQQDAjBzMQsw CQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNU2FuIEZy YW5jaXNjbzEZMBcGA1UEChMQb3JnMS5leGFtcGxlLmNvbTEcMBoGA1UEAxMTY2Eu b3JnMS5leGFtcGxlLmNvbTAeFw0xNzA4MTAxNTMyNDlaFw0yNzA4MDgxNTMyNDla MFsxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1T YW4gRnJhbmNpc2NvMR8wHQYDVQQDExZwZWVyMC5vcmcxLmV4YW1wbGUuY29tMFkw EwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAElrov/lsUPTequQmGlpXEWaGns9q+LVtI 4igu+6DZxE1OYPfT9SoOvNyEYl4kj2xTjwuFaONH8K01moeeCsuQwaNNMEswDgYD VR0PAQH/BAQDAgeAMAwGA1UdEwEB/wQCMAAwKwYDVR0jBCQwIoAgaJ7EjSXkGtFT IO81qYkZh2hj0w7MkHTty+UU4KMiUQUwCgYIKoZIzj0EAwIDSAAwRQIhAMoz2r0Y l9kdpALKAOOAgkuUf7h8OPmNERvachWqAR52AiA/NbGl5yeAsQYukxaOHUPz3/xr EZpIfwconq/5ASnnNA== -----END CERTIFICATE----- Change-Id: I3e1e5d2ddfc13ec3d83bf2cfa675071159f65eeb Signed-off-by: yacovm <[email protected]> (cherry picked from commit 82f0bd9) Signed-off-by: Gari Singh <[email protected]>
1 parent 650fb6b commit f2d52f5

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

msp/cert.go

+40-15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
/*
2-
Copyright IBM Corp. 2017 All Rights Reserved.
2+
Copyright IBM Corp. All Rights Reserved.
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
4+
SPDX-License-Identifier: Apache-2.0
155
*/
166

177
package msp
@@ -22,11 +12,12 @@ import (
2212
"crypto/x509"
2313
"crypto/x509/pkix"
2414
"encoding/asn1"
15+
"encoding/pem"
16+
"errors"
17+
"fmt"
2518
"math/big"
2619
"time"
2720

28-
"errors"
29-
3021
"github.com/hyperledger/fabric/bccsp/sw"
3122
)
3223

@@ -101,7 +92,7 @@ func sanitizeECDSASignedCert(cert *x509.Certificate, parentCert *x509.Certificat
10192
// the lower level interface that represent an x509 certificate
10293
// encoding
10394
var newCert certificate
104-
_, err = asn1.Unmarshal(cert.Raw, &newCert)
95+
newCert, err = certFromX509Cert(cert)
10596
if err != nil {
10697
return nil, err
10798
}
@@ -119,3 +110,37 @@ func sanitizeECDSASignedCert(cert *x509.Certificate, parentCert *x509.Certificat
119110
// 4. parse newRaw to get an x509 certificate
120111
return x509.ParseCertificate(newRaw)
121112
}
113+
114+
func certFromX509Cert(cert *x509.Certificate) (certificate, error) {
115+
var newCert certificate
116+
_, err := asn1.Unmarshal(cert.Raw, &newCert)
117+
if err != nil {
118+
return certificate{}, err
119+
}
120+
return newCert, nil
121+
}
122+
123+
// String returns a PEM representation of a certificate
124+
func (c certificate) String() string {
125+
b, err := asn1.Marshal(c)
126+
if err != nil {
127+
return fmt.Sprintf("Failed marshaling cert: %v", err)
128+
}
129+
block := &pem.Block{
130+
Bytes: b,
131+
Type: "CERTIFICATE",
132+
}
133+
b = pem.EncodeToMemory(block)
134+
return string(b)
135+
}
136+
137+
// certToPEM converts the given x509.Certificate to a PEM
138+
// encoded string
139+
func certToPEM(certificate *x509.Certificate) string {
140+
cert, err := certFromX509Cert(certificate)
141+
if err != nil {
142+
mspIdentityLogger.Warning("Failed converting certificate to asn1", err)
143+
return ""
144+
}
145+
return cert.String()
146+
}

msp/identities.go

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
/*
2-
Copyright IBM Corp. 2016 All Rights Reserved.
2+
Copyright IBM Corp. All Rights Reserved.
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
4+
SPDX-License-Identifier: Apache-2.0
155
*/
166

177
package msp
@@ -49,7 +39,7 @@ type identity struct {
4939
}
5040

5141
func newIdentity(id *IdentityIdentifier, cert *x509.Certificate, pk bccsp.Key, msp *bccspmsp) (Identity, error) {
52-
mspIdentityLogger.Debugf("Creating identity instance for ID %s", id)
42+
mspIdentityLogger.Debugf("Creating identity instance for ID %s", certToPEM(cert))
5343

5444
// Sanitize first the certificate
5545
cert, err := msp.sanitizeCert(cert)

0 commit comments

Comments
 (0)