aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ui/src/main/java/com/wireguard/android/updater/Updater.kt
blob: 651e3cd7971b7b92f34dbb5894171c977691b590 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
 * Copyright © 2017-2023 WireGuard LLC. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */
package com.wireguard.android.updater

import android.Manifest
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageInstaller
import android.content.pm.PackageManager
import android.os.Build
import android.util.Base64
import android.util.Log
import androidx.core.content.ContextCompat
import androidx.core.content.IntentCompat
import com.wireguard.android.Application
import com.wireguard.android.BuildConfig
import com.wireguard.android.activity.MainActivity
import com.wireguard.android.util.UserKnobs
import com.wireguard.android.util.applicationScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.IOException
import java.net.HttpURLConnection
import java.net.URL
import java.nio.charset.StandardCharsets
import java.security.InvalidKeyException
import java.security.InvalidParameterException
import java.security.MessageDigest
import java.util.UUID
import kotlin.math.max
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds

object Updater {
    private const val TAG = "WireGuard/Updater"
    private const val UPDATE_URL_FMT = "https://download.wireguard.com/android-client/%s"
    private const val APK_NAME_PREFIX = BuildConfig.APPLICATION_ID + "-"
    private const val APK_NAME_SUFFIX = ".apk"
    private const val LATEST_FILE = "latest.sig"
    private const val RELEASE_PUBLIC_KEY_BASE64 = "RWTAzwGRYr3EC9px0Ia3fbttz8WcVN6wrOwWp2delz4el6SI8XmkKSMp"
    private val CURRENT_VERSION by lazy { Version(BuildConfig.VERSION_NAME) }

    private val updaterScope = CoroutineScope(Job() + Dispatchers.IO)

    private fun installer(context: Context): String = try {
        val packageName = context.packageName
        val pm = context.packageManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            pm.getInstallSourceInfo(packageName).installingPackageName ?: ""
        } else {
            @Suppress("DEPRECATION")
            pm.getInstallerPackageName(packageName) ?: ""
        }
    } catch (_: Throwable) {
        ""
    }

    fun installerIsGooglePlay(context: Context): Boolean = installer(context) == "com.android.vending"

    sealed class Progress {
        object Complete : Progress()
        class Available(val version: String) : Progress() {
            fun update() {
                applicationScope.launch {
                    UserKnobs.setUpdaterNewerVersionConsented(version)
                }
            }
        }

        object Rechecking : Progress()
        class Downloading(val bytesDownloaded: ULong, val bytesTotal: ULong) : Progress()
        object Installing : Progress()
        class NeedsUserIntervention(val intent: Intent, private val id: Int) : Progress() {

            private suspend fun installerActive(): Boolean {
                if (mutableState.firstOrNull() != this@NeedsUserIntervention)
                    return true
                try {
                    if (Application.get().packageManager.packageInstaller.getSessionInfo(id)?.isActive == true)
                        return true
                } catch (_: SecurityException) {
                    return true
                }
                return false
            }

            fun markAsDone() {
                applicationScope.launch {
                    if (installerActive())
                        return@launch
                    delay(7.seconds)
                    if (installerActive())
                        return@launch
                    emitProgress(Failure(Exception("Ignored by user")))
                }
            }
        }

        class Failure(val error: Throwable) : Progress() {
            fun retry() {
                updaterScope.launch {
                    downloadAndUpdateWrapErrors()
                }
            }
        }

        class Corrupt(private val betterFile: String?) : Progress() {
            val downloadUrl: String
                get() = UPDATE_URL_FMT.format(betterFile ?: "")
        }
    }

    private val mutableState = MutableStateFlow<Progress>(Progress.Complete)
    val state = mutableState.asStateFlow()

    private suspend fun emitProgress(progress: Progress, force: Boolean = false) {
        if (force || mutableState.firstOrNull()?.javaClass != progress.javaClass)
            mutableState.emit(progress)
    }

    private class Sha256Digest(hex: String) {
        val bytes: ByteArray

        init {
            if (hex.length != 64)
                throw InvalidParameterException("SHA256 hashes must be 32 bytes long")
            bytes = hex.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
        }
    }

    @OptIn(ExperimentalUnsignedTypes::class)
    private class Version(version: String) : Comparable<Version> {
        val parts: ULongArray

        init {
            val strParts = version.split(".")
            if (strParts.isEmpty())
                throw InvalidParameterException("Version has no parts")
            parts = ULongArray(strParts.size)
            for (i in parts.indices) {
                parts[i] = strParts[i].toULong()
            }
        }

        override fun toString(): String {
            return parts.joinToString(".")
        }

        override fun compareTo(other: Version): Int {
            for (i in 0 until max(parts.size, other.parts.size)) {
                val lhsPart = if (i < parts.size) parts[i] else 0UL
                val rhsPart = if (i < other.parts.size) other.parts[i] else 0UL
                if (lhsPart > rhsPart)
                    return 1
                else if (lhsPart < rhsPart)
                    return -1
            }
            return 0
        }
    }

    private class Update(val fileName: String, val version: Version, val hash: Sha256Digest)

    private fun versionOfFile(name: String): Version? {
        if (!name.startsWith(APK_NAME_PREFIX) || !name.endsWith(APK_NAME_SUFFIX))
            return null
        return try {
            Version(name.substring(APK_NAME_PREFIX.length, name.length - APK_NAME_SUFFIX.length))
        } catch (_: Throwable) {
            null
        }
    }

    private fun verifySignedFileList(signifyDigest: String): List<Update> {
        val updates = ArrayList<Update>(1)
        val publicKeyBytes = Base64.decode(RELEASE_PUBLIC_KEY_BASE64, Base64.DEFAULT)
        if (publicKeyBytes == null || publicKeyBytes.size != 32 + 10 || publicKeyBytes[0] != 'E'.code.toByte() || publicKeyBytes[1] != 'd'.code.toByte())
            throw InvalidKeyException("Invalid public key")
        val lines = signifyDigest.split("\n", limit = 3)
        if (lines.size != 3)
            throw InvalidParameterException("Invalid signature format: too few lines")
        if (!lines[0].startsWith("untrusted comment: "))
            throw InvalidParameterException("Invalid signature format: missing comment")
        val signatureBytes = Base64.decode(lines[1], Base64.DEFAULT)
        if (signatureBytes == null || signatureBytes.size != 64 + 10)
            throw InvalidParameterException("Invalid signature format: wrong sized or missing signature")
        for (i in 0..9) {
            if (signatureBytes[i] != publicKeyBytes[i])
                throw InvalidParameterException("Invalid signature format: wrong signer")
        }
        if (!Ed25519.verify(
                lines[2].toByteArray(StandardCharsets.UTF_8),
                signatureBytes.sliceArray(10 until 10 + 64),
                publicKeyBytes.sliceArray(10 until 10 + 32)
            )
        )
            throw SecurityException("Invalid signature")
        for (line in lines[2].split("\n").dropLastWhile { it.isEmpty() }) {
            val components = line.split("  ", limit = 2)
            if (components.size != 2)
                throw InvalidParameterException("Invalid file list format: too few components")
            /* If version is null, it's not a file we understand, but still a legitimate entry, so don't throw. */
            val version = versionOfFile(components[1]) ?: continue
            updates.add(Update(components[1], version, Sha256Digest(components[0])))
        }
        return updates
    }

    private fun checkForUpdates(): Update? {
        val connection = URL(UPDATE_URL_FMT.format(LATEST_FILE)).openConnection() as HttpURLConnection
        connection.setRequestProperty("User-Agent", Application.USER_AGENT)
        connection.connect()
        if (connection.responseCode != HttpURLConnection.HTTP_OK)
            throw IOException(connection.responseMessage)
        var fileListBytes = ByteArray(1024 * 512 /* 512 KiB */)
        connection.inputStream.use {
            val len = it.read(fileListBytes)
            if (len <= 0)
                throw IOException("File list is empty")
            fileListBytes = fileListBytes.sliceArray(0 until len)
        }
        return verifySignedFileList(fileListBytes.decodeToString()).maxByOrNull { it.version }
    }

    private suspend fun downloadAndUpdate() = withContext(Dispatchers.IO) {
        val receiver = InstallReceiver()
        val context = Application.get().applicationContext
        val pendingIntent = withContext(Dispatchers.Main) {
            ContextCompat.registerReceiver(context, receiver, IntentFilter(receiver.sessionId), ContextCompat.RECEIVER_NOT_EXPORTED)
            PendingIntent.getBroadcast(
                context,
                0,
                Intent(receiver.sessionId).setPackage(context.packageName),
                PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
            )
        }

        emitProgress(Progress.Rechecking)
        val update = checkForUpdates()
        if (update == null || update.version <= CURRENT_VERSION) {
            emitProgress(Progress.Complete)
            return@withContext
        }

        emitProgress(Progress.Downloading(0UL, 0UL), true)
        val connection = URL(UPDATE_URL_FMT.format(update.fileName)).openConnection() as HttpURLConnection
        connection.setRequestProperty("User-Agent", Application.USER_AGENT)
        connection.connect()
        if (connection.responseCode != HttpURLConnection.HTTP_OK)
            throw IOException("Update could not be fetched: ${connection.responseCode}")

        var downloadedByteLen: ULong = 0UL
        val totalByteLen = (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) connection.contentLengthLong else connection.contentLength).toLong().toULong()
        val fileBytes = ByteArray(1024 * 32 /* 32 KiB */)
        val digest = MessageDigest.getInstance("SHA-256")
        emitProgress(Progress.Downloading(downloadedByteLen, totalByteLen), true)

        val installer = context.packageManager.packageInstaller
        val params = PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
            params.setRequireUserAction(PackageInstaller.SessionParams.USER_ACTION_NOT_REQUIRED)
        params.setAppPackageName(context.packageName) /* Enforces updates; disallows new apps. */
        val session = installer.openSession(installer.createSession(params))
        var sessionFailure = true
        try {
            val installDest = session.openWrite(receiver.sessionId, 0, -1)

            installDest.use { dest ->
                connection.inputStream.use { src ->
                    while (true) {
                        val readLen = src.read(fileBytes)
                        if (readLen <= 0)
                            break

                        digest.update(fileBytes, 0, readLen)
                        dest.write(fileBytes, 0, readLen)

                        downloadedByteLen += readLen.toUInt()
                        emitProgress(Progress.Downloading(downloadedByteLen, totalByteLen), true)

                        if (downloadedByteLen >= 1024UL * 1024UL * 100UL /* 100 MiB */)
                            throw IOException("File too large")
                    }
                }
            }

            emitProgress(Progress.Installing)
            if (!digest.digest().contentEquals(update.hash.bytes))
                throw SecurityException("Update has invalid hash")
            sessionFailure = false
        } finally {
            if (sessionFailure) {
                session.abandon()
                session.close()
            }
        }
        session.commit(pendingIntent.intentSender)
        session.close()
    }

    private var updating = false
    private suspend fun downloadAndUpdateWrapErrors() {
        if (updating)
            return
        updating = true
        try {
            downloadAndUpdate()
        } catch (e: Throwable) {
            Log.e(TAG, "Update failure", e)
            emitProgress(Progress.Failure(e))
        }
        updating = false
    }

    private class InstallReceiver : BroadcastReceiver() {
        val sessionId = UUID.randomUUID().toString()

        override fun onReceive(context: Context, intent: Intent) {
            if (sessionId != intent.action)
                return

            when (val status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, PackageInstaller.STATUS_FAILURE_INVALID)) {
                PackageInstaller.STATUS_PENDING_USER_ACTION -> {
                    val id = intent.getIntExtra(PackageInstaller.EXTRA_SESSION_ID, 0)
                    val userIntervention = IntentCompat.getParcelableExtra(intent, Intent.EXTRA_INTENT, Intent::class.java)!!
                    applicationScope.launch {
                        emitProgress(Progress.NeedsUserIntervention(userIntervention, id))
                    }
                }

                PackageInstaller.STATUS_SUCCESS -> {
                    applicationScope.launch {
                        emitProgress(Progress.Complete)
                    }
                    context.applicationContext.unregisterReceiver(this)
                }

                else -> {
                    val id = intent.getIntExtra(PackageInstaller.EXTRA_SESSION_ID, 0)
                    try {
                        context.applicationContext.packageManager.packageInstaller.abandonSession(id)
                    } catch (_: SecurityException) {
                    }
                    val message = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE) ?: "Installation error $status"
                    applicationScope.launch {
                        val e = Exception(message)
                        Log.e(TAG, "Update failure", e)
                        emitProgress(Progress.Failure(e))
                    }
                    context.applicationContext.unregisterReceiver(this)
                }
            }
        }
    }

    fun monitorForUpdates() {
        if (BuildConfig.DEBUG)
            return

        val context = Application.get()

        if (installerIsGooglePlay(context))
            return

        if (!if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
                @Suppress("DEPRECATION")
                context.packageManager.getPackageInfo(context.packageName, PackageManager.GET_PERMISSIONS)
            } else {
                context.packageManager.getPackageInfo(context.packageName, PackageManager.PackageInfoFlags.of(PackageManager.GET_PERMISSIONS.toLong()))
            }.requestedPermissions.contains(Manifest.permission.REQUEST_INSTALL_PACKAGES)
        ) {
            if (installer(context).isNotEmpty()) {
                updaterScope.launch {
                    val update = try {
                        checkForUpdates()
                    } catch (_: Throwable) {
                        null
                    }
                    emitProgress(Progress.Corrupt(update?.fileName))
                }
            }
            return
        }

        updaterScope.launch {
            if (UserKnobs.updaterNewerVersionSeen.firstOrNull()?.let { Version(it) > CURRENT_VERSION } == true)
                return@launch

            var waitTime = 15
            while (true) {
                try {
                    val update = checkForUpdates() ?: continue
                    if (update.version > CURRENT_VERSION) {
                        Log.i(TAG, "Update available: ${update.version}")
                        UserKnobs.setUpdaterNewerVersionSeen(update.version.toString())
                        return@launch
                    }
                } catch (_: Throwable) {
                }
                delay(waitTime.minutes)
                waitTime = 45
            }
        }

        UserKnobs.updaterNewerVersionSeen.onEach { ver ->
            if (
                ver != null &&
                Version(ver) > CURRENT_VERSION &&
                UserKnobs.updaterNewerVersionConsented.firstOrNull()?.let { Version(it) > CURRENT_VERSION } != true
            )
                emitProgress(Progress.Available(ver))
        }.launchIn(applicationScope)

        UserKnobs.updaterNewerVersionConsented.onEach { ver ->
            if (ver != null && Version(ver) > CURRENT_VERSION)
                updaterScope.launch {
                    downloadAndUpdateWrapErrors()
                }
        }.launchIn(applicationScope)
    }

    class AppUpdatedReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            if (intent.action != Intent.ACTION_MY_PACKAGE_REPLACED)
                return

            if (installer(context) != context.packageName)
                return

            /* TODO: does not work because of restrictions placed on broadcast receivers. */
            val start = Intent(context, MainActivity::class.java)
            start.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            start.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(start)
        }
    }
}