You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ciphering.rb 3.1KB

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