I'd like to obtain the user's identity from a smartcard (PKI) from a Java fat client on Windows with Sun's MSCAPIProvider. The target is:
- user opens the app
- prompted for card
- prompted for PIN
- I get the X509Certicate in Java
- grant access, etc.
I have found based on this stackoverflow question sample which list's all certs in MY store and I am able to sign some bytes with the Microsoft Smartcard Provider PIN prompt.
This is my code:
Provider provider = Security.getProvider("SunMSCAPI");
KeyStore store = KeyStore.getInstance("Windows-MY", provider);
store.load(null, null);
System.out.println(store);
Enumeration<?> aliases = store.aliases();
while(aliases.hasMoreElements()) {
String alias = aliases.nextElement().toString();
Certificate[] signerKey = (Certificate[]) store.getCertificateChain(alias);
Object entry = store.getKey(alias, null);
System.out.println(alias + " " + Arrays.toString(signerKey));
System.out.println(entry);
}
Signature sig = Signature.getInstance("SHA1withRSA",provider);
PrivateKey key = (PrivateKey) store.getKey("Michael-O", null);
sig.initSign(key);
sig.update("Test".getBytes());
System.out.println(Arrays.toString(sig.sign()));
Now I am facing two problems:
- I do not know what his alias might be (chicken-egg-problem)
- How do I force PIN authorization with X509Cert as a result with establishing a SSL context though HTTPS?
Which are the missing bits?