If user changes back to default channel, use the standard key

This commit is contained in:
geeksville 2020-06-12 20:38:43 -07:00
parent aa3a8bf089
commit 6195874982
2 changed files with 18 additions and 1 deletions

View file

@ -9,6 +9,9 @@ import com.google.zxing.MultiFormatWriter
import com.journeyapps.barcodescanner.BarcodeEncoder
import java.net.MalformedURLException
/** Utility function to make it easy to declare byte arrays - FIXME move someplace better */
fun byteArrayOfInts(vararg ints: Int) = ByteArray(ints.size) { pos -> ints[pos].toByte() }
data class Channel(
val settings: MeshProtos.ChannelSettings = MeshProtos.ChannelSettings.getDefaultInstance()
@ -17,6 +20,12 @@ data class Channel(
// Note: this string _SHOULD NOT BE LOCALIZED_ because it directly hashes to values used on the device for the default channel name.
val defaultChannelName = "Default"
// These bytes must math the well known and not secret bytes used the default channel AES128 key device code
val channelDefaultKey = byteArrayOfInts(
0xd4, 0xf1, 0xbb, 0x3a, 0x20, 0x29, 0x07, 0x59,
0xf0, 0xbc, 0xff, 0xab, 0xcf, 0x4e, 0x69, 0xbf
)
// Placeholder when emulating
val emulated = Channel(
MeshProtos.ChannelSettings.newBuilder().setName(defaultChannelName)

View file

@ -147,12 +147,20 @@ class ChannelFragment : ScreenFragment("Channel"), Logging {
newSettings.name = channelNameEdit.text.toString().trim()
// Generate a new AES256 key (for any channel not named Default)
if (newSettings.name != Channel.defaultChannelName) {
if (!newSettings.name.equals(
Channel.defaultChannelName,
ignoreCase = true
)
) {
debug("ASSIGNING NEW AES256 KEY")
val random = SecureRandom()
val bytes = ByteArray(32)
random.nextBytes(bytes)
newSettings.psk = ByteString.copyFrom(bytes)
} else {
debug("ASSIGNING NEW default AES128 KEY")
newSettings.name = Channel.defaultChannelName // Fix any case errors
newSettings.psk = ByteString.copyFrom(Channel.channelDefaultKey)
}
// Try to change the radio, if it fails, tell the user why and throw away their redits