Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9cb73a12 authored by Jonathan Klee's avatar Jonathan Klee
Browse files

Update DroidGuard for PlayIntegrity

parent ba86de1f
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -72,9 +72,9 @@ public class DroidGuardChimeraService extends TracingIntentService {
        } else {
            Log.d("GmsGuardChimera", "c(null)", new RuntimeException().fillInStackTrace());
        }
        byte[] bytes = b.createPingHandle(getPackageName(), "full", b(""), ping).run(Collections.emptyMap());
        byte[] bytes = b.createPingHandle("com.google.android.gms",  "full", b(""), ping).run(Collections.emptyMap());
        Log.d("GmsGuardChimera", "c.bytes = " + Base64.encodeToString(bytes, Base64.NO_WRAP));
        Request fastRequest = b.createRequest("fast", getPackageName(), null, bytes);
        Request fastRequest = b.createRequest("fast", "com.google.android.gms", null, bytes);
        b.fetchFromServer("fast", fastRequest);
    }

+17 −2
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ package org.microg.gms.droidguard.core

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.os.ConditionVariable
import android.os.ParcelFileDescriptor
import android.os.Parcelable
@@ -80,17 +81,31 @@ class DroidGuardHandleImpl(private val context: Context, private val packageName
        return DroidGuardInitReply(null, null)
    }

    override fun initWithStartArguments(arguments: Bundle?): DroidGuardInitReply {
        Log.d(TAG, "initWithStartArguments()")
        val handleProxy = factory.createHandle(arguments!!)
        handleProxy.init()
        handleProxy.close()
        //this.handleProxy = handleProxy
        this.condition.open()
        return DroidGuardInitReply(null, null)
    }

    override fun snapshot(map: MutableMap<Any?, Any?>): ByteArray {
        Log.d(TAG, "snapshot()")
        condition.block()
        handleInitError?.let { return FallbackCreator.create(flow, context, map, it) }
        handleInitError?.let {
            return FallbackCreator.create(flow, context, map, it)
        }
        val handleProxy = this.handleProxy ?: return FallbackCreator.create(flow, context, map, IllegalStateException())

        return try {
            handleProxy.handle::class.java.getDeclaredMethod("ss", Map::class.java).invoke(handleProxy.handle, map) as ByteArray
        } catch (e: Exception) {
            try {
                throw BytesException(handleProxy.extra, e)
            } catch (e2: Exception) {
                Log.e(TAG, "exception caught", e)
                FallbackCreator.create(flow, context, map, e2)
            }
        }
@@ -111,6 +126,6 @@ class DroidGuardHandleImpl(private val context: Context, private val packageName
    companion object {
        private const val TAG = "GmsGuardHandleImpl"
        private val LOW_LATENCY_ENABLED = false
        private val NOT_LOW_LATENCY_FLOWS = setOf("ad_attest", "attest", "checkin", "federatedMachineLearningReduced", "msa-f", "ad-event-attest-token")
        private val NOT_LOW_LATENCY_FLOWS = setOf("ad_attest", "attest", "checkin", "federatedMachineLearningReduced", "msa-f", "ad-event-attest-token", "pia_attest", "pia_attest_e1")
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -21,6 +21,6 @@ class DroidGuardServiceBroker(val service: DroidGuardChimeraService) : AbstractG

    override fun handleServiceRequest(callback: IGmsCallbacks?, request: GetServiceRequest?, service: GmsService?) {
        val packageName = PackageUtils.getAndCheckCallingPackageOrExtendedAccess(this.service, request!!.packageName)
        callback!!.onPostInitComplete(0, DroidGuardServiceImpl(this.service, packageName!!), null)
        callback!!.onPostInitComplete(0, DroidGuardServiceImpl(this.service, "com.google.android.gms"), null)
    }
}
+9 −0
Original line number Diff line number Diff line
@@ -19,6 +19,15 @@ class HandleProxy(val handle: Any, val vmKey: String, val extra: ByteArray = Byt
            vmKey
    )

    constructor(clazz: Class<*>, vmKey: String, context: Context, data: Bundle) : this (
        kotlin.runCatching {
            clazz.getDeclaredConstructor(Context::class.java, Parcelable::class.java).newInstance(context, data)
        }.getOrElse {
            throw BytesException(ByteArray(0), it)
        },
        vmKey
    )

    constructor(clazz: Class<*>, context: Context, flow: String?, byteCode: ByteArray, callback: Any, vmKey: String, extra: ByteArray, bundle: Bundle?) : this(
            kotlin.runCatching {
                clazz.getDeclaredConstructor(Context::class.java, String::class.java, ByteArray::class.java, Object::class.java, Bundle::class.java).newInstance(context, flow, byteCode, callback, bundle)
+20 −0
Original line number Diff line number Diff line
@@ -6,6 +6,8 @@
package org.microg.gms.droidguard.core

import android.content.Context
import android.os.Bundle
import android.util.Log
import com.android.volley.NetworkResponse
import com.android.volley.VolleyError
import com.android.volley.toolbox.RequestFuture
@@ -31,6 +33,10 @@ class HandleProxyFactory(private val context: Context) {
    private val version = VersionUtil(context)
    private val queue = Volley.newRequestQueue(context)

    fun createHandle(bundle: Bundle): HandleProxy {
        return createHandleProxy(bundle)
    }

    fun createHandle(packageName: String, flow: String?, callback: GuardCallback, request: DroidGuardResultsRequest?): HandleProxy {
        val (vmKey, byteCode, bytes) = readFromDatabase(flow) ?: fetchFromServer(flow, packageName)
        return createHandleProxy(flow, vmKey, byteCode, bytes, callback, request)
@@ -162,9 +168,23 @@ class HandleProxyFactory(private val context: Context) {
    private fun createHandleProxy(flow: String?, vmKey: String, byteCode: ByteArray, extra: ByteArray, callback: GuardCallback, request: DroidGuardResultsRequest?): HandleProxy {
        ProfileManager.ensureInitialized(context)
        val clazz = loadClass(vmKey, extra)
        /*if (request != null) {
            for (key in request.bundle.keySet()) {
                Log.i("jklee", "createHandleProxy key=$key value=${request.bundle.get(key)}")
            }
        } else {
            Log.i("jklee", "createHandleProxy bundle=null")
        }*/
        return HandleProxy(clazz, context, flow, byteCode, callback, vmKey, extra, request?.bundle)
    }

    private fun createHandleProxy(bundle: Bundle): HandleProxy {
        ProfileManager.ensureInitialized(context)
        val vmKey = bundle.getString("h")!!.lowercase(Locale.getDefault())
        val clazz = loadClass(vmKey, ByteArray(0))
        return HandleProxy(clazz, vmKey, context, bundle)
    }

    fun getTheApkFile(vmKey: String) = File(getCacheDir(vmKey), "the.apk")
    private fun getCacheDir() = context.getDir(CACHE_FOLDER_NAME, Context.MODE_PRIVATE)
    private fun getCacheDir(vmKey: String) = File(getCacheDir(), vmKey)
Loading