Search in sources :

Example 1 with HostKey

use of com.jcraft.jsch.HostKey in project gerrit by GerritCodeReview.

the class ConvertKey method main.

public static void main(String[] args) throws GeneralSecurityException, JSchException, IOException {
    SimpleGeneratorHostKeyProvider p;
    if (args.length != 1) {
        System.err.println("Error: requires path to the SSH host key");
        return;
    } else {
        File file = new File(args[0]);
        if (!file.exists() || !file.isFile() || !file.canRead()) {
            System.err.println("Error: ssh key should exist and be readable");
            return;
        }
    }
    p = new SimpleGeneratorHostKeyProvider();
    // Gerrit's SSH "simple" keys are always RSA.
    p.setPath(args[0]);
    p.setAlgorithm("RSA");
    // forces the key to generate.
    Iterable<KeyPair> keys = p.loadKeys();
    for (KeyPair k : keys) {
        System.out.println("Public Key (" + k.getPublic().getAlgorithm() + "):");
        // From Gerrit's SshDaemon class; use JSch to get the public
        // key/type
        final Buffer buf = new Buffer();
        buf.putRawPublicKey(k.getPublic());
        final byte[] keyBin = buf.getCompactData();
        HostKey pub = new HostKey("localhost", keyBin);
        System.out.println(pub.getType() + " " + pub.getKey());
        System.out.println("Private Key:");
        // Use Bouncy Castle to write the private key back in PEM format
        // (PKCS#1)
        // http://stackoverflow.com/questions/25129822/export-rsa-public-key-to-pem-string-using-java
        StringWriter privout = new StringWriter();
        JcaPEMWriter privWriter = new JcaPEMWriter(privout);
        privWriter.writeObject(k.getPrivate());
        privWriter.close();
        System.out.println(privout);
    }
}
Also used : SimpleGeneratorHostKeyProvider(org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider) Buffer(org.apache.sshd.common.util.Buffer) KeyPair(java.security.KeyPair) HostKey(com.jcraft.jsch.HostKey) StringWriter(java.io.StringWriter) JcaPEMWriter(org.bouncycastle.openssl.jcajce.JcaPEMWriter) File(java.io.File)

Example 2 with HostKey

use of com.jcraft.jsch.HostKey in project sw360portal by sw360.

the class FossologyHostKeyRepositoryTest method testCheckAValidKnownTrustedKeyIsTrusted.

@Test
public void testCheckAValidKnownTrustedKeyIsTrusted() throws Exception {
    String host = "host";
    byte[] key = GOOD_KEY.getBytes();
    final String expectedFingerPrint = new HostKey(host, key).getFingerPrint(jSch);
    assertThat(expectedFingerPrint, not(isEmptyOrNullString()));
    when(connector.getAll()).thenReturn(ImmutableList.of(new FossologyHostFingerPrint().setFingerPrint(expectedFingerPrint).setTrusted(true)));
    assertThat(repo.check(host, key), is(HostKeyRepository.OK));
    verify(connector).getAll();
    verify(connector, never()).add(any(FossologyHostFingerPrint.class));
}
Also used : HostKey(com.jcraft.jsch.HostKey) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) FossologyHostFingerPrint(org.eclipse.sw360.datahandler.thrift.fossology.FossologyHostFingerPrint) Test(org.junit.Test)

Example 3 with HostKey

use of com.jcraft.jsch.HostKey in project sw360portal by sw360.

the class FossologyHostKeyRepository method check.

@Override
public int check(String host, byte[] key) {
    String fingerPrint;
    try {
        fingerPrint = new HostKey(host, key).getFingerPrint(J_SHC);
        for (FossologyHostFingerPrint savedFingerPrint : hostKeyDb.getAll()) {
            if (fingerPrint.equals(savedFingerPrint.getFingerPrint())) {
                if (savedFingerPrint.isTrusted()) {
                    return OK;
                } else {
                    log.error("attempting connection to untrusted Host");
                    return NOT_INCLUDED;
                }
            }
        }
    } catch (Exception e) {
        log.error(format("exception while verifying host '%s'", host), e);
        return NOT_INCLUDED;
    }
    log.error(format("cannot verify host '%s', fingerprint = '%s'", host, fingerPrint));
    final FossologyHostFingerPrint newFossologyHostFingerPrint = new FossologyHostFingerPrint().setFingerPrint(fingerPrint).setTrusted(false);
    hostKeyDb.add(newFossologyHostFingerPrint);
    return NOT_INCLUDED;
}
Also used : HostKey(com.jcraft.jsch.HostKey) FossologyHostFingerPrint(org.eclipse.sw360.datahandler.thrift.fossology.FossologyHostFingerPrint)

Example 4 with HostKey

use of com.jcraft.jsch.HostKey in project ats-framework by Axway.

the class SftpClient method addPublicKeyToHostKeyRepostitory.

private void addPublicKeyToHostKeyRepostitory(PublicKey key, HostKeyRepository hostKeyRepository) throws Exception {
    if (!key.getAlgorithm().contains("RSA")) {
        throw new Exception("Only RSA keys are supported!.");
    }
    byte[] opensshKeyContent = convertToOpenSSHKeyFormat((RSAPublicKey) key);
    HostKey hostkey = new HostKey(hostname, HostKey.SSHRSA, opensshKeyContent);
    hostKeyRepository.add(hostkey, null);
}
Also used : HostKey(com.jcraft.jsch.HostKey) SftpException(com.jcraft.jsch.SftpException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) JSchException(com.jcraft.jsch.JSchException)

Example 5 with HostKey

use of com.jcraft.jsch.HostKey in project gerrit by GerritCodeReview.

the class SystemInfoServiceImpl method daemonHostKeys.

@Override
public void daemonHostKeys(final AsyncCallback<List<SshHostKey>> callback) {
    final ArrayList<SshHostKey> r = new ArrayList<>(hostKeys.size());
    for (final HostKey hk : hostKeys) {
        String host = hk.getHost();
        if (host.startsWith("*:")) {
            final String port = host.substring(2);
            host = "[" + httpRequest.get().getServerName() + "]:" + port;
        }
        final String fp = hk.getFingerPrint(JSCH);
        r.add(new SshHostKey(host, hk.getType() + " " + hk.getKey(), fp));
    }
    callback.onSuccess(r);
}
Also used : HostKey(com.jcraft.jsch.HostKey) SshHostKey(com.google.gerrit.common.data.SshHostKey) SshHostKey(com.google.gerrit.common.data.SshHostKey) ArrayList(java.util.ArrayList)

Aggregations

HostKey (com.jcraft.jsch.HostKey)10 Test (org.junit.Test)5 FossologyHostFingerPrint (org.eclipse.sw360.datahandler.thrift.fossology.FossologyHostFingerPrint)4 Matchers.isEmptyOrNullString (org.hamcrest.Matchers.isEmptyOrNullString)3 JSchException (com.jcraft.jsch.JSchException)2 FileTransferException (com.axway.ats.common.filetransfer.FileTransferException)1 SshHostKey (com.google.gerrit.common.data.SshHostKey)1 SftpException (com.jcraft.jsch.SftpException)1 UserInfo (com.jcraft.jsch.UserInfo)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 KeyPair (java.security.KeyPair)1 ArrayList (java.util.ArrayList)1 Buffer (org.apache.sshd.common.util.Buffer)1 SimpleGeneratorHostKeyProvider (org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider)1 JcaPEMWriter (org.bouncycastle.openssl.jcajce.JcaPEMWriter)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1