Search in sources :

Example 1 with KeyPair

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

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)

Example 2 with KeyPair

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

the class AuthKeysAction method generateUserKey.

/**
	 * generates public private key from passphrase
	 *  
	 * @param username username to set in public key comment
	 * @param keyname keyname to set in public key comment
	 * @return public key
	 */
public String generateUserKey(String username, String keyname) {
    //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;
    }
    JSch jsch = new JSch();
    String pubKey = null;
    try {
        KeyPair keyPair = KeyPair.genKeyPair(jsch, type, SSHUtil.KEY_LENGTH);
        OutputStream os = new ByteArrayOutputStream();
        keyPair.writePrivateKey(os, publicKey.getPassphrase().getBytes());
        //set private key
        servletRequest.getSession().setAttribute(PVT_KEY, EncryptionUtil.encrypt(os.toString()));
        os = new ByteArrayOutputStream();
        keyPair.writePublicKey(os, username + "@" + keyname);
        pubKey = os.toString();
        keyPair.dispose();
    } catch (Exception ex) {
        log.error(ex.toString(), ex);
    }
    return pubKey;
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JSch(com.jcraft.jsch.JSch)

Example 3 with KeyPair

use of com.jcraft.jsch.KeyPair in project MGit by maks.

the class PrivateKeyUtils method getPublicKeyEnsure.

public static File getPublicKeyEnsure(File privateKey) {
    File publicKey = getPublicKey(privateKey);
    if (!publicKey.exists()) {
        try {
            JSch jsch = new JSch();
            KeyPair kpair = KeyPair.load(jsch, privateKey.getAbsolutePath());
            kpair.writePublicKey(new FileOutputStream(publicKey), "mgit");
            kpair.dispose();
        } catch (Exception e) {
            // TODO
            e.printStackTrace();
        }
    }
    return publicKey;
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) FileOutputStream(java.io.FileOutputStream) JSch(com.jcraft.jsch.JSch) File(java.io.File) IOException(java.io.IOException)

Example 4 with KeyPair

use of com.jcraft.jsch.KeyPair in project MGit by maks.

the class SGitSessionFactory method createDefaultJSch.

@Override
protected JSch createDefaultJSch(FS fs) throws JSchException {
    JSch jsch = new JSch();
    PrivateKeyUtils.migratePrivateKeys();
    File sshDir = PrivateKeyUtils.getPrivateKeyFolder();
    for (File file : sshDir.listFiles()) {
        KeyPair kpair = KeyPair.load(jsch, file.getAbsolutePath());
        jsch.addIdentity(file.getAbsolutePath());
    }
    return jsch;
}
Also used : KeyPair(com.jcraft.jsch.KeyPair) JSch(com.jcraft.jsch.JSch) File(java.io.File)

Example 5 with KeyPair

use of com.jcraft.jsch.KeyPair in project airavata by apache.

the class SSHCredentialGenerator method generateCredential.

/**
 * @return a SSH Credential generated and encrypted using a randomly generated password
 * @throws CredentialStoreException
 */
public SSHCredential generateCredential(String tokenId) throws CredentialStoreException {
    JSch jsch = new JSch();
    try {
        KeyPair kpair = KeyPair.genKeyPair(jsch, KeyPair.RSA);
        File file;
        file = File.createTempFile("id_rsa", "");
        String fileName = file.getAbsolutePath();
        String password = generateRandomString();
        // We are encrypting the private key with the hash of (tokenId+password).
        // Any client which wants to use this private key will also generate a hash and then use it to decrypt the key.
        kpair.writePrivateKey(fileName, password.getBytes());
        kpair.writePublicKey(fileName + ".pub", "");
        kpair.dispose();
        byte[] priKey = FileUtils.readFileToByteArray(new File(fileName));
        byte[] pubKey = FileUtils.readFileToByteArray(new File(fileName + ".pub"));
        SSHCredential sshCredential = new SSHCredential();
        sshCredential.setPrivateKey(priKey);
        sshCredential.setPublicKey(pubKey);
        sshCredential.setPassphrase(password);
        return sshCredential;
    } catch (IOException e) {
        logger.error("IO Exception when creating SSH credential ", e);
        throw new CredentialStoreException("Unable to generate SSH Credential", e);
    } catch (JSchException e) {
        logger.error("JSch SSH credential creation exception ", e);
        throw new CredentialStoreException("Unable to generate SSH Credential. JSch exception ", e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) KeyPair(com.jcraft.jsch.KeyPair) IOException(java.io.IOException) CredentialStoreException(org.apache.airavata.credential.store.store.CredentialStoreException) JSch(com.jcraft.jsch.JSch) File(java.io.File)

Aggregations

KeyPair (com.jcraft.jsch.KeyPair)21 JSch (com.jcraft.jsch.JSch)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 JSchException (com.jcraft.jsch.JSchException)8 File (java.io.File)8 IOException (java.io.IOException)7 OutputStream (java.io.OutputStream)3 BasicSSHUserPrivateKey (com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey)2 FileOutputStream (java.io.FileOutputStream)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 EditText (android.widget.EditText)1 ZoneException (com.att.cdp.exceptions.ZoneException)1 CredentialsMatchers (com.cloudbees.plugins.credentials.CredentialsMatchers)1