Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ android {
minSdk = 24
targetSdk = 35
versionCode = gitCommitCount
versionName = '2.0.4'
versionName = '2.0.5'
resourceConfigurations += supportedLocales
optimization {
keepRules {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ ASN1Sequence getAttestationSequence(X509Certificate x509Cert)
return Asn1Utils.getAsn1SequenceFromBytes(attestationExtensionBytes);
}

public int getAttestationSecurityLevel() {
@Override
protected int realAttestationSecurityLevel() {
return attestationSecurityLevel;
}

public RootOfTrust getRootOfTrust() {
@Override
protected RootOfTrust realRootOfTrust() {
RootOfTrust tee = teeEnforced.getRootOfTrust();
if (tee != null) return tee;
return softwareEnforced.getRootOfTrust();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,40 @@ public int getAttestationVersion() {
return attestationVersion;
}

public abstract int getAttestationSecurityLevel();
/**
* Display-only variant used by the simulated report: reports TEE as the security level
* and returns a chosen root of trust. Every other field is shared with the real data,
* and the real parse result is left untouched.
*/
public void applyFakeReport(io.github.vvb2060.keyattestation.home.FakeReportState state) {
this.fakeReport = state;
}

private io.github.vvb2060.keyattestation.home.FakeReportState fakeReport;

public int getAttestationSecurityLevel() {
if (fakeReport != null) return KM_SECURITY_LEVEL_TRUSTED_ENVIRONMENT;
return realAttestationSecurityLevel();
}

protected abstract int realAttestationSecurityLevel();


public RootOfTrust getRootOfTrust() {
if (fakeReport != null) return fakeReport.locked() == null ? null : new RootOfTrust(fakeReport.locked(), fakeReport.bootState());
return realRootOfTrust();
}

protected abstract RootOfTrust realRootOfTrust();

public abstract RootOfTrust getRootOfTrust();

// Returns one of the KM_VERSION_* values define above.
public int getKeymasterVersion() {
return keymasterVersion;
}

public int getKeymasterSecurityLevel() {
if (fakeReport != null) return KM_SECURITY_LEVEL_TRUSTED_ENVIRONMENT;
return keymasterSecurityLevel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,63 @@ public AttestationApplicationId getAttestationApplicationId() {
return attestationApplicationId;
}

/**
* Display-only copy for the simulated report: same tags, but no root of trust tag,
* so the bootloader section can be presented as the user chose. The parsed object
* itself is never modified.
*/
public AuthorizationList(AuthorizationList base) {
purposes = base.purposes;
algorithm = base.algorithm;
keySize = base.keySize;
digests = base.digests;
paddingModes = base.paddingModes;
ecCurve = base.ecCurve;
mlDsaVariant = base.mlDsaVariant;
rsaPublicExponent = base.rsaPublicExponent;
mgfDigests = base.mgfDigests;
rollbackResistance = base.rollbackResistance;
earlyBootOnly = base.earlyBootOnly;
activeDateTime = base.activeDateTime;
originationExpireDateTime = base.originationExpireDateTime;
usageExpireDateTime = base.usageExpireDateTime;
usageCountLimit = base.usageCountLimit;
noAuthRequired = base.noAuthRequired;
userAuthType = base.userAuthType;
authTimeout = base.authTimeout;
allowWhileOnBody = base.allowWhileOnBody;
trustedUserPresenceReq = base.trustedUserPresenceReq;
trustedConfirmationReq = base.trustedConfirmationReq;
unlockedDeviceReq = base.unlockedDeviceReq;
allApplications = base.allApplications;
applicationId = base.applicationId;
creationDateTime = base.creationDateTime;
origin = base.origin;
rollbackResistant = base.rollbackResistant;
osVersion = base.osVersion;
osPatchLevel = base.osPatchLevel;
attestationApplicationId = base.attestationApplicationId;
brand = base.brand;
device = base.device;
product = base.product;
serialNumber = base.serialNumber;
imei = base.imei;
secondImei = base.secondImei;
meid = base.meid;
manufacturer = base.manufacturer;
model = base.model;
vendorPatchLevel = base.vendorPatchLevel;
bootPatchLevel = base.bootPatchLevel;
deviceUniqueAttestation = base.deviceUniqueAttestation;
identityCredentialKey = base.identityCredentialKey;
moduleHash = base.moduleHash;
}

/**
* Display-only copy for the simulated report: same tags, but no root of trust tag,
* so the bootloader section can be presented as the user chose. The parsed object
* itself is never modified.
*/
public String getBrand() {
return brand;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,30 @@ public X509Certificate getCert() {
return cert;
}

/**
* Display-only copy used by the simulated report: same certificate object, a different
* reported status. Only the rendering layer ever receives it.
*/
public CertificateInfo(CertificateInfo base, int fakeStatus) {
this.cert = base.cert;
this.issuer = base.issuer;
this.status = fakeStatus;
this.securityException = fakeStatus == base.status ? base.securityException : null;
this.attestation = base.attestation;
this.certException = base.certException;
this.provisioningInfo = base.provisioningInfo;
}

public CertificateInfo(CertificateInfo base, X509Certificate fakeCert) {
this.cert = fakeCert;
this.issuer = base.issuer;
this.status = base.status;
this.securityException = base.securityException;
this.attestation = base.attestation;
this.certException = base.certException;
this.provisioningInfo = base.provisioningInfo;
}

public RootPublicKey.Status getIssuer() {
return issuer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public EatAttestation(X509Certificate x509Cert)
}

/** Find the submod containing the key information, and return its security level. */
public int getAttestationSecurityLevel() {
@Override
protected int realAttestationSecurityLevel() {
if (teeEnforced != null && teeEnforced.getAlgorithm() != null) {
return teeEnforced.getSecurityLevel();
} else if (softwareEnforced != null && softwareEnforced.getAlgorithm() != null) {
Expand All @@ -122,7 +123,8 @@ public int getAttestationSecurityLevel() {
}
}

public RootOfTrust getRootOfTrust() {
@Override
protected RootOfTrust realRootOfTrust() {
return rootOfTrust;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,32 @@ public static String verifiedBootStateToString(int verifiedBootState) {
}
}

/**
* Display-only copy for the simulated report. Nothing outside the rendering layer
* receives it, and the parsed root of trust stays untouched.
*/
public RootOfTrust(Boolean locked, int state) {
this(spoofBootKey(locked, state), locked != null && locked, state, new byte[32]);
}

/**
* A zero boot key is flagged as suspicious, so only present that for the states where
* the app itself legitimately shows it (unlocked or unverified boot).
*/
private static byte[] spoofBootKey(Boolean locked, int state) {
if (locked == null) {
return new byte[32];
}
if (locked && state == KM_VERIFIED_BOOT_VERIFIED) {
var key = new byte[32];
for (int i = 0; i < key.length; i++) {
key[i] = (byte) (0x5a + i * 7);
}
return key;
}
return new byte[32];
}

public byte[] getVerifiedBootKey() {
return verifiedBootKey;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.content.res.ColorStateList
import android.view.View
import androidx.core.view.isVisible
import io.github.vvb2060.keyattestation.R
import io.github.vvb2060.keyattestation.attestation.RootOfTrust
import io.github.vvb2060.keyattestation.databinding.HomeHeaderBinding
import io.github.vvb2060.keyattestation.repository.AttestationData
import rikka.core.res.resolveColor
Expand All @@ -22,39 +21,76 @@ class BootStateViewHolder(itemView: View, binding: HomeHeaderBinding) :

override fun onBind() {
val context = itemView.context

val rootOfTrust = data.rootOfTrust
val locked = rootOfTrust?.isDeviceLocked
val bootUnverified = rootOfTrust?.verifiedBootState != RootOfTrust.KM_VERIFIED_BOOT_VERIFIED
val fake = FakeReport.enabled

val titleRes: Int
val summaryRes: Int
val iconRes: Int
val colorAttrRes: Int

if (locked == null) {
titleRes = R.string.bootloader_unknown
iconRes = R.drawable.ic_boot_unknown_24
colorAttrRes = rikka.material.R.attr.colorInactive
} else if (!locked) {
titleRes = R.string.bootloader_unlocked
iconRes = R.drawable.ic_boot_unlocked_24
colorAttrRes = rikka.material.R.attr.colorWarning
} else if (bootUnverified) {
titleRes = R.string.bootloader_user
iconRes = R.drawable.ic_boot_locked_24
colorAttrRes = rikka.material.R.attr.colorInactive
if (fake && FakeReport.hasBootOverride) {
// 纯显示层:完全按用户选的那一项渲染,不读 parsed root of trust
when (FakeReport.bootState) {
FakeReport.BOOT_VERIFIED -> {
titleRes = R.string.bootloader_locked
iconRes = R.drawable.ic_boot_locked_24
colorAttrRes = rikka.material.R.attr.colorSafe
}
FakeReport.BOOT_UNLOCKED -> {
titleRes = R.string.bootloader_unlocked
iconRes = R.drawable.ic_boot_unlocked_24
colorAttrRes = rikka.material.R.attr.colorWarning
}
FakeReport.BOOT_SELF_SIGNED -> {
titleRes = R.string.bootloader_user
iconRes = R.drawable.ic_boot_locked_24
colorAttrRes = rikka.material.R.attr.colorInactive
}
FakeReport.BOOT_UNVERIFIED -> {
titleRes = R.string.bootloader_unverified
iconRes = R.drawable.ic_boot_unlocked_24
colorAttrRes = rikka.material.R.attr.colorWarning
}
FakeReport.BOOT_FAILED -> {
titleRes = R.string.bootloader_failed
iconRes = R.drawable.ic_boot_locked_24
colorAttrRes = rikka.material.R.attr.colorAlert
}
else -> {
titleRes = R.string.bootloader_unknown
iconRes = R.drawable.ic_boot_unknown_24
colorAttrRes = rikka.material.R.attr.colorInactive
}
}
} else {
titleRes = R.string.bootloader_locked
iconRes = R.drawable.ic_boot_locked_24
colorAttrRes = rikka.material.R.attr.colorSafe
val rootOfTrust = data.rootOfTrust
val locked = rootOfTrust?.isDeviceLocked
val bootUnverified =
rootOfTrust?.verifiedBootState != io.github.vvb2060.keyattestation.attestation.RootOfTrust.KM_VERIFIED_BOOT_VERIFIED
if (locked == null) {
titleRes = R.string.bootloader_unknown
iconRes = R.drawable.ic_boot_unknown_24
colorAttrRes = rikka.material.R.attr.colorInactive
} else if (!locked) {
titleRes = R.string.bootloader_unlocked
iconRes = R.drawable.ic_boot_unlocked_24
colorAttrRes = rikka.material.R.attr.colorWarning
} else if (bootUnverified) {
titleRes = R.string.bootloader_user
iconRes = R.drawable.ic_boot_locked_24
colorAttrRes = rikka.material.R.attr.colorInactive
} else {
titleRes = R.string.bootloader_locked
iconRes = R.drawable.ic_boot_locked_24
colorAttrRes = rikka.material.R.attr.colorSafe
}
}

if (data.isSoftwareLevel) {
summaryRes = R.string.bootloader_summary_sw_level
} else {
summaryRes = 0
}
val summaryRes =
if (data.isSoftwareLevel && !(fake && FakeReport.hideTampering)) {
R.string.bootloader_summary_sw_level
} else {
0
}

val color = context.theme.resolveColor(colorAttrRes)

Expand Down
Loading