fix(crypto): nil pointer dereference in crypto.BytesToPrivateKey (#491) (#493)

This commit is contained in:
Oleksandr Shepetko 2023-12-05 17:15:59 +02:00 committed by GitHub
parent fe3e02b80a
commit 3a4d44cae7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 4 deletions

View file

@ -4,14 +4,19 @@ import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
)
func BytesToPrivateKey(priv []byte) (*rsa.PrivateKey, error) {
block, _ := pem.Decode(priv)
b := block.Bytes
key, err := x509.ParsePKCS1PrivateKey(b)
func BytesToPrivateKey(b []byte) (*rsa.PrivateKey, error) {
block, _ := pem.Decode(b)
if block == nil {
return nil, errors.New("PEM decode failed")
}
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, err
}
return key, nil
}