org.apache.sshd.common.cipher.BuiltinCiphers Java Examples

The following examples show how to use org.apache.sshd.common.cipher.BuiltinCiphers. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KeyReExchangeTest.java    From termd with Apache License 2.0 6 votes vote down vote up
@Test
public void testSwitchToNoneCipher() throws Exception {
    setUp(0L, 0L, 0L);

    sshd.getCipherFactories().add(BuiltinCiphers.none);
    try (SshClient client = setupTestClient()) {
        client.getCipherFactories().add(BuiltinCiphers.none);
        client.start();

        try (ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) {
            session.addPasswordIdentity(getCurrentTestName());
            session.auth().verify(5L, TimeUnit.SECONDS);

            outputDebugMessage("Request switch to none cipher for %s", session);
            KeyExchangeFuture switchFuture = session.switchToNoneCipher();
            switchFuture.verify(5L, TimeUnit.SECONDS);
            try (ClientChannel channel = session.createSubsystemChannel(SftpConstants.SFTP_SUBSYSTEM_NAME)) {
                channel.open().verify(5L, TimeUnit.SECONDS);
            }
        } finally {
            client.stop();
        }
    }
}
 
Example #2
Source File: KeyReExchangeTest.java    From termd with Apache License 2.0 6 votes vote down vote up
@Test
public void testSwitchToNoneCipher() throws Exception {
    setUp(0L, 0L, 0L);

    sshd.getCipherFactories().add(BuiltinCiphers.none);
    try (SshClient client = setupTestClient()) {
        client.getCipherFactories().add(BuiltinCiphers.none);
        client.start();

        try (ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) {
            session.addPasswordIdentity(getCurrentTestName());
            session.auth().verify(5L, TimeUnit.SECONDS);

            outputDebugMessage("Request switch to none cipher for %s", session);
            KeyExchangeFuture switchFuture = session.switchToNoneCipher();
            switchFuture.verify(5L, TimeUnit.SECONDS);
            try (ClientChannel channel = session.createSubsystemChannel(SftpConstants.SFTP_SUBSYSTEM_NAME)) {
                channel.open().verify(5L, TimeUnit.SECONDS);
            }
        } finally {
            client.stop();
        }
    }
}
 
Example #3
Source File: KeyReExchangeTest.java    From termd with Apache License 2.0 4 votes vote down vote up
@Test   // see SSHD-558
public void testKexFutureExceptionPropagation() throws Exception {
    setUp(0L, 0L, 0L);
    sshd.getCipherFactories().add(BuiltinCiphers.none);

    try (SshClient client = setupTestClient()) {
        client.getCipherFactories().add(BuiltinCiphers.none);
        // replace the original KEX factories with wrapped ones that we can fail intentionally
        List<NamedFactory<KeyExchange>> kexFactories = new ArrayList<>();
        final AtomicBoolean successfulInit = new AtomicBoolean(true);
        final AtomicBoolean successfulNext = new AtomicBoolean(true);
        final ClassLoader loader = getClass().getClassLoader();
        final Class<?>[] interfaces = {KeyExchange.class};
        for (final NamedFactory<KeyExchange> factory : client.getKeyExchangeFactories()) {
            kexFactories.add(new NamedFactory<KeyExchange>() {
                @Override
                public String getName() {
                    return factory.getName();
                }

                @Override
                public KeyExchange create() {
                    final KeyExchange proxiedInstance = factory.create();
                    return (KeyExchange) Proxy.newProxyInstance(loader, interfaces, new InvocationHandler() {
                        @Override
                        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                            String name = method.getName();
                            if ("init".equals(name) && (!successfulInit.get())) {
                                throw new UnsupportedOperationException("Intentionally failing 'init'");
                            } else if ("next".equals(name) && (!successfulNext.get())) {
                                throw new UnsupportedOperationException("Intentionally failing 'next'");
                            } else {
                                return method.invoke(proxiedInstance, args);
                            }
                        }
                    });
                }
            });
        }
        client.setKeyExchangeFactories(kexFactories);
        client.start();

        try {
            try {
                testKexFutureExceptionPropagation("init", successfulInit, client);
            } finally {
                successfulInit.set(true);
            }

            try {
                testKexFutureExceptionPropagation("next", successfulNext, client);
            } finally {
                successfulNext.set(true);
            }
        } finally {
            client.stop();
        }
    }
}
 
Example #4
Source File: LoadTest.java    From termd with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("checkstyle:nestedtrydepth")
protected void runClient(String msg) throws Exception {
    try (SshClient client = setupTestClient()) {
        PropertyResolverUtils.updateProperty(client, FactoryManager.MAX_PACKET_SIZE, 1024 * 16);
        PropertyResolverUtils.updateProperty(client, FactoryManager.WINDOW_SIZE, 1024 * 8);
        client.setKeyExchangeFactories(Arrays.asList(
                ClientBuilder.DH2KEX.transform(BuiltinDHFactories.dhg1)));
        client.setCipherFactories(Arrays.<NamedFactory<Cipher>>asList(BuiltinCiphers.blowfishcbc));
        client.start();
        try (ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) {
            session.addPasswordIdentity(getCurrentTestName());
            session.auth().verify(5L, TimeUnit.SECONDS);

            try (ByteArrayOutputStream out = new ByteArrayOutputStream();
                 ByteArrayOutputStream err = new ByteArrayOutputStream();
                 ClientChannel channel = session.createChannel(Channel.CHANNEL_SHELL)) {
                channel.setOut(out);
                channel.setErr(err);

                try {
                    channel.open().verify(9L, TimeUnit.SECONDS);
                    try (OutputStream pipedIn = channel.getInvertedIn()) {
                        msg += "\nexit\n";
                        pipedIn.write(msg.getBytes(StandardCharsets.UTF_8));
                        pipedIn.flush();
                    }

                    Collection<ClientChannelEvent> result =
                            channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), TimeUnit.SECONDS.toMillis(15L));
                    assertFalse("Timeout while waiting for channel closure", result.contains(ClientChannelEvent.TIMEOUT));
                } finally {
                    channel.close(false);
                }

                assertArrayEquals("Mismatched message data", msg.getBytes(StandardCharsets.UTF_8), out.toByteArray());
            }
        } finally {
            client.stop();
        }
    }
}
 
Example #5
Source File: KeyReExchangeTest.java    From termd with Apache License 2.0 4 votes vote down vote up
@Test   // see SSHD-558
public void testKexFutureExceptionPropagation() throws Exception {
    setUp(0L, 0L, 0L);
    sshd.getCipherFactories().add(BuiltinCiphers.none);

    try (SshClient client = setupTestClient()) {
        client.getCipherFactories().add(BuiltinCiphers.none);
        // replace the original KEX factories with wrapped ones that we can fail intentionally
        List<NamedFactory<KeyExchange>> kexFactories = new ArrayList<>();
        final AtomicBoolean successfulInit = new AtomicBoolean(true);
        final AtomicBoolean successfulNext = new AtomicBoolean(true);
        final ClassLoader loader = getClass().getClassLoader();
        final Class<?>[] interfaces = {KeyExchange.class};
        for (final NamedFactory<KeyExchange> factory : client.getKeyExchangeFactories()) {
            kexFactories.add(new NamedFactory<KeyExchange>() {
                @Override
                public String getName() {
                    return factory.getName();
                }

                @Override
                public KeyExchange create() {
                    final KeyExchange proxiedInstance = factory.create();
                    return (KeyExchange) Proxy.newProxyInstance(loader, interfaces, new InvocationHandler() {
                        @Override
                        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                            String name = method.getName();
                            if ("init".equals(name) && (!successfulInit.get())) {
                                throw new UnsupportedOperationException("Intentionally failing 'init'");
                            } else if ("next".equals(name) && (!successfulNext.get())) {
                                throw new UnsupportedOperationException("Intentionally failing 'next'");
                            } else {
                                return method.invoke(proxiedInstance, args);
                            }
                        }
                    });
                }
            });
        }
        client.setKeyExchangeFactories(kexFactories);
        client.start();

        try {
            try {
                testKexFutureExceptionPropagation("init", successfulInit, client);
            } finally {
                successfulInit.set(true);
            }

            try {
                testKexFutureExceptionPropagation("next", successfulNext, client);
            } finally {
                successfulNext.set(true);
            }
        } finally {
            client.stop();
        }
    }
}
 
Example #6
Source File: LoadTest.java    From termd with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("checkstyle:nestedtrydepth")
protected void runClient(String msg) throws Exception {
    try (SshClient client = setupTestClient()) {
        PropertyResolverUtils.updateProperty(client, FactoryManager.MAX_PACKET_SIZE, 1024 * 16);
        PropertyResolverUtils.updateProperty(client, FactoryManager.WINDOW_SIZE, 1024 * 8);
        client.setKeyExchangeFactories(Arrays.asList(
                ClientBuilder.DH2KEX.transform(BuiltinDHFactories.dhg1)));
        client.setCipherFactories(Arrays.<NamedFactory<Cipher>>asList(BuiltinCiphers.blowfishcbc));
        client.start();
        try (ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, port).verify(7L, TimeUnit.SECONDS).getSession()) {
            session.addPasswordIdentity(getCurrentTestName());
            session.auth().verify(5L, TimeUnit.SECONDS);

            try (ByteArrayOutputStream out = new ByteArrayOutputStream();
                 ByteArrayOutputStream err = new ByteArrayOutputStream();
                 ClientChannel channel = session.createChannel(Channel.CHANNEL_SHELL)) {
                channel.setOut(out);
                channel.setErr(err);

                try {
                    channel.open().verify(9L, TimeUnit.SECONDS);
                    try (OutputStream pipedIn = channel.getInvertedIn()) {
                        msg += "\nexit\n";
                        pipedIn.write(msg.getBytes(StandardCharsets.UTF_8));
                        pipedIn.flush();
                    }

                    Collection<ClientChannelEvent> result =
                            channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED), TimeUnit.SECONDS.toMillis(15L));
                    assertFalse("Timeout while waiting for channel closure", result.contains(ClientChannelEvent.TIMEOUT));
                } finally {
                    channel.close(false);
                }

                assertArrayEquals("Mismatched message data", msg.getBytes(StandardCharsets.UTF_8), out.toByteArray());
            }
        } finally {
            client.stop();
        }
    }
}