Search in sources :

Example 91 with CertificateException

use of java.security.cert.CertificateException in project carat by amplab.

the class SamplingLibrary method getSignatures.

public static List<String> getSignatures(PackageInfo pak) {
    List<String> sigList = new LinkedList<String>();
    String[] pmInfos = pak.requestedPermissions;
    if (pmInfos != null) {
        byte[] bytes = getPermissionBytes(pmInfos);
        String hexB = convertToHex(bytes);
        sigList.add(hexB);
    }
    Signature[] sigs = pak.signatures;
    for (Signature s : sigs) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA-1");
            md.update(s.toByteArray());
            byte[] dig = md.digest();
            // Add SHA-1
            sigList.add(convertToHex(dig));
            CertificateFactory fac = CertificateFactory.getInstance("X.509");
            if (fac == null)
                continue;
            X509Certificate cert = (X509Certificate) fac.generateCertificate(new ByteArrayInputStream(s.toByteArray()));
            if (cert == null)
                continue;
            PublicKey pkPublic = cert.getPublicKey();
            if (pkPublic == null)
                continue;
            String al = pkPublic.getAlgorithm();
            if (al.equals("RSA")) {
                md = MessageDigest.getInstance("SHA-256");
                RSAPublicKey rsa = (RSAPublicKey) pkPublic;
                byte[] data = rsa.getModulus().toByteArray();
                if (data[0] == 0) {
                    byte[] copy = new byte[data.length - 1];
                    System.arraycopy(data, 1, copy, 0, data.length - 1);
                    md.update(copy);
                } else
                    md.update(data);
                dig = md.digest();
                // Add SHA-256 of modulus
                sigList.add(convertToHex(dig));
            } else if (al.equals("DSA")) {
                DSAPublicKey dsa = (DSAPublicKey) pkPublic;
                md = MessageDigest.getInstance("SHA-256");
                byte[] data = dsa.getY().toByteArray();
                if (data[0] == 0) {
                    byte[] copy = new byte[data.length - 1];
                    System.arraycopy(data, 1, copy, 0, data.length - 1);
                    md.update(copy);
                } else
                    md.update(data);
                dig = md.digest();
                // Add SHA-256 of public key (DSA)
                sigList.add(convertToHex(dig));
            } else {
                Log.e("SamplingLibrary", "Weird algorithm: " + al + " for " + pak.packageName);
            }
        } catch (NoSuchAlgorithmException e) {
        // Do nothing
        } catch (CertificateException e) {
        // Do nothing
        }
    }
    return sigList;
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateFactory(java.security.cert.CertificateFactory) LinkedList(java.util.LinkedList) X509Certificate(java.security.cert.X509Certificate) DSAPublicKey(java.security.interfaces.DSAPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) ByteArrayInputStream(java.io.ByteArrayInputStream) Signature(android.content.pm.Signature) MessageDigest(java.security.MessageDigest)

Example 92 with CertificateException

use of java.security.cert.CertificateException in project Lazy by l123456789jy.

the class AppUtils method isDebuggable.

/**
     * 检测当前应用是否是Debug版本
     *
     * @param ctx 上下文
     * @return 是否是Debug版本
     */
public static boolean isDebuggable(Context ctx) {
    boolean debuggable = false;
    try {
        PackageInfo pinfo = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), PackageManager.GET_SIGNATURES);
        Signature[] signatures = pinfo.signatures;
        for (int i = 0; i < signatures.length; i++) {
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            ByteArrayInputStream stream = new ByteArrayInputStream(signatures[i].toByteArray());
            X509Certificate cert = (X509Certificate) cf.generateCertificate(stream);
            debuggable = cert.getSubjectX500Principal().equals(DEBUG_DN);
            if (debuggable)
                break;
        }
    } catch (NameNotFoundException e) {
    } catch (CertificateException e) {
    }
    return debuggable;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) PackageInfo(android.content.pm.PackageInfo) Signature(android.content.pm.Signature) CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate)

Example 93 with CertificateException

use of java.security.cert.CertificateException in project android-async-http by loopj.

the class CustomCASample method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        InputStream is = null;
        try {
            // Configure the library to use a custom 'bks' file to perform
            // SSL negotiation.
            KeyStore store = KeyStore.getInstance(KeyStore.getDefaultType());
            is = getResources().openRawResource(R.raw.store);
            store.load(is, STORE_PASS.toCharArray());
            getAsyncHttpClient().setSSLSocketFactory(new SecureSocketFactory(store, STORE_ALIAS));
        } catch (IOException e) {
            throw new KeyStoreException(e);
        } catch (CertificateException e) {
            throw new KeyStoreException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new KeyStoreException(e);
        } catch (KeyManagementException e) {
            throw new KeyStoreException(e);
        } catch (UnrecoverableKeyException e) {
            throw new KeyStoreException(e);
        } finally {
            AsyncHttpClient.silentCloseInputStream(is);
        }
    } catch (KeyStoreException e) {
        Log.e(LOG_TAG, "Unable to initialize key store", e);
        showCustomCAHelp();
    }
}
Also used : UnrecoverableKeyException(java.security.UnrecoverableKeyException) InputStream(java.io.InputStream) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStore(java.security.KeyStore) SecureSocketFactory(com.loopj.android.http.sample.util.SecureSocketFactory) KeyManagementException(java.security.KeyManagementException)

Example 94 with CertificateException

use of java.security.cert.CertificateException in project keywhiz by square.

the class ExpirationExtractor method expirationFromRawCertificate.

@Nullable
public static Instant expirationFromRawCertificate(byte[] content) {
    CertificateFactory cf;
    try {
        cf = CertificateFactory.getInstance("X.509");
    } catch (CertificateException e) {
        // Should never happen (X.509 supported by default)
        throw Throwables.propagate(e);
    }
    X509Certificate cert;
    try {
        cert = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(content));
    } catch (CertificateException e) {
        // Certificate must have been invalid
        logger.info("Failed to parse certificate", e);
        return null;
    }
    return cert.getNotAfter().toInstant();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) Nullable(javax.annotation.Nullable)

Example 95 with CertificateException

use of java.security.cert.CertificateException in project keywhiz by square.

the class ExpirationExtractor method expirationFromKeystore.

@Nullable
public static Instant expirationFromKeystore(String type, String password, byte[] content) {
    KeyStore ks;
    try {
        ks = KeyStore.getInstance(type);
    } catch (KeyStoreException e) {
        // Should never occur (assuming JCE is installed)
        throw Throwables.propagate(e);
    }
    try {
        ks.load(new ByteArrayInputStream(content), password.toCharArray());
    } catch (IOException | NoSuchAlgorithmException | CertificateException e) {
        // Failed to parse
        logger.info("Failed to parse keystore", e);
        return null;
    }
    Instant earliest = null;
    try {
        for (String alias : list(ks.aliases())) {
            Certificate[] chain = ks.getCertificateChain(alias);
            if (chain == null) {
                Certificate certificate = ks.getCertificate(alias);
                if (certificate == null) {
                    // No certs in this entry
                    continue;
                }
                chain = new Certificate[] { certificate };
            }
            for (Certificate cert : chain) {
                if (cert instanceof X509Certificate) {
                    X509Certificate c = (X509Certificate) cert;
                    if (earliest == null || c.getNotAfter().toInstant().isBefore(earliest)) {
                        earliest = c.getNotAfter().toInstant();
                    }
                }
            }
        }
    } catch (KeyStoreException e) {
        // Should never occur (ks was initialized)
        throw Throwables.propagate(e);
    }
    return earliest;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Instant(java.time.Instant) CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) Nullable(javax.annotation.Nullable)

Aggregations

CertificateException (java.security.cert.CertificateException)456 IOException (java.io.IOException)221 X509Certificate (java.security.cert.X509Certificate)215 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)141 KeyStoreException (java.security.KeyStoreException)123 CertificateFactory (java.security.cert.CertificateFactory)103 ByteArrayInputStream (java.io.ByteArrayInputStream)97 Certificate (java.security.cert.Certificate)75 KeyStore (java.security.KeyStore)58 InputStream (java.io.InputStream)55 UnrecoverableKeyException (java.security.UnrecoverableKeyException)53 ArrayList (java.util.ArrayList)49 InvalidKeyException (java.security.InvalidKeyException)44 X509TrustManager (javax.net.ssl.X509TrustManager)41 SSLContext (javax.net.ssl.SSLContext)36 FileInputStream (java.io.FileInputStream)34 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)34 RemoteException (android.os.RemoteException)33 FileNotFoundException (java.io.FileNotFoundException)30 KeyManagementException (java.security.KeyManagementException)30