From 5abc62e8657e70c405afa175e52c73316d8a53a2 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Thu, 24 Jan 2019 17:22:12 +0100 Subject: Added alasql database and pgp worker --- public/scripts/2_encryption.js | 51 ++++++++++++++++++++++++++++++------------ public/scripts/chat.js | 4 ++-- 2 files changed, 39 insertions(+), 16 deletions(-) (limited to 'public/scripts') diff --git a/public/scripts/2_encryption.js b/public/scripts/2_encryption.js index 98bd49d..d9da7ac 100644 --- a/public/scripts/2_encryption.js +++ b/public/scripts/2_encryption.js @@ -1,8 +1,19 @@ +const alasql = require('alasql'); const openpgp = require('openpgp'); -//openpgp.initWorker({ path:'openpgp.worker.js' }); // TODO: Add openpgp web worker support +openpgp.initWorker({path: 'openpgp.worker.js'}); let encrypted, decrypted; // REMEMBER: Remove testing variables (leaking) +function setupDatabase() { + // REMEMBER: "key" and "type" are names reserved for SQL + alasql('CREATE localstorage DATABASE IF NOT EXISTS texx'); + alasql('ATTACH localStorage DATABASE texx AS db'); + alasql('CREATE TABLE IF NOT EXISTS db.own_keys (key_type STRING, key_data STRING)'); + alasql('CREATE TABLE IF NOT EXISTS db.keys (peer_id STRING, key_data STRING)'); + alasql('CREATE TABLE IF NOT EXISTS db.messages (id INT AUTO_INCREMENT, message STRING)'); + return true; +} + /** * Generates and stores encrypted private key, public key and a revocation certificate * @param peerId @@ -11,16 +22,16 @@ let encrypted, decrypted; // REMEMBER: Remove testing variables (leaking) */ async function generateKeys(peerId, passphrase) { const options = { - peerIds: [{name: peerId}], + userIds: [{name: peerId}], numBits: 4096, passphrase: passphrase }; await openpgp.generateKey(options).then((key) => { - localStorage.setItem('private_key', key.privateKeyArmored); - localStorage.setItem('public_key', key.publicKeyArmored); - localStorage.setItem('revocation_certificate', key.revocationCertificate); - console.log('[LOG] Successfully generated and stored keys!') + alasql(`INSERT INTO db.own_keys VALUES ("private_key", "${key.privateKeyArmored}")`); + alasql(`INSERT INTO db.own_keys VALUES ("public_key", "${key.publicKeyArmored}")`); + alasql(`INSERT INTO db.own_keys VALUES ("revocation_certificate", "${key.revocationCertificate}")`); + console.log('[LOG] Successfully generated and stored keys!'); }); } @@ -29,7 +40,8 @@ async function generateKeys(peerId, passphrase) { * @returns {string} */ function getPrivateKey() { - return localStorage.getItem('private_key'); + const privateKey = alasql('SELECT key_data FROM db.own_keys WHERE key_type = "private_key" LIMIT 1'); + return privateKey.length > 0 ? privateKey[0]['key_data'] : ''; } /** @@ -37,7 +49,17 @@ function getPrivateKey() { * @returns {string} */ function getPublicKey() { - return localStorage.getItem('public_key'); + const publicKey = alasql('SELECT key_data FROM db.own_keys WHERE key_type = "public_key" LIMIT 1'); + return publicKey.length > 0 ? publicKey[0]['key_data'] : ''; +} + +/** + * Gets the peers revocation certificate + * @returns {string} + */ +function getRevocationCertificate() { + const revocationCertificate = alasql('SELECT key_data FROM db.own_keys WHERE key_type = "revocation_certificate" LIMIT 1'); + return revocationCertificate.length > 0 ? revocationCertificate[0]['key_data'] : ''; } /** @@ -93,9 +115,9 @@ async function decrypt(data, publicKey, privateKey, passphrase) { * @returns {boolean} */ function isEncrypted() { - const hasPrivateKey = localStorage.getItem('private_key') !== null; - const hasPublicKey = localStorage.getItem('public_key') !== null; - const hasRevocationCertificate = localStorage.getItem('revocation_certificate') !== null; + const hasPrivateKey = getPrivateKey() !== ''; + const hasPublicKey = getPublicKey() !== ''; + const hasRevocationCertificate = getRevocationCertificate() !== ''; return (hasPrivateKey && hasPublicKey && hasRevocationCertificate); } @@ -105,7 +127,7 @@ function isEncrypted() { * @param key */ function storePublicKey(peerId, key) { - localStorage.setItem(peerId, key); + alasql(`INSERT INTO db.keys VALUES ("${peerId}", "${key}")`); console.log('[LOG] Stored public key of ' + peerId); } @@ -114,8 +136,8 @@ function storePublicKey(peerId, key) { */ function testEncryption() { generateKeys('test_id', 'supersecure').then(() => { - encrypt('The meaning of life', localStorage.getItem('public_key')).then(() => { - decrypt(encrypted, localStorage.getItem('public_key'), localStorage.getItem('private_key'), 'supersecure').then(() => { + encrypt('The meaning of life', getPublicKey()).then(() => { + decrypt(encrypted, getPublicKey(), getPrivateKey(), 'supersecure').then(() => { if (decrypted === 'The meaning of life') console.log("YEEHA, Test succeeded!") }) @@ -123,6 +145,7 @@ function testEncryption() { }) } +exports.setup = setupDatabase; exports.generate = generateKeys; exports.getPrivate = getPrivateKey; exports.getPublic = getPublicKey; diff --git a/public/scripts/chat.js b/public/scripts/chat.js index a31173e..ec27a11 100644 --- a/public/scripts/chat.js +++ b/public/scripts/chat.js @@ -2,12 +2,12 @@ const $ = require('jquery'); const encryption = require('./2_encryption'); const nanoid = require('nanoid'); -let connectedPeers = []; +let connectedPeers = []; // TODO: Save new peers in array let connectedPeer; const peerId = nanoid(); // setup encryption -if (encryption.check()) { +if (encryption.setup() && encryption.check()) { // TODO: Ask for password chat(); } else { -- cgit v1.2.3