Example usage for android.security KeyChain getCertificateChain

List of usage examples for android.security KeyChain getCertificateChain

Introduction

In this page you can find the example usage for android.security KeyChain getCertificateChain.

Prototype

@Nullable
@WorkerThread
public static X509Certificate[] getCertificateChain(@NonNull Context context, @NonNull String alias)
        throws KeyChainException, InterruptedException 

Source Link

Document

Returns the X509Certificate chain for the requested alias, or null if there is no result.

Usage

From source file:org.strongswan.android.logic.CharonVpnService.java

/**
 * Function called via JNI to get a list containing the DER encoded certificates
 * of the user selected certificate chain (beginning with the user certificate).
 *
 * Since this method is called from a thread of charon's thread pool we are safe
 * to call methods on KeyChain directly.
 *
 * @return list containing the certificates (first element is the user certificate)
 * @throws InterruptedException/*  w  ww  .j  av  a 2 s.  co  m*/
 * @throws KeyChainException
 * @throws CertificateEncodingException
 */
private byte[][] getUserCertificate()
        throws KeyChainException, InterruptedException, CertificateEncodingException {
    ArrayList<byte[]> encodings = new ArrayList<byte[]>();
    X509Certificate[] chain = KeyChain.getCertificateChain(getApplicationContext(),
            mCurrentUserCertificateAlias);
    if (chain == null || chain.length == 0) {
        return null;
    }
    for (X509Certificate cert : chain) {
        encodings.add(cert.getEncoded());
    }
    return encodings.toArray(new byte[encodings.size()][]);
}