annotate .svn/pristine/6c/6c63de300d0e80bc0b6e3de12c96ec81bd4c86b4.svn-base @ 1295:622f24f53b42 redmine-2.3

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