-
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
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,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; | ||
| } | ||
|
Comment on lines
109
to
126
|
||
|
|
||
| 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); | ||
| } | ||
|
Comment on lines
+128
to
+138
|
||
|
|
||
| return baos.toString(); | ||
| public String getPrivateKey() { | ||
| try { | ||
| return CertUtils.privateKeyToPem(keyPair.getPrivate()); | ||
| } catch (Exception e) { | ||
| e.printStackTrace(); | ||
| } | ||
| return null; | ||
| } | ||
|
Comment on lines
+140
to
147
|
||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The constructor catches exceptions and uses printStackTrace without proper logging. Following the codebase convention seen in other utility classes (like CertUtils, RSAHelper, SSHCmdHelper), this class should have a Logger field and use LOGGER.error to log exceptions instead of printStackTrace. Additionally, if the keyPair generation fails, it remains null which could cause NullPointerException in getPublicKey and getPrivateKey methods.