Java Code Examples for android.security.KeyChain#createInstallIntent()

The following examples show how to use android.security.KeyChain#createInstallIntent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CertificateInstallActivity.java    From NetBare with MIT License 6 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle bundle = getIntent().getExtras();
    if (bundle == null) {
        finish();
        return;
    }
    Intent intent = KeyChain.createInstallIntent();
    intent.putExtras(bundle);
    try {
        startActivityForResult(intent, REQUEST_CODE_INSTALL);
    } catch (ActivityNotFoundException e) {
        NetBareLog.e("Unable to start certificate installer.");
        finish();
    }
}
 
Example 2
Source File: AndroidNetworkLibrary.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Stores the key pair through the CertInstaller activity.
 * @param publicKey The public key bytes as DER-encoded SubjectPublicKeyInfo (X.509)
 * @param privateKey The private key as DER-encoded PrivateKeyInfo (PKCS#8).
 * @return: true on success, false on failure.
 *
 * Note that failure means that the function could not launch the CertInstaller
 * activity. Whether the keys are valid or properly installed will be indicated
 * by the CertInstaller UI itself.
 */
@CalledByNative
public static boolean storeKeyPair(byte[] publicKey, byte[] privateKey) {
    // TODO(digit): Use KeyChain official extra values to pass the public and private
    // keys when they're available. The "KEY" and "PKEY" hard-coded constants were taken
    // from the platform sources, since there are no official KeyChain.EXTRA_XXX definitions
    // for them. b/5859651
    try {
        Intent intent = KeyChain.createInstallIntent();
        intent.putExtra("PKEY", privateKey);
        intent.putExtra("KEY", publicKey);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ContextUtils.getApplicationContext().startActivity(intent);
        return true;
    } catch (ActivityNotFoundException e) {
        Log.w(TAG, "could not store key pair: " + e);
    }
    return false;
}
 
Example 3
Source File: AndroidNetworkLibrary.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Stores the key pair through the CertInstaller activity.
 * @param context: current application context.
 * @param public_key: The public key bytes as DER-encoded SubjectPublicKeyInfo (X.509)
 * @param private_key: The private key as DER-encoded PrivateKeyInfo (PKCS#8).
 * @return: true on success, false on failure.
 *
 * Note that failure means that the function could not launch the CertInstaller
 * activity. Whether the keys are valid or properly installed will be indicated
 * by the CertInstaller UI itself.
 */
@CalledByNative
static public boolean storeKeyPair(Context context, byte[] public_key, byte[] private_key) {
    // TODO(digit): Use KeyChain official extra values to pass the public and private
    // keys when they're available. The "KEY" and "PKEY" hard-coded constants were taken
    // from the platform sources, since there are no official KeyChain.EXTRA_XXX definitions
    // for them. b/5859651
    try {
        Intent intent = KeyChain.createInstallIntent();
        intent.putExtra("PKEY", private_key);
        intent.putExtra("KEY", public_key);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        return true;
    } catch (ActivityNotFoundException e) {
        Log.w(TAG, "could not store key pair: " + e);
    }
    return false;
}
 
Example 4
Source File: AndroidNetworkLibrary.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Stores the key pair through the CertInstaller activity.
 * @param context: current application context.
 * @param public_key: The public key bytes as DER-encoded SubjectPublicKeyInfo (X.509)
 * @param private_key: The private key as DER-encoded PrivateKeyInfo (PKCS#8).
 * @return: true on success, false on failure.
 *
 * Note that failure means that the function could not launch the CertInstaller
 * activity. Whether the keys are valid or properly installed will be indicated
 * by the CertInstaller UI itself.
 */
@CalledByNative
static public boolean storeKeyPair(Context context, byte[] public_key, byte[] private_key) {
    // TODO(digit): Use KeyChain official extra values to pass the public and private
    // keys when they're available. The "KEY" and "PKEY" hard-coded constants were taken
    // from the platform sources, since there are no official KeyChain.EXTRA_XXX definitions
    // for them. b/5859651
    try {
        Intent intent = KeyChain.createInstallIntent();
        intent.putExtra("PKEY", private_key);
        intent.putExtra("KEY", public_key);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        return true;
    } catch (ActivityNotFoundException e) {
        Log.w(TAG, "could not store key pair: " + e);
    }
    return false;
}
 
Example 5
Source File: MainActivity.java    From CapturePacket with MIT License 5 votes vote down vote up
@Override
public void onProxyStarted() {
    if (!mBinder.isProxyStarted()) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Snackbar.make(getWindow().getDecorView(),"端口被占用或其他异常,启动失败!",Snackbar.LENGTH_SHORT).show();
            }
        });
        return;
    }
    if (!SPUtil.getBoolean(this,SPUtil.KEY_IS_INSTALL_CER,false)) {
        byte[] cerBytes = mBinder.getCerBytes();
        if (cerBytes != null) {
            Intent intent = KeyChain.createInstallIntent();
            intent.putExtra(KeyChain.EXTRA_CERTIFICATE, cerBytes);
            intent.putExtra(KeyChain.EXTRA_NAME, "CapturePacket CA Certificate");
            startActivityForResult(intent, REQUEST_INSTALL_CER);
        }
    }
    boolean result = ProxyUtil.setProxy(this, CaptureService.PROXY_PORT);
    final String text = result ? "Set proxy host success !!!" : "Set proxy host failure ~~~";

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Snackbar.make(getWindow().getDecorView(),text,Snackbar.LENGTH_SHORT).show();
        }
    });

    if (result) {
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(CaptureListFragment.TAG);
        if (fragment instanceof CaptureListFragment) {
            ((CaptureListFragment) fragment).onProxyStarted(mBinder);
        }
    }
}
 
Example 6
Source File: AndroidNetworkLibrary.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
  * Adds a cryptographic file (User certificate, a CA certificate or
  * PKCS#12 keychain) through the system's CertInstaller activity.
  *
  * @param context: current application context.
  * @param cert_type: cryptographic file type. E.g. CertificateMimeType.X509_USER_CERT
  * @param data: certificate/keychain data bytes.
  * @return true on success, false on failure.
  *
  * Note that failure only indicates that the function couldn't launch the
  * CertInstaller activity, not that the certificate/keychain was properly
  * installed to the keystore.
  */
@CalledByNative
static public boolean storeCertificate(Context context, int cert_type, byte[] data) {
    try {
        Intent intent = KeyChain.createInstallIntent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        switch (cert_type) {
          case CertificateMimeType.X509_USER_CERT:
          case CertificateMimeType.X509_CA_CERT:
            intent.putExtra(KeyChain.EXTRA_CERTIFICATE, data);
            break;

          case CertificateMimeType.PKCS12_ARCHIVE:
            intent.putExtra(KeyChain.EXTRA_PKCS12, data);
            break;

          default:
            Log.w(TAG, "invalid certificate type: " + cert_type);
            return false;
        }
        context.startActivity(intent);
        return true;
    } catch (ActivityNotFoundException e) {
        Log.w(TAG, "could not store crypto file: " + e);
    }
    return false;
}
 
Example 7
Source File: AndroidNetworkLibrary.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
  * Adds a cryptographic file (User certificate, a CA certificate or
  * PKCS#12 keychain) through the system's CertInstaller activity.
  *
  * @param context: current application context.
  * @param cert_type: cryptographic file type. E.g. CertificateMimeType.X509_USER_CERT
  * @param data: certificate/keychain data bytes.
  * @return true on success, false on failure.
  *
  * Note that failure only indicates that the function couldn't launch the
  * CertInstaller activity, not that the certificate/keychain was properly
  * installed to the keystore.
  */
@CalledByNative
static public boolean storeCertificate(Context context, int cert_type, byte[] data) {
    try {
        Intent intent = KeyChain.createInstallIntent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        switch (cert_type) {
          case CertificateMimeType.X509_USER_CERT:
          case CertificateMimeType.X509_CA_CERT:
            intent.putExtra(KeyChain.EXTRA_CERTIFICATE, data);
            break;

          case CertificateMimeType.PKCS12_ARCHIVE:
            intent.putExtra(KeyChain.EXTRA_PKCS12, data);
            break;

          default:
            Log.w(TAG, "invalid certificate type: " + cert_type);
            return false;
        }
        context.startActivity(intent);
        return true;
    } catch (ActivityNotFoundException e) {
        Log.w(TAG, "could not store crypto file: " + e);
    }
    return false;
}