Skip to content

Commit

Permalink
Merge pull request #1274 from akshata-s-banoshi/sprint-75-odim-7450-u…
Browse files Browse the repository at this point in the history
…buntu-22.04

modify code for backward compatibility for ubuntu 20.04
  • Loading branch information
jeevan-kamkar authored Jul 18, 2023
2 parents 11d5770 + f22d56f commit 0805030
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 31 deletions.
15 changes: 11 additions & 4 deletions lib-messagebus/datacommunicator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,23 @@ func decryptRSAOAEPEncryptedPasswords(encryptedPassword string) ([]byte, error)
}

func bytesToPrivateKey(privateKey []byte) (*rsa.PrivateKey, error) {
var key *rsa.PrivateKey
block, _ := pem.Decode(privateKey)
if block == nil {
return nil, fmt.Errorf("failed to parse PEM block containing the public key for the RSAPrivateKeyPath:%s",
MQ.RedisStreams.RSAPrivateKeyPath)
}

key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
pkcs1Key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse DER encoded public key for the RSAPrivateKeyPath:%s with %v",
MQ.RedisStreams.RSAPrivateKeyPath, err)
pkcs8Key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse DER encoded public key for the RSAPrivateKeyPath:%s with %v",
MQ.RedisStreams.RSAPrivateKeyPath, err)
}
key = pkcs8Key.(*rsa.PrivateKey)
} else {
key = pkcs1Key
}
return key.(*rsa.PrivateKey), nil
return key, nil
}
13 changes: 10 additions & 3 deletions lib-utilities/common/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,22 @@ func DecryptWithPrivateKey(ciphertext []byte) ([]byte, error) {
return nil, fmt.Errorf("error while trying to decrypt pem block: %v", err)
}
}
key, err := x509.ParsePKCS8PrivateKey(b)
var key *rsa.PrivateKey
pkcs1Key, err := x509.ParsePKCS1PrivateKey(b)
if err != nil {
return nil, fmt.Errorf("error while parsing private key: %v", err)
pkcs8Key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("error while parsing private key: %v", err)
}
key = pkcs8Key.(*rsa.PrivateKey)
} else {
key = pkcs1Key
}
hash := sha512.New()
plainText, err := rsa.DecryptOAEP(
hash,
rand.Reader,
key.(*rsa.PrivateKey),
key,
ciphertext,
nil,
)
Expand Down
16 changes: 12 additions & 4 deletions lib-utilities/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,18 @@ func decryptRSAOAEPEncryptedPasswords(passwordFilePath string) ([]byte, error) {
Data.KeyCertConf.RSAPrivateKeyPath)
}

privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
var privateKey *rsa.PrivateKey

pkcs1Key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse DER encoded public key for the RSAPrivateKeyPath:%s with %v",
Data.KeyCertConf.RSAPrivateKeyPath, err)
pkcs8Key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse DER encoded public key for the RSAPrivateKeyPath:%s with %v",
Data.KeyCertConf.RSAPrivateKeyPath, err)
}
privateKey = pkcs8Key.(*rsa.PrivateKey)
} else {
privateKey = pkcs1Key
}

cipherText, err := ioutil.ReadFile(passwordFilePath)
Expand All @@ -398,7 +406,7 @@ func decryptRSAOAEPEncryptedPasswords(passwordFilePath string) ([]byte, error) {
}

rng := rand.Reader
password, err := rsa.DecryptOAEP(sha512.New(), rng, privateKey.(*rsa.PrivateKey), ct, nil)
password, err := rsa.DecryptOAEP(sha512.New(), rng, privateKey, ct, nil)
if err != nil {
return nil, fmt.Errorf("password decryption failed for the passwordFilePath:%s with %v", passwordFilePath, err)
}
Expand Down
15 changes: 11 additions & 4 deletions plugin-dell/dputilities/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,32 @@ func GetPlainText(ctx context.Context, password []byte) ([]byte, error) {
enc := x509.IsEncryptedPEMBlock(block)
b := block.Bytes
var err error
var key *rsa.PrivateKey
if enc {
b, err = x509.DecryptPEMBlock(block, nil)
if err != nil {
l.LogWithFields(ctx).Error(err.Error())
return []byte{}, err
}
}
key, err := x509.ParsePKCS8PrivateKey(b)
pkcs1Key, err := x509.ParsePKCS1PrivateKey(b)
if err != nil {
l.LogWithFields(ctx).Error(err.Error())
return []byte{}, err
pkcs8Key, err := x509.ParsePKCS8PrivateKey(b)
if err != nil {
l.LogWithFields(ctx).Error(err.Error())
return []byte{}, err
}
key = pkcs8Key.(*rsa.PrivateKey)
} else {
key = pkcs1Key
}

hash := sha512.New()

return rsa.DecryptOAEP(
hash,
rand.Reader,
key.(*rsa.PrivateKey),
key,
password,
nil,
)
Expand Down
2 changes: 1 addition & 1 deletion plugin-dell/dputilities/utility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestGetPlainText(t *testing.T) {
// Marshal the rsa private key to PKCS8 format
marshalledPrivateKey, err := x509.MarshalPKCS8PrivateKey(key)
if err != nil {
t.Fatalf("error marshalling private key : %v", err)
marshalledPrivateKey = x509.MarshalPKCS1PrivateKey(key)
}

// Convert the private key to PEM format
Expand Down
16 changes: 12 additions & 4 deletions plugin-lenovo/lputilities/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func GetPlainText(password []byte) ([]byte, error) {
enc := x509.IsEncryptedPEMBlock(block)
b := block.Bytes
var err error
var key *rsa.PrivateKey
if enc {
log.Info("is encrypted pem block")
b, err = x509.DecryptPEMBlock(block, nil)
Expand All @@ -60,18 +61,25 @@ func GetPlainText(password []byte) ([]byte, error) {
return []byte{}, err
}
}
key, err := x509.ParsePKCS8PrivateKey(b)

pkcs1Key, err := x509.ParsePKCS1PrivateKey(b)
if err != nil {
log.Info(err.Error())
return []byte{}, err
pkcs8Key, err := x509.ParsePKCS8PrivateKey(b)
if err != nil {
log.Info(err.Error())
return []byte{}, err
}
key = pkcs8Key.(*rsa.PrivateKey)
} else {
key = pkcs1Key
}

hash := sha512.New()

return rsa.DecryptOAEP(
hash,
rand.Reader,
key.(*rsa.PrivateKey),
key,
password,
nil,
)
Expand Down
15 changes: 11 additions & 4 deletions plugin-redfish/rfputilities/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func GetPlainText(password []byte) ([]byte, error) {
enc := x509.IsEncryptedPEMBlock(block)
b := block.Bytes
var err error
var key *rsa.PrivateKey
if enc {
log.Info("is encrypted pem block")
b, err = x509.DecryptPEMBlock(block, nil)
Expand All @@ -60,18 +61,24 @@ func GetPlainText(password []byte) ([]byte, error) {
return []byte{}, err
}
}
key, err := x509.ParsePKCS8PrivateKey(b)
pkcs1Key, err := x509.ParsePKCS1PrivateKey(b)
if err != nil {
log.Info(err.Error())
return []byte{}, err
pkcs8Key, err := x509.ParsePKCS8PrivateKey(b)
if err != nil {
log.Info(err.Error())
return []byte{}, err
}
key = pkcs8Key.(*rsa.PrivateKey)
} else {
key = pkcs1Key
}

hash := sha512.New()

return rsa.DecryptOAEP(
hash,
rand.Reader,
key.(*rsa.PrivateKey),
key,
password,
nil,
)
Expand Down
15 changes: 11 additions & 4 deletions plugin-unmanaged-racks/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,17 @@ func decryptRSAOAEPEncryptedPasswords(passwordFilePath, RSAPrivateKeyPath string
RSAPrivateKeyPath)
}

privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
var privateKey *rsa.PrivateKey
pkcs1Key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse DER encoded public key for the RSAPrivateKeyPath:%s with %v",
RSAPrivateKeyPath, err)
pkcs8Key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse DER encoded public key for the RSAPrivateKeyPath:%s with %v",
RSAPrivateKeyPath, err)
}
privateKey = pkcs8Key.(*rsa.PrivateKey)
} else {
privateKey = pkcs1Key
}

cipherText, err := ioutil.ReadFile(passwordFilePath)
Expand All @@ -181,7 +188,7 @@ func decryptRSAOAEPEncryptedPasswords(passwordFilePath, RSAPrivateKeyPath string
}

rng := rand.Reader
password, err := rsa.DecryptOAEP(sha512.New(), rng, privateKey.(*rsa.PrivateKey), ct, nil)
password, err := rsa.DecryptOAEP(sha512.New(), rng, privateKey, ct, nil)
if err != nil {
return nil, fmt.Errorf("password decryption failed for the passwordFilePath:%s with %v", passwordFilePath, err)
}
Expand Down
14 changes: 11 additions & 3 deletions plugin-unmanaged-racks/utils/enigma.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,24 @@ func bytesToPrivateKey(privateKey []byte) *rsa.PrivateKey {
enc := x509.IsEncryptedPEMBlock(block)
b := block.Bytes
var err error
var key *rsa.PrivateKey
if enc {
logging.Info("is encrypted pem block")
b, err = x509.DecryptPEMBlock(block, nil)
if err != nil {
logging.Error(err)
}
}
key, err := x509.ParsePKCS8PrivateKey(b)

pkcs1Key, err := x509.ParsePKCS1PrivateKey(b)
if err != nil {
logging.Fatal(err)
pkcs8Key, err := x509.ParsePKCS8PrivateKey(b)
if err != nil {
logging.Fatal(err)
}
key = pkcs8Key.(*rsa.PrivateKey)
} else {
key = pkcs1Key
}
return key.(*rsa.PrivateKey)
return key
}

0 comments on commit 0805030

Please sign in to comment.