ssh-key: fix Signature::encode for sk-ecdsa-sha2-nistp256#532
Open
jkolo wants to merge 1 commit into
Open
Conversation
`Signature`'s `Decode` impl handles the SK signature trailer (flags + counter) for both `SkEd25519` and `SkEcdsaSha2NistP256`, but the `Encode` impl only special-cases `SkEd25519`. As a result, an `sk-ecdsa-sha2-nistp256@openssh.com` signature falls through to the generic branch, which writes the whole `data` buffer (ECDSA signature + flags + counter) as a single length-prefixed string instead of emitting `string ecdsa_signature || byte flags || uint32 counter`. Round-tripping such a signature through `Decode` then fails because the trailer bytes are buried inside the string. Extend the `Encode` sk-trailer branch to also match `SkEcdsaSha2NistP256`; the existing trailer-splitting logic is generic over the trailer size and works unchanged for both algorithms. Adds `decode_sk_ecdsa_sha2_p256` and `encode_sk_ecdsa_sha2_p256` tests mirroring the existing sk-ed25519 ones; `encode_sk_ecdsa_sha2_p256` fails before this change and passes after.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Signature'sDecodeandEncodeimpls are asymmetric forsk-ecdsa-sha2-nistp256@openssh.comsignatures.Decodereads the security-key trailer (byte flags || uint32 counter)for both SK algorithms:
But
Encodeonly special-casesSkEd25519:For an
sk-ecdsa-sha2-nistp256@openssh.comsignature this writes thewhole
databuffer (ECDSA signature plus the 5-byte trailer) as asingle length-prefixed string, instead of the wire layout required by
OpenSSH's
PROTOCOL.u2f:A signature produced this way fails to decode again — the trailer bytes
end up buried inside the inner string — and is rejected by OpenSSH as an
invalid signature.
Fix
Extend the
Encodesk-trailer branch to also matchSkEcdsaSha2NistP256. The existing trailer-splitting logic is genericover
SK_SIGNATURE_TRAILER_SIZEand works unchanged for both algorithms.Tests
Adds
decode_sk_ecdsa_sha2_p256andencode_sk_ecdsa_sha2_p256,mirroring the existing sk-ed25519 tests. The test vector reuses the
ECDSA signature components from
ECDSA_SHA2_P256_SIGNATUREwith ansk-ecdsa algorithm prefix and a flags/counter trailer.
encode_sk_ecdsa_sha2_p256fails before this change (encode emits a72-byte body as a 77-byte string) and passes after.