2021-12-31 08:22:10 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
2022-01-17 06:02:13 +00:00
|
|
|
"encoding/json"
|
2021-12-31 08:22:10 +00:00
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2022-01-17 06:02:13 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/envstore"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
2021-12-31 08:22:10 +00:00
|
|
|
)
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// EncryptB64 encrypts data into base64 string
|
2021-12-31 08:22:10 +00:00
|
|
|
func EncryptB64(text string) string {
|
|
|
|
return base64.StdEncoding.EncodeToString([]byte(text))
|
|
|
|
}
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// DecryptB64 decrypts from base64 string to readable string
|
2021-12-31 08:22:10 +00:00
|
|
|
func DecryptB64(s string) (string, error) {
|
|
|
|
data, err := base64.StdEncoding.DecodeString(s)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(data), nil
|
|
|
|
}
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// EncryptAES encrypts data using AES algorithm
|
2021-12-31 08:22:10 +00:00
|
|
|
func EncryptAES(text []byte) ([]byte, error) {
|
2022-01-20 11:22:37 +00:00
|
|
|
key := []byte(envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyEncryptionKey))
|
2021-12-31 08:22:10 +00:00
|
|
|
c, err := aes.NewCipher(key)
|
|
|
|
var res []byte
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// gcm or Galois/Counter Mode, is a mode of operation
|
|
|
|
// for symmetric key cryptographic block ciphers
|
|
|
|
// - https://en.wikipedia.org/wiki/Galois/Counter_Mode
|
|
|
|
gcm, err := cipher.NewGCM(c)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// creates a new byte array the size of the nonce
|
|
|
|
// which must be passed to Seal
|
|
|
|
nonce := make([]byte, gcm.NonceSize())
|
|
|
|
// populates our nonce with a cryptographically secure
|
|
|
|
// random sequence
|
|
|
|
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// here we encrypt our text using the Seal function
|
|
|
|
// Seal encrypts and authenticates plaintext, authenticates the
|
|
|
|
// additional data and appends the result to dst, returning the updated
|
|
|
|
// slice. The nonce must be NonceSize() bytes long and unique for all
|
|
|
|
// time, for a given key.
|
|
|
|
return gcm.Seal(nonce, nonce, text, nil), nil
|
|
|
|
}
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// DecryptAES decrypts data using AES algorithm
|
2021-12-31 08:22:10 +00:00
|
|
|
func DecryptAES(ciphertext []byte) ([]byte, error) {
|
2022-01-20 11:22:37 +00:00
|
|
|
key := []byte(envstore.EnvInMemoryStoreObj.GetStringStoreEnvVariable(constants.EnvKeyEncryptionKey))
|
2021-12-31 08:22:10 +00:00
|
|
|
c, err := aes.NewCipher(key)
|
|
|
|
var res []byte
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
gcm, err := cipher.NewGCM(c)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nonceSize := gcm.NonceSize()
|
|
|
|
if len(ciphertext) < nonceSize {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
|
|
|
|
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return plaintext, nil
|
|
|
|
}
|
2022-01-17 06:02:13 +00:00
|
|
|
|
|
|
|
// EncryptEnvData is used to encrypt the env data
|
2022-01-29 11:32:44 +00:00
|
|
|
func EncryptEnvData(data envstore.Store) (string, error) {
|
2022-01-17 06:02:13 +00:00
|
|
|
jsonBytes, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
2022-01-29 11:32:44 +00:00
|
|
|
return "", err
|
2022-01-17 06:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
envStoreObj := envstore.EnvInMemoryStoreObj.GetEnvStoreClone()
|
|
|
|
|
|
|
|
err = json.Unmarshal(jsonBytes, &envStoreObj)
|
|
|
|
if err != nil {
|
2022-01-29 11:32:44 +00:00
|
|
|
return "", err
|
2022-01-17 06:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
configData, err := json.Marshal(envStoreObj)
|
|
|
|
if err != nil {
|
2022-01-29 11:32:44 +00:00
|
|
|
return "", err
|
2022-01-17 06:02:13 +00:00
|
|
|
}
|
|
|
|
encryptedConfig, err := EncryptAES(configData)
|
|
|
|
if err != nil {
|
2022-01-29 11:32:44 +00:00
|
|
|
return "", err
|
2022-01-17 06:02:13 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 11:32:44 +00:00
|
|
|
return EncryptB64(string(encryptedConfig)), nil
|
2022-01-17 06:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EncryptPassword is used for encrypting password
|
|
|
|
func EncryptPassword(password string) (string, error) {
|
|
|
|
pw, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(pw), nil
|
|
|
|
}
|