fix: private key parsing with PKCS1 (#1591)

This commit is contained in:
Henit Chobisa
2024-10-23 19:55:01 +05:30
committed by GitHub
parent 438b2f088e
commit fbaf5d34ec
2 changed files with 11 additions and 10 deletions

View File

@@ -34,11 +34,6 @@ var StartCmd = &cobra.Command{
}
db.Initialize()
_, err = feat_flag.ParsePrivateKey(PRIVATE_KEY)
if err != nil {
return err
}
_, err = feat_flag.ParsePrivateKey(PRIVATE_KEY)
if err != nil {

View File

@@ -6,6 +6,7 @@ import (
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
@@ -68,6 +69,7 @@ func decodeBase64Key(base64EncodedKey string) ([]byte, error) {
/* ---------------------- Helper Functions ---------------------------- */
// Parses the private key given to the rsa.PrivateKey type
func ParsePrivateKey(base64EncodedKey string) (*rsa.PrivateKey, error) {
decodedKey, err := decodeBase64Key(base64EncodedKey)
if err != nil {
@@ -78,15 +80,19 @@ func ParsePrivateKey(base64EncodedKey string) (*rsa.PrivateKey, error) {
if block == nil {
return nil, fmt.Errorf("failed to parse PEM block containing the key")
}
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
privateKey, err := pkcs8.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse RSA private key: %v", err)
privateKey, err := pkcs8.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse RSA private key: %v", err)
}
key := privateKey.(*rsa.PrivateKey)
return key, nil
}
key := privateKey.(*rsa.PrivateKey)
return key, nil
return privateKey, nil
}
func decryptWithPrivateKey(data EncryptedData, privateKey *rsa.PrivateKey) ([]byte, error) {