Module: Mail::Gpg

Defined in:
lib/mail/gpg.rb,
lib/mail/gpg/version.rb,
lib/mail/gpg/sign_part.rb,
lib/mail/gpg/signed_part.rb,
lib/mail/gpg/gpgme_helper.rb,
lib/mail/gpg/version_part.rb,
lib/mail/gpg/message_patch.rb,
lib/mail/gpg/verified_part.rb,
lib/mail/gpg/decrypted_part.rb,
lib/mail/gpg/encrypted_part.rb,
lib/mail/gpg/delivery_handler.rb,
lib/mail/gpg/missing_keys_error.rb,
lib/mail/gpg/mime_signed_message.rb,
lib/mail/gpg/inline_signed_message.rb,
lib/mail/gpg/verify_result_attribute.rb,
lib/mail/gpg/inline_decrypted_message.rb,
lib/mail/gpg/rails/action_mailer_base_patch.rb

Defined Under Namespace

Modules: MessagePatch, Rails, VerifyResultAttribute Classes: DecryptedPart, DeliveryHandler, EncryptedPart, GpgmeHelper, InlineDecryptedMessage, InlineSignedMessage, MimeSignedMessage, MissingKeysError, SignPart, SignedPart, VerifiedPart, VersionPart

Constant Summary collapse

VERSION =
"0.4.1"

Class Method Summary collapse

Class Method Details

.decrypt(encrypted_mail, options = {}) ⇒ Object

options are: :verify: decrypt and verify



64
65
66
67
68
69
70
71
72
# File 'lib/mail/gpg.rb', line 64

def self.decrypt(encrypted_mail, options = {})
  if encrypted_mime?(encrypted_mail)
    decrypt_pgp_mime(encrypted_mail, options)
  elsif encrypted_inline?(encrypted_mail)
    decrypt_pgp_inline(encrypted_mail, options)
  else
    raise EncodingError, "Unsupported encryption format '#{encrypted_mail.content_type}'"
  end
end

.encrypt(cleartext_mail, options = {}) ⇒ Object

options are: :sign: sign message using the sender’s private key :sign_as: sign using this key (give the corresponding email address or key fingerprint) :passphrase: passphrase for the signing key :keys: A hash mapping recipient email addresses to public keys or public key ids. Imports any keys given here that are not already part of the local keychain before sending the mail. :always_trust: send encrypted mail to untrusted receivers, true by default



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mail/gpg.rb', line 28

def self.encrypt(cleartext_mail, options = {})
  construct_mail(cleartext_mail, options) do
    receivers = []
    receivers += cleartext_mail.to if cleartext_mail.to
    receivers += cleartext_mail.cc if cleartext_mail.cc
    receivers += cleartext_mail.bcc if cleartext_mail.bcc

    if options[:sign_as]
      options[:sign] = true
      options[:signers] = options.delete(:sign_as)
    elsif options[:sign]
      options[:signers] = cleartext_mail.from
    end

    add_part VersionPart.new
    add_part EncryptedPart.new(cleartext_mail,
                               options.merge({recipients: receivers}))
    content_type "multipart/encrypted; protocol=\"application/pgp-encrypted\"; boundary=#{boundary}"
    body.preamble = options[:preamble] || "This is an OpenPGP/MIME encrypted message (RFC 2440 and 3156)"
  end
end

.encrypted?(mail) ⇒ Boolean

true if a mail is encrypted

Returns:

  • (Boolean)


85
86
87
88
89
# File 'lib/mail/gpg.rb', line 85

def self.encrypted?(mail)
  return true if encrypted_mime?(mail)
  return true if encrypted_inline?(mail)
  false
end

.sign(cleartext_mail, options = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mail/gpg.rb', line 50

def self.sign(cleartext_mail, options = {})
  options[:sign_as] ||= cleartext_mail.from
  construct_mail(cleartext_mail, options) do
    to_be_signed = SignedPart.build(cleartext_mail)
    add_part to_be_signed
    add_part to_be_signed.sign(options)

    content_type "multipart/signed; micalg=pgp-sha1; protocol=\"application/pgp-signature\"; boundary=#{boundary}"
    body.preamble = options[:preamble] || "This is an OpenPGP/MIME signed message (RFC 4880 and 3156)"
  end
end

.signature_valid?(signed_mail, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
# File 'lib/mail/gpg.rb', line 74

def self.signature_valid?(signed_mail, options = {})
  if signed_mime?(signed_mail)
    signature_valid_pgp_mime?(signed_mail, options)
  elsif signed_inline?(signed_mail)
    signature_valid_inline?(signed_mail, options)
  else
    raise EncodingError, "Unsupported signature format '#{signed_mail.content_type}'"
  end
end

.signed?(mail) ⇒ Boolean

true if a mail is signed.

throws EncodingError if called on an encrypted mail (so only call this method if encrypted? is false)

Returns:

  • (Boolean)


94
95
96
97
98
99
100
101
# File 'lib/mail/gpg.rb', line 94

def self.signed?(mail)
  return true if signed_mime?(mail)
  return true if signed_inline?(mail)
  if encrypted?(mail)
    raise EncodingError, 'Unable to determine signature on an encrypted mail, use :verify option on decrypt()'
  end
  false
end