Skip to content

Commit

Permalink
Initial commit from a private mercurial repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Penso Fabien committed May 11, 2008
1 parent f0f4c06 commit 51a4e5d
Show file tree
Hide file tree
Showing 18 changed files with 906 additions and 0 deletions.
25 changes: 25 additions & 0 deletions BSD-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright (c) 2007, 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.
153 changes: 153 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
= ActionmailerX509

Fabien Penso / CONOVAE
http://www.conovae.com

== DESCRIPTION

This plugin allows you to send X509 signed mails.

It has been tested with Rails 2.0.1.

== 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.

== USING THE PLUGIN

If you wish to send a signed email you just need to put new informations in
your ActionMailer model.

class FooModel < ActionMailer::Base

def sending_method(email, from , subject = "Empty subject for signed")
recipients email
from from
subject subject
sent_on Time.now

# If you want to sign the mail
x509_sign true
x509_cert "certs/yourwebsite.crt"
x509_key "certs/yourwebsite.key"
# In case your certificate has a passphrase
passphrase "my passphrase for the certificate"
end
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"

== USING TEST

You can benchmark the plugin with:

rake actionmailer_x509:performance_test

Send yourself a signed mail with:

rake actionmailer_x509:send_test

Verify the plugin generates valid signature

rake actionmailer_x509:verify_signature

Generate a signed mail in a local file

rake actionmailer_x509:generate_mail

== REQUIREMENTS

* Ruby 1.8 or later
* Rails 2.0 or later
* 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.

* Mutt 1.5: OK
* Outlook 2007: OK
* iPhone: The message appears without the signature but no other problem
* Thunderbird 2.0: OK
* Evolution 2.12: OK
* Apple Mail.app: OK
* Google Mail: A file smime.p3s appears as attachment

== AUTHORS

This development is done by Fabien Penso <fabien.penso@conovae.com> from
CONOVAE http://www.conovae.com for Dimelo http://www.dimelo.fr

This code is under the BSD license.

== LICENSE

Copyright (c) 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.
4 changes: 4 additions & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require File.dirname(__FILE__) + '/lib/actionmailer_x509'
require File.dirname(__FILE__) + '/lib/models/notifier.rb'

#ActionMailer::Base.send(:include, Conovae::ActionMailerX509)
Empty file added install.rb
Empty file.
189 changes: 189 additions & 0 deletions lib/actionmailer_x509.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
# Copyright (c) 2007 Fabien Penso <fabien.penso@conovae.com>
#
# actionmailer_x509 is a rails plugin to allow X509 outgoing mail to be X509
# signed.
#
# 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.

require "openssl"

module ActionMailer #:nodoc:
class Base #:nodoc:
@@default_x509_sign = false
@@default_x509_crypt = false # not used, for later.
@@default_x509_cert = nil
@@default_x509_key = nil
@@default_x509_sign_method = :smime
@@default_x509_crypt_method = :smime # not used, for later.
@@default_x509_passphrase = nil

# Should we sign the outgoing mail?
adv_attr_accessor :x509_sign

# Should we crypt the outgoing mail?. NOTE: not used yet.
adv_attr_accessor :x509_crypt

# Which certificate will be used for signing.
adv_attr_accessor :x509_cert

# Which private key will be used for signing.
adv_attr_accessor :x509_key

# Which signing method is used. NOTE: For later, if needed.
adv_attr_accessor :x509_sign_method

# Which crypting method is used. NOTE: not used yet.
adv_attr_accessor :x509_crypt_method

# Passphrase for the key, if needed.
adv_attr_accessor :x509_passphrase


# We replace the create! methods and run a new method if signing is required
def create_with_sign!(method_name, *parameters)
mail = create_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
alias_method_chain :create!, :sign

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

# We create a new mail holding the older mail + signature
m = TMail::Mail.new
m.subject = mail.subject
m.to = mail.to
m.cc = mail.cc
m.from = mail.from
m.mime_version = mail.mime_version
m.date = mail.date
m.body = "This is an S/MIME signed message\n"
headers.each { |k, v| m[k] = v } # that does nothing in general

# 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
# We add the encapsulated mail as attachement
m.parts << mail

# 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
# NOTE: we can not reparse the whole mail, TMail adds a \r\n which breaks the signature...
newm = TMail::Mail.parse(smime0)
for part in newm.parts do
if part.content_type == "application/x-pkcs7-signature"
m.parts << part
break
end
end

# We need to overwrite the content-type of the mail so MUA notices this is a signed mail
m.content_type = 'multipart/signed; protocol="application/x-pkcs7-signature"; micalg=sha1; '

# 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 = TMail::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
# m.parts << signature

@mail = m
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

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

protected

# Shall we sign the mail?
def should_sign?
if @x509_sign == true
if not @x509_cert.nil? and not @x509_key.nil?
return true
else
logger.info "X509 signing required, but no certificate and key files configured"
end
end
return false
end

# Initiate from the default class attributes
def x509_initvar
@x509_sign ||= @@default_x509_sign
@x509_crypt ||= @@default_x509_crypt
@x509_cert ||= @@default_x509_cert
@x509_key ||= @@default_x509_key
@x509_sign_method ||= @@default_x509_sign_method
@x509_crypt_method ||= @@default_x509_crypt_method
@x509_passphrase ||= @@default_x509_passphrase
end
end
end
Loading

0 comments on commit 51a4e5d

Please sign in to comment.