How to import a privatekey/keypair using JCryptoki?

4/24/2015 1:28:11 PM
Gravatar
Total Posts 3

How to import a privatekey/keypair using JCryptoki?

Hi everyone, I created a Keystore from a P12 file and I can access it's objects like this: java.security.cert.X509Certificate certificate1 = (java.security.cert.X509Certificate) keystore.getCertificate("some DN"); RSAPublicKey publickey = certificate1.getPublicKey(); RSAPrivateKey privatekey = keystore.getKey("some DN", "mypass".toCharArray()); But but it reports the error: sun.security.rsa.RSAPrivateCrtKeyImpl cannot be cast to com.cryptware.jcryptoki.RSAPrivateKey If instead I use, java.security.interfaces.RSAPrivateKey privatekey =(java.security.interfaces.RSAPrivateKey) store.getKey("some DN", "mypass".toCharArray()); it doesn't report an error, however later while filling the template using this piece of code: templatePri.add(new CryptokiAttribute(CryptokiAttribute.CKA_CLASS, CryptokiObject.CKO_PRIVATE_KEY)); templatePri.add(new CryptokiAttribute(CryptokiAttribute.CKA_KEY_TYPE, Key.CKK_RSA)); templatePri.add(new CryptokiAttribute(CryptokiAttribute.CKA_SUBJECT, null)); templatePri.add(new CryptokiAttribute(CryptokiAttribute.CKA_ID, "1")); templatePri.add(new CryptokiAttribute(CryptokiAttribute.CKA_LABEL, "CEGER" )); templatePri.add(new CryptokiAttribute(CryptokiAttribute.CKA_TOKEN, true)); templatePri.add(new CryptokiAttribute(CryptokiAttribute.CKA_MODULUS, privatekey.getModulus()); templatePri.add(new CryptokiAttribute(CryptokiAttribute.CKA_PUBLIC_EXPONENT, publickey.getPublicExponent())); templatePri.add(new CryptokiAttribute(CryptokiAttribute.CKA_PRIVATE_EXPONENT, privatekey.getPrivateExponent())); templatePri.add(new CryptokiAttribute(CryptokiAttribute.CKA_PRIVATE, true)); templatePri.add(new CryptokiAttribute(CryptokiAttribute.CKA_MODIFIABLE, true)); I get a "Exception in thread "main" java.lang.IllegalArgumentException: Argument value 0x851640f8890c3e3f exceeds native capacity (4 bytes) mask=0xffffffff00000000" and it referes to the "templatePri.add(new CryptokiAttribute(CryptokiAttribute.CKA_MODULUS, privatekey.getModulus());" line Don't know how i should fill in the modulus field. Thanks in advance, i could really appreciate some hints, João Gaspar