How to do mutual SSL authentication in java

Bharat Rathod :

I want to do mutual SSL authentication using java code but I didn't get success I have key.pem and cert.pem file to do authentication I have tried it with Curl

curl -X POST -d '{ "Channel": "....}' -H "Content-Type: application/json" -H "Auth1: ***" -H "Auth2: ***" -k https://******/webservices/JSON/Default.aspx --cert "cert.pem" --key "Key.pem"

and it was working fine then I trying to create java program reference Send https request in java using .pem file but server returns "CERT_MISSING". I also tried with this https://www.naschenweng.info/2018/02/01/java-mutual-ssl-authentication-2-way-ssl-authentication/ this code creating p12, crt and jks file as above link says but still getting same error "CERT_MISSING". This is working NodeJS example:

var https = require("https");
var fs = require("fs");

var jsonData = {
    "Channel": ....
}

var options = {
    hostname: "****",
    port: 443,
    path: '/webservices/JSON/Default.aspx',
    method: 'POST',
    timeout: this.TimeOut,
    headers: {'Content-Type':'application/json',"Auth1": "****","Auth2": "*****"},
    json: true,
    key: fs.readFileSync('Key.pem'),
    cert: fs.readFileSync('cert.pem')
}

var req = https.request(options, function(res) {
    res.on('data', function(data) {
        var response = JSON.parse(data)
            console.log(response)

            req.end();
    });
});
req.on('error', function(e) {
    console.log("ERROR:");

    })


req.write(JSON.stringify(jsonData));
req.end();

Please help me in this.

Yogesh Bhatt :

I think you need "internediate-cert" file to Concatenate all certificates into one PEM file Like cat "internediate-cert.pem" "codika_cert.pem" "Key.pem" > full-chain.keycert.pem then Generate the PKCS12(.p12) keystore with the alias and password Like pkcs12 -export -in "full-chain.keycert.pem" -out full-chain.keycert.p12 -name alias -noiter -nomaciter then use full-chain.keycert.p12 as KeyStore with password. It should work.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=21193&siteId=1