annotate .svn/pristine/f5/f57cb4b2fd4e8b282707982829017761e21ff080.svn-base @ 1464:261b3d9a4903 redmine-2.4

Update to Redmine 2.4 branch rev 12663
author Chris Cannam
date Tue, 14 Jan 2014 14:37:42 +0000
parents
children
rev   line source
Chris@1464 1 # Redmine - project management software
Chris@1464 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@1464 3 #
Chris@1464 4 # This program is free software; you can redistribute it and/or
Chris@1464 5 # modify it under the terms of the GNU General Public License
Chris@1464 6 # as published by the Free Software Foundation; either version 2
Chris@1464 7 # of the License, or (at your option) any later version.
Chris@1464 8 #
Chris@1464 9 # This program is distributed in the hope that it will be useful,
Chris@1464 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1464 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1464 12 # GNU General Public License for more details.
Chris@1464 13 #
Chris@1464 14 # You should have received a copy of the GNU General Public License
Chris@1464 15 # along with this program; if not, write to the Free Software
Chris@1464 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1464 17
Chris@1464 18 module Redmine
Chris@1464 19 module Ciphering
Chris@1464 20 def self.included(base)
Chris@1464 21 base.extend ClassMethods
Chris@1464 22 end
Chris@1464 23
Chris@1464 24 class << self
Chris@1464 25 def encrypt_text(text)
Chris@1464 26 if cipher_key.blank? || text.blank?
Chris@1464 27 text
Chris@1464 28 else
Chris@1464 29 c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
Chris@1464 30 iv = c.random_iv
Chris@1464 31 c.encrypt
Chris@1464 32 c.key = cipher_key
Chris@1464 33 c.iv = iv
Chris@1464 34 e = c.update(text.to_s)
Chris@1464 35 e << c.final
Chris@1464 36 "aes-256-cbc:" + [e, iv].map {|v| Base64.encode64(v).strip}.join('--')
Chris@1464 37 end
Chris@1464 38 end
Chris@1464 39
Chris@1464 40 def decrypt_text(text)
Chris@1464 41 if text && match = text.match(/\Aaes-256-cbc:(.+)\Z/)
Chris@1464 42 if cipher_key.blank?
Chris@1464 43 logger.error "Attempt to decrypt a ciphered text with no cipher key configured in config/configuration.yml" if logger
Chris@1464 44 return text
Chris@1464 45 end
Chris@1464 46 text = match[1]
Chris@1464 47 c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
Chris@1464 48 e, iv = text.split("--").map {|s| Base64.decode64(s)}
Chris@1464 49 c.decrypt
Chris@1464 50 c.key = cipher_key
Chris@1464 51 c.iv = iv
Chris@1464 52 d = c.update(e)
Chris@1464 53 d << c.final
Chris@1464 54 else
Chris@1464 55 text
Chris@1464 56 end
Chris@1464 57 end
Chris@1464 58
Chris@1464 59 def cipher_key
Chris@1464 60 key = Redmine::Configuration['database_cipher_key'].to_s
Chris@1464 61 key.blank? ? nil : Digest::SHA256.hexdigest(key)
Chris@1464 62 end
Chris@1464 63
Chris@1464 64 def logger
Chris@1464 65 Rails.logger
Chris@1464 66 end
Chris@1464 67 end
Chris@1464 68
Chris@1464 69 module ClassMethods
Chris@1464 70 def encrypt_all(attribute)
Chris@1464 71 transaction do
Chris@1464 72 all.each do |object|
Chris@1464 73 clear = object.send(attribute)
Chris@1464 74 object.send "#{attribute}=", clear
Chris@1464 75 raise(ActiveRecord::Rollback) unless object.save(:validation => false)
Chris@1464 76 end
Chris@1464 77 end ? true : false
Chris@1464 78 end
Chris@1464 79
Chris@1464 80 def decrypt_all(attribute)
Chris@1464 81 transaction do
Chris@1464 82 all.each do |object|
Chris@1464 83 clear = object.send(attribute)
Chris@1464 84 object.send :write_attribute, attribute, clear
Chris@1464 85 raise(ActiveRecord::Rollback) unless object.save(:validation => false)
Chris@1464 86 end
Chris@1464 87 end
Chris@1464 88 end ? true : false
Chris@1464 89 end
Chris@1464 90
Chris@1464 91 private
Chris@1464 92
Chris@1464 93 # Returns the value of the given ciphered attribute
Chris@1464 94 def read_ciphered_attribute(attribute)
Chris@1464 95 Redmine::Ciphering.decrypt_text(read_attribute(attribute))
Chris@1464 96 end
Chris@1464 97
Chris@1464 98 # Sets the value of the given ciphered attribute
Chris@1464 99 def write_ciphered_attribute(attribute, value)
Chris@1464 100 write_attribute(attribute, Redmine::Ciphering.encrypt_text(value))
Chris@1464 101 end
Chris@1464 102 end
Chris@1464 103 end