-
Notifications
You must be signed in to change notification settings - Fork 30
Normalize EC2 key size at input time to ensure RFC 9053 compliance #223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kentakayama
wants to merge
8
commits into
veraison:main
Choose a base branch
from
kentakayama:normalize-ec2-key-size
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cb33f56
update: always handle x, y and d with full size for the curve
kentakayama 1337f29
add: test ensuring the NewKeyEC2 always returns correct byte size of …
kentakayama f3b1be1
add: failure test ensuring invalid size of x and d for OKP
kentakayama b9d3c0b
update: Normalize and validate EC2 key lengths to ensure compliance w…
kentakayama 52af599
fix: typo
kentakayama 88057f2
refactor: clarify error handling for missing vs malformed params for …
kentakayama 13e02ff
refactor: comments
kentakayama b745bfa
refactor: split curveSize into keySizeEC2 and keySizeOKP
kentakayama File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@OR13
I've noticed that I made a significant change to how X and Y are handled in the existing code. My apologies.
For EC2 private keys, the previous implementation allowed both X and Y to be nil and did not raise an error in those cases.
For OKP private keys, the implementation likewise permitted X to be nil without producing an error.
See:
go-cose/key.go
Lines 455 to 457 in 022cb54
Specifically, the old code returned an error when
len(x) > size, so if x was nil it did not raise an error.However, in the modified code I introduced, an error is returned when
len(x) != size, which means that a nil x now triggers an error.RFC 9053 Section 7.1.1 states the following, so this change may need to be considered carefully.
From the perspective of simplifying error handling, it might be preferable to require that both X and Y are always present and conform to the lengths specified in RFC 9053. What do you think?
https://www.rfc-editor.org/rfc/rfc9053#section-7.1.1-4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps the best solution here is a separate key representation for the "minimal cose key".
I would have x, and y be present and yield errors when omitted in the default.
I would also defer implementation of the "minimal cose key", until someone asks for it, or is willing to implement it.
@thomas-fossati @shizhMSFT wdyt?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @OR13 .
The policy to "require both x and y by default for EC2 public and private keys" aligns with my own view, and it also simplifies handling of other cases—such as compressed point encoding. With this approach, we can compute the y-coordinate (from x or d, if present) during
UnmarshalCBOR, which avoids the need for the Key struct to retain whether the original CBOR data used compressed mode.Hence, the following compressed mode check will not be required:
(Also, I realized that the code above in my PR is incorrect — the correct check should be
len(x) == size. That said, theKeystruct doesn't explicitly indicate whether the y-coordinate is compressed. In any case, if we enforce the presence of both x and y with standard lengths, we can drop this logic entirely and avoid handling compressed mode.)When marshaling with
MarshalCBOR, we generally avoid using compressed mode. By requiring both x and y as bstr, we can support both private keys (with d) and public keys (without d) in a consistent way. Callingk.validate()at the beginning ofMarshalCBORwould help ensure that we don't emit CBOR data that violates RFC 9053.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @OR13 ,
I hope the commit 88057f2 resolves this conversation.
At this point, missing x and y for PrivateKey during
validate(KeyOpSign)is acceptable, since RFC 9053 defines them as RECOMMENDED, not required.That said, I've refactored the validation logic to align more precisely with RFC 9053. In particular:
Previously, both
x := []byte{}; len(x) == 0(presents but empty) andvar x []byte; len(x) == 0(absent) would pass the same check.To distinguish these cases, I've updated the conditionals in the first block from
len(x) == 0tox == nil.In the second block, if x, y or d are present, their lengths are now explicitly checked.
As for deriving (x, y) from d, or recovering compressed y from x, I believe that should be handled in a separate issue or pull request.