@Override public void run() { try { byte[] keychainBytes; FileInputStream is = null; try { is = new FileInputStream(CERTIFICATE_RESOURCE); keychainBytes = new byte[is.available()]; is.read(keychainBytes); } finally { IOUtils.closeQuietly(is); } Intent intent = KeyChain.createInstallIntent(); intent.putExtra(KeyChain.EXTRA_CERTIFICATE, keychainBytes); intent.putExtra(KeyChain.EXTRA_NAME, "NetworkDiagnosis CA Certificate"); startActivityForResult(intent, 3); } catch (Exception e) { e.printStackTrace(); } } };
private X509Certificate[] fetchCertificateChain(Context context, String alias) throws KeyChainException, InterruptedException, MessagingException { X509Certificate[] chain = KeyChain.getCertificateChain(context, alias); if (chain == null || chain.length == 0) { throw new MessagingException("No certificate chain found for: " + alias); } try { for (X509Certificate certificate : chain) { certificate.checkValidity(); } } catch (CertificateException e) { throw new CertificateValidationException(e.getMessage(), Reason.Expired, alias); } return chain; }
private PrivateKey fetchPrivateKey(Context context, String alias) throws KeyChainException, InterruptedException, MessagingException { PrivateKey privateKey = KeyChain.getPrivateKey(context, alias); if (privateKey == null) { throw new MessagingException("No private key found for: " + alias); } return privateKey; }
mPrivateKey = KeyChain.getPrivateKey(context, mAlias); String keystoreChain = null; X509Certificate[] caChain = KeyChain.getCertificateChain(context, mAlias); if (caChain == null) throw new NoCertReturnedException("No certificate returned from Keystore"); if (caChain.length <= 1 && TextUtils.isEmpty(mCaFilename)) {
public void chooseCertificate() { // NOTE: keyTypes, issuers, hosts, port are not known before we actually // open a connection, thus we cannot set them here! KeyChain.choosePrivateKeyAlias(mActivity, new KeyChainAliasCallback() { @Override public void alias(String alias) { Timber.d("User has selected client certificate alias: %s", alias); setAlias(alias); } }, null, null, null, -1, getAlias()); }
/** * Calls KeyChain#choosePrivateKeyAlias with the provided arguments. */ public void choosePrivateKeyAlias() throws ActivityNotFoundException { KeyChain.choosePrivateKeyAlias(mActivity, mCallback, mKeyTypes, mPrincipalsForCallback, mHostName, mPort, mAlias); } }
@Override public void run() { try { byte[] keychainBytes; FileInputStream is = null; try { is = new FileInputStream(CERTIFICATE_RESOURCE); keychainBytes = new byte[is.available()]; is.read(keychainBytes); } finally { IOUtils.closeQuietly(is); } Intent intent = KeyChain.createInstallIntent(); intent.putExtra(KeyChain.EXTRA_CERTIFICATE, keychainBytes); intent.putExtra(KeyChain.EXTRA_NAME, "NetworkDiagnosis CA Certificate"); startActivityForResult(intent, 3); } catch (Exception e) { e.printStackTrace(); } } };
private PrivateKey getPrivateKey(String alias) { try { return KeyChain.getPrivateKey(mContext, alias); } catch (KeyChainException e) { Log.w(TAG, "KeyChainException when looking for '" + alias + "' certificate"); return null; } catch (InterruptedException e) { Log.w(TAG, "InterruptedException when looking for '" + alias + "'certificate"); return null; } }
private X509Certificate[] getCertificateChain(String alias) { try { return KeyChain.getCertificateChain(mContext, alias); } catch (KeyChainException e) { Log.w(TAG, "KeyChainException when looking for '" + alias + "' certificate"); return null; } catch (InterruptedException e) { Log.w(TAG, "InterruptedException when looking for '" + alias + "'certificate"); return null; } } }
@Override public void onClick(View v) { try { byte[] keychainBytes; InputStream bis = CaptureActivity.class.getResourceAsStream(CA_RESOURCE); keychainBytes = new byte[bis.available()]; bis.read(keychainBytes); Intent intent = KeyChain.createInstallIntent(); intent.putExtra(KeyChain.EXTRA_CERTIFICATE, keychainBytes); intent.putExtra(KeyChain.EXTRA_NAME, "DreamCatcher CA Certificate"); startActivityForResult(intent, INSTALL_CA_REQUEST_CODE); } catch (Exception e) { e.printStackTrace(); } } });
@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); startActivityForResult(intent, REQUEST_CODE_INSTALL); }
private void installPkcs12() { try { // debug - filenames in assets directory String[] f = getAssets().list(""); for(String f1 : f){ Log.v("names",f1); } BufferedInputStream bis = new BufferedInputStream(getAssets().open(PKCS12_FILENAME)); byte[] keychain = new byte[bis.available()]; bis.read(keychain); Intent installIntent = KeyChain.createInstallIntent(); installIntent.putExtra(KeyChain.EXTRA_PKCS12, keychain); startActivity(installIntent); // installIntent.putExtra(KeyChain.EXTRA_NAME, DEFAULT_ALIAS); // startActivityForResult(installIntent, INSTALL_KEYCHAIN_CODE); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }