I'm trying to sign a string with different certs from MS-KeyStore. But, I know there are imported keys from a token in MS-Keystore. So, my problem is - if I go through the Keystore and try to sign with a cert which has a reference to pkcs11 I get a pop up to enter the pkcs11 password. How can I check if the cert is from my token?
Thanks in advance!!!
This is my code for now:
String alias;
byte[] data = "test".getBytes();
char[] pin = "pass".toCharArray();
try {
KeyStore ks = KeyStore.getInstance("Windows-MY");
ks.load(null, pin);
System.out.println("Provider: "+ks.getProvider());
System.out.println("KS size: " + ks.size());
Enumeration enumeration = ks.aliases();
while (enumeration.hasMoreElements()) {
alias = (String) enumeration.nextElement();
PrivateKey privateKey = (PrivateKey) ks.getKey(alias, null);
Certificate certificate = ks.getCertificate(alias);
Provider provider = ks.getProvider();
Signature signature = Signature.getInstance("SHA1withRSA", provider);
try {
signature.initSign(privateKey);
signature.update(data);
byte[] signedSignature = signature.sign();
System.out.println("\tGenerated signature for " + alias);
signature.initVerify(certificate);
signature.update(data);
if (signature.verify(signedSignature)) {
System.out.println("\tSignature verifified for " + alias);
} else {
System.out.println("\tCould not verify signature for " + alias);
}
} catch (Exception ex) {
System.out.println("\tError for " + alias);
}
}
} catch (KeyStoreException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (CertificateException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (NoSuchAlgorithmException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (FileNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (UnrecoverableKeyException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}