RSA1_5 decryption with HSM key fails

Issue #201 resolved
Vladimir Dzhuvinov created an issue

with

com.nimbusds.jose.JOSEException: AES/GCM/NoPadding decryption failed: Tag mismatch!

Comments (2)

  1. Vladimir Dzhuvinov reporter

    Nitrokey is having issues:

    Decrypting CEK...
    com.nimbusds.jose.JOSEException: Couldn't decrypt Content Encryption Key (CEK): init() failed
        at com.nimbusds.jose.crypto.RSA1_5.decryptCEK(RSA1_5.java:121)
        at com.nimbusds.jose.crypto.RSADecrypter.decrypt(RSADecrypter.java:213)
        at com.nimbusds.jose.JWEObject.decrypt(JWEObject.java:415)
        at com.nimbusds.jose.crypto.HSMTest.testRSADecryptWithHSM(HSMTest.java:279)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.junit.runners.Suite.runChild(Suite.java:128)
        at org.junit.runners.Suite.runChild(Suite.java:27)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
        at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
        at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
        at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
        at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
    Caused by: java.security.InvalidKeyException: init() failed
        at sun.security.pkcs11.P11RSACipher.implInit(P11RSACipher.java:239)
        at sun.security.pkcs11.P11RSACipher.engineInit(P11RSACipher.java:168)
        at javax.crypto.Cipher.init(Cipher.java:1246)
        at javax.crypto.Cipher.init(Cipher.java:1186)
        at com.nimbusds.jose.crypto.RSA1_5.decryptCEK(RSA1_5.java:103)
        ... 38 more
    Caused by: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_KEY_TYPE_INCONSISTENT
        at sun.security.pkcs11.wrapper.PKCS11.C_DecryptInit(Native Method)
        at sun.security.pkcs11.P11RSACipher.initialize(P11RSACipher.java:304)
        at sun.security.pkcs11.P11RSACipher.implInit(P11RSACipher.java:237)
        ... 42 more
    
  2. Vladimir Dzhuvinov reporter

    Fixed by updating the HSM config:

    private static String HSM_CONFIG =
            "name = NitroKeyHSM\n" +
            "library = /usr/lib/x86_64-linux-gnu/opensc-pkcs11.so\n" +
            "slotListIndex = 1\n" +
            "attributes(*,CKO_PRIVATE_KEY,CKK_RSA) = {\n" +
            "  CKA_SIGN = true\n" +
            "}\n" +
            "attributes(*,CKO_PRIVATE_KEY,CKK_RSA) = {\n" +
            "  CKA_DECRYPT = true\n" +
            "}\n";
    
  3. Log in to comment