The app I am trying to make needs to request a token from the TaxCore server by providing it with a personal certificate acquired from a smart card.
I've exported the certificate from the smart card, and named it buisness.cer.
I also have 2 more certificates that I need in order to establish the https connection (Sandbox SUF Issuing CA 1.cer
and Sandbox SUF RCA.cer
).
The official documentation states the following steps:
- Create HTTPS GET request object
- Add HTTP headers "Accept: application/json" and "Content-Type: application/json"
- Read certificate from the PKI Applet
- Use the certificate from the PKI Applet to establish SSL/TLS connection
- Send a request to "/api/v3/sdc/token" operation on TaxCore.API web service.
- Read the response as JSON structure defined below
I've lost days trying to make this work, and tested all of the examples I could find around the internet, but despite my efforts I always end up with a 401 respponse.
{"Message":"Authorization has been denied for this request."}
Currently I have this (non-working):
private static X509Certificate getCert(String f) {
InputStream is0;
try {
CertificateFactory cf0 = CertificateFactory.getInstance("X.509");
is0 = new FileInputStream(f);
var cer = (X509Certificate) cf0.generateCertificate(is0);
is0.close();
return cer;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public final static void main(String[] args) throws Exception {
var buisnessCert = getCert("someplace/buisness.cer");
var issuingCaCert = getCert("someplace/issuingCa.cer");
var rcaCert = getCert("someplace/rca.cer");
var tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null);
ks.setCertificateEntry("issuingCaCert", issuingCaCert);
ks.setCertificateEntry("rcaCert", rcaCert);
ks.setCertificateEntry("buisnessCert", buisnessCert);
tmf.init(ks);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, null, null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
try {
HttpGet httpget = new HttpGet("https://taxcoreservergoeshere/api/v3/sdc/token");
httpget.setHeader("Accept", "application/json");
httpget.setHeader("Content-Type", "application/json");
CloseableHttpResponse response = httpclient.execute(httpget);
try {
HttpEntity entity = response.getEntity();
System.out.println(EntityUtils.toString(entity));
EntityUtils.consume(entity);
} finally {
response.close();
}
} finally {
httpclient.close();
}
}
Any help is highly appreciated.