eufy-security-client 3.7.0 → 3.7.1

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.
@@ -1514,24 +1514,36 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
1514
1514
  let return_code = 0;
1515
1515
  let resultData;
1516
1516
  if (message.bytesToRead > 0) {
1517
- if (message.signCode > 0) {
1518
- try {
1519
- message.data = (0, utils_1.decryptP2PData)(message.data, this.p2pKey);
1517
+ if (message.signCode > 0 && message.data.length > 0) {
1518
+ if (message.data.length % 16 === 0) {
1519
+ try {
1520
+ message.data = (0, utils_1.decryptP2PData)(message.data, this.p2pKey);
1521
+ }
1522
+ catch (err) {
1523
+ const error = (0, error_1.ensureError)(err);
1524
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Decrypt Error`, {
1525
+ error: (0, utils_3.getError)(error),
1526
+ stationSN: this.rawStation.station_sn,
1527
+ message: {
1528
+ seqNo: message.seqNo,
1529
+ channel: message.channel,
1530
+ commandType: types_1.CommandType[message.commandId],
1531
+ signCode: message.signCode,
1532
+ type: message.type,
1533
+ dataType: types_1.P2PDataType[message.dataType],
1534
+ data: message.data.toString("hex"),
1535
+ },
1536
+ });
1537
+ }
1520
1538
  }
1521
- catch (err) {
1522
- const error = (0, error_1.ensureError)(err);
1523
- logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Decrypt Error`, {
1524
- error: (0, utils_3.getError)(error),
1539
+ else {
1540
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - Skipping decryption, data not block-aligned`, {
1525
1541
  stationSN: this.rawStation.station_sn,
1526
- message: {
1527
- seqNo: message.seqNo,
1528
- channel: message.channel,
1529
- commandType: types_1.CommandType[message.commandId],
1530
- signCode: message.signCode,
1531
- type: message.type,
1532
- dataType: types_1.P2PDataType[message.dataType],
1533
- data: message.data.toString("hex"),
1534
- },
1542
+ seqNo: message.seqNo,
1543
+ commandType: types_1.CommandType[message.commandId],
1544
+ signCode: message.signCode,
1545
+ dataLength: message.data.length,
1546
+ mod16: message.data.length % 16,
1535
1547
  });
1536
1548
  }
1537
1549
  }
@@ -3200,7 +3212,9 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
3200
3212
  data: data.toString("hex"),
3201
3213
  cipherID: cipherID,
3202
3214
  });
3203
- const encryptedKey = (0, utils_1.readNullTerminatedBuffer)(data.subarray(4));
3215
+ // Keep full raw buffer for ECDH — readNullTerminatedBuffer truncates binary ECIES envelopes at 0x00 bytes
3216
+ const rawEncryptedKey = data.subarray(4);
3217
+ const encryptedKey = (0, utils_1.readNullTerminatedBuffer)(rawEncryptedKey);
3204
3218
  this.api
3205
3219
  .getCipher(/*this.rawStation.station_sn, */ cipherID, this.rawStation.member.admin_user_id)
3206
3220
  .then((cipher) => {
@@ -3212,10 +3226,48 @@ class P2PClientProtocol extends tiny_typed_emitter_1.TypedEmitter {
3212
3226
  cipher: JSON.stringify(cipher),
3213
3227
  });
3214
3228
  if (cipher !== undefined) {
3215
- this.encryption = types_1.EncryptionType.LEVEL_2;
3216
- const rsa = (0, utils_1.getRSAPrivateKey)(cipher.private_key, this.enableEmbeddedPKCS1Support);
3217
- this.p2pKey = rsa.decrypt(encryptedKey);
3218
- 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") });
3229
+ // Try RSA first
3230
+ try {
3231
+ this.encryption = types_1.EncryptionType.LEVEL_2;
3232
+ const rsa = (0, utils_1.getRSAPrivateKey)(cipher.private_key, this.enableEmbeddedPKCS1Support);
3233
+ this.p2pKey = rsa.decrypt(encryptedKey);
3234
+ 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") });
3235
+ }
3236
+ catch (rsaErr) {
3237
+ const rsaError = (0, error_1.ensureError)(rsaErr);
3238
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GATEWAYINFO - RSA decrypt failed, trying ECDH`, {
3239
+ error: (0, utils_3.getError)(rsaError),
3240
+ stationSN: this.rawStation.station_sn,
3241
+ hasEccKey: !!cipher.ecc_private_key,
3242
+ });
3243
+ // Try ECDH if ecc_private_key is available
3244
+ if (cipher.ecc_private_key) {
3245
+ try {
3246
+ this.encryption = types_1.EncryptionType.LEVEL_2;
3247
+ this.p2pKey = (0, utils_1.decryptP2PKeyECDH)(rawEncryptedKey, cipher.ecc_private_key);
3248
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GATEWAYINFO - ECDH success - set encryption level 2`, {
3249
+ stationSN: this.rawStation.station_sn,
3250
+ key: this.p2pKey.toString("hex"),
3251
+ keyLength: this.p2pKey.length,
3252
+ });
3253
+ }
3254
+ catch (ecdhErr) {
3255
+ const ecdhError = (0, error_1.ensureError)(ecdhErr);
3256
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GATEWAYINFO - ECDH also failed, falling back to Level 1`, {
3257
+ error: (0, utils_3.getError)(ecdhError),
3258
+ stationSN: this.rawStation.station_sn,
3259
+ });
3260
+ this.encryption = types_1.EncryptionType.LEVEL_1;
3261
+ this.p2pKey = Buffer.from((0, utils_1.getP2PCommandEncryptionKey)(this.rawStation.station_sn, this.rawStation.p2p_did));
3262
+ }
3263
+ }
3264
+ else {
3265
+ // No ECC key available, fall back to Level 1
3266
+ this.encryption = types_1.EncryptionType.LEVEL_1;
3267
+ this.p2pKey = Buffer.from((0, utils_1.getP2PCommandEncryptionKey)(this.rawStation.station_sn, this.rawStation.p2p_did));
3268
+ logging_1.rootP2PLogger.debug(`Handle DATA ${types_1.P2PDataType[message.dataType]} - CMD_GATEWAYINFO - No ECC key, set encryption level 1`, { stationSN: this.rawStation.station_sn, key: this.p2pKey.toString("hex") });
3269
+ }
3270
+ }
3219
3271
  }
3220
3272
  else {
3221
3273
  this.encryption = types_1.EncryptionType.LEVEL_1;