petRUShka, Fabien Penso
This plugin allows you to send X509 signed andor crypted mails with Rails 3 ActionMailer. If you want to sign mails with Rails 2, you should use the original plugin: github.com/penso/actionmailer_x509
It has been tested with Rails 3.0.3.
Creation of the certificates
(1) Generate your own Certificate Authority (CA).
openssl genrsa -des3 -out ca.key 4096 openssl req -new -x509 -days 365 -key ca.key -out ca.crt
(2) Generate a server key and request for signing (csr).
Note : use your email address for the Common Name (CN) field openssl genrsa -des3 -out server.key 4096 openssl req -new -key server.key -out server.csr
(3) Sign the certificate signing request (csr) with the self-created
certificate authority (CA) that you made earlier. openssl x509 -req -days 365 -in server.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out server.crt
(3 bis) or self sign your certificate with the same key.
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
(4) Compute all thoses files into a PCKS#12 file (so you can include it in your mailer):
openssl pkcs12 -export -in server.crt -inkey server.key -certfile ca.key -name "My Cert" -out name-cert.p12
Extracting the files from a PKCS#12 file
If you have a PKCS#12 file, usualy named .p12, you can extract the required files with the following commands.
(1) Extract the private key
openssl pkcs12 -in file_input.p12 -clcerts -nocerts -out file_out.key -nodes
(2) Extract the certificate
openssl pkcs12 -in file_input.p12 -clcerts -nokeys -out file_out.crt -nodes
Please note the -nodes to leave the private key uncrypted, use -des if you wish to protect it.
INSTALLING THE PLUGIN
Put this line in your Gemfile
USING THE PLUGIN
If you wish to send a signed email you just need to put new informations in your ActionMailer model.
class FooMailer < ActionMailer::Base def sending_method(email, from , subject = "Empty subject for signed") # If you want to sign the mail x509_sign true x509_sign_cert "certs/yourwebsite.crt" x509_sign_key "certs/yourwebsite.key" # In case your certificate has a passphrase x509_sign_passphrase "my passphrase for the certificate" # If you want to crypt the mail x509_crypt true x509_crypt_cert "certs/crypt.crt" # In case you want to set non DES cipher x509_crypt_cipher "AES-128-CBC" mail(:subject => subject, :to => email, :from => from) end
You can also specify the certificate and key in your environment file:
ActionMailer::Base.default_x509_sign = true ActionMailer::Base.default_x509_cert = "certs/server.crt" ActionMailer::Base.default_x509_key = "certs/server.key" ActionMailer::Base.default_x509_crypt = true ActionMailer::Base.default_x509_crypt_cert = "certs/crypt.crt"
You can benchmark the plugin with:
Send yourself a signed mail with:
Verify the plugin generates valid signature
Generate a signed mail in a local file
* Ruby 1.8 or later * Rails 3.0 or later * OpenSSL 0.9.8e or later and Ruby/OpenSSL 22.214.171.124 or later
Mail User Agent tested
We checked with the following MUA for making sure the signed mails are readable.
* Mutt 1.5: OK * Outlook 2007: OK * iPhone: The message appears without the signature but no other problem * Thunderbird 5.0: OK * Evolution 2.12: OK * Apple Mail.app: OK * Google Mail: A file smime.p3s appears as attachment * Yandex Mail: A file smime.p3s appears as attachment * Opera Mail: A file smime.p3s appears as attachment
Porting to Rails 3\Mail from Rails 2\Tmail, adding crypting and wrap code into the gem was done by petRUShka <firstname.lastname@example.org>.
Special thanks to Geal for noticing on content-id error.
This code is under the BSD license.
Copyright © 2011, petRUShka, 2008, Fabien Penso
All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the University of California, Berkeley nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS “AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.