Files
floci/src/main/java/io/github
Allen Sanborn 2fbe2b7c64 fix(ec2): generate real key material in CreateKeyPair and fingerprint imported keys (#3307)
* fix(ec2): generate real key material in CreateKeyPair

CreateKeyPair returned a fixed string literal as KeyMaterial:

    -----BEGIN RSA PRIVATE KEY-----
    MIIEpAIBAAKCAQEA0Z3VS5JJcds3xHn/ygWep4Ib/ue7YiKbCIZgYpYDe0+FAKE
    -----END RSA PRIVATE KEY-----

That is a 63-character body -- a placeholder, not a truncated key. It does
not parse:

    $ ssh-keygen -y -f key.pem
    Load key "key.pem": invalid format

and it was byte-identical for every key pair in every region. No public
half was stored either, so Ec2ContainerManager.injectSshKey() had nothing
to write to authorized_keys for a created key.

The AWS API reference for CreateKeyPair describes the response as "an
unencrypted PEM encoded RSA or ED25519 private key", with KeyFingerprint
being "the SHA-1 digest of the DER encoded private key" for a key pair AWS
created. This is the only call that ever discloses the private key, and
callers are expected to use it directly: the Packer amazon-ebs builder
creates a temporary key pair, writes the returned material to a file, and
SSHes in with it. A placeholder cannot serve that, so no Packer build --
and no test that SSHes into an instance -- could work.

Generates a real 2048-bit RSA key pair (the size and type AWS uses for the
default KeyType=rsa), returns it as a PKCS#1 "BEGIN RSA PRIVATE KEY" PEM to
match AWS's encoding rather than Java's native PKCS#8, stores the matching
OpenSSH public key so injection has something to inject, and reports a real
SHA-1 fingerprint. KeyPairGenerator is already used this way in KmsService,
CognitoService and EksOidcService, and bcpkix is already a dependency.

ImportKeyPair reported the same constant fingerprint for every key. It now
reports the MD5 of the DER public key, which is what AWS reports for
imported keys. Only ssh-rsa is decoded back to a SubjectPublicKeyInfo; for
other key types the digest is taken over the wire-format blob, which is
stable and distinct per key but not byte-identical to AWS. Material that
does not parse yields no fingerprint rather than a digest of garbage, and
the caller falls back to the old placeholder.

Tests parse and use the material rather than pattern-matching it -- the
original literal would satisfy any shape check, which is how it survived.
They assert the PEM parses as a 2048-bit RSA key, that the emitted public
key is genuinely the other half of the returned private key, that distinct
calls yield distinct keys, that the OpenSSH length-prefix framing round
trips, and that an imported key's fingerprint ignores the trailing comment.

Related: the SSH path is also blocked by sshd never starting; see the
companion fix for Ec2ContainerManager.startSshd().

* test(ec2): cover the CreateKeyPair and ImportKeyPair wiring

Ec2KeyMaterialTest exercises the generator directly, so reverting the Ec2Service
side of the change left the whole EC2 suite green: 821 tests passed with
createKeyPair still handing out the placeholder literal. The production wiring
was the part with no coverage.

These two go through Ec2Service. The first parses what createKeyPair returns
rather than matching its shape, since the placeholder was a well formed PEM
envelope around 63 bytes and satisfied any pattern check, and asserts the public
half is stored, because that is what RunInstances injects into authorized_keys.
The second asserts two imported keys get different fingerprints, which the old
constant could not.

With the Ec2Service change reverted these fail as
"malformed PEM data: unable to decode base64 string: Index 63 out of bounds for
length 63" and "expected: not equal but was:
<00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00>".

* fix(ec2): bound imported key blob fields and fingerprint ed25519 as AWS does

Two problems in the ImportKeyPair path, both reachable with caller supplied
PublicKeyMaterial.

SshReader.readBytes allocated an array sized by a length read straight out of
the blob, and only then checked that the bytes were there. An eleven byte
request could declare a two gigabyte field, and the resulting OutOfMemoryError
is an Error, so the catch inside fingerprintOf did not contain it. The declared
length is now rejected before it is allocated when it is negative, larger than
what remains in the buffer, or over an absolute ceiling of 64 KiB. Bounding by
the remainder ties the allocation to the size of the request that carried it.

ed25519 imports were fingerprinted with MD5 over the wire blob. EC2 reports the
base64 encoded SHA-256 digest for ed25519 key pairs however they were created,
which is the ssh-keygen value without its SHA256: prefix and with the base64
padding kept.

The RSA import path is unchanged and was already right: AWS documents the check
as "openssl rsa -pubout -outform DER | openssl md5 -c", which is MD5 over the
DER SubjectPublicKeyInfo rather than over the SSH blob, and that is what
RSAPublicKey.getEncoded produces.

The existing fingerprint tests asserted digest shape, or compared the service
against the same helper it calls, so neither could see a wrong scheme. The new
ones pin both key types to values derived outside this codebase from fixed
throwaway keys, and assert the RSA value is not the MD5 of the SSH blob, which
is the scheme it could plausibly be confused with.

Refs: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/verify-keys.html
2026-09-09 16:35:40 -04:00
..