Get certificate information from Url Android programmatically

Can I get certificate information from Url

? On iOS, it has NSURLAuthenticationChallenge

, which gives information if it Url

contains https

.

Similarly, can we somehow get the certificate information for a specific Url

one through code?

+3


source to share


2 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 this one // cert gives the server Certificate Information.

in the above code.

+2


source


Yes, it is possible:

Step1: enter url in broswer, click on tab 2 as below:

enter image description here

Step 2: open the "Details" tab



Step 3. Export the public key.

enter image description here

Step 4: get the .cert file.

Step 5: generate keystore from .cert file and put it in android

0


source