Skip to content

If no document, omit "documents" key from DeviceResponse map #577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.android.identity.cbor.Tagged
*/
class DeviceResponseGenerator(private val mStatusCode: Long) {
private val mDocumentsBuilder = CborArray.builder()
private var mDocumentAdded = false

/**
* Adds a new document to the device response.
Expand Down Expand Up @@ -87,6 +88,8 @@ class DeviceResponseGenerator(private val mStatusCode: Long) {
errors: Map<String?, Map<String?, Long?>>?,
encodedIssuerAuth: ByteArray
) = apply {
mDocumentAdded = true

val insOuter = CborMap.builder()
for ((ns, encodedIssuerSignedItemBytesList) in issuerNameSpaces) {
insOuter.putArray(ns!!).let { insInner ->
Expand Down Expand Up @@ -150,6 +153,8 @@ class DeviceResponseGenerator(private val mStatusCode: Long) {
* @return the generator.
*/
fun addDocument(encodedDocument: ByteArray) = apply {
mDocumentAdded = true

mDocumentsBuilder.add(Cbor.decode(encodedDocument))
}

Expand All @@ -161,7 +166,9 @@ class DeviceResponseGenerator(private val mStatusCode: Long) {
fun generate(): ByteArray =
CborMap.builder().run {
put("version", "1.0")
put("documents", mDocumentsBuilder.end().build())
if (mDocumentAdded) {
put("documents", mDocumentsBuilder.end().build())
}
// TODO: The documentErrors map entry should only be present if there is a non-zero
// number of elements in the array. Right now we don't have a way for the application
// to convey document errors but when we add that API we'll need to do something so
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,6 @@ object TestVectors {
+ "02943c3bc5a13ad669fd79d8e4ee5f5be82dc638ba9a1d")

const val ISO_18013_5_ANNEX_D_SESSION_TERMINATION = "a16673746174757314"

const val DEVICE_RESPONSE_NO_DOCUMENTS_STATUS_20 = "A26776657273696F6E63312E306673746174757314"
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import com.android.identity.crypto.EcPrivateKey
import com.android.identity.mdoc.mso.MobileSecurityObjectGenerator
import com.android.identity.mdoc.mso.StaticAuthDataGenerator
import com.android.identity.mdoc.mso.StaticAuthDataParser
import com.android.identity.mdoc.TestVectors
import com.android.identity.mdoc.util.MdocUtil
import com.android.identity.securearea.KeyPurpose
import com.android.identity.securearea.SecureArea
Expand Down Expand Up @@ -544,6 +545,16 @@ class DeviceResponseGeneratorTest {
Assert.assertEquals("foo1", doc.getIssuerEntryString("ns2", "bar1"))
}

@Test
@Throws(Exception::class)
fun testDocumentGeneratorNoDocuments() {
val deviceResponseGenerator = DeviceResponseGenerator(20)
val encodedDeviceResponse = deviceResponseGenerator.generate()

Assert.assertEquals(TestVectors.DEVICE_RESPONSE_NO_DOCUMENTS_STATUS_20.fromHex, encodedDeviceResponse)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally stuff in TestVectors.kt are the test vectors from Annex D. I think it would be cleaner to do

 Assert.assertEquals("<insert pretty-printed-CBOR>", Cbor.toDiagnostics(encodedDeviceResponse, setOf(DiagnosticOption.PRETTY_PRINT)))

and then maybe add a comment above to say that there's no "documents" map entry.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still using hex and not pretty-print...

}


companion object {
const val DOC_TYPE = "com.example.document_xyz"
}
Expand Down
Loading