Ask Your Question
3

Retrieve the approved CA certificates from the server using Java.

asked 2023-05-27 16:52:00 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-05-27 17:02:02 +0000

huitzilopochtli gravatar image

There are several ways to retrieve the approved CA certificates from a server using Java. One of the common ways is to use the Java KeyStore API. Here is an example code snippet that demonstrates how to retrieve the approved CA certificates from a server using Java:

import java.io.InputStream;
import java.security.KeyStore;
import java.security.cert.Certificate;

public class RetrieveCACertificates {

  public static void main(String[] args) throws Exception {

    String keystoreFile = "path/to/keystore.jks";
    String keystorePassword = "keystorepassword";

    // Load the keystore from file
    InputStream keystoreStream = new FileInputStream(keystoreFile);
    KeyStore keystore = KeyStore.getInstance("JKS");
    keystore.load(keystoreStream, keystorePassword.toCharArray());

    // Retrieve the certificates from the keystore
    String alias = null;
    int i = 1;
    while ((alias = keystore.aliases().nextElement()) != null) {
      Certificate cert = keystore.getCertificate(alias);
      if (cert.getPublicKey().getAlgorithm().equals("RSA")) {
        System.out.println("Cert " + i++ + ": " + cert.toString());
      }
    }

  }

}

In this code snippet, we are loading the keystore file using the FileInputStream class and initializing a KeyStore object. Then, we are retrieving the certificate aliases from the keystore using the aliases() method and iterating through them to get the individual certificates using the getCertificate() method. Finally, we are checking if the certificate's public key algorithm is RSA and printing the certificate information to the console. Note that you may need to modify this code to suit your specific use case.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-27 16:52:00 +0000

Seen: 8 times

Last updated: May 27 '23