Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for PKCS8 key and non encrypted #198

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions depot/file/depot.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,9 @@ func (d *fileDepot) path(name string) string {
}

const (
rsaPrivateKeyPEMBlockType = "RSA PRIVATE KEY"
certificatePEMBlockType = "CERTIFICATE"
rsaPrivateKeyPEMBlockType = "RSA PRIVATE KEY"
pkcs8PrivateKeyPEMBlockType = "PRIVATE KEY"
certificatePEMBlockType = "CERTIFICATE"
)

// load an encrypted private key from disk
Expand All @@ -375,15 +376,33 @@ func loadKey(data []byte, password []byte) (*rsa.PrivateKey, error) {
if pemBlock == nil {
return nil, errors.New("PEM decode failed")
}
if pemBlock.Type != rsaPrivateKeyPEMBlockType {
switch pemBlock.Type {
case rsaPrivateKeyPEMBlockType:
if x509.IsEncryptedPEMBlock(pemBlock) {
b, err := x509.DecryptPEMBlock(pemBlock, password)
if err != nil {
return nil, err
}
return x509.ParsePKCS1PrivateKey(b)
}
return x509.ParsePKCS1PrivateKey(pemBlock.Bytes)
case pkcs8PrivateKeyPEMBlockType:
priv, err := x509.ParsePKCS8PrivateKey(pemBlock.Bytes)
if err != nil {
return nil, err
}
switch priv := priv.(type) {
case *rsa.PrivateKey:
return priv, nil
// case *dsa.PublicKey:
// case *ecdsa.PublicKey:
// case ed25519.PublicKey:
default:
panic("unsupported type of public key. SCEP need RSA private key")
}
default:
return nil, errors.New("unmatched type or headers")
}

b, err := x509.DecryptPEMBlock(pemBlock, password)
if err != nil {
return nil, err
}
return x509.ParsePKCS1PrivateKey(b)
}

// load an encrypted private key from disk
Expand Down