| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.security.keystore2; |
| 18 |
|
| 19 |
import android.annotation.CallSuper; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.hardware.security.keymint.KeyParameter; |
| 22 |
import android.security.KeyStoreException; |
| 23 |
import android.security.KeyStoreOperation; |
| 24 |
import android.security.keymaster.KeymasterDefs; |
| 25 |
import android.security.keystore.ArrayUtils; |
| 26 |
import android.security.keystore.KeyStoreCryptoOperation; |
| 27 |
|
| 28 |
import libcore.util.EmptyArray; |
| 29 |
|
| 30 |
import java.nio.ByteBuffer; |
| 31 |
import java.security.InvalidKeyException; |
| 32 |
import java.security.InvalidParameterException; |
| 33 |
import java.security.NoSuchAlgorithmException; |
| 34 |
import java.security.PrivateKey; |
| 35 |
import java.security.ProviderException; |
| 36 |
import java.security.PublicKey; |
| 37 |
import java.security.SecureRandom; |
| 38 |
import java.security.Signature; |
| 39 |
import java.security.SignatureException; |
| 40 |
import java.security.SignatureSpi; |
| 41 |
import java.util.ArrayList; |
| 42 |
import java.util.List; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
abstract class AndroidKeyStoreSignatureSpiBase extends SignatureSpi |
| 50 |
implements KeyStoreCryptoOperation { |
| 51 |
private static final String TAG = "AndroidKeyStoreSignatureSpiBase"; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
private boolean mSigning; |
| 56 |
private AndroidKeyStoreKey mKey; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
private KeyStoreOperation mOperation; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
private long mOperationChallenge; |
| 71 |
private KeyStoreCryptoOperationStreamer mMessageStreamer; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
private Exception mCachedException; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
private Signature mSignature; |
| 87 |
|
| 88 |
AndroidKeyStoreSignatureSpiBase() { |
| 89 |
mOperation = null; |
| 90 |
mOperationChallenge = 0; |
| 91 |
mSigning = false; |
| 92 |
mKey = null; |
| 93 |
appRandom = null; |
| 94 |
mMessageStreamer = null; |
| 95 |
mCachedException = null; |
| 96 |
mSignature = null; |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
protected final void engineInitSign(PrivateKey key) throws InvalidKeyException { |
| 101 |
engineInitSign(key, null); |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
protected final void engineInitSign(PrivateKey privateKey, SecureRandom random) |
| 106 |
throws InvalidKeyException { |
| 107 |
resetAll(); |
| 108 |
|
| 109 |
boolean success = false; |
| 110 |
try { |
| 111 |
if (privateKey == null) { |
| 112 |
throw new InvalidKeyException("Unsupported key: null"); |
| 113 |
} |
| 114 |
AndroidKeyStoreKey keystoreKey; |
| 115 |
if (privateKey instanceof AndroidKeyStorePrivateKey) { |
| 116 |
keystoreKey = (AndroidKeyStoreKey) privateKey; |
| 117 |
} else { |
| 118 |
throw new InvalidKeyException("Unsupported private key type: " + privateKey); |
| 119 |
} |
| 120 |
mSigning = true; |
| 121 |
initKey(keystoreKey); |
| 122 |
appRandom = random; |
| 123 |
ensureKeystoreOperationInitialized(); |
| 124 |
success = true; |
| 125 |
} finally { |
| 126 |
if (!success) { |
| 127 |
resetAll(); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
protected final void engineInitVerify(PublicKey publicKey) throws InvalidKeyException { |
| 134 |
resetAll(); |
| 135 |
|
| 136 |
try { |
| 137 |
mSignature = Signature.getInstance(getAlgorithm()); |
| 138 |
} catch (NoSuchAlgorithmException e) { |
| 139 |
throw new InvalidKeyException(e); |
| 140 |
} |
| 141 |
|
| 142 |
mSignature.initVerify(publicKey); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
@CallSuper |
| 151 |
protected void initKey(AndroidKeyStoreKey key) throws InvalidKeyException { |
| 152 |
mKey = key; |
| 153 |
} |
| 154 |
|
| 155 |
private void abortOperation() { |
| 156 |
KeyStoreCryptoOperationUtils.abortOperation(mOperation); |
| 157 |
mOperation = null; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
@CallSuper |
| 168 |
protected void resetAll() { |
| 169 |
abortOperation(); |
| 170 |
mOperationChallenge = 0; |
| 171 |
mSigning = false; |
| 172 |
mKey = null; |
| 173 |
appRandom = null; |
| 174 |
mMessageStreamer = null; |
| 175 |
mCachedException = null; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
@CallSuper |
| 187 |
protected void resetWhilePreservingInitState() { |
| 188 |
abortOperation(); |
| 189 |
mOperationChallenge = 0; |
| 190 |
mMessageStreamer = null; |
| 191 |
mCachedException = null; |
| 192 |
} |
| 193 |
|
| 194 |
private void ensureKeystoreOperationInitialized() throws InvalidKeyException { |
| 195 |
if (mMessageStreamer != null) { |
| 196 |
return; |
| 197 |
} |
| 198 |
if (mCachedException != null) { |
| 199 |
return; |
| 200 |
} |
| 201 |
if (mKey == null) { |
| 202 |
throw new IllegalStateException("Not initialized"); |
| 203 |
} |
| 204 |
|
| 205 |
List<KeyParameter> parameters = new ArrayList<>(); |
| 206 |
addAlgorithmSpecificParametersToBegin(parameters); |
| 207 |
|
| 208 |
int purpose = mSigning ? KeymasterDefs.KM_PURPOSE_SIGN : KeymasterDefs.KM_PURPOSE_VERIFY; |
| 209 |
|
| 210 |
parameters.add(KeyStore2ParameterUtils.makeEnum(KeymasterDefs.KM_TAG_PURPOSE, purpose)); |
| 211 |
|
| 212 |
try { |
| 213 |
mOperation = mKey.getSecurityLevel().createOperation( |
| 214 |
mKey.getKeyIdDescriptor(), |
| 215 |
parameters); |
| 216 |
} catch (KeyStoreException keyStoreException) { |
| 217 |
throw KeyStoreCryptoOperationUtils.getInvalidKeyException( |
| 218 |
mKey, keyStoreException); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
mOperationChallenge = KeyStoreCryptoOperationUtils.getOrMakeOperationChallenge( |
| 225 |
mOperation, mKey); |
| 226 |
|
| 227 |
mMessageStreamer = createMainDataStreamer(mOperation); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
@NonNull |
| 236 |
protected KeyStoreCryptoOperationStreamer createMainDataStreamer( |
| 237 |
@NonNull KeyStoreOperation operation) { |
| 238 |
return new KeyStoreCryptoOperationChunkedStreamer( |
| 239 |
new KeyStoreCryptoOperationChunkedStreamer.MainDataStream( |
| 240 |
operation)); |
| 241 |
} |
| 242 |
|
| 243 |
@Override |
| 244 |
public final long getOperationHandle() { |
| 245 |
return mOperationChallenge; |
| 246 |
} |
| 247 |
|
| 248 |
@Override |
| 249 |
protected final void engineUpdate(byte[] b, int off, int len) throws SignatureException { |
| 250 |
if (mSignature != null) { |
| 251 |
mSignature.update(b, off, len); |
| 252 |
return; |
| 253 |
} |
| 254 |
|
| 255 |
if (mCachedException != null) { |
| 256 |
throw new SignatureException(mCachedException); |
| 257 |
} |
| 258 |
|
| 259 |
try { |
| 260 |
ensureKeystoreOperationInitialized(); |
| 261 |
} catch (InvalidKeyException e) { |
| 262 |
throw new SignatureException(e); |
| 263 |
} |
| 264 |
|
| 265 |
if (len == 0) { |
| 266 |
return; |
| 267 |
} |
| 268 |
|
| 269 |
byte[] output; |
| 270 |
try { |
| 271 |
output = mMessageStreamer.update(b, off, len); |
| 272 |
} catch (KeyStoreException e) { |
| 273 |
throw new SignatureException(e); |
| 274 |
} |
| 275 |
|
| 276 |
if (output.length != 0) { |
| 277 |
throw new ProviderException( |
| 278 |
"Update operation unexpectedly produced output: " + output.length + " bytes"); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
@Override |
| 283 |
protected final void engineUpdate(byte b) throws SignatureException { |
| 284 |
engineUpdate(new byte[] {b}, 0, 1); |
| 285 |
} |
| 286 |
|
| 287 |
@Override |
| 288 |
protected final void engineUpdate(ByteBuffer input) { |
| 289 |
byte[] b; |
| 290 |
int off; |
| 291 |
int len = input.remaining(); |
| 292 |
if (input.hasArray()) { |
| 293 |
b = input.array(); |
| 294 |
off = input.arrayOffset() + input.position(); |
| 295 |
input.position(input.limit()); |
| 296 |
} else { |
| 297 |
b = new byte[len]; |
| 298 |
off = 0; |
| 299 |
input.get(b); |
| 300 |
} |
| 301 |
|
| 302 |
try { |
| 303 |
engineUpdate(b, off, len); |
| 304 |
} catch (SignatureException e) { |
| 305 |
mCachedException = e; |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
@Override |
| 310 |
protected final int engineSign(byte[] out, int outOffset, int outLen) |
| 311 |
throws SignatureException { |
| 312 |
return super.engineSign(out, outOffset, outLen); |
| 313 |
} |
| 314 |
|
| 315 |
@Override |
| 316 |
protected final byte[] engineSign() throws SignatureException { |
| 317 |
if (mCachedException != null) { |
| 318 |
throw new SignatureException(mCachedException); |
| 319 |
} |
| 320 |
|
| 321 |
byte[] signature; |
| 322 |
try { |
| 323 |
ensureKeystoreOperationInitialized(); |
| 324 |
|
| 325 |
byte[] additionalEntropy = |
| 326 |
KeyStoreCryptoOperationUtils.getRandomBytesToMixIntoKeystoreRng( |
| 327 |
appRandom, getAdditionalEntropyAmountForSign()); |
| 328 |
signature = mMessageStreamer.doFinal( |
| 329 |
EmptyArray.BYTE, 0, 0, |
| 330 |
null); |
| 331 |
} catch (InvalidKeyException | KeyStoreException e) { |
| 332 |
throw new SignatureException(e); |
| 333 |
} |
| 334 |
|
| 335 |
resetWhilePreservingInitState(); |
| 336 |
return signature; |
| 337 |
} |
| 338 |
|
| 339 |
@Override |
| 340 |
protected final boolean engineVerify(byte[] signature) throws SignatureException { |
| 341 |
if (mSignature != null) { |
| 342 |
return mSignature.verify(signature); |
| 343 |
} |
| 344 |
throw new IllegalStateException("Not initialised."); |
| 345 |
} |
| 346 |
|
| 347 |
@Override |
| 348 |
protected final boolean engineVerify(byte[] sigBytes, int offset, int len) |
| 349 |
throws SignatureException { |
| 350 |
return engineVerify(ArrayUtils.subarray(sigBytes, offset, len)); |
| 351 |
} |
| 352 |
|
| 353 |
@Deprecated |
| 354 |
@Override |
| 355 |
protected final Object engineGetParameter(String param) throws InvalidParameterException { |
| 356 |
throw new InvalidParameterException(); |
| 357 |
} |
| 358 |
|
| 359 |
@Deprecated |
| 360 |
@Override |
| 361 |
protected final void engineSetParameter(String param, Object value) |
| 362 |
throws InvalidParameterException { |
| 363 |
throw new InvalidParameterException(); |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
protected abstract String getAlgorithm(); |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
protected final boolean isSigning() { |
| 378 |
return mSigning; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
protected abstract int getAdditionalEntropyAmountForSign(); |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
protected abstract void addAlgorithmSpecificParametersToBegin( |
| 401 |
@NonNull List<KeyParameter> parameters); |
| 402 |
} |