Project

General

Profile

« Previous | Next » 

Revision 56793619

Added by Marc Dequènes about 15 years ago

  • ID 56793619de70bf79a709917c56581ee31364f171

[evol] preliminary work on crypting mail response

View differences:

lib/cyborghood/mail.rb
require 'delegate'
require 'tmail'
require 'tmail_extra'
require 'gpgme'
require 'gpgme' # >= 1.0.2 needed for :always_trust sign option
require 'net/smtp'
# This class handles RFC3156 signed messages, validates them, and extract orders properly.
......
def to_s
@mail.to_s
end
def crypt(fingerprint)
# build a fake mail to get the generated content to be crypted
# TODO: this is buggy yet
if @mail.multipart?
parts = @mail.parts
else
# transform to multipart
single_part = TMail::Mail.new
single_part.content_type = @mail.content_type
single_part.body = @mail.body
single_part.transfer_encoding = @mail.transfer_encoding
parts = [single_part]
end
fake_mail = TMail::Mail.new
fake_mail.set_content_type("multipart", "mixed", {'boundary' => TMail.new_boundary})
parts.each {|p| fake_mail.parts << p }
clear_data = fake_mail.to_s
# remove fake headers to get only generated parts
clear_data.sub!(/^.*\n\n/, "")
# retrieve key and encrypt
gpg = GPGME::Ctx.new
key = gpg.get_key(fingerprint)
p clear_data
encrypted_data = GPGME.encrypt([key], clear_data, {:armor => true, :always_trust => true})
# build properly encrypted mail
p_pgp = TMail::Mail.new
p_pgp.set_content_type("application", "pgp-encrypted")
p_pgp.body = "Version: 1"
p_encrypted = TMail::Mail.new
p_encrypted.set_content_type("application", "octet-stream")
p_encrypted.body = encrypted_data
@mail.set_content_type("multipart", "encrypted", {'boundary' => TMail.new_boundary})
@mail.parts << p_pgp
@mail.parts << p_encrypted
end
end
end

Also available in: Unified diff