org.apache.sshd.common.util.SecurityUtils Java Examples

The following examples show how to use org.apache.sshd.common.util.SecurityUtils. 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: Utils.java    From termd with Apache License 2.0 6 votes vote down vote up
public static AbstractFileKeyPairProvider createTestKeyPairProvider(String resource) {
    File file = getFile(resource);
    String filePath = file.getAbsolutePath();
    AbstractFileKeyPairProvider provider = PROVIDERS_MAP.get(filePath);
    if (provider != null) {
        return provider;
    }

    provider = SecurityUtils.createFileKeyPairProvider();
    provider.setFiles(Collections.singletonList(file));
    provider = validateKeyPairProvider(provider);

    AbstractFileKeyPairProvider prev = PROVIDERS_MAP.put(filePath, provider);
    if (prev != null) { // check if somebody else beat us to it
        return prev;
    } else {
        return provider;
    }
}
 
Example #2
Source File: Utils.java    From termd with Apache License 2.0 6 votes vote down vote up
public static AbstractFileKeyPairProvider createTestKeyPairProvider(String resource) {
    File file = getFile(resource);
    String filePath = file.getAbsolutePath();
    AbstractFileKeyPairProvider provider = PROVIDERS_MAP.get(filePath);
    if (provider != null) {
        return provider;
    }

    provider = SecurityUtils.createFileKeyPairProvider();
    provider.setFiles(Collections.singletonList(file));
    provider = validateKeyPairProvider(provider);

    AbstractFileKeyPairProvider prev = PROVIDERS_MAP.put(filePath, provider);
    if (prev != null) { // check if somebody else beat us to it
        return prev;
    } else {
        return provider;
    }
}
 
Example #3
Source File: KeyReExchangeTest.java    From termd with Apache License 2.0 5 votes vote down vote up
@Test
public void testReExchangeFromJschClient() throws Exception {
    Assume.assumeTrue("DH Group Exchange not supported", SecurityUtils.isDHGroupExchangeSupported());
    setUp(0L, 0L, 0L);

    JSch.setConfig("kex", BuiltinDHFactories.Constants.DIFFIE_HELLMAN_GROUP_EXCHANGE_SHA1);
    JSch sch = new JSch();
    com.jcraft.jsch.Session s = sch.getSession(getCurrentTestName(), TEST_LOCALHOST, port);
    try {
        s.setUserInfo(new SimpleUserInfo(getCurrentTestName()));
        s.connect();

        com.jcraft.jsch.Channel c = s.openChannel(Channel.CHANNEL_SHELL);
        c.connect();
        try (OutputStream os = c.getOutputStream();
             InputStream is = c.getInputStream()) {

            String expected = "this is my command\n";
            byte[] bytes = expected.getBytes(StandardCharsets.UTF_8);
            byte[] data = new byte[bytes.length + Long.SIZE];
            for (int i = 1; i <= 10; i++) {
                os.write(bytes);
                os.flush();

                int len = is.read(data);
                String str = new String(data, 0, len);
                assertEquals("Mismatched data at iteration " + i, expected, str);

                outputDebugMessage("Request re-key #%d", i);
                s.rekey();
            }
        } finally {
            c.disconnect();
        }
    } finally {
        s.disconnect();
    }
}
 
Example #4
Source File: Utils.java    From termd with Apache License 2.0 5 votes vote down vote up
public static KeyPair generateKeyPair(String algorithm, int keySize) throws GeneralSecurityException {
    KeyPairGenerator gen = SecurityUtils.getKeyPairGenerator(algorithm);
    if (KeyUtils.EC_ALGORITHM.equalsIgnoreCase(algorithm)) {
        ECCurves curve = ECCurves.fromCurveSize(keySize);
        if (curve == null) {
            throw new InvalidKeySpecException("Unknown curve for key size=" + keySize);
        }
        gen.initialize(curve.getParameters());
    } else {
        gen.initialize(keySize);
    }

    return gen.generateKeyPair();
}
 
Example #5
Source File: KeyReExchangeTest.java    From termd with Apache License 2.0 5 votes vote down vote up
@Test
public void testReExchangeFromJschClient() throws Exception {
    Assume.assumeTrue("DH Group Exchange not supported", SecurityUtils.isDHGroupExchangeSupported());
    setUp(0L, 0L, 0L);

    JSch.setConfig("kex", BuiltinDHFactories.Constants.DIFFIE_HELLMAN_GROUP_EXCHANGE_SHA1);
    JSch sch = new JSch();
    com.jcraft.jsch.Session s = sch.getSession(getCurrentTestName(), TEST_LOCALHOST, port);
    try {
        s.setUserInfo(new SimpleUserInfo(getCurrentTestName()));
        s.connect();

        com.jcraft.jsch.Channel c = s.openChannel(Channel.CHANNEL_SHELL);
        c.connect();
        try (OutputStream os = c.getOutputStream();
             InputStream is = c.getInputStream()) {

            String expected = "this is my command\n";
            byte[] bytes = expected.getBytes(StandardCharsets.UTF_8);
            byte[] data = new byte[bytes.length + Long.SIZE];
            for (int i = 1; i <= 10; i++) {
                os.write(bytes);
                os.flush();

                int len = is.read(data);
                String str = new String(data, 0, len);
                assertEquals("Mismatched data at iteration " + i, expected, str);

                outputDebugMessage("Request re-key #%d", i);
                s.rekey();
            }
        } finally {
            c.disconnect();
        }
    } finally {
        s.disconnect();
    }
}
 
Example #6
Source File: Utils.java    From termd with Apache License 2.0 5 votes vote down vote up
public static KeyPair generateKeyPair(String algorithm, int keySize) throws GeneralSecurityException {
    KeyPairGenerator gen = SecurityUtils.getKeyPairGenerator(algorithm);
    if (KeyUtils.EC_ALGORITHM.equalsIgnoreCase(algorithm)) {
        ECCurves curve = ECCurves.fromCurveSize(keySize);
        if (curve == null) {
            throw new InvalidKeySpecException("Unknown curve for key size=" + keySize);
        }
        gen.initialize(curve.getParameters());
    } else {
        gen.initialize(keySize);
    }

    return gen.generateKeyPair();
}
 
Example #7
Source File: Utils.java    From termd with Apache License 2.0 4 votes vote down vote up
public static Random getRandomizerInstance() {
    Factory<Random> factory = SecurityUtils.getRandomFactory();
    return factory.create();
}
 
Example #8
Source File: Utils.java    From termd with Apache License 2.0 4 votes vote down vote up
public static Random getRandomizerInstance() {
    Factory<Random> factory = SecurityUtils.getRandomFactory();
    return factory.create();
}