Reputation: 71
I am making an POST request in Java that requires an ssl certificate and private key combination. I have looked at Java KeyStore and create a .jks file from both the .key and .cert files using the two commands:
winpty openssl pkcs12 -export -in certificate.crt -inkey privatekey.key -out abc.p12
keytool -importkeystore -srckeystore abc.p12 -srcstoretype PKCS12 -destkeystore abc.jks -deststoretype JKS
But this failed the request with a 403 exception. Effectively, I want to perform the following Javascript's functions in java:
function cardBalance(intent, session, response) {
var options = {
key: fs.readFileSync('privatekey.key'),
cert: fs.readFileSync('certificate.crt'),
host: "blah.blah.blah.com',
path: '/abc/def/ghi/jkl/mno/pqr/creditcardsummary',
method: 'POST',
headers: {
'Authorization': 'Bearer ' + session.user.accessToken,
'Content-Type': 'application/json',
'Version': '1.1.0',
'zId': '1234',
'aId': '123456789',
'bId': 'bId',
'AppName': 'AppName'
}
};
var postData = JSON.stringify({
'acctnum': '00002600452999820832'
});
console.log(options);
var req = https.request(options, function (res) {
console.log('Request Credit Card Balance');
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
data = JSON.parse(data);
console.log(data);
if (data.CreditCardSummary.status === 'SUCCESS') {
var balance = data.balance;
console.log('Balance: ' + balance);
var speechOutput = 'The current balance is $' + balance;
response.tell(speechOutput);
} else {
response.tell('There was a problem getting your card balance.');
}
});
});
req.on('error', function (e) {
console.log('An Error Occurred when calling the Gateway. ' + e);
response.tell(e);
});
req.write(postData);
req.end();
}
Upvotes: 3
Views: 11479
Reputation: 71
I figured it out and I wanted to provide reference for other people. I had to convert my .key and .crt files to .p12 file and then convert that to a .jks file. I did that by following this link here importing an existing x509 certificate and private key in Java keystore to use in ssl Once I had my keystore properly set up I followed this page in java How to request a URL that requires a client certificate for authentication. Where I went wrong is that I did not correctly set my JVM arguments. I had to put these javax.net.ssl.keyStore and javax.net.ssl.keyStorePassword. Once I added that to my JVM it ran perfectly.
Upvotes: 3