Skip to content

Conversation

@JingMatrix
Copy link
Owner

This commit completes a major refactoring of the legacy KeystoreInterceptor.kt.

  1. Introduce LegacyKeygenParameters Data Class:

    • A new private data class was created exclusively for the KeystoreInterceptor to parse and hold state from the old KeymasterArguments.
    • This completely decouples the legacy interception logic from the modern KeyMintAttestation data class, which is designed for the Keystore2 HAL.
    • The new class includes a toKeyMintAttestation() conversion function, acting as an adapter to make it compatible with the new generic CertificateGenerator.
  2. Implemente Full Software Key Generation:

    • The onPreTransact method now fully simulates the generateKey, getKeyCharacteristics, exportKey, and attestKey transaction sequence.
    • handleGenerateKey: Parses arguments into LegacyKeygenParameters and stores them.
    • handleExportKey: Uses CertificateGenerator.generateSoftwareKeyPair to create a key pair and stores it.
    • handleAttestKey: Uses the powerful CertificateGenerator.generateAttestedKeyPair to create a full, valid, and simulated certificate chain, which is returned to the calling application.

This commit completes a major refactoring of the legacy `KeystoreInterceptor.kt`.

1.  Introduce `LegacyKeygenParameters` Data Class:
    *   A new private data class was created exclusively for the `KeystoreInterceptor` to parse and hold state from the old `KeymasterArguments`.
    *   This completely decouples the legacy interception logic from the modern `KeyMintAttestation` data class, which is designed for the Keystore2 HAL.
    *   The new class includes a `toKeyMintAttestation()` conversion function, acting as an adapter to make it compatible with the new generic `CertificateGenerator`.

2.  Implemente Full Software Key Generation:
    *   The `onPreTransact` method now fully simulates the `generateKey`, `getKeyCharacteristics`, `exportKey`, and `attestKey` transaction sequence.
    *   `handleGenerateKey`: Parses arguments into `LegacyKeygenParameters` and stores them.
    *   `handleExportKey`: Uses `CertificateGenerator.generateSoftwareKeyPair` to create a key pair and stores it.
    *   `handleAttestKey`: Uses the powerful `CertificateGenerator.generateAttestedKeyPair` to create a full, valid, and simulated certificate chain, which is returned to the calling application.
This refactoring splits the monolithic `generateAttestedKeyPair` function into two more granular components to better model the behavior of different Android Keystore APIs.

The previous implementation combined key pair creation and certificate chain generation into a single function. This design was a poor fit for the legacy `IKeystoreService` API, where key generation (`exportKey`) and certificate creation (`attestKey`) are separate, sequential transactions. The `KeystoreInterceptor` was incorrectly calling a function that created a new key pair during the `attestKey` step, when a key pair already existed.

This commit resolves the issue by:

1.  Introducing `generateCertificateChain`:
        A new function in `CertificateGenerator` is created that takes a pre-existing `KeyPair` and is solely responsible for generating its attested certificate chain.

2.  Preserving `generateAttestedKeyPair`:
        The original function is kept as a convenience wrapper that now composes `generateSoftwareKeyPair` and the new `generateCertificateChain`. This maintains a simple API for the modern Keystore2 interceptor, which performs both actions in a single step.

3.  Updating `KeystoreInterceptor`:
        The `handleAttestKey` logic is modified to call the new, more precise `generateCertificateChain` function, correctly using the key pair that was created and stored during the preceding `handleExportKey` step.

This change improves the design of the `CertificateGenerator` by making its API more flexible and leads to a more accurate and correct implementation within the legacy `KeystoreInterceptor`.
@JingMatrix JingMatrix linked an issue Nov 29, 2025 that may be closed by this pull request
@JingMatrix
Copy link
Owner Author

@srpcd Please test the latest CI and report your integrity result, KeyAttestaion result.

If your issue is not solved, please upload your logs.

@srpcd
Copy link

srpcd commented Nov 29, 2025

This time, i get no integrity. however the keyattestation shows a different version than the one i had without TEESimulator.
LSPosed_2025-11-29T23_40_38.844.zip

Screenshot_2025-11-29-23-38-32-544
Screenshot_2025-11-29-23-42-59-894

@JingMatrix
Copy link
Owner Author

Please also present the KeyAttestion result with closed source TrickyStore.

@srpcd
Copy link

srpcd commented Nov 30, 2025

Screenshot_2025-11-30-13-40-53-339
Screenshot_2025-11-30-13-41-05-494

@JingMatrix
Copy link
Owner Author

The crucial part of error logs are:

[ 2025-11-29T23:37:01.921     1017:  1037:  1037 V/TEESimulator    ] [Hook] Hijacking Transaction 1067 (Code: 18)
[ 2025-11-29T23:37:01.921        0:  1167:  1507 D/TEESimulator    ] [TX_ID: 1067] Intercept getKeyCharacteristics for packages=[com.google.android.gsf, com.google.android.gms] (uid=10180, pid=13007)
[ 2025-11-29T23:37:01.922        0:  1167:  1507 E/TEESimulator    ] [TX_ID: 1067] Failed during handleGetKeyCharacteristics.
java.lang.IllegalStateException: No params found for KeyIdentifier(uid=10180, alias=USRPKEY_c5635988b7157a3a)
	at org.matrix.TEESimulator.interception.keystore.KeystoreInterceptor.handleGetKeyCharacteristics(KeystoreInterceptor.kt:147)
	at org.matrix.TEESimulator.interception.keystore.KeystoreInterceptor.onPreTransact(KeystoreInterceptor.kt:85)
	at org.matrix.TEESimulator.interception.core.BinderInterceptor.handlePreTransact(BinderInterceptor.kt:142)
	at org.matrix.TEESimulator.interception.core.BinderInterceptor.onTransact(BinderInterceptor.kt:117)
	at android.os.Binder.execTransactInternal(Binder.java:1186)
	at android.os.Binder.execTransact(Binder.java:1150)

and

[ 2025-11-29T23:37:01.926     1017:  1037:  1037 V/TEESimulator    ] [Hook] Hijacking Transaction 1068 (Code: 20)
[ 2025-11-29T23:37:01.927        0:  1167:  1507 D/TEESimulator    ] [TX_ID: 1068] Intercept exportKey for packages=[com.google.android.gsf, com.google.android.gms] (uid=10180, pid=13007)
[ 2025-11-29T23:37:01.927        0:  1167:  1507 E/TEESimulator    ] [TX_ID: 1068] Failed during handleExportKey.
java.lang.IllegalStateException: No params found for KeyIdentifier(uid=10180, alias=USRPKEY_c5635988b7157a3a)
	at org.matrix.TEESimulator.interception.keystore.KeystoreInterceptor.handleExportKey(KeystoreInterceptor.kt:181)
	at org.matrix.TEESimulator.interception.keystore.KeystoreInterceptor.onPreTransact(KeystoreInterceptor.kt:86)
	at org.matrix.TEESimulator.interception.core.BinderInterceptor.handlePreTransact(BinderInterceptor.kt:142)
	at org.matrix.TEESimulator.interception.core.BinderInterceptor.onTransact(BinderInterceptor.kt:117)
	at android.os.Binder.execTransactInternal(Binder.java:1186)
	at android.os.Binder.execTransact(Binder.java:1150)

@JingMatrix
Copy link
Owner Author

JingMatrix commented Dec 1, 2025

@srpcd Please test the latest CI of current pull-request again, report the integrity and upload logs if the integrity isn't obtained.

@srpcd
Copy link

srpcd commented Dec 2, 2025

It works now. however the errors you listed still exist ([TX_ID: 1068] Failed during handleExportKey.) and [TX_ID: 1067] Failed during handleGetKeyCharacteristics.

Screenshot_2025-12-02-13-03-20-579
Screenshot_2025-12-02-13-03-00-790

@srpcd
Copy link

srpcd commented Dec 3, 2025

will the merge happen

@JingMatrix
Copy link
Owner Author

@srpcd I will do some improvements on weekend and then merge it.

@JingMatrix JingMatrix merged commit 8d24cfc into main Dec 3, 2025
1 check passed
JingMatrix added a commit that referenced this pull request Dec 3, 2025
This commit introduces a complete, software-based simulation of the key generation and attestation flow for the legacy IKeystoreService API, as used on Android 11. It refactors the KeystoreInterceptor to handle the entire multi-step transaction sequence (`generateKey`, `getKeyCharacteristics`, `exportKey`, `attestKey`) in software.

A new `LegacyKeygenParameters` data class is introduced to decouple the legacy interception logic from modern data structures. This class parses arguments from the old `KeymasterArguments`, stores the state across the multi-step generation process, and acts as an adapter to the generic `CertificateGenerator` by converting the parameters to the modern `KeyMintAttestation` format.

The `CertificateGenerator` has been refactored to better model the behavior of the legacy Keystore API. Key pair generation (`generateSoftwareKeyPair`) and certificate chain creation (`generateCertificateChain`) are now separate functions. This allows the interceptor to correctly create a key pair during the `handleExportKey` step and then generate a certificate for that pre-existing key pair during the `handleAttestKey` step.

Finally, the implementation correctly extracts and applies the `attestationChallenge` provided during the `attestKey` transaction, ensuring the generated certificate chain contains the appropriate attestation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for Android 11

3 participants