Example usage for org.apache.commons.ssl PKCS8Key getPublicKey

List of usage examples for org.apache.commons.ssl PKCS8Key getPublicKey

Introduction

In this page you can find the example usage for org.apache.commons.ssl PKCS8Key getPublicKey.

Prototype

public PublicKey getPublicKey() throws GeneralSecurityException 

Source Link

Usage

From source file:org.apache.karaf.itests.ssh.SshKeyFormatTest.java

@Test
public void usePemKey() throws Exception {
    SshClient client = SshClient.setUpDefaultClient();
    URL testPemURL = Resources.getResource(SshKeyFormatTest.class, "test.pem");
    ByteSource source = Resources.asByteSource(testPemURL);
    PKCS8Key pkcs8 = new PKCS8Key(source.openStream(), null);

    String sshPort = getSshPort();

    client.setServerKeyVerifier(new RequiredServerKeyVerifier(pkcs8.getPublicKey()));
    client.start();//from   w  ww.  ja v a2  s.c om
    ConnectFuture future = client.connect("karaf", "localhost", Integer.parseInt(sshPort));
    future.await();
    ClientSession session = future.getSession();

    Set<ClientSessionEvent> ret = EnumSet.of(ClientSessionEvent.WAIT_AUTH);
    while (ret.contains(ClientSessionEvent.WAIT_AUTH)) {
        session.addPasswordIdentity("karaf");
        session.auth().verify();
        ret = session.waitFor(
                EnumSet.of(ClientSessionEvent.WAIT_AUTH, ClientSessionEvent.CLOSED, ClientSessionEvent.AUTHED),
                0);
    }
    if (ret.contains(ClientSessionEvent.CLOSED)) {
        throw new Exception("Could not open SSH channel");
    }
    session.close(true);
}

From source file:org.apache.karaf.shell.ssh.keygenerator.OpenSSHGeneratorKeyFileProviderTest.java

@Test
public void convertSimpleKey() throws Exception {
    File temp = File.createTempFile(this.getClass().getCanonicalName(), ".pem");
    temp.deleteOnExit();/*from w w w  .j  a v  a2s .c o m*/

    SimpleGeneratorHostKeyProvider simpleGenerator = new SimpleGeneratorHostKeyProvider(temp);
    simpleGenerator.setKeySize(2048);
    simpleGenerator.setAlgorithm("DSA");
    List<KeyPair> keys = simpleGenerator.loadKeys();
    KeyPair simpleKeyPair = keys.stream().findFirst().get();

    Assert.assertEquals("DSA", simpleKeyPair.getPrivate().getAlgorithm());

    OpenSSHKeyPairProvider provider = new OpenSSHKeyPairProvider(temp, "DSA", 2048);
    KeyPair convertedKeyPair = provider.loadKeys().iterator().next();
    Assert.assertEquals("DSA", convertedKeyPair.getPrivate().getAlgorithm());

    Assert.assertArrayEquals(simpleKeyPair.getPrivate().getEncoded(),
            convertedKeyPair.getPrivate().getEncoded());
    Assert.assertArrayEquals(simpleKeyPair.getPublic().getEncoded(), convertedKeyPair.getPublic().getEncoded());

    //also test that the original file has been replaced
    PKCS8Key pkcs8 = new PKCS8Key(Files.newInputStream(temp.toPath()), null);
    KeyPair keyPair = new KeyPair(pkcs8.getPublicKey(), pkcs8.getPrivateKey());
    Assert.assertArrayEquals(simpleKeyPair.getPrivate().getEncoded(), keyPair.getPrivate().getEncoded());

}

From source file:org.apache.karaf.shell.ssh.keygenerator.OpenSSHKeyPairProvider.java

private KeyPair getKeyPair(FileInputStream is) throws GeneralSecurityException, IOException {
    PKCS8Key pkcs8 = new PKCS8Key(is, password == null ? null : password.toCharArray());
    KeyPair kp = new KeyPair(pkcs8.getPublicKey(), pkcs8.getPrivateKey());
    return kp;//w  w  w  .ja v  a2  s .c o m
}

From source file:org.apache.karaf.shell.ssh.OpenSSHGeneratorFileKeyProvider.java

@Override
protected KeyPair doReadKeyPair(String resourceKey, InputStream is)
        throws IOException, GeneralSecurityException {
    PKCS8Key pkcs8 = new PKCS8Key(is, password == null ? null : password.toCharArray());
    return new KeyPair(pkcs8.getPublicKey(), pkcs8.getPrivateKey());
}

From source file:org.apache.whirr.util.KeyPair.java

public static boolean sameKeyPair(File privateKeyFile, File publicKeyFile) throws IOException {
    try {/*from w w  w. ja v  a 2  s .  c  om*/
        PKCS8Key decodedKey = new PKCS8Key(new FileInputStream(privateKeyFile), null);
        PublicKey publicKey = decodedKey.getPublicKey();

        byte[] actual = encodePublicKey((RSAPublicKey) publicKey);
        byte[] expected = IOUtils.toByteArray(new FileReader(publicKeyFile));

        for (int i = 0; i < actual.length; i += 1) {
            if (actual[i] != expected[i]) {
                return false;
            }
        }
        return true;
    } catch (GeneralSecurityException e) {
        LOG.error("Key pair validation failed", e);
        return false;
    }
}