annotate .svn/pristine/82/82d5006881dc9639677a0a6d5907160232a37a0f.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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