Skip to content

Commit

Permalink
extract constants to PoTokenConstants
Browse files Browse the repository at this point in the history
  • Loading branch information
DaVinci9196 committed Dec 19, 2023
1 parent dc6daaa commit 47450aa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.google.android.gms.potokens.utils

object PoTokenConstants {

const val TOKEN_URL =
"https://deviceintegritytokens-pa.googleapis.com/v1/getPoIntegrityToken?alt=proto&key=AIzaSyBtL0AK6Hzgr69rQyeyhi-V1lmtsPGZd1M"

const val TYPE_URL = "type.googleapis.com/google.crypto.tink.AesGcmKey"

const val KEY_TOKEN = "po-token-fast"

const val KEY_FAST = "po-fast-key"

const val KEY_FALLBACK = "extraKeysRetainedInFallback"

const val KEY_DESC = "tokenDesc"

const val KEY_BACKUP = "tokenBackup"

const val KEY_SET_STR = "keySetStr"

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.google.android.gms.potokens.utils

import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.text.TextUtils
import android.util.Base64
import android.util.Log
Expand All @@ -29,6 +28,7 @@ import com.google.android.gms.tasks.Tasks
import okio.ByteString
import okio.ByteString.Companion.toByteString
import org.microg.gms.common.Constants
import org.microg.gms.profile.Build
import java.nio.ByteBuffer
import java.security.MessageDigest
import java.security.SecureRandom
Expand All @@ -41,9 +41,6 @@ import kotlin.math.abs

class PoTokenHelper {

private val tokenUrl: String
get() = "https://deviceintegritytokens-pa.googleapis.com/v1/getPoIntegrityToken?alt=proto&key=AIzaSyBtL0AK6Hzgr69rQyeyhi-V1lmtsPGZd1M"

private fun buildKeySet(): KeySet {
val builder: KeySet.Builder = KeySet.Builder()
val keyBuilder: Key.Builder = Key.Builder()
Expand All @@ -53,7 +50,7 @@ class PoTokenHelper {
val byteString = initKey.toByteString()
val cipherKey: CipherKey = CipherKey.Builder().value_(byteString).build()
val keyData: KeyData = KeyData.Builder()
.typeUrl("type.googleapis.com/google.crypto.tink.AesGcmKey")
.typeUrl(PoTokenConstants.TYPE_URL)
.keyMaterialType(1)
.value_(cipherKey).build()
keyBuilder.data_(keyData)
Expand Down Expand Up @@ -95,7 +92,7 @@ class PoTokenHelper {
map: Map<String, String>?
): String? {
try {
val resultTask = DroidGuard.getClient(context).getResults("po-token-fast", map, request)
val resultTask = DroidGuard.getClient(context).getResults(PoTokenConstants.KEY_TOKEN, map, request)
return Tasks.await(resultTask, 15, TimeUnit.SECONDS)
} catch (e: Throwable) {
e.printStackTrace()
Expand All @@ -110,11 +107,11 @@ class PoTokenHelper {
request.setOpenHandles(2)
request.setClientVersion(220221045)
keySet?.let {
request.bundle.putByteArray("po-fast-key", it.encode())
request.bundle.putByteArray(PoTokenConstants.KEY_FAST, it.encode())
}
val tmpList = ArrayList<String>()
tmpList.add("po-fast-key")
request.bundle.putStringArrayList("extraKeysRetainedInFallback", tmpList)
tmpList.add(PoTokenConstants.KEY_FAST)
request.bundle.putStringArrayList(PoTokenConstants.KEY_FALLBACK, tmpList)

val secureRandom = SecureRandom()
val randKeyBuf = ByteArray(0x20)
Expand Down Expand Up @@ -144,7 +141,7 @@ class PoTokenHelper {
val future = RequestFuture.newFuture<GetPoIntegrityTokenResponse>()
Log.d(TAG,"postPoTokenForGms ")
Volley.newRequestQueue(context).add(object : Request<GetPoIntegrityTokenResponse>(
Method.POST, tokenUrl, future) {
Method.POST, PoTokenConstants.TOKEN_URL, future) {

override fun deliverResponse(response: GetPoIntegrityTokenResponse?) {
future.onResponse(response)
Expand Down Expand Up @@ -175,19 +172,19 @@ class PoTokenHelper {
fun callPoToken(ctx: Context, packageName: String?, inputData: ByteArray?): ByteArray {
var keySet: KeySet? = null
val spUtil = SpUtil[ctx]
var tokenDesc = spUtil.getString("tokenDesc", "")
var tokenBackup = spUtil.getString("tokenBackup", "")
var keySetStr = spUtil.getString("keySetStr", "")
var tokenDesc = spUtil.getString(PoTokenConstants.KEY_DESC, "")
var tokenBackup = spUtil.getString(PoTokenConstants.KEY_BACKUP, "")
var keySetStr = spUtil.getString(PoTokenConstants.KEY_SET_STR, "")

if (TextUtils.isEmpty(tokenDesc) || TextUtils.isEmpty(tokenBackup) || TextUtils.isEmpty(keySetStr)) {
keySet = buildKeySet()
val response = PoTokenHelper().getPoIntegrityToken(ctx, keySet)
tokenDesc = Base64.encodeToString(response?.desc?.toByteArray(), Base64.DEFAULT)
tokenBackup = Base64.encodeToString(response?.backup?.toByteArray(), Base64.DEFAULT)
keySetStr = Base64.encodeToString(keySet.encode(), Base64.DEFAULT)
spUtil.save("tokenDesc", tokenDesc)
spUtil.save("tokenBackup", tokenBackup)
spUtil.save("keySetStr", keySetStr)
spUtil.save(PoTokenConstants.KEY_DESC, tokenDesc)
spUtil.save(PoTokenConstants.KEY_BACKUP, tokenBackup)
spUtil.save(PoTokenConstants.KEY_SET_STR, keySetStr)
} else if (!TextUtils.isEmpty(keySetStr)) {
val result = Base64.decode(keySetStr, Base64.DEFAULT)
keySet = KeySet.ADAPTER.decode(result)
Expand Down

0 comments on commit 47450aa

Please sign in to comment.