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
4 files changed