-
Notifications
You must be signed in to change notification settings - Fork 1.3k
utils: use CertUtils.generateRandomKeyPair to create SSH keypair #12708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 4.22
Are you sure you want to change the base?
Changes from all commits
a9627b4
aa95bc0
7be5a86
738ec86
f0402ec
a876cfb
9d6ec47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,14 +20,19 @@ | |
| package com.cloud.utils.ssh; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.StringWriter; | ||
| import java.math.BigInteger; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.charset.StandardCharsets; | ||
| 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; | ||
| import org.bouncycastle.openssl.jcajce.JcaPEMWriter; | ||
|
|
||
| public class SSHKeysHelper { | ||
|
|
||
|
|
@@ -45,8 +50,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(); | ||
| } | ||
| } | ||
weizhouapache marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
@@ -105,17 +110,53 @@ public static String getPublicKeyFromKeyMaterial(String keyMaterial) { | |
| } | ||
|
|
||
| public String getPublicKey() { | ||
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
| keyPair.writePublicKey(baos, ""); | ||
| if (keyPair == null || keyPair.getPublic() == null) { | ||
| return null; | ||
| } | ||
| try { | ||
| RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic(); | ||
|
|
||
| ByteArrayOutputStream buffer = new ByteArrayOutputStream(); | ||
|
|
||
| return baos.toString(); | ||
| writeString(buffer, "ssh-rsa"); | ||
| writeBigInt(buffer, rsaPublicKey.getPublicExponent()); | ||
| writeBigInt(buffer, rsaPublicKey.getModulus()); | ||
|
|
||
| String base64 = Base64.encodeBase64String(buffer.toByteArray()); | ||
|
|
||
| return "ssh-rsa " + base64; | ||
| } catch (Exception e) { | ||
weizhouapache marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| e.printStackTrace(); | ||
weizhouapache marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| return null; | ||
| } | ||
weizhouapache marked this conversation as resolved.
Show resolved
Hide resolved
weizhouapache marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
112
to
132
|
||
|
|
||
| public String getPrivateKey() { | ||
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
| keyPair.writePrivateKey(baos); | ||
| private static void writeString(ByteArrayOutputStream out, String str) throws Exception { | ||
| byte[] data = str.getBytes(StandardCharsets.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() { | ||
| if (keyPair == null || keyPair.getPrivate() == null) { | ||
| return null; | ||
| } | ||
| try { | ||
| StringWriter sw = new StringWriter(); | ||
| try (JcaPEMWriter pemWriter = new JcaPEMWriter(sw)) { | ||
| pemWriter.writeObject(keyPair.getPrivate()); | ||
| } | ||
| return sw.toString(); | ||
| } catch (Exception e) { | ||
| e.printStackTrace(); | ||
| } | ||
| return null; | ||
| } | ||
weizhouapache marked this conversation as resolved.
Show resolved
Hide resolved
weizhouapache marked this conversation as resolved.
Show resolved
Hide resolved
weizhouapache marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.