refactor(core): extract the OpenSSH public key wire form from Ec2KeyMaterial (#5036)

Moves the parts of Ec2KeyMaterial that are key-format handling rather than
anything EC2 owns into core.common.SshPublicKeys: decoding an
"ssh-rsa AAAA..." line into its length-prefixed fields, reading the RSA key
they carry, writing that form back, and the colon-hex MD5 helper. The
field-length bound that keeps an attacker-declared length from being
allocated moves with it, comment included.

No behaviour change. EC2 keeps its own semantics where they are its own: the
DER-based ssh-rsa fingerprint, the ed25519 branch, and the SHA-1 private-key
fingerprint for a generated pair. The shared digest helper is named for what
it computes rather than for a fingerprint, because EC2 hashes the DER while
OpenSSH hashes the blob and both produce sixteen bytes of colon-hex, so a
function named for one would invite the other's caller to reuse it wrongly.

Step 1 of 2: IAM's SSH public key operations need the same parse, and the
PEM conversions they also need land with that code rather than uncalled here.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-03 21:05:31 -05:00
1 parent 8776c5fc3a
commit 8d5cd2c6e7
4 files changed
+310 -78

No files matched your search

@@ -0,0 +1,17 @@
package io.github.hectorvent.floci.core.common;
/**
* Public key material that could not be read. Thrown by {@link SshPublicKeys} so that each caller
* maps it onto whatever its own API calls the failure, rather than the parser deciding: EC2
* answers an unreadable imported key with no fingerprint at all.
*/
public class SshPublicKeyException extends RuntimeException {
public SshPublicKeyException(String message) {
super(message);
}
public SshPublicKeyException(String message, Throwable cause) {
super(message, cause);
}
}
@@ -0,0 +1,149 @@
package io.github.hectorvent.floci.core.common;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPublicKeySpec;
import java.util.Base64;
import java.util.HexFormat;
/**
* The OpenSSH public key wire form, {@code "ssh-rsa AAAA..."}: decoding a line into its
* length-prefixed fields, and reading or writing the RSA key they carry.
*
* <p>Extracted from {@code Ec2KeyMaterial} rather than left there because it is key-format
* handling rather than anything EC2 owns. The digest helper is here for the same reason, and
* deliberately says only what it computes: EC2 documents its {@code ssh-rsa} fingerprint as the
* MD5 of the DER, which is not the same value as the MD5 of the blob that OpenSSH reports, so a
* function named for one of them would invite the other's caller to reuse it wrongly.
*/
public final class SshPublicKeys {
/**
* Absolute ceiling on a single field, well clear of anything a real key carries: the largest
* is the modulus of a 16384-bit RSA key, 2049 bytes with its sign byte.
*/
private static final int MAX_FIELD_LENGTH = 64 * 1024;
private SshPublicKeys() {}
/**
* The base64 blob from an {@code "ssh-rsa AAAA... comment"} line, or {@code null} when the
* value is not an OpenSSH public key line at all.
*/
public static byte[] decodeBlob(String openSshPublicKey) {
if (openSshPublicKey == null) {
return null;
}
// "ssh-rsa AAAAB3Nza... comment": the middle field is the blob.
String[] fields = openSshPublicKey.trim().split("\\s+");
if (fields.length < 2) {
return null;
}
try {
return Base64.getDecoder().decode(fields[1]);
} catch (IllegalArgumentException e) {
return null;
}
}
/**
* A reader over an OpenSSH blob's length-prefixed fields, which reads them on demand.
*
* <p>On demand rather than all at once, because the number of fields is as caller-controlled
* as their sizes: a blob of four-byte zero-length fields declares as many as it has room for,
* and eagerly materialising them would allocate once per field however few the caller needs.
* A reader that stops where its caller stops is bounded by what that caller asks for.
*/
public static Reader reader(byte[] blob) {
if (blob == null) {
throw new SshPublicKeyException("no OpenSSH key blob");
}
return new Reader(blob);
}
/** Reads the length-prefixed fields of an OpenSSH public key blob, one at a time. */
public static final class Reader {
private final ByteBuffer buffer;
private Reader(byte[] blob) {
this.buffer = ByteBuffer.wrap(blob);
}
/**
* The next field.
*
* @throws SshPublicKeyException when the blob is truncated or declares an impossible length
*/
public byte[] readField() {
if (buffer.remaining() < Integer.BYTES) {
throw new SshPublicKeyException("truncated OpenSSH key blob");
}
int length = buffer.getInt();
// The blob comes straight from a caller's request, so the declared length is
// attacker-controlled and has to be checked before it is allocated. Reading it first
// meant an eleven-byte blob could declare a two-gigabyte field and the array was built
// before anything noticed the bytes were not there; the resulting OutOfMemoryError is
// an Error, so a catch around the caller did not contain it either. Bounding by what
// is left in the buffer ties the allocation to the size of the request.
if (length < 0 || length > MAX_FIELD_LENGTH || length > buffer.remaining()) {
throw new SshPublicKeyException("invalid OpenSSH key field length: " + length);
}
byte[] field = new byte[length];
buffer.get(field);
return field;
}
}
/**
* The RSA public key an {@code ssh-rsa} blob carries, whose fields after the type are the
* exponent and then the modulus. Reads those three and stops: anything after them is not part
* of the key, and refusing a blob over it would reject material the parse never needed.
*/
public static RSAPublicKey rsaKeyOf(byte[] blob) {
Reader reader = reader(blob);
reader.readField();
BigInteger exponent = new BigInteger(reader.readField());
BigInteger modulus = new BigInteger(reader.readField());
try {
return (RSAPublicKey) KeyFactory.getInstance("RSA")
.generatePublic(new RSAPublicKeySpec(modulus, exponent));
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new SshPublicKeyException("not a usable RSA public key: " + e.getMessage(), e);
}
}
/** The {@code "ssh-rsa AAAA..."} line for a key, without a trailing comment. */
public static String toOpenSsh(RSAPublicKey key) {
return "ssh-rsa " + Base64.getEncoder().encodeToString(openSshBlob(key));
}
/** The OpenSSH blob for a key: the type, then the exponent, then the modulus. */
private static byte[] openSshBlob(RSAPublicKey key) {
byte[] type = "ssh-rsa".getBytes(StandardCharsets.US_ASCII);
byte[] exponent = key.getPublicExponent().toByteArray();
byte[] modulus = key.getModulus().toByteArray();
ByteBuffer blob = ByteBuffer.allocate(
3 * Integer.BYTES + type.length + exponent.length + modulus.length);
for (byte[] field : new byte[][] {type, exponent, modulus}) {
blob.putInt(field.length).put(field);
}
return blob.array();
}
/** MD5 of the given bytes as colon-delimited lowercase hex. */
public static String md5ColonHex(byte[] bytes) {
try {
return HexFormat.of().withDelimiter(":")
.formatHex(MessageDigest.getInstance("MD5").digest(bytes));
} catch (NoSuchAlgorithmException e) {
throw new SshPublicKeyException("MD5 is unavailable", e);
}
}
}
@@ -1,18 +1,15 @@
package io.github.hectorvent.floci.services.ec2;
import io.github.hectorvent.floci.core.common.SshPublicKeys;
import org.bouncycastle.openssl.jcajce.JcaPEMWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPublicKeySpec;
import java.util.Base64;
import java.util.HexFormat;
@@ -100,99 +97,34 @@ public final class Ec2KeyMaterial {
* @see <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/verify-keys.html">Verify the fingerprint of your key pair</a>
*/
public static String fingerprintOf(String openSshPublicKey) {
byte[] blob = decodeOpenSshBlob(openSshPublicKey);
byte[] blob = SshPublicKeys.decodeBlob(openSshPublicKey);
if (blob == null) {
return null;
}
try {
byte[] der = blob;
SshReader reader = new SshReader(blob);
String type = new String(reader.readBytes(), StandardCharsets.UTF_8);
SshPublicKeys.Reader reader = SshPublicKeys.reader(blob);
String type = new String(reader.readField(), StandardCharsets.UTF_8);
if ("ssh-ed25519".equals(type)) {
if (reader.readBytes().length != ED25519_KEY_LENGTH) {
if (reader.readField().length != ED25519_KEY_LENGTH) {
return null;
}
return Base64.getEncoder().encodeToString(
MessageDigest.getInstance("SHA-256").digest(blob));
}
if ("ssh-rsa".equals(type)) {
BigInteger exponent = new BigInteger(reader.readBytes());
BigInteger modulus = new BigInteger(reader.readBytes());
der = KeyFactory.getInstance("RSA")
.generatePublic(new RSAPublicKeySpec(modulus, exponent))
.getEncoded();
}
return colonHex(MessageDigest.getInstance("MD5").digest(der));
// EC2 documents its ssh-rsa fingerprint as the MD5 of the DER, which is not the
// OpenSSH fingerprint of the same key. SshPublicKeys keeps the two apart on purpose.
byte[] der = "ssh-rsa".equals(type) ? SshPublicKeys.rsaKeyOf(blob).getEncoded() : blob;
return SshPublicKeys.md5ColonHex(der);
} catch (Exception e) { // malformed material is the caller's problem, not a crash
return null;
}
}
static String openSshPublicKey(RSAPublicKey key) {
byte[] type = "ssh-rsa".getBytes(StandardCharsets.US_ASCII);
byte[] exponent = key.getPublicExponent().toByteArray();
byte[] modulus = key.getModulus().toByteArray();
ByteBuffer blob = ByteBuffer.allocate(
12 + type.length + exponent.length + modulus.length);
for (byte[] field : new byte[][]{type, exponent, modulus}) {
blob.putInt(field.length).put(field);
}
return "ssh-rsa " + Base64.getEncoder().encodeToString(blob.array());
}
private static byte[] decodeOpenSshBlob(String openSshPublicKey) {
if (openSshPublicKey == null) {
return null;
}
// "ssh-rsa AAAAB3Nza... comment" -- the middle field is the blob.
String[] fields = openSshPublicKey.trim().split("\\s+");
if (fields.length < 2) {
return null;
}
try {
return Base64.getDecoder().decode(fields[1]);
} catch (IllegalArgumentException e) {
return null;
}
return SshPublicKeys.toOpenSsh(key);
}
private static String colonHex(byte[] digest) {
return HexFormat.of().withDelimiter(":").formatHex(digest);
}
/** Reads the length-prefixed fields of an OpenSSH public key blob. */
private static final class SshReader {
/**
* Absolute ceiling on a single field, well clear of anything a real key carries: the
* largest is the modulus of a 16384-bit RSA key, 2049 bytes with its sign byte.
*/
private static final int MAX_FIELD_LENGTH = 64 * 1024;
private final ByteBuffer buffer;
SshReader(byte[] blob) {
this.buffer = ByteBuffer.wrap(blob);
}
byte[] readBytes() {
if (buffer.remaining() < Integer.BYTES) {
throw new IllegalArgumentException("truncated OpenSSH key blob");
}
int length = buffer.getInt();
// ImportKeyPair hands the caller's PublicKeyMaterial straight to this parser, so
// the declared length is attacker-controlled and has to be checked before it is
// allocated. Reading it first meant an eleven-byte blob could declare a
// two-gigabyte field and the array was built before anything noticed the bytes
// were not there; the resulting OutOfMemoryError is an Error, so the catch around
// fingerprintOf did not contain it either. Bounding by what is left in the buffer
// ties the allocation to the size of the request that carried it.
if (length < 0 || length > MAX_FIELD_LENGTH || length > buffer.remaining()) {
throw new IllegalArgumentException("invalid OpenSSH key field length: " + length);
}
byte[] field = new byte[length];
buffer.get(field);
return field;
}
}
}