Skip to content

Reject Curve25519/Curve448 keys with the wrong length - #825

Open
dlazobnaKocka wants to merge 1 commit into
libtom:developfrom
dlazobnaKocka:fix-ec25519-448-key-length
Open

dlazobnaKocka wants to merge 1 commit into
libtom:developfrom
dlazobnaKocka:fix-ec25519-448-key-length

Conversation

@dlazobnaKocka

Copy link
Copy Markdown
Contributor

A certificate or SSH2 public key with a short Ed25519, X25519, Ed448 or X448 key reaches the *_import_raw() functions through x509_import_spki() or pem_ssh.c. Those check the length with LTC_ARGCHK, so the default build calls abort(). With ARGTYPE 2 or 3 the memcpy reads past the end of the input buffer.

This SSH2 key with a 1 byte Ed25519 key is enough, passed to pem_decode_openssh():

---- BEGIN SSH2 PUBLIC KEY ----
AAAAC3NzaC1lZDI1NTE5AAAAAQE=
---- END SSH2 PUBLIC KEY ----
LTC_ARGCHK 'inlen == 32uL' failure on line 35 of file src/pk/ed25519/ed25519_import_raw.c
Aborted

The PKCS#8 and SubjectPublicKeyInfo importers had a related problem. They only checked the upper bound, so a 1 byte key returned CRYPT_OK and the rest of the key buffer kept whatever was in it before.

The fix returns CRYPT_PK_INVALID_SIZE in all of these cases. Each curve gets a small test with a short PKCS#8, SPKI and raw key. The tests fail on develop and pass with the fix.

Checklist

  • tests are added or updated
  • if this fixes something: added a Fixes: tag to the commit message

The *_import_raw() functions checked the key length with LTC_ARGCHK.
A certificate or SSH public key with a short Ed25519 key could reach
them, so the default build called abort(). With ARGTYPE 2 or 3 the
memcpy read past the end of the input buffer.

The PKCS#8 and SubjectPublicKeyInfo importers only checked the
upper bound. A 1 byte key returned CRYPT_OK and the rest of the key
buffer kept whatever was in it before.

Return CRYPT_PK_INVALID_SIZE in all of these cases.

Fixes: 52a24ca ("add Curve25519 API")
Fixes: 076a110 ("Ed448 + X448")
Fixes: 8d19047 ("Add support for more PEM file types + some fixes")
Fixes: 208ee2b ("Refactor SubjectPublicKeyInfo import")
@karel-m

karel-m commented Nov 17, 2026

Copy link
Copy Markdown
Member

Looks good to me

small correction: the default build via plain make or CMake Release uses ARGTYPE=4 not ARGTYPE=0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants