Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6121419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:46:55+00:00 2026-05-23T15:46:55+00:00

Lets say I have something like this (client side code): TrustManager[] trustAllCerts = new

  • 0

Lets say I have something like this (client side code):

TrustManager[] trustAllCerts = new TrustManager[]{
    new X509TrustManager() {

        @Override
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkClientTrusted(
                java.security.cert.X509Certificate[] certs, String authType) {
        }

        @Override
        public void checkServerTrusted(
                java.security.cert.X509Certificate[] certs, String authType) {
        }
    }
};

SSLContext sslc = SSLContext.getInstance("TLS");
sslc.init(null, trustAllCerts, null);

SocketFactory sf = sslc.getSocketFactory();
SSLSocket s = (SSLSocket) sf.createSocket("127.0.0.1", 9124);

This code is complete functional, but I really can not figure out, how to validate server’s certificate against one concrete CA certificate that I have available in pem file.

All certificates are signed by my self-signed CA, and it is the CA I need to validate against (only against this one).

Every answer is appreciated.

EDIT:

In response to jglouie (thank you very much this way – can not vote up your answer).

I founded the solution:

new X509TrustManager() {

        @Override
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkClientTrusted(
                java.security.cert.X509Certificate[] certs, String authType) {
        }

        @Override
        public void checkServerTrusted(
                java.security.cert.X509Certificate[] certs, String authType)
                throws CertificateException {
            InputStream inStream = null;
            try {
                // Loading the CA cert
                URL u = getClass().getResource("tcp/cacert.pem");
                inStream = new FileInputStream(u.getFile());
                CertificateFactory cf = CertificateFactory.getInstance("X.509");
                X509Certificate ca = (X509Certificate) cf.generateCertificate(inStream);
                inStream.close();

                for (X509Certificate cert : certs) {
                    // Verifing by public key
                    cert.verify(ca.getPublicKey());
                }
            } catch (Exception ex) {
                Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    inStream.close();
                } catch (IOException ex) {
                    Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        }
    }
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:46:55+00:00Added an answer on May 23, 2026 at 3:46 pm

    I assume that the self-signed certificate of your CA is already loaded as follows:

    CertificateFactory cf = CertificateFactory.getInstance("X.509");   
    FileInputStream finStream = new FileInputStream("CACertificate.pem"); 
    X509Certificate caCertificate = (X509Certificate)cf.generateCertificate(finStream);  
    

    Then in the method to check certificate:

    @Override        
     public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)  throws CertificateException {
    
     if (certs == null || certs.length == 0) {  
          throw new IllegalArgumentException("null or zero-length certificate chain");  
     }  
    
     if (authType == null || authType.length() == 0) {  
                throw new IllegalArgumentException("null or zero-length authentication type");  
      }  
    
       //Check if certificate send is your CA's
        if(!certs[0].equals(caCertificate)){
             try
             {   //Not your CA's. Check if it has been signed by your CA
                 certs[0].verify(caCertificate.getPublicKey())
             }
             catch(Exception e){   
                  throw new CertificateException("Certificate not trusted",e);
             }
        }
        //If we end here certificate is trusted. Check if it has expired.  
         try{
              certs[0].checkValidity();
          }
          catch(Exception e){
                throw new CertificateException("Certificate not trusted. It has expired",e);
          }  
    }
    

    Disclaimer: Have not even atempted to compile the code

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have something like this: public ActionResult Test(SomeModel m) { try {
Let's say I have something like this: $db=new PDO($dsn); $statement=$db->query('Select * from foo'); while
Let's say I have data structures that're something like this: Public Class AttendenceRecord Public
Let's say I have some code like this if(isset($_GET['foo'])) //do something if(isset($_GET['bar'])) //do something
Lets say I have something like this: <tr> <td><input type=text /></td> <td>Somevalue</td> <td><intput type=text
So let's say I have something like this: public class Service : IService {
Lets say i have something like this: This is file tree.py: class leaf(): def
Lets say I have something like this uid tag 1 HeLLo 2 heLLO 3
Let's say I have a MySQL table that is something like this: software table:
Let's say I have a class that implements the IDisposable interface. Something like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.