Skip to content
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

add optional target length #44

Merged
merged 2 commits into from
Jul 24, 2024
Merged

add optional target length #44

merged 2 commits into from
Jul 24, 2024

Conversation

andrewkmin
Copy link
Contributor

@andrewkmin andrewkmin commented Jul 23, 2024

Some randomly generated credentials would not be large enough to fully occupy the expected length of 32 bytes. This is specifically to address an issue where a JWT token would have d, x, or y values that are too short.

Here's a sample error message:

The JWK's "x" member defines an octet string of length 31 bytes but should be 32

Note that this PR does not add similar optionality to other iframes (import/export), since those do not require importing a JWT token.

Sample webcrypto tests can be found here: https://chromium.googlesource.com/chromium/src/+/master/components/test/data/webcrypto/bad_ec_keys.json

Tested locally by: creating a credential that results in a buffer of length 31 for any of the d, x, or y components of the JWT, ensuring that the buffer is padded, and that ultimately the credential can be injected + used to stamp a payload.

Screen.Recording.2024-07-23.at.5.03.38.PM.mov

@andrewkmin andrewkmin force-pushed the andrew/zero-pad-jwk branch 4 times, most recently from 3dcfbe3 to 65ea7f8 Compare July 23, 2024 20:57
@andrewkmin andrewkmin force-pushed the andrew/zero-pad-jwk branch from 65ea7f8 to bfae48a Compare July 23, 2024 20:58
})

it("uncompresses raw P-256 public keys", async () => {
let uncompressedFrom02 = TKHQ.uncompressRawPublicKey(TKHQ.uint8arrayFromHexString("02c6de3e1d08270d39076651a2b14fd38031dae89892dc124d2f9557816e7e5da4"));
expect(uncompressedFrom02).toEqual(TKHQ.uint8arrayFromHexString("04c6de3e1d08270d39076651a2b14fd38031dae89892dc124d2f9557816e7e5da4f510c344715f84cf0ba0cc71bd04136c0fb2633a3f459e68ffb8620be16900f0", "hex"));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this "hex" parameter was not being used prior

@@ -208,16 +208,23 @@ <h2>Message log</h2>
};

/**
* Takes a hex string (e.g. "e4567ab") and returns an array buffer (Uint8Array)
* Takes a hex string (e.g. "e4567abc") and returns an array buffer (Uint8Array)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: e4567abc is of even-length

@andrewkmin andrewkmin requested a review from r-n-o July 23, 2024 20:59
Copy link
Contributor

@moe-dev moe-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beauty 🙏

auth/index.html Outdated
@@ -496,21 +503,22 @@ <h2>Message log</h2>
/**
* Converts a `BigInt` into a base64url encoded string
* @param {BigInt} num
* @param {number} length: optional expected length of the resulting buffer
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't return a buffer, it returns a string. So...maybe this is "number of bytes contained in the resulting string"? Tricky tricky.

* @returns {Uint8Array}
*/
var uint8arrayFromHexString = function(hexString) {
var uint8arrayFromHexString = function(hexString, length) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's add a test to exercise this new behavior? e.g. uint8arrayFromHexString("0001") (should result in a buffer with a single byte) vs uint8arrayFromHexString("0001", 2) (should result in a buffer with 2 bytes)

@andrewkmin andrewkmin merged commit 29f5a45 into main Jul 24, 2024
6 checks passed
@andrewkmin andrewkmin deleted the andrew/zero-pad-jwk branch July 24, 2024 15:58
@andrewkmin andrewkmin mentioned this pull request Aug 6, 2024
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.

3 participants