436 Commits

Author SHA1 Message Date
Kenny Root
8a6c191094 Use canonical name for SHA1withECDSA
The Standard Names documentation says that "ECDSA" should not be used
due to its ambiguity. Bouncycastle has switched from "ECDSA" to
"SHA1withECDSA" so therefore we should switch this "workaround" Provider
to use the same name to keep it functioning correctly.

Bug: 27753949
Change-Id: I5acfca9d89c85bf005902a2f06cb5d7b1ef9dff2
2016-03-23 08:52:28 -07: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
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
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
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
Alex Klyubin
ae1e0e9265 Fix a bug in Android Keystore updateAAD.
The variant of CipherSpi.engineUpdateAAD which takes ByteBuffers
delegated to a byte[]-taking variant of superclass instead of its own
class.

Bug: 18088752
Change-Id: I3b426bbdca54bbf7f6f3939589da07b24db14d0d
2015-06-19 15:50:45 -07:00
Alex Klyubin
4ecd092226 Support more KeySpecs in Android Keystore KeyFactory.
This adds support obtaining RSAPublicKeySpec, ECPublicKeySpec,
X509EncodedKeySpec from Android Keystore public keys.

Using a KeyFactory to obtain such specs is the endorsed way for
obtaining algorithm-specific parameters or X.509 encoding of PublicKey
instances.

Bug: 18088752
Change-Id: I2c653238e3c89d9cfc97bea6c8a0ef0c6d039385
2015-06-18 14:18:18 -07:00
Narayan Kamath
240e48d2e1 Use consistent cases for aliases in AndroidKeystoreBCWorkaround.
Should be "with" and not "With". Has the side effect of fixing
a test that assumes that these names are case-sensitive. While the
test must be fixed separately, this is still good for consistency.

bug: 21870226

Change-Id: I884b4abdbb18be064210555aec8e0cd16b0d0bcb
2015-06-18 09:52:11 +00:00
Alex Klyubin
2e3aaa7dc1 Remove unnecessary PKCS#1 authorization on legacy keys.
There is no need to authorize PKCS#1 signature padding scheme when
NONE padding scheme is authorized. NONE authorizes the use of any
padding scheme.

Bug: 18088752
Change-Id: I73ccb373d577c988acde372d972092278923c4e4
2015-06-17 13:58:00 -07:00
Alex Klyubin
97f4d3b407 Merge "Prefer GCM to CBC or CTR in documentation." into mnc-dev 2015-06-17 18:32:24 +00:00
Alex Klyubin
211cb7cb98 Merge "Improve documentation for keys requiring user auth." into mnc-dev 2015-06-17 18:28:21 +00:00
Alex Klyubin
a5e21f0ee2 Prefer GCM to CBC or CTR in documentation.
Bug: 21786749
Change-Id: If3824d1321ef1d8730d4384717b0c25d43518fe4
2015-06-17 11:24:45 -07:00
Alex Klyubin
83cc7a347f Improve documentation for keys requiring user auth.
Bug: 18088752
Change-Id: I24eeb33790a74d8d81d10fcdb1eb058d47144c44
2015-06-17 11:08:29 -07:00
Alex Klyubin
4250c8d643 Merge "Expose AES GCM backed by Android Keystore." into mnc-dev 2015-06-17 17:58:35 +00:00
Alex Klyubin
bf5c91c8cd Drop support for P-192 EC keys.
The minimum EC keys size is now 224 bit. This is because keymaster 1.0
does not support EC keys smaller than 224 bit.

Bug: 18088752
Change-Id: I8010ff2d6ccaeea7dcf86f697aee1d9ebd683cb2
2015-06-17 10:51:19 -07:00
Alex Klyubin
00af27b7d9 Expose AES GCM backed by Android Keystore.
Bug: 18088752
Bug: 21786749
Change-Id: Ica90491037d2920f7635195894ba18882fc4406d
2015-06-17 09:24:06 -07:00
Alex Klyubin
ffdfb57931 Harden KeyInfo.
This ensures that KeyInfo instances are truly immutable.

Bug: 18088752
Change-Id: Ic157d3b928b129a02c3cab9dd20ba9936a005813
2015-06-16 13:13:13 -07:00
Alex Klyubin
cb3bb3f03a Harden and clean up KeyProtection.
This ensures that instances of this class are immutable and cleans up
some flawed logic discovered when writing up CTS tests.

Bug: 18088752
Change-Id: If9cc33192ada5a8982a2b200ab24cb18331cf958
2015-06-16 12:31:34 -07:00
Alex Klyubin
1b937eebdd Harden and clean up KeyGenParameterSpec.
This ensures that instances of this class are immutable and cleans up
some flawed logic discovered when writing up CTS tests.

Bug: 18088752
Change-Id: I7a27dbfc3445599bccabbd26e39535ce988a2ab2
2015-06-16 12:11:10 -07:00