/** * Read key from keyFile. */ public void readFrom(File keyFile) throws IOException, GeneralSecurityException { final String password = isPemEncrypted(keyFile) ? askForPasswd(keyFile.getCanonicalPath()) : null ; privateKeys.add(loadKey(keyFile, password)); }
public static KeyPair loadKey(File f, String passwd) throws IOException, GeneralSecurityException { return loadKey(readPemFile(f), passwd); }
public static int _main(String[] _args) throws Exception { List<String> args = Arrays.asList(_args); PrivateKeyProvider provider = new PrivateKeyProvider(); boolean sshAuthRequestedExplicitly = false; String httpProxy=null; provider.readFrom(f); args = Arrays.asList("help"); // default to help if (tryLoadPKey && !provider.hasKeys()) provider.readFromDefaultLocations(); if (provider.hasKeys()) { try { cli.authenticate(provider.getKeys()); } catch (IllegalStateException e) { if (sshAuthRequestedExplicitly) {
/** * Loads RSA/DSA private key in a PEM format into {@link KeyPair}. */ public static KeyPair loadKey(String pemString, String passwd) throws IOException, GeneralSecurityException { return PrivateKeyProvider.loadKey(pemString, passwd); }
private static boolean isPemEncrypted(File f) throws IOException{ //simple check if the file is encrypted return readPemFile(f).contains("4,ENCRYPTED"); }
/** * Read keys from default keyFiles * * {@code .ssh/id_rsa}, {@code .ssh/id_dsa} and {@code .ssh/identity}. * * @return true if some key was read successfully. */ public boolean readFromDefaultLocations() { final File home = new File(System.getProperty("user.home")); boolean read = false; for (String path : new String[] {".ssh/id_rsa", ".ssh/id_dsa", ".ssh/identity"}) { final File key = new File(home, path); if (!key.exists()) continue; try { readFrom(key); read = true; } catch (IOException e) { LOGGER.log(FINE, "Failed to load " + key, e); } catch (GeneralSecurityException e) { LOGGER.log(FINE, "Failed to load " + key, e); } } return read; }
cf.await(); try (ClientSession session = cf.getSession()) { for (KeyPair pair : provider.getKeys()) { CLI.LOGGER.log(FINE, "Offering {0} private key", pair.getPrivate().getAlgorithm()); session.addPublicKeyIdentity(pair);
/** * Loads RSA/DSA private key in a PEM format into {@link KeyPair}. */ public static KeyPair loadKey(File f, String passwd) throws IOException, GeneralSecurityException { return PrivateKeyProvider.loadKey(f, passwd); }