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 f0af037a141a662e46f512403c8d18ccc7a10333
parent 052e10b173b29fb089acd5c8ce439e87a89708b0
Author: lash <dev@holbrook.no>
Date:   Thu, 22 Sep 2022 07:56:20 +0000

Factor out pubkey encryption code

Diffstat:
Mindex.html | 56++++++++++++++++++++++++++++++++++++--------------------
1 file changed, 36 insertions(+), 20 deletions(-)

diff --git a/index.html b/index.html @@ -264,16 +264,34 @@ let g_counter = undefined; }); let r_enc = await encryptMessage(msg_sig, pfx); - let rcpt = await dispatchMessage(r_enc, pfx); + let rcpt = await dispatchToEndpoint(r_enc, pfx); stateChange(rcpt, STATE['ACK_MESSAGE']); stateChange('message submit complete', undefined, STATE['ACK_MESSAGE']); g_counter += 1; - stateChange('update local state, next message is: ' + g_counter); localStorage.setItem('msg-count', g_counter); + stateChange('update local state, next message is: ' + g_counter); + + const r_enc_pub = await encryptPublicKey(g_local_key); + let rcpt_pubkey = await dispatchToEndpoint(r_enc_pub, PUBKEY_PFX + g_remote_key.getFingerprint()); +// +// res = await fetch(g_data_endpoint + '/' + PUBKEY_PFX + g_remote_key.getFingerprint(), { +// method: 'PUT', +// body: enc_pubkey, +// headers: { +// 'Content-Type': 'application/octet-stream', +// 'Authorization': 'PUBSIG ' + pubkey_auth, +// } +// }); + +// rcpt_pubkey = await res.text(); + stateChange(rcpt_pubkey, STATE['ACK_PUBKEY']); + stateChange('publickey submit complete', undefined, STATE['ACK_PUBKEY']); + return rcpt; + } - stateChange('sign and encode public key store request'); + async function encryptPublicKey(k) { const pubkey_bin = g_local_key.toPublic().write(); const msg_pubkey = await openpgp.createMessage({ binary: pubkey_bin, @@ -288,23 +306,16 @@ let g_counter = undefined; binary: enc_pubkey, }); - const pubkey_auth = await generateAuth(g_local_key, envelope_pubkey); - res = await fetch(g_data_endpoint + '/' + PUBKEY_PFX + g_remote_key.getFingerprint(), { - method: 'PUT', - body: enc_pubkey, - headers: { - 'Content-Type': 'application/octet-stream', - 'Authorization': 'PUBSIG ' + pubkey_auth, - } - }); + const auth = await generateAuth(g_local_key, envelope_pubkey); - rcpt_pubkey = await res.text(); - stateChange(rcpt_pubkey, STATE['ACK_PUBKEY']); - stateChange('publickey submit complete', undefined, STATE['ACK_PUBKEY']); - return rcpt; + return { + msg: enc_pubkey, + auth: auth, + rcpt: null, + }; } - async function dispatchMessage(o, pfx) { + async function dispatchToEndpoint(o, pfx) { let res = await fetch(g_data_endpoint + '/' + pfx, { method: 'PUT', body: o.msg, @@ -313,10 +324,15 @@ let g_counter = undefined; 'Authorization': 'PUBSIG ' + o.auth, } }); - + rcpt_remote = await res.text(); - if (rcpt_remote.toLowerCase() != o.rcpt.toLowerCase()) { - throw "mutable ref mismatch between local and server; " + o.rcpt + " != " + rcpt_remote; + + if (o.rcpt) { + if (rcpt_remote.toLowerCase() != o.rcpt.toLowerCase()) { + throw "mutable ref mismatch between local and server; " + o.rcpt + " != " + rcpt_remote; + } + } else { + console.warn('have no digest to check server reply against'); } return rcpt_remote; }