mr_smime 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 687e80fee82c8782704981eb2a3889da7a26cf4e
4
+ data.tar.gz: ec4468a3fcdfe6eda52839db21e9b61ced078e00
5
+ SHA512:
6
+ metadata.gz: dd5f3d2a00eccc9e1ffdcd3b0ea62836e120df121ea7299aad3efe0de7f39caf363b5ff0e3359e1249a1dab152b649cf922ef08aa85a42e94d566d79496da39d
7
+ data.tar.gz: 4eb613dfcb1448e47608f4faaecb2bd9c4a98660364d9b4578413f2a9ffc3e79d521e77c4f98aebeb006999af4c4ee26e7442057b72b761d88fc4d0a80f7b806
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .rubocop-*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,2 @@
1
+ inherit_from:
2
+ - https://raw.githubusercontent.com/bluerail/rubocop-config/master/rubocop.yml
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mr_smime.gemspec
4
+ gemspec
@@ -0,0 +1,84 @@
1
+ # MrSmime
2
+
3
+ Secure/Multipurpose Internet Mail Extensions (S/MIME) support for ActionMailer
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mr_smime'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ## Usage
18
+
19
+ Setup a certificate-path in your applicaion.rb:
20
+
21
+ ```ruby
22
+ module MyApp
23
+ class Application < Rails::Application
24
+
25
+ ...
26
+
27
+ MrSmime.configure do |config|
28
+ config.certificate_path = Rails.root.join('config', 'certificates')
29
+ end
30
+
31
+ ...
32
+ end
33
+ end
34
+ ```
35
+
36
+ Add certificates for each of your senders (and recipients if you want encryption). We expect a .key and .pem file where
37
+ @ has been replaced by . (e.g. john@example.com results in john.example.com.key and john.example.com.pem)
38
+
39
+ That's it!
40
+
41
+ ## Configuration-options
42
+
43
+ | Option | Default value | Description |
44
+ |------------------|---------------|------------------------------------------------------|
45
+ | certificate_path | | Pathname to location of certificate-files |
46
+ | enabled | true | Boolean to have Mr Smime actually perform it's magic |
47
+
48
+ ## TODO
49
+
50
+ * Make it easy to save certificates from incoming e-mails (so we can sent encrypted mails back to them)
51
+ * Add options to use keys with passphrases
52
+ * Add options to enable on a per something base
53
+
54
+ ## Development
55
+
56
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
57
+ also run `bin/console` for an interactive prompt that will allow you to experiment.
58
+
59
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
60
+ version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
61
+ push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
62
+
63
+ ### Creating self signed certificates
64
+
65
+ Create a CA certificate first:
66
+
67
+ ```
68
+ $ openssl genrsa -out ca.key 4096
69
+ $ openssl req -new -x509 -days 365 -key ca.key -out ca.crt
70
+ ```
71
+
72
+ Then create a certificate for each of your mail addresses (i'm certain there is a better way to do this):
73
+
74
+ ```
75
+ $ openssl genrsa -out jane.example.com.key 4096
76
+ $ openssl req -new -key jane.example.com.key -out jane.example.com.csr
77
+ $ openssl x509 -req -days 365 -in jane.example.com.csr -CA ca.crt -CAkey ca.key -set_serial 1 -out jane.example.com.crt -setalias "Self Signed SMIME" -addtrust emailProtection -addreject clientAuth -addreject serverAuth -trustout
78
+ $ openssl pkcs12 -export -in jane.example.com.crt -inkey jane.example.com.key -out jane.example.com.p12
79
+ $ openssl pkcs12 -in jane.example.com.p12 -clcerts -nokeys -out jane.example.com.pem
80
+ ```
81
+
82
+ ## Contributing
83
+
84
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bluerail/mr_smime.
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'mr_smime'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require 'pry'
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ require 'mail'
2
+
3
+ require 'mr_smime/certificate'
4
+ require 'mr_smime/configuration'
5
+ require 'mr_smime/encrypter'
6
+ require 'mr_smime/interceptor'
7
+ require 'mr_smime/signer'
8
+ require 'mr_smime/version'
9
+
10
+ module MrSmime
11
+ class << self
12
+ attr_accessor :configuration
13
+ end
14
+
15
+ def self.configuration
16
+ @configuration ||= Configuration.new
17
+ end
18
+
19
+ def self.configure
20
+ yield(configuration)
21
+ end
22
+ end
23
+
24
+ Mail.register_interceptor(MrSmime::Interceptor)
@@ -0,0 +1,53 @@
1
+ require 'openssl'
2
+
3
+ module MrSmime
4
+ class Certificate
5
+ include OpenSSL
6
+
7
+ def self.find(email)
8
+ return unless exist?(email)
9
+
10
+ new(email)
11
+ end
12
+
13
+ def self.exist?(email)
14
+ new(email).present?
15
+ end
16
+
17
+ def self.root
18
+ MrSmime.configuration.certificate_path
19
+ end
20
+
21
+ def self.filename(email, extension)
22
+ email.tr('@', '.') + '.' + extension.to_s
23
+ end
24
+
25
+ def initialize(email)
26
+ @email = email
27
+ end
28
+
29
+ def present?
30
+ File.exist? certificate_path
31
+ end
32
+
33
+ def certificate
34
+ X509::Certificate.new(File.read(certificate_path))
35
+ end
36
+
37
+ def certificate_path
38
+ filename(:pem)
39
+ end
40
+
41
+ def private_key
42
+ PKey::RSA.new(File.read(private_key_path))
43
+ end
44
+
45
+ def private_key_path
46
+ filename(:key)
47
+ end
48
+
49
+ def filename(extension)
50
+ File.join Certificate.root, Certificate.filename(@email, extension)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,9 @@
1
+ module MrSmime
2
+ class Configuration
3
+ attr_accessor :certificate_path, :enabled
4
+
5
+ def initialize
6
+ @enabled = true
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,36 @@
1
+ require 'openssl'
2
+
3
+ module MrSmime
4
+ class Encrypter
5
+ def initialize(message)
6
+ @message = message
7
+ end
8
+
9
+ def encrypted_data(data)
10
+ return data unless encryptable?
11
+
12
+ OpenSSL::PKCS7.write_smime(
13
+ OpenSSL::PKCS7.encrypt(
14
+ recipient_certificates.map(&:certificate),
15
+ data,
16
+ cipher
17
+ )
18
+ )
19
+ end
20
+
21
+ def encryptable?
22
+ Signer.new(@message).signable? &&
23
+ recipient_certificates.all? { |certificate| !certificate.nil? && certificate.present? }
24
+ end
25
+
26
+ def cipher
27
+ @cipher ||= OpenSSL::Cipher.new('AES-128-CBC')
28
+ end
29
+
30
+ def recipient_certificates
31
+ @message.to.map do |email_address|
32
+ Certificate.find(email_address)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,33 @@
1
+ module MrSmime
2
+ class Interceptor
3
+ class << self
4
+ def delivering_email(message)
5
+ return message unless MrSmime.configuration.enabled
6
+
7
+ encrypted_message = Mail.new(encrypted_data(message, signed_data(message, message.encoded)))
8
+
9
+ overwrite_body(message, encrypted_message)
10
+ overwrite_headers(message, encrypted_message)
11
+ end
12
+
13
+ def signed_data(message, data)
14
+ Signer.new(message).signed_data(data)
15
+ end
16
+
17
+ def encrypted_data(message, data)
18
+ Encrypter.new(message).encrypted_data(data)
19
+ end
20
+
21
+ def overwrite_body(message, encrypted_message)
22
+ message.body = nil
23
+ message.body = encrypted_message.body.encoded
24
+ end
25
+
26
+ def overwrite_headers(message, encrypted_message)
27
+ message.content_disposition = encrypted_message.content_disposition if encrypted_message.content_disposition
28
+ message.content_transfer_encoding = encrypted_message.content_transfer_encoding
29
+ message.content_type = encrypted_message.content_type
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,39 @@
1
+ require 'openssl'
2
+
3
+ module MrSmime
4
+ class Signer
5
+ def initialize(message)
6
+ @message = message
7
+ end
8
+
9
+ def signed_data(data)
10
+ return data unless signable?
11
+
12
+ OpenSSL::PKCS7.write_smime(
13
+ OpenSSL::PKCS7.sign(
14
+ sender_certificate.certificate,
15
+ sender_certificate.private_key,
16
+ data,
17
+ [],
18
+ OpenSSL::PKCS7::DETACHED
19
+ )
20
+ )
21
+ end
22
+
23
+ def signable?
24
+ @message.from.length == 1 &&
25
+ !sender_certificate.nil? &&
26
+ sender_certificate.present?
27
+ end
28
+
29
+ def sender_certificate
30
+ sender_certificates.first
31
+ end
32
+
33
+ def sender_certificates
34
+ @message.from.map do |email_address|
35
+ Certificate.find email_address
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module MrSmime
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mr_smime/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'mr_smime'
8
+ spec.version = MrSmime::VERSION
9
+ spec.authors = ['Rene van Lieshout']
10
+ spec.email = ['rene@bluerail.nl']
11
+
12
+ spec.summary = 'Secure/Multipurpose Internet Mail Extensions (S/MIME) support for ActionMailer'
13
+ spec.homepage = ''
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = 'exe'
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.11'
21
+ spec.add_development_dependency 'rake', '~> 10.0'
22
+ spec.add_development_dependency 'rspec', '~> 3.0'
23
+
24
+ spec.add_dependency 'mail', '>= 2.1.2' # register_interceptor
25
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mr_smime
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rene van Lieshout
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mail
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 2.1.2
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: 2.1.2
69
+ description:
70
+ email:
71
+ - rene@bluerail.nl
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - .rubocop.yml
79
+ - Gemfile
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - lib/mr_smime.rb
85
+ - lib/mr_smime/certificate.rb
86
+ - lib/mr_smime/configuration.rb
87
+ - lib/mr_smime/encrypter.rb
88
+ - lib/mr_smime/interceptor.rb
89
+ - lib/mr_smime/signer.rb
90
+ - lib/mr_smime/version.rb
91
+ - mr_smime.gemspec
92
+ homepage: ''
93
+ licenses: []
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.0.14.1
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Secure/Multipurpose Internet Mail Extensions (S/MIME) support for ActionMailer
115
+ test_files: []