security_options.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class SecureMailing::PGP::SecurityOptions < SecureMailing::Backend::HandlerSecurityOptions
  3. def type
  4. 'PGP'
  5. end
  6. private
  7. def sign_security_options_status_default_message
  8. __('There was no PGP key found.')
  9. end
  10. def config
  11. Setting.get('pgp_config')
  12. end
  13. def group_has_valid_secure_objects?(signing_result, group_email)
  14. begin
  15. sign_key = PGPKey.find_by_uid(from(group_email), only_valid: false, secret: true)
  16. return key_valid?(signing_result, sign_key, group_email)
  17. rescue ActiveRecord::RecordNotFound
  18. signing_result.message = __('The PGP key for %s was not found.')
  19. signing_result.message_placeholders = [group_email]
  20. rescue => e
  21. signing_result.message = e.message
  22. end
  23. false
  24. end
  25. def key_valid?(signing_result, sign_key, email)
  26. result = false
  27. if sign_key
  28. result = !sign_key.expired?
  29. signing_result.message = if sign_key.expired?
  30. __('The PGP key for %s was found, but has expired.')
  31. else
  32. __('The PGP key for %s was found.')
  33. end
  34. else
  35. signing_result.message = __('The PGP key for %s was not found.')
  36. end
  37. signing_result.message_placeholders = [email]
  38. result
  39. end
  40. def recipients_have_valid_secure_objects?(encryption_result, recipients)
  41. keys = recipients.map do |recipient|
  42. PGPKey.find_by_uid(recipient, only_valid: false)
  43. rescue ActiveRecord::RecordNotFound
  44. encryption_result.message = __('The PGP key for %s was not found.')
  45. encryption_result.message_placeholders = [recipient]
  46. return false
  47. end
  48. keys_valid?(encryption_result, keys, recipients)
  49. rescue => e
  50. encryption_result.message = e.message
  51. false
  52. end
  53. def keys_valid?(encryption_result, keys, recipients)
  54. result = false
  55. if keys
  56. result = keys.none?(&:expired?)
  57. encryption_result.message = if keys.any?(&:expired?)
  58. __('There were PGP keys found for %s, but at least one of them has expired.')
  59. else
  60. __('The PGP keys for %s were found.')
  61. end
  62. encryption_result.message_placeholders = [recipients.join(', ')]
  63. else
  64. encryption_result.message = __('The PGP keys for %s were not found.')
  65. end
  66. result
  67. end
  68. end