Chris@1296: # Redmine - project management software Chris@1296: # Copyright (C) 2006-2012 Jean-Philippe Lang Chris@1296: # Chris@1296: # This program is free software; you can redistribute it and/or Chris@1296: # modify it under the terms of the GNU General Public License Chris@1296: # as published by the Free Software Foundation; either version 2 Chris@1296: # of the License, or (at your option) any later version. Chris@1296: # Chris@1296: # This program is distributed in the hope that it will be useful, Chris@1296: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1296: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1296: # GNU General Public License for more details. Chris@1296: # Chris@1296: # You should have received a copy of the GNU General Public License Chris@1296: # along with this program; if not, write to the Free Software Chris@1296: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1296: Chris@1296: module Redmine Chris@1296: module Ciphering Chris@1296: def self.included(base) Chris@1296: base.extend ClassMethods Chris@1296: end Chris@1296: Chris@1296: class << self Chris@1296: def encrypt_text(text) Chris@1296: if cipher_key.blank? || text.blank? Chris@1296: text Chris@1296: else Chris@1296: c = OpenSSL::Cipher::Cipher.new("aes-256-cbc") Chris@1296: iv = c.random_iv Chris@1296: c.encrypt Chris@1296: c.key = cipher_key Chris@1296: c.iv = iv Chris@1296: e = c.update(text.to_s) Chris@1296: e << c.final Chris@1296: "aes-256-cbc:" + [e, iv].map {|v| Base64.encode64(v).strip}.join('--') Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: def decrypt_text(text) Chris@1296: if text && match = text.match(/\Aaes-256-cbc:(.+)\Z/) Chris@1296: if cipher_key.blank? Chris@1296: logger.error "Attempt to decrypt a ciphered text with no cipher key configured in config/configuration.yml" if logger Chris@1296: return text Chris@1296: end Chris@1296: text = match[1] Chris@1296: c = OpenSSL::Cipher::Cipher.new("aes-256-cbc") Chris@1296: e, iv = text.split("--").map {|s| Base64.decode64(s)} Chris@1296: c.decrypt Chris@1296: c.key = cipher_key Chris@1296: c.iv = iv Chris@1296: d = c.update(e) Chris@1296: d << c.final Chris@1296: else Chris@1296: text Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: def cipher_key Chris@1296: key = Redmine::Configuration['database_cipher_key'].to_s Chris@1296: key.blank? ? nil : Digest::SHA256.hexdigest(key) Chris@1296: end Chris@1296: Chris@1296: def logger Chris@1296: Rails.logger Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: module ClassMethods Chris@1296: def encrypt_all(attribute) Chris@1296: transaction do Chris@1296: all.each do |object| Chris@1296: clear = object.send(attribute) Chris@1296: object.send "#{attribute}=", clear Chris@1296: raise(ActiveRecord::Rollback) unless object.save(:validation => false) Chris@1296: end Chris@1296: end ? true : false Chris@1296: end Chris@1296: Chris@1296: def decrypt_all(attribute) Chris@1296: transaction do Chris@1296: all.each do |object| Chris@1296: clear = object.send(attribute) Chris@1296: object.send :write_attribute, attribute, clear Chris@1296: raise(ActiveRecord::Rollback) unless object.save(:validation => false) Chris@1296: end Chris@1296: end Chris@1296: end ? true : false Chris@1296: end Chris@1296: Chris@1296: private Chris@1296: Chris@1296: # Returns the value of the given ciphered attribute Chris@1296: def read_ciphered_attribute(attribute) Chris@1296: Redmine::Ciphering.decrypt_text(read_attribute(attribute)) Chris@1296: end Chris@1296: Chris@1296: # Sets the value of the given ciphered attribute Chris@1296: def write_ciphered_attribute(attribute, value) Chris@1296: write_attribute(attribute, Redmine::Ciphering.encrypt_text(value)) Chris@1296: end Chris@1296: end Chris@1296: end