FairEmail/decrypt/index.html

172 lines
6.3 KiB
HTML

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Decrypt text</title>
<meta name="theme-color" content="#006db3">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<meta name="description" content="Decrypt text">
<meta name="author" content="M66B">
<meta name="robots" content="noindex">
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; script-src 'unsafe-inline' *.faircode.eu;">
<style>
body { padding-left: 10px; padding-right: 10px; font-family: Arial, Helvetica, sans-serif; }
</style>
<style>
.noscript { display: none; }
</style>
<noscript>
<style>
.noscript { display: block; }
</style>
</noscript>
<!-- https://github.com/cure53/DOMPurify 2.4.0 -->
<script src="https://email.faircode.eu/decrypt/purify.min.js"></script>
<script>
window.addEventListener('load', load);
function load() {
let form = document.getElementById('form')
let password = document.getElementById('password');
let message = document.getElementById('message');
let error = document.getElementById('error');
let details = document.getElementById('details');
form.addEventListener('submit', submit);
if (window.location.hash)
if (crypto.subtle &&
typeof Uint8Array === 'function' &&
typeof TextEncoder === 'function') {
form.style.display = 'block';
password.focus();
}
else {
error.textContent = 'Your browser is unsuitable for decrypting text';
error.style.display = 'block';
details.innerHTML =
'crypto.subtle: ' + (crypto.subtle ? 'Yes' : 'No') + '<br>' +
'Uint8Array: ' + (Uint8Array ? 'Yes' : 'No') + '<br>' +
'TextEncoder: ' + (TextEncoder ? 'Yes' : 'No') + '<br>';
details.style.display = 'block';
}
else {
message.innerHTML = 'Nothing to see here';
message.style.display = 'block';
}
document.getElementById('year').textContent = new Date().getFullYear();
}
function submit(event) {
event.preventDefault();
decrypt();
}
async function decrypt() {
let fields = document.getElementById('fields');
let form = document.getElementById('form')
let password = document.getElementById('password');
let message = document.getElementById('message');
let error = document.getElementById('error');
let details = document.getElementById('details');
let copyright = document.getElementById('copyright');
try {
fields.disabled = true;
message.style.display = 'none';
error.style.display = 'none';
details.style.display = 'none';
if (!password.value)
throw new Error('Password required');
let dirty = await _decrypt(password.value);
let clean = DOMPurify.sanitize(dirty, { USE_PROFILES: { html: true } });
form.style.display = 'none';
message.innerHTML = clean;
message.style.display = 'block';
copyright.style.display = 'none';
} catch (e) {
console.log("%O", e);
fields.disabled = false;
password.value = '';
password.focus();
error.textContent = 'Could not decrypt the message. Is the password correct?';
error.style.display = 'block';
details.textContent = e.toString();
details.style.display = 'block';
}
}
async function _decrypt(password) {
let msg = atob(window.location.hash.substr(1).replaceAll('-', '+').replaceAll('_', '/'));
const buf = new Uint8Array(msg.length);
for (let i = 0; i < msg.length; i++)
buf[i] = msg.charCodeAt(i);
const version = buf[0];
const salt = buf.slice(1, 1 + 16);
const iv = buf.slice(1 + 16, 1 + 16 + 12);
const e = buf.slice(1 + 16 + 12, buf.length);
// https://developer.mozilla.org/en-US/docs/Web/API/Crypto/subtle
// https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder
const passwordBuffer = new TextEncoder('UTF-8').encode(password);
const importedKey = await crypto.subtle.importKey('raw', passwordBuffer, 'PBKDF2', false, ['deriveBits']);
const derivation = await crypto.subtle.deriveBits({name: 'PBKDF2', hash: 'SHA-512', salt: salt, iterations: 120000}, importedKey, 256);
const importedEncryptionKey = await crypto.subtle.importKey('raw', derivation, {name: 'AES-GCM'}, false, ['decrypt']);
const decrypted = await crypto.subtle.decrypt({name: 'AES-GCM', iv: iv, tagLength: 128}, importedEncryptionKey, e);
return new TextDecoder('UTF-8').decode(decrypted);
}
</script>
</head>
<body>
<p class="noscript" style="color: red; font-weight: bold;">Please enable JavaScript</p>
<form id="form" action="#" method="GET" style="display: none;">
<p>
Someone sent you password protected text with <a href="https://email.faircode.eu/" target="_blank">FairEmail</a>.
</p>
<fieldset id="fields" style="border:0 none; margin: 0; padding: 0;">
<p>
<label for="password">Enter password:</label><br>
<input id="password" name="password" type="password" required><br>
<span style="font-size: smaller;">Passwords are case-sensitive </span>
</p>
<p>
<input id="submit" type="submit" value="Decrypt">
</p>
</fieldset>
<p style="font-size: smaller;">
Password protected text is sent as a <a href="https://en.wikipedia.org/wiki/URI_fragment" target="_blank">URI fragment</a> and not stored on third party servers, and decrypted in the browser with JavaScript.<br>
Password protected text is encrypted with AES/GCM with a 256 bits key derived with PBKDF2/SHA-512 with 120,000 iterations.<br>
Please see <a href="https://github.com/M66B/FairEmail/blob/master/FAQ.md#user-content-faq184" target="_blank">here</a> for more information.
</p>
</form>
<p id="message" style="display: none;"></p>
<p id="error" style="color: red; font-weight: bold; display: none;"></p>
<p id="details" style="font-size: x-small; display: none;"></p>
<p id="copyright" style="padding-top: 20px;">
Copyright &copy; 2018&ndash;<span id="year">2022</span> by Marcel Bokhorst (M66B)
<br>
<br>
<span style="font-size: smaller;">This page is <a href="https://github.com/M66B/FairEmail/blob/master/decrypt/index.html" target="_blank">open source</a>.</span>
</p>
</body>
</html>