mail-gpg 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0b78947766a213897b05befac957f4897f431b9a
4
+ data.tar.gz: 50434795fe71f90c31e792080a2e961ad18424d4
5
+ SHA512:
6
+ metadata.gz: e4d529af60a76911181bd5be28dbf074e2636f9401bed1634cf277c567d9f8041a63cd5b6b6159be5db82a0cf92f80ec9278d98d46261e9f669f3bdc0eec4e5d
7
+ data.tar.gz: a40e501cf9b446d6be4c9a1342d4bbed72427f28bb5b41301409959baef215dba9e8a0eb52fd5c275e2f0170a7ac3df968bc571ee13035383985c79310abb89b
@@ -0,0 +1,18 @@
1
+ test/gpghome
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mail-gpg.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jens Kraemer
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,84 @@
1
+ # Mail::Gpg
2
+
3
+ This gem adds GPG/MIME encryption capabilities to the [Ruby Mail
4
+ Library](https://github.com/mikel/mail)
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'mail-gpg'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install mail-gpg
19
+
20
+
21
+ ## Usage
22
+
23
+ ### Encrypting / Signing
24
+ Construct your Mail object as usual and hand it to `Mail::Gpg.encrypt` to get
25
+ an encrypted Mail::Message object:
26
+
27
+ m = Mail.new do
28
+ to 'jane@doe.net'
29
+ from 'john@doe.net'
30
+ subject 'gpg test'
31
+ body "encrypt me!"
32
+ add_file "some_attachment.zip"
33
+ end
34
+
35
+ # encrypt message, no signing
36
+ Mail::Gpg.encrypt(m).deliver
37
+
38
+ # encrypt and sign message with sender's private key, using the given
39
+ # passphrase to decrypt the key
40
+ Mail::Gpg.encrypt(m, sign: true, password: 'secret').deliver
41
+
42
+ # encrypt and sign message using a different key
43
+ Mail::Gpg.encrypt(m, sign_as: 'joe@otherdomain.com', password: 'secret').deliver
44
+
45
+ # encrypt and sign message and use a callback function to provide the
46
+ # passphrase.
47
+ Mail::Gpg.encrypt(m, sign_as: 'joe@otherdomain.com',
48
+ passphrase_callback: ->(obj, uid_hint, passphrase_info, prev_was_bad, fd){puts "Enter passphrase for #{passphrase_info}: "; (IO.for_fd(fd, 'w') << readline.chomp).flush }).deliver
49
+
50
+ Make sure all recipients' public keys are in your local gpg keychain. You may also want to have a look at the [GPGME](https://github.com/ueno/ruby-gpgme) docs and code base for more info on the various options, especially regarding the `passphrase_callback` arguments.
51
+
52
+
53
+ ### Signing only
54
+
55
+ This is not implemented yet
56
+
57
+
58
+ ## Running the tests
59
+
60
+ rake
61
+
62
+ The first run might take a while since it sets up a mock gpg home directory in
63
+ test/gpghome containing two different identities used in the test cases.
64
+ Following test runs will use that directory if it still exists and will
65
+ therefore be substantially faster.
66
+
67
+ ## Todo
68
+
69
+ * Signing of unencrypted mails
70
+ * Add optional on the fly import of recipients' keys from public key servers based on email address
71
+ * Send encrypted mails to recipients when possible, fall back to unencrypted
72
+ mail otherwise
73
+ * Ease and document usage with Rails' ActionMailer
74
+
75
+
76
+ ## Contributing
77
+
78
+ 1. Fork it
79
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
80
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
81
+ 4. Push to the branch (`git push origin my-new-feature`)
82
+ 5. Create new Pull Request
83
+
84
+
@@ -0,0 +1,48 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+ require 'gpgme'
4
+
5
+ def setup_gpghome
6
+ gpghome = File.join File.dirname(__FILE__), 'test', 'gpghome'
7
+ ENV['GNUPGHOME'] = gpghome
8
+ unless File.directory? gpghome
9
+ FileUtils.mkdir_p gpghome
10
+ GPGME::Ctx.new do |gpg|
11
+ gpg.generate_key <<-END
12
+ <GnupgKeyParms format="internal">
13
+ Key-Type: DSA
14
+ Key-Length: 1024
15
+ Subkey-Type: ELG-E
16
+ Subkey-Length: 1024
17
+ Name-Real: Joe Tester
18
+ Name-Comment: with stupid passphrase
19
+ Name-Email: joe@foo.bar
20
+ Expire-Date: 0
21
+ Passphrase: abc
22
+ </GnupgKeyParms>
23
+ END
24
+ gpg.generate_key <<-END
25
+ <GnupgKeyParms format="internal">
26
+ Key-Type: DSA
27
+ Key-Length: 1024
28
+ Subkey-Type: ELG-E
29
+ Subkey-Length: 1024
30
+ Name-Real: Jane Doe
31
+ Name-Comment: with stupid passphrase
32
+ Name-Email: jane@foo.bar
33
+ Expire-Date: 0
34
+ Passphrase: abc
35
+ </GnupgKeyParms>
36
+ END
37
+ end
38
+ end
39
+ end
40
+
41
+ task :default => [:test]
42
+ Rake::TestTask.new(:test) do |test|
43
+ setup_gpghome
44
+ test.libs << 'test'
45
+ test.test_files = FileList['test/**/*_test.rb']
46
+ test.verbose = true
47
+ end
48
+
@@ -0,0 +1,33 @@
1
+ require 'open-uri'
2
+ class Hkp
3
+ def initialize(keyserver = 'http://pool.sks-keyservers.net:11371')
4
+ @keyserver = keyserver
5
+ end
6
+
7
+ def search(name)
8
+ [].tap do |results|
9
+ open("#{@keyserver}/pks/lookup?options=mr&search=#{URI.escape name}") do |f|
10
+ f.each_line do |l|
11
+ if l =~ /pub:(\w{8}):/
12
+ results << $1
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ def fetch(id)
20
+ open("#{@keyserver}/pks/lookup?options=mr&op=get&search=0x#{URI.escape id}") do |f|
21
+ return clean_key f.read
22
+ end
23
+
24
+ end
25
+
26
+ private
27
+ def clean_key(key)
28
+ if key =~ /(-----BEGIN PGP PUBLIC KEY BLOCK-----.*-----END PGP PUBLIC KEY BLOCK-----)/m
29
+ return $1
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1 @@
1
+ require 'mail/gpg'
@@ -0,0 +1,46 @@
1
+ require 'mail'
2
+ require 'gpgme'
3
+
4
+ require 'mail/gpg/version'
5
+ require 'mail/gpg/version_part'
6
+ require 'mail/gpg/encrypted_part'
7
+
8
+ module Mail
9
+ module Gpg
10
+ # options are:
11
+ # :sign : sign message using the sender's private key
12
+ # :sign_as : sign using this key (give the corresponding email address)
13
+ # :passphrase: passphrase for the signing key
14
+ # :always_trust : send encrypted mail to untrusted receivers, true by default
15
+ def self.encrypt(cleartext_mail, options = {})
16
+ receivers = []
17
+ receivers += cleartext_mail.to if cleartext_mail.to
18
+ receivers += cleartext_mail.cc if cleartext_mail.cc
19
+ receivers += cleartext_mail.bcc if cleartext_mail.bcc
20
+
21
+ if options[:sign_as]
22
+ options[:sign] = true
23
+ options[:signers] = options.delete(:sign_as)
24
+ elsif options[:sign]
25
+ options[:signers] = cleartext_mail.from
26
+ end
27
+
28
+ Mail.new do
29
+ from cleartext_mail.from
30
+ to cleartext_mail.to
31
+ cc cleartext_mail.cc
32
+ bcc cleartext_mail.bcc
33
+ subject cleartext_mail.subject
34
+ add_part VersionPart.new
35
+ add_part EncryptedPart.new(cleartext_mail,
36
+ options.merge({recipients: receivers}))
37
+ content_type "multipart/encrypted; protocol=\"application/pgp-encrypted\"; boundary=#{boundary}"
38
+ body.preamble = "This is an OpenPGP/MIME encrypted message (RFC 2440 and 3156)"
39
+ end
40
+ end
41
+
42
+ def self.decrypt(encrypted_mail, options = {})
43
+ # TODO :)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,84 @@
1
+ module Mail
2
+ module Gpg
3
+ class EncryptedPart < Mail::Part
4
+
5
+ # options are:
6
+ # :signers : sign using this key (give the corresponding email address)
7
+ # :passphrase: passphrase for the signing key
8
+ # :recipients : array of receiver addresses
9
+ # :always_trust : send encrypted mail to untrusted receivers, true by default
10
+ def initialize(cleartext_mail, options = {})
11
+ options = { always_trust: true }.merge options
12
+ #clear_part = Mail.new
13
+ #parts.each do |p|
14
+ # clear_part.add_part p
15
+ #end
16
+
17
+ encrypted = encrypt(cleartext_mail.encoded, options)
18
+ super() do
19
+ body encrypted.to_s
20
+ content_type 'application/octet-stream; name="encrypted.asc"'
21
+ content_disposition 'inline; filename="encrypted.asc"'
22
+ content_description 'OpenPGP encrypted message'
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def encrypt(plain, options = {})
29
+ options = options.merge({armor: true})
30
+
31
+ plain_data = GPGME::Data.new(plain)
32
+ cipher_data = GPGME::Data.new(options[:output])
33
+
34
+ recipient_keys = keys_for_data options[:recipients], options.delete(:keys)
35
+
36
+ flags = 0
37
+ flags |= GPGME::ENCRYPT_ALWAYS_TRUST if options[:always_trust]
38
+
39
+ GPGME::Ctx.new(options) do |ctx|
40
+ begin
41
+ if options[:sign]
42
+ if options[:signers]
43
+ signers = Key.find(:public, options[:signers], :sign)
44
+ ctx.add_signer(*signers)
45
+ end
46
+ ctx.encrypt_sign(recipient_keys, plain_data, cipher_data, flags)
47
+ else
48
+ ctx.encrypt(recipient_keys, plain_data, cipher_data, flags)
49
+ end
50
+ rescue GPGME::Error::UnusablePublicKey => exc
51
+ exc.keys = ctx.encrypt_result.invalid_recipients
52
+ raise exc
53
+ rescue GPGME::Error::UnusableSecretKey => exc
54
+ exc.keys = ctx.sign_result.invalid_signers
55
+ raise exc
56
+ end
57
+ end
58
+
59
+ cipher_data.seek(0)
60
+ cipher_data
61
+ end
62
+
63
+ # normalizes the list of recipients' emails, key ids and key data to a
64
+ # list of Key objects
65
+ def keys_for_data(emails_or_shas_or_keys, key_data = nil)
66
+ keys = if key_data
67
+ emails_or_shas_or_keys.map do |r|
68
+ # import any given keys
69
+ k = key_data[r]
70
+ if k and k =~ /-----BEGIN PGP/
71
+ GPGME::Key.import k
72
+ k = nil
73
+ end
74
+ GPGME::Key.find(:public, k || r)
75
+ end.flatten
76
+ else
77
+ # key lookup in keychain for all receivers
78
+ GPGME::Key.find :public, emails_or_shas_or_keys, :encrypt
79
+ end
80
+ end
81
+
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,5 @@
1
+ module Mail
2
+ module Gpg
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ require 'mail/part'
2
+
3
+ module Mail
4
+ module Gpg
5
+ class VersionPart < Mail::Part
6
+ def initialize(*args)
7
+ super
8
+ body 'Version 1.0'
9
+ content_type 'application/pgp-encrypted'
10
+ content_description 'PGP/MIME Versions Identification'
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mail/gpg/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mail-gpg"
8
+ spec.version = Mail::Gpg::VERSION
9
+ spec.authors = ["Jens Kraemer"]
10
+ spec.email = ["jk@jkraemer.net"]
11
+ spec.description = "GPG/MIME encryption plugin for the Ruby Mail Library\nBecause privacy matters."
12
+ spec.summary = %q{GPG/MIME encryption plugin for the Ruby Mail Library}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "mail", "~> 2.5.3"
22
+ spec.add_dependency "gpgme", "~> 2.0.2"
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "pry-nav"
26
+ spec.add_development_dependency "shoulda-context"
27
+ end
@@ -0,0 +1,89 @@
1
+ require 'test_helper'
2
+
3
+ class GpgTest < Test::Unit::TestCase
4
+
5
+ def check_headers(mail, encrypted)
6
+ assert_equal mail.to, encrypted.to
7
+ assert_equal mail.cc, encrypted.cc
8
+ assert_equal mail.bcc, encrypted.bcc
9
+ assert_equal mail.subject, encrypted.subject
10
+ end
11
+
12
+ def check_mime_structure(mail, encrypted)
13
+ assert_equal 2, encrypted.parts.size
14
+ assert_match /Version 1/, encrypted.parts.first.to_s
15
+ end
16
+
17
+ context "gpg installation" do
18
+ should "have keys for jane and joe" do
19
+ assert joe = GPGME::Key.find(:public, 'joe@foo.bar').first
20
+ assert jane = GPGME::Key.find(:public, 'jane@foo.bar').first
21
+ end
22
+ end
23
+
24
+ context "gpg encrypted" do
25
+
26
+ setup do
27
+ @mail = Mail.new do
28
+ to 'jane@foo.bar'
29
+ from 'joe@foo.bar'
30
+ subject 'test test'
31
+ body 'encrypt me!'
32
+ end
33
+ end
34
+
35
+
36
+ context 'mail with multiple recipients' do
37
+ setup do
38
+ # @mail.bcc 'joe@foo.bar'
39
+ @encrypted = Mail::Gpg.encrypt(@mail)
40
+ end
41
+
42
+ should 'have same recipients and subject' do
43
+ check_headers @mail, @encrypted
44
+ end
45
+
46
+ should 'have proper gpgmime structure' do
47
+ check_mime_structure @mail, @encrypted
48
+ end
49
+
50
+ should "encrypt for all recipients" do
51
+ assert encrypted_body = @encrypted.parts.last.to_s
52
+ end
53
+
54
+ end
55
+
56
+ context 'multipart mail' do
57
+ setup do
58
+ @mail.add_file 'Rakefile'
59
+ @encrypted = Mail::Gpg.encrypt(@mail)
60
+ end
61
+
62
+ should 'have same recipients and subject' do
63
+ check_headers @mail, @encrypted
64
+ end
65
+
66
+ should 'have proper gpgmime structure' do
67
+ check_mime_structure @mail, @encrypted
68
+ end
69
+ end
70
+
71
+ context 'simple mail' do
72
+ setup do
73
+ @encrypted = Mail::Gpg.encrypt(@mail)
74
+ end
75
+
76
+ should 'have same recipients and subject' do
77
+ check_headers @mail, @encrypted
78
+ end
79
+
80
+ should 'have proper gpgmime structure' do
81
+ check_mime_structure @mail, @encrypted
82
+ end
83
+
84
+ end
85
+
86
+
87
+ end
88
+ end
89
+
@@ -0,0 +1,5 @@
1
+ require 'test/unit'
2
+ require 'shoulda/context'
3
+ require 'mail-gpg'
4
+ require 'pry-nav'
5
+
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mail-gpg
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jens Kraemer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mail
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 2.5.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.5.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: gpgme
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 2.0.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 2.0.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-nav
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: shoulda-context
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: |-
98
+ GPG/MIME encryption plugin for the Ruby Mail Library
99
+ Because privacy matters.
100
+ email:
101
+ - jk@jkraemer.net
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - .gitignore
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - lib/hkp.rb
112
+ - lib/mail-gpg.rb
113
+ - lib/mail/gpg.rb
114
+ - lib/mail/gpg/encrypted_part.rb
115
+ - lib/mail/gpg/version.rb
116
+ - lib/mail/gpg/version_part.rb
117
+ - mail-gpg.gemspec
118
+ - test/gpg_test.rb
119
+ - test/test_helper.rb
120
+ homepage: ''
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.0.3
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: GPG/MIME encryption plugin for the Ruby Mail Library
144
+ test_files:
145
+ - test/gpg_test.rb
146
+ - test/test_helper.rb
147
+ has_rdoc: