boho 2.1.3 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -19
- package/dist/boho.d.ts +29 -20
- package/dist/boho.js +3 -3
- package/dist/boho.js.map +1 -1
- package/dist/boho.min.js +3 -3
- package/dist/boho.min.js.map +1 -1
- package/package.json +11 -4
- package/src/boho.js +128 -103
- package/test/AUTH_process.js +4 -4
- package/test/arduino/host.cpp +51 -0
- package/test/arduino/interop.test.js +89 -0
- package/test/arduino/shims/Arduino.h +8 -0
- package/test/arduino/shims/Crypto.h +1 -0
- package/test/arduino/shims/SHA256.h +46 -0
- package/test/fixtures/legacy-pack.json +5 -0
- package/test/iosignal/boho-loader.js +4 -0
- package/test/iosignal/full-flow.test.js +97 -0
- package/test/iosignal/host.cpp +62 -0
- package/test/iosignal/register.js +2 -0
- package/test/iosignal/shims/Arduino.h +18 -0
- package/test/iosignal/shims/Client.h +9 -0
- package/test/security-regressions.js +196 -0
- package/test/types/api.mts +17 -0
- package/test/types/tsconfig.json +11 -0
- package/test-browser/index.html +4 -4
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { it } from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import { EventEmitter } from 'node:events'
|
|
4
|
+
import { spawn, spawnSync } from 'node:child_process'
|
|
5
|
+
import { mkdtempSync, rmSync } from 'node:fs'
|
|
6
|
+
import { tmpdir } from 'node:os'
|
|
7
|
+
import { join, resolve } from 'node:path'
|
|
8
|
+
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
9
|
+
import readline from 'node:readline'
|
|
10
|
+
import PatchedBoho from '../../dist/boho.js'
|
|
11
|
+
|
|
12
|
+
const dir = fileURLToPath(new URL('.', import.meta.url))
|
|
13
|
+
const iosignal = process.env.IOSIGNAL_PATH || resolve(dir, '../../../iosignal')
|
|
14
|
+
const arduino = process.env.IOSIGNAL_ARDUINO_PATH || resolve(dir, '../../../iosignal-arduino')
|
|
15
|
+
const load = path => import(pathToFileURL(join(iosignal, path)))
|
|
16
|
+
const {Manager} = await load('src/server/Manager.js')
|
|
17
|
+
const {BohoAuth} = await load('src/auth/BohoAuth.js')
|
|
18
|
+
const {IOCongSocket} = await load('src/client/IOCongSocket.js')
|
|
19
|
+
const {CongRx} = await load('src/client/CongPacket.js')
|
|
20
|
+
const {STATE} = await load('src/common/constants.js')
|
|
21
|
+
|
|
22
|
+
it('Arduino IOSignal (80-byte RX) ↔ real iosignal server ↔ JS client: auth, normal, E2EE', {timeout:30000}, async () => {
|
|
23
|
+
const build=mkdtempSync(join(tmpdir(),'iosignal-full-flow-'))
|
|
24
|
+
const executable=join(build,'peer')
|
|
25
|
+
let child, lines, io, manager
|
|
26
|
+
try {
|
|
27
|
+
const args=['-std=c++11','-ffunction-sections','-fdata-sections','-Wno-deprecated-declarations',
|
|
28
|
+
'-I',join(dir,'shims'),'-I',join(dir,'../arduino/shims'),'-I',join(arduino,'src'),
|
|
29
|
+
join(dir,'host.cpp'),...['Boho.cpp','CongPacket.cpp','IOSignal.cpp'].map(f=>join(arduino,'src',f)),
|
|
30
|
+
'-o',executable]
|
|
31
|
+
args.push(process.platform==='darwin'?'-Wl,-dead_strip':'-Wl,--gc-sections')
|
|
32
|
+
if(process.platform!=='darwin')args.push('-lcrypto')
|
|
33
|
+
const compiled=spawnSync('c++',args,{encoding:'utf8',timeout:20000})
|
|
34
|
+
assert.equal(compiled.status,0,compiled.stderr||String(compiled.error))
|
|
35
|
+
child=spawn(executable,[],{stdio:['pipe','pipe','inherit']})
|
|
36
|
+
lines=readline.createInterface({input:child.stdout})[Symbol.asyncIterator]()
|
|
37
|
+
const ask=async command=>{
|
|
38
|
+
const line=lines.next();child.stdin.write(command+'\n');const value=await line
|
|
39
|
+
assert.equal(value.done,false);return value.value
|
|
40
|
+
}
|
|
41
|
+
class Socket extends EventEmitter {
|
|
42
|
+
constructor(){super();this.socketType='cong';this.readyState='open';this.remoteAddress='127.0.0.1';this.pending=[]}
|
|
43
|
+
write(data){this.pending.push(Buffer.from(data))}
|
|
44
|
+
end(){this.readyState='closed'} destroy(){this.end()}
|
|
45
|
+
}
|
|
46
|
+
const server=new EventEmitter();server.serviceNames=new Set()
|
|
47
|
+
const auth=new BohoAuth({getAuth:async id=>id==='arduino'?{key:'arduino-key',cid:'arduino',level:1}:
|
|
48
|
+
id==='jsclient'?{key:'js-key',cid:'js',level:1}:null})
|
|
49
|
+
manager=new Manager(server,auth)
|
|
50
|
+
const aSocket=new Socket(),jsSocket=new Socket(),jsOutbound=[]
|
|
51
|
+
io=new IOCongSocket();io.autoReconnect=false;io.auth('jsclient','js-key')
|
|
52
|
+
io.socket={readyState:'open',write:data=>jsOutbound.push(Buffer.from(data))}
|
|
53
|
+
const jsRx=new CongRx();jsRx.on('data',data=>io.onTCPSocketMessage(data))
|
|
54
|
+
manager.addRemote(aSocket,{});manager.addRemote(jsSocket,{})
|
|
55
|
+
assert.ok(io.boho instanceof PatchedBoho, 'JS client must use the working Boho build')
|
|
56
|
+
for (const remote of manager.remotes) assert.ok(remote.boho instanceof PatchedBoho, 'Server must use the working Boho build')
|
|
57
|
+
const pump=async()=>{
|
|
58
|
+
for(let pass=0;pass<30;pass++) {
|
|
59
|
+
let activity=false
|
|
60
|
+
while(aSocket.pending.length){activity=true;const frame=aSocket.pending.shift()
|
|
61
|
+
// Exercise actual Arduino CongPacket parsing with split headers/body.
|
|
62
|
+
for(const piece of [frame.subarray(0,1),frame.subarray(1,3),frame.subarray(3)]) {
|
|
63
|
+
if(!piece.length)continue
|
|
64
|
+
const returned=await ask('FEED '+piece.toString('hex'))
|
|
65
|
+
if(returned)aSocket.emit('data',Buffer.from(returned,'hex'))
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
while(jsSocket.pending.length){activity=true;jsRx.write(jsSocket.pending.shift())}
|
|
69
|
+
while(jsOutbound.length){activity=true;const frame=jsOutbound.shift();jsSocket.emit('data',frame.subarray(0,1));jsSocket.emit('data',frame.subarray(1))}
|
|
70
|
+
await new Promise(r=>setImmediate(r))
|
|
71
|
+
if(!activity&&!aSocket.pending.length&&!jsSocket.pending.length&&!jsOutbound.length)return
|
|
72
|
+
}
|
|
73
|
+
assert.fail('Message pump did not settle')
|
|
74
|
+
}
|
|
75
|
+
await pump()
|
|
76
|
+
assert.equal(await ask('STATE'),'19:1')
|
|
77
|
+
assert.equal(io.state,STATE.READY)
|
|
78
|
+
assert.equal(manager.cid2remote.size,2)
|
|
79
|
+
const received=[];io.on('@',(tag,data)=>received.push({tag,data:Buffer.from(data)}))
|
|
80
|
+
aSocket.emit('data',Buffer.from(await ask('SIGNAL js@t 68656c6c6f'),'hex'));await pump()
|
|
81
|
+
assert.equal(received.pop().data.toString(),'hello')
|
|
82
|
+
io.signal('arduino@t',Buffer.from('world'));await pump()
|
|
83
|
+
assert.equal(await ask('MESSAGES'),'@t:776f726c64;')
|
|
84
|
+
aSocket.emit('data',Buffer.from(await ask('E2E js@t 736563726574'),'hex'));await pump()
|
|
85
|
+
const secret=received.pop();assert.equal(secret.data[0],0xb6)
|
|
86
|
+
assert.equal(io.decrypt_e2e(secret.data,'interop-e2e-key').data.toString(),'secret')
|
|
87
|
+
assert.equal(io.decrypt_e2e(secret.data,'wrong-key'),undefined)
|
|
88
|
+
io.signal_e2e('arduino@t',Buffer.from('reply'),'interop-e2e-key');await pump()
|
|
89
|
+
assert.equal(await ask('MESSAGES'),'@t:7265706c79;')
|
|
90
|
+
} finally {
|
|
91
|
+
if(io){io.socket=null;io.destroy()}
|
|
92
|
+
if(manager){clearInterval(manager.pingIntervalID);clearInterval(manager.monitIntervalID);clearInterval(manager.metrics.tickId)}
|
|
93
|
+
if(child){child.stdin.end();child.kill()}
|
|
94
|
+
if(lines)await lines.return()
|
|
95
|
+
rmSync(build,{recursive:true,force:true})
|
|
96
|
+
}
|
|
97
|
+
})
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
#include <iostream>
|
|
2
|
+
#include <sstream>
|
|
3
|
+
#include <vector>
|
|
4
|
+
#include <deque>
|
|
5
|
+
#include <string>
|
|
6
|
+
#include "IOSignal.h"
|
|
7
|
+
uint32_t clockMs = 1234;
|
|
8
|
+
uint32_t millis() { return clockMs; }
|
|
9
|
+
uint32_t micros() { return clockMs * 1000; }
|
|
10
|
+
void delay(unsigned long ms) { clockMs += ms; }
|
|
11
|
+
TestSerial Serial;
|
|
12
|
+
class MemoryClient : public Client {
|
|
13
|
+
public:
|
|
14
|
+
std::deque<uint8_t> incoming;
|
|
15
|
+
std::vector<uint8_t> outgoing;
|
|
16
|
+
bool opened = true;
|
|
17
|
+
int available() override { return incoming.size(); }
|
|
18
|
+
int read() override { if (incoming.empty()) return -1; auto b=incoming.front(); incoming.pop_front(); return b; }
|
|
19
|
+
size_t readBytes(uint8_t* output, size_t count) override {
|
|
20
|
+
size_t used=0; while (used<count && available()) output[used++]=read(); return used;
|
|
21
|
+
}
|
|
22
|
+
size_t write(const uint8_t* data, size_t size) override { outgoing.insert(outgoing.end(),data,data+size); return size; }
|
|
23
|
+
int connected() override { return opened; }
|
|
24
|
+
int connect(const char*, uint16_t) override { opened=true; return 1; }
|
|
25
|
+
void stop() override { opened=false; }
|
|
26
|
+
void flush() override {}
|
|
27
|
+
};
|
|
28
|
+
MemoryClient client;
|
|
29
|
+
IOSignal io;
|
|
30
|
+
std::vector<std::string> received;
|
|
31
|
+
std::string hex(const uint8_t* data, size_t size) {
|
|
32
|
+
const char* digits="0123456789abcdef"; std::string out;
|
|
33
|
+
for (size_t i=0;i<size;i++) {out+=digits[data[i]>>4];out+=digits[data[i]&15];} return out;
|
|
34
|
+
}
|
|
35
|
+
std::vector<uint8_t> unhex(const std::string& text) {
|
|
36
|
+
std::vector<uint8_t> out; for(size_t i=0;i<text.size();i+=2)out.push_back(std::stoul(text.substr(i,2),nullptr,16));return out;
|
|
37
|
+
}
|
|
38
|
+
void onMessage(char* tag, uint8_t, uint8_t* data, size_t size) {
|
|
39
|
+
if (size && data[0]==Boho::ENC_PACK) {
|
|
40
|
+
std::vector<uint8_t> plain(size);
|
|
41
|
+
auto length=io.decrypt_e2e(plain.data(),data,size,"interop-e2e-key");
|
|
42
|
+
received.push_back(std::string(tag)+":"+hex(plain.data(),length));
|
|
43
|
+
} else received.push_back(std::string(tag)+":"+hex(data,size));
|
|
44
|
+
}
|
|
45
|
+
int main() {
|
|
46
|
+
io.setRxBuffer(80); io.auth("arduino","arduino-key"); io.begin(&client,"localhost",55488);io.onMessage(onMessage);
|
|
47
|
+
std::string line;
|
|
48
|
+
while(std::getline(std::cin,line)) {
|
|
49
|
+
std::istringstream input(line);std::string command,arg,body;input>>command>>arg>>body;
|
|
50
|
+
if(command=="FEED") {
|
|
51
|
+
auto data=unhex(arg);client.incoming.insert(client.incoming.end(),data.begin(),data.end());
|
|
52
|
+
while(client.available()) {auto before=client.available();io.loop();if(client.available()==before)break;}
|
|
53
|
+
} else if(command=="SIGNAL" || command=="E2E") {
|
|
54
|
+
auto data=unhex(body);
|
|
55
|
+
if(command=="SIGNAL")io.signal(arg.c_str(),data.data(),data.size());
|
|
56
|
+
else io.signal_e2e(arg.c_str(),data.data(),data.size(),"interop-e2e-key");
|
|
57
|
+
} else if(command=="STATE") {std::cout<<unsigned(io.state)<<":"<<io.isAuthorized<<std::endl;continue;}
|
|
58
|
+
else if(command=="MESSAGES") { for(auto& entry:received)std::cout<<entry<<";";std::cout<<std::endl;received.clear();continue; }
|
|
59
|
+
else return 3;
|
|
60
|
+
std::cout<<hex(client.outgoing.data(),client.outgoing.size())<<std::endl;client.outgoing.clear();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
#include <cstdint>
|
|
3
|
+
#include <cstdlib>
|
|
4
|
+
#include <cstdio>
|
|
5
|
+
#include <cstddef>
|
|
6
|
+
uint32_t millis();
|
|
7
|
+
uint32_t micros();
|
|
8
|
+
void delay(unsigned long);
|
|
9
|
+
struct TestSerial { void write(const char*) {} };
|
|
10
|
+
extern TestSerial Serial;
|
|
11
|
+
class Stream {
|
|
12
|
+
public:
|
|
13
|
+
virtual ~Stream() = default;
|
|
14
|
+
virtual int available() = 0;
|
|
15
|
+
virtual int read() = 0;
|
|
16
|
+
virtual size_t readBytes(uint8_t*, size_t) = 0;
|
|
17
|
+
virtual size_t write(const uint8_t*, size_t) = 0;
|
|
18
|
+
};
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { describe, it } from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import { readFileSync } from 'node:fs'
|
|
4
|
+
import vm from 'node:vm'
|
|
5
|
+
import Source from '../src/index.js'
|
|
6
|
+
import Distribution from '../dist/boho.js'
|
|
7
|
+
|
|
8
|
+
const browser = { crypto: globalThis.crypto, TextEncoder, TextDecoder, Uint8Array, ArrayBuffer }
|
|
9
|
+
vm.runInNewContext(readFileSync(new URL('../dist/boho.min.js', import.meta.url), 'utf8'), browser)
|
|
10
|
+
const fixture = JSON.parse(readFileSync(new URL('./fixtures/legacy-pack.json', import.meta.url)))
|
|
11
|
+
|
|
12
|
+
for (const [name, B] of [['source', Source], ['ESM', Distribution], ['browser UMD', browser.Boho]]) {
|
|
13
|
+
const bytes = value => B.Buffer.from(value)
|
|
14
|
+
function pair() {
|
|
15
|
+
const client = new B(), server = new B()
|
|
16
|
+
client.set_id8('test-id'); client.set_key('shared-test-key')
|
|
17
|
+
const request = client.auth_req(server.server_time_nonce())
|
|
18
|
+
server.set_key('shared-test-key')
|
|
19
|
+
assert.equal(client.verify_auth_res(server.verify_auth_req(request)), true)
|
|
20
|
+
return { client, server, request }
|
|
21
|
+
}
|
|
22
|
+
describe(`${name}: security regressions`, () => {
|
|
23
|
+
it('rejects missing, cleared, empty and incorrectly sized keys', () => {
|
|
24
|
+
const b = new B()
|
|
25
|
+
assert.throws(() => b.encryptPack('hello'), /key/i)
|
|
26
|
+
assert.equal(b.auth_req(new B().server_time_nonce()), false)
|
|
27
|
+
assert.throws(() => b.set_key(''), /empty/i)
|
|
28
|
+
assert.throws(() => b.copy_key(bytes([1])), /32/)
|
|
29
|
+
b.set_key('key'); b.clearAuth()
|
|
30
|
+
assert.throws(() => b.encryptPack('hello'), /key/i)
|
|
31
|
+
assert.equal(b.isAuthorized, false)
|
|
32
|
+
})
|
|
33
|
+
it('decrypts legacy 2.1.3 packet bytes', () => {
|
|
34
|
+
const b = new B(); b.set_key(fixture.key)
|
|
35
|
+
assert.equal(b.decryptPack(B.Buffer.from(fixture.packet, 'hex')).data.toString(), fixture.plaintext)
|
|
36
|
+
})
|
|
37
|
+
it('round trips empty and multi-block binary messages', () => {
|
|
38
|
+
const b = new B(); b.set_key('key')
|
|
39
|
+
for (const size of [0, 1, 31, 32, 33, 1024]) {
|
|
40
|
+
const plain = B.Buffer.alloc(size, 0xa5)
|
|
41
|
+
const packet = b.encryptPack(plain)
|
|
42
|
+
assert.equal(packet.length, size + B.MetaSize.ENC_PACK)
|
|
43
|
+
assert.deepEqual(Array.from(b.decryptPack(new Uint8Array(packet)).data), Array.from(plain))
|
|
44
|
+
}
|
|
45
|
+
const {client, server} = pair()
|
|
46
|
+
assert.equal(server.decrypt_488(client.encrypt_488('')).length, 0)
|
|
47
|
+
})
|
|
48
|
+
it('rejects all truncated lengths, bad types, lengths and trailing bytes', () => {
|
|
49
|
+
const b = new B(); b.set_key('key')
|
|
50
|
+
const packet = b.encryptPack('test message')
|
|
51
|
+
for (let length = 0; length < packet.length; length++) {
|
|
52
|
+
assert.equal(b.decryptPack(packet.subarray(0, length)), undefined)
|
|
53
|
+
}
|
|
54
|
+
for (const invalid of [undefined, null, {}, 'text', [], bytes([B.BohoMsg.ENC_PACK])]) {
|
|
55
|
+
assert.equal(b.decryptPack(invalid), undefined)
|
|
56
|
+
}
|
|
57
|
+
const wrongType = bytes(packet); wrongType[0] = B.BohoMsg.ENC_488
|
|
58
|
+
const wrongLength = bytes(packet); wrongLength.writeUInt32LE(0, 1)
|
|
59
|
+
for (const invalid of [wrongType, wrongLength, B.Buffer.concat([packet, bytes([0])])]) {
|
|
60
|
+
assert.equal(b.decryptPack(invalid), undefined)
|
|
61
|
+
}
|
|
62
|
+
})
|
|
63
|
+
it('rejects tampering in every byte of a normal packet', () => {
|
|
64
|
+
const b = new B(); b.set_key('key')
|
|
65
|
+
const packet = b.encryptPack('payload crossing a thirty two byte boundary')
|
|
66
|
+
for (let i = 0; i < packet.length; i++) {
|
|
67
|
+
const modified = bytes(packet); modified[i] ^= 1
|
|
68
|
+
assert.equal(b.decryptPack(modified), undefined, `byte ${i}`)
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
it('rejects wrong keys', () => {
|
|
72
|
+
const b = new B(), other = new B(); b.set_key('key'); other.set_key('other')
|
|
73
|
+
assert.equal(other.decryptPack(b.encryptPack('secret')), undefined)
|
|
74
|
+
})
|
|
75
|
+
it('requires authorization on both 488 entry points', () => {
|
|
76
|
+
const {client, server} = pair(), packet = client.encrypt_488('hello')
|
|
77
|
+
server.isAuthorized = false
|
|
78
|
+
assert.equal(server.decrypt_488(packet), undefined)
|
|
79
|
+
assert.equal(server.encrypt_488('hello'), undefined)
|
|
80
|
+
})
|
|
81
|
+
it('rejects malformed 488 packets without consuming replay state', () => {
|
|
82
|
+
const {client, server} = pair(), packet = client.encrypt_488('hello')
|
|
83
|
+
for (let length = 0; length < packet.length; length++) {
|
|
84
|
+
assert.equal(server.decrypt_488(packet.subarray(0, length)), undefined)
|
|
85
|
+
}
|
|
86
|
+
for (let i = 0; i < packet.length; i++) {
|
|
87
|
+
const modified = bytes(packet); modified[i] ^= 1
|
|
88
|
+
assert.equal(server.decrypt_488(modified), undefined)
|
|
89
|
+
}
|
|
90
|
+
assert.equal(server.decrypt_488(B.Buffer.concat([packet, bytes([0])])), undefined)
|
|
91
|
+
assert.equal(server.decrypt_488(packet).toString(), 'hello')
|
|
92
|
+
})
|
|
93
|
+
it('accepts iosignal E2E routing headers without decrypting the opaque tail', () => {
|
|
94
|
+
const {client, server} = pair()
|
|
95
|
+
const routingHeader = bytes([0x42, 1, 120, 0])
|
|
96
|
+
const encryptedHeader = client.encrypt_488(routingHeader)
|
|
97
|
+
encryptedHeader[0] = B.BohoMsg.ENC_E2E
|
|
98
|
+
const opaqueBody = client.encrypt_e2e('body', 'separate-key')
|
|
99
|
+
const wire = B.Buffer.concat([encryptedHeader, opaqueBody])
|
|
100
|
+
const originalWire = bytes(wire)
|
|
101
|
+
const truncated = wire.subarray(0, B.MetaSize.ENC_488 - 1)
|
|
102
|
+
assert.equal(server.decrypt_488(truncated), undefined)
|
|
103
|
+
const badLength = bytes(wire); badLength.writeUInt32LE(wire.length, 1)
|
|
104
|
+
assert.equal(server.decrypt_488(badLength), undefined)
|
|
105
|
+
const badTag = bytes(wire); badTag[13] ^= 1
|
|
106
|
+
assert.equal(server.decrypt_488(badTag), undefined)
|
|
107
|
+
assert.deepEqual(Array.from(server.decrypt_488(wire)), Array.from(routingHeader))
|
|
108
|
+
assert.deepEqual(Array.from(wire), Array.from(originalWire))
|
|
109
|
+
assert.deepEqual(Array.from(server.decrypt_488(wire)), Array.from(routingHeader))
|
|
110
|
+
assert.equal(server.decrypt_e2e(opaqueBody, 'separate-key').data.toString(), 'body')
|
|
111
|
+
})
|
|
112
|
+
it('leaves replay and delivery-order policy to iosignal', () => {
|
|
113
|
+
const {client, server} = pair()
|
|
114
|
+
const first = client.encrypt_488('first'), second = client.encrypt_488('second')
|
|
115
|
+
assert.equal(server.decrypt_488(second).toString(), 'second')
|
|
116
|
+
assert.equal(server.decrypt_488(first).toString(), 'first')
|
|
117
|
+
assert.equal(server.decrypt_488(second).toString(), 'second')
|
|
118
|
+
})
|
|
119
|
+
it('leaves authentication request reuse policy to iosignal', () => {
|
|
120
|
+
const {server, request, client} = pair()
|
|
121
|
+
server.set_key('shared-test-key')
|
|
122
|
+
const response = server.verify_auth_req(request)
|
|
123
|
+
assert.ok(response)
|
|
124
|
+
assert.equal(client.verify_auth_res(response), true)
|
|
125
|
+
assert.equal(server.isAuthorized, true)
|
|
126
|
+
})
|
|
127
|
+
it('rejects malformed auth messages and full-length forged responses', () => {
|
|
128
|
+
const c = new B(), s = new B(); c.set_key('key'); s.set_key('key')
|
|
129
|
+
const challenge = s.server_time_nonce()
|
|
130
|
+
const wrongChallenge = bytes(challenge); wrongChallenge[0] ^= 1
|
|
131
|
+
assert.equal(c.auth_req(wrongChallenge), false)
|
|
132
|
+
assert.equal(c.auth_req(challenge.subarray(0, 12)), false)
|
|
133
|
+
const request = c.auth_req(challenge)
|
|
134
|
+
for (const bad of [null, {}, request.subarray(0, 44), B.Buffer.concat([request, bytes([0])])]) {
|
|
135
|
+
assert.equal(s.verify_auth_req(bad), false)
|
|
136
|
+
}
|
|
137
|
+
const wrongRequest = bytes(request); wrongRequest[0] ^= 1
|
|
138
|
+
assert.equal(s.verify_auth_req(wrongRequest), false)
|
|
139
|
+
const response = s.verify_auth_req(request)
|
|
140
|
+
const wrongResponse = bytes(response); wrongResponse[1] ^= 1
|
|
141
|
+
assert.ok(!c.verify_auth_res(wrongResponse)); assert.equal(c.isAuthorized, false)
|
|
142
|
+
const wrongType = bytes(response); wrongType[0] ^= 1
|
|
143
|
+
assert.ok(!c.verify_auth_res(wrongType))
|
|
144
|
+
assert.equal(c.verify_auth_res(response), true)
|
|
145
|
+
})
|
|
146
|
+
it('preserves authorization when iosignal reloads the connection key', () => {
|
|
147
|
+
const {client, server} = pair()
|
|
148
|
+
client.set_key('shared-test-key')
|
|
149
|
+
assert.equal(client.isAuthorized, true)
|
|
150
|
+
assert.equal(server.decrypt_488(client.encrypt_488('reloaded')).toString(), 'reloaded')
|
|
151
|
+
server.copy_key(B.sha256.hash('shared-test-key'))
|
|
152
|
+
assert.equal(server.isAuthorized, true)
|
|
153
|
+
assert.equal(client.decrypt_488(server.encrypt_488('reply')).toString(), 'reply')
|
|
154
|
+
// Caller starts reauthentication explicitly, just as it does on reconnect.
|
|
155
|
+
client.isAuthorized = false; server.isAuthorized = false
|
|
156
|
+
const request = client.auth_req(server.server_time_nonce())
|
|
157
|
+
assert.equal(client.verify_auth_res(server.verify_auth_req(request)), true)
|
|
158
|
+
})
|
|
159
|
+
it('restores E2E state on success, rejection and thrown errors', () => {
|
|
160
|
+
const {client, server} = pair()
|
|
161
|
+
const before = Array.from(client._otpSrc44)
|
|
162
|
+
const packet = client.encrypt_e2e('e2e payload', 'e2e key')
|
|
163
|
+
assert.equal(client.decrypt_e2e(packet, 'e2e key').data.toString(), 'e2e payload')
|
|
164
|
+
assert.equal(client.decrypt_e2e(bytes([B.BohoMsg.ENC_PACK]), 'e2e key'), undefined)
|
|
165
|
+
const original = client.decryptPack
|
|
166
|
+
client.decryptPack = () => { throw new Error('forced failure') }
|
|
167
|
+
assert.throws(() => client.decrypt_e2e(packet, 'e2e key'), /forced failure/)
|
|
168
|
+
client.decryptPack = original
|
|
169
|
+
assert.deepEqual(Array.from(client._otpSrc44), before)
|
|
170
|
+
assert.equal(client.isAuthorized, true)
|
|
171
|
+
assert.equal(server.decrypt_488(client.encrypt_488('still works')).toString(), 'still works')
|
|
172
|
+
const keyless = new B(); keyless.encrypt_e2e('hello', 'temporary')
|
|
173
|
+
assert.throws(() => keyless.encryptPack('hello'), /key/i)
|
|
174
|
+
})
|
|
175
|
+
it('keeps sending clocks monotonic through rollback and counter wrap', () => {
|
|
176
|
+
const b = new B(); b.set_key('key')
|
|
177
|
+
b._lastSendTime = Date.now() + 10000; b.counter = 65535
|
|
178
|
+
const lastTime = b._lastSendTime
|
|
179
|
+
b.set_clock_nonce(bytes([1, 2, 3, 4]))
|
|
180
|
+
assert.equal(b._lastSendTime, lastTime + 1)
|
|
181
|
+
assert.equal(b.counter, 0)
|
|
182
|
+
const before = b._lastSendTime
|
|
183
|
+
b.set_clock_nonce(bytes([1, 2, 3, 4]))
|
|
184
|
+
assert.equal(b._lastSendTime, before)
|
|
185
|
+
assert.equal(b.counter, 1)
|
|
186
|
+
})
|
|
187
|
+
})
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
it('both distribution source maps include the installed meta-buffer-pack build', () => {
|
|
191
|
+
const installed = readFileSync(new URL('../node_modules/meta-buffer-pack/dist/meta-buffer-pack.js', import.meta.url), 'utf8')
|
|
192
|
+
for (const file of ['boho.js.map', 'boho.min.js.map']) {
|
|
193
|
+
const map = JSON.parse(readFileSync(new URL(`../dist/${file}`, import.meta.url)))
|
|
194
|
+
assert.ok(map.sourcesContent.includes(installed), file)
|
|
195
|
+
}
|
|
196
|
+
})
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import Boho, { type MetaSizeType } from 'boho'
|
|
2
|
+
// @ts-expect-error Runtime exports only the default class.
|
|
3
|
+
import { RAND } from 'boho'
|
|
4
|
+
|
|
5
|
+
const boho = new Boho()
|
|
6
|
+
boho.set_key(Boho.RAND(32))
|
|
7
|
+
const packet = boho.encryptPack('hello')
|
|
8
|
+
const clear: Uint8Array | undefined = boho.decryptPack(packet)?.data
|
|
9
|
+
const sizes: MetaSizeType = Boho.MetaSize
|
|
10
|
+
const challengeSize: number = sizes.SERVER_TIME_NONCE
|
|
11
|
+
const request = boho.auth_req(new Uint8Array(challengeSize))
|
|
12
|
+
if (request) boho.verify_auth_req(request)
|
|
13
|
+
boho.encrypt_e2e('hello', 'key')
|
|
14
|
+
Boho.sha256.hmac(Boho.RAND(32), packet)
|
|
15
|
+
Boho.Buffer.from('hello')
|
|
16
|
+
Boho.MBP.unpack(packet, Boho.Meta.ENC_PACK)
|
|
17
|
+
void clear
|
package/test-browser/index.html
CHANGED
|
@@ -85,12 +85,12 @@
|
|
|
85
85
|
s.set_key(key) // if correct.
|
|
86
86
|
const auth_res_buffer = s.verify_auth_req(unpack)
|
|
87
87
|
|
|
88
|
-
const auth_res_buffer_with_incorrect_hmac = Boho.Buffer.alloc(
|
|
89
|
-
const isCorrectSeverHMAC = c.verify_auth_res(auth_res_buffer)
|
|
88
|
+
const auth_res_buffer_with_incorrect_hmac = Boho.Buffer.alloc(MetaSize.AUTH_RES)
|
|
90
89
|
|
|
91
90
|
auth_res_buffer.copy(auth_res_buffer_with_incorrect_hmac)
|
|
92
91
|
auth_res_buffer_with_incorrect_hmac[2] ^= 0x55 // change hmac
|
|
93
92
|
const wrongSeverHMACResult = c.verify_auth_res(auth_res_buffer_with_incorrect_hmac)
|
|
93
|
+
const isCorrectSeverHMAC = c.verify_auth_res(auth_res_buffer)
|
|
94
94
|
|
|
95
95
|
|
|
96
96
|
describe('1. server send SERVER_TIME_NONCE', function () {
|
|
@@ -99,7 +99,7 @@
|
|
|
99
99
|
})
|
|
100
100
|
|
|
101
101
|
it('should pack[0] is header type ', function () {
|
|
102
|
-
assert.
|
|
102
|
+
assert.equal(auth_nonce_buffer[0], BohoMsg.SERVER_TIME_NONCE)
|
|
103
103
|
})
|
|
104
104
|
})
|
|
105
105
|
|
|
@@ -109,7 +109,7 @@
|
|
|
109
109
|
})
|
|
110
110
|
|
|
111
111
|
it('should pack[0] is header type ', function () {
|
|
112
|
-
assert.
|
|
112
|
+
assert.equal(auth_req_buffer[0], BohoMsg.AUTH_REQ)
|
|
113
113
|
})
|
|
114
114
|
})
|
|
115
115
|
|