Example usage for android.security KeyChain EXTRA_PKCS12

List of usage examples for android.security KeyChain EXTRA_PKCS12

Introduction

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

Prototype

String EXTRA_PKCS12

To view the source code for android.security KeyChain EXTRA_PKCS12.

Click Source Link

Document

Optional extra for use with the Intent returned by #createInstallIntent to specify a PKCS#12 key store to install.

Usage

From source file:org.strongswan.android.ui.VpnProfileImportActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close_white_24dp);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mDataSource = new VpnProfileDataSource(this);
    mDataSource.open();//ww  w  .  j  a  v  a2 s. co m

    setContentView(R.layout.profile_import_view);

    mExistsWarning = (TextView) findViewById(R.id.exists_warning);
    mBasicDataGroup = (ViewGroup) findViewById(R.id.basic_data_group);
    mName = (TextView) findViewById(R.id.name);
    mGateway = (TextView) findViewById(R.id.gateway);
    mSelectVpnType = (TextView) findViewById(R.id.vpn_type);

    mUsernamePassword = (ViewGroup) findViewById(R.id.username_password_group);
    mUsername = (EditText) findViewById(R.id.username);
    mUsernameWrap = (TextInputLayoutHelper) findViewById(R.id.username_wrap);
    mPassword = (EditText) findViewById(R.id.password);

    mUserCertificate = (ViewGroup) findViewById(R.id.user_certificate_group);
    mSelectUserCert = (RelativeLayout) findViewById(R.id.select_user_certificate);
    mImportUserCert = (Button) findViewById(R.id.import_user_certificate);

    mRemoteCertificate = (ViewGroup) findViewById(R.id.remote_certificate_group);
    mRemoteCert = (RelativeLayout) findViewById(R.id.remote_certificate);

    mExistsWarning.setVisibility(View.GONE);
    mBasicDataGroup.setVisibility(View.GONE);
    mUsernamePassword.setVisibility(View.GONE);
    mUserCertificate.setVisibility(View.GONE);
    mRemoteCertificate.setVisibility(View.GONE);

    mSelectUserCert.setOnClickListener(new SelectUserCertOnClickListener());
    mImportUserCert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = KeyChain.createInstallIntent();
            intent.putExtra(KeyChain.EXTRA_NAME, getString(R.string.profile_cert_alias, mProfile.getName()));
            intent.putExtra(KeyChain.EXTRA_PKCS12, mProfile.PKCS12);
            startActivityForResult(intent, INSTALL_PKCS12);
        }
    });

    Intent intent = getIntent();
    String action = intent.getAction();
    if (Intent.ACTION_VIEW.equals(action)) {
        loadProfile(getIntent().getData());
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Intent openIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        openIntent.setType("*/*");
        startActivityForResult(openIntent, OPEN_DOCUMENT);
    }

    if (savedInstanceState != null) {
        mUserCertLoading = savedInstanceState.getString(VpnProfileDataSource.KEY_USER_CERTIFICATE);
        if (mUserCertLoading != null) {
            getLoaderManager().initLoader(USER_CERT_LOADER, null, mUserCertificateLoaderCallbacks);
        }
        mImportUserCert.setEnabled(!savedInstanceState.getBoolean(PKCS12_INSTALLED));
    }
}

From source file:nl.nikhef.eduroam.WiFiEduroam.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
// Step 3 for android 4.0 - 4.2
private void installClientCertificate() {
    try {/*from  w  w w. j  a  v  a 2 s . co m*/
        updateStatus("Inputting client certificate.");

        // Parse the certificate that we got from the server
        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
        InputStream in = new ByteArrayInputStream(
                Base64.decode(certificate.replaceAll("-----(BEGIN|END) CERTIFICATE-----", "")));
        X509Certificate cert = (X509Certificate) certFactory.generateCertificate(in);

        client_cert_name = ssid + " " + INT_CLIENT_CERT_NAME;

        // Create a pkcs12 certificate/private key combination
        Security.addProvider(new BouncyCastleProvider());
        KeyStore keystore = KeyStore.getInstance("PKCS12", "BC");
        keystore.load(null, null);
        Certificate chain[] = new Certificate[] { (Certificate) cert };
        keystore.setKeyEntry(client_cert_name, csr.getPrivate(), null, chain);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        keystore.store(out, ssid.toCharArray());
        out.flush();
        byte[] buffer = out.toByteArray();
        out.close();

        // Install the private key/client certificate combination
        Intent intent = KeyChain.createInstallIntent();
        intent.putExtra(KeyChain.EXTRA_NAME, ssid + " " + INT_CLIENT_CERT_NAME);
        intent.putExtra(KeyChain.EXTRA_PKCS12, buffer);
        startActivityForResult(intent, 3);
    } catch (CertificateException e) {
        e.printStackTrace();
        throw new RuntimeException("Certificate error.");
    } catch (KeyStoreException e) {
        e.printStackTrace();
        System.out.println(e.getMessage());
        throw new RuntimeException("Certificate error: KeyStore");
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
        throw new RuntimeException("Certificate error: Provider");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        throw new RuntimeException("Certificate error: Algorithm");
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Certificate error: IO");
    }
}