Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1280f43c authored by cketti's avatar cketti
Browse files

Fix STARTTLS bug in `SmtpTransport`

parent 0a058028
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -130,15 +130,16 @@ class SmtpTransport(
                if (extensions.containsKey("STARTTLS")) {
                    executeCommand("STARTTLS")

                    this.socket = trustedSocketFactory.createSocket(
                    val tlsSocket = trustedSocketFactory.createSocket(
                        socket,
                        host,
                        port,
                        clientCertificateAlias
                    )
                    inputStream = PeekableInputStream(BufferedInputStream(socket.getInputStream(), 1024))
                    this.socket = tlsSocket
                    inputStream = PeekableInputStream(BufferedInputStream(tlsSocket.getInputStream(), 1024))
                    responseParser = SmtpResponseParser(logger, inputStream!!)
                    outputStream = BufferedOutputStream(socket.getOutputStream(), 1024)
                    outputStream = BufferedOutputStream(tlsSocket.getOutputStream(), 1024)

                    // Now resend the EHLO. Required by RFC2487 Sec. 5.2, and more specifically, Exim.
                    extensions = sendHello(helloName)
+10 −0
Original line number Diff line number Diff line
@@ -68,6 +68,11 @@ public class MockSmtpServer {
        interactions.add(new ExpectedCommand(command));
    }

    public void startTls() {
        checkServerNotRunning();
        interactions.add(new UpgradeToTls());
    }

    public void closeConnection() {
        checkServerNotRunning();
        interactions.add(new CloseConnection());
@@ -212,6 +217,9 @@ public class MockSmtpServer {
        }
    }

    private static class UpgradeToTls implements SmtpInteraction {
    }

    private static class CloseConnection implements SmtpInteraction {
    }

@@ -303,6 +311,8 @@ public class MockSmtpServer {
                readExpectedCommand((ExpectedCommand) interaction);
            } else if (interaction instanceof CannedResponse) {
                writeCannedResponse((CannedResponse) interaction);
            } else if (interaction instanceof UpgradeToTls) {
                upgradeToTls(socket);
            } else if (interaction instanceof CloseConnection) {
                clientSocket.close();
            }
+29 −0
Original line number Diff line number Diff line
@@ -567,6 +567,35 @@ class SmtpTransportTest {
        server.verifyInteractionCompleted()
    }

    @Test
    fun `open() with STARTTLS`() {
        val server = MockSmtpServer().apply {
            output("220 localhost Simple Mail Transfer Service Ready")
            expect("EHLO [127.0.0.1]")
            output("250-localhost Hello 127.0.0.1")
            output("250-STARTTLS")
            output("250 HELP")
            expect("STARTTLS")
            output("220 Ready to start TLS")
            startTls()
            expect("EHLO [127.0.0.1]")
            output("250-localhost Hello 127.0.0.1")
            output("250 AUTH PLAIN LOGIN")
            expect("AUTH PLAIN AHVzZXIAcGFzc3dvcmQ=")
            output("235 2.7.0 Authentication successful")
        }
        val transport = startServerAndCreateSmtpTransport(
            server,
            authenticationType = AuthType.PLAIN,
            connectionSecurity = ConnectionSecurity.STARTTLS_REQUIRED
        )

        transport.open()

        server.verifyConnectionStillOpen()
        server.verifyInteractionCompleted()
    }

    @Test
    fun `sendMessage() without address to send to should not open connection`() {
        val message = MimeMessage()