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());
}
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();
}
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);
}
}
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;
}
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);
}
}
Aggregations