Skip to content

Commit

Permalink
refactor: add base 58 check for gateway identity keys (#2312)
Browse files Browse the repository at this point in the history
  • Loading branch information
zaneschepke authored Feb 28, 2025
1 parent 9e8735f commit 0721317
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
45 changes: 45 additions & 0 deletions nym-vpn-android/core/src/main/java/net/nymtech/vpn/util/Base58.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package net.nymtech.vpn.util

import java.math.BigInteger

object Base58 {
private const val ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
private val INDEXES = IntArray(128) { -1 }.also {
for (i in ALPHABET.indices) {
it[ALPHABET[i].code] = i
}
}

fun isValidBase58(input: String, expectedByteLength: Int = 32): Boolean {
try {
if (input.isEmpty() || input.any { it.code >= 128 || INDEXES[it.code] == -1 }) {
return false
}

val bytes = decode(input)

return bytes.size == expectedByteLength
} catch (e: Exception) {
return false
}
}

private fun decode(input: String): ByteArray {
if (input.isEmpty()) return ByteArray(0)

val bigInt = input.fold(BigInteger.ZERO) { acc, char ->
val index = INDEXES[char.code]
if (index == -1) throw IllegalArgumentException("Invalid Base58 character: $char")
acc.multiply(BigInteger.valueOf(58)).add(BigInteger.valueOf(index.toLong()))
}

val bytes = bigInt.toByteArray()

val leadingZeros = input.takeWhile { it == '1' }.length
return if (bytes[0].toInt() == 0 && bytes.size > 1) {
ByteArray(leadingZeros) + bytes.drop(1).toByteArray()
} else {
ByteArray(leadingZeros) + bytes
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal class NotificationManager private constructor(val context: Context) {
private const val VPN_CHANNEL_ID = "VpnForegroundChannel"
}

fun createNotificationChannel() {
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create the NotificationChannel.
val name = context.getString(R.string.channel_name)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.nymtech.vpn.util.extensions

import net.nymtech.vpn.backend.Tunnel
import net.nymtech.vpn.util.Base58
import nym_vpn_lib.EntryPoint
import nym_vpn_lib.ExitPoint
import nym_vpn_lib.TunnelEvent
Expand Down Expand Up @@ -39,16 +40,16 @@ fun String.asEntryPoint(): EntryPoint {
this == "random" -> EntryPoint.Random
this == "randomlowlatency" -> EntryPoint.RandomLowLatency
length == 2 -> EntryPoint.Location(this.uppercase())
length == 44 || length == 43 -> EntryPoint.Gateway(this)
Base58.isValidBase58(this, 32) -> EntryPoint.Gateway(this)
else -> EntryPoint.Random
}
}

fun String.asExitPoint(): ExitPoint {
return when (this.length) {
2 -> ExitPoint.Location(this.uppercase())
134 -> ExitPoint.Address(this)
44, 43 -> ExitPoint.Gateway(this)
return when {
length == 2 -> ExitPoint.Location(this.uppercase())
length == 134 -> ExitPoint.Address(this)
Base58.isValidBase58(this, 32) -> ExitPoint.Gateway(this)
else -> throw IllegalArgumentException("Invalid exit id")
}
}

0 comments on commit 0721317

Please sign in to comment.