• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.devicediagnostics
17 
18 import android.os.Bundle
19 import androidx.preference.Preference
20 import androidx.preference.PreferenceFragmentCompat
21 import com.google.android.attestation.AuthorizationList
22 import com.google.android.attestation.ParsedAttestationRecord
23 import java.util.Optional
24 
25 class DisplayAttestationResultFragment(
26     private val record: ParsedAttestationRecord,
27     private val result: AttestationResult,
28 ) : PreferenceFragmentCompat() {
onCreatePreferencesnull29     override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
30         setPreferencesFromResource(R.xml.attestation_details, rootKey)
31 
32         // By default, show a failure code for IMEI/Serial. It will get overridden if it's actually
33         // present.
34         if (
35             result == AttestationResult.VERIFIED || result == AttestationResult.SKIPPED_VERIFICATION
36         ) {
37             if (result == AttestationResult.VERIFIED) {
38                 show("attestation_verified", activity!!.resources.getString(R.string.yes))
39             } else {
40                 hide("attestation_verified")
41             }
42             show("attestation_imei", activity!!.resources.getString(R.string.unknown))
43             show("attestation_serial", activity!!.resources.getString(R.string.unknown))
44         } else {
45             show("attestation_verified", activity!!.resources.getString(R.string.no))
46             show("attestation_imei", activity!!.resources.getString(R.string.attestation_failed))
47             show("attestation_serial", activity!!.resources.getString(R.string.attestation_failed))
48         }
49 
50         if (isDeviceLocked(record)) {
51             show("attestation_device_locked", activity!!.resources.getString(R.string.yes))
52         } else {
53             show("attestation_device_locked", activity!!.resources.getString(R.string.no))
54         }
55 
56         show(
57             "attestation_verified_boot_state",
58             activity!!.resources.getString(getVerifiedBootStateResId(record)),
59         )
60         show("attestation_security_level", record.attestationSecurityLevel.name)
61         show("attestation_keymaster_version", record.keymasterVersion.toString())
62         show("attestation_keymaster_security_level", record.keymasterSecurityLevel.name)
63         processAuthorizationList(record.softwareEnforced)
64         processAuthorizationList(record.teeEnforced)
65     }
66 
processAuthorizationListnull67     private fun processAuthorizationList(list: AuthorizationList) {
68         showInt("attestation_os_version", list.osVersion)
69         show("attestation_brand", list.attestationIdBrand)
70         show("attestation_device", list.attestationIdDevice)
71         show("attestation_product", list.attestationIdProduct)
72         show("attestation_serial", list.attestationIdSerial)
73         show("attestation_imei", list.attestationIdImei)
74         show("attestation_second_imei", list.attestationIdSecondImei)
75         show("attestation_meid", list.attestationIdMeid)
76         show("attestation_manufacturer", list.attestationIdManufacturer)
77         show("attestation_model", list.attestationIdModel)
78         showInt("attestation_vendor_patch_level", list.vendorPatchLevel)
79         showInt("attestation_boot_patch_level", list.bootPatchLevel)
80     }
81 
showIntnull82     private fun showInt(key: String, value: Optional<Int>?) {
83         if (value != null && value.isPresent) return show(key, value.get().toString())
84     }
85 
shownull86     private fun show(key: String, value: Optional<ByteArray>) {
87         if (value.isPresent) return show(key, value.get().toString(Charsets.UTF_8))
88     }
89 
shownull90     private fun show(key: String, text: String) {
91         val pref = findPreference<Preference>(key)!!
92         pref.summary = text
93         pref.isVisible = true
94     }
95 
hidenull96     private fun hide(key: String) {
97         findPreference<Preference>(key)!!.isVisible = false
98     }
99 }
100 
getVerifiedBootStateResIdnull101 fun getVerifiedBootStateResId(record: ParsedAttestationRecord): Int {
102     if (getVerifiedBootState(record)) {
103         return R.string.avb_verified
104     }
105     return R.string.avb_not_verified
106 }
107