Search in sources :

Example 11 with CertificateValidationException

use of com.fsck.k9.mail.CertificateValidationException in project k-9 by k9mail.

the class ImapConnectionTest method open_authExternalWithoutAuthExternalCapability_shouldThrow.

@Test
public void open_authExternalWithoutAuthExternalCapability_shouldThrow() throws Exception {
    settings.setAuthType(AuthType.EXTERNAL);
    MockImapServer server = new MockImapServer();
    preAuthenticationDialog(server, "AUTH=PLAIN");
    ImapConnection imapConnection = startServerAndCreateImapConnection(server);
    try {
        imapConnection.open();
        fail("Expected exception");
    } catch (CertificateValidationException e) {
        assertEquals(Reason.MissingCapability, e.getReason());
    }
    server.verifyConnectionClosed();
    server.verifyInteractionCompleted();
}
Also used : MockImapServer(com.fsck.k9.mail.store.imap.mockserver.MockImapServer) CertificateValidationException(com.fsck.k9.mail.CertificateValidationException) XOAuth2ChallengeParserTest(com.fsck.k9.mail.XOAuth2ChallengeParserTest) Test(org.junit.Test)

Example 12 with CertificateValidationException

use of com.fsck.k9.mail.CertificateValidationException in project k-9 by k9mail.

the class MessagingControllerTest method sendPendingMessagesSynchronous_withCertificateFailure_shouldNotify.

@Test
public void sendPendingMessagesSynchronous_withCertificateFailure_shouldNotify() throws MessagingException {
    setupAccountWithMessageToSend();
    doThrow(new CertificateValidationException("Test")).when(backend).sendMessage(localMessageToSend1);
    controller.sendPendingMessagesSynchronous(account);
    verify(notificationController).showCertificateErrorNotification(account, false);
}
Also used : CertificateValidationException(com.fsck.k9.mail.CertificateValidationException) K9RobolectricTest(com.fsck.k9.K9RobolectricTest) Test(org.junit.Test)

Example 13 with CertificateValidationException

use of com.fsck.k9.mail.CertificateValidationException in project k-9 by k9mail.

the class Pop3Connection method open.

void open() throws MessagingException {
    try {
        SocketAddress socketAddress = new InetSocketAddress(settings.getHost(), settings.getPort());
        if (settings.getConnectionSecurity() == ConnectionSecurity.SSL_TLS_REQUIRED) {
            socket = trustedSocketFactory.createSocket(null, settings.getHost(), settings.getPort(), settings.getClientCertificateAlias());
        } else {
            socket = new Socket();
        }
        socket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT);
        in = new BufferedInputStream(socket.getInputStream(), 1024);
        out = new BufferedOutputStream(socket.getOutputStream(), 512);
        socket.setSoTimeout(SOCKET_READ_TIMEOUT);
        if (!isOpen()) {
            throw new MessagingException("Unable to connect socket");
        }
        String serverGreeting = executeSimpleCommand(null);
        capabilities = getCapabilities();
        if (settings.getConnectionSecurity() == ConnectionSecurity.STARTTLS_REQUIRED) {
            performStartTlsUpgrade(trustedSocketFactory, settings.getHost(), settings.getPort(), settings.getClientCertificateAlias());
        }
        performAuthentication(settings.getAuthType(), serverGreeting);
    } catch (SSLException e) {
        if (e.getCause() instanceof CertificateException) {
            throw new CertificateValidationException(e.getMessage(), e);
        } else {
            throw new MessagingException("Unable to connect", e);
        }
    } catch (GeneralSecurityException gse) {
        throw new MessagingException("Unable to open connection to POP server due to security error.", gse);
    } catch (IOException ioe) {
        throw new MessagingException("Unable to open connection to POP server.", ioe);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) MessagingException(com.fsck.k9.mail.MessagingException) InetSocketAddress(java.net.InetSocketAddress) GeneralSecurityException(java.security.GeneralSecurityException) CertificateException(java.security.cert.CertificateException) CertificateValidationException(com.fsck.k9.mail.CertificateValidationException) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) BufferedOutputStream(java.io.BufferedOutputStream) SSLException(javax.net.ssl.SSLException) Socket(java.net.Socket)

Example 14 with CertificateValidationException

use of com.fsck.k9.mail.CertificateValidationException in project k-9 by k9mail.

the class Pop3Connection method performStartTlsUpgrade.

/*
     * If STARTTLS is not available throws a CertificateValidationException which in K-9
     * triggers a "Certificate error" notification that takes the user to the incoming
     * server settings for review. This might be needed if the account was configured with an obsolete
     * "STARTTLS (if available)" setting.
     */
private void performStartTlsUpgrade(TrustedSocketFactory trustedSocketFactory, String host, int port, String clientCertificateAlias) throws MessagingException, NoSuchAlgorithmException, KeyManagementException, IOException {
    if (capabilities.stls) {
        executeSimpleCommand(STLS_COMMAND);
        socket = trustedSocketFactory.createSocket(socket, host, port, clientCertificateAlias);
        socket.setSoTimeout(SOCKET_READ_TIMEOUT);
        in = new BufferedInputStream(socket.getInputStream(), 1024);
        out = new BufferedOutputStream(socket.getOutputStream(), 512);
        if (!isOpen()) {
            throw new MessagingException("Unable to connect socket");
        }
        capabilities = getCapabilities();
    } else {
        throw new CertificateValidationException("STARTTLS connection security not available");
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) MessagingException(com.fsck.k9.mail.MessagingException) CertificateValidationException(com.fsck.k9.mail.CertificateValidationException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 15 with CertificateValidationException

use of com.fsck.k9.mail.CertificateValidationException in project k-9 by k9mail.

the class RealImapConnectionTest method open_withStartTlsButWithoutStartTlsCapability_shouldThrow.

@Test
public void open_withStartTlsButWithoutStartTlsCapability_shouldThrow() throws Exception {
    settings.setConnectionSecurity(ConnectionSecurity.STARTTLS_REQUIRED);
    MockImapServer server = new MockImapServer();
    preAuthenticationDialog(server);
    ImapConnection imapConnection = startServerAndCreateImapConnection(server);
    try {
        imapConnection.open();
        fail("Expected exception");
    } catch (CertificateValidationException e) {
        // FIXME: CertificateValidationException seems wrong
        assertEquals("STARTTLS connection security not available", e.getMessage());
    }
    server.verifyConnectionClosed();
    server.verifyInteractionCompleted();
}
Also used : MockImapServer(com.fsck.k9.mail.store.imap.mockserver.MockImapServer) CertificateValidationException(com.fsck.k9.mail.CertificateValidationException) XOAuth2ChallengeParserTest(com.fsck.k9.mail.XOAuth2ChallengeParserTest) Test(org.junit.Test)

Aggregations

CertificateValidationException (com.fsck.k9.mail.CertificateValidationException)15 Test (org.junit.Test)8 XOAuth2ChallengeParserTest (com.fsck.k9.mail.XOAuth2ChallengeParserTest)7 MockImapServer (com.fsck.k9.mail.store.imap.mockserver.MockImapServer)6 MessagingException (com.fsck.k9.mail.MessagingException)5 BufferedInputStream (java.io.BufferedInputStream)3 BufferedOutputStream (java.io.BufferedOutputStream)3 IOException (java.io.IOException)3 SSLException (javax.net.ssl.SSLException)3 InetSocketAddress (java.net.InetSocketAddress)2 Socket (java.net.Socket)2 SocketAddress (java.net.SocketAddress)2 GeneralSecurityException (java.security.GeneralSecurityException)2 SuppressLint (android.annotation.SuppressLint)1 VisibleForTesting (androidx.annotation.VisibleForTesting)1 K9RobolectricTest (com.fsck.k9.K9RobolectricTest)1 Backend (com.fsck.k9.backend.api.Backend)1 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)1 FetchProfile (com.fsck.k9.mail.FetchProfile)1 ServerSettings (com.fsck.k9.mail.ServerSettings)1