Skip to content

Commit a5a9881

Browse files
committed
PI: Fix Failed to generate key pair and related crashes
1 parent 27bacdd commit a5a9881

File tree

2 files changed

+52
-47
lines changed

2 files changed

+52
-47
lines changed

vending-app/src/main/kotlin/com/google/android/finsky/IntegrityExtensions.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,16 @@ fun fetchCertificateChain(context: Context, attestationChallenge: ByteArray?): L
182182
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
183183
val devicePropertiesAttestationIncluded = context.packageManager.hasSystemFeature("android.software.device_id_attestation")
184184
val keyGenParameterSpecBuilder =
185-
KeyGenParameterSpec.Builder("integrity.api.key.alias", KeyProperties.PURPOSE_SIGN).setAlgorithmParameterSpec(ECGenParameterSpec("secp256r1")).setDigests(KeyProperties.DIGEST_SHA512)
186-
.setAttestationChallenge(attestationChallenge)
187-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
188-
keyGenParameterSpecBuilder.setDevicePropertiesAttestationIncluded(devicePropertiesAttestationIncluded)
189-
}
185+
KeyGenParameterSpec.Builder("integrity.api.key.alias", KeyProperties.PURPOSE_SIGN).apply {
186+
this.setAlgorithmParameterSpec(ECGenParameterSpec("secp256r1"))
187+
this.setDigests(KeyProperties.DIGEST_SHA512)
188+
if (devicePropertiesAttestationIncluded){
189+
this.setAttestationChallenge(attestationChallenge)
190+
}
191+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
192+
this.setDevicePropertiesAttestationIncluded(devicePropertiesAttestationIncluded)
193+
}
194+
}
190195
val keyGenParameterSpec = keyGenParameterSpecBuilder.build()
191196
val keyPairGenerator = KeyPairGenerator.getInstance("EC", "AndroidKeyStore").apply {
192197
initialize(keyGenParameterSpec)

vending-app/src/main/kotlin/com/google/android/finsky/expressintegrityservice/ExpressIntegrityService.kt

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -229,52 +229,52 @@ private class ExpressIntegrityServiceImpl(private val context: Context, override
229229
override fun requestExpressIntegrityToken(bundle: Bundle, callback: IExpressIntegrityServiceCallback?) {
230230
Log.d(TAG, "requestExpressIntegrityToken bundle:$bundle")
231231
lifecycleScope.launchWhenCreated {
232-
val expressIntegritySession = ExpressIntegritySession(
233-
packageName = bundle.getString(KEY_PACKAGE_NAME) ?: "",
234-
cloudProjectVersion = bundle.getLong(KEY_CLOUD_PROJECT, 0L),
235-
sessionId = Random.nextLong(),
236-
requestHash = bundle.getString(KEY_NONCE),
237-
originatingWarmUpSessionId = bundle.getLong(KEY_WARM_UP_SID, 0),
238-
verdictOptOut = bundle.getIntegerArrayList(KEY_REQUEST_VERDICT_OPT_OUT),
239-
webViewRequestMode = bundle.getInt(KEY_REQUEST_MODE, 0)
240-
)
241-
242-
if (TextUtils.isEmpty(expressIntegritySession.packageName)) {
243-
Log.w(TAG, "packageName is empty.")
244-
callback?.onRequestResult(bundleOf(KEY_ERROR to IntegrityErrorCode.INTERNAL_ERROR))
245-
return@launchWhenCreated
246-
}
247-
248-
if (expressIntegritySession.cloudProjectVersion <= 0L) {
249-
Log.w(TAG, "cloudProjectVersion error")
250-
callback?.onRequestResult(bundleOf(KEY_ERROR to IntegrityErrorCode.CLOUD_PROJECT_NUMBER_IS_INVALID))
251-
return@launchWhenCreated
252-
}
232+
runCatching {
233+
val expressIntegritySession = ExpressIntegritySession(
234+
packageName = bundle.getString(KEY_PACKAGE_NAME) ?: "",
235+
cloudProjectVersion = bundle.getLong(KEY_CLOUD_PROJECT, 0L),
236+
sessionId = Random.nextLong(),
237+
requestHash = bundle.getString(KEY_NONCE),
238+
originatingWarmUpSessionId = bundle.getLong(KEY_WARM_UP_SID, 0),
239+
verdictOptOut = bundle.getIntegerArrayList(KEY_REQUEST_VERDICT_OPT_OUT),
240+
webViewRequestMode = bundle.getInt(KEY_REQUEST_MODE, 0)
241+
)
253242

254-
if (expressIntegritySession.requestHash?.length!! > 500) {
255-
Log.w(TAG, "requestHash error")
256-
callback?.onRequestResult(bundleOf(KEY_ERROR to IntegrityErrorCode.REQUEST_HASH_TOO_LONG))
257-
return@launchWhenCreated
258-
}
243+
if (TextUtils.isEmpty(expressIntegritySession.packageName)) {
244+
Log.w(TAG, "packageName is empty.")
245+
callback?.onRequestResult(bundleOf(KEY_ERROR to IntegrityErrorCode.INTERNAL_ERROR))
246+
return@launchWhenCreated
247+
}
259248

260-
updateExpressSessionTime(context, expressIntegritySession, refreshWarmUpMethodTime = false, refreshRequestMethodTime = true)
249+
if (expressIntegritySession.cloudProjectVersion <= 0L) {
250+
Log.w(TAG, "cloudProjectVersion error")
251+
callback?.onRequestResult(bundleOf(KEY_ERROR to IntegrityErrorCode.CLOUD_PROJECT_NUMBER_IS_INVALID))
252+
return@launchWhenCreated
253+
}
261254

262-
val defaultAccountName: String = runCatching {
263-
if (expressIntegritySession.webViewRequestMode != 0) {
264-
RESULT_UN_AUTH
265-
} else {
266-
AccountManager.get(context).getAccountsByType(DEFAULT_ACCOUNT_TYPE).firstOrNull()?.name ?: RESULT_UN_AUTH
255+
if (expressIntegritySession.requestHash?.length!! > 500) {
256+
Log.w(TAG, "requestHash error")
257+
callback?.onRequestResult(bundleOf(KEY_ERROR to IntegrityErrorCode.REQUEST_HASH_TOO_LONG))
258+
return@launchWhenCreated
267259
}
268-
}.getOrDefault(RESULT_UN_AUTH)
269260

270-
val integrityRequestWrapper = getIntegrityRequestWrapper(context, expressIntegritySession, defaultAccountName)
271-
if (integrityRequestWrapper == null) {
272-
Log.w(TAG, "integrityRequestWrapper is null")
273-
callback?.onRequestResult(bundleOf(KEY_ERROR to IntegrityErrorCode.INTEGRITY_TOKEN_PROVIDER_INVALID))
274-
return@launchWhenCreated
275-
}
261+
updateExpressSessionTime(context, expressIntegritySession, refreshWarmUpMethodTime = false, refreshRequestMethodTime = true)
262+
263+
val defaultAccountName: String = runCatching {
264+
if (expressIntegritySession.webViewRequestMode != 0) {
265+
RESULT_UN_AUTH
266+
} else {
267+
AccountManager.get(context).getAccountsByType(DEFAULT_ACCOUNT_TYPE).firstOrNull()?.name ?: RESULT_UN_AUTH
268+
}
269+
}.getOrDefault(RESULT_UN_AUTH)
270+
271+
val integrityRequestWrapper = getIntegrityRequestWrapper(context, expressIntegritySession, defaultAccountName)
272+
if (integrityRequestWrapper == null) {
273+
Log.w(TAG, "integrityRequestWrapper is null")
274+
callback?.onRequestResult(bundleOf(KEY_ERROR to IntegrityErrorCode.INTEGRITY_TOKEN_PROVIDER_INVALID))
275+
return@launchWhenCreated
276+
}
276277

277-
try {
278278
val integritySession = IntermediateIntegritySession.Builder().creationTime(makeTimestamp(System.currentTimeMillis())).requestHash(expressIntegritySession.requestHash)
279279
.sessionId(Random.nextBytes(8).toByteString()).timestampMillis(0).build()
280280

@@ -297,8 +297,8 @@ private class ExpressIntegrityServiceImpl(private val context: Context, override
297297
)
298298
)
299299
Log.d(TAG, "requestExpressIntegrityToken token: $token, sid: ${expressIntegritySession.sessionId}, mode: ${expressIntegritySession.webViewRequestMode}")
300-
} catch (exception: RemoteException) {
301-
Log.e(TAG, "requesting token has failed for ${expressIntegritySession.packageName}.")
300+
}.onFailure {
301+
Log.e(TAG, "requesting token has failed for ${bundle.getString(KEY_PACKAGE_NAME)}.")
302302
callback?.onRequestResult(bundleOf(KEY_ERROR to IntegrityErrorCode.INTEGRITY_TOKEN_PROVIDER_INVALID))
303303
}
304304
}

0 commit comments

Comments
 (0)