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 8436951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:18:46+00:00 2026-06-10T07:18:46+00:00

I’m trying to use Java CertPathBuilder to create CertPath from user certificate and CA

  • 0

I’m trying to use Java CertPathBuilder to create CertPath from user certificate and CA certificates, but I receive

Exception in thread "main" sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:174)
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:238)
    at CertPathBuilderTest.main(CertPathBuilderTest.java:63)

when I have enabled revocation checking (using .setRevocationEnabled(true)).
So it looks like Java is rejecting certificates or CRLs even though I can validate them using OpenSSL and gnupg without problem.

The CA structure is simplest possible: just CA signing certificates and CRLs, no intermediate CAs.

Problematic certificates: cacerts.jks and private.jks

Example program:

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CRL;
import java.security.cert.CRLException;
import java.security.cert.CertPath;
import java.security.cert.CertPathBuilder;
import java.security.cert.CertPathBuilderException;
import java.security.cert.CertPathBuilderResult;
import java.security.cert.CertStore;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.CollectionCertStoreParameters;
import java.security.cert.PKIXBuilderParameters;
import java.security.cert.X509CRL;
import java.security.cert.X509CertSelector;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

public class CertPathBuilderTest {

/**
 * @param args
 * @throws IOException 
 * @throws CertificateException 
 * @throws NoSuchAlgorithmException 
 * @throws KeyStoreException 
 * @throws CRLException 
 * @throws InvalidAlgorithmParameterException 
 * @throws UnexpectedJCAException 
 * @throws SigningCertChainException 
 * @throws CertPathBuilderException 
 */
public static void main(String[] args) 
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, CRLException, InvalidAlgorithmParameterException, CertPathBuilderException
{
    // TODO Auto-generated method stub
    // keytool -importcert -file CA.pem -keystore cacerts.jks -storepass changeit
    KeyStore trustAnchors = loadJKSKeyStore("cacerts.jks", "changeit");
    KeyStore myKeyStore = loadJKSKeyStore("private.jks", "changeit");

    String crlLocation = "http://crl.qbs.com.pl/QBSJanKuban.crl";
    X509CRL crl = downloadCRL(crlLocation);

    CertStore cs = otherCertificatesCertStore(trustAnchors, myKeyStore, crl);
    ///*
    CertPathBuilder cpb = CertPathBuilder.getInstance("PKIX");
    X509CertSelector certSelector = new X509CertSelector();
    certSelector.setCertificate((X509Certificate) myKeyStore.getCertificate("mykey"));
    PKIXBuilderParameters cpp = new PKIXBuilderParameters(trustAnchors, certSelector);
    cpp.addCertStore(cs);
    cpp.setRevocationEnabled(true);
    cpp.setMaxPathLength(6);
    cpp.setDate(new Date());

    CertPathBuilderResult a = cpb.build(cpp);
    CertPath certPath = a.getCertPath();
}

private static KeyStore loadJKSKeyStore(String path, String password)
    throws KeyStoreException,
    NoSuchAlgorithmException, CertificateException, IOException
{
    FileInputStream fis = new FileInputStream(path);
    KeyStore ks = KeyStore.getInstance("jks");
    ks.load(fis, password.toCharArray());
    fis.close();
    return ks;
}

private static X509CRL downloadCRL(String crlLocation)
        throws IOException, CertificateException, CRLException
{
    URL crlURL = new URL(crlLocation);
    BufferedInputStream in = new BufferedInputStream(crlURL.openStream());

    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    CRL crl = cf.generateCRL(in);

    return (X509CRL) crl;
}

private static CertStore otherCertificatesCertStore(KeyStore trustAnchors,
        KeyStore myCerts, X509CRL... crl)
        throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, KeyStoreException
{

    CertStore cs;

    Collection<Object> contentList = new ArrayList<Object>();
    contentList.add(trustAnchors.getCertificate("qbsca"));
    contentList.add(myCerts.getCertificate("mykey"));
    for (int i=0; i < crl.length; i++) {
        contentList.add(crl[i]);
    }
    cs = CertStore.getInstance("Collection", new CollectionCertStoreParameters(contentList));

    return cs;
}
}
  • 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-06-10T07:18:47+00:00Added an answer on June 10, 2026 at 7:18 am

    Problem was caused by a bug(?) in SUN and BC cryptographic providers.

    When the user certificate has specified not only CRL Distribution Point in form of URI, but also CRL Issuer then CertPathBuilder isn’t able to verify the validity of CRL and whole process fails.

    The workaround is to create certificates lacking this extension.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I need to clean up various Word 'smart' characters in user input, including but
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.