<img src=“https://travis-ci.org/jeyboy/action_mailer_x509.png?branch=master” alt=“Build Status” />
ActionmailerX509¶ ↑
Allows you to send X509 signed and/or crypted mails for Ruby on Rails. Also my you possibility verify and decrypt X509 signed and/or crypted mails.
It has been tested with Rails 3.2.12 and Rails 4.
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
Additional settings
(5) Create a CRL.
To create a CRL for your CA, you first need to create an index file. This will initially just be a blank file (created with touch). However, as you start to revoke certificates, things will go into here. It's human readable and unsigned, which is why we need to use OpenSSL to make the signed PEM form of it.
So, once you’ve got your empty index file (in wherever the openssl.cnf file you’ve got say’s it’ll be.… why can’t that be a command line option like everything else?), then you can make a CRL from it using:
openssl ca -gencrl -keyfile ca.key -cert ca.crt -crldays xxx -out my_crl.pem
(6) Revocation.
Now, with your initial CRL set up, we revoke a certificate:
openssl ca -revoke bad_crt_file -keyfile ca_key -cert ca_crt
This will automagically update your index.txt file with the new details of your revoked certificate. Now you need to generate a new CRL file, with the same command we used above to generate the blank one. With your new CRL created, you need to publish it!
(7) Check CRL file
To peek at a CRL file, showing things like the validity and what certificates (if any) have been revoked, use:
openssl crl -in crl_file -noout -text
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.
Common way to connect your certificates¶ ↑
A lot of mailer apps has the same procedure of certificate inclusion which include next steps :
(1) Open the Preferences. (2) Go to Advanced and open the Certificates tab. Click on View Certificates. (3) Select the Your Certificates tab and click on Import. Browse the .p12 file you created and hit Open. Every time you import a certificate, you will need to give the export passphrase you specified when you created the .p12 document. (4) Next, go to the Servers tab and click Import again. Here, browse the .crt file belonging to your certificate (there are two if you created a certificate authority). (5) Still on the same tab, select the newly imported entry and click Edit. (6) Select Trust the authenticity of this certificate and click Edit CA Trust.
You only need to do the next step if you created a CA.
(1) Go to the Authorities tab and hit Import. (2) Now select the .crt file belonging to your certificate authority and hit Open. You will then be asked how far you want to trust the certificate authority. After pressing OK the created CA will be listed on the tab.
For Mac OS procedure has some differences:
To use your certificate on Mac, import the certificate into Keychain Access on that Mac. Double-click the certificate file. If Keychain Access can’t import the certificate, drag the file onto the Keychain Access icon in Finder. If you still can’t import the certificate, the certificate may have expired or may be invalid. You can verify whether your certificate is successfully installed if it appears in the My Certificates category in Keychain Access. Open your certificate in Keychain Access and make sure its trust setting is “Use System Defaults” or “Always Trust.”
INSTALLING¶ ↑
Put this line in your Gemfile
gem 'actionmailer_x509'
SETTINGS¶ ↑
First for all - run install generator, which adds configuration file
rails g action_mailer_x509:install
After the process, add base configurations for emails processing in ‘/config/initializers/x509_settings’
#standart config config.add_configuration :demo, { sign_enable: false, crypt_enable: true, sign_cert: 'cert.crt', sign_key: 'cert.key', sign_passphrase: 'demo', crypt_cert: 'cert.crt', crypt_key: 'cert.key', crypt_passphrase: 'demo', crypt_cipher: 'des', certs_path: Rails.root.join('certs') } #p12 certificate using config.add_configuration :p12, { sign_enable: false, crypt_enable: true, sign_cert_p12: 'cert.p12', sign_passphrase: 'demo', crypt_cert_p12: 'cert.p12', crypt_passphrase: 'demo', crypt_cipher: 'aes128', certs_path: Rails.root.join('certs') } #short variant - in this case used one cert/key for sign and crypt config.add_configuration :short_demo, { sign_enable: false, crypt_enable: true, cert: 'cert.crt', key: 'cert.key', passphrase: 'demo', crypt_cipher: 'aes256', certs_path: Rails.root.join('certs') } #short p12 variant - in this case used one cert for sign and crypt config.add_configuration :short_p12, { sign_enable: false, crypt_enable: true, cert_p12: 'cert.p12', passphrase: 'demo', certs_path: Rails.root.join('certs') } config.add_configuration :none, { sign_enable: false, crypt_enable: false, }
and set default configuration
config.default_configuration = :hisp
You may also add the configurations dynamically in any place.
And you may also change the default configuration in any place of the program:
1) Change globally
ActionMailerX509.default_configuration = :demo
2) Change only for decoding and verifyng if you use Mail object
Mail.new(encoded_text).proceed(result_type, configuration)
If you set configuration as nil in “proceed” func, “ActionMailerX509.default_configuration” will be used.
USING¶ ↑
Encoding/Sign
You may set simpy set configuration for all funcs of mailer class SendCrypted < ActionMailer::Base x509_configuration :test end or set it how param of 'mail' func for each mailer method class SendCrypted < ActionMailer::Base def prepare_mail(from, to, subject, body, x509_configuration = ActionMailerX509.default_configuration) mail( from: from, to: to, subject: subject, x509_configuration: x509_configuration )do |format| #format.text { render text: body } format.html { render text: body } end end end
Decoding/Verify
Mail.new(encoded_mail).proceed(result_type, configuration) 'result_type' param represent type of presentation ('plain', 'html' and etc.)
HELPERS¶ ↑
You may use some helpers for fast encoding in case when you proceed received email. Helper has the following signature:
method_name_as_new_encoding
For example:
mail = Mail.new(raw_email) mail.body => return body in default encoding mail.body_as_koi8_r => return body in koi8-r encoding
Also you may generate new p12 certificates
bytes = SecurityObject.p12_certificate({ subject: { :country => 'TestCountry', :state => 'TestState', :location => 'TestLocation', :organization => 'TestOrganization', :organizational_unit => 'TestOrganizationalUnit', :common_name => 'TestCommonName', :email => 'a@b.com' }, password: 'your passphrase', time_from: Time.now, time_length: 2.years }) File.open('1.p12', 'wb') do |file| file.write(bytes) end
USING TEST¶ ↑
You mau use rspec tests by command
bundle exec rake
or use next rake tests:
Start all main tests
rake action_mailer_x509:all
You can benchmark the gem with:
rake action_mailer_x509:performance_test
Send yourself a signed email with:
rake action_mailer_x509:send_test
Verify the gem which generates valid signature
rake action_mailer_x509:verify_signature
Generate a signed email in a local file
rake action_mailer_x509:generate_mail
Generates a crypted mail in a file
rake action_mailer_x509:generate_crypted_mail
Verify the gem which generates valid encrypted mail
rake action_mailer_x509:verify_crypt
Check if crypt text is valid by openssl
rake action_mailer_x509:verify_crypt_by_openssl
Check sign and crypt
rake action_mailer_x509:verify_sign_and_crypt
REQUIREMENTS¶ ↑
* Ruby 1.9 or later (Ruby 1.8 is not tested) * Rails 3.2 or later (All other versions are not tested) * OpenSSL 0.9.8e or later and Ruby/OpenSSL 1.8.6.36 or later
Mail User Agent tested¶ ↑
We checked with the following MUA for making sure the signed mails are readable.
* Thunderbird 15.0: OK * Apple Mail.app: OK * Google Mail: A file smime.p7s appears as attachment * Google Mail + Penango: OK
AUTHORS¶ ↑
Gem was done by Jenua Boiko <jeyboy1985@gmail.com>. Inspired by gem of petRUShka
Special thanks to Ekaterina Krivich.
This code is under the BSD license.
LICENSE¶ ↑
Copyright © 2013, Jenua Boiko
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. * 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.