mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
8776c5fc3a
commit
8d5cd2c6e7
4 files changed
+310
-78
No files matched your search
@@ -0,0 +1,134 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.security.KeyPairGenerator;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class SshPublicKeysTest {
|
||||
|
||||
private static final RSAPublicKey KEY = rsaKeyQuietly();
|
||||
|
||||
private static RSAPublicKey rsaKeyQuietly() {
|
||||
try {
|
||||
return rsaKey();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new IllegalStateException("RSA is unavailable", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static RSAPublicKey rsaKey() throws NoSuchAlgorithmException {
|
||||
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
|
||||
generator.initialize(2048);
|
||||
return (RSAPublicKey) generator.generateKeyPair().getPublic();
|
||||
}
|
||||
|
||||
@Test
|
||||
void aKeyRoundTripsThroughTheOpenSshWireForm() throws Exception {
|
||||
RSAPublicKey key = rsaKey();
|
||||
String line = SshPublicKeys.toOpenSsh(key);
|
||||
assertTrue(line.startsWith("ssh-rsa "));
|
||||
|
||||
RSAPublicKey parsed = SshPublicKeys.rsaKeyOf(SshPublicKeys.decodeBlob(line));
|
||||
assertEquals(key.getModulus(), parsed.getModulus());
|
||||
assertEquals(key.getPublicExponent(), parsed.getPublicExponent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theTypeIsTheFirstFieldOfTheBlob() throws Exception {
|
||||
byte[] blob = SshPublicKeys.decodeBlob(SshPublicKeys.toOpenSsh(rsaKey()));
|
||||
assertEquals("ssh-rsa", new String(SshPublicKeys.reader(blob).readField()));
|
||||
}
|
||||
|
||||
/** An authorized_keys line usually carries a comment, which is not part of the blob. */
|
||||
@Test
|
||||
void aTrailingCommentDoesNotDisturbTheBlob() throws Exception {
|
||||
RSAPublicKey key = rsaKey();
|
||||
String line = SshPublicKeys.toOpenSsh(key);
|
||||
RSAPublicKey parsed = SshPublicKeys.rsaKeyOf(
|
||||
SshPublicKeys.decodeBlob(line + " someone@example.com"));
|
||||
assertEquals(key.getModulus(), parsed.getModulus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whatIsNotAnOpenSshLineDecodesToNothing() {
|
||||
assertNull(SshPublicKeys.decodeBlob(null));
|
||||
assertNull(SshPublicKeys.decodeBlob(""));
|
||||
assertNull(SshPublicKeys.decodeBlob("not-a-key"));
|
||||
assertNull(SshPublicKeys.decodeBlob("ssh-rsa !!!not-base64!!!"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void malformedMaterialIsRejectedRatherThanGuessedAt() {
|
||||
assertThrows(SshPublicKeyException.class, () -> SshPublicKeys.reader(null));
|
||||
assertThrows(SshPublicKeyException.class, () -> SshPublicKeys.reader(new byte[0]).readField());
|
||||
// A type, but no exponent and no modulus after it.
|
||||
ByteBuffer typeOnly = ByteBuffer.allocate(Integer.BYTES + 7)
|
||||
.putInt(7).put("ssh-rsa".getBytes());
|
||||
assertThrows(SshPublicKeyException.class, () -> SshPublicKeys.rsaKeyOf(typeOnly.array()));
|
||||
}
|
||||
|
||||
/**
|
||||
* The blob arrives in a caller's request, so a declared field length is attacker-controlled.
|
||||
* Bounding it by what the buffer actually holds is what stops a short blob claiming a huge
|
||||
* field and provoking an OutOfMemoryError, which is an Error and would escape a catch on
|
||||
* Exception. This is the property the extraction had to carry over intact.
|
||||
*/
|
||||
@Test
|
||||
void aFieldLengthLargerThanTheBlobIsRefusedBeforeAnythingIsAllocated() {
|
||||
byte[] lying = ByteBuffer.allocate(8).putInt(Integer.MAX_VALUE).putInt(0).array();
|
||||
assertThrows(SshPublicKeyException.class, () -> SshPublicKeys.reader(lying).readField());
|
||||
|
||||
byte[] negative = ByteBuffer.allocate(8).putInt(-1).putInt(0).array();
|
||||
assertThrows(SshPublicKeyException.class, () -> SshPublicKeys.reader(negative).readField());
|
||||
}
|
||||
|
||||
/** Sixteen bytes of colon-delimited lowercase hex, which is what an MD5 fingerprint is. */
|
||||
@Test
|
||||
void theDigestHelperFormatsColonHex() {
|
||||
String digest = SshPublicKeys.md5ColonHex(new byte[] {1, 2, 3});
|
||||
assertTrue(digest.matches("([0-9a-f]{2}:){15}[0-9a-f]{2}"), digest);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reading is on demand, which is observable: a field after the one asked for may be malformed
|
||||
* without disturbing the read. That is also what bounds the work, because the field count is
|
||||
* as caller-controlled as the field sizes. A blob of four-byte zero-length fields declares one
|
||||
* per four bytes it occupies, so materialising them all would cost an allocation per field
|
||||
* however few the caller wants.
|
||||
*/
|
||||
@Test
|
||||
void aMalformedLaterFieldDoesNotDisturbAnEarlierRead() {
|
||||
byte[] blob = blobWithTrailingJunk();
|
||||
assertEquals("ssh-rsa", new String(SshPublicKeys.reader(blob).readField()),
|
||||
"the first field must be readable even though a later one is not");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* EC2 fingerprints a key by reading only the fields the fingerprint needs. Material after the
|
||||
* modulus is not part of the key, so rejecting a blob over it would turn a usable imported key
|
||||
* into one with no fingerprint at all.
|
||||
*/
|
||||
@Test
|
||||
void fieldsAfterTheKeyDoNotInvalidateIt() {
|
||||
RSAPublicKey withJunk = SshPublicKeys.rsaKeyOf(blobWithTrailingJunk());
|
||||
assertEquals(2048, withJunk.getModulus().bitLength());
|
||||
}
|
||||
|
||||
/** A valid ssh-rsa blob with an unreadable extra field welded onto the end. */
|
||||
private static byte[] blobWithTrailingJunk() {
|
||||
byte[] good = SshPublicKeys.decodeBlob(SshPublicKeys.toOpenSsh(KEY));
|
||||
ByteBuffer out = ByteBuffer.allocate(good.length + 8);
|
||||
// A trailing field claiming far more bytes than follow it.
|
||||
out.put(good).putInt(Integer.MAX_VALUE).putInt(0);
|
||||
return out.array();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user