0

I would be obliged if someone could provide me with sample code in groovy or java that will clearly demonstrate how I can use a pks12 certificate file to make a https call to a given url and how to get and interpret the response code given. Thanks in advance for any contributions

For purpose of this question lets assume that the pfx file is called TEST.pfx and the pwd is Alpha123

So far I have assembled this code. The 1st part I use to load the certificate and look at its contents. In the 2nd part I want to use the loaded certificate to access the url provided, but nothing happens.

package myGroovyProject


import java.security.KeyStore
import java.security.Principal
import java.security.cert.X509Certificate
import javax.net.ssl.HttpsURLConnection
import javax.net.ssl.SSLContext
import javax.net.ssl.TrustManagerFactory
import java.io.FileInputStream;
import java.security.Key;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
    //1st part
    //========
    KeyStore myKeyStore = KeyStore.getInstance("pkcs12");
    myKeyStore.load(new FileInputStream("TEST.pfx"), "Alpha123".toCharArray());
    Enumeration<String> e = myKeyStore.aliases();
    while (e.hasMoreElements()) 
    {
        String alias = e.nextElement();
        X509Certificate c = (X509Certificate) myKeyStore.getCertificate(alias);
        Principal subject = c.getSubjectDN();
        System.out.println(c);
                  
        String[] subjectArray  = subject.toString().split(",");
        
        for (String s : subjectArray) 
        {
            String[] str = s.trim().split("=");
            String key = str[0];
            String value = str[1];
            System.out.println("Details Are : " + key + " - " + value);
        }
    }
    
    //2nd part
    //========
    def TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(myKeyStore);
    
    def SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
    
     def HttpsURLConnection connection = (HttpsURLConnection) new URL("https://certauth.idrix.fr").openConnection();
    connection.setSSLSocketFactory(sslContext.getSocketFactory());
    
    
    System.out.println("==== Finished ====")
Alias Cartellano
  • 366
  • 1
  • 3
  • 12
Bobby
  • 1
  • 1
  • Please, consider review this [SO question](https://stackoverflow.com/questions/65776006/ssl-socket-connection-with-client-authentication/65837262#65837262), with minimal changes you should be able to achieve the desired behavior. – jccampanero Sep 18 '21 at 22:14
  • `nothing happens` ? nothing happens after what? are you trying to open stream ? (i don't see it in the code) – daggett Sep 20 '21 at 03:12
  • What are the minmal changes that you are referring too? Please note that I am not a java programmer, rather I have compiled my code block by browsing via google and copy and paste what I believe is relevant code. Also what part of SQ Question should I be looking at. I guess I am looking for an idiots cookbook that clearly documents the steps that need doing. Thanks for your help and understanding – Bobby Sep 22 '21 at 09:14

0 Answers0