diff --git a/pom.xml b/pom.xml index b4e2ec57f81b..7767d3500525 100644 --- a/pom.xml +++ b/pom.xml @@ -161,7 +161,6 @@ 5.5.0 2.12.5 2.2.1 - 0.1.55 20231013 1.2 2.7.0 @@ -335,11 +334,6 @@ java-ipv6 ${cs.java-ipv6.version} - - com.jcraft - jsch - ${cs.jsch.version} - com.rabbitmq amqp-client diff --git a/utils/pom.xml b/utils/pom.xml index ee6df9602b8f..92bf145de388 100755 --- a/utils/pom.xml +++ b/utils/pom.xml @@ -78,10 +78,6 @@ org.bouncycastle bctls-jdk15on - - com.jcraft - jsch - org.jasypt jasypt diff --git a/utils/src/main/java/com/cloud/utils/ssh/SSHKeysHelper.java b/utils/src/main/java/com/cloud/utils/ssh/SSHKeysHelper.java index f25881ca09bd..98f02810c49a 100644 --- a/utils/src/main/java/com/cloud/utils/ssh/SSHKeysHelper.java +++ b/utils/src/main/java/com/cloud/utils/ssh/SSHKeysHelper.java @@ -20,15 +20,17 @@ package com.cloud.utils.ssh; import java.io.ByteArrayOutputStream; +import java.math.BigInteger; +import java.nio.ByteBuffer; +import java.security.KeyPair; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.interfaces.RSAPublicKey; +import org.apache.cloudstack.utils.security.CertUtils; import org.apache.commons.codec.binary.Base64; -import com.jcraft.jsch.JSch; -import com.jcraft.jsch.JSchException; -import com.jcraft.jsch.KeyPair; - public class SSHKeysHelper { private KeyPair keyPair; @@ -45,8 +47,8 @@ private static String toHexString(byte[] b) { public SSHKeysHelper(Integer keyLength) { try { - keyPair = KeyPair.genKeyPair(new JSch(), KeyPair.RSA, keyLength); - } catch (JSchException e) { + keyPair = CertUtils.generateRandomKeyPair(keyLength); + } catch (NoSuchAlgorithmException | NoSuchProviderException e) { e.printStackTrace(); } } @@ -105,17 +107,43 @@ public static String getPublicKeyFromKeyMaterial(String keyMaterial) { } public String getPublicKey() { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - keyPair.writePublicKey(baos, ""); + try { + RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic(); + + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + + writeString(buffer,"ssh-rsa"); + writeBigInt(buffer, rsaPublicKey.getPublicExponent()); + writeBigInt(buffer, rsaPublicKey.getModulus()); - return baos.toString(); + String base64 = Base64.encodeBase64String(buffer.toByteArray()); + + return "ssh-rsa " + base64; + } catch (Exception e) { + e.printStackTrace(); + } + return null; } - public String getPrivateKey() { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - keyPair.writePrivateKey(baos); + private static void writeString(ByteArrayOutputStream out, String str) throws Exception { + byte[] data = str.getBytes("UTF-8"); + out.write(ByteBuffer.allocate(4).putInt(data.length).array()); + out.write(data); + } + + private static void writeBigInt(ByteArrayOutputStream out, BigInteger value) throws Exception { + byte[] data = value.toByteArray(); + out.write(ByteBuffer.allocate(4).putInt(data.length).array()); + out.write(data); + } - return baos.toString(); + public String getPrivateKey() { + try { + return CertUtils.privateKeyToPem(keyPair.getPrivate()); + } catch (Exception e) { + e.printStackTrace(); + } + return null; } }