def test_encrypt_msg_no_pgp_env(tmpdir, tpath): # without a gpg environment, we cannot encrypt with (tpath / "samples/full-mail02").open("r") as fp: msg = Parser(policy=default_policy).parse(fp) changed, new_msg = pgp.encrypt_msg(msg, ["*****@*****.**"], str(tmpdir / "nowhere")) assert changed is False assert new_msg is msg
def test_encrypt_msg_no_key(tmpdir, tpath): # without key, we cannot encrypt gpg = gnupg.GPG(gnupghome=str(tmpdir)) gpg.import_keys((tpath / "alice.pub").read_text()) with (tpath / "samples/full-mail02").open("r") as fp: msg = Parser(policy=default_policy).parse(fp) changed, new_msg = pgp.encrypt_msg(msg, ["*****@*****.**"], str(tmpdir)) assert changed is False assert new_msg is msg
def test_encrypt_msg_not_all_keys(tmpdir, tpath): # we do only encrypt if all keys are available gpg = gnupg.GPG(gnupghome=str(tmpdir)) gpg.import_keys((tpath / "alice.pub").read_text()) with (tpath / "samples/full-mail02").open("r") as fp: msg = Parser(policy=default_policy).parse(fp) changed, new_msg = pgp.encrypt_msg(msg, ["*****@*****.**", "*****@*****.**"], str(tmpdir)) assert changed is False assert new_msg is msg
def test_encrypt_msg_multi_rcpts(tmpdir, tpath): # we can encypt messages for multple recipients gpg = gnupg.GPG(gnupghome=str(tmpdir)) gpg.import_keys((tpath / "alice.pub").read_text()) gpg.import_keys((tpath / "bob.pub").read_text()) with (tpath / "samples/full-mail02").open("r") as fp: msg = Parser(policy=default_policy).parse(fp) changed, new_msg = pgp.encrypt_msg(msg, ["*****@*****.**", "*****@*****.**"], str(tmpdir)) assert changed is True assert "-----BEGIN PGP MESSAGE-----" in new_msg.as_string()
def test_encrypt_msg(tmpdir, tpath): # we can encrypt a message gpg = gnupg.GPG(gnupghome=str(tmpdir)) gpg.import_keys((tpath / "alice3.pub").read_text()) with (tpath / "samples/full-mail02").open("r") as fp: msg = Parser(policy=default_policy).parse(fp) result = pgp.encrypt_msg(msg, ["*****@*****.**"], str(tmpdir)) assert result[0] is True enc_msg = result[1].as_string() assert "-----BEGIN PGP MESSAGE-----" in enc_msg assert result[1]['Content-Type'].startswith('multipart/encrypted') gpg.import_keys((tpath / "alice3.sec").read_text()) dec_msg = gpg.decrypt(enc_msg) assert dec_msg.ok is True assert dec_msg.data == (b'Content-Type: text/plain; charset=us-ascii\n' b'Content-Disposition: inline\n\nfoo bar baz\n\n') assert dec_msg.data == msg.as_bytes()
def eom(self): """Called when end of message is reached. """ self.addheader("X-PGPMilter", "Scanned by PGPMilter %s" % __version__, -1) self.fp.seek(0) msg = message_from_binary_file(self.fp, policy=default_policy) changed, new_msg = encrypt_msg(msg, self.rcpts, self.config.pgphome) if not changed: return Milter.ACCEPT self.update_headers(msg, new_msg) fp = BytesIO(new_msg.as_bytes().split(b'\n\n', 1)[1]) while True: buf = fp.read(8192) if len(buf) == 0: break self.replacebody(buf) return Milter.ACCEPT