Skip to content

Commit a4640c0

Browse files
committed
Add some NullPointerExceptions to AndroidKeyStore
Existing KeyStore implementations throw NullPointerExceptions beacuse the KeyStoreSpi doesn't check these arguments for null. Add in checks so we don't accidentally check some bogus values. Also switch a RuntimeException to a KeyStoreException Change-Id: I18f4d4474d607cb2057ea8069b901e0992275e78
1 parent 2701f32 commit a4640c0

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

keystore/java/android/security/AndroidKeyStore.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ public Key engineGetKey(String alias, char[] password) throws NoSuchAlgorithmExc
9090

9191
@Override
9292
public Certificate[] engineGetCertificateChain(String alias) {
93+
if (alias == null) {
94+
throw new NullPointerException("alias == null");
95+
}
96+
9397
final X509Certificate leaf = (X509Certificate) engineGetCertificate(alias);
9498
if (leaf == null) {
9599
return null;
@@ -119,6 +123,10 @@ public Certificate[] engineGetCertificateChain(String alias) {
119123

120124
@Override
121125
public Certificate engineGetCertificate(String alias) {
126+
if (alias == null) {
127+
throw new NullPointerException("alias == null");
128+
}
129+
122130
byte[] certificate = mKeyStore.get(Credentials.USER_CERTIFICATE + alias);
123131
if (certificate != null) {
124132
return toCertificate(certificate);
@@ -166,6 +174,10 @@ private Date getModificationDate(String alias) {
166174

167175
@Override
168176
public Date engineGetCreationDate(String alias) {
177+
if (alias == null) {
178+
throw new NullPointerException("alias == null");
179+
}
180+
169181
Date d = getModificationDate(Credentials.USER_PRIVATE_KEY + alias);
170182
if (d != null) {
171183
return d;
@@ -325,7 +337,7 @@ private void setPrivateKeyEntry(String alias, PrivateKey key, Certificate[] chai
325337
@Override
326338
public void engineSetKeyEntry(String alias, byte[] userKey, Certificate[] chain)
327339
throws KeyStoreException {
328-
throw new RuntimeException("Operation not supported because key encoding is unknown");
340+
throw new KeyStoreException("Operation not supported because key encoding is unknown");
329341
}
330342

331343
@Override
@@ -334,6 +346,11 @@ public void engineSetCertificateEntry(String alias, Certificate cert) throws Key
334346
throw new KeyStoreException("Entry exists and is not a trusted certificate");
335347
}
336348

349+
// We can't set something to null.
350+
if (cert == null) {
351+
throw new NullPointerException("cert == null");
352+
}
353+
337354
final byte[] encoded;
338355
try {
339356
encoded = cert.getEncoded();
@@ -348,6 +365,10 @@ public void engineSetCertificateEntry(String alias, Certificate cert) throws Key
348365

349366
@Override
350367
public void engineDeleteEntry(String alias) throws KeyStoreException {
368+
if (!isKeyEntry(alias) && !isCertificateEntry(alias)) {
369+
return;
370+
}
371+
351372
if (!Credentials.deleteAllTypesForAlias(mKeyStore, alias)) {
352373
throw new KeyStoreException("No such entry " + alias);
353374
}
@@ -380,6 +401,10 @@ public Enumeration<String> engineAliases() {
380401

381402
@Override
382403
public boolean engineContainsAlias(String alias) {
404+
if (alias == null) {
405+
throw new NullPointerException("alias == null");
406+
}
407+
383408
return mKeyStore.contains(Credentials.USER_PRIVATE_KEY + alias)
384409
|| mKeyStore.contains(Credentials.USER_CERTIFICATE + alias)
385410
|| mKeyStore.contains(Credentials.CA_CERTIFICATE + alias);
@@ -396,12 +421,24 @@ public boolean engineIsKeyEntry(String alias) {
396421
}
397422

398423
private boolean isKeyEntry(String alias) {
424+
if (alias == null) {
425+
throw new NullPointerException("alias == null");
426+
}
427+
399428
return mKeyStore.contains(Credentials.USER_PRIVATE_KEY + alias);
400429
}
401430

431+
private boolean isCertificateEntry(String alias) {
432+
if (alias == null) {
433+
throw new NullPointerException("alias == null");
434+
}
435+
436+
return mKeyStore.contains(Credentials.CA_CERTIFICATE + alias);
437+
}
438+
402439
@Override
403440
public boolean engineIsCertificateEntry(String alias) {
404-
return !isKeyEntry(alias) && mKeyStore.contains(Credentials.CA_CERTIFICATE + alias);
441+
return !isKeyEntry(alias) && isCertificateEntry(alias);
405442
}
406443

407444
@Override

0 commit comments

Comments
 (0)