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