Skip to content

Commit 7e67871

Browse files
committed
Clean up DevicePolicyManager CA certificate APIs
For consistency all CA-related methods now require the ComponentName of a DeviceAdminReceiver as the first parameter. Updated javadoc for this and added in some more detail about the methods in general as well. Created two new utility APIs,one to list all installed CAs and one to remove all installed (user) CAs. Deleted old hasAnyCaCertsInstalled method because it is now redundant. @bug 16488006 Change-Id: I55eec17e01489ab323f8a0e68b11592605a7b740
1 parent 8d2ba6d commit 7e67871

2 files changed

Lines changed: 53 additions & 15 deletions

File tree

api/current.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5412,6 +5412,7 @@ package android.app.admin {
54125412
method public boolean getCrossProfileCallerIdDisabled(android.content.ComponentName);
54135413
method public java.util.List<java.lang.String> getCrossProfileWidgetProviders(android.content.ComponentName);
54145414
method public int getCurrentFailedPasswordAttempts();
5415+
method public java.util.List<byte[]> getInstalledCaCerts();
54155416
method public int getKeyguardDisabledFeatures(android.content.ComponentName);
54165417
method public int getMaximumFailedPasswordsForWipe(android.content.ComponentName);
54175418
method public long getMaximumTimeToLock(android.content.ComponentName);
@@ -5431,7 +5432,6 @@ package android.app.admin {
54315432
method public boolean getStorageEncryption(android.content.ComponentName);
54325433
method public int getStorageEncryptionStatus();
54335434
method public java.util.List<java.lang.String> getTrustAgentFeaturesEnabled(android.content.ComponentName, android.content.ComponentName);
5434-
method public boolean hasAnyCaCertsInstalled();
54355435
method public boolean hasCaCertInstalled(byte[]);
54365436
method public boolean hasGrantedPolicy(android.content.ComponentName, int);
54375437
method public boolean installCaCert(android.content.ComponentName, byte[]);
@@ -5479,6 +5479,7 @@ package android.app.admin {
54795479
method public int setStorageEncryption(android.content.ComponentName, boolean);
54805480
method public void setTrustAgentFeaturesEnabled(android.content.ComponentName, android.content.ComponentName, java.util.List<java.lang.String>);
54815481
method public boolean switchUser(android.content.ComponentName, android.os.UserHandle);
5482+
method public void uninstallAllUserCaCerts(android.content.ComponentName);
54825483
method public void uninstallCaCert(android.content.ComponentName, byte[]);
54835484
method public void wipeData(int);
54845485
field public static final java.lang.String ACTION_ADD_DEVICE_ADMIN = "android.app.action.ADD_DEVICE_ADMIN";

core/java/android/app/admin/DevicePolicyManager.java

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,7 +1708,7 @@ public boolean getStorageEncryption(ComponentName admin) {
17081708
* storage. If the result is {@link #ENCRYPTION_STATUS_ACTIVATING} or
17091709
* {@link #ENCRYPTION_STATUS_ACTIVE}, no further action is required.
17101710
*
1711-
* @return current status of encryption. The value will be one of
1711+
* @return current status of encryption. The value will be one of
17121712
* {@link #ENCRYPTION_STATUS_UNSUPPORTED}, {@link #ENCRYPTION_STATUS_INACTIVE},
17131713
* {@link #ENCRYPTION_STATUS_ACTIVATING}, or{@link #ENCRYPTION_STATUS_ACTIVE}.
17141714
*/
@@ -1729,15 +1729,18 @@ public int getStorageEncryptionStatus(int userHandle) {
17291729
}
17301730

17311731
/**
1732-
* Installs the given certificate as a User CA.
1732+
* Installs the given certificate as a user CA.
1733+
*
1734+
* @param admin Which {@link DeviceAdminReceiver} this request is associated with.
1735+
* @param certBuffer encoded form of the certificate to install.
17331736
*
17341737
* @return false if the certBuffer cannot be parsed or installation is
1735-
* interrupted, otherwise true
1738+
* interrupted, true otherwise.
17361739
*/
1737-
public boolean installCaCert(ComponentName who, byte[] certBuffer) {
1740+
public boolean installCaCert(ComponentName admin, byte[] certBuffer) {
17381741
if (mService != null) {
17391742
try {
1740-
return mService.installCaCert(who, certBuffer);
1743+
return mService.installCaCert(admin, certBuffer);
17411744
} catch (RemoteException e) {
17421745
Log.w(TAG, "Failed talking with device policy service", e);
17431746
}
@@ -1746,13 +1749,16 @@ public boolean installCaCert(ComponentName who, byte[] certBuffer) {
17461749
}
17471750

17481751
/**
1749-
* Uninstalls the given certificate from the list of User CAs, if present.
1752+
* Uninstalls the given certificate from trusted user CAs, if present.
1753+
*
1754+
* @param admin Which {@link DeviceAdminReceiver} this request is associated with.
1755+
* @param certBuffer encoded form of the certificate to remove.
17501756
*/
1751-
public void uninstallCaCert(ComponentName who, byte[] certBuffer) {
1757+
public void uninstallCaCert(ComponentName admin, byte[] certBuffer) {
17521758
if (mService != null) {
17531759
try {
17541760
final String alias = getCaCertAlias(certBuffer);
1755-
mService.uninstallCaCert(who, alias);
1761+
mService.uninstallCaCert(admin, alias);
17561762
} catch (CertificateException e) {
17571763
Log.w(TAG, "Unable to parse certificate", e);
17581764
} catch (RemoteException e) {
@@ -1762,16 +1768,47 @@ public void uninstallCaCert(ComponentName who, byte[] certBuffer) {
17621768
}
17631769

17641770
/**
1765-
* Returns whether there are any user-installed CA certificates.
1771+
* Returns all CA certificates that are currently trusted, excluding system CA certificates.
1772+
* If a user has installed any certificates by other means than device policy these will be
1773+
* included too.
1774+
*
1775+
* @return a List of byte[] arrays, each encoding one user CA certificate.
17661776
*/
1767-
public boolean hasAnyCaCertsInstalled() {
1768-
TrustedCertificateStore certStore = new TrustedCertificateStore();
1769-
Set<String> aliases = certStore.userAliases();
1770-
return aliases != null && !aliases.isEmpty();
1777+
public List<byte[]> getInstalledCaCerts() {
1778+
final TrustedCertificateStore certStore = new TrustedCertificateStore();
1779+
List<byte[]> certs = new ArrayList<byte[]>();
1780+
for (String alias : certStore.userAliases()) {
1781+
try {
1782+
certs.add(certStore.getCertificate(alias).getEncoded());
1783+
} catch (CertificateException ce) {
1784+
Log.w(TAG, "Could not encode certificate: " + alias, ce);
1785+
}
1786+
}
1787+
return certs;
17711788
}
17721789

17731790
/**
1774-
* Returns whether this certificate has been installed as a User CA.
1791+
* Uninstalls all custom trusted CA certificates from the profile. Certificates installed by
1792+
* means other than device policy will also be removed, except for system CA certificates.
1793+
*
1794+
* @param admin Which {@link DeviceAdminReceiver} this request is associated with.
1795+
*/
1796+
public void uninstallAllUserCaCerts(ComponentName admin) {
1797+
if (mService != null) {
1798+
for (String alias : new TrustedCertificateStore().userAliases()) {
1799+
try {
1800+
mService.uninstallCaCert(admin, alias);
1801+
} catch (RemoteException re) {
1802+
Log.w(TAG, "Failed talking with device policy service", re);
1803+
}
1804+
}
1805+
}
1806+
}
1807+
1808+
/**
1809+
* Returns whether this certificate is installed as a trusted CA.
1810+
*
1811+
* @param certBuffer encoded form of the certificate to look up.
17751812
*/
17761813
public boolean hasCaCertInstalled(byte[] certBuffer) {
17771814
try {

0 commit comments

Comments
 (0)