I am working on this Java project, where i need to load a private key from Windows KeyStore using the provider SunMSCAPI, but i don't supply any password at all, i don't know if i need to do so. this is sample test case of what i'm doing:
public static void main(String[] args) throws Throwable {
Provider provider = Security.getProvider("SunMSCAPI");
KeyStore wins=KeyStore.getInstance("Windows-MY", provider);
wins.load(null, null);
Enumeration<String> aliases = wins.aliases();
while (aliases.hasMoreElements()) {
String alias = (String) aliases.nextElement();
System.out.println(alias);
Certificate[] chain = wins.getCertificateChain(alias);
X509Certificate[] x509 = CERManager.toX509(chain);
for (int i = 0; i < x509.length; i++) {
System.out.println(x509[i].getSubjectX500Principal());
}
Key key = wins.getKey(alias, "1234".toCharArray());
System.out.println(key);
}
}
when i run this i get some certificate that i imported previously from a pfx file using Adobe Reader, but i can't get the private key corresponding to that certificate, instead, i just get null.
any help around this issue? thanks in advance
I think i found a solution that solved my problem. I tried to import a pfx in Java using this piece of code
and then i used the first code from the question to list the keys and certificates of Windows Key Store, and i got the private key OK.
A important detail, when importing the certificate and the private key, you should just pass the user certificate, no the whole chain. at least is the only way it worked for me.