BLE LTK: define all the steps

This commit is contained in:
Andrei Vereha 2021-02-27 13:39:47 +01:00
parent 1aa6d02893
commit 39408ac535

View file

@ -6,8 +6,10 @@ import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.Id import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.Id
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.OmnipodDashBleManagerImpl import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.OmnipodDashBleManagerImpl
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.MessageIO import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.MessageIO
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.MessagePacket
import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.StringLengthPrefixEncoding import info.nightscout.androidaps.plugins.pump.omnipod.dash.driver.comm.message.StringLengthPrefixEncoding
import info.nightscout.androidaps.utils.extensions.hexStringToByteArray import info.nightscout.androidaps.utils.extensions.hexStringToByteArray
import info.nightscout.androidaps.utils.extensions.toHex
import java.security.SecureRandom import java.security.SecureRandom
internal class LTKExchanger(private val aapsLogger: AAPSLogger, private val msgIO: MessageIO) { internal class LTKExchanger(private val aapsLogger: AAPSLogger, private val msgIO: MessageIO) {
@ -17,6 +19,8 @@ internal class LTKExchanger(private val aapsLogger: AAPSLogger, private val msgI
private val controllerId = Id.fromInt(OmnipodDashBleManagerImpl.CONTROLLER_ID) private val controllerId = Id.fromInt(OmnipodDashBleManagerImpl.CONTROLLER_ID)
val nodeId = controllerId.increment() val nodeId = controllerId.increment()
private var seq: Byte = 1 private var seq: Byte = 1
private var ltk = ByteArray(0)
private var noncePrefix = ByteArray(0)
init { init {
val random = SecureRandom() val random = SecureRandom()
@ -36,28 +40,36 @@ internal class LTKExchanger(private val aapsLogger: AAPSLogger, private val msgI
// read SPS1 // read SPS1
val podSps1 = msgIO.receiveMessage() val podSps1 = msgIO.receiveMessage()
aapsLogger.info(LTag.PUMPBTCOMM, "Received message: %s", podSps1) aapsLogger.info(LTag.PUMPBTCOMM, "Received message: %s", podSps1)
/* processSps1FromPod(podSps1)
seq++
// send SPS2 // send SPS2
var sps2 = PairMessage() val sps2 = sps2()
msgIO.sendMesssage(sps2.messagePacket) msgIO.sendMesssage(sps2.messagePacket)
// read SPS2 // read SPS2
val podSps2 = msgIO.receiveMessage() val podSps2 = msgIO.receiveMessage()
validatePodSps2(podSps2)
// send SP0GP0 // send SP0GP0
msgIO.sendMesssage(sps2.messagePacket) msgIO.sendMesssage(sp0gp0().messagePacket)
// read P0 // read P0
//TODO: if we fail to read or validate p0 will lead to undefined state
// it could be that:
// - the pod answered with p0 and we did not receive/could not process the answer
// - the pod answered with some sort of error
val p0 = msgIO.receiveMessage() val p0 = msgIO.receiveMessage()
*/ validateP0(p0)
return null
return LTK(
ltk = ltk,
noncePrefix = noncePrefix,
)
} }
private fun sp2(): ByteArray { private fun sp1sp2(sp1: ByteArray, sp2: ByteArray): PairMessage {
// This is GetPodStatus command, with page 0 parameter.
// We could replace that in the future with the serialized GetPodStatus()
return GET_POD_STATUS_HEX_COMMAND.hexStringToByteArray()
}
fun sp1sp2(sp1: ByteArray, sp2: ByteArray): PairMessage {
val payload = StringLengthPrefixEncoding.formatKeys( val payload = StringLengthPrefixEncoding.formatKeys(
arrayOf("SP1=", ",SP2="), arrayOf("SP1=", ",SP2="),
arrayOf(sp1, sp2), arrayOf(sp1, sp2),
@ -70,7 +82,7 @@ internal class LTKExchanger(private val aapsLogger: AAPSLogger, private val msgI
) )
} }
fun sps1(): PairMessage { private fun sps1(): PairMessage {
val publicKey = X25519.publicFromPrivate(privateKey) val publicKey = X25519.publicFromPrivate(privateKey)
val payload = StringLengthPrefixEncoding.formatKeys( val payload = StringLengthPrefixEncoding.formatKeys(
arrayOf("SPS1="), arrayOf("SPS1="),
@ -84,6 +96,34 @@ internal class LTKExchanger(private val aapsLogger: AAPSLogger, private val msgI
) )
} }
private fun processSps1FromPod(msg: MessagePacket) {
aapsLogger.debug(LTag.PUMPBTCOMM, "Received SPS1 from pod: ${msg.payload.toHex()}")
}
private fun sps2(): PairMessage {
TODO("implement")
}
private fun validatePodSps2(podSps2: MessagePacket) {
TODO("implement")
}
private fun sp2(): ByteArray {
// This is GetPodStatus command, with page 0 parameter.
// We could replace that in the future with the serialized GetPodStatus()
return GET_POD_STATUS_HEX_COMMAND.hexStringToByteArray()
}
private fun sp0gp0(): PairMessage {
TODO("implement")
}
private fun validateP0(p0: MessagePacket) {
TODO("implement")
}
companion object { companion object {
private val GET_POD_STATUS_HEX_COMMAND = "ffc32dbd08030e0100008a" // TODO for now we are assuming this command is build out of constant parameters, use a proper command builder for that. private val GET_POD_STATUS_HEX_COMMAND = "ffc32dbd08030e0100008a" // TODO for now we are assuming this command is build out of constant parameters, use a proper command builder for that.