Commit graph

1069 commits

Author SHA1 Message Date
Shaquille Johnson
ac3c2cdea4 Add new error for system errors that are retryable
Some issues require a system error to be raised that
indicates we should retry the process. This adds a new
error and bumps the version of the api for future use.

Test: atest keystore2_test
Bug: 238619180
Change-Id: Iff8fa83f7b223e08de9fa31434e16aa3aa2153f6
2024-01-12 15:50:54 +00:00
Rajesh Nyamagoud
4347357814 Created libkeystore-engine as cc-library instead of cc-test-library to
avoid issues while linking shared libraries with Rust test binaries.

This change is made to avoid vts-tradefed failure to link the shared
library while running the Rust VTS `keystore2_client_tests` test
suite. As suggested in b/314110490#24 using the libkeystore-engine
static-library to run keystore2_client_tests.

Bug: 314110490, 298668920
Test: atest keystore2_client_tests; run vts -m keystore2_client_tests
Change-Id: If956865eeb4af908f33b1ad81a2b2e26300aae0e
2024-01-03 01:44:24 +00:00
Shaquille Johnson
52b8c9321b When wal flag not enabled set db back to default
When a database is set once it will still maintain that
setting even if on the next connection it is not specified.
Any databases that set the wal flag will need to turn the
database back to its default when the flag is disabled or
there will be an error in the access of the database.

Bug: 314419678
Test: atest keystore2_test && atest legacykeystore_test
Change-Id: I008f2d2f6ac055704b721cdd451fc8bdfe448832
2023-12-21 18:30:50 +00:00
Rajesh Nyamagoud
b061f9cf67 Changes made to compile keystore2_client_tests module with
compile_multilib set to first.

To avoid missing dependent library (libkeymaster_portable.so) error,
enforcing to compile for 64-bit on a 64-bit platform, and 32-bit on
a 32-bit platform.

Bug: 314110490
Test: run vts -m keystore2_client_tests
Change-Id: I5e8bf94ed37209f69ace2d7dd2c0ca1b680fc86d
2023-12-15 02:48:01 +00:00
Eric Biggers
93a028a391 Merge "Increase RLIMIT_MEMLOCK for keystore2" into main 2023-12-12 17:43:43 +00:00
Eric Biggers
2202fcc19f Increase RLIMIT_MEMLOCK for keystore2
By default Android only allows processes to lock up to 65536 bytes of
memory, resulting from the command 'setrlimit memlock 65536 65536' in
system/core/rootdir/init.rc.  The recent Keystore changes to create each
user's super keys at user creation time cause Keystore to sometimes lock
more memory and sometimes exceed this limit.  To reproduce this issue
myself, I had to create almost 100 users.  However, it apparently can
happen with fewer users too, based on CTS test failure report.

Fix this issue by setting the memlock limit for keystore2 to unlimited.
Note that the amount actually used remains fairly small, but I don't
think there's a reason to set an arbitrary limit here.  A memlock limit
makes sense for unprivileged apps but not for system processes.

Bug: 296464083
Bug: 314474709
Bug: 314561033
Test: adb shell setprop debug.user.creation_override 1
      for i in `seq 1 100`; do adb shell pm create-user --profileOf 0 --managed profile; done
      adb logcat | grep -i keystore
      # Saw ENOMEM error near the end without this CL, but not with it.
Flag: Not feasible to flag this CL, and it's a pretty safe change.
Change-Id: I3ef062d737ffb1431dca78c0d568ad6c2d713de6
2023-12-04 19:43:16 +00:00
Eric Biggers
10afa966fa Split Keystore's onLockScreenEvent into onDevice{Unlocked,Locked}
Currently Keystore is notified of the device being unlocked and locked
for each user via onLockScreenEvent(lockScreenEvent, userId, password,
unlockingSids), where lockScreenEvent is UNLOCK or LOCK.  This is a bit
confusing because the password parameter is only meaningful for UNLOCK,
and the unlockingSids parameter is only meaningful for LOCK.  This
problem will get worse when we add a parameter that tells Keystore
whether unlocking via a weak biometric or trust agent is possible, as
that will be another parameter that is only meaningful for LOCK.

Therefore, this CL splits onLockScreenEvent into two methods
onDeviceUnlocked and onDeviceLocked, each with the appropriate
parameters.  No change in behavior intended.

Bug: 296464083
Test: atest -p --include-subdirs system/security/keystore2 \
      && atest CtsKeystoreTestCases \
      && atest TrustTests \
      && atest com.android.server.locksettings
Flag: N/A, straightforward refactoring
Change-Id: Ie2afd118bddca6112a5469558569c63b68ee10fb
2023-12-02 03:05:43 +00:00
Shaquille Johnson
6f80e95312 Merge "Remove duplicate code and add fn for flag check" into main 2023-12-01 15:22:59 +00:00
Shaquille Johnson
f015af1d56 Remove duplicate code and add fn for flag check
Test: atest CtsKeystoreTestCases
Change-Id: I47975e028896ebe5777bae8efe8b17507bb36500
2023-11-30 15:23:21 +00:00
Eric Biggers
4835baecd7 Merge "Rename the fix_unlocked_device_required_keys flag" into main 2023-11-29 23:28:13 +00:00
Shaquille Johnson
2dc300aee0 Merge "Deprecate put and return error in ILegacyKeystore" into main 2023-11-28 16:13:22 +00:00
Shaquille Johnson
be6e91d15f Deprecate put and return error in ILegacyKeystore
Legacy keystore is a old relic that was suppoed to be
disabled a while ago. It has enabled functionality that was
supposed to be removed but wasn't because it would break
changes in the VPN and WIFI code. This would begin the
process of permanently removing it.

Test: atest CtsKeystoreTestCases
Change-Id: Iedc1dca24a40eb0cf30c5280fc2842ff79cf7f17
2023-11-28 15:12:04 +00:00
Shaquille Johnson
9c7503368f Merge "Rename flag wal_db_journalmode" into main 2023-11-27 16:07:15 +00:00
Shaquille Johnson
d28f5cbe9b Rename flag wal_db_journalmode
This flag was defined as a regular flag and then was later changed to a
fixed_read_only flag.  This scenario is currently "unsupported" by the
flags infrastructure; an error occurs when trying to advance the flag to
staging. Work around this by renaming the flag so that the flags
infrastructure sees it as an entirely new flag. This cl adds this flag
to the legacykeystore code as well.

Bug: 296464083
Bug: 311648623
Test: m keystore2
Change-Id: If62a5fac2404113ca0bbc0807f154401c4241bf1
2023-11-27 12:17:13 +00:00
Alice Wang
f7148408b5 [keystore2] Update comment when fetching rkpd attestation key
No code change.

Test: atest keystore2_test
Bug: 310047761
Change-Id: I4269bd4bc146ac0d0aa7b5ca9af93957399aa7b6
2023-11-24 08:36:51 +00:00
Alice Wang
b03ed83251 Fix android.keystore.cts.KeyAttestationTest
This cl moves the RPC name searching logic inside the attestation
key fetch function to fix the failing tests.

Test: atest keystore2_test
Bug: 310047761
Change-Id: Ied5fbd3248cae6aec230cacfa6807b3cb2b7cf4b
2023-11-23 11:59:45 +00:00
Eric Biggers
6ea1c60e67 Rename the fix_unlocked_device_required_keys flag
This flag was defined as a regular flag and then was later changed to a
fixed_read_only flag.  This scenario is currently "unsupported" by the
flags infrastructure; an error occurs when trying to advance the flag to
staging.  Work around this by renaming the flag so that the flags
infrastructure sees it as an entirely new flag.

Bug: 296464083
Bug: 311648623
Test: build
Change-Id: Iafde2d63578bf65b3f5a08ab57561eadbe8f6b7a
2023-11-22 16:21:59 +00:00
David Drysdale
39b7af2fcd Merge changes I28f673b6,I146f7cfd into main
* changes:
  Flag for import of previously-emulated keys
  Cope with previously-emulated keys
2023-11-20 10:43:44 +00:00
Alice Wang
416683bc23 Merge "Make librkpd_client available to com.android.virt" into main 2023-11-17 09:05:45 +00:00
Eric Biggers
dfbab7e6bc Merge "keystore2: fix UnlockedDeviceRequired to work without LSKF" into main 2023-11-15 17:33:33 +00:00
Shaquille Johnson
f1a07b2fe1 Merge "Change name of flag to specify put" into main 2023-11-15 12:14:42 +00:00
Eran Messeri
a68eb23f51 Merge "Test added to generate a key with specifying cerificate subject and certificate serial number. Test generates a key and verifies the specified key characteristics." into main 2023-11-15 12:11:14 +00:00
Shaquille Johnson
6084409db8 Change name of flag to specify put
Test: unapplicable
Change-Id: If1bba7c7cc486ed443a75e024b04cd017424126b
2023-11-15 09:38:09 +00:00
Rajesh Nyamagoud
e5557ffdce Test added to generate a key with specifying cerificate subject and
certificate serial number. Test generates a key and verifies the
specified key characteristics.

Bug: 279721870
Test: atest keystore2_client_tests
Change-Id: I3ea356da8ca3404a94081a680210a9f426a2b908
2023-11-14 23:25:30 +00:00
Treehugger Robot
0e0b09a901 Merge "Adding tests to verify Device-Unique-Attestation." into main 2023-11-14 23:04:35 +00:00
Rajesh Nyamagoud
b1c8e83392 Adding tests to verify Device-Unique-Attestation.
1. Test to verify Device-Unique-Attestation is not supported on
   `TRUSTED_ENVIRONMENT` security level. Test shoould fail to generate a
   key with device-unique-attestation with `INVALID_ARGUMENT` error code.

2. Generate EC/RSA keys with `DEVICE_UNIQUE_ATTESTATION` using `STRONGBOX`
   security level. Test should generate akey and verify key
   characteristics and cert-chain signatures. Test should be able to
   perform an operation using the generated key successfully.

3. Try to generate a device unique attested key with attestation of
   invalid device's identifiers. Test should fail to generate a key with
   error code `CANNOT_ATTEST_IDS`.

4. Generate a device unique attested key with attestation of the
   device's identifiers. Test should succeed in generating a attested
   key with attestation of device identifiers. Test might fail on
   devices which don't support device id attestation with error response
   code `CANNOT_ATTEST_IDS`. Separate test is added for each attestation
   id with RSA and EC keys.

Bug: 279721870
Test: atest keystore2_client_tests
Change-Id: I627a01dc44558a4393d14f9931b1708196ee6ff9
2023-11-14 21:33:19 +00:00
Eric Biggers
b0478cfa34 keystore2: fix UnlockedDeviceRequired to work without LSKF
The security improvements to UnlockedDeviceRequired in Android 12
regressed its behavior by making it no longer work for unsecured users,
e.g. users with a Swipe lock screen.  Two different things broke it:

1. Keystore started enforcing that a HardwareAuthToken be present for
   all keys that use UnlockedDeviceRequired.

2. Keystore started superencrypting all keys that use
   UnlockedDeviceRequired.  Previously, only keys that used
   UserAuthenticationRequired were superencrypted.

The above changes apparently resulted from a misconception that for the
device to be unlocked, the user must have authenticated.  However,
unsecured users cannot authenticate and cannot have HardwareAuthTokens,
yet the device is always considered unlocked for them.

This change first fixes cause (1) by making Keystore allow keys that use
UnlockedDeviceRequired to be used without a HardwareAuthToken, provided
that they don't also use UserAuthenticationRequired (which is the
protection that actually requires a HardwareAuthToken).

Regarding cause (2), superencryption is an important security
enhancement for UnlockedDeviceRequired, so it's not being removed.
Instead, the real problem is in the way that Keystore unnecessarily ties
superencryption to the existence of the LSKF.  That is, Keystore creates
a user's super keys only when an LSKF is set, and Keystore deletes all
the user's super keys and superencrypted keys when the LSKF is removed.

Therefore, this change, in coordination with the corresponding
LockSettingsService change, makes each user's Keystore super keys have
the same lifetime as the user's synthetic password.  That basically
means they are created when the user is created and are deleted only
when the user is deleted.  In addition, when a user's LSKF is removed,
Keystore now deletes *only* the user's auth-bound keys.

The fix for cause (1) is entirely in Keystore and is guarded by the
fix_unlocked_device_required_keys flag.  The fix for cause (2) consists
of two new IKeystoreMaintenance methods, initUserSuperKeys() and
onUserLskfRemoved(), that are called by LockSettingsService and are
flagged at the LockSettingsService level.  Note that once the flag is
removed, it will be possible to remove superseded code, including the
onUserPasswordChanged() method of IKeystoreMaintenance and the
init_user() and reset_user() functions that it calls.

Bug: 296464083
Test: # Did the following with and without the flag enabled:
      atest com.android.server.locksettings \
      && atest -p --include-subdirs system/security/keystore2 \
      && atest CtsKeystoreTestCases
Change-Id: If12824369fbad4a90e5cd0427e792655fd233b96
2023-11-13 18:39:29 +00:00
Alice Wang
307ff9e008 Make librkpd_client available to com.android.virt
Test: atest keystore2_test
Bug: 241428146
Change-Id: Ife0d4e9b55622089f262685d61bdefc2a52cbc47
2023-11-13 10:31:19 +00:00
Alice Wang
01c16b6142 [rkpd] Refactor rkpd_client into an independent lib for reuse
This allows rkpd_client to be reused by both keystore2 and
AVF pVM remote attestation.

Test: atest keystore2_test librkpd_client.test
Bug: 241428146
Change-Id: Ibdf95c4deb2ba499daaecd170c2971cda4e80bba
2023-11-13 10:31:08 +00:00
Alice Wang
849cfe4347 Revert^2 "[rkpd_client] Add Error type to rkpd_client"
This reverts commit f84c46c3b3.

Reason for revert: Reland the original cl aosp/2821995
with an adjustment about the Timeout error type in order
to maintain the original ResponseCode.

Test: atest RkpdAppIntegrationTests
Bug: 310139666
Change-Id: Id4ee05eb616c125f9d28b25f4668ca3071ccb26c
2023-11-13 10:28:14 +00:00
David Drysdale
093811ef22 Flag for import of previously-emulated keys
Test: build
Bug: 283077822
Change-Id: I28f673b6eb905c2953fbb91f2658ff224ca0e21c
2023-11-10 13:21:41 +00:00
Alice Wang
0b140f418a Merge "Revert "[rkpd_client] Add Error type to rkpd_client"" into main 2023-11-10 12:40:10 +00:00
Aashna Jena
f84c46c3b3 Revert "[rkpd_client] Add Error type to rkpd_client"
This reverts commit 2dbabf3b72.

Reason for revert: DroidMonitor revert for b/310139666

Bug: 310139666
Change-Id: I1213940cc4e3112038c1cc66f5a218a9378d6b0f
2023-11-10 12:13:22 +00:00
Alice Wang
6bd83e633f Merge "[rkpd_client] Add Error type to rkpd_client" into main 2023-11-10 08:18:07 +00:00
Alice Wang
2dbabf3b72 [rkpd_client] Add Error type to rkpd_client
This makes rkpd_client independent of keystore2 and facilitates
the extraction of rkpd_client as a standalone library later.

Test: atest keystore2_test
Bug: 241428146
Change-Id: I3bcf0afdb587b2e95bd9a970631c29696f57ed4f
2023-11-09 09:28:36 +00:00
David Drysdale
746e1be8ef Cope with previously-emulated keys
If a device has upgraded Android versions then the KeyMint device
may also have been upgraded.  If that's the case, then there may
be keyblobs that were created in software on the old device, because it
didn't support some feature.

Watch out for these keys, and if encountered, try to import them into
the current KeyMint device:

- extract the key material from the key blob
- add PKCS#8 wrapping for import

Bug: 283077822
Bug: 296403357
Test: tested with ARC upgrade, see b/296403357
Change-Id: I146f7cfdaac9fe22b7bb6850b7e48ea113945902
2023-11-09 07:20:23 +00:00
Alice Wang
779c94e7f2 Merge "[rkpd] Move watchdog calls from rkpd_client to keystore2" into main 2023-11-08 21:41:34 +00:00
Eric Biggers
17e96a8797 Merge changes I26394509,I9d76a0ec,I88779273 into main
* changes:
  keystore2: link to android.security flags library
  keystore2: log super key creation
  keystore2: factor out create_super_key()
2023-11-08 18:42:45 +00:00
James Willcox
464c7d3d2b Merge "Use CLOCK_BOOTTIME for keystore2 auth token received time" into main 2023-11-08 18:12:42 +00:00
James Willcox
80f7be1516 Use CLOCK_BOOTTIME for keystore2 auth token received time
CLOCK_BOOTTIME is more correct because it includes time spent
while the device is suspended.

This also fixes an issue when comparing the times resulting from the
get_last_auth_time() API in the Java world, because we want to use
SystemClock.elapsedRealtime(), which uses CLOCK_BOOTTIME.

Bug: 309686873
Test: atest keystore2_client_tests
Change-Id: I89d71ccfcfe4f8b3495fede40ae26ad6fa2b0118
2023-11-08 17:13:49 +00:00
Alice Wang
4277d2e1ca [rkpd] Move watchdog calls from rkpd_client to keystore2
This cl moves watchdog calls to keystore2 to make rkpd_client
less dependent on keystore2, this allows us to make rkpd_client
an independent library more easily later.

Test: atest keystore2_test
Bug: 241428146
Change-Id: Ic3040ad65356aa7e25d38f36d453a258caf28403
2023-11-08 09:15:54 +00:00
Alice Wang
e66c3310cd [refactor] Split the message macro in a standalone library for reuse
This simplifies the task of creating an independent library of
rkpd_client later.

Test: atest keystore2_test
Bug: 241428146
Change-Id: Idddf37d14580e691fde5a494e54297465cb693b6
2023-11-07 13:47:54 +00:00
Alice Wang
bf6a693217 [rkpd] Move security level to RPC name conversion out of rkpd_client
This will facilitate the extraction of rkpd_client as a standalone
library later.

Test: atest keystore2_test
Bug: 241428146
Change-Id: Icff6f88f2c3cc3dc50dd126067ed5f10c8aa7b29
2023-11-07 13:27:54 +00:00
Alice Wang
83c6aefb5d [refactor] Split watchdog in a standalone library for reuse
This simplifies the task of creating an independent library of
rkpd_client later.

Test: atest keystore2_test
Bug: 241428146
Change-Id: I2834c9be9f5100d52829e6392f0dd48e7c76beb1
2023-11-07 10:30:26 +00:00
Charisee
78e844b86a Update needed for Rust v1.73.0
Remove redundancy
Bug: 303252546
Test: ./test_compiler.py --prebuilt-path dist/rust-dev.tar.xz  --target aosp_cf_x86_64_phone --image

Change-Id: Ia4366c2694c235f8931eea56d13548f08805952f
2023-11-06 20:41:19 +00:00
Orlando Arbildo
c0e1f3589b Merge "Adding 'static bound on binder Interface implementation" into main 2023-11-06 18:44:01 +00:00
Eric Biggers
698922b0a6 keystore2: link to android.security flags library
Make it possible for keystore2 to check android.security flags.

Bug: 296464083
Test: Build
Change-Id: I263945093ed9c76d914018b7ae24bf6151157c0c
2023-11-02 21:19:42 +00:00
Eric Biggers
6745f53b46 keystore2: log super key creation
Log an informational message when creating each of a user's super keys,
as these are significant events.

Bug: 296464083
Test: atest -p --include-subdirs system/security/keystore2
Flag: exempt, just adds a log message
Change-Id: I9d76a0ec06fae208412f4c6cf1b7dd739b023a61
2023-11-02 21:19:31 +00:00
Eric Biggers
456a3a6974 keystore2: factor out create_super_key()
Currently the UnlockedDeviceRequired super keys are created by
get_or_create_super_key(), while the AfterFirstUnlock super key is
created by separate code in init_user().  The super key creation code in
get_or_create_super_key() is generic enough to work for all super keys,
however.  This CL factors this code out into a new function
create_super_key(), which a later CL will use for the AfterFirstUnlock
super key.  No change in behavior.

Bug: 296464083
Test: atest -p --include-subdirs system/security/keystore2
Flag: exempt, mechanical refactoring
Change-Id: I88779273efef6cb925152381c07549e1f49daecf
2023-11-02 21:19:31 +00:00
James Willcox
d215da817a Add getLastAuthTime() to IKeystoreAuthorization
This returns the time (from CLOCK_MONOTONIC_RAW) that the specified user
last authenticated using the given authenticator.

Bug: 303839446
Test: atest keystore2_client_tests
Change-Id: Idd4c477365ffa556b7985d1d926dfa554680ff28
2023-10-31 20:30:50 +00:00