Search in sources :

Example 31 with KeyPair

use of com.jcraft.jsch.KeyPair in project KeyBox by skavanagh.

the class SSHUtil method keyGen.

/**
 * generates system's public/private key par and returns passphrase
 *
 * @return passphrase for system generated key
 */
public static String keyGen(String passphrase) throws IOException, JSchException {
    FileUtils.forceMkdir(new File(KEY_PATH));
    deleteGenSSHKeys();
    if (StringUtils.isEmpty(AppConfig.getProperty(PRIVATE_KEY)) || StringUtils.isEmpty(AppConfig.getProperty(PUBLIC_KEY))) {
        // set key type
        int type = KeyPair.RSA;
        if ("dsa".equals(SSHUtil.KEY_TYPE)) {
            type = KeyPair.DSA;
        } else if ("ecdsa".equals(SSHUtil.KEY_TYPE)) {
            type = KeyPair.ECDSA;
        } else if ("ed448".equals(SSHUtil.KEY_TYPE)) {
            type = KeyPair.ED448;
        } else if ("ed25519".equals(SSHUtil.KEY_TYPE)) {
            type = KeyPair.ED25519;
        }
        String comment = "bastillion@global_key";
        JSch jsch = new JSch();
        KeyPair keyPair = KeyPair.genKeyPair(jsch, type, KEY_LENGTH);
        keyPair.writePrivateKey(PVT_KEY, passphrase.getBytes());
        keyPair.writePublicKey(PUB_KEY, comment);
        System.out.println("Finger print: " + keyPair.getFingerPrint());
        keyPair.dispose();
    }
    return passphrase;
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) JSch(com.jcraft.jsch.JSch) File(java.io.File)

Example 32 with KeyPair

use of com.jcraft.jsch.KeyPair in project cdap by cdapio.

the class DefaultSSHContext method generate.

@Override
public SSHKeyPair generate(String user, int bits) throws KeyException {
    JSch jsch = new JSch();
    try {
        KeyPair keyPair = KeyPair.genKeyPair(jsch, KeyPair.RSA, bits);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        keyPair.writePublicKey(bos, user);
        SSHPublicKey publicKey = new SSHPublicKey(user, new String(bos.toByteArray(), StandardCharsets.UTF_8));
        bos.reset();
        keyPair.writePrivateKey(bos);
        byte[] privateKey = bos.toByteArray();
        return new SSHKeyPair(publicKey, () -> privateKey);
    } catch (JSchException e) {
        throw new KeyException("Failed to generate ssh key pair", e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) SSHKeyPair(io.cdap.cdap.runtime.spi.ssh.SSHKeyPair) KeyPair(com.jcraft.jsch.KeyPair) SSHKeyPair(io.cdap.cdap.runtime.spi.ssh.SSHKeyPair) SSHPublicKey(io.cdap.cdap.runtime.spi.ssh.SSHPublicKey) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JSch(com.jcraft.jsch.JSch) KeyException(java.security.KeyException)

Example 33 with KeyPair

use of com.jcraft.jsch.KeyPair in project wildfly-core by wildfly.

the class RemoteSshGitRepositoryTestCase method prepareTest.

@Before
public void prepareTest() throws Exception {
    remoteRoot = new File("target", "remote").toPath();
    Path repoConfigDir = remoteRoot.resolve("configuration");
    Files.createDirectories(repoConfigDir);
    File baseDir = remoteRoot.toAbsolutePath().toFile();
    Path jbossConfigDir = new File(System.getProperty("jboss.home", System.getenv("JBOSS_HOME"))).toPath().resolve("standalone").resolve("configuration");
    PathUtil.copyRecursively(jbossConfigDir, repoConfigDir, true);
    Path properties = repoConfigDir.resolve("logging.properties");
    if (Files.exists(properties)) {
        Files.delete(properties);
    }
    Path jbossAuthDir = new File(System.getProperty("jboss.home", System.getenv("JBOSS_HOME"))).toPath().resolve("standalone").resolve("tmp").resolve("auth");
    Files.createDirectories(jbossAuthDir);
    File gitDir = new File(baseDir, Constants.DOT_GIT);
    if (!gitDir.exists()) {
        try (Git git = Git.init().setDirectory(baseDir).setInitialBranch(Constants.MASTER).call()) {
            git.add().addFilepattern("configuration").call();
            git.commit().setSign(false).setMessage("Repository initialized").call();
        }
    }
    remoteRepository = new FileRepositoryBuilder().setWorkTree(baseDir).setGitDir(gitDir).setup().build();
    // Generate new key pair for the server
    ByteArrayOutputStream publicHostKey = new ByteArrayOutputStream();
    JSch jsch = new JSch();
    KeyPair hostKeyPair = KeyPair.genKeyPair(jsch, 2, 2048);
    ByteArrayOutputStream hostPrivateKey = new ByteArrayOutputStream();
    hostKeyPair.writePrivateKey(hostPrivateKey);
    hostPrivateKey.flush();
    hostKeyPair.writePublicKey(publicHostKey, "");
    sshServer = new SSHServer(EC_USER, SSH_DIR.resolve(EC_PUBKEY), remoteRepository, // create key pair gen
    hostPrivateKey.toByteArray());
    port = sshServer.start();
    // Add new server to known_hosts
    KNOWN_HOSTS = SSH_DIR.resolve("known_hosts").toFile();
    FileWriter fileWritter = new FileWriter(KNOWN_HOSTS, true);
    String knownHostTemplate = "[%s]:" + port + ' ' + publicHostKey.toString(US_ASCII.name()) + "\n";
    try (BufferedWriter bw = new BufferedWriter(fileWritter)) {
        bw.write(String.format(knownHostTemplate, "127.0.0.1"));
        bw.write(String.format(knownHostTemplate, "localhost"));
        bw.write(String.format(knownHostTemplate, InetAddress.getLocalHost().getHostName()));
        if (System.getenv().containsKey("COMPUTERNAME")) {
            bw.write(String.format(knownHostTemplate, System.getenv().get("COMPUTERNAME")));
        }
    }
}
Also used : Path(java.nio.file.Path) KeyPair(com.jcraft.jsch.KeyPair) Git(org.eclipse.jgit.api.Git) FileWriter(java.io.FileWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JSch(com.jcraft.jsch.JSch) File(java.io.File) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) BufferedWriter(java.io.BufferedWriter) Before(org.junit.Before)

Example 34 with KeyPair

use of com.jcraft.jsch.KeyPair in project heroku.jar by heroku.

the class KeysRequestIntegrationTest method getPublicKey.

public String getPublicKey(String comment) throws JSchException, IOException {
    JSch jsch = new JSch();
    KeyPair keyPair = KeyPair.genKeyPair(jsch, KeyPair.RSA);
    ByteArrayOutputStream publicKeyOutputStream = new ByteArrayOutputStream();
    keyPair.writePublicKey(publicKeyOutputStream, comment);
    publicKeyOutputStream.close();
    return new String(publicKeyOutputStream.toByteArray());
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JSch(com.jcraft.jsch.JSch)

Example 35 with KeyPair

use of com.jcraft.jsch.KeyPair in project che-server by eclipse-che.

the class SshManager method generatePair.

/**
 * Generates and stores ssh pair for specified user.
 *
 * @param owner the id of the user who will be the owner of the ssh pair
 * @param service service name pf ssh pair
 * @param name name of pair
 * @return instance of generated ssh pair
 * @throws ConflictException when given ssh pair cannot be generated or created
 * @throws ServerException when any other error occurs during ssh pair generating or creating
 */
public SshPairImpl generatePair(String owner, String service, String name) throws ServerException, ConflictException {
    KeyPair keyPair;
    try {
        keyPair = KeyPair.genKeyPair(genJSch, 2, 2048);
    } catch (JSchException e) {
        throw new ServerException("Failed to generate ssh pair.", e);
    }
    ByteArrayOutputStream privateBuff = new ByteArrayOutputStream();
    keyPair.writePrivateKey(privateBuff);
    ByteArrayOutputStream publicBuff = new ByteArrayOutputStream();
    keyPair.writePublicKey(publicBuff, null);
    final SshPairImpl generatedSshPair = new SshPairImpl(owner, service, name, publicBuff.toString(), privateBuff.toString());
    sshDao.create(generatedSshPair);
    return generatedSshPair;
}
Also used : JSchException(com.jcraft.jsch.JSchException) KeyPair(com.jcraft.jsch.KeyPair) ServerException(org.eclipse.che.api.core.ServerException) SshPairImpl(org.eclipse.che.api.ssh.server.model.impl.SshPairImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

KeyPair (com.jcraft.jsch.KeyPair)38 JSch (com.jcraft.jsch.JSch)32 ByteArrayOutputStream (java.io.ByteArrayOutputStream)20 JSchException (com.jcraft.jsch.JSchException)17 File (java.io.File)11 IOException (java.io.IOException)10 OutputStream (java.io.OutputStream)5 FileOutputStream (java.io.FileOutputStream)3 Path (java.nio.file.Path)3 ServerException (org.eclipse.che.api.core.ServerException)3 SshPairImpl (org.eclipse.che.api.ssh.server.model.impl.SshPairImpl)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 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