Skip to content

Commit b76bbd2

Browse files
- anonymize COSE content according to debug mode rules; (#60)
1 parent a32bffc commit b76bbd2

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

decoder/src/main/java/dgca/verifier/app/decoder/cose/CoseService.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ import dgca.verifier.app.decoder.model.VerificationResult
3131
interface CoseService {
3232

3333
fun decode(input: ByteArray, verificationResult: VerificationResult): CoseData?
34+
35+
fun anonymizeCose(input: ByteArray): ByteArray?
3436
}

decoder/src/main/java/dgca/verifier/app/decoder/cose/DefaultCoseService.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
package dgca.verifier.app.decoder.cose
2424

2525
import COSE.HeaderKeys
26+
import com.google.common.primitives.Bytes
2627
import com.upokecenter.cbor.CBORObject
2728
import dgca.verifier.app.decoder.model.CoseData
2829
import dgca.verifier.app.decoder.model.VerificationResult
@@ -60,4 +61,35 @@ class DefaultCoseService : CoseService {
6061
unprotectedHeader.get(key)
6162
}
6263
}
64+
65+
override fun anonymizeCose(input: ByteArray): ByteArray? {
66+
return try {
67+
val messageObject = CBORObject.DecodeFromBytes(input)
68+
69+
val content = messageObject[2].EncodeToBytes()
70+
val index = Bytes.indexOf(input, content)
71+
72+
val newArray = ByteArray(input.size)
73+
74+
val anonymize = ByteArray(content.size)
75+
anonymize.forEachIndexed { i, _ ->
76+
anonymize[i] = 0x58
77+
}
78+
79+
System.arraycopy(input, 0, newArray, 0, index)
80+
System.arraycopy(anonymize, 0, newArray, index, anonymize.size)
81+
System.arraycopy(
82+
input,
83+
index + anonymize.size,
84+
newArray,
85+
index + anonymize.size,
86+
input.size - (anonymize.size + index)
87+
)
88+
89+
newArray
90+
91+
} catch (e: Throwable) {
92+
null
93+
}
94+
}
6395
}

0 commit comments

Comments
 (0)