Skip to content

Commit 07d17c3

Browse files
committed
Fix error on incorrectly ordered PEM files
1 parent 4c5f14d commit 07d17c3

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

core/connection/tlsconfig.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,13 @@ func (c *TLSConfig) AddClientCertFromFile(clientFile string) (string, error) {
8787
}
8888

8989
if currentBlock.Type == "CERTIFICATE" {
90-
// Maintain a copy of
91-
if len(certBlock) != 0 {
92-
return "", fmt.Errorf("multiple CERTIFICATE sections in .pem file")
93-
}
94-
95-
certBlock = data[start : len(data)-len(remaining)-1]
90+
certBlock = data[start : len(data)-len(remaining)]
9691
certDecodedBlock = currentBlock.Bytes
9792
start += len(certBlock)
9893
} else if strings.HasSuffix(currentBlock.Type, "PRIVATE KEY") {
94+
if len(certBlock) == 0 {
95+
return "", fmt.Errorf("failed to find CERTIFICATE but did find private key; PEM inputs may be switched")
96+
}
9997
if c.clientCertPass != nil && x509.IsEncryptedPEMBlock(currentBlock) {
10098
var encoded bytes.Buffer
10199
buf, err := x509.DecryptPEMBlock(currentBlock, []byte(c.clientCertPass()))
@@ -107,7 +105,7 @@ func (c *TLSConfig) AddClientCertFromFile(clientFile string) (string, error) {
107105
keyBlock = encoded.Bytes()
108106
start = len(data) - len(remaining)
109107
} else {
110-
keyBlock = data[start : len(data)-len(remaining)]
108+
keyBlock = data[start:len(data)-len(remaining)]
111109
start += len(keyBlock)
112110
}
113111
}

0 commit comments

Comments
 (0)