/** * The TLS command execution. * @throws IOException If an I/O error occurs while sending * the command or performing the negotiation. * @return TRUE if the command and negotiation succeeded. */ public boolean execTLS() throws IOException { if (!SMTPReply.isPositiveCompletion(sendCommand("STARTTLS"))) { return false; //throw new SSLException(getReplyString()); } performSSLNegotiation(); return true; }
/** * Performs a lazy init of the SSL context. * @throws IOException When could not initialize the SSL context. */ private void initSSLContext() throws IOException { if (context == null) { context = SSLContextUtils.createSSLContext(protocol, getKeyManager(), getTrustManager()); } }
initSSLContext(); String host = (_hostname_ != null) ? _hostname_ : getRemoteAddress().getHostAddress(); int port = getRemotePort(); SSLSocket socket = (SSLSocket) ssf.createSocket(_socket_, host, port, true);
@Test public void startTlsShouldFailWhenFollowedByInjectedCommand() throws Exception { server = createServer(createProtocol(Optional.empty()), Encryption.createStartTls(BogusSslContextFactory.getServerContext())); smtpsClient = createClient(); server.bind(); InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); smtpsClient.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpsClient.sendCommand("EHLO localhost"); smtpsClient.sendCommand("STARTTLS\r\nRSET\r\n"); assertThat(SMTPReply.isPositiveCompletion(smtpsClient.getReplyCode())).isFalse(); }
@Test public void startTlsShouldReturnTrueWhenServerSupportsIt() throws Exception { server = createServer(createProtocol(Optional.empty()), Encryption.createStartTls(BogusSslContextFactory.getServerContext())); smtpsClient = createClient(); server.bind(); InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); smtpsClient.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpsClient.sendCommand("EHLO localhost"); boolean execTLS = smtpsClient.execTLS(); assertThat(execTLS).isTrue(); }
@Test public void startTlsShouldBeAnnouncedWhenServerSupportsIt() throws Exception { server = createServer(createProtocol(Optional.empty()), Encryption.createStartTls(BogusSslContextFactory.getServerContext())); smtpsClient = createClient(); server.bind(); InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); smtpsClient.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpsClient.sendCommand("EHLO localhost"); assertThat(new StartTLSAssert(smtpsClient)).isStartTLSAnnounced(); }
@Override protected SMTPClient createClient() { SMTPSClient client = new SMTPSClient(true,BogusSslContextFactory.getClientContext()); client.setTrustManager(BogusTrustManagerFactory.getTrustManagers()[0]); return client; }
@Test public void connectShouldReturnTrueWhenConnecting() throws Exception { server = createServer(createProtocol(Optional.empty()), Encryption.createStartTls(BogusSslContextFactory.getServerContext())); smtpsClient = createClient(); server.bind(); InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); smtpsClient.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(SMTPReply.isPositiveCompletion(smtpsClient.getReplyCode())).isTrue(); }
@After public void tearDown() throws Exception { if (smtpsClient != null) { smtpsClient.disconnect(); } if (server != null) { server.unbind(); } hashedWheelTimer.stop(); }
@Override public boolean addRecipient(String address) throws IOException { boolean ok = super.addRecipient(address); if (ok) { rcptCount++; } return ok; }
@Test public void startTlsShouldFailWhenFollowedByInjectedCommandAndNotAtBeginningOfLine() throws Exception { server = createServer(createProtocol(Optional.empty()), Encryption.createStartTls(BogusSslContextFactory.getServerContext())); smtpsClient = createClient(); server.bind(); InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); smtpsClient.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpsClient.sendCommand("EHLO localhost"); smtpsClient.sendCommand("RSET\r\nSTARTTLS\r\nRSET\r\n"); assertThat(SMTPReply.isPositiveCompletion(smtpsClient.getReplyCode())).isFalse(); }
@Test public void startTlsShouldReturnTrueWhenServerSupportsIt() throws Exception { server = createServer(createProtocol(Optional.empty()), Encryption.createStartTls(BogusSslContextFactory.getServerContext())); smtpsClient = createClient(); server.bind(); InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); smtpsClient.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpsClient.sendCommand("EHLO localhost"); boolean execTLS = smtpsClient.execTLS(); assertThat(execTLS).isTrue(); }
@Test public void startTlsShouldBeAnnouncedWhenServerSupportsIt() throws Exception { server = createServer(createProtocol(Optional.empty()), Encryption.createStartTls(BogusSslContextFactory.getServerContext())); smtpsClient = createClient(); server.bind(); InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); smtpsClient.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpsClient.sendCommand("EHLO localhost"); assertThat(new StartTLSAssert(smtpsClient)).isStartTLSAnnounced(); }
private SMTPSClient createClient() { SMTPSClient client = new SMTPSClient(false, BogusSslContextFactory.getClientContext()); client.setTrustManager(BogusTrustManagerFactory.getTrustManagers()[0]); return client; }
@Test public void connectShouldReturnTrueWhenConnecting() throws Exception { server = createServer(createProtocol(Optional.empty()), Encryption.createStartTls(BogusSslContextFactory.getServerContext())); smtpsClient = createClient(); server.bind(); InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); smtpsClient.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); assertThat(SMTPReply.isPositiveCompletion(smtpsClient.getReplyCode())).isTrue(); }
@After public void tearDown() throws Exception { if (smtpsClient != null) { smtpsClient.disconnect(); } if (server != null) { server.unbind(); } hashedWheelTimer.stop(); }
@Override public boolean addRecipient(RelayPath path) throws IOException { boolean ok = super.addRecipient(path); if (ok) { rcptCount++; } return ok; }
@Test public void startTlsShouldFailWhenFollowedByInjectedCommandAndNotAtBeginningOfLine() throws Exception { server = createServer(createProtocol(Optional.empty()), Encryption.createStartTls(BogusSslContextFactory.getServerContext())); smtpsClient = createClient(); server.bind(); InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); smtpsClient.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpsClient.sendCommand("EHLO localhost"); smtpsClient.sendCommand("RSET\r\nSTARTTLS\r\nRSET\r\n"); assertThat(SMTPReply.isPositiveCompletion(smtpsClient.getReplyCode())).isFalse(); }
@Override protected SMTPClient createClient() { SMTPSClient client = new SMTPSClient(true,BogusSslContextFactory.getClientContext()); client.setTrustManager(BogusTrustManagerFactory.getTrustManagers()[0]); return client; }
@Test public void startTlsShouldFailWhenFollowedByInjectedCommand() throws Exception { server = createServer(createProtocol(Optional.empty()), Encryption.createStartTls(BogusSslContextFactory.getServerContext())); smtpsClient = createClient(); server.bind(); InetSocketAddress bindedAddress = new ProtocolServerUtils(server).retrieveBindedAddress(); smtpsClient.connect(bindedAddress.getAddress().getHostAddress(), bindedAddress.getPort()); smtpsClient.sendCommand("EHLO localhost"); smtpsClient.sendCommand("STARTTLS\r\nRSET\r\n"); assertThat(SMTPReply.isPositiveCompletion(smtpsClient.getReplyCode())).isFalse(); }