Hi, I have to implement an applet for digital signature, the key are in a smart card, I follow the PKCS#11 format to withdraw their. I configure the provider
String pkcs11config = "name=SmartCard library=libraryPath";
byte[] pkcs11configBytes = pkcs11config.getBytes();
ByteArrayInputStream configStream = new ByteArrayInputStream(pkcs11configBytes);
Provider pkcs11Provider = new sun.security.pkcs11.SunPKCS11(configStream);
Security.addProvider(pkcs11Provider);
then i get the key (for semplicity i don't write the exception)
char[] pin = "1234".getBytes();
KeyStore smartCardKeyStore = KeyStore.getInstance("PKCS11");
smartCardKeyStore.load(null, pin);
Enumeration aliasesEnum = smartCardKeyStore.aliases();
if(aliasesEnum.hasMoreElements()){
String alias = (String) aliasesEnum.nextElement();
X509Certificate cert = (X509Certificate) smartCardKeyStore.getCertificate(alias);
PrivateKey privateKey = (PrivateKey) smartCardKeyStore.getKey(alias, null);
}
and then i create the digest
private byte[] signDocument(byte[] aDocument, PrivateKey aPrivateKey) throws GeneralSecurityException{
Signature signatureAlgorithm = Signature.getInstance("SHA1withRSA");
signatureAlgorithm.initSign(aPrivateKey);
signatureAlgorithm.update(aDocument);
byte[] digitalSignature = signatureAlgorithm.sign();
return digitalSignature;
}//SignDocument
now, i have byte[]digitalSignature and byte[]aDocument and i have to create a p7m file with the PKCS#7 format using the bouncycastle library. Can someone help me please?