diff options
author | drebs <drebs@leap.se> | 2013-05-09 18:09:07 -0300 |
---|---|---|
committer | drebs <drebs@leap.se> | 2013-05-11 17:01:36 -0300 |
commit | f492a7983ac4694ed8052f3ca79e5011e8711f83 (patch) | |
tree | bb3731d54f8753fe21d61f6d5899383c44fa5e3c /src/leap/mail/tests | |
parent | a7584ae58e7d08498199d8a556c928f7b8d6b20e (diff) |
Make smtp-relay sign all outgoing message.
* Update docstrings.
* Remove prepareHeader and its bug.
* Make smtp-relay always sign outgoing mail.
Diffstat (limited to 'src/leap/mail/tests')
-rw-r--r-- | src/leap/mail/tests/smtp/__init__.py | 65 | ||||
-rw-r--r-- | src/leap/mail/tests/smtp/test_smtprelay.py | 142 |
2 files changed, 154 insertions, 53 deletions
diff --git a/src/leap/mail/tests/smtp/__init__.py b/src/leap/mail/tests/smtp/__init__.py index 113e047..c69c34f 100644 --- a/src/leap/mail/tests/smtp/__init__.py +++ b/src/leap/mail/tests/smtp/__init__.py @@ -106,6 +106,9 @@ class TestCaseWithKeyManager(BaseLeapTest): # Key material for testing KEY_FINGERPRINT = "E36E738D69173C13D709E44F2F455E2824D18DDF" + +ADDRESS = 'leap@leap.se' + PUBLIC_KEY = """ -----BEGIN PGP PUBLIC KEY BLOCK----- Version: GnuPG v1.4.10 (GNU/Linux) @@ -159,6 +162,7 @@ ZtQ/VymwFL3XdUWV6B/hU4PVAFvO3qlOtdJ6TpE+nEWgcWjCv5g7RjXX =MuOY -----END PGP PUBLIC KEY BLOCK----- """ + PRIVATE_KEY = """ -----BEGIN PGP PRIVATE KEY BLOCK----- Version: GnuPG v1.4.10 (GNU/Linux) @@ -266,3 +270,64 @@ RZXoH+FTg9UAW87eqU610npOkT6cRaBxaMK/mDtGNdc= =JTFu -----END PGP PRIVATE KEY BLOCK----- """ + +ADDRESS_2 = 'anotheruser@leap.se' + +PUBLIC_KEY_2 = """ +-----BEGIN PGP PUBLIC KEY BLOCK----- +Version: GnuPG v1.4.10 (GNU/Linux) + +mI0EUYwJXgEEAMbTKHuPJ5/Gk34l9Z06f+0WCXTDXdte1UBoDtZ1erAbudgC4MOR +gquKqoj3Hhw0/ILqJ88GcOJmKK/bEoIAuKaqlzDF7UAYpOsPZZYmtRfPC2pTCnXq +Z1vdeqLwTbUspqXflkCkFtfhGKMq5rH8GV5a3tXZkRWZhdNwhVXZagC3ABEBAAG0 +IWFub3RoZXJ1c2VyIDxhbm90aGVydXNlckBsZWFwLnNlPoi4BBMBAgAiBQJRjAle +AhsDBgsJCAcDAgYVCAIJCgsEFgIDAQIeAQIXgAAKCRB/nfpof+5XWotuA/4tLN4E +gUr7IfLy2HkHAxzw7A4rqfMN92DIM9mZrDGaWRrOn3aVF7VU1UG7MDkHfPvp/cFw +ezoCw4s4IoHVc/pVlOkcHSyt4/Rfh248tYEJmFCJXGHpkK83VIKYJAithNccJ6Q4 +JE/o06Mtf4uh/cA1HUL4a4ceqUhtpLJULLeKo7iNBFGMCV4BBADsyQI7GR0wSAxz +VayLjuPzgT+bjbFeymIhjuxKIEwnIKwYkovztW+4bbOcQs785k3Lp6RzvigTpQQt +Z/hwcLOqZbZw8t/24+D+Pq9mMP2uUvCFFqLlVvA6D3vKSQ/XNN+YB919WQ04jh63 +yuRe94WenT1RJd6xU1aaUff4rKizuQARAQABiJ8EGAECAAkFAlGMCV4CGwwACgkQ +f536aH/uV1rPZQQAqCzRysOlu8ez7PuiBD4SebgRqWlxa1TF1ujzfLmuPivROZ2X +Kw5aQstxgGSjoB7tac49s0huh4X8XK+BtJBfU84JS8Jc2satlfwoyZ35LH6sDZck +I+RS/3we6zpMfHs3vvp9xgca6ZupQxivGtxlJs294TpJorx+mFFqbV17AzQ= +=Thdu +-----END PGP PUBLIC KEY BLOCK----- +""" + +PRIVATE_KEY_2 = """ +-----BEGIN PGP PRIVATE KEY BLOCK----- +Version: GnuPG v1.4.10 (GNU/Linux) + +lQHYBFGMCV4BBADG0yh7jyefxpN+JfWdOn/tFgl0w13bXtVAaA7WdXqwG7nYAuDD +kY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Mnbd1SrVC +rUJrNWQZL59mm5oigS00uIyR0SvusOr+UzTtd8ysRuwHy5d/LAZsbjQStaOMBILx +77TJveOel0a1QK0YSMF2ywZMCKvquvjli4hAtWYz/EwfuzQN3t23jc5ny+GqmqD2 +3FUxLJosFUfLNmECAO9KhVmJi+L9dswIs+2Dkjd1eiRQzNOEVffvYkGYZyKxNiXF +UA5kvyZcB4iAN9sWCybE4WHZ9jd4myGB0MPDGxkCAP1RsXJbbuD6zS7BXe5gwunO +2q4q7ptdSl/sJYQuTe1KNP5d/uGsvlcFfsYjpsopasPjFBIncc/2QThMKlhoEaEB +/0mVAxpT6SrEvUbJ18z7kna24SgMPr3OnPMxPGfvNLJY/Xv/A17YfoqjmByCvsKE +JCDjopXtmbcrZyoEZbEht9mko4ifBBgBAgAJBQJRjAleAhsMAAoJEH+d+mh/7lda +z2UEAKgs0crDpbvHs+z7ogQ+Enm4EalpcWtUxdbo83y5rj4r0TmdlysOWkLLcYBk +o6Ae7WnOPbNIboeF/FyvgbSQX1POCUvCXNrGrZX8KMmd+Sx+rA2XJCPkUv98Hus6 +THx7N776fcYHGumbqUMYrxrcZSbNveE6SaK8fphRam1dewM0 +=a5gs +-----END PGP PRIVATE KEY BLOCK----- +""" + diff --git a/src/leap/mail/tests/smtp/test_smtprelay.py b/src/leap/mail/tests/smtp/test_smtprelay.py index 6ef4e85..e48f129 100644 --- a/src/leap/mail/tests/smtp/test_smtprelay.py +++ b/src/leap/mail/tests/smtp/test_smtprelay.py @@ -28,6 +28,7 @@ from datetime import datetime from twisted.test import proto_helpers from twisted.mail.smtp import ( User, + Address, SMTPBadRcpt, ) from mock import Mock @@ -37,7 +38,11 @@ from leap.mail.smtp.smtprelay import ( SMTPFactory, EncryptedMessage, ) -from leap.mail.tests.smtp import TestCaseWithKeyManager +from leap.mail.tests.smtp import ( + TestCaseWithKeyManager, + ADDRESS, + ADDRESS_2, +) from leap.common.keymanager import openpgp @@ -52,11 +57,11 @@ IP_OR_HOST_REGEX = '(' + IP_REGEX + '|' + HOSTNAME_REGEX + ')' class TestSmtpRelay(TestCaseWithKeyManager): EMAIL_DATA = ['HELO relay.leap.se', - 'MAIL FROM: <user@leap.se>', - 'RCPT TO: <leap@leap.se>', + 'MAIL FROM: <%s>' % ADDRESS_2, + 'RCPT TO: <%s>' % ADDRESS, 'DATA', - 'From: User <user@leap.se>', - 'To: Leap <leap@leap.se>', + 'From: User <%s>' % ADDRESS_2, + 'To: Leap <%s>' % ADDRESS, 'Date: ' + datetime.now().strftime('%c'), 'Subject: test message', '', @@ -77,13 +82,15 @@ class TestSmtpRelay(TestCaseWithKeyManager): "Test if openpgp can encrypt and decrypt." text = "simple raw text" pubkey = self._km.get_key( - 'leap@leap.se', openpgp.OpenPGPKey, private=False) + ADDRESS, openpgp.OpenPGPKey, private=False) encrypted = openpgp.encrypt_asym(text, pubkey) - self.assertNotEqual(text, encrypted, "failed encrypting text") + self.assertNotEqual( + text, encrypted, "Ciphertext is equal to plaintext.") privkey = self._km.get_key( - 'leap@leap.se', openpgp.OpenPGPKey, private=True) + ADDRESS, openpgp.OpenPGPKey, private=True) decrypted = openpgp.decrypt_asym(encrypted, privkey) - self.assertEqual(text, decrypted, "failed decrypting text") + self.assertEqual(text, decrypted, + "Decrypted text differs from plaintext.") def test_relay_accepts_valid_email(self): """ @@ -104,7 +111,8 @@ class TestSmtpRelay(TestCaseWithKeyManager): for i, line in enumerate(self.EMAIL_DATA): proto.lineReceived(line + '\r\n') self.assertMatch(transport.value(), - '\r\n'.join(SMTP_ANSWERS[0:i + 1])) + '\r\n'.join(SMTP_ANSWERS[0:i + 1]), + 'Did not get expected answer from relay.') proto.setTimeout(None) def test_message_encrypt(self): @@ -113,17 +121,77 @@ class TestSmtpRelay(TestCaseWithKeyManager): """ proto = SMTPFactory( self._km, self._config).buildProtocol(('127.0.0.1', 0)) - user = User('leap@leap.se', 'relay.leap.se', proto, 'leap@leap.se') - m = EncryptedMessage(user, self._km, self._config) + fromAddr = Address(ADDRESS_2) + dest = User(ADDRESS, 'relay.leap.se', proto, ADDRESS) + m = EncryptedMessage(fromAddr, dest, self._km, self._config) for line in self.EMAIL_DATA[4:12]: m.lineReceived(line) m.eomReceived() privkey = self._km.get_key( - 'leap@leap.se', openpgp.OpenPGPKey, private=True) + ADDRESS, openpgp.OpenPGPKey, private=True) decrypted = openpgp.decrypt_asym(m._message.get_payload(), privkey) self.assertEqual( '\r\n'.join(self.EMAIL_DATA[9:12]) + '\r\n', - decrypted) + decrypted, + 'Decrypted text differs from plaintext.') + + def test_message_encrypt_sign(self): + """ + Test if message gets encrypted to destination email and signed with + sender key. + """ + proto = SMTPFactory( + self._km, self._config).buildProtocol(('127.0.0.1', 0)) + user = User(ADDRESS, 'relay.leap.se', proto, ADDRESS) + fromAddr = Address(ADDRESS_2) + m = EncryptedMessage(fromAddr, user, self._km, self._config) + for line in self.EMAIL_DATA[4:12]: + m.lineReceived(line) + # trigger encryption and signing + m.eomReceived() + # decrypt and verify + privkey = self._km.get_key( + ADDRESS, openpgp.OpenPGPKey, private=True) + pubkey = self._km.get_key(ADDRESS_2, openpgp.OpenPGPKey) + decrypted = openpgp.decrypt_asym( + m._message.get_payload(), privkey, verify=pubkey) + self.assertEqual( + '\r\n'.join(self.EMAIL_DATA[9:12]) + '\r\n', + decrypted, + 'Decrypted text differs from plaintext.') + + def test_message_sign(self): + """ + Test if message is signed with sender key. + """ + # mock the key fetching + self._km.fetch_keys_from_server = Mock(return_value=[]) + proto = SMTPFactory( + self._km, self._config).buildProtocol(('127.0.0.1', 0)) + user = User('ihavenopubkey@nonleap.se', 'relay.leap.se', proto, ADDRESS) + fromAddr = Address(ADDRESS_2) + m = EncryptedMessage(fromAddr, user, self._km, self._config) + for line in self.EMAIL_DATA[4:12]: + m.lineReceived(line) + # trigger signing + m.eomReceived() + # assert content of message + self.assertTrue( + m._message.get_payload().startswith( + '-----BEGIN PGP SIGNED MESSAGE-----\n' + + 'Hash: SHA1\n\n' + + ('\r\n'.join(self.EMAIL_DATA[9:12]) + '\r\n' + + '-----BEGIN PGP SIGNATURE-----\n')), + 'Message does not start with signature header.') + self.assertTrue( + m._message.get_payload().endswith( + '-----END PGP SIGNATURE-----\n'), + 'Message does not end with signature footer.') + # assert signature is valid + pubkey = self._km.get_key(ADDRESS_2, openpgp.OpenPGPKey) + self.assertTrue( + openpgp.verify(m._message.get_payload(), pubkey), + 'Signature could not be verified.') def test_missing_key_rejects_address(self): """ @@ -131,7 +199,7 @@ class TestSmtpRelay(TestCaseWithKeyManager): True. """ # remove key from key manager - pubkey = self._km.get_key('leap@leap.se', openpgp.OpenPGPKey) + pubkey = self._km.get_key(ADDRESS, openpgp.OpenPGPKey) pgp = openpgp.OpenPGPScheme(self._soledad) pgp.delete_key(pubkey) # mock the key fetching @@ -148,7 +216,8 @@ class TestSmtpRelay(TestCaseWithKeyManager): lines = transport.value().rstrip().split('\n') self.assertEqual( '550 Cannot receive for specified address', - lines[-1]) + lines[-1], + 'Address should have been rejecetd with appropriate message.') def test_missing_key_accepts_address(self): """ @@ -156,7 +225,7 @@ class TestSmtpRelay(TestCaseWithKeyManager): False. """ # remove key from key manager - pubkey = self._km.get_key('leap@leap.se', openpgp.OpenPGPKey) + pubkey = self._km.get_key(ADDRESS, openpgp.OpenPGPKey) pgp = openpgp.OpenPGPScheme(self._soledad) pgp.delete_key(pubkey) # mock the key fetching @@ -171,42 +240,9 @@ class TestSmtpRelay(TestCaseWithKeyManager): proto.lineReceived(self.EMAIL_DATA[0] + '\r\n') proto.lineReceived(self.EMAIL_DATA[1] + '\r\n') proto.lineReceived(self.EMAIL_DATA[2] + '\r\n') - # ensure the address was rejected + # ensure the address was accepted lines = transport.value().rstrip().split('\n') self.assertEqual( '250 Recipient address accepted', - lines[-1]) - - def test_malformed_address_rejects(self): - """ - Test if server rejects to send to malformed addresses. - """ - # mock the key fetching - self._km.fetch_keys_from_server = Mock(return_value=[]) - # prepare the SMTP factory - for malformed in ['leap@']: - proto = SMTPFactory( - self._km, self._config).buildProtocol(('127.0.0.1', 0)) - transport = proto_helpers.StringTransport() - proto.makeConnection(transport) - proto.lineReceived(self.EMAIL_DATA[0] + '\r\n') - proto.lineReceived(self.EMAIL_DATA[1] + '\r\n') - proto.lineReceived('RCPT TO: <%s>%s' % (malformed, '\r\n')) - # ensure the address was rejected - lines = transport.value().rstrip().split('\n') - self.assertEqual( - '550 Cannot receive for specified address', - lines[-1]) - - def test_prepare_header_adds_from(self): - """ - Test if message headers are OK. - """ - proto = SMTPFactory( - self._km, self._config).buildProtocol(('127.0.0.1', 0)) - user = User('leap@leap.se', 'relay.leap.se', proto, 'leap@leap.se') - m = EncryptedMessage(user, self._km, self._config) - for line in self.EMAIL_DATA[4:12]: - m.lineReceived(line) - m.eomReceived() - self.assertEqual('<leap@leap.se>', m._message['From']) + lines[-1], + 'Address should have been accepted with appropriate message.') |