Skip to content

Commit

Permalink
Fix MSB 1 leading to negative modulus in bun (#155)
Browse files Browse the repository at this point in the history
* Fix MSB 1 leading to negative modulus in bun

* Add unit test for previous error situation
  • Loading branch information
ottokruse authored Feb 12, 2024
1 parent bbf20e1 commit 34198c4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
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

0 comments on commit 34198c4

Please sign in to comment.