FileDocCategorySizeDatePackage
Credentials.javaAPI DocAndroid 5.1 API8534Thu Mar 12 22:22:30 GMT 2015android.security

Credentials

public class Credentials extends Object
{@hide}

Fields Summary
private static final String
LOGTAG
public static final String
INSTALL_ACTION
public static final String
INSTALL_AS_USER_ACTION
public static final String
UNLOCK_ACTION
public static final String
CA_CERTIFICATE
Key prefix for CA certificates.
public static final String
USER_CERTIFICATE
Key prefix for user certificates.
public static final String
USER_PRIVATE_KEY
Key prefix for user private keys.
public static final String
VPN
Key prefix for VPN.
public static final String
WIFI
Key prefix for WIFI.
public static final String
LOCKDOWN_VPN
Key containing suffix of lockdown VPN profile.
public static final String
EXTRA_PUBLIC_KEY
Data type for public keys.
public static final String
EXTRA_PRIVATE_KEY
Data type for private keys.
public static final String
EXTENSION_CRT
public static final String
EXTENSION_P12
public static final String
EXTENSION_CER
public static final String
EXTENSION_PFX
public static final String
EXTRA_INSTALL_AS_UID
Intent extra: install the certificate bundle as this UID instead of system.
public static final String
EXTRA_USER_PRIVATE_KEY_NAME
Intent extra: name for the user's private key.
public static final String
EXTRA_USER_PRIVATE_KEY_DATA
Intent extra: data for the user's private key in PEM-encoded PKCS#8.
public static final String
EXTRA_USER_CERTIFICATE_NAME
Intent extra: name for the user's certificate.
public static final String
EXTRA_USER_CERTIFICATE_DATA
Intent extra: data for the user's certificate in PEM-encoded X.509.
public static final String
EXTRA_CA_CERTIFICATES_NAME
Intent extra: name for CA certificate chain
public static final String
EXTRA_CA_CERTIFICATES_DATA
Intent extra: data for CA certificate chain in PEM-encoded X.509.
private static Credentials
singleton
Constructors Summary
Methods Summary
public static java.util.ListconvertFromPem(byte[] bytes)
Convert objects from PEM format, which is used for CA_CERTIFICATE and USER_CERTIFICATE entries.

        ByteArrayInputStream bai = new ByteArrayInputStream(bytes);
        Reader reader = new InputStreamReader(bai, StandardCharsets.US_ASCII);
        PemReader pr = new PemReader(reader);

        CertificateFactory cf = CertificateFactory.getInstance("X509");

        List<X509Certificate> result = new ArrayList<X509Certificate>();
        PemObject o;
        while ((o = pr.readPemObject()) != null) {
            if (o.getType().equals("CERTIFICATE")) {
                Certificate c = cf.generateCertificate(new ByteArrayInputStream(o.getContent()));
                result.add((X509Certificate) c);
            } else {
                throw new IllegalArgumentException("Unknown type " + o.getType());
            }
        }
        pr.close();
        return result;
    
public static byte[]convertToPem(java.security.cert.Certificate objects)
Convert objects to a PEM format which is used for CA_CERTIFICATE and USER_CERTIFICATE entries.


                       
        
               
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        Writer writer = new OutputStreamWriter(bao, StandardCharsets.US_ASCII);
        PemWriter pw = new PemWriter(writer);
        for (Certificate o : objects) {
            pw.writeObject(new PemObject("CERTIFICATE", o.getEncoded()));
        }
        pw.close();
        return bao.toByteArray();
    
static booleandeleteAllTypesForAlias(KeyStore keystore, java.lang.String alias)
Delete all types (private key, certificate, CA certificate) for a particular {@code alias}. All three can exist for any given alias. Returns {@code true} if there was at least one of those types.

        /*
         * Make sure every type is deleted. There can be all three types, so
         * don't use a conditional here.
         */
        return keystore.delKey(Credentials.USER_PRIVATE_KEY + alias)
                | deleteCertificateTypesForAlias(keystore, alias);
    
static booleandeleteCertificateTypesForAlias(KeyStore keystore, java.lang.String alias)
Delete all types (private key, certificate, CA certificate) for a particular {@code alias}. All three can exist for any given alias. Returns {@code true} if there was at least one of those types.

        /*
         * Make sure every certificate type is deleted. There can be two types,
         * so don't use a conditional here.
         */
        return keystore.delete(Credentials.USER_CERTIFICATE + alias)
                | keystore.delete(Credentials.CA_CERTIFICATE + alias);
    
public static android.security.CredentialsgetInstance()

        if (singleton == null) {
            singleton = new Credentials();
        }
        return singleton;
    
public voidinstall(android.content.Context context)

        try {
            Intent intent = KeyChain.createInstallIntent();
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Log.w(LOGTAG, e.toString());
        }
    
public voidinstall(android.content.Context context, java.security.KeyPair pair)

        try {
            Intent intent = KeyChain.createInstallIntent();
            intent.putExtra(EXTRA_PRIVATE_KEY, pair.getPrivate().getEncoded());
            intent.putExtra(EXTRA_PUBLIC_KEY, pair.getPublic().getEncoded());
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Log.w(LOGTAG, e.toString());
        }
    
public voidinstall(android.content.Context context, java.lang.String type, byte[] value)

        try {
            Intent intent = KeyChain.createInstallIntent();
            intent.putExtra(type, value);
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Log.w(LOGTAG, e.toString());
        }
    
public voidunlock(android.content.Context context)

        try {
            Intent intent = new Intent(UNLOCK_ACTION);
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Log.w(LOGTAG, e.toString());
        }