Class: ActionMailer::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/actionmailer_x509.rb

Overview

:nodoc:

Direct Known Subclasses

Notifier

Constant Summary collapse

@@default_x509_sign =
false
@@default_x509_crypt =

not used, for later.

false
@@default_x509_cert =
nil
@@default_x509_key =
nil
@@default_x509_sign_method =
:smime
@@default_x509_crypt_method =

not used, for later.

:smime
@@default_x509_passphrase =
nil

Instance Method Summary collapse

Instance Method Details

#initialize_with_sign(method_name, *parameters) ⇒ Object

We replace the create! methods and run a new method if signing is required



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/actionmailer_x509.rb', line 67

def initialize_with_sign(method_name, *parameters)
  mail = initialize_without_sign(method_name, *parameters)

  x509_initvar()

  # If we need to sign the outgoing mail.
  if should_sign?
    if logger
      logger.debug("actionmailer_x509: We should sign the mail with #{@x509_sign_method} method.")
    end
    __send__("x509_sign_#{@x509_sign_method}", mail)
  end

end

#x509_crypt_smime(mail) ⇒ Object

X509 SMIME crypting



152
153
154
# File 'lib/actionmailer_x509.rb', line 152

def x509_crypt_smime(mail)
  logger.debug("X509 SMIME crypting")
end

#x509_sign_smime(mail) ⇒ Object

X509 SMIME signing



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/actionmailer_x509.rb', line 84

def x509_sign_smime(mail)
  if logger
    logger.debug("actionmailer_x509: X509 SMIME signing with cert #{@x509_cert} and key #{@x509_key}")
  end

  # We should set content_id, otherwise Mail will set content_id after signing and will broke sign
  mail.content_id ||= nil

  # We can remove the headers from the older mail we encapsulate.
  # Leaving allows to have the headers signed too within the encapsulated
  # email, but MUAs make no use of them... :(
  #
  # mail.subject = nil
  # mail.to = nil
  # mail.cc = nil
  # mail.from = nil
  # mail.date = nil
  # headers.each { |k, v| mail[k] = nil }
  # mail['Content-Type'] = 'text/plain'
  # mail.mime_version = nil

  # We load certificate and private key
  cert = OpenSSL::X509::Certificate.new( File::read(@x509_cert) )
  prv_key = OpenSSL::PKey::RSA.new( File::read(@x509_key), @x509_passphrase)

  begin
    # Sign the mail
    # NOTE: the one following line is the slowest part of this code, signing is sloooow
    p7sign = OpenSSL::PKCS7.sign(cert,prv_key,mail.encoded, [], OpenSSL::PKCS7::DETACHED)
    smime0 = OpenSSL::PKCS7::write_smime(p7sign)

    # Adding the signature part to the older mail
    newm = Mail.new(smime0)

    # We need to overwrite the content-type of the mail so MUA notices this is a signed mail
#        newm.content_type = 'multipart/signed; protocol="application/x-pkcs7-signature"; micalg=sha1; '
     newm.delivery_method(mail.delivery_method.class, mail.delivery_method.settings)
     newm.subject = mail.subject
     newm.to = mail.to
     newm.cc = mail.cc
     newm.from = mail.from
     newm.mime_version = mail.mime_version
     newm.date = mail.date
#        newm.body = "This is an S/MIME signed message\n"
#        headers.each { |k, v| m[k] = v } # that does nothing in general

    # NOTE: We can not use this as we need a B64 encoded signature, and no
    # methods provides it within the Ruby OpenSSL library... :(
    #
    # We add the signature
    # signature = Mail.new
    # signature.mime_version = '1.0'
    # signature['Content-Type'] = 'application/x-pkcs7-mime; smime-type=signed-data; name="smime.p7m"'
    # signature['Content-Transfer-Encoding'] = 'base64'
    # signature['Content-Disposition']  = 'attachment; filename="smime.p7m"'
    # signature.body = p7sign.to_s
    # newm.parts << signature

    @_message = newm
  rescue Exception => detail
    logger.error("Error while SMIME signing the mail : #{detail}")
  end

  ## logger.debug("x509_sign_smime, resulted email\n-------------( test X509 )----------\n#{m.encoded}\n-------------( test X509 )----------")

end