Programmatically add server certificate information to Trust Manager Android


Manohar perepa

I am new to this concept SSLand X509Certificate. What I need is, is there a way to get the certificate information from a given certificateUrl

For example: if the user entered https://www.google.com, I need to enter the information for that certificate programmatically.

edit:

Finally, I got the certificate information from Server .

Now, my question is:

1. How to check if a certificate is trusted?

2. How do I add a certificate to the trust manager?

3. Even with an untrusted certificate, if the user wants to keep using it, then I need to add the certificate to the trust manager. How can I do this?

4. Do we really need another certificate for comparison in order to check if the certificate is trusted?

I am very new to theseX.509 Certificate.

Any help would be greatly appreciated.

edit:

This is what I have tried. However, none of them are helping me. I need to get whether the certificate is trusted.

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) {

                    for (X509Certificate cert : chain) {

                        final String mCertificatinoType = cert.getType();
                        Date afterDate = cert.getNotAfter();
                        Date beforeDate = cert.getNotBefore();
                        Date currentDate = new Date();

                        try {
                            cert.checkValidity(new Date());
                        } catch (CertificateExpiredException e) {
                            LoginActivity.isExpired = true;
                            e.printStackTrace();
                        } catch (CertificateNotYetValidException e) {
                            LoginActivity.isInValid = true;
                            e.printStackTrace();
                        }

                        try {
                            cert.verify(trustedRoot.getPublicKey());
                        } catch (InvalidKeyException e) {
                            e.printStackTrace();
                        } catch (CertificateException e) {
                            e.printStackTrace();
                        } catch (NoSuchAlgorithmException e) {
                            e.printStackTrace();
                        } catch (NoSuchProviderException e) {
                            e.printStackTrace();
                        } catch (SignatureException e) {
                            e.printStackTrace();
                        }

                        try {
                            if (cert.getIssuerX500Principal().equals(
                                    trustedRoot.getIssuerX500Principal())) {

                            }
                            cert.verify(trustedHost.getPublicKey());
                        } catch (InvalidKeyException e) {
                            e.printStackTrace();
                        } catch (CertificateException e) {
                            e.printStackTrace();
                        } catch (NoSuchAlgorithmException e) {
                            e.printStackTrace();
                        } catch (NoSuchProviderException e) {
                            e.printStackTrace();
                        } catch (SignatureException e) {
                            e.printStackTrace();
                        }

                        if (afterDate.compareTo(currentDate)
                                * currentDate.compareTo(beforeDate) > 0) {
                        } else {

                        }

                        if (cert.getIssuerX500Principal().equals(
                                trustedRoot.getIssuerX500Principal())) {
                            return;
                        }
                    }

                    // for (X509Certificate cert : chain) {
                    // URL url;
                    // String host = "";
                    // if (baseHostString.equalsIgnoreCase("")) {
                    // final Settings settings = mApplication
                    // .getSettings();
                    // try {
                    // url = new URL(
                    // settings.serverAddress.toString());
                    // host = url.getAuthority();
                    // } catch (MalformedURLException e) {
                    // e.printStackTrace();
                    // }
                    // } else {
                    //
                    // }
                    //
                    // String dn = cert.getSubjectDN().getName();
                    // String CN = getValByAttributeTypeFromIssuerDN(dn,
                    // "CN=");
                    // if (CN.equalsIgnoreCase(host)) {
                    // if (cert.getIssuerX500Principal().equals(
                    // trustedRoot.getIssuerX500Principal())) {
                    // return;
                    // } else {
                    // }
                    // } else {
                    // }
                    // }
                    for (TrustManager tm : managers) {
                        if (tm instanceof X509TrustManager) {
                            try {
                                ((X509TrustManager) tm).checkServerTrusted(
                                        chain, authType);
                            } catch (CertificateException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                }

                @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()]);
                }

            };
Manohar perepa

Finally cracked!

                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(
                        final X509Certificate[] chain, String authType) {

                    for (X509Certificate cert : chain) {

                        final String mCertificatinoType = cert.getType();
                        Date afterDate = cert.getNotAfter();
                        Date beforeDate = cert.getNotBefore();
                        Date currentDate = new Date();

                        try {
                            cert.checkValidity(new Date());
                        } catch (CertificateExpiredException e) {
                            isExpired = true;
                            e.printStackTrace();
                        } catch (CertificateNotYetValidException e) {
                            isInValid = true;
                            e.printStackTrace();
                        }

                        if (afterDate.compareTo(currentDate)
                                * currentDate.compareTo(beforeDate) > 0) {
                            isExpired = false;
                        } else {
                            isExpired = true;
                        }

                        String dn = cert.getSubjectDN().getName();
                        String CN = getValByAttributeTypeFromIssuerDN(dn,
                                "CN=");

                        String host = "";
                        if (TextUtils.isEmpty(query)) {
                            if (baseHostString.equalsIgnoreCase("")) {
                                final Settings settings = mApplication
                                        .getSettings();
                                try {
                                    URL url = new URL(
                                            settings.serverAddress
                                                    .toString());
                                    host = url.getAuthority();
                                    if (host.contains(String.valueOf(url
                                            .getPort()))) {
                                        String toBeReplaced = ":"
                                                + url.getPort();
                                        host = host.replace(toBeReplaced,
                                                "");
                                    }
                                } catch (MalformedURLException e) {
                                    e.printStackTrace();
                                }
                            } else {
                                try {
                                    URL url = new URL(baseHostString);
                                    host = url.getAuthority();
                                    if (host.contains(String.valueOf(url
                                            .getPort()))) {
                                        String toBeReplaced = ":"
                                                + url.getPort();
                                        host = host.replace(toBeReplaced,
                                                "");
                                    }
                                } catch (MalformedURLException e) {
                                    e.printStackTrace();
                                }
                            }
                        } else {
                            try {
                                URL url = new URL(query);
                                host = url.getAuthority();
                                if (host.contains(String.valueOf(url
                                        .getPort()))) {
                                    String toBeReplaced = ":"
                                            + url.getPort();
                                    host = host.replace(toBeReplaced, "");
                                }
                            } catch (MalformedURLException e) {
                                e.printStackTrace();
                            }
                        }

                        if (CN.equalsIgnoreCase(host)) {
                            isHostMisMatch = false;
                        } else {
                            isHostMisMatch = true;
                        }

                        for (TrustManager tm : managers) {
                            if (tm instanceof X509TrustManager) {
                                try {
                                    ((X509TrustManager) tm)
                                            .checkServerTrusted(chain,
                                                    authType);
                                } catch (CertificateException e) {
                                    if (e.getMessage() != null
                                            && e.getMessage()
                                                    .contains(
                                                            "Trust anchor for certification path not found.")) {
                                        isNotTrusted = true;
                                        mApplication
                                                .setCurrentCertificate(chain);
                                    }
                                    e.printStackTrace();
                                }
                            }
                        }

                        if (cert.getIssuerX500Principal().equals(
                                trustedRoot.getIssuerX500Principal())) {
                            return;
                        }
                    }

                }

                @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()]);
                }

            };

thank you all.

Related


How to add certificate to Android trust store?

scotty 9811 I have a client application designed for Android. Android applications are based on XML and Java, as are our Windows server-side applications. The problem is that since we're using Android now, we have to deal with trust stores. Is there any way to

How to add certificate to Android trust store?

scotty 9811 I have a client application designed for Android. Android applications are based on XML and Java, as are our Windows server-side applications. The problem is that since we're using Android now, we have to deal with trust stores. Is there any way to

Programmatically trust Hoverfly Java certificate

Amr Lotfy: Using Hoverfly-java to mock a web service in a unit test, and using HttpClient as the web client, found that the Hoverfly proxy settings are propagated correctly using Hoverfly, useSystemProperties()but I still get the TLS error: Unknown certificate

Programmatically trust Hoverfly Java certificate

Amr Lotfy: Using Hoverfly-java to mock a web service in a unit test, and using HttpClient as the web client, found that the Hoverfly proxy settings are propagated correctly using Hoverfly, useSystemProperties()but I still get the TLS error: Unknown certificate

Programmatically trust Hoverfly Java certificate

Amr Lotfy: Using Hoverfly-java to mock a web service in a unit test, and using HttpClient as the web client, found that the Hoverfly proxy settings are propagated correctly using Hoverfly, useSystemProperties()but I still get the TLS error: Unknown certificate

Programmatically trust Hoverfly Java certificate

Amr Lotfy: Using Hoverfly-java to mock a web service in a unit test, and using HttpClient as the web client, found that the Hoverfly proxy settings are propagated correctly using Hoverfly, useSystemProperties()but I still get the TLS error: Unknown certificate

Programmatically trust Hoverfly Java certificate

Amr Lotfy: Using Hoverfly-java to mock a web service in a unit test, and using HttpClient as the web client, found that the Hoverfly proxy settings are propagated correctly using Hoverfly, useSystemProperties()but I still get the TLS error: Unknown certificate

lftp trust server certificate

MayeulC When connecting to the server using lftp, I get the following problem: Certificate verification: Not trusted: no issuer was found (AA:AA:AA:[...]:AA:AA) This at least indicates that the certificate verification failed. I want to whitelist that certifi

Programmatically get certificate information from Url Android

Manohar perepa Is it possible to get certificate information from it Url? In iOS, it has the contained NSURLAuthenticationChallengeinformation .urlhttps Do we have the same way to get information about a specific certificate urlthrough code ? Manohar perepa X5

Does Android trust the StartSSL certificate?

marc_aragones I am developing an android application and I want to encrypt the data sent to the server. I've seen a lot of people saying that the best practice is to use SSL, and I found that StartSSL is free. my question is: Will Android trust my StartSSL cer

android doesn't trust certificate

Yar I run openssl s_client -connect mywishboard.com:443 | openssl x509 -noout -subject -issuerand get the following information about the certificate (set by the client developer) depth=2 C = IL, O = StartCom Ltd., OU = Secure Digital Certificate Signing, CN =

android doesn't trust certificate

Yar I run openssl s_client -connect mywishboard.com:443 | openssl x509 -noout -subject -issuerand get the following information about the certificate (set by the client developer) depth=2 C = IL, O = StartCom Ltd., OU = Secure Digital Certificate Signing, CN =

Does Android trust the StartSSL certificate?

marc_aragones I am developing an android application and I want to encrypt the data sent to the server. I've seen a lot of people saying that the best practice is to use SSL, and I found that StartSSL is free. my question is: Will Android trust my StartSSL cer

Programmatically add certificate to personal store

Nick The project I'm working on includes an MVC website that communicates with a WCF web service, which is Windows authenticated. I have an identity delegation certificate that I want to add programmatically. To do this manually, open the Certificates snap-in

Programmatically add certificate to personal store

Nick The project I'm working on includes an MVC website that communicates with a WCF web service, which is Windows authenticated. I have an identity delegation certificate that I am trying to add programmatically. To do this manually, open the Certificates sna

Programmatically add certificate to personal store

Nick The project I'm working on includes an MVC website that communicates with a WCF web service, which is Windows authenticated. I have an identity delegation certificate that I am trying to add programmatically. To do this manually, open the Certificates sna

How can I make my server trust the certificate?

Leonardo Alves Machado: On a java application running on Tomcat on a RedHat linux machine, I need to send a request to an API using an ssl trusted certificate. Our partner has sent us two documents that should be used to trust them: wse-tst_partner_com.crtand

How can I make my server trust the certificate?

Leonardo Alves Machado: On a java application running on Tomcat on a RedHat linux machine, I need to send a request to an API using an ssl trusted certificate. Our partner has sent us two documents that should be used to trust them: wse-tst_partner_com.crtand