Search in sources :

Example 1 with PKCS8KeyFile

use of net.schmizz.sshj.userauth.keyprovider.PKCS8KeyFile in project agileway by fangjinuo.

the class SshjConnection method authenticateWithPublicKey.

@Override
public boolean authenticateWithPublicKey(String user, byte[] pemPrivateKey, String passphrase) throws SshException {
    Preconditions.checkNotNull(sshClient);
    String keyContent = new String(pemPrivateKey);
    List<KeyProvider> keyProviders = Collects.emptyArrayList();
    PasswordFinder passwordFinder = null;
    if (Strings.isNotBlank(passphrase)) {
        passwordFinder = PasswordUtils.createOneOff(passphrase.toCharArray());
    }
    try {
        StringReader reader = new StringReader(keyContent);
        PuTTYKeyFile puTTYKeyFile = new PuTTYKeyFile();
        if (passwordFinder == null) {
            puTTYKeyFile.init(reader);
        } else {
            puTTYKeyFile.init(reader, passwordFinder);
        }
        puTTYKeyFile.getPrivate();
        keyProviders.add(puTTYKeyFile);
    } catch (Throwable ex) {
    // ignore it
    }
    try {
        StringReader reader = new StringReader(keyContent);
        PKCS8KeyFile pkcs8KeyFile = new PKCS8KeyFile();
        if (passwordFinder == null) {
            pkcs8KeyFile.init(reader);
        } else {
            pkcs8KeyFile.init(reader, passwordFinder);
        }
        pkcs8KeyFile.getPrivate();
        keyProviders.add(pkcs8KeyFile);
    } catch (Throwable ex) {
    // ignore it
    }
    try {
        StringReader reader = new StringReader(keyContent);
        OpenSSHKeyFile openSSHKeyFile = new OpenSSHKeyFile();
        if (passwordFinder == null) {
            openSSHKeyFile.init(reader);
        } else {
            openSSHKeyFile.init(reader, passwordFinder);
        }
        openSSHKeyFile.getPrivate();
        keyProviders.add(openSSHKeyFile);
    } catch (Throwable ex) {
    // ignore it
    }
    try {
        Preconditions.checkState(!keyProviders.isEmpty(), "the private key is invalid: " + keyContent);
        sshClient.authPublickey(user, keyProviders);
    } catch (UserAuthException ex) {
        logger.error(ex.getMessage(), ex);
        return false;
    } catch (Throwable ex) {
        throw new SshException(ex);
    }
    return false;
}
Also used : KeyProvider(net.schmizz.sshj.userauth.keyprovider.KeyProvider) PasswordFinder(net.schmizz.sshj.userauth.password.PasswordFinder) PuTTYKeyFile(net.schmizz.sshj.userauth.keyprovider.PuTTYKeyFile) OpenSSHKeyFile(net.schmizz.sshj.userauth.keyprovider.OpenSSHKeyFile) StringReader(java.io.StringReader) PKCS8KeyFile(net.schmizz.sshj.userauth.keyprovider.PKCS8KeyFile) SshException(com.jn.agileway.ssh.client.SshException) UserAuthException(net.schmizz.sshj.userauth.UserAuthException)

Aggregations

SshException (com.jn.agileway.ssh.client.SshException)1 StringReader (java.io.StringReader)1 UserAuthException (net.schmizz.sshj.userauth.UserAuthException)1 KeyProvider (net.schmizz.sshj.userauth.keyprovider.KeyProvider)1 OpenSSHKeyFile (net.schmizz.sshj.userauth.keyprovider.OpenSSHKeyFile)1 PKCS8KeyFile (net.schmizz.sshj.userauth.keyprovider.PKCS8KeyFile)1 PuTTYKeyFile (net.schmizz.sshj.userauth.keyprovider.PuTTYKeyFile)1 PasswordFinder (net.schmizz.sshj.userauth.password.PasswordFinder)1