updated validation

This commit is contained in:
Lakr Aream
2022-09-02 13:45:21 +08:00
committed by Lakr Aream
parent 6795844de4
commit d54f5c18e4
8 changed files with 492 additions and 463 deletions

40
lib/license/boundary.rb Normal file
View File

@@ -0,0 +1,40 @@
module Gitlab
class License
module Boundary
BOUNDARY_START = /(\A|\r?\n)-*BEGIN .+? LICENSE-*\r?\n/.freeze
BOUNDARY_END = /\r?\n-*END .+? LICENSE-*(\r?\n|\z)/.freeze
class << self
def add_boundary(data, product_name)
data = remove_boundary(data)
product_name.upcase!
pad = lambda do |message, width|
total_padding = [width - message.length, 0].max
padding = total_padding / 2.0
[
'-' * padding.ceil,
message,
'-' * padding.floor
].join
end
[
pad.call("BEGIN #{product_name} LICENSE", 60),
data.strip,
pad.call("END #{product_name} LICENSE", 60)
].join("\n")
end
def remove_boundary(data)
after_boundary = data.split(BOUNDARY_START).last
in_boundary = after_boundary.split(BOUNDARY_END).first
in_boundary
end
end
end
end
end

92
lib/license/encryptor.rb Normal file
View File

@@ -0,0 +1,92 @@
module Gitlab
class License
class Encryptor
class Error < StandardError; end
class KeyError < Error; end
class DecryptionError < Error; end
attr_accessor :key
def initialize(key)
raise KeyError, 'No RSA encryption key provided.' if key && !key.is_a?(OpenSSL::PKey::RSA)
@key = key
end
def encrypt(data)
raise KeyError, 'Provided key is not a private key.' unless key.private?
# Encrypt the data using symmetric AES encryption.
cipher = OpenSSL::Cipher::AES128.new(:CBC)
cipher.encrypt
aes_key = cipher.random_key
aes_iv = cipher.random_iv
encrypted_data = cipher.update(data) + cipher.final
# Encrypt the AES key using asymmetric RSA encryption.
encrypted_key = key.private_encrypt(aes_key)
encryption_data = {
'data' => Base64.encode64(encrypted_data),
'key' => Base64.encode64(encrypted_key),
'iv' => Base64.encode64(aes_iv)
}
json_data = JSON.dump(encryption_data)
Base64.encode64(json_data)
end
def decrypt(data)
raise KeyError, 'Provided key is not a public key.' unless key.public?
json_data = Base64.decode64(data.chomp)
begin
encryption_data = JSON.parse(json_data)
rescue JSON::ParserError
raise DecryptionError, 'Encryption data is invalid JSON.'
end
unless %w[data key iv].all? { |key| encryption_data[key] }
raise DecryptionError, 'Required field missing from encryption data.'
end
encrypted_data = Base64.decode64(encryption_data['data'])
encrypted_key = Base64.decode64(encryption_data['key'])
aes_iv = Base64.decode64(encryption_data['iv'])
begin
# Decrypt the AES key using asymmetric RSA encryption.
aes_key = self.key.public_decrypt(encrypted_key)
rescue OpenSSL::PKey::RSAError
raise DecryptionError, 'AES encryption key could not be decrypted.'
end
# Decrypt the data using symmetric AES encryption.
cipher = OpenSSL::Cipher::AES128.new(:CBC)
cipher.decrypt
begin
cipher.key = aes_key
rescue OpenSSL::Cipher::CipherError
raise DecryptionError, 'AES encryption key is invalid.'
end
begin
cipher.iv = aes_iv
rescue OpenSSL::Cipher::CipherError
raise DecryptionError, 'AES IV is invalid.'
end
begin
data = cipher.update(encrypted_data) + cipher.final
rescue OpenSSL::Cipher::CipherError
raise DecryptionError, 'Data could not be decrypted.'
end
data
end
end
end
end

5
lib/license/version.rb Normal file
View File

@@ -0,0 +1,5 @@
module Gitlab
class License
VERSION = '2.2.1'.freeze
end
end