Reintroduce Firefox support

This commit is contained in:
Jack Yu 2024-10-26 23:42:36 -07:00
parent b95c60972f
commit a26a92f6bd
1 changed files with 71 additions and 54 deletions

View File

@ -28,11 +28,25 @@ let certDB = null;
let localCert = null; let localCert = null;
// Utilities // Utilities
function getCertificateChecksum(cert) { function getCertificateChecksumFromSDP(desc) {
let fp = desc.sdp.split('\n').reduce((res, curr) =>
curr.startsWith('a=fingerprint:sha-256 ') ? curr : res, null);
console.assert(fp !== null, 'Description does not have a sha256sum');
return fp.trim().split(' ')[1].split(':').join('').toLowerCase();
}
async function getCertificateChecksum(cert) {
if (cert.getFingerprints) {
let fp = cert.getFingerprints().reduce((res, curr) => let fp = cert.getFingerprints().reduce((res, curr) =>
curr.algorithm === 'sha-256' ? curr : res, null); curr.algorithm === 'sha-256' ? curr : res, null);
console.assert(fp !== null, 'Certificate does not have a sha256sum'); console.assert(fp !== null, 'Certificate does not have a sha256sum');
return fp.value.split(':').join(''); return fp.value.split(':').join('');
} else { // Firefox shim
let testConn = new RTCPeerConnection({certificates: [cert]});
let desc = await testConn.createOffer();
testConn.close();
return getCertificateChecksumFromSDP(desc);
}
} }
// IndexedDB // IndexedDB
@ -87,8 +101,10 @@ function onLoadCert() {
loadCertificate(certDB, loadCertificate(certDB,
cert => { cert => {
if (cert !== null) { if (cert !== null) {
console.log('Loaded certificate:', getCertificateChecksum(cert)); getCertificateChecksum(cert).then(fp => {
console.log('Expires:', cert.expires); let ts = new Date(cert.expires).toISOString();
console.log(`Loaded certificate (expires ${ts}): ${fp}`);
});
localCert = cert; localCert = cert;
setCertStatusDisplay('Loaded'); setCertStatusDisplay('Loaded');
setButtonClickable('certUpload', true); setButtonClickable('certUpload', true);
@ -128,8 +144,10 @@ function onNewCert() {
hash: 'SHA-256', hash: 'SHA-256',
namedCurve: 'P-256', namedCurve: 'P-256',
}).then(cert => { }).then(cert => {
console.log('Generated new certificate:', getCertificateChecksum(cert)); getCertificateChecksum(cert).then(fp => {
console.log('Expires:', cert.expires); let ts = new Date(cert.expires).toISOString();
console.log(`Generated new certificate (expires ${ts}): ${fp}`);
});
localCert = cert; localCert = cert;
setCertStatusDisplay('Available'); setCertStatusDisplay('Available');
setButtonClickable('certUpload', true); setButtonClickable('certUpload', true);
@ -142,6 +160,7 @@ function onNewCert() {
// Upload Button // Upload Button
function onUploadCert() { function onUploadCert() {
console.assert(localCert !== null, 'No local certificate available'); console.assert(localCert !== null, 'No local certificate available');
getCertificateChecksum(localCert).then(fp => {
fetch('/keys', { fetch('/keys', {
method: 'POST', method: 'POST',
headers: { headers: {
@ -149,7 +168,7 @@ function onUploadCert() {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
body: JSON.stringify({ body: JSON.stringify({
'key': getCertificateChecksum(localCert), 'key': fp,
'expires': localCert.expires, 'expires': localCert.expires,
}), }),
}).then(res => { }).then(res => {
@ -160,13 +179,14 @@ function onUploadCert() {
setCertStatusDisplay(`Upload Failed (${res.status})`); setCertStatusDisplay(`Upload Failed (${res.status})`);
}) })
} }
})
}); });
} }
// Clear Button // Clear Button
function onClearCert() { function onClearCert() {
console.assert(localCert !== null, 'No local certificate available'); console.assert(localCert !== null, 'No local certificate available');
let fp = getCertificateChecksum(localCert); getCertificateChecksum(localCert).then(fp => {
fetch(`/keys/${fp}`, { fetch(`/keys/${fp}`, {
method: 'DELETE', method: 'DELETE',
headers: { headers: {
@ -187,6 +207,7 @@ function onClearCert() {
}) })
} }
}); });
});
} }
// =============================== Chat Control =============================== // =============================== Chat Control ===============================
@ -202,13 +223,6 @@ function setChatStatusDisplay(status) {
document.getElementById('chatStatus').innerText = status; document.getElementById('chatStatus').innerText = status;
} }
function getCertificateChecksumFromSDP(desc) {
let fp = desc.sdp.split('\n').reduce((res, curr) =>
curr.startsWith('a=fingerprint:sha-256 ') ? curr : res, null);
console.assert(fp !== null, 'Description does not have a sha256sum');
return fp.trim().split(' ')[1].split(':').join('').toLowerCase();
}
function verifyFingerprint(desc) { function verifyFingerprint(desc) {
let fp = getCertificateChecksumFromSDP(desc); let fp = getCertificateChecksumFromSDP(desc);
fetch(`/keys/${fp}`, { fetch(`/keys/${fp}`, {
@ -319,6 +333,9 @@ function onRTCConnectionStateChange(event) {
let state = connection.connectionState; let state = connection.connectionState;
console.log('WebRTC connection state update:', state); console.log('WebRTC connection state update:', state);
if (state === 'failed') {
onLeaveChat();
}
} }
// Join Button // Join Button