forro

Forro is a end-to-end encrypted contract form based on PGP.
git clone git://git.defalsify.org/forro.git
Log | Files | Refs | LICENSE

commit 209519cd0f3c71d677c993aa6f661ee1c8be10e2
parent c429eb632c9a94235ff72e2a9c74b2ca8cf22476
Author: lash <dev@holbrook.no>
Date:   Thu, 22 Sep 2022 08:54:45 +0000

Store counter on endpoint

Diffstat:
Mindex.html | 52+++++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 47 insertions(+), 5 deletions(-)

diff --git a/index.html b/index.html @@ -19,7 +19,9 @@ const STATE = { ENC_MESSAGE: 1 << 13, ACK_PUBKEY: 1 << 14, ENC_PUBKEY: 1 << 15, - HELP: 1 << 16, + ACK_COUNTER: 1 << 16, + ENC_COUNTER: 1 << 17, + HELP: 1 << 18, }; const STATE_KEYS = Object.keys(STATE); @@ -63,6 +65,10 @@ let g_counter = undefined; return PUBKEY_PFX + g_remote_key.getFingerprint(); } + function counter_identifier() { + return 'msgidx'; + } + function debugState(state) { let s = ''; for (let i = 0; i < STATE_KEYS.length; i++) { @@ -213,13 +219,12 @@ let g_counter = undefined; let r = undefined; try { r = await dispatch(s, name, email) + stateChange('ready to send again', STATE['RTS']); } catch(e) { console.error(e); stateChange('send fail: ' + e, STATE['SEND_ERROR']); r = 'failed'; - // on fail the msg count will be wrong in error message } - stateChange('ready to send again', STATE['RTS']); return r; } @@ -248,6 +253,7 @@ let g_counter = undefined; let pfx = msg_identifier(); let pfx_pub = pubkey_identifier(); + let pfx_count = counter_identifier(); stateChange('sign and encrypt message ' + g_counter); const sha_raw = new jsSHA("SHA-256", "TEXT", { encoding: "UTF8" }); @@ -256,7 +262,8 @@ let g_counter = undefined; console.debug('digest for unencrypted message:', digest); // this is done twice, improve - const rcpt_pubkey_verify = await generatePointer(g_local_key, pfx); + const rcpt_pubkey_verify = await generatePointer(g_local_key, pfx_pub); + console.debug('pointer for pubkey', rcpt_pubkey_verify); const payload = "msg id: " + pfx + "\npubkey link: " + g_data_endpoint + "/" + rcpt_pubkey_verify + "\n\n" + s; const msg_sig = await signMessage(payload); @@ -267,6 +274,11 @@ let g_counter = undefined; let rcpt = await dispatchToEndpoint(r_enc, pfx); stateChange([g_counter, rcpt], STATE['ACK_MESSAGE']); + let r_count = await encryptCounter(g_counter, pfx_count); + stateChange([g_counter, r_count.rcpt], STATE['ENC_COUNTER']); + let rcpt_count = await dispatchToEndpoint(r_count, pfx_count); + stateChange([g_counter, rcpt_count], STATE['ACK_COUNTER']); + g_counter += 1; localStorage.setItem('msg-count', g_counter); @@ -301,6 +313,32 @@ let g_counter = undefined; return msg_sig; } + async function encryptCounter(c, pfx) { + const msg_count = await openpgp.createMessage({ + text: '' + g_counter, + }); + + const enc_count = await openpgp.encrypt({ + encryptionKeys: g_local_key, + format: 'binary', + message: msg_count, + }); + let envelope_count = await openpgp.createMessage({ + binary: enc_count, + }); + + const auth = await generateAuth(g_local_key, envelope_count); + + const rcpt_count_verify = await generatePointer(g_local_key, pfx); + + return { + msg: enc_count, + auth: auth, + rcpt: rcpt_count_verify, + }; + + } + async function encryptPublicKey(k, pfx) { const pubkey_bin = g_local_key.toPublic().write(); const msg_pubkey = await openpgp.createMessage({ @@ -454,7 +492,11 @@ let g_counter = undefined; window.addEventListener('messagestatechange', (v) => { state_change = (~v.detail.old_state) & v.detail.state; - console.debug('message state change:', [v.detail.s, v.detail.state, debugState(v.detail.state), state_change, debugState(state_change)]); + let s = v.detail.s; + if (Array.isArray(s)) { + s = '[' + s.join(', ') + ']'; + } + console.debug('message state change:', [s, v.detail.state, debugState(v.detail.state), state_change, debugState(state_change)]); });