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

  • SEARCH
  • Home
  • 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 6167591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:32:24+00:00 2026-05-23T22:32:24+00:00

I am connecting an SSL client to my SSL server. When the client fails

  • 0

I am connecting an SSL client to my SSL server.
When the client fails to verify a certificate due to the root not existing in the client’s key store, I need the option to add that certificate to the local key store in code and continue.
There are examples for always accepting all certificates, but I want the user to verify the cert and add it to local key store without leaving the application.

SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket("localhost", 23467);
try{
    sslsocket.startHandshake();
} catch (IOException e) {
    //here I want to get the peer's certificate, conditionally add to local key store, then reauthenticate successfully
}

There is a whole lot of stuff about custom SocketFactory, TrustManager, SSLContext, etc and I don’t really understand how they all fit together or which would be the shortest path to my goal.

  • 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-23T22:32:25+00:00Added an answer on May 23, 2026 at 10:32 pm

    You could implement this using a X509TrustManager.

    Obtain an SSLContext with

    SSLContext ctx = SSLContext.getInstance("TLS");
    

    Then initialize it with your custom X509TrustManager by using SSLContext#init. The SecureRandom and the KeyManager[] may be null. The latter is only useful if you perform client authentication, if in your scenario only the server needs to authenticate you don’t need to set it.

    From this SSLContext, get your SSLSocketFactory using SSLContext#getSocketFactory and proceed as planned.

    As concerns your X509TrustManager implementation, it could look like this:

    TrustManager tm = new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] chain,
                        String authType)
                        throws CertificateException {
            //do nothing, you're the client
        }
    
        public X509Certificate[] getAcceptedIssuers() {
            //also only relevant for servers
        }
    
        public void checkServerTrusted(X509Certificate[] chain,
                        String authType)
                        throws CertificateException {
            /* chain[chain.length -1] is the candidate for the
             * root certificate. 
             * Look it up to see whether it's in your list.
             * If not, ask the user for permission to add it.
             * If not granted, reject.
             * Validate the chain using CertPathValidator and 
             * your list of trusted roots.
             */
        }
    };
    

    Edit:

    Ryan was right, I forgot to explain how to add the new root to the existing ones. Let’s assume your current KeyStore of trusted roots was derived from cacerts (the ‘Java default trust store’ that comes with your JDK, located under jre/lib/security). I assume you loaded that key store (it’s in JKS format) with KeyStore#load(InputStream, char[]).

    KeyStore ks = KeyStore.getInstance("JKS");
    FileInputStream in = new FileInputStream("<path to cacerts"");
    ks.load(in, "changeit".toCharArray);
    

    The default password to cacerts is “changeit” if you haven’t, well, changed it.

    Then you may add addtional trusted roots using KeyStore#setEntry. You can omit the ProtectionParameter (i.e. null), the KeyStore.Entry would be a TrustedCertificateEntry that takes the new root as parameter to its constructor.

    KeyStore.Entry newEntry = new KeyStore.TrustedCertificateEntry(newRoot);
    ks.setEntry("someAlias", newEntry, null);
    

    If you’d like to persist the altered trust store at some point, you may achieve this with KeyStore#store(OutputStream, char[].

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

Sidebar

Related Questions

I have a client program that talks to a web server through a SSL
I need to send an object file from my client to the server program.
I'm making a secure SSL connection to a server using python and M2Crypto. See
Will IE6 negotiate a 256 bit AES SSL connection if the server is capable?
I am implementing a push notification in my application. Server: php Client: iphone Sever
I am using .net client to post mesages to MQ server which is hosted
I am having trouble connecting Redmine to a locally hosted subversion repository using SSL.
My project invoves a user connecting from client to web service, and then web
I need to consume a Web Service via SSL. In order to accomplish that
I have successfully setup my MySQL server to support SSL connections I'm just wondering

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.