eufy-security-client 3.8.0-dev.24 → 3.8.0-dev.28

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.
@@ -1520,24 +1520,36 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1520
1520
  let return_code = 0;
1521
1521
  let resultData;
1522
1522
  if (message.bytesToRead > 0) {
1523
- if (message.signCode > 0) {
1524
- try {
1525
- message.data = (0, utils_1.decryptP2PData)(message.data, this.p2pKey);
1523
+ if (message.signCode > 0 && message.data.length > 0) {
1524
+ if (message.data.length % 16 === 0) {
1525
+ try {
1526
+ message.data = (0, utils_1.decryptP2PData)(message.data, this.p2pKey);
1527
+ }
1528
+ catch (err) {
1529
+ const error = (0, error_1.ensureError)(err);
1530
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Decrypt Error`, {
1531
+ error: (0, utils_3.getError)(error),
1532
+ stationSN: this.rawStation.station_sn,
1533
+ message: {
1534
+ seqNo: message.seqNo,
1535
+ channel: message.channel,
1536
+ commandType: types_1.CommandType[message.commandId],
1537
+ signCode: message.signCode,
1538
+ type: message.type,
1539
+ dataType: types_1.P2PDataType[message.dataType],
1540
+ data: message.data.toString("hex"),
1541
+ },
1542
+ });
1543
+ }
1526
1544
  }
1527
- catch (err) {
1528
- const error = (0, error_1.ensureError)(err);
1529
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Decrypt Error`, {
1530
- error: (0, utils_3.getError)(error),
1545
+ else {
1546
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Skipping decryption, data not block-aligned`, {
1531
1547
  stationSN: this.rawStation.station_sn,
1532
- message: {
1533
- seqNo: message.seqNo,
1534
- channel: message.channel,
1535
- commandType: types_1.CommandType[message.commandId],
1536
- signCode: message.signCode,
1537
- type: message.type,
1538
- dataType: types_1.P2PDataType[message.dataType],
1539
- data: message.data.toString("hex"),
1540
- },
1548
+ seqNo: message.seqNo,
1549
+ commandType: types_1.CommandType[message.commandId],
1550
+ signCode: message.signCode,
1551
+ dataLength: message.data.length,
1552
+ mod16: message.data.length % 16,
1541
1553
  });
1542
1554
  }
1543
1555
  }
@@ -3262,7 +3274,10 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
3262
3274
  data: data.toString("hex"),
3263
3275
  cipherID: cipherID,
3264
3276
  });
3265
- const encryptedKey = (0, utils_1.readNullTerminatedBuffer)(data.subarray(4));
3277
+ // Keep full raw buffer for ECDH — readNullTerminatedBuffer truncates binary ECIES envelopes at 0x00 bytes
3278
+ const rawEncryptedKey = data.subarray(4);
3279
+ const encryptedKey = (0, utils_1.readNullTerminatedBuffer)(rawEncryptedKey);
3280
+ const isECDHDevice = this.rawStation.station_sn.startsWith("T8214") || this.rawStation.station_sn.startsWith("T8425");
3266
3281
  this.api
3267
3282
  .getCipher(/*this.rawStation.station_sn, */ cipherID, this.rawStation.member.admin_user_id)
3268
3283
  .then((cipher) => {
@@ -3274,10 +3289,49 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
3274
3289
  cipher: JSON.stringify(cipher),
3275
3290
  });
3276
3291
  if (cipher !== undefined) {
3277
- this.encryption = types_1.EncryptionType.LEVEL_2;
3278
- const rsa = (0, utils_1.getRSAPrivateKey)(cipher.private_key, this.enableEmbeddedPKCS1Support);
3279
- this.p2pKey = rsa.decrypt(encryptedKey);
3280
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GATEWAYINFO - set encryption level 2`, { stationSN: this.rawStation.station_sn, key: this.p2pKey.toString("hex") });
3292
+ // Try RSA first
3293
+ try {
3294
+ this.encryption = types_1.EncryptionType.LEVEL_2;
3295
+ const rsa = (0, utils_1.getRSAPrivateKey)(cipher.private_key, this.enableEmbeddedPKCS1Support);
3296
+ this.p2pKey = rsa.decrypt(encryptedKey);
3297
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GATEWAYINFO - RSA success - set encryption level 2`, { stationSN: this.rawStation.station_sn, key: this.p2pKey.toString("hex") });
3298
+ }
3299
+ catch (rsaErr) {
3300
+ const rsaError = (0, error_1.ensureError)(rsaErr);
3301
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GATEWAYINFO - RSA decrypt failed`, {
3302
+ error: (0, utils_3.getError)(rsaError),
3303
+ stationSN: this.rawStation.station_sn,
3304
+ isECDHDevice: isECDHDevice,
3305
+ hasEccKey: !!cipher.ecc_private_key,
3306
+ });
3307
+ // Try ECDH only for known ECDH devices (T8214/T8425)
3308
+ if (isECDHDevice && cipher.ecc_private_key) {
3309
+ try {
3310
+ this.encryption = types_1.EncryptionType.LEVEL_2;
3311
+ this.p2pKey = (0, utils_1.decryptP2PKeyECDH)(rawEncryptedKey, cipher.ecc_private_key);
3312
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GATEWAYINFO - ECDH success - set encryption level 2`, {
3313
+ stationSN: this.rawStation.station_sn,
3314
+ key: this.p2pKey.toString("hex"),
3315
+ keyLength: this.p2pKey.length,
3316
+ });
3317
+ }
3318
+ catch (ecdhErr) {
3319
+ const ecdhError = (0, error_1.ensureError)(ecdhErr);
3320
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GATEWAYINFO - ECDH also failed, falling back to Level 1`, {
3321
+ error: (0, utils_3.getError)(ecdhError),
3322
+ stationSN: this.rawStation.station_sn,
3323
+ });
3324
+ this.encryption = types_1.EncryptionType.LEVEL_1;
3325
+ this.p2pKey = Buffer.from((0, utils_1.getP2PCommandEncryptionKey)(this.rawStation.station_sn, this.rawStation.p2p_did));
3326
+ }
3327
+ }
3328
+ else {
3329
+ // Non-ECDH device or no ECC key — fall back to Level 1
3330
+ this.encryption = types_1.EncryptionType.LEVEL_1;
3331
+ this.p2pKey = Buffer.from((0, utils_1.getP2PCommandEncryptionKey)(this.rawStation.station_sn, this.rawStation.p2p_did));
3332
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GATEWAYINFO - RSA failed, set encryption level 1`, { stationSN: this.rawStation.station_sn, key: this.p2pKey.toString("hex") });
3333
+ }
3334
+ }
3281
3335
  }
3282
3336
  else {
3283
3337
  this.encryption = types_1.EncryptionType.LEVEL_1;