user101666, I am also working with eSocial, but I am developing in C#/VB.NET. No . NET there is a ready method that displays the dialog for the user to choose the certificate desired:
X509Certificate2UI.SelectFromCollection
Your code does not open a certificate choice window because you have not written anything to make it happen. Your code:
KeyStore ks = KeyStore.getInstance("Windows-MY");
ks.load(null, "@Techne".toCharArray());
only loads all certificates stored in that Windows repository.
I did a quick search on this feature in Java and found this post on Stackoverflow global:
https://stackoverflow.com/q/23080214/8133067
There user Florian says that this functionality does not exist ready in Java, but that he wrote a solution in Java to display the Windows dialog for choice of certificate. He put the project on Github:
https://github.com/FlorianSW/org.droidwiki.certtest
And to get to that solution, he relied on this other post, by Tech Junkie:
https://stackoverflow.com/a/42088543/8133067
But basically what they did was directly access the function CryptUIDlgSelectCertificateFromStore
library Cryptui.dll, windows native:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa380288.aspx
I will replicate here the code posted by the user Tech Junkie, which is easier to post if the links no longer work:
NativeLibrary cryptUI = NativeLibrary.getInstance("Cryptui");
NativeLibrary crypt32 = NativeLibrary.getInstance("Crypt32");
Function functionCertOpenSystemStore = crypt32.getFunction("CertOpenSystemStoreA");
Object[] argsCertOpenSystemStore = new Object[] { 0, "CA"};
HANDLE h = (HANDLE) functionCertOpenSystemStore.invoke(HANDLE.class, argsCertOpenSystemStore);
Function functionCryptUIDlgSelectCertificateFromStore = cryptUI.getFunction("CryptUIDlgSelectCertificateFromStore");
System.out.println(functionCryptUIDlgSelectCertificateFromStore.getName());
Object[] argsCryptUIDlgSelectCertificateFromStore = new Object[] { h, 0, 0, 0, 16, 0, 0};
Pointer ptrCertContext = (Pointer) functionCryptUIDlgSelectCertificateFromStore.invoke(Pointer.class, argsCryptUIDlgSelectCertificateFromStore);
Function functionCertGetNameString = crypt32.getFunction("CertGetNameStringW");
char[] ptrName = new char[128];
Object[] argsCertGetNameString = new Object[] { ptrCertContext, 5, 0, 0, ptrName, 128};
functionCertGetNameString.invoke(argsCertGetNameString);
System.out.println("Selected certificate is " + new String(ptrName));
Function functionCertFreeCertificateContext = crypt32.getFunction("CertFreeCertificateContext");
Object[] argsCertFreeCertificateContext = new Object[] { ptrCertContext};
functionCertFreeCertificateContext.invoke(argsCertFreeCertificateContext);
Function functionCertCloseStore = crypt32.getFunction("CertCloseStore");
Object[] argsCertCloseStore = new Object[] { h, 0};
functionCertCloseStore.invoke(argsCertCloseStore);
I hope it helps.
I am also working with eSocial, but I am developing in C#/VB.NET. No . NET there is a ready-made method that displays the dialog for the user to choose the desired certificate: > X509certificate2ui.Selectfromcollection I did a quick search and found this solution for Java: https://stackoverflow.com/questions/23080214/windows-security-dialog-for-selecting-a-Certificate-in-java I hope it helps.
– Pedro Gaspar