forked from WalletConnect/kotlin-walletconnect-lib
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Verify HMAC when decrypting received payloads. (#11)
* Verify HMAC when decrypting received payloads.
- Loading branch information
Showing
6 changed files
with
139 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
lib/src/test/java/org/walletconnect/impls/MoshiPayloadEncryptionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package org.walletconnect.impls | ||
|
||
import com.squareup.moshi.Moshi | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
import java.security.SecureRandom | ||
|
||
class MoshiPayloadEncryptionTest { | ||
|
||
private val moshi = Moshi.Builder().build() | ||
private val payloadEncryption = MoshiPayloadEncryption(Moshi.Builder().build()) | ||
private val encryptedPayloadAdapter = moshi.adapter(EncryptedPayload::class.java) | ||
|
||
@Test | ||
fun `happy path encryption + decryption`() { | ||
val key = generateKey() | ||
val payloadJson = | ||
""" | ||
{ | ||
"id" : 123456, | ||
"method" : "wc_sessionRequest", | ||
} | ||
""".trimIndent() | ||
val encryptedPayload = payloadEncryption.encrypt(payloadJson, key) | ||
|
||
val result = payloadEncryption.decrypt(encryptedPayload, key) | ||
|
||
assertThat(result).isEqualTo(payloadJson) | ||
} | ||
|
||
@Test | ||
fun `invalid hmac throws exception`() { | ||
val key = generateKey() | ||
// this is the original payload | ||
val payloadJson = | ||
""" | ||
{ | ||
"id" : 123456, | ||
"method" : "wc_sessionRequest", | ||
} | ||
""".trimIndent() | ||
val mutatedPayload = with(encryptedPayloadAdapter.fromJson(payloadEncryption.encrypt(payloadJson, key))) { | ||
requireNotNull(this) | ||
encryptedPayloadAdapter.toJson(copy(hmac = hmac.dropLast(4)+"1234")) | ||
} | ||
|
||
assertThrows<IllegalArgumentException> { | ||
payloadEncryption.decrypt(mutatedPayload, key) | ||
} | ||
} | ||
|
||
@Test | ||
fun `invalid iv throws exception`() { | ||
val key = generateKey() | ||
// this is the original payload | ||
val payloadJson = | ||
""" | ||
{ | ||
"id" : 123456, | ||
"method" : "wc_sessionRequest", | ||
} | ||
""".trimIndent() | ||
val mutatedPayload = with(encryptedPayloadAdapter.fromJson(payloadEncryption.encrypt(payloadJson, key))) { | ||
requireNotNull(this) | ||
encryptedPayloadAdapter.toJson(copy(iv = iv.dropLast(4)+"1234")) | ||
} | ||
|
||
assertThrows<IllegalArgumentException> { | ||
payloadEncryption.decrypt(mutatedPayload, key) | ||
} | ||
} | ||
|
||
@Test | ||
fun `invalid data throws exception`() { | ||
val key = generateKey() | ||
// this is the original payload | ||
val payloadJson = | ||
""" | ||
{ | ||
"id" : 123456, | ||
"method" : "wc_sessionRequest", | ||
} | ||
""".trimIndent() | ||
val mutatedPayload = with(encryptedPayloadAdapter.fromJson(payloadEncryption.encrypt(payloadJson, key))) { | ||
requireNotNull(this) | ||
encryptedPayloadAdapter.toJson(copy(data = data.dropLast(4)+"1234")) | ||
} | ||
|
||
assertThrows<IllegalArgumentException> { | ||
payloadEncryption.decrypt(mutatedPayload, key) | ||
} | ||
} | ||
|
||
private fun generateKey() = ByteArray(32).also { SecureRandom().nextBytes(it) }.joinToString(separator = "") { "%02x".format(it) } | ||
} |