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);
}
}
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));
}
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;
}
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);
}
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);
}
Aggregations