Hash all fingerprints into the manual security check
The manual check used to show the first 8 bytes of the first fingerprint each SDP named, so an interposer could add its own certificate beside the claimed one and the codes still matched. Now each code is the first 16 bytes of a SHA-256 over every fingerprint the SDP names, also ones hidden in other lines, so whatever the browser uses changes the code. Without a fingerprint on either side the connection ends. End-to-end tests try a claimed certificate in several hiding places. The README says that a working connection isn't necessarily direct and what the meeting tracker sees, TODO.md lists hardening from a security review. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ae871a2647
commit
ae1e3bef9b
5 files changed
+205
-38
No files matched your search
+46
-11
@@ -508,23 +508,45 @@ describe('messages', () => {
|
||||
expect(() => decode('not base64!', 'answer')).toThrow('not a valid answer, copy all of it');
|
||||
});
|
||||
|
||||
it('counts every fingerprint once, at session or media level', () => {
|
||||
it('counts every fingerprint once, at session or media level, also hidden ones', () => {
|
||||
const sdp =
|
||||
'v=0\r\na=fingerprint:sha-256 AB:CD\r\nm=application\r\na=fingerprint:SHA-256 ab:cd\r\na=fingerprint:sha-1 01\r\n';
|
||||
expect(fingerprints(sdp)).toEqual(['sha-1 01', 'sha-256 ab:cd']);
|
||||
'v=0\r\na=fingerprint:sha-256 AB:CD\r\nm=application\r\na=fingerprint:SHA-256 ab:cd\na=fingerprint:sha-1\t01\r\n' +
|
||||
// Hidden in lines the browser reads otherwise.
|
||||
'a=candidate:1 1 udp 1 127.0.0.1 9 typ host a=fingerprint:sha-256 01:02\r\n' +
|
||||
'a=x-decoy:a=fingerprint:sha-256 03:04\r\na=x-decoy:\ra=fingerprint:sha-256 05:06\r\n' +
|
||||
// A bare CR doesn't end the line for the browser.
|
||||
'a=fingerprint:sha-256\r07:08\r\n';
|
||||
expect(fingerprints(sdp)).toEqual([
|
||||
'sha-1 01',
|
||||
'sha-256 01:02',
|
||||
'sha-256 03:04',
|
||||
'sha-256 05:06',
|
||||
'sha-256 07:08',
|
||||
'sha-256 ab:cd',
|
||||
]);
|
||||
});
|
||||
|
||||
it('shows the first 8 bytes of both fingerprints for the check', () => {
|
||||
const sdp = (fingerprint: string) => ({ sdp: `v=0\r\na=fingerprint:sha-256 ${fingerprint}\r\n` });
|
||||
it('shows the first 16 bytes of a hash over all fingerprints for the check', async () => {
|
||||
const code = (list: string[]) =>
|
||||
createHash('sha256')
|
||||
.update(JSON.stringify(list))
|
||||
.digest('hex')
|
||||
.slice(0, 32)
|
||||
.toUpperCase()
|
||||
.match(/../g)!
|
||||
.join(' ');
|
||||
const pc = {
|
||||
localDescription: sdp('aa:bb:cc:dd:ee:ff:00:11:22:33'),
|
||||
remoteDescription: sdp('01:02:03:04:05:06:07:08:09'),
|
||||
localDescription: { sdp: 'v=0\r\na=fingerprint:sha-256 aa:bb\r\n' },
|
||||
remoteDescription: { sdp: 'v=0\r\na=fingerprint:sha-256 0A:0B\r\nm=application\r\na=fingerprint:sha-1 0c\r\n' },
|
||||
} as RTCPeerConnection;
|
||||
expect(securityCheck(pc)).toEqual({ local: 'AA BB CC DD EE FF 00 11', remote: '01 02 03 04 05 06 07 08' });
|
||||
expect(securityCheck({ localDescription: null, remoteDescription: null } as RTCPeerConnection)).toEqual({
|
||||
local: '?',
|
||||
remote: '?',
|
||||
expect(await securityCheck(pc)).toEqual({
|
||||
local: code(['sha-256 aa:bb']),
|
||||
remote: code(['sha-1 0c', 'sha-256 0a:0b']),
|
||||
});
|
||||
expect(await securityCheck({ ...pc, remoteDescription: { sdp: 'v=0\r\n' } } as RTCPeerConnection)).toBeUndefined();
|
||||
expect(
|
||||
await securityCheck({ localDescription: null, remoteDescription: null } as RTCPeerConnection),
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it('names the side without a public address', () => {
|
||||
@@ -1170,6 +1192,19 @@ describe('manual exchange', () => {
|
||||
expect(openPeers()).toEqual([]);
|
||||
});
|
||||
|
||||
it('shows other codes for a peer that named a second certificate', async () => {
|
||||
const [sender, receiver] = [side(), side()];
|
||||
const { offer, accept } = await createOffer(file(10), sender.events, sender.signal);
|
||||
// The real one first, the fake one connects too. FakePeer only knows the first.
|
||||
const tampered = btoa(`${atob(offer)}m=application\r\na=fingerprint:sha-256 AA:BB\r\n`);
|
||||
await accept(await answerOffer(tampered, receiver.events, receiver.signal));
|
||||
await until(() => sender.checks[0] && receiver.checks[0]);
|
||||
const [own, peer] = [sender.checks[0]!, receiver.checks[0]!];
|
||||
expect(peer.remote).not.toBe(own.local);
|
||||
expect(peer.local).toBe(own.remote);
|
||||
expect(receiver.status()).toBe('wait: Compare the security codes above with the sender');
|
||||
});
|
||||
|
||||
it('reports a broken offer and answer, the offer stays good for the right answer', async () => {
|
||||
const receiver = side();
|
||||
await expect(answerOffer(btoa('v=0 but nothing else'), receiver.events, receiver.signal)).rejects.toThrow(
|
||||
|
||||
+40
-19
@@ -442,24 +442,39 @@ const BLOCKED = 'a network in between, e.g. a VPN or firewall, may block direct
|
||||
|
||||
const host = (url: string) => URL.parse(url)?.host || url;
|
||||
|
||||
// Anti-MITM check: the first 8 bytes of both DTLS certificate fingerprints, fewer an interposer can
|
||||
// brute force with matching certificates. My local code must equal the peer's remote code and vice
|
||||
// versa, otherwise someone interposed on the signaling.
|
||||
export function securityCheck(pc: RTCPeerConnection) {
|
||||
const short = (sdp = '') =>
|
||||
sdp
|
||||
.match(/a=fingerprint:\S+ ([0-9A-F:]+)/i)?.[1]
|
||||
?.toUpperCase()
|
||||
.split(':')
|
||||
.slice(0, 8)
|
||||
.join(' ') ?? '?';
|
||||
return { local: short(pc.localDescription?.sdp), remote: short(pc.remoteDescription?.sdp) };
|
||||
}
|
||||
|
||||
// Every fingerprint the SDP names, in full: one added beside the real one changes the result too.
|
||||
// Browsers may move them between session and media level, so each counts once.
|
||||
// Browsers may move them between session and media level, so each counts once. Any a=fingerprint
|
||||
// counts, also one hidden in another line: one the browser ignores only makes the result differ,
|
||||
// one missed that the browser uses would let an interposer through. So nothing depends on how a
|
||||
// browser splits lines.
|
||||
export const fingerprints = (sdp: string) =>
|
||||
[...new Set([...sdp.matchAll(/^a=fingerprint:(.+)$/gim)].map(([, line]) => line!.trim().toLowerCase()))].sort();
|
||||
[
|
||||
...new Set(
|
||||
[...sdp.matchAll(/a=fingerprint:([^\n]*)/gi)].map(([, fingerprint]) =>
|
||||
fingerprint!.trim().replace(/\s+/g, ' ').toLowerCase(),
|
||||
),
|
||||
),
|
||||
].sort();
|
||||
|
||||
// Anti-MITM check of the manual exchange: the first 16 bytes of a SHA-256 over all DTLS
|
||||
// certificate fingerprints of each side. An interposer can try one cheaply, it only has to vary a
|
||||
// line the browser ignores, so it takes that many to be out of its reach. My local code must equal
|
||||
// the peer's remote code and vice versa, otherwise someone interposed on the signaling. All of them
|
||||
// count, like in prove: one certificate may be named by several hash functions, and a browser may
|
||||
// accept a certificate by any of them. Without one on either side there is nothing to check:
|
||||
// undefined.
|
||||
export async function securityCheck(pc: RTCPeerConnection) {
|
||||
const short = async (sdp = '') => {
|
||||
const list = fingerprints(sdp);
|
||||
if (!list.length) return;
|
||||
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(JSON.stringify(list)));
|
||||
return Array.from(new Uint8Array(digest, 0, 16), (byte) => byte.toString(16).padStart(2, '0'))
|
||||
.join(' ')
|
||||
.toUpperCase();
|
||||
};
|
||||
const [local, remote] = await Promise.all([short(pc.localDescription?.sdp), short(pc.remoteDescription?.sdp)]);
|
||||
if (local && remote) return { local, remote };
|
||||
}
|
||||
|
||||
// The security check of tracker signaling, resolves once the peer proved the code. Each side signs its
|
||||
// role, its SDP type, and the fingerprints of both certificates as it sees them. The peer checks that
|
||||
@@ -522,7 +537,6 @@ function connected(
|
||||
events.log(`connected via ${via}`);
|
||||
events.log('waiting for the security check');
|
||||
if (key) events.status('start', 'Connected, checking the code');
|
||||
else events.status('wait', `Compare the security codes above with the ${peer}`);
|
||||
let done = false;
|
||||
const end = (reason: string) => {
|
||||
if (done) return;
|
||||
@@ -541,8 +555,15 @@ function connected(
|
||||
return {
|
||||
checked: key
|
||||
? prove(pc, channel, key, end).then(() => events.log('security check passed'))
|
||||
: new Promise((resolve) =>
|
||||
events.check({ ...securityCheck(pc), confirm: resolve, reject: () => end('security check failed') }),
|
||||
: securityCheck(pc).then(
|
||||
(codes) =>
|
||||
new Promise<void>((resolve) => {
|
||||
// The peer may have left while the codes were computed.
|
||||
if (done) return;
|
||||
if (!codes) return end('no certificate to check');
|
||||
events.check({ ...codes, confirm: resolve, reject: () => end('security check failed') });
|
||||
events.status('wait', `Compare the security codes above with the ${peer}`);
|
||||
}),
|
||||
),
|
||||
start: () => (done = true),
|
||||
end,
|
||||
|
||||
Reference in new issue
Block a user