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