def test_missing_key_accepts_address(self): """ Test if server accepts to send unencrypted when 'encrypted_only' is False. """ # remove key from key manager pubkey = self._km.get_key(ADDRESS, openpgp.OpenPGPKey) pgp = openpgp.OpenPGPScheme(self._soledad) pgp.delete_key(pubkey) # mock the key fetching self._km.fetch_keys_from_server = Mock(return_value=[]) # change the configuration self._config['encrypted_only'] = False # prepare the SMTP factory 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(self.EMAIL_DATA[2] + '\r\n') # ensure the address was accepted lines = transport.value().rstrip().split('\n') self.assertEqual( '250 Recipient address accepted', lines[-1], 'Address should have been accepted with appropriate message.')
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('*****@*****.**', '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_accepts_address(self): """ Test if server accepts to send unencrypted when 'encrypted_only' is False. """ # remove key from key manager pubkey = self._km.get_key(ADDRESS, openpgp.OpenPGPKey) pgp = openpgp.OpenPGPScheme( self._soledad, gpgbinary=self.GPG_BINARY_PATH) pgp.delete_key(pubkey) # mock the key fetching self._km.fetch_keys_from_server = Mock(return_value=[]) # change the configuration self._config['encrypted_only'] = False # prepare the SMTP factory 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(self.EMAIL_DATA[2] + '\r\n') # ensure the address was accepted lines = transport.value().rstrip().split('\n') self.assertEqual( '250 Recipient address accepted', lines[-1], 'Address should have been accepted with appropriate message.')
def setup_smtp_relay(port, keymanager, smtp_host, smtp_port, smtp_username, smtp_password, encrypted_only): """ Setup SMTP relay to run with Twisted. This function sets up the SMTP relay configuration and the Twisted reactor. @param port: The port in which to run the server. @type port: int @param keymanager: A Key Manager from where to get recipients' public keys. @type keymanager: leap.common.keymanager.KeyManager @param smtp_host: The hostname of the remote SMTP server. @type smtp_host: str @param smtp_port: The port of the remote SMTP server. @type smtp_port: int @param smtp_username: The username used to connect to remote SMTP server. @type smtp_username: str @param smtp_password: The password used to connect to remote SMTP server. @type smtp_password: str @param encrypted_only: Whether the SMTP relay should send unencrypted mail or not. @type encrypted_only: bool """ # The configuration for the SMTP relay is a dict with the following # format: # # { # 'host': '<host>', # 'port': <int>, # 'username': '******', # 'password': '******', # 'encrypted_only': <True/False> # } config = { 'host': smtp_host, 'port': smtp_port, 'username': smtp_username, 'password': smtp_password, 'encrypted_only': encrypted_only } # configure the use of this service with twistd factory = SMTPFactory(keymanager, config) reactor.listenTCP(port, factory)
def test_message_encrypt(self): """ Test if message gets encrypted to destination email. """ proto = SMTPFactory( self._km, self._config).buildProtocol(('127.0.0.1', 0)) 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( 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 text differs from plaintext.')
def test_relay_accepts_valid_email(self): """ Test if SMTP server responds correctly for valid interaction. """ SMTP_ANSWERS = ['220 ' + IP_OR_HOST_REGEX + ' NO UCE NO UBE NO RELAY PROBES', '250 ' + IP_OR_HOST_REGEX + ' Hello ' + IP_OR_HOST_REGEX + ', nice to meet you', '250 Sender address accepted', '250 Recipient address accepted', '354 Continue'] proto = SMTPFactory( self._km, self._config).buildProtocol(('127.0.0.1', 0)) transport = proto_helpers.StringTransport() proto.makeConnection(transport) 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]), 'Did not get expected answer from relay.') proto.setTimeout(None)
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.')