Skip to content

Commit b2050af

Browse files
committed
fix(mobile): reject stored DPoP keys with non-canonical coordinates
The native key check rebuilt the public point from the stored JWK by copying each decoded coordinate into a fixed buffer, so a 31- or 33-byte coordinate could still name the stored point and pass. The old string comparison rejected these keys; require 32 canonical bytes again.
1 parent 95b9fa3 commit b2050af

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎apps/mobile/src/features/cloud/dpop.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,32 @@ describe("mobile DPoP", () => {
125125
}),
126126
);
127127

128+
// X ends in 0x00, so a zero-filled 31-byte X, or a 33-byte X whose extra byte Y overwrites,
129+
// still names the same point.
130+
const privateJwkWithZeroEndedX = {
131+
kty: "EC",
132+
crv: "P-256",
133+
x: "TCE1CTXQ3-B54Fwv9yDlkE3rgcWsJsuGIv2Q9QHcZAA",
134+
y: "zL4Eewo43mYmVjtqh597_dGnKa7lkxxB5XaQk32V9UM",
135+
d: "mzWsJz_1-NerI3_NPtF_hcQhQrXnEITcNEmu5BzaDK4",
136+
};
137+
const xBytes = Buffer.from(privateJwkWithZeroEndedX.x, "base64url");
138+
it.effect.each([
139+
{ length: "short", x: xBytes.subarray(0, 31) },
140+
{ length: "long", x: Buffer.concat([xBytes, Buffer.of(7)]) },
141+
])("rejects a persisted key with a $length coordinate", ({ x }) =>
142+
Effect.gen(function* () {
143+
secureStore.set(
144+
"t3code.cloud.dpop-proof-key",
145+
JSON.stringify({ ...privateJwkWithZeroEndedX, x: x.toString("base64url") }),
146+
);
147+
148+
const error = yield* loadOrCreateDpopProofKeyPair().pipe(Effect.flip);
149+
150+
expect(error.message).toBe("Stored DPoP proof key is invalid.");
151+
}),
152+
);
153+
128154
it.effect("signs proofs that the relay's verifier accepts", () =>
129155
Effect.gen(function* () {
130156
secureStore.clear();

‎apps/mobile/src/features/cloud/dpop.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,19 @@ function base64UrlToBytes(value: string): Uint8Array {
9090
return Result.getOrThrow(Base64Url.decode(value));
9191
}
9292

93+
function coordinateBytes(value: string): Uint8Array {
94+
const bytes = base64UrlToBytes(value);
95+
if (bytes.length !== 32 || Base64Url.encode(bytes) !== value) {
96+
throw new Error("DPoP key coordinate is not 32 canonical bytes.");
97+
}
98+
return bytes;
99+
}
100+
93101
function uncompressedPublicKeyFromJwk(jwk: DpopPublicJwk): Uint8Array {
94102
const publicKey = new Uint8Array(65);
95103
publicKey[0] = 0x04;
96-
publicKey.set(base64UrlToBytes(jwk.x), 1);
97-
publicKey.set(base64UrlToBytes(jwk.y), 33);
104+
publicKey.set(coordinateBytes(jwk.x), 1);
105+
publicKey.set(coordinateBytes(jwk.y), 33);
98106
return publicKey;
99107
}
100108

0 commit comments

Comments
 (0)