From 6a49d217a3f13817dbdbbecafe222e78005d3175 Mon Sep 17 00:00:00 2001 From: Ever Morales Date: Thu, 12 Nov 2026 07:12:49 -0600 Subject: [PATCH] Keep the verifier's hash algorithms for every transaction type. `TransactionType.parseJson()` read the payload of an OpenID4VP `transaction_data` item but not its `transaction_data_hashes_alg`, so `TransactionData.hashAlgorithms` was always null for a type that does not override it. Such a type hashed the item with SHA-256 even when the verifier asked for another algorithm, and the verifier could not match the hash. `PaymentTransaction` and `PingTransaction` read the list in their own `parseJson()`, so they were not affected. The base `parseJson()` now reads the list with `parseJoseHashAlgorithms()`, as those overrides do: unknown names are skipped and a list naming no known algorithm is refused. A value that is not an array of strings is refused as well, with a message naming `transaction_data_hashes_alg`, instead of being answered in SHA-256. An absent or null value leaves the default. Test: ./gradlew :multipaz:jvmTest, :multipaz-doctypes:jvmTest and detektMetadataCommonMain, run as separate invocations. The new `TransactionTypeParseJsonTest` covers the round trip, absent and null values, unknown and unsupported names, and malformed values, including a number next to a valid name. That last case fails if the per-element type check in `parseJson()` is removed. Signed-off-by: Ever Morales --- .../multipaz/documenttype/TransactionType.kt | 33 ++++++ .../TransactionTypeParseJsonTest.kt | 108 ++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 multipaz/src/commonTest/kotlin/org/multipaz/documenttype/TransactionTypeParseJsonTest.kt diff --git a/multipaz/src/commonMain/kotlin/org/multipaz/documenttype/TransactionType.kt b/multipaz/src/commonMain/kotlin/org/multipaz/documenttype/TransactionType.kt index ae14dde6ab..dd7c28059b 100644 --- a/multipaz/src/commonMain/kotlin/org/multipaz/documenttype/TransactionType.kt +++ b/multipaz/src/commonMain/kotlin/org/multipaz/documenttype/TransactionType.kt @@ -2,8 +2,13 @@ package org.multipaz.documenttype import kotlinx.io.bytestring.ByteString import kotlinx.io.bytestring.decodeToString +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonArray import kotlinx.serialization.json.JsonElement +import kotlinx.serialization.json.JsonNull import kotlinx.serialization.json.JsonPrimitive +import kotlinx.serialization.json.jsonArray +import kotlinx.serialization.json.jsonObject import org.multipaz.cbor.Bstr import org.multipaz.cbor.Cbor import org.multipaz.cbor.DataItem @@ -144,6 +149,18 @@ abstract class TransactionType( /** * Parses transaction data serialized for use in OpenID4VP protocol. + * + * The payload comes from [parseOpenId4VpRequest]. The hash algorithms the verifier accepts + * come from `transaction_data_hashes_alg`, so a type that does not override this function + * still hashes its transaction data with the algorithm the verifier asked for. + * + * If `transaction_data_hashes_alg` is absent or `null`, [TransactionData.hashAlgorithms] is + * left `null` and the default algorithm applies. + * + * @param serialized the base64url-encoded JSON transaction data item. + * @return transaction data wrapping the parsed payload. + * @throws IllegalArgumentException if `transaction_data_hashes_alg` is present but not an + * array of strings, or names no known hash algorithm. */ open fun parseJson(serialized: ByteString): TransactionData { val jsonString = serialized.decodeToString().fromBase64Url().decodeToString() @@ -152,6 +169,22 @@ abstract class TransactionType( payload = parseOpenId4VpRequest(jsonString), protocol = TransactionProtocol.OPENID4VP, rawBytes = serialized, + hashAlgorithms = parseJoseHashAlgorithms( + Json.parseToJsonElement(jsonString).jsonObject["transaction_data_hashes_alg"] + ?.takeIf { it != JsonNull } + ?.also { + require(it is JsonArray) { + "'transaction_data_hashes_alg' must be an array of strings: $it" + } + } + ?.jsonArray + ?.map { alg -> + require(alg is JsonPrimitive && alg.isString) { + "Hash algorithm in 'transaction_data_hashes_alg' must be a string: $alg" + } + alg.content + } + ), ) } diff --git a/multipaz/src/commonTest/kotlin/org/multipaz/documenttype/TransactionTypeParseJsonTest.kt b/multipaz/src/commonTest/kotlin/org/multipaz/documenttype/TransactionTypeParseJsonTest.kt new file mode 100644 index 0000000000..510311296b --- /dev/null +++ b/multipaz/src/commonTest/kotlin/org/multipaz/documenttype/TransactionTypeParseJsonTest.kt @@ -0,0 +1,108 @@ +package org.multipaz.documenttype + +import kotlinx.io.bytestring.encodeToByteString +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonArray +import kotlinx.serialization.json.JsonElement +import kotlinx.serialization.json.JsonNull +import kotlinx.serialization.json.JsonPrimitive +import kotlinx.serialization.json.buildJsonObject +import kotlinx.serialization.json.jsonObject +import kotlinx.serialization.json.jsonPrimitive +import kotlinx.serialization.json.put +import org.multipaz.crypto.Algorithm +import org.multipaz.presentment.TransactionData +import org.multipaz.presentment.TransactionProtocol +import org.multipaz.util.toBase64Url +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith +import kotlin.test.assertNull +import kotlin.test.assertTrue + +class TransactionTypeParseJsonTest { + + // Relies on the base parseJson(), as a transaction type does unless it overrides it. + private object NoteTransaction : TransactionType( + displayName = "Note", + identifier = "com.example.note", + ) { + override fun serializeOpenId4VpRequest( + payload: String, + credentialIds: List, + hashAlgorithms: List? + ): String = buildJsonObject { + put("type", identifier) + put("credential_ids", JsonArray(credentialIds.map { JsonPrimitive(it) })) + joseHashAlgorithms(hashAlgorithms)?.let { + put("transaction_data_hashes_alg", JsonArray(it.map { alg -> JsonPrimitive(alg) })) + } + put("note", payload) + }.toString() + + override fun parseOpenId4VpRequest(jsonString: String): String = + Json.parseToJsonElement(jsonString).jsonObject.getValue("note").jsonPrimitive.content + } + + private fun parse(json: String): TransactionData = + NoteTransaction.parseJson(json.encodeToByteArray().toBase64Url().encodeToByteString()) + + private fun parseWithHashesAlg(hashesAlg: JsonElement): TransactionData = + parse(buildJsonObject { + put("type", NoteTransaction.identifier) + put("credential_ids", JsonArray(listOf(JsonPrimitive("pid")))) + put("transaction_data_hashes_alg", hashesAlg) + put("note", "hello") + }.toString()) + + private fun algs(vararg names: String) = JsonArray(names.map { JsonPrimitive(it) }) + + @Test + fun requestedHashAlgorithmsSurviveTheRoundTrip() { + val data = parse(NoteTransaction.serializeOpenId4VpRequest( + payload = "hello", + credentialIds = listOf("pid"), + hashAlgorithms = listOf(Algorithm.SHA384, Algorithm.SHA256) + )) + assertEquals("hello", data.payload) + assertEquals(TransactionProtocol.OPENID4VP, data.protocol) + assertEquals(listOf(Algorithm.SHA384, Algorithm.SHA256), data.hashAlgorithms) + } + + @Test + fun noHashAlgorithmsLeavesTheDefault() { + val data = parse(NoteTransaction.serializeOpenId4VpRequest("hello", listOf("pid"), null)) + assertNull(data.hashAlgorithms) + assertNull(parseWithHashesAlg(JsonNull).hashAlgorithms) + } + + @Test + fun unknownHashAlgorithmsAreSkipped() { + assertEquals(listOf(Algorithm.SHA512), parseWithHashesAlg(algs("sha-3000", "sha-512")).hashAlgorithms) + } + + @Test + fun noKnownHashAlgorithmIsRefused() { + assertFailsWith { parseWithHashesAlg(algs("md5")) } + assertFailsWith { parseWithHashesAlg(algs()) } + } + + @Test + fun malformedHashAlgorithmsAreRefused() { + assertFailsWith { parseWithHashesAlg(JsonPrimitive("sha-384")) } + assertFailsWith { parseWithHashesAlg(JsonArray(listOf(JsonPrimitive(384)))) } + // A number next to a valid name: without the per-element check the number would become + // "384", be skipped as unknown, and the list would parse to [SHA256]. + assertFailsWith { + parseWithHashesAlg(JsonArray(listOf(JsonPrimitive(384), JsonPrimitive("sha-256")))) + } + } + + @Test + fun nonArrayHashAlgorithmsErrorNamesTheField() { + val error = assertFailsWith { + parseWithHashesAlg(JsonPrimitive("sha-384")) + } + assertTrue(error.message!!.contains("transaction_data_hashes_alg")) + } +}