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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:17:19+00:00 2026-05-15T17:17:19+00:00

Need some help with crypto routines in Java. Given a PKCS#7 signature, I want

  • 0

Need some help with crypto routines in Java.

Given a PKCS#7 signature, I want to verify all certificates it contains against a trusted store. I assume that all certificates contained in signature are in the correct order to form a valid certificate path (or chain, whatever), so that

  • topmost (#0) is a signing certificate;
  • next one (#1) is an intermediate certificate, used to sign #0;
  • next one (#2) is another intermediate certificate, used to sign #1;
  • and so on.

The last certificate (#N) is signed by CA.

That’s what I’ve managed to hack so far:

// Exception handling skipped for readability

//byte[] signature = ...
pkcs7 = new PKCS7(signature); // `sun.security.pkcs.PKCS7;`

// *** Checking some PKCS#7 parameters here

X509Certificate prevCert = null; // Previous certificate we've found
X509Certificate[] certs = pkcs7.getCertificates(); // `java.security.cert.X509Certificate`
for (int i = 0; i < certs.length; i++) {
    // *** Checking certificate validity period here

    if (cert != null) {
        // Verify previous certificate in chain against this one
        prevCert.verify(certs[i].getPublicKey());
    }
    prevCert = certs[i];
}

//String keyStorePath = ...
KeyStore keyStore = KeyStore.getInstance("JKS"); // `java.security.KeyStore`
keyStore.load(new FileInputStream(keyStorePath), null);

// Get trusted VeriSign class 1 certificate
Certificate caCert = keyStore.getCertificate("verisignclass1ca"); // `java.security.cert.Certificate`

// Verify last certificate against trusted certificate
cert.verify(caCert.getPublicKey());

So the question is — how can this be done using standard Java classes like CertPath and friends? I have a strong feeling I’m re-inventing a bicycle. Or, if someone has an example with BouncyCastle library, that would also be fine.

Bonus question: how to verify a certificate against a trusted store so that root certificate is selected automatically?

  • 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-15T17:17:20+00:00Added an answer on May 15, 2026 at 5:17 pm

    Found the solution myself. So, here’s how one can extract and validate a certificate chain against the trusted store (exception handling skipped for readability):

    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    
    // Get ContentInfo
    //byte[] signature = ... // PKCS#7 signature bytes
    InputStream signatureIn = new ByteArrayInputStream(signature);
    DERObject obj = new ASN1InputStream(signatureIn).readObject();
    ContentInfo contentInfo = ContentInfo.getInstance(obj);
    
    // Extract certificates
    SignedData signedData = SignedData.getInstance(contentInfo.getContent());
    Enumeration certificates = signedData.getCertificates().getObjects();
    
    // Build certificate path
    List certList = new ArrayList();
    while (certificates.hasMoreElements()) {
        DERObject certObj = (DERObject) certificates.nextElement();
        InputStream in = new ByteArrayInputStream(certObj.getDEREncoded());
        certList.add(cf.generateCertificate(in));
    }
    CertPath certPath = cf.generateCertPath(certList);
    
    // Load key store
    //String keyStorePath = ...
    KeyStore keyStore = KeyStore.getInstance("JKS");
    keyStore.load(new FileInputStream(keyStorePath), null);
    
    // Set validation parameters
    PKIXParameters params = new PKIXParameters(keyStore);
    params.setRevocationEnabled(false); // to avoid exception on empty CRL
    
    // Validate certificate path
    CertPathValidator validator = CertPathValidator.getInstance("PKIX");
    CertPathValidatorResult result = validator.validate(certPath, params);
    

    validate() will throw an exception if validation fails.

    Docs: ASN1Set, ContentInfo, SignedData. All other exotic names and related docs can be found in java.security.cert.

    No SUN-dependencies here, only BouncyCastle provider library is needed.

    This question (and especially an answer) may help too.

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

Sidebar

Related Questions

I am new to all the anonymous features and need some help. I have
I need some help from the shell-script gurus out there. I have a .txt
I need some help regarding algorithm for randomness. So Problem is. There are 50
I need some help calculating Pi. I am trying to write a python program
I need some help with jQuery script again :-) Just trying to play with
I need some help ... I'm a bit (read total) n00b when it comes
Hi I need some help with the following scenario in php. I have a
I am getting a little confused and need some help please. Take these two
I'm working with jQuery for the first time and need some help. I have
I've gone through most of the example code and I still need some help.

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.