Search in sources :

Example 11 with KeyPair

use of com.jcraft.jsch.KeyPair in project jcabi-github by jcabi.

the class RtDeployKeysITCase method key.

/**
 * Generates a random public key for test.
 *
 * @return The encoded SSH public key.
 * @throws Exception If a problem occurs.
 */
private static String key() throws Exception {
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    try {
        final KeyPair kpair = KeyPair.genKeyPair(new JSch(), KeyPair.DSA);
        kpair.writePublicKey(stream, "");
        kpair.dispose();
    } finally {
        stream.close();
    }
    return new String(stream.toByteArray());
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JSch(com.jcraft.jsch.JSch)

Example 12 with KeyPair

use of com.jcraft.jsch.KeyPair in project AJSC by att.

the class GenerateKeys method generateKeyPair.

public static void generateKeyPair(com.att.cdp.zones.model.KeyPair kp) throws IOException, JSchException, ZoneException {
    KeyPair kpair;
    kpair = KeyPair.genKeyPair(new JSch(), KeyPair.RSA);
    // String publicKeyFilename = PUBLIC_KEY;
    // String privateKeyFilename = PRIVATE_KEY;
    OutputStream os = new ByteArrayOutputStream();
    kpair.writePrivateKey(os);
    String str = "";
    try {
        str = reformatSSHKey(os.toString());
    } catch (Exception e) {
        LOG.error(e.getMessage());
    }
    kp.setPrivateKey(str);
    os = new ByteArrayOutputStream();
    kpair.writePublicKey(os, "");
    kp.setPublicKey(os.toString());
    kpair.getFingerPrint();
    kpair.dispose();
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JSch(com.jcraft.jsch.JSch) ZoneException(com.att.cdp.exceptions.ZoneException) IOException(java.io.IOException) JSchException(com.jcraft.jsch.JSchException)

Example 13 with KeyPair

use of com.jcraft.jsch.KeyPair in project teamcity-git by JetBrains.

the class GitCommandLine method getPrivateKey.

@Nullable
private File getPrivateKey(@NotNull AuthSettings authSettings) throws VcsException {
    File privateKey = null;
    final boolean useSshAskPass = myCtx.isUseSshAskPass();
    try {
        switch(authSettings.getAuthMethod()) {
            case TEAMCITY_SSH_KEY:
                privateKey = getUploadedPrivateKey(authSettings);
                break;
            case PRIVATE_KEY_FILE:
                final String keyPath = authSettings.getPrivateKeyFilePath();
                if (StringUtil.isEmpty(keyPath)) {
                    throw new VcsException("Authentication method is \"" + AuthenticationMethod.PRIVATE_KEY_FILE.uiName() + "\", but no private key path provided");
                }
                final File finalPrivateKey = createTmpKeyFile();
                addPostAction(() -> FileUtil.delete(finalPrivateKey));
                privateKey = finalPrivateKey;
                FileUtil.copy(new File(keyPath), privateKey);
                break;
            case PRIVATE_KEY_DEFAULT:
                // we do not decrypt default ssh keys
                return null;
            default:
                return null;
        }
        final String passphrase = authSettings.getPassphrase();
        if (useSshAskPass) {
            withAskPassScript(passphrase, askPassPath -> {
                addEnvParam("SSH_ASKPASS", askPassPath);
                addEnvParam("DISPLAY", ":0.0");
            });
        } else {
            final KeyPair keyPair = KeyPair.load(new JSch(), privateKey.getAbsolutePath());
            OutputStream out = null;
            try {
                out = new BufferedOutputStream(new FileOutputStream(privateKey));
                if (keyPair.isEncrypted() && !keyPair.decrypt(passphrase)) {
                    throw new VcsException("Wrong SSH key passphrase");
                }
                keyPair.writePrivateKey(out, null);
            } finally {
                FileUtil.close(out);
            }
        }
        // set permissions to 600, without that ssh client rejects the key on *nix
        privateKey.setReadable(false, false);
        privateKey.setReadable(true, true);
        privateKey.setWritable(false, false);
        privateKey.setWritable(true, true);
        return privateKey;
    } catch (Exception e) {
        if (privateKey != null)
            FileUtil.delete(privateKey);
        if (e instanceof VcsException)
            throw (VcsException) e;
        throw new VcsException(e);
    }
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) VcsException(jetbrains.buildServer.vcs.VcsException) LineAwareByteArrayOutputStream(jetbrains.buildServer.LineAwareByteArrayOutputStream) JSch(com.jcraft.jsch.JSch) CheckoutCanceledException(jetbrains.buildServer.buildTriggers.vcs.git.command.errors.CheckoutCanceledException) VcsException(jetbrains.buildServer.vcs.VcsException) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with KeyPair

use of com.jcraft.jsch.KeyPair in project onos by opennetworkinglab.

the class SshkeyExchange method generateKeyPair.

private boolean generateKeyPair() {
    KeyPair kpair;
    StringBuilder command = new StringBuilder().append("chmod 600 ").append(HOME_ENV).append(PRIVATE_KEY);
    try {
        kpair = KeyPair.genKeyPair(new JSch(), KeyPair.RSA, KEY_SIZE);
        kpair.writePrivateKey(HOME_ENV + PRIVATE_KEY);
        kpair.writePublicKey(HOME_ENV + PUBLIC_KEY, USER_ENV);
        Runtime.getRuntime().exec(command.toString());
        kpair.dispose();
    } catch (JSchException | IOException e) {
        log.error("Exception in generateKeyPair", e);
        return false;
    }
    return true;
}
Also used : JSchException(com.jcraft.jsch.JSchException) KeyPair(com.jcraft.jsch.KeyPair) IOException(java.io.IOException) JSch(com.jcraft.jsch.JSch)

Example 15 with KeyPair

use of com.jcraft.jsch.KeyPair in project Netshot by netfishers-onl.

the class Ssh method connect.

/* (non-Javadoc)
	 * @see onl.netfishers.netshot.device.access.Cli#connect()
	 */
@Override
public void connect() throws IOException {
    jsch = new JSch();
    try {
        if (privateKey != null && publicKey != null) {
            KeyPair kpair = KeyPair.load(jsch, privateKey.getBytes(), publicKey.getBytes());
            jsch.addIdentity(new Identity() {

                @Override
                public boolean setPassphrase(byte[] passphrase) throws JSchException {
                    return kpair.decrypt(passphrase);
                }

                @Override
                public boolean isEncrypted() {
                    return kpair.isEncrypted();
                }

                @Override
                public byte[] getSignature(byte[] data) {
                    return kpair.getSignature(data);
                }

                @Override
                public byte[] getSignature(byte[] data, String alg) {
                    return kpair.getSignature(data, alg);
                }

                @Override
                public byte[] getPublicKeyBlob() {
                    return kpair.getPublicKeyBlob();
                }

                @Override
                public String getName() {
                    return "Key";
                }

                @Override
                public String getAlgName() {
                    switch(kpair.getKeyType()) {
                        case KeyPair.RSA:
                            return "ssh-rsa";
                        case KeyPair.DSA:
                            return "ssh-dss";
                        case KeyPair.ERROR:
                            return "ERROR";
                        default:
                            return "UNKNOWN";
                    }
                }

                @Override
                public boolean decrypt() {
                    throw new RuntimeException("Not implemented");
                }

                @Override
                public void clear() {
                    kpair.dispose();
                }
            }, password == null ? null : password.getBytes());
        }
        session = jsch.getSession(username, host.getIp(), port);
        if (privateKey == null || publicKey == null) {
            session.setPassword(password);
        }
        // Disable Strict Key checking
        session.setConfig("StrictHostKeyChecking", "no");
        session.setTimeout(this.receiveTimeout);
        session.connect(this.connectionTimeout);
        channel = session.openChannel("shell");
        this.inStream = channel.getInputStream();
        this.outStream = new PrintStream(channel.getOutputStream());
        channel.connect(this.connectionTimeout);
    } catch (JSchException e) {
        throw new IOException(e.getMessage(), e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) PrintStream(java.io.PrintStream) KeyPair(com.jcraft.jsch.KeyPair) IOException(java.io.IOException) JSch(com.jcraft.jsch.JSch) Identity(com.jcraft.jsch.Identity)

Aggregations

KeyPair (com.jcraft.jsch.KeyPair)30 JSch (com.jcraft.jsch.JSch)27 JSchException (com.jcraft.jsch.JSchException)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 File (java.io.File)10 IOException (java.io.IOException)9 OutputStream (java.io.OutputStream)3 BasicSSHUserPrivateKey (com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey)2 SSHKeyPair (io.cdap.cdap.runtime.spi.ssh.SSHKeyPair)2 SSHPublicKey (io.cdap.cdap.runtime.spi.ssh.SSHPublicKey)2 FileOutputStream (java.io.FileOutputStream)2 KeyException (java.security.KeyException)2 ArrayList (java.util.ArrayList)2 SuppressLint (android.annotation.SuppressLint)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 AlertDialog (android.support.v7.app.AlertDialog)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1