450 Commits

Author SHA1 Message Date
Robin Lee
21bcbc54dd Guard against null component when binding KeyChain
If keychain is removed from a device, there will be no sensible
resolution and client apps will bind to whatever is available.

Doesn't affect system apps which are forcibly prevented from wildcard
binding.

Bug: 27475655
Change-Id: Ide1aab3778e12f0b9a96662deb297a76d2f4997f
2016-03-03 18:57:32 +00:00
Shawn Willden
c38eae5229 Revert "Revert "Add option to allow key validity after fingerprint enrollment.""
This reverts commit 512c132f49fc6e8e4fc119f4cf167d33b2393509.

Change-Id: Iac381dfebcfe42f0468569eb2395ebeb97a95887
2016-02-22 20:38:00 -07:00
Shawn Willden
512c132f49 Revert "Add option to allow key validity after fingerprint enrollment."
This reverts commit ada0fc1b05902b11401094b15480377d1b6c5a35.

Change-Id: I934b6e6af49ab680af726c544f0193dfcb80054e
2016-02-22 22:59:34 +00:00
Shawn Willden
ada0fc1b05 Add option to allow key validity after fingerprint enrollment.
Bug: 21563854
Change-Id: I4f601e59fbfcd601e6a80ddcbc7b83ced6cc18c8
2016-02-18 15:13:50 -07:00
Shawn Willden
adef49640d Add support for extending key validity while on body.
Bug: 21563854
Change-Id: I3b622d2af77ec4ac3ba42407fc391112c153ef0f
2016-02-18 05:50:21 -07:00
Shawn Willden
8d8c747774 Add attestation API to Android KeyStore.
Bug: 22914603
Change-Id: I7c6162dc7a390aa48a2542494780959b01c23bd4
2016-02-08 19:29:54 +00:00
Alex Klyubin
bd0d4265bb Merge "Fix typo in KeyInfo Javadoc." am: e8fbc20e12
am: b21416e287

* commit 'b21416e2875973706d8b922428225194d9625875':
  Fix typo in KeyInfo Javadoc.
2016-02-05 22:37:31 +00:00
Alex Klyubin
d9dded5a46 Fix typo in KeyInfo Javadoc.
Change-Id: I801005a93aee71b93b3d6cf3f7c38361493ff203
2016-02-05 14:18:19 -08:00
Neil Fuller
9498e8a8d2 Fix @code escapes
The body of {@code} must not be HTML escaped. This is one of
several changes that fix the source in conjunction with a
doclava fix.

Bug: 25757239
(cherry-picked from commit 71fbb81b14958b80fe55738607740c6630e4e9da)

Change-Id: I19dafddc6501be6fee362c396ac5bbdc934ae39d
2016-01-15 16:14:57 +00:00
Robin Lee
3a435f0390 Return null on getPrivateKey failure not exception
According to documentation:

Returns the {@code PrivateKey} for the requested alias, or null if
there is no result.

@throws KeyChainException if the alias was valid but there was some
problem accessing it.

@throws IllegalStateException if called from the main thread.

In this case the alias doesn't exist or isn't visible to the caller so
they should get null back instead of KeyChainException.

Change-Id: Ied5603ac6aefbcef79050f24c2aa7ee8f386be0b
2015-12-21 12:09:20 +00:00
Robin Lee
fbc65644b9 DevicePolicy API to remove an installed KeyPair
The keypair is specified by alias and removed via a call to the
KeyChainService, which will have installed the pair in the first place.

Bug: 22541933
Change-Id: I37317e7c22e89816156e6e9a7abf4c5a59e8440a
2015-12-08 16:40:56 +00:00
Neil Fuller
71fbb81b14 Fix @code escapes
The body of {@code} must not be HTML escaped. This is one of
several changes that fix the source in conjunction with a
doclava fix.

Bug: 25757239
Change-Id: Ib38a0fa2dd2a3d68e467f78a812071e763d7e881
2015-12-02 14:24:11 +00:00
Alex Klyubin
7b0b9a1351 Merge "Custom engineUpdate/engineDoFinal(ByteBuffer, ByteBuffer)." am: e766e399c5 am: 4bc1904119
am: 0540e1b127

* commit '0540e1b127e03ecc97c32719ac79769d69c16aec':
  Custom engineUpdate/engineDoFinal(ByteBuffer, ByteBuffer).
2015-11-25 19:23:55 +00:00
Alex Klyubin
d976c5a4ee Custom engineUpdate/engineDoFinal(ByteBuffer, ByteBuffer).
This makes Android Keystore's Cipher implementation use a custom
implementation of engineUpdate(ByteBuffer, ByteBuffer) and
engineDoFinal(ByteBuffer, ByteBuffer). The implementation is
explicitly designed around the fact that Android Keystore transmits
input and receives output via Binder and thus there's no need to
attempt any optimizations to avoid copying input and output.

Bug: 25863382
Change-Id: I311072891f02f5e7a283628b51b8d6058b55231c
2015-11-24 17:22:27 -08:00
Alex Klyubin
8f49178b8f Merge "Insert Android Keystore JCA Provider at the correct position." am: 887d348d7e am: 5995579f5f
am: 00245ffce4

* commit '00245ffce4d08fbb6aeaeba62b8a30ce234862fa':
  Insert Android Keystore JCA Provider at the correct position.
2015-11-17 23:14:38 +00:00
Alex Klyubin
8e028e6507 Insert Android Keystore JCA Provider at the correct position.
Security.insertProviderAt uses 1-based positions whereas the
AndroidKeyStoreProvider.install code was incorrectly passing in
0-based positions, thus installing the AndroidKeyStoreBCWorkaround
provider one level higher than intended. This change fixes the issue
in AndroidKeyStoreProvider.

Bug: 25399691
Change-Id: I4a66bf37c0d151edb9a2349db9d91939064c0574
2015-11-16 10:11:50 -08:00
Alex Klyubin
2cea8d4b7b Merge "Allow uid to be passed for more operations" 2015-09-10 22:37:50 +00:00
Alex Klyubin
3876b1be27 Support cross-UID access from AndroidKeyStore.
This is meant for exposing the pre-existing cross-UID access to keys
backed by the keystore service via higher-level JCA API. For example,
this lets system_server use Wi-Fi or VPN UID keys via JCA API.

To obtain a JCA AndroidKeyStore KeyStore for another UID, use the
hidden system API AndroidKeyStoreProvider.getKeyStoreForUid(uid).

To generate a key owned by another UID, invoke setUid(uid) on
KeyGenParameterSpec.Builder.

This CL does not change the security policy, such as which UID can
access/modify which UIDs' keys. The policy is that only certain system
UIDs are permitted to access keys of certain other system UIDs.

Bug: 23978113
Change-Id: Ie381530f41dc41c50d52f675fb9e68bc87c006de
2015-09-10 15:35:06 -07:00
Chad Brubaker
5bbf04803f Allow uid to be passed for more operations
This expands get, getmtime, exportKey, getKeyCharacteristcs and begin to
accept a uid to run as. This is only for system to use keys owned by
Wifi and VPN, and not something that can be used to do operations as
another arbitrary application.

Bug: 23978113
Change-Id: I06aa089859edc934a5415e3b184b917d6d171ae2
2015-09-10 15:34:11 -07:00
Alex Klyubin
72245d7909 Add more examples of generating/importing keys to Javadocs.
Based on developer feedback, this updates Android Keystore Javadocs
with more examples of generating and importing keys of various
algorithms. This also clarifies that key use authorizations apply to
secret and private key and do no apply to public keys.

Bug: 23102874
Change-Id: If0dc20fda4836fd23b9cd9c92490a04e71b19fc0
2015-08-11 11:36:09 -07:00
Shawn Willden
75e51ec0b1 Merge "Raw RSA Cipher relies on keymaster for padding and range checks." into mnc-dev 2015-07-30 19:57:22 +00:00
Alex Klyubin
e4928a2912 Keymaster digest/padding NONE no longer means ANY.
This adjusts the public API documentation to no longer say that
digest/padding NONE means any digest/padding. This also changes the
implementation of legacy key generation and import to explicitly
list which digests/paddings the generated/imported key is authorized
for. Previously, such keys were simply authorized for digest NONE and
padding NONE.

Bug: 22556114
Change-Id: Id02d9450a07de16ccb795b76b6de0006dd49dcca
2015-07-28 13:00:56 -06:00
Alex Klyubin
3ab1f04004 Raw RSA Cipher relies on keymaster for padding and range checks.
This makes Android Keystore's RSA/ECB/NoPadding Cipher implementation
rely on the underlying keystore/keymaster to left-pad the input (if
necessary) and to reject invalid input. Prior to this change the
Cipher implementation attempted to do it itself, but wasn't doing it
right anyway.

This fixes a regression where Android Keystore's raw RSA Cipher
("RSA/ECB/NoPadding") refused to encrypt plaintexts of the same length
(in bytes) as RSA modulus which were nevertheless numerically smaller
than the RSA modulus.

Bug: 22599805
Change-Id: I591a8115a574eaf8f6075f29b50d93a87532c5eb
2015-07-22 13:16:04 -07:00
Alex Klyubin
ada70be897 Merge "Add KM_MIN_MAC_LENGTH tag to HMAC and AES-GCM keys." into mnc-dev 2015-07-22 15:24:27 +00:00
Alex Klyubin
5d586dd585 Merge "KM module may consume less input than provided by finish time." into mnc-dev 2015-07-21 19:50:50 +00:00
Alex Klyubin
a95550f801 Add KM_MIN_MAC_LENGTH tag to HMAC and AES-GCM keys.
This makes Android Keystore add the KM_MIN_MAC_LENGTH tag to generated
and imported HMAC and AES-GCM keys. This tag specifies the minimum
length of the MAC/authentication tag authorized to be used for the
key.

For HMAC keys the minimum MAC length is set to the length of the
digest associated with the key (HMAC keys are authorized for exactly
one digest). For AES keys the minimum authetication tag length is set
to 96 bit. This is the minimum supported by Android Keystore's AES-GCM
implementation.

Bug: 22337277
Change-Id: Ic6e47cf084734d1592788dc58088889f7fff74eb
2015-07-16 16:52:51 -07:00
Alex Klyubin
53d544a4b9 KM module may consume less input than provided by finish time.
Keymaster1 HAL permits the implementation of "update" operation to
leave some input unconsumed by the time "finish" operation neeeds to
be invoked. This needs to be treated as "invalid input" error rather
than a "can't happen" exception.

This CL was confirmed to fix the issue by the vendor who encountered
the issue.

Bug: 22512100
Change-Id: Ibb1a37d58f650d03605612559a154ce2416d147c
2015-07-16 14:18:49 -07:00
Alex Klyubin
c58153b2d7 HMAC keys are authorized for exactly one digest.
This is in preparation for enforcing the minimum length of MACs
which in the case of HMAC keys generated or imported by Android
Keystore will be set to the length of the digest for which the
key is authorized.

Bug: 22337277
Change-Id: I0255d5ba184dabfb6b45d8f32ddadeb84ab7fc19
2015-07-16 09:59:51 -07:00
Alex Klyubin
6f2eb6d7a6 Fix Android Keystore key factories to obey JCA contract.
Android Keystore provider's KeyFactory and SecretKeyFactory
implementations were throwing UnsupportedOperationException instead of
InvalidKeyException/InvalidKeySpecException from their
translateKey/generateKey methods.

Bug: 22459811
Change-Id: I6d5a5dc1bed724e858ad324d558b7480b9b848da
2015-07-13 15:26:17 -07:00
Alex Klyubin
d25093d2fd Merge "Reject AlgorithmParameters of wrong type." into mnc-dev 2015-07-08 19:32:32 +00:00
Alex Klyubin
5c38e7918a Reject AlgorithmParameters of wrong type.
This makes Android Keystore Cipher implementations reject rather than
ignore AlgorithmParameters of wrong algorithm type. The danger in not
doing so is that a Cipher will produce output that does not actually
depend on the provided AlgorithmParameters.

Bug: 22330716
Change-Id: Ifa9de2c74f2fe4b738a3731c895059dddd075a13
2015-07-07 15:39:10 -07:00
Alex Klyubin
024bce8229 Merge "Support loading AES keys with authorized digests." into mnc-dev 2015-07-07 18:54:33 +00:00
Alex Klyubin
ca7aaeaeee Support loading AES keys with authorized digests.
Due to a bug, Android Keystore failed to load AES keys authorized for
use with one or more digests. This CL fixes this bug.

Bug: 22300737
Change-Id: Ia49e27833dddb526565e4dc4977ed1e352e5836b
2015-07-06 14:09:01 -07:00
Alex Klyubin
b6e628644a Avoid IllegalStateException when generating/importing keys.
This avoids IllegalStateException when generating/importing keys which
require user authentication when the system is not configured to
generate/import such keys (e.g., secure lock screen not set up).

The documentation states that before generating/importing such keys
apps should check (using public API) whether the system is in a
suitable state. However, some apps are not doing that and instead
catching the IllegalStateException thrown during key
generation/import. This is a bad practice because this exception is an
undocumented implementation detail and should thus not be depended
upon.

This CL addresses this issue as follows:
1. Key(Pair)Generator.init now throws a checked
   InvalidAlgorithmParameterException when the system is in a wrong
   state. Because in most uses of Key(Pair)Generator .init is
   immediately followed by .generate, this prevents .generate from
   encountering this state and does so using a checked exception
   which is part of public API.
2. Key import rethrows the IllegalStateException as a checked
   KeyStoreException which is meant to be thrown if the key cannot be
   imported for any reason. Key(Pair)Generator.generate unfortunately
   cannot throw any checked exceptions and thus has to continue
   throwing unchecked exceptions.

Bug: 22262809
Change-Id: Ic0f7b7a90e0ba63df9139c79b80a8649d2645d2a
2015-07-06 11:27:16 -07:00
Robin Lee
a7bdb6c99b Merge "KeyChain: annotate with @WorkerThread" into mnc-dev 2015-07-03 16:48:15 +00:00
Robin Lee
59e3baa8ab KeyChain: annotate with @WorkerThread
Several methods need to be called off the main UI thread. This is
the first documentation of that requirement.

Bug: 19440165
Change-Id: I0303011c0ded6ec1efa92119c1e02a8a39b14a59
2015-06-30 13:08:46 -07:00
Alex Klyubin
fdbc02a433 Enforce IND-CPA requirement when generating asymmetric keys.
This enforces the randomized encryption requirement (IND-CPA), if
requested, when generating asymmetric key pairs. Whether randomized
encryption is used depends on the encryption padding modes authorized
for the key pair. Thus, if randomized encryption is required, the
KeyPairGenerator must reject attempts to generate keys authorized for
encryption using non-compliant padding schemes.

This is similar to the existing check in AndroidKeyStoreImpl during
asymmetric key import.

Bug: 22179911
Change-Id: I3d85367259c17bd44198a736ace853d0d3567d5e
2015-06-29 14:39:29 -07:00
Alex Klyubin
903d0fb988 Don't offer crypto ops for public keys of trusted cert entries.
Android Keystore cannot offer crypto operations for public keys of
trusted certificate entries (entries without a private key). Prior to
this CL it accidentally tried to do so, causing crypto operations on
these keys to fail.

The fix is for Android Keystore to offer crypto operations only for
public keys for which there is a corresponding private key in the
keystore. Crypto operations on public keys from trusted certificate
entries will be handled by other installed crypto providers. Those
providers don't need a private key to carry out these operations on
public keys.

Bug: 22091725
Bug: 21835320
Change-Id: Ib7d92b067711e4c57128d0db72c08bf288a45ce1
2015-06-25 18:44:43 -07:00
Alex Klyubin
25d2270704 Support RSA encrypt using private key and PKCS#1 paddding.
Some apps such as OpenVPN Connect for some reason generate
PKCS#1-padded RSA signatures using Cipher (initialized in
Cipher.ENCRYPT_MODE with private key!) instead of using Signature.

Unfortunately, RI supports this strange behavior and previous releases
of Android Keystore did the same. So, we have to continue supporting
this craziness.

Bug: 22083023
Change-Id: Ife3950d0d4ceb4e44e08014635312c1252878b69
2015-06-25 09:09:04 -07:00
Alex Klyubin
d23dc502b0 Make NONEwithECDSA truncate input when necessary.
Keymaster's implementation of ECDSA with digest NONE rejects input
longer than group size in bytes. RI's NONEwithECDSA accepts inputs
of arbitrary length by truncating them to the above size. This CL
makes Android Keystore's NONEwithECDSA do the truncation to keep
the JCA and Keymaster happy.

The change is inside AndroidKeyStoreECDSASignatureSpi$NONE. All other
small modifications are for supporting that change by making it
possible for AndroidKeyStoreSignatureSpiBase to pass in the signature
being verified into KeyStoreCryptoOperationStreamer. This in turn is
needed to make it possible for NONEwithECDSA implementation to provide
a wrapper streamer which truncates input.

Bug: 22030217
Change-Id: I26064f6df37ef8c631d70a36a356aa0b76a9ad29
2015-06-24 15:42:09 -07:00
Alex Klyubin
ae6cb7aad5 Keymaster INT, LONG and DATE tag values are unsigned.
This CL ensures that Android Keystore framework code complies with
signedness of keymaster tags. In particular:
* INT tags are unsigned 32-bit numbers, and
* LONG and DATE tags are unsigned 64-bit numbers.

The ensure compliance, KeymasterArguments and KeyCharacteristics
classes through which Android Keystore interacts with Keymaster tags
have been modified as follows:
* ENUM and INT tags which used to be conflated are now added/queried
  via separate methods, because ENUM can remain represented as an int
  data type whereas INT is now represented as a long data type with
  permitted range being [0; 2^32).
* Methods for adding/quering LONG tags have been switched from the long
  data type to the BigInteger data type and now ensure that the value
  is in the permitted [0; 2^63).
* Methods for adding/querying DATE tags now ensure the Date value is
  in the permitted range [0; 2^63) ms since Unix epoch.
* Methods for adding tags throw an IllegalArgumentException if the tag
  type is unsuitable for the method. This is to ensure that tags with
  invalid values cannot be added through similar methods (e.g., INT tag
  added via an ENUM tag addition method invoked with a negative value).

Bug: 22008538
Change-Id: I6eefd5cbb561cc52d27de952691af4d9d5e1af1e
2015-06-23 20:04:28 -07:00
Alex Klyubin
90c9e4eb54 Merge "Keymaster key validity dates are optional." into mnc-dev 2015-06-23 20:35:30 +00:00
Alex Klyubin
d6c7799b9a Keymaster key validity dates are optional.
This CL makes Android Keystore framework code add
KM_TAG_ACTIVE_DATETIME, KM_TAG_ORIGINATION_EXPIRE_DATETIME, and
KM_TAG_USAGE_EXPIRE_DATETIME tags to the authorizations set only
if the corresponding time instants were specified through the
framework-level API. This is fine because these tags are optional as
it turns out.

Bug: 18088752
Change-Id: I6a5ae4cadb441e61576231815e6bec6e9248bc72
2015-06-23 12:06:27 -07:00
Alex Klyubin
81d299e4b8 Merge "Don't fail if self-signed certificate can't be signed." into mnc-dev 2015-06-23 18:58:25 +00:00
Alex Klyubin
856aebe571 Don't fail if self-signed certificate can't be signed.
This makes Android Keystore's KeyPairGenerator fall back to generating
a self-signed certificate with an invalid/fake signature when the
attempt to generate a self-signed certificate with a valid signature
fails.

There is a growing number of reasons/authorizations due to which the
generated private key cannot be used to sign the self-signed
certificate. It's safer for KeyPairGenerator to succeed than to fail.

Bug: 22033161
Change-Id: I1ecbd421346166bfd536b5cfbaea169b11f0b1c8
2015-06-23 10:54:44 -07:00
Alex Klyubin
7ecb298c6d Fix typo in Android Keystore KeyFactory exception message.
The typo was pointed out by kroot@ in code review of
4ecd092226fbd748b4a26f3bbb5c4d25a3488fff after it was submitted.

Bug: 18088752
Change-Id: I50d2fa93e1a2d352fe432e34ce98add82a71c483
2015-06-23 09:09:11 -07:00
Alex Klyubin
e26f9d95c6 Merge "Adjust minimum size of PKCS#1 RSA padding overhead." into mnc-dev 2015-06-22 22:27:51 +00:00
Alex Klyubin
3867709fb3 Document that RSA OAEP requires digest authorization.
RSA encryption/decryption keys used with RSA OAEP padding scheme now
require the key to be authorized for the digest used by OAEP.

Bug: 21998286
Change-Id: I1f5eb1b30a1b574c45ffcabed6096de8671882d3
2015-06-22 13:42:46 -07:00
Alex Klyubin
6e90ade5dd Adjust minimum size of PKCS#1 RSA padding overhead.
This raises the space overhead estimate by one byte, due to me having
originally forgotten that when signing using RSA PKCS#1 padding
scheme, the plaintext of the RSA operation needs a leading 0 byte.

Bug: 21955742
Change-Id: If78efcd375afd8902a14a2217ddedfea87d4d337
2015-06-22 13:21:59 -07:00
Alex Klyubin
84a378f256 Merge "Fix a bug in Android Keystore updateAAD." into mnc-dev 2015-06-19 22:53:21 +00:00