AbstractGeneratorHostKeyProvider hostKeyProvider = new SimpleGeneratorHostKeyProvider(SERVER_KEY_FILE.toPath()); hostKeyProvider.setAlgorithm("RSA"); sshd.setKeyPairProvider(hostKeyProvider);
protected void writeKeyPair(KeyPair kp, Path keyPath, OpenOption... options) throws IOException, GeneralSecurityException { if ((!Files.exists(keyPath)) || isOverwriteAllowed()) { try (OutputStream os = Files.newOutputStream(keyPath, options)) { doWriteKeyPair(keyPath.toString(), kp, os); } catch (Throwable e) { log.warn("writeKeyPair({}) failed ({}) to write key {}: {}", keyPath, e.getClass().getSimpleName(), e.getMessage()); if (log.isDebugEnabled()) { log.debug("writeKeyPair(" + keyPath + ") write failure details", e); } } } else { log.error("Overwriting key ({}) is disabled: using throwaway {}: {}", keyPath, KeyUtils.getKeyType(kp), KeyUtils.getFingerPrint((kp == null) ? null : kp.getPublic())); } }
@Override // co-variant return public synchronized List<KeyPair> loadKeys() { Path keyPath = getPath(); KeyPair kp; synchronized (keyPairHolder) { kp = keyPairHolder.get(); if (kp == null) { try { kp = resolveKeyPair(keyPath); if (kp != null) { keyPairHolder.set(kp); } } catch (Throwable t) { log.warn("loadKeys({}) Failed ({}) to resolve: {}", keyPath, t.getClass().getSimpleName(), t.getMessage()); if (log.isDebugEnabled()) { log.debug("loadKeys(" + keyPath + ") resolution failure details", t); } } } } if (kp == null) { return Collections.emptyList(); } else { return Collections.singletonList(kp); } }
protected KeyPair resolveKeyPair(Path keyPath) throws IOException, GeneralSecurityException { String alg = getAlgorithm(); KeyPair kp; if (keyPath != null) { try { kp = loadFromFile(alg, keyPath); if (kp != null) { return kp; kp = generateKeyPair(alg); if (kp == null) { return null; writeKeyPair(kp, keyPath); } catch (Throwable e) { log.warn("resolveKeyPair({})[{}] Failed ({}) to write {} key: {}",
public static SshServer create() { SshServer sshd = SshServer.setUpDefaultServer(); sshd.setPort(SpashConfig.getInstance().spashListenPort()); AbstractGeneratorHostKeyProvider keyProvider = new SimpleGeneratorHostKeyProvider(new File(SpashConfig.getInstance().spashKeyFileName())); keyProvider.setAlgorithm(SpashConfig.getInstance().spashKeyAlgorithm()); keyProvider.setKeySize(SpashConfig.getInstance().spashKeyLength()); sshd.setKeyPairProvider(keyProvider); List<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<NamedFactory<UserAuth>>(); userAuthFactories.add(new UserAuthPasswordFactory()); sshd.setUserAuthFactories(userAuthFactories); sshd.setPasswordAuthenticator(new PasswordAuthenticator() { @Override public boolean authenticate(String username, String password, ServerSession serverSession) throws PasswordChangeRequiredException { return username!=null && username.length()>0 && username.equals(password); } }); sshd.setShellFactory(new SpashShellFactory()); List<NamedFactory<Command>> namedFactoryList = new ArrayList<>(); namedFactoryList.add(new SftpSubsystemFactory()); sshd.setSubsystemFactories(namedFactoryList); sshd.setCommandFactory(new ScpCommandFactory()); sshd.setFileSystemFactory(new FileSystemFactory() { @Override public FileSystem createFileSystem(Session session) throws IOException { return SpashFileSystem.get().getFileSystem(); } }); return sshd; }
Path keyPath = getPath(); Iterable<KeyPair> ids; synchronized (keyPairHolder) { if (ids == null) { try { ids = resolveKeyPairs(session, keyPath); if (ids != null) { keyPairHolder.set(ids);
public void clearLoadedKeys() { Iterable<KeyPair> ids; synchronized (keyPairHolder) { ids = keyPairHolder.getAndSet(null); } if ((ids != null) & log.isDebugEnabled()) { log.debug("clearLoadedKeys({}) removed keys", getPath()); } }
protected Iterable<KeyPair> resolveKeyPairs(SessionContext session, Path keyPath) throws IOException, GeneralSecurityException { String alg = getAlgorithm(); if (keyPath != null) { try { Iterable<KeyPair> ids = loadFromFile(session, alg, keyPath); KeyPair kp = GenericUtils.head(ids); if (kp != null) { kp = generateKeyPair(alg); if (kp == null) { return null; writeKeyPair(kp, keyPath); } catch (Throwable e) { log.warn("resolveKeyPair({})[{}] Failed ({}) to write {} key: {}",
public void clearLoadedKeys() { KeyPair kp; synchronized (keyPairHolder) { kp = keyPairHolder.getAndSet(null); } if ((kp != null) & log.isDebugEnabled()) { PublicKey key = kp.getPublic(); log.debug("clearLoadedKeys({}) removed key={}-{}", getPath(), KeyUtils.getKeyType(key), KeyUtils.getFingerPrint(key)); } }
protected void setupKeyPair() { final AbstractGeneratorHostKeyProvider provider; if (SecurityUtils.isBouncyCastleRegistered()) { provider = SecurityUtils.createGeneratorHostKeyProvider(new File(HOSTKEY_FILE_PEM).toPath()); } else { provider = new SimpleGeneratorHostKeyProvider(new File(HOSTKEY_FILE_SER)); } provider.setAlgorithm(KeyUtils.RSA_ALGORITHM); sshd.setKeyPairProvider(provider); }
protected void writeKeyPair(KeyPair kp, Path keyPath, OpenOption... options) throws IOException, GeneralSecurityException { if ((!Files.exists(keyPath)) || isOverwriteAllowed()) { PathResource location = new PathResource(keyPath); // The options are for write (!!) try (OutputStream os = Files.newOutputStream(keyPath, options)) { doWriteKeyPair(location, kp, os); } catch (Throwable e) { log.warn("writeKeyPair({}) failed ({}) to write key {}: {}", keyPath, e.getClass().getSimpleName(), e.getMessage()); if (log.isDebugEnabled()) { log.debug("writeKeyPair(" + keyPath + ") write failure details", e); } } } else { log.error("Overwriting key ({}) is disabled: using throwaway {}: {}", keyPath, KeyUtils.getKeyType(kp), KeyUtils.getFingerPrint((kp == null) ? null : kp.getPublic())); } }
keyPairProvider.setAlgorithm(algorithm); } catch (final Exception ignore) { throw new RuntimeException(String.format("Can't use '%s' algorithm for ssh key pair generator.",