Skip to content

Commit

Permalink
Support PKCS1 and PKCS8 private keys (#843)
Browse files Browse the repository at this point in the history
* Support PKCS1 and PKCS8 private keys for server ca cert
  • Loading branch information
lkysow authored Nov 5, 2021
1 parent f5042fd commit a3d023d
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 54 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## UNRELEASED

IMPROVEMENTS:
* Control Plane
* TLS: Support PKCS1 and PKCS8 private keys for Consul certificate authority. [[GH-843](https://github.com/hashicorp/consul-k8s/pull/843)]

BUG FIXES:
* Control Plane
* ACLs: Fix issue where if one or more servers fail to have their ACL tokens set on the initial run of server-acl-init
Expand Down
19 changes: 19 additions & 0 deletions control-plane/helper/cert/tls_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
"time"
)

// NOTE: A lot of this code is taken from
// https://github.com/hashicorp/consul/blob/44c023a3020fdd139c5be330f318a3c12339f08e/agent/connect/parsing.go.

// GenerateCA generates a CA with the provided
// common name valid for 10 years. It returns the private key as
// a crypto.Signer and a PEM string and certificate
Expand Down Expand Up @@ -162,6 +165,22 @@ func ParseSigner(pemValue string) (crypto.Signer, error) {
switch block.Type {
case "EC PRIVATE KEY":
return x509.ParseECPrivateKey(block.Bytes)

case "RSA PRIVATE KEY":
return x509.ParsePKCS1PrivateKey(block.Bytes)

case "PRIVATE KEY":
signer, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
pk, ok := signer.(crypto.Signer)
if !ok {
return nil, fmt.Errorf("private key is not a valid format")
}

return pk, nil

default:
return nil, fmt.Errorf("unknown PEM block type for signing key: %s", block.Type)
}
Expand Down
Loading

0 comments on commit a3d023d

Please sign in to comment.