private List<HostKey> computeHostKeys() { if (listen.isEmpty()) { return Collections.emptyList(); } final List<PublicKey> keys = myHostKeys(); final List<HostKey> r = new ArrayList<>(); for (PublicKey pub : keys) { final Buffer buf = new ByteArrayBuffer(); buf.putRawPublicKey(pub); final byte[] keyBin = buf.getCompactData(); for (String addr : advertised) { try { r.add(new HostKey(addr, keyBin)); } catch (JSchException e) { sshDaemonLog.warn( String.format( "Cannot format SSHD host key [%s]: %s", pub.getAlgorithm(), e.getMessage())); } } } return Collections.unmodifiableList(r); }
@Override public Session connect(final UserDetails userDetails, boolean verifyHostKey) throws Throwable { _jsch.setKnownHosts(new ByteArrayInputStream(new byte[8192])); String hostKey = _serverDetails.hostKey(); if (hostKey != null) { _jsch.getHostKeyRepository().add(new HostKey(_serverDetails.host(), Base64.decode(hostKey.getBytes())), JSchUserInfo.makeUserInfo(userDetails)); } /* * added user's identity (private & public keys) */ if (userDetails.privateKey() != null) { _jsch.addIdentity(userDetails.username() + "_identity", userDetails.privateKey().getBytes(), userDetails .publicKey().getBytes(), userDetails.passphrase() != null ? userDetails.passphrase().getBytes() : null); } JSchSession session = new JSchSession(userDetails, this, verifyHostKey); session.open(); return session; }
public JschConnection(ConnectionDetails cxnDetails, int maxChannels, boolean verbose) throws Throwable { _cxnDetails = cxnDetails; _maxChannels = maxChannels; _verbose = verbose; _channels = Collections.synchronizedCollection(new HashSet<com.jcraft.jsch.Channel>()); _jsch = new JSch(); _jsch.setKnownHosts(new ByteArrayInputStream(new byte[8192])); String hostKey = _cxnDetails.hostKey(); if (hostKey != null) { _jsch.getHostKeyRepository() .add(new HostKey(_cxnDetails.host(), Base64.getDecoder().decode(hostKey.getBytes())), userInfo()); } /* * added user's identity (private & public keys) */ if (_cxnDetails.privateKey() != null) { _jsch.addIdentity(_cxnDetails.username() + "_identity", _cxnDetails.privateKey().getBytes(), _cxnDetails.publicKey() != null ? _cxnDetails.publicKey().getBytes() : null, _cxnDetails.passphrase() != null ? _cxnDetails.passphrase().getBytes() : null); } /* * connect */ _jschSession = _jsch.getSession(_cxnDetails.username(), _cxnDetails.host(), _cxnDetails.port()); _jschSession.setConfig("StrictHostKeyChecking", _cxnDetails.hostKey() != null ? "yes" : "no"); _jschSession.setUserInfo(userInfo()); if (_verbose) { System.out.print("opening connection to " + _cxnDetails.host() + ":" + _cxnDetails.port() + " ..."); } _jschSession.connect(); if (_verbose) { System.out.println("done"); } }
@Override protected Session createSession(Host hc, String user, String host, int port, FS fs) throws JSchException { if (sshKeysByHostname.containsKey(host)) { SshUri sshUriProperties = sshKeysByHostname.get(host); jSch.addIdentity(host, sshUriProperties.getPrivateKey().getBytes(), null, null); if (sshUriProperties.getKnownHostsFile() != null) { jSch.setKnownHosts(sshUriProperties.getKnownHostsFile()); } if (sshUriProperties.getHostKey() != null) { HostKey hostkey = new HostKey(host, Base64.decode(sshUriProperties.getHostKey())); jSch.getHostKeyRepository().add(hostkey, null); } return jSch.getSession(user, host, port); } throw new JSchException("no keys configured for hostname " + host); }
public HostKey[] getHostKey(String host, String type) { HostKey[] arrayOfHostKey = new HostKey[0]; List<HostKeys> keyList = dbInstance.getHostKey(host); try { if(keyList.size() > 0) { Iterator localIterator = keyList.iterator(); while (localIterator.hasNext()) { HostKeys localHost = (HostKeys)localIterator.next(); arrayOfHostKey[arrayOfHostKey.length] = new HostKey(localHost.getHostName(), localHost.getKey().getBytes()); } } } catch(Exception e) { } return arrayOfHostKey; }
@Test public void testGetType() throws Exception { assertEquals(HostKey.SSHRSA, HostKeyType.SSH_RSA.getType()); assertEquals(HostKey.SSHDSS, HostKeyType.SSH_DSS.getType()); for (HostKeyType hostKeyType : HostKeyType.values()) { assertTrue(hostKeyType.getType() > 0); } }
/** * */ @Test public void createDefaultJSch_WithKnownHosts() throws Exception { final SshAgentSessionFactory factory = new SshAgentSessionFactory(log, KNOWN_HOSTS, null, null); factory.setKnownHosts(getFile(KNOWN_HOSTS)); final JSch jsch = factory.createDefaultJSch(fs); final HostKey[] keys = jsch.getHostKeyRepository().getHostKey("github.com", "ssh-rsa"); assertEquals(1, keys.length); }
@Test public void createDefaultJSch_WithKnownHosts() throws Exception { final SshAgentSessionFactory factory = new SshAgentSessionFactory(log, KNOWN_HOSTS, null, null); factory.setKnownHosts(getFile(KNOWN_HOSTS)); final JSch jsch = factory.createDefaultJSch(fs); final HostKey[] keys = jsch.getHostKeyRepository().getHostKey("github.com", "ssh-rsa"); assertEquals(1, keys.length); }
@Override public void daemonHostKeys(AsyncCallback<List<SshHostKey>> callback) { final ArrayList<SshHostKey> r = new ArrayList<>(hostKeys.size()); for (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); }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws IOException { final List<HostKey> hostKeys = sshd.getHostKeys(); final String out; if (!hostKeys.isEmpty()) { String host = hostKeys.get(0).getHost(); String port = "22"; if (host.contains(":")) { final int p = host.lastIndexOf(':'); port = host.substring(p + 1); host = host.substring(0, p); } if (host.equals("*")) { host = req.getServerName(); } else if (host.startsWith("[") && host.endsWith("]")) { host = host.substring(1, host.length() - 1); } out = host + " " + port; } else { out = "NOT_AVAILABLE"; } CacheHeaders.setNotCacheable(rsp); rsp.setCharacterEncoding(UTF_8.name()); rsp.setContentType("text/plain"); try (PrintWriter w = rsp.getWriter()) { w.write(out); } }
private void updateFingerPrint() { if (txtHost.getText() != null && txtHostKey.getText() != null && txtHostKey.getText().length() != 0) { byte[] keybytes = Util.base64Decode(txtHostKey.getText()); String fg = "<" + RM.getLabel("sftpedition.invalid.key.label") + ">"; if (keybytes != null) { try { HostKey key = new HostKey(txtHost.getText(), keybytes); fg = key.getFingerPrint(new JSch()); } catch (JSchException ignored) { } } lblFingerprint.setText(RM.getLabel("sftpedition.fg.label") + " " + fg); } }
/** * Enforce server reconnection (closes the current connection if it is still alive) */ public synchronized void connect() throws SFTPConnectionException { //checkLocked(); try { // Try to disconnect this.disconnect(); // Open new connection Logger.defaultLogger().info("Trying to connect to server : " + this.remoteServer + " ..."); debug("connect : connect", remoteServer); JSch jsch = new JSch(); if (checkHostKey) { HostKeyRepository hkr = jsch.getHostKeyRepository(); byte[] keybytes = this.getHostKeyAsByteArray(); if (keybytes == null) { throw new SFTPConnectionException("Incorrect hostkey : " + this.getHostKeyAsString()); } HostKey key = new HostKey(remoteServer, keybytes); hkr.add(key, null); jsch.setHostKeyRepository(hkr); } if (useCertificateAuth) { if (certificateFileName == null || certificateFileName.trim().length() == 0 || ! FileSystemManager.exists(new File(certificateFileName))) { throw new SFTPConnectionException("Certificate file not set or not found : " + certificateFileName); } else { Logger.defaultLogger().info("Using private key file : " + certificateFileName); if (certificateFileName.toLowerCase().endsWith(".ppk")) { Logger.defaultLogger().warn("Your private key file seems to be in PuTTY's \"ppk\" file format. Please convert it to the standard OpenSSH format (this can be done by using the \"puttygen.exe\" utility - see \"Convertions\" menu.)"); } jsch.addIdentity(certificateFileName); } } session = jsch.getSession(login, remoteServer, remotePort); UserInfo ui = new DefaultUserInfo(this.password, certPassPhrase, certificateFileName); session.setUserInfo(ui); session.setDaemonThread(true); session.setConfig("StrictHostKeyChecking", checkHostKey ? "yes":"no"); String preferredAuth; String configuredPAuth = FrameworkConfiguration.getInstance().getSftpPreferredAuthOverride(); if (configuredPAuth != null && configuredPAuth.trim().length() != 0) { preferredAuth = configuredPAuth; } else { preferredAuth = useCertificateAuth ? "publickey,password" : "password,publickey"; } Logger.defaultLogger().fine("Authentication methods: " + preferredAuth); session.setConfig("PreferredAuthentications", preferredAuth); session.setTimeout(FrameworkConfiguration.getInstance().getSFTPTimeout()); Logger.defaultLogger().info("Trying to log in with user : " + this.login + " (" + (useCertificateAuth ? "certificate":"password") + ") ..."); debug("connect : login", login); session.connect(); client = (ChannelSftp)session.openChannel("sftp"); client.connect(); this.connectionId = Util.getRndLong(); this.updateOpTime(); Logger.defaultLogger().info("Connected to server : " + this.remoteServer); } catch (JSchException e) { resetClient(e); throw new SFTPConnectionException("Unable to connect to server : " + this.remoteServer + " (" + e.getMessage() + ")"); } finally { clearCache(); } }
public int check(String host, byte[] key) { try { List<HostKeys> keyList = dbInstance.getHostKey(host); if(keyList.size() > 0) { Iterator localIterator = keyList.iterator(); while (localIterator.hasNext()) { HostKeys localHost = (HostKeys)localIterator.next(); HostKey JcHost = new HostKey(host, key); boolean res = JcHost.getKey().equals(localHost.getKey()); if(res == true) { return 0; } else { return 2; } } } } catch(Exception e) { return 1; } return 1; }
@Override public void add(HostKey hostkey, UserInfo ui) {}
@Override public HostKey[] getHostKey() { throw new UnsupportedOperationException(); }
@Override public HostKey[] getHostKey(String host, String type) { // TODO(trustin): Store the hostkeys in the meta repository. return EMPTY_HOST_KEYS; }
@Override public void add(HostKey hostkey, com.jcraft.jsch.UserInfo ui) { //No Op implementation }
@Override public HostKey[] getHostKey() { return new HostKey[0]; }
@Override public HostKey[] getHostKey(String host, String type) { return new HostKey[0]; }
@Override public void add(HostKey hostkey, UserInfo ui) { // nothing to do }
@Override public List<HostKey> getHostKeys() { return hostKeys; }
@Override public List<HostKey> getHostKeys() { return Collections.emptyList(); }
@Override public void add(final HostKey hostkey, final UserInfo info) { // do nothing }
@Override public HostKey[] getHostKey(final String host, final String type) { return new HostKey[0]; }
public void add(HostKey hostkey, UserInfo ui) { }
public HostKey[] getHostKey() { return new HostKey[0]; }
public HostKey[] getHostKey(String host, String type) { return new HostKey[0]; }
@Override public void add(HostKey hostkey, UserInfo ui) { }
@Override public HostKey[] getHostKey() { return EMPTY; }
@Override public HostKey[] getHostKey(String host, String type) { return EMPTY; }
/** * Does nothing. {@inheritDoc} */ @Override public void add(HostKey hostkey, UserInfo ui) { }
public void add(HostKey hostkey, UserInfo ui) { HostKeys host = new HostKeys(hostkey.getHost(), hostkey.getFingerPrint(parameter), hostkey.getKey(), hostkey.getType()); dbInstance.addHostKey(host); }
public HostKey[] getHostKey() { Log.d(log, "getHostKey"); return null; }
List<HostKey> getHostKeys();