Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

PKCS#11 and P7M

2938054Apr 26 2015 — edited Apr 26 2015

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?

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 24 2015
Added on Apr 26 2015
0 comments
730 views