Skip to content

Commit

Permalink
pki: handle both RSA PRIVATE KEY and PRIVATE KEY (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
neoaggelos authored Apr 8, 2024
1 parent ad5ad44 commit 3866fe4
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/k8s/pkg/k8sd/pki/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,24 @@ func loadCertificate(certPEM string, keyPEM string) (*x509.Certificate, *rsa.Pri
var key *rsa.PrivateKey
if keyPEM != "" {
pb, _ := pem.Decode([]byte(keyPEM))
key, err = x509.ParsePKCS1PrivateKey(pb.Bytes)
if err != nil {
return nil, nil, fmt.Errorf("failed to parse private key: %w", err)
switch pb.Type {
case "RSA PRIVATE KEY":
key, err = x509.ParsePKCS1PrivateKey(pb.Bytes)
if err != nil {
return nil, nil, fmt.Errorf("failed to parse RSA private key: %w", err)
}
case "PRIVATE KEY":
parsed, err := x509.ParsePKCS8PrivateKey(pb.Bytes)
if err != nil {
return nil, nil, fmt.Errorf("failed to parse private key: %w", err)
}
v, ok := parsed.(*rsa.PrivateKey)
if !ok {
return nil, nil, fmt.Errorf("not an RSA private key")
}
key = v
default:
return nil, nil, fmt.Errorf("unknown private key block type %q", pb.Type)
}
}
return cert, key, nil
Expand Down

0 comments on commit 3866fe4

Please sign in to comment.