-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAB-6155] Add multiple certs for peer client TLS
The GetPeerCredentials function only adds only a single certificate to the pool, but it should add multiple in order to handle certificates issued by an intermediate CA. Add it in such a way so that we get more specific warning messages if it fails. Change-Id: Ia708775ff852ca3355c4693bc2ee739f5dadaf20 Signed-off-by: Keith Smith <[email protected]>
- Loading branch information
Keith Smith
committed
Sep 15, 2017
1 parent
b02e9f4
commit 239ac67
Showing
3 changed files
with
58 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package comm | ||
|
||
import ( | ||
"crypto/x509" | ||
"encoding/pem" | ||
) | ||
|
||
// AddPemToCertPool adds PEM-encoded certs to a cert pool | ||
func AddPemToCertPool(pemCerts []byte, pool *x509.CertPool) error { | ||
certs, _, err := pemToX509Certs(pemCerts) | ||
if err != nil { | ||
return err | ||
} | ||
for _, cert := range certs { | ||
pool.AddCert(cert) | ||
} | ||
return nil | ||
} | ||
|
||
//utility function to parse PEM-encoded certs | ||
func pemToX509Certs(pemCerts []byte) ([]*x509.Certificate, []string, error) { | ||
|
||
//it's possible that multiple certs are encoded | ||
certs := []*x509.Certificate{} | ||
subjects := []string{} | ||
for len(pemCerts) > 0 { | ||
var block *pem.Block | ||
block, pemCerts = pem.Decode(pemCerts) | ||
if block == nil { | ||
break | ||
} | ||
/** TODO: check why msp does not add type to PEM header | ||
if block.Type != "CERTIFICATE" || len(block.Headers) != 0 { | ||
continue | ||
} | ||
*/ | ||
|
||
cert, err := x509.ParseCertificate(block.Bytes) | ||
if err != nil { | ||
return nil, subjects, err | ||
} else { | ||
certs = append(certs, cert) | ||
//extract and append the subject | ||
subjects = append(subjects, string(cert.RawSubject)) | ||
} | ||
} | ||
return certs, subjects, nil | ||
} |