Repair EC x/y coordinates when importing JWK #585
Merged
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.
Description
Some encoders on some platform omit a single 0-byte at the start of either Y or X coordinate of the elliptic curve point. This leads to an encoding error when data is passed to OpenSSL BN. It is known to have happened to exported JWKs on a Java application and on a Flutter/Dart application (both iOS and Android). All that is needed to fix the problem is adding a leading 0-byte. We know the required byte is 0 because with any other byte the point is no longer on the curve - and OpenSSL will actually communicate this via another exception. The indication of a stripped byte will be the fact that the coordinates - once decoded into bytes - should always be an even bytesize. For example, with a P-521 curve, both x and y must be 66 bytes. With a P-256 curve, both x and y must be 32 and so on. The simplest way to check for this truncation is thus to check whether the number of bytes is odd, and restore the leading 0-byte if it is.
Closes #412 and (by extension) #542
Checklist
Before the PR can be merged be sure the following are checked: