summaryrefslogtreecommitdiff
path: root/lib/gitlab/crypto_helper.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2021-02-05 16:20:45 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2021-02-05 16:20:45 +0000
commitd298fad0c0564454271cba11e6f20c19681534ac (patch)
tree0a19d07d8b3bdd2574617305c300e404f2ace581 /lib/gitlab/crypto_helper.rb
parentc9f9eec79cab801a50db698f682aacffbedf07f7 (diff)
downloadgitlab-ce-1ca13fc6800d22bb81a53919fc2d7b41d89c2a6d.tar.gz
Add latest changes from gitlab-org/gitlab@13-9-stable-eev13.9.0-rc41
Diffstat (limited to 'lib/gitlab/crypto_helper.rb')
-rw-r--r--lib/gitlab/crypto_helper.rb31
1 files changed, 25 insertions, 6 deletions
diff --git a/lib/gitlab/crypto_helper.rb b/lib/gitlab/crypto_helper.rb
index 87a03d9c58f..4428354642d 100644
--- a/lib/gitlab/crypto_helper.rb
+++ b/lib/gitlab/crypto_helper.rb
@@ -6,25 +6,44 @@ module Gitlab
AES256_GCM_OPTIONS = {
algorithm: 'aes-256-gcm',
- key: Settings.attr_encrypted_db_key_base_32,
- iv: Settings.attr_encrypted_db_key_base_12
+ key: Settings.attr_encrypted_db_key_base_32
}.freeze
+ AES256_GCM_IV_STATIC = Settings.attr_encrypted_db_key_base_12
+
def sha256(value)
salt = Settings.attr_encrypted_db_key_base_truncated
::Digest::SHA256.base64digest("#{value}#{salt}")
end
- def aes256_gcm_encrypt(value)
- encrypted_token = Encryptor.encrypt(AES256_GCM_OPTIONS.merge(value: value))
- Base64.strict_encode64(encrypted_token)
+ def aes256_gcm_encrypt(value, nonce: nil)
+ aes256_gcm_encrypt_using_static_nonce(value)
end
def aes256_gcm_decrypt(value)
return unless value
+ nonce = Feature.enabled?(:dynamic_nonce_creation) ? dynamic_nonce(value) : AES256_GCM_IV_STATIC
encrypted_token = Base64.decode64(value)
- Encryptor.decrypt(AES256_GCM_OPTIONS.merge(value: encrypted_token))
+ decrypted_token = Encryptor.decrypt(AES256_GCM_OPTIONS.merge(value: encrypted_token, iv: nonce))
+ decrypted_token
+ end
+
+ def dynamic_nonce(value)
+ TokenWithIv.find_nonce_by_hashed_token(value) || AES256_GCM_IV_STATIC
+ end
+
+ def aes256_gcm_encrypt_using_static_nonce(value)
+ create_encrypted_token(value, AES256_GCM_IV_STATIC)
+ end
+
+ def read_only?
+ Gitlab::Database.read_only?
+ end
+
+ def create_encrypted_token(value, iv)
+ encrypted_token = Encryptor.encrypt(AES256_GCM_OPTIONS.merge(value: value, iv: iv))
+ Base64.strict_encode64(encrypted_token)
end
end
end