Skip to content

Commit

Permalink
fix(encryption): better exception handling for failed decryption of i…
Browse files Browse the repository at this point in the history
…ncoming encrypted messages

Fixes #1831
  • Loading branch information
growse committed Sep 8, 2024
1 parent f231213 commit 42f6d24
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
- Fix bug where geofencing client wasn't initialized properly, leading to very unreliable region transition detection
- Fix bug where some settings (`pubQos`, `mqttProtocolLevel` etc.) couldn't be set via the config editor (#1801)
- Fix crash when trying to decode an invalid face image on an info card
- Fix MQTT disconnect when receiving an encrypted message that can't be decrypted (#1831)
- Fix HTTP client certs not working properly with Nginx (#1793)

## Version 2.5.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,25 @@ public EncryptionProvider(Preferences preferences) {
}

String decrypt(String cyphertextb64) throws Parser.EncryptionException {
byte[] onTheWire = Base64.decode(cyphertextb64.getBytes(), Base64.DEFAULT);
byte[] nonce = new byte[crypto_secretbox_NONCEBYTES];
if (onTheWire.length <= crypto_secretbox_NONCEBYTES) {
throw new Parser.EncryptionException("Message length shorter than nonce");
}
byte[] cyphertext = new byte[onTheWire.length - crypto_secretbox_NONCEBYTES];
try {
byte[] onTheWire = Base64.decode(cyphertextb64.getBytes(), Base64.DEFAULT);

System.arraycopy(onTheWire, 0, nonce, 0, crypto_secretbox_NONCEBYTES);
System.arraycopy(onTheWire, crypto_secretbox_NONCEBYTES, cyphertext, 0, onTheWire.length - crypto_secretbox_NONCEBYTES);
return new String(b.decrypt(nonce, cyphertext));
byte[] nonce = new byte[crypto_secretbox_NONCEBYTES];
if (onTheWire.length <= crypto_secretbox_NONCEBYTES) {
throw new Parser.EncryptionException("Message length shorter than nonce");
}
byte[] cyphertext = new byte[onTheWire.length - crypto_secretbox_NONCEBYTES];

System.arraycopy(onTheWire, 0, nonce, 0, crypto_secretbox_NONCEBYTES);
System.arraycopy(onTheWire, crypto_secretbox_NONCEBYTES, cyphertext, 0, onTheWire.length - crypto_secretbox_NONCEBYTES);
try {
return new String(b.decrypt(nonce, cyphertext));
} catch (Exception e) {
throw new Parser.EncryptionException("Decryption failed", e);
}
} catch (IllegalArgumentException e) {
throw new Parser.EncryptionException("Base64 decoding failed", e);
}
}

String encrypt(@NonNull String plaintext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class Parser @Inject constructor(private val encryptionProvider: EncryptionProvi
return if (a.size == 1 && a[0] is MessageEncrypted) {
if (encryptionProvider == null || !encryptionProvider.isPayloadEncryptionEnabled) {
throw EncryptionException(
"received encrypted message but payload encryption is not enabled")
"received encrypted message but payload encryption is not enabled", null)
}
defaultMapper.readValue(
encryptionProvider.decrypt((a[0] as MessageEncrypted).data),
Expand Down Expand Up @@ -139,7 +139,10 @@ class Parser @Inject constructor(private val encryptionProvider: EncryptionProvi
return input
}

class EncryptionException internal constructor(s: String?) : Exception(s)
class EncryptionException internal constructor(s: String, cause: Throwable?) :
Exception(s, cause) {
constructor(s: String) : this(s, null)
}
}

val thisModule =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ class MQTTMessageProcessorEndpoint(
}
.also { Timber.d("Parsed message: $it") })
} catch (e: Parser.EncryptionException) {
Timber.w("Enable to decrypt received message ${message.id} on $topic")
Timber.w("Unable to decrypt received message ${message.id} on $topic")
} catch (e: InvalidFormatException) {
Timber.w("Malformed JSON message received ${message.id} on $topic")
}
Expand Down

0 comments on commit 42f6d24

Please sign in to comment.