(passcode)
| 315 | } |
| 316 | |
| 317 | async authenticate(passcode) { |
| 318 | const jpake = new ECJPAKE(passcode, 'client'); |
| 319 | |
| 320 | // 等待服务器选择协议 |
| 321 | let response = await this.recv(); |
| 322 | if (response[0] === GatewayClient.DATA_TYPE.SELECTED_PROTOCOL) { |
| 323 | const data = JSON.parse(response.slice(1).toString('utf8')); |
| 324 | console.log(`[*] Selected protocol: ${data.protocol}`); |
| 325 | if (data.protocol !== 'passcode') { |
| 326 | throw new Error(`Unsupported protocol: ${data.protocol}`); |
| 327 | } |
| 328 | } else { |
| 329 | throw new Error('Protocol selection failed'); |
| 330 | } |
| 331 | |
| 332 | // 发送 Round One |
| 333 | const roundOne = jpake.writeRoundOne(); |
| 334 | const msgR1 = Buffer.alloc(1 + roundOne.length); |
| 335 | msgR1[0] = GatewayClient.DATA_TYPE.ECJPAKE_ROUND_ONE; |
| 336 | roundOne.copy(msgR1, 1); |
| 337 | this.ws.send(msgR1); |
| 338 | console.log(`[*] Sent Round One (${roundOne.length} bytes)`); |
| 339 | |
| 340 | // 接收服务器 Round One |
| 341 | response = await this.recv(); |
| 342 | if (response[0] !== GatewayClient.DATA_TYPE.ECJPAKE_ROUND_ONE) { |
| 343 | throw new Error(`Unexpected response type: ${response[0]}`); |
| 344 | } |
| 345 | const serverRoundOne = response.slice(1); |
| 346 | console.log(`[*] Received server Round One (${serverRoundOne.length} bytes)`); |
| 347 | jpake.readRoundOne(serverRoundOne); |
| 348 | |
| 349 | // 发送客户端 Round Two(在收到服务器Round One后立即发送) |
| 350 | const roundTwo = jpake.writeRoundTwo(); |
| 351 | const msgR2 = Buffer.alloc(1 + roundTwo.length); |
| 352 | msgR2[0] = GatewayClient.DATA_TYPE.ECJPAKE_ROUND_TWO; |
| 353 | roundTwo.copy(msgR2, 1); |
| 354 | this.ws.send(msgR2); |
| 355 | console.log(`[*] Sent Round Two (${roundTwo.length} bytes)`); |
| 356 | |
| 357 | // 接收服务器 Round Two |
| 358 | response = await this.recv(); |
| 359 | if (response[0] !== GatewayClient.DATA_TYPE.ECJPAKE_ROUND_TWO) { |
| 360 | throw new Error(`Unexpected response type: ${response[0]}`); |
| 361 | } |
| 362 | const serverRoundTwo = response.slice(1); |
| 363 | console.log(`[*] Received server Round Two (${serverRoundTwo.length} bytes)`); |
| 364 | |
| 365 | // 读取服务器 Round Two,计算共享密钥 |
| 366 | console.log('[*] Processing server Round Two...'); |
| 367 | const sharedKey = jpake.readRoundTwo(serverRoundTwo); |
| 368 | console.log(`[*] Shared key: ${sharedKey.toString('hex')}`); |
| 369 | |
| 370 | // SESSION_KEY_EXCHANGE |
| 371 | const sharedCipher = new AESGCMCipher(sharedKey.slice(0, 16), sharedKey.slice(16, 24)); |
| 372 | const myKeyNonce = crypto.randomBytes(24); |
| 373 | |
| 374 | // 尝试接收服务器的SESSION_KEY_EXCHANGE |
no test coverage detected