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

Fix MSB 1 leading to negative modulus in bun #155

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/asn1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,17 @@ function encodeLength(length: number) {
}

/**
* Encode a buffer (that represent an integer) as integer per ASN.1 spec (DER-encoding)
* Encode a buffer (that represents a positive integer) as integer per ASN.1 spec (DER-encoding)
* See https://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf chapter 8.3
*
* @param buffer - The buffer that represent an integer to encode
* @param buffer - The buffer that represents a positive integer
* @returns The buffer
*/
function encodeBufferAsInteger(buffer: Buffer) {
function encodeBufferAsPositiveInteger(buffer: Buffer) {
if (buffer[0] >> 7) {
// MSB is 1, add leading zero to indicate positive number
buffer = Buffer.concat([Buffer.from([0]), buffer]);
}
return Buffer.concat([
encodeIdentifier({
class: Asn1Class.Universal,
Expand Down Expand Up @@ -213,7 +217,10 @@ export function constructPublicKeyInDerFormat(n: Buffer, e: Buffer): Buffer {
return encodeSequence([
ALGORITHM_RSA_ENCRYPTION,
encodeBufferAsBitString(
encodeSequence([encodeBufferAsInteger(n), encodeBufferAsInteger(e)])
encodeSequence([
encodeBufferAsPositiveInteger(n),
encodeBufferAsPositiveInteger(e),
])
),
]);
}
Expand Down
39 changes: 39 additions & 0 deletions tests/unit/asn1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,45 @@ describe("unit tests asn1", () => {
expect(publicKey).toEqual(priorGeneratedPublicKey);
});

test("Prepends 0 byte only if MSB is 1", () => {
const n = Buffer.from([0b10000000]); // MSB 1
const e = Buffer.from([0b01000000]); // MSB 0
const publicKeyDer = constructPublicKeyInDerFormat(n, e);
expect(publicKeyDer).toEqual(
Buffer.from([
48,
27,
48,
13,
6,
9,
42,
134,
72,
134,
247,
13,
1,
1,
1,
5,
0,
3,
10,
0,
48,
7,
2,
2,
0, // here is the leading 0
128, // prepended with leading 0
2,
1,
64, // not prepended with leading 0
])
);
});

test("Deconstructing public key works", () => {
const priorGeneratedPublicKey = readFileSync(
join(__dirname, "pubkey-test.der")
Expand Down
4 changes: 1 addition & 3 deletions tests/unit/https.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ describe("unit tests https", () => {
const payload = JSON.stringify({ hello: "world" }) + "}";
mockHttpsUri(uri, { responsePayload: payload });
expect.assertions(1);
return expect(fetchJson(uri)).rejects.toThrow(
"Unexpected token } in JSON at position 17"
);
return expect(fetchJson(uri)).rejects.toThrow("JSON at position 17");
});

test("Fetch JSON error flow works: 404", () => {
Expand Down
Loading