Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Certificate Information from Url Android Programmatically

Is it possible to get the certificate information from the Url ? In iOS, it has NSURLAuthenticationChallengewhich gives the information if the url contains https.

The same way do we have any way to get the certificate information for the particular url through Code?

like image 955
Manu Avatar asked Aug 05 '26 12:08

Manu


1 Answers

X509TrustManager trustManager = new X509TrustManager() {
                @Override
                public void checkClientTrusted(X509Certificate[] chain,
                        String authType) throws CertificateException {
                    for (TrustManager tm : managers) {
                        if (tm instanceof X509TrustManager) {
                            ((X509TrustManager) tm).checkClientTrusted(
                                    chain, authType);
                        }
                    }
                }

                @Override
                public void checkServerTrusted(X509Certificate[] chain,
                        String authType) throws CertificateException {
                    for (X509Certificate cert : chain) {
                        // cert gives the server Certificate Information.
                        if (cert.getIssuerX500Principal().equals(
                                trustedRoot.getIssuerX500Principal())) {
                            return;
                        }
                    }
                    for (TrustManager tm : managers) {
                        if (tm instanceof X509TrustManager) {
                            ((X509TrustManager) tm).checkServerTrusted(
                                    chain, authType);
                        }
                    }
                }

                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    ArrayList<X509Certificate> issuers = new ArrayList<>();
                    for (TrustManager tm : managers) {
                        if (tm instanceof X509TrustManager) {
                            issuers.addAll(Arrays
                                    .asList(((X509TrustManager) tm)
                                            .getAcceptedIssuers()));
                        }
                    }
                    return issuers.toArray(new X509Certificate[issuers
                            .size()]);
                }

            };

Check for this // cert gives the server Certificate Information. in the above code.

like image 173
Manu Avatar answered Aug 08 '26 01:08

Manu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!