|
# attempt to check PGP signature in a RFC3156-compliant way
|
|
module TMail
|
|
class Mail
|
|
def is_pgp_signed?
|
|
content_type == "multipart/signed" and parts.size == 2 and
|
|
parts[1].content_type == "application/pgp-signature"
|
|
end
|
|
|
|
def pgp_signature
|
|
return nil unless is_pgp_signed?
|
|
parts[1].decoded
|
|
end
|
|
|
|
def pgp_signed_part
|
|
return nil unless is_pgp_signed?
|
|
parts[0]
|
|
end
|
|
|
|
def verify_pgp_signature
|
|
return nil unless is_pgp_signed?
|
|
|
|
content = parts[0].to_rfc3156
|
|
sig = pgp_signature()
|
|
|
|
sigs_check = nil
|
|
GPGME.verify(sig, content) do |signature|
|
|
sigs_check ||= []
|
|
sigs_check << signature
|
|
end
|
|
|
|
sigs_check
|
|
end
|
|
|
|
def is_pgp_encrypted?
|
|
content_type == "multipart/encrypted" and parts.size == 2 and
|
|
parts[0].content_type == "application/pgp-encrypted" and
|
|
parts[1].content_type == "application/octet-stream"
|
|
end
|
|
|
|
def pgp_crypt_info
|
|
return nil unless is_pgp_encrypted?
|
|
a = parts[0].body.split("\n").collect{|l| l.chomp.split(": ") if l =~ /: / }.compact.flatten
|
|
Hash[*a]
|
|
end
|
|
|
|
def pgp_encrypted_part
|
|
return nil unless is_pgp_encrypted?
|
|
parts[1].body
|
|
end
|
|
|
|
def pgp_decrypt(&passphrase_callback)
|
|
return nil unless is_pgp_encrypted?
|
|
protocol_version = pgp_crypt_info()["Version"].to_i
|
|
raise NotImplementedError, "pgp-encrypted protocol version #{protocol_version} is not implemented" unless protocol_version == 1
|
|
|
|
encrypted_data = pgp_encrypted_part()
|
|
GPGME.decrypt(encrypted_data, {:passphrase_callback => method(:gpg_passphrase_callback_wrapper),
|
|
:passphrase_callback_value => passphrase_callback, :textmode => true})
|
|
end
|
|
|
|
def pgp_crypt(fingerprint)
|
|
GPGME.encrypt([gpg_key(fingerprint)], self.to_s, {:armor => true, :always_trust => true})
|
|
end
|
|
|
|
def pgp_sign(signers_id, &passphrase_callback)
|
|
signers = signers_id.collect{|key_id| gpg_key(key_id) }
|
|
GPGME.sign(self.to_rfc3156, {:signers => signers, :passphrase_callback => method(:gpg_passphrase_callback_wrapper),
|
|
:passphrase_callback_value => passphrase_callback, :armor => true})
|
|
end
|
|
|
|
def to_rfc3156
|
|
# using RAW part, without any decoding
|
|
# remove last EOL due to MIME protocol and properly convert all EOL to CRLF
|
|
raw.chomp.gsub(/\r?\n/, "\r\n")
|
|
end
|
|
|
|
protected
|
|
|
|
def gpg_key(fingerprint)
|
|
gpg = GPGME::Ctx.new
|
|
gpg.get_key(fingerprint)
|
|
end
|
|
|
|
def gpg_passphrase_callback_wrapper(hook, uid_hint, passphrase_info, prev_was_bad, fd)
|
|
# sending key
|
|
io = IO.for_fd(fd, 'w')
|
|
io.puts hook.call(uid_hint, passphrase_info, prev_was_bad)
|
|
io.flush
|
|
end
|
|
|
|
def raw
|
|
@port.read_all
|
|
end
|
|
end
|
|
end
|