Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

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

Priamo Germosen8d5cd2c6e778
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/SshPublicKeyException.java b/src/main/java/io/github/hectorvent/floci/core/common/SshPublicKeyException.java
new file mode 100644
index 0000000000..b162719db6
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/core/common/SshPublicKeyException.java
@@ -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);
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/SshPublicKeys.java b/src/main/java/io/github/hectorvent/floci/core/common/SshPublicKeys.java
new file mode 100644
index 0000000000..ba84cd026c
--- /dev/null
+++ b/src/main/java/io/github/hectorvent/floci/core/common/SshPublicKeys.java
@@ -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);
+ }
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2KeyMaterial.java b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2KeyMaterial.java
index 58e2d2e6e3..5558fcb8cd 100644
--- a/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2KeyMaterial.java
+++ b/src/main/java/io/github/hectorvent/floci/services/ec2/Ec2KeyMaterial.java
@@ -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;
- }
- }
}
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/SshPublicKeysTest.java b/src/test/java/io/github/hectorvent/floci/core/common/SshPublicKeysTest.java
new file mode 100644
index 0000000000..5db86583ec
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/core/common/SshPublicKeysTest.java
@@ -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 + " [email protected]"));
+ 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();
+ }
+}