Skip to content

Commit

Permalink
Convert all toFoo/fromFoo members from properties to methods.
Browse files Browse the repository at this point in the history
This is a large mechanical change which changes toHex/fromHex/toBase64
and fromBase64 extension properties to functions. Additionally, it
changes the toDataItem property to be a function. This includes
changes in the processor which generates code.

Fixes Issue #599.

Test: ./gradlew check
Test: ./gradlew connectedCheck
Signed-off-by: David Zeuthen <[email protected]>
  • Loading branch information
davidz25 committed Jun 26, 2024
1 parent 2516e49 commit 8559226
Show file tree
Hide file tree
Showing 102 changed files with 668 additions and 695 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ fun X509Certificate.getSubjectKeyIdentifier(): String {
val extensionValue = this.getExtensionValue(Extension.subjectKeyIdentifier.id)
val octets = DEROctetString.getInstance(extensionValue).octets
val subjectKeyIdentifier = SubjectKeyIdentifier.getInstance(octets)
return subjectKeyIdentifier.keyIdentifier.toHex
return subjectKeyIdentifier.keyIdentifier.toHex()
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,18 +207,18 @@ class ProvisioningUtil private constructor(
protectedHeaders = mapOf(
Pair(
CoseNumberLabel(Cose.COSE_LABEL_ALG),
Algorithm.ES256.coseAlgorithmIdentifier.toDataItem
Algorithm.ES256.coseAlgorithmIdentifier.toDataItem()
)
),
unprotectedHeaders = mapOf(
Pair(
CoseNumberLabel(Cose.COSE_LABEL_X5CHAIN),
X509CertChain(
listOf(X509Cert(issuerCert.encoded))
).toDataItem
).toDataItem()
)
),
).toDataItem
).toDataItem()
)

val issuerProvidedAuthenticationData = StaticAuthDataGenerator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ fun X509Certificate.getSubjectKeyIdentifier(): String {
val extensionValue = this.getExtensionValue(Extension.subjectKeyIdentifier.id)
val octets = DEROctetString.getInstance(extensionValue).octets
val subjectKeyIdentifier = SubjectKeyIdentifier.getInstance(octets)
return subjectKeyIdentifier.keyIdentifier.toHex
return subjectKeyIdentifier.keyIdentifier.toHex()
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ class DeviceRetrievalHelperTest {
//
val protectedHeaders = java.util.Map.of<CoseLabel, DataItem>(
CoseNumberLabel(Cose.COSE_LABEL_ALG),
Algorithm.ES256.coseAlgorithmIdentifier.toDataItem
Algorithm.ES256.coseAlgorithmIdentifier.toDataItem()
)
val unprotectedHeaders = java.util.Map.of<CoseLabel, DataItem>(
CoseNumberLabel(Cose.COSE_LABEL_X5CHAIN),
X509CertChain(listOf(documentSignerCert)).toDataItem
X509CertChain(listOf(documentSignerCert)).toDataItem()
)
val encodedIssuerAuth = encode(
coseSign1Sign(
Expand All @@ -212,7 +212,7 @@ class DeviceRetrievalHelperTest {
Algorithm.ES256,
protectedHeaders,
unprotectedHeaders
).toDataItem
).toDataItem()
)
val issuerProvidedAuthenticationData = StaticAuthDataGenerator(
stripIssuerNameSpaces(issuerNameSpaces, null),
Expand Down Expand Up @@ -269,7 +269,7 @@ class DeviceRetrievalHelperTest {
val encodedDeviceEngagement = qrHelper.deviceEngagement
val eReaderKey = createEcPrivateKey(EcCurve.P256)
val encodedEReaderKeyPub =
encode(eReaderKey.publicKey.toCoseKey(java.util.Map.of()).toDataItem)
encode(eReaderKey.publicKey.toCoseKey(java.util.Map.of()).toDataItem())
val encodedSessionTranscript = encode(
CborArray.builder()
.addTaggedEncodedCbor(encodedDeviceEngagement)
Expand Down Expand Up @@ -484,7 +484,7 @@ class DeviceRetrievalHelperTest {
val encodedDeviceEngagement = qrHelper.deviceEngagement
val eReaderKey = createEcPrivateKey(EcCurve.P256)
val encodedEReaderKeyPub =
encode(eReaderKey.publicKey.toCoseKey(java.util.Map.of()).toDataItem)
encode(eReaderKey.publicKey.toCoseKey(java.util.Map.of()).toDataItem())
val encodedSessionTranscript = encode(
CborArray.builder()
.addTaggedEncodedCbor(encodedDeviceEngagement)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class NfcEnagementHelperTest {
Assert.assertNotNull(responseApdu)
// The response is the CC file followed by STATUS_WORD_OK. Keep in sync with
// NfcEngagementHelper.handleSelectFile() for the contents.
Assert.assertEquals("000f207fff7fff0406e1047fff00ff9000", responseApdu.toHex)
Assert.assertEquals("000f207fff7fff0406e1047fff00ff9000", responseApdu.toHex())

// Select NDEF file
responseApdu = helper.nfcProcessCommandApdu(
Expand Down Expand Up @@ -268,7 +268,7 @@ class NfcEnagementHelperTest {
Assert.assertNotNull(responseApdu)
// The response is the CC file followed by STATUS_WORD_OK. Keep in sync with
// NfcEngagementHelper.handleSelectFile() for the contents.
Assert.assertEquals("000f207fff7fff0406e1047fff00009000", responseApdu.toHex)
Assert.assertEquals("000f207fff7fff0406e1047fff00009000", responseApdu.toHex())

// Select NDEF file
responseApdu = helper.nfcProcessCommandApdu(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ class DataTransportHttpTest {
null,
DataTransportOptions.Builder().build()
)
val messageSentByVerifier = "010203".fromHex
val messageSentByProver = "0405".fromHex
val messageSentByVerifier = "010203".fromHex()
val messageSentByProver = "0405".fromHex()
val messageReceivedByProver = arrayOf<ByteArray?>(null)
val messageReceivedByVerifier = arrayOf<ByteArray?>(null)
val proverMessageReceivedCondVar = ConditionVariable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class DataTransportTcpTest {
DataTransport.Role.MDOC,
DataTransportOptions.Builder().build()
)
val messageSentByVerifier = "010203".fromHex
val messageSentByProver = "0405".fromHex
val messageSentByVerifier = "010203".fromHex()
val messageSentByProver = "0405".fromHex()
val messageReceivedByProver = arrayOf<ByteArray?>(null)
val messageReceivedByVerifier = arrayOf<ByteArray?>(null)
val proverMessageReceivedCondVar = ConditionVariable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class AndroidStorageTest {
storage.put("foo", data)
var targetFile = File(storageDir, PREFIX + URLEncoder.encode("foo", "UTF-8"))
var fileContents = AtomicFile(targetFile).readFully()
Assert.assertEquals(-1, (fileContents.toHex).indexOf(data.toHex).toLong())
Assert.assertEquals(-1, (fileContents.toHex()).indexOf(data.toHex()).toLong())

// Try again without encryption. The data should start at offset 4.
storage = AndroidStorageEngine.Builder(context, storageDir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ class ConnectionMethodNdefTest {
Assert.assertEquals(uuidBoth, decoded.centralClientModeUuid)
Assert.assertEquals(
"da2015016170706c69636174696f6e2f766e642e626c7565746f6f74682e6c652e6f6f6230021c03110702000000000000000000000000000000",
pair.first.toByteArray().toHex
pair.first.toByteArray().toHex()
)
Assert.assertEquals(
"01013001046d646f63",
pair.second.toHex
pair.second.toHex()
)
val hrPair = NfcUtil.toNdefRecord(cm, listOf(""), false)
Assert.assertArrayEquals(pair.first.toByteArray(), hrPair!!.first.toByteArray())
Expand Down Expand Up @@ -161,11 +161,11 @@ class ConnectionMethodNdefTest {
Assert.assertEquals(uuid, decoded.centralClientModeUuid)
Assert.assertEquals(
"da2015016170706c69636174696f6e2f766e642e626c7565746f6f74682e6c652e6f6f6230021c011107b168de3a0000000015cd5b0700000000",
pair.first.toByteArray().toHex
pair.first.toByteArray().toHex()
)
Assert.assertEquals(
"01013001046d646f63",
pair.second!!.toHex
pair.second!!.toHex()
)
pair = NfcUtil.toNdefRecord(cm, emptyList(), false)
decoded = fromNdefRecord(pair!!.first, false)
Expand All @@ -176,9 +176,9 @@ class ConnectionMethodNdefTest {
Assert.assertEquals(uuid, decoded.centralClientModeUuid)
Assert.assertEquals(
"da2015016170706c69636174696f6e2f766e642e626c7565746f6f74682e6c652e6f6f6230021c001107b168de3a0000000015cd5b0700000000",
pair.first.toByteArray().toHex
pair.first.toByteArray().toHex()
)
Assert.assertEquals("01013000", pair.second!!.toHex)
Assert.assertEquals("01013000", pair.second!!.toHex())
}

@Test
Expand Down Expand Up @@ -223,11 +223,11 @@ class ConnectionMethodNdefTest {
Assert.assertNull(decoded.centralClientModeUuid)
Assert.assertEquals(
"da2015016170706c69636174696f6e2f766e642e626c7565746f6f74682e6c652e6f6f6230021c00110701000000000000000000000000000000",
pair.first.toByteArray().toHex
pair.first.toByteArray().toHex()
)
Assert.assertEquals(
"01013001046d646f63",
pair.second!!.toHex
pair.second!!.toHex()
)
pair = NfcUtil.toNdefRecord(cm, emptyList(), false)
decoded = fromNdefRecord(pair!!.first, false)
Expand All @@ -238,9 +238,9 @@ class ConnectionMethodNdefTest {
Assert.assertNull(decoded.centralClientModeUuid)
Assert.assertEquals(
"da2015016170706c69636174696f6e2f766e642e626c7565746f6f74682e6c652e6f6f6230021c01110701000000000000000000000000000000",
pair.first.toByteArray().toHex
pair.first.toByteArray().toHex()
)
Assert.assertEquals("01013000", pair.second!!.toHex)
Assert.assertEquals("01013000", pair.second!!.toHex())
}

@Test
Expand Down Expand Up @@ -273,7 +273,7 @@ class ConnectionMethodNdefTest {
@Throws(FormatException::class)
fun testConnectionMethodVector() {
val ndefHsMessage =
NdefMessage("91020f487315d10209616301013001046d646f631a2003016170706c69636174696f6e2f766e642e626c7565746f6f74682e6c652e6f6f6230021c015c1e580469736f2e6f72673a31383031333a646576696365656e676167656d656e746d646f63a20063312e30018201d818584ba401022001215820e778fcb1513fad715c755462cb4d3ee3c1de2f618d10e07788a35eda2da58b982258205e6ee59512414cdb11ee330db2590ab6d1b5a78ede4a0ecac02e3af65cafbcd9".fromHex)
NdefMessage("91020f487315d10209616301013001046d646f631a2003016170706c69636174696f6e2f766e642e626c7565746f6f74682e6c652e6f6f6230021c015c1e580469736f2e6f72673a31383031333a646576696365656e676167656d656e746d646f63a20063312e30018201d818584ba401022001215820e778fcb1513fad715c755462cb4d3ee3c1de2f618d10e07788a35eda2da58b982258205e6ee59512414cdb11ee330db2590ab6d1b5a78ede4a0ecac02e3af65cafbcd9".fromHex())
Assert.assertNotNull(ndefHsMessage)
var cm: ConnectionMethodBle? = null
for (r in ndefHsMessage.records) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ class DeviceRetrievalHelper internal constructor(
val encodedEDeviceKeyBytes: ByteArray = Cbor.encode(
Tagged(
24, Bstr(
Cbor.encode(eDeviceKey.toCoseKey().toDataItem)
Cbor.encode(eDeviceKey.toCoseKey().toDataItem())
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ class VerificationHelper internal constructor(
// to avoid delaying the transaction...
ephemeralKey = Crypto.createEcPrivateKey(engagement.eSenderKey.curve)
val encodedEReaderKeyPub: ByteArray = Cbor.encode(
ephemeralKey!!.publicKey.toCoseKey().toDataItem
ephemeralKey!!.publicKey.toCoseKey().toDataItem()
)
encodedSessionTranscript = Cbor.encode(
CborArray.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class NfcEngagementHelper private constructor(
Logger.d(TAG, "Setting up transports")
val timeStartedSettingUpTransports = System.currentTimeMillis()
val encodedEDeviceKeyBytes = Cbor.encode(
Tagged(24, Bstr(Cbor.encode(eDeviceKey.toCoseKey().toDataItem))))
Tagged(24, Bstr(Cbor.encode(eDeviceKey.toCoseKey().toDataItem()))))

// Need to disambiguate the connection methods here to get e.g. two ConnectionMethods
// if both BLE modes are available at the same time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class QrEngagementHelper internal constructor(

init {
val encodedEDeviceKeyBytes =
Cbor.encode(Tagged(24, Bstr(Cbor.encode(eDeviceKey.toCoseKey().toDataItem))))
Cbor.encode(Tagged(24, Bstr(Cbor.encode(eDeviceKey.toCoseKey().toDataItem()))))

// Set EDeviceKey for transports we were given.
if (transports != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,13 @@ internal class GattClient(
if (characteristic.uuid == characteristicIdentUuid) {
val identValue = characteristic.value
if (Logger.isDebugEnabled) {
Logger.d(TAG, "Received identValue: ${identValue.toHex}")
Logger.d(TAG, "Received identValue: ${identValue.toHex()}")
}
// TODO: Don't even request IDENT since it cannot work w/ reverse engagement (there's
// no way the mdoc reader knows EDeviceKeyBytes at this point) and it's also optional.
if (!Arrays.equals(identValue, this.identValue)) {
Logger.w(TAG, "Received ident '${identValue.toHex}' does not match " +
"expected ident '${this.identValue!!.toHex}'")
Logger.w(TAG, "Received ident '${identValue.toHex()}' does not match " +
"expected ident '${this.identValue!!.toHex()}'")
}
afterIdentObtained(gatt)
} else if (characteristic.uuid == characteristicL2CAPUuid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ internal class GattServer(
) {
val charUuid = characteristic.uuid
Logger.d(TAG, "onCharacteristicWriteRequest: ${device.address} $requestId " +
"$offset ${characteristic.uuid} ${value.toHex}")
"$offset ${characteristic.uuid} ${value.toHex()}")

// If we are connected to a device, ignore write from any other device
if (currentConnection != null &&
Expand Down Expand Up @@ -387,7 +387,7 @@ internal class GattServer(
if (Logger.isDebugEnabled) {
Logger.d(
TAG, "onDescriptorWriteRequest: ${device.address}" +
"${descriptor.characteristic.uuid} $offset ${value.toHex}"
"${descriptor.characteristic.uuid} $offset ${value.toHex()}"
)
}
if (responseNeeded) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ class AndroidKeystoreSecureArea(
map.put("userAuthenticationRequired", settings.userAuthenticationRequired)
map.put("userAuthenticationTimeoutMillis", settings.userAuthenticationTimeoutMillis)
map.put("useStrongBox", settings.useStrongBox)
map.put("attestation", attestation.toDataItem)
map.put("attestation", attestation.toDataItem())
map.put("curve", settings.ecCurve.coseCurveIdentifier)
storageEngine.put(PREFIX + alias, Cbor.encode(map.end().build()))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class AndroidStorageEngine internal constructor(
} else if (Arrays.equals(magic, MAGIC_NOT_ENCRYPTED)) {
Arrays.copyOfRange(data, MAGIC_SIZE, data.size)
} else {
throw IllegalStateException("Unexpected magic ${magic.toHex}")
throw IllegalStateException("Unexpected magic ${magic.toHex()}")
}
} catch (e: FileNotFoundException) {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ object NfcUtil {

// Defined by NFC Forum
@JvmField
val AID_FOR_TYPE_4_TAG_NDEF_APPLICATION = "D2760000850101".fromHex
val AID_FOR_TYPE_4_TAG_NDEF_APPLICATION = "D2760000850101".fromHex()

// Defined by 18013-5 Section 8.3.3.1.2 Data retrieval using near field communication (NFC)
val AID_FOR_MDL_DATA_TRANSFER = "A0000002480400".fromHex
val AID_FOR_MDL_DATA_TRANSFER = "A0000002480400".fromHex()

const val COMMAND_TYPE_OTHER = 0
const val COMMAND_TYPE_SELECT_BY_AID = 1
Expand Down Expand Up @@ -216,8 +216,8 @@ object NfcUtil {
if (Logger.isDebugEnabled) {
Logger.d(
TAG, "ConnectionMethod $cm: alternativeCarrierRecord: "
+ "${records.second.toHex} carrierConfigurationRecord: "
+ "${records.first.payload.toHex}"
+ "${records.second.toHex()} carrierConfigurationRecord: "
+ "${records.first.payload.toHex()}"
)
}
alternativeCarrierRecords.add(records.second)
Expand Down
Loading

0 comments on commit 8559226

Please sign in to comment.