smime_controller.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Integration::SMIMEController < ApplicationController
  3. prepend_before_action :authenticate_and_authorize!
  4. def certificate_download
  5. cert = SMIMECertificate.find(params[:id])
  6. send_data(
  7. cert.pem,
  8. filename: "#{cert.subject_hash}.crt",
  9. type: 'text/plain',
  10. disposition: 'attachment'
  11. )
  12. end
  13. def private_key_download
  14. cert = SMIMECertificate.find(params[:id])
  15. send_data(
  16. cert.private_key,
  17. filename: "#{cert.subject_hash}.key",
  18. type: 'text/plain',
  19. disposition: 'attachment'
  20. )
  21. end
  22. def certificate_list
  23. list = SMIMECertificate.all.map { |cert| cert_obj_to_json(cert) }
  24. render json: list
  25. end
  26. def certificate_delete
  27. SMIMECertificate.find(params[:id]).destroy!
  28. render json: {
  29. result: 'ok',
  30. }
  31. end
  32. def certificate_add
  33. string = params[:data]
  34. if string.blank? && params[:file].present?
  35. string = params[:file].read.force_encoding('utf-8')
  36. end
  37. cert = Certificate::X509::SMIME.parse(string)
  38. cert.valid_smime_certificate!
  39. items = SMIMECertificate.create_certificates(string)
  40. render json: {
  41. result: 'ok',
  42. response: items.map { |c| cert_obj_to_json(c) },
  43. }
  44. rescue => e
  45. unprocessable_entity(e)
  46. end
  47. def private_key_delete
  48. SMIMECertificate.find(params[:id]).update!(
  49. private_key: nil,
  50. private_key_secret: nil,
  51. )
  52. render json: {
  53. result: 'ok',
  54. }
  55. end
  56. def private_key_add
  57. string = params[:data]
  58. if string.blank? && params[:file].present?
  59. string = params[:file].read.force_encoding('utf-8')
  60. end
  61. raise __("Parameter 'data' or 'file' required.") if string.blank?
  62. private_key = SecureMailing::SMIME::PrivateKey.read(string, params[:secret])
  63. private_key.valid_smime_private_key!
  64. SMIMECertificate.create_certificates(string)
  65. SMIMECertificate.create_private_keys(string, params[:secret])
  66. render json: {
  67. result: 'ok',
  68. }
  69. rescue => e
  70. unprocessable_entity(e)
  71. end
  72. def search
  73. security_options = SecureMailing::SMIME::SecurityOptions.new(ticket: params[:ticket], article: params[:article]).process
  74. result = {
  75. type: 'S/MIME',
  76. encryption: map_result(security_options.encryption),
  77. sign: map_result(security_options.signing),
  78. }
  79. render json: result
  80. end
  81. private
  82. def map_result(method_result)
  83. {
  84. success: method_result.possible?,
  85. comment: method_result.message,
  86. commentPlaceholders: method_result.message_placeholders,
  87. }
  88. end
  89. def cert_obj_to_json(cert)
  90. info = cert.parsed
  91. {
  92. id: cert.id,
  93. subject: info.subject.to_s,
  94. doc_hash: cert.subject_hash,
  95. fingerprint: cert.fingerprint,
  96. modulus: cert.uid,
  97. not_before_at: info.not_before,
  98. not_after_at: info.not_after,
  99. raw: cert.pem,
  100. private_key: cert.private_key,
  101. private_key_secret: cert.private_key_secret,
  102. created_at: cert.created_at,
  103. updated_at: cert.updated_at,
  104. subject_alternative_name: cert.email_addresses.join(', ')
  105. }
  106. end
  107. end