Unable to sign using PKCS#11 tokens

classic Classic list List threaded Threaded
22 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Unable to sign using PKCS#11 tokens

Uri Blumenthal
I’ve hit an interesting problem. I use PIV tokens (YubiKey NEO devices and DOD CAC). They have four asymmetric key-pair slots:
  1. PIV Authentication - used for TLS client authentication, and smart card login to computers.
  2. Digital Signature - used to digitally sign email, sign documents (like PDF with Adobe Acrobat), sign code.
  3. Key Management - used to encrypt email, documents, and for encrypted filesystems.
  4. Card Authentication - used for door locks and such (like with badge readers that unlock doors and allow building entrance).

The point is that only (2) is appropriate for document signing. And PIV standard states that to use this key (to sign something), the request to sign must be immediately preceded by a token login (VERIFY PIN) command - if any operation with this hardware token is performed between the login and the signature request, the signature request fails, as the appended log shows.

So I’d like a mechanism that would allow the following (you can see what it is by the trace) to work:

net.sf.keystore_explorer.crypto.CryptoException: Block signing failed.
    at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:805)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
    at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
    at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

java.security.ProviderException: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_USER_NOT_LOGGED_IN
    at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:591)
    at java.security.Signature$Delegate.engineSign(Signature.java:1207)
    at java.security.Signature.sign(Signature.java:579)
    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$SignatureOutputStream.getSignature(JcaContentSignerBuilder.java:158)
    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$1.getSignature(JcaContentSignerBuilder.java:91)
    at org.bouncycastle.cms.SignerInfoGenerator.generate(SignerInfoGenerator.java:220)
    at org.bouncycastle.cms.CMSSignedDataGenerator.generate(CMSSignedDataGenerator.java:177)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:796)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
    at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
    at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

sun.security.pkcs11.wrapper.PKCS11Exception: CKR_USER_NOT_LOGGED_IN
    at sun.security.pkcs11.wrapper.PKCS11.C_SignFinal(Native Method)
    at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:553)
    at java.security.Signature$Delegate.engineSign(Signature.java:1207)
    at java.security.Signature.sign(Signature.java:579)
    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$SignatureOutputStream.getSignature(JcaContentSignerBuilder.java:158)
    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$1.getSignature(JcaContentSignerBuilder.java:91)
    at org.bouncycastle.cms.SignerInfoGenerator.generate(SignerInfoGenerator.java:220)
    at org.bouncycastle.cms.CMSSignedDataGenerator.generate(CMSSignedDataGenerator.java:177)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:796)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
    at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
    at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

In case it matters, Oracle’s jarsigner seems to exhibit the same problem, when one attempts to sign a JAR using key on a PKCS#11 hardware token.
--
Uri Blumenthal

Reply | Threaded
Open this post in threaded view
|

Re: Unable to sign using PKCS#11 tokens

David Hook

Initially I thought the way to deal with this was with a custom implementation of the ContentSigner interface. I'm not sure if you'll be able to use the PKC#11 provider as well - you may need something a little more local to the hardware to guarantee VERIFY PIN happens just be the signature request. Doing this would certainly work.

Looking at the docs for the PKCS11 provider though, I think you should just need to add:

KeyStore ks = KeyStore.getInstance("PKCS11");
ks.load(null, pin);

to the right place in the JcaContentSignerBuilder as a starting point as it appears it will trigger the VERIFY PIN (although it may do something else next, in which case it's back to the custom implementation). I have to admit I've never tried it - this might work just before the generate() method as well (in which case no changes are required).

Regards,

David

On 14/08/16 22:10, Uri Blumenthal wrote:
I’ve hit an interesting problem. I use PIV tokens (YubiKey NEO devices and DOD CAC). They have four asymmetric key-pair slots:
  1. PIV Authentication - used for TLS client authentication, and smart card login to computers.
  2. Digital Signature - used to digitally sign email, sign documents (like PDF with Adobe Acrobat), sign code.
  3. Key Management - used to encrypt email, documents, and for encrypted filesystems.
  4. Card Authentication - used for door locks and such (like with badge readers that unlock doors and allow building entrance).

The point is that only (2) is appropriate for document signing. And PIV standard states that to use this key (to sign something), the request to sign must be immediately preceded by a token login (VERIFY PIN) command - if any operation with this hardware token is performed between the login and the signature request, the signature request fails, as the appended log shows.

So I’d like a mechanism that would allow the following (you can see what it is by the trace) to work:

net.sf.keystore_explorer.crypto.CryptoException: Block signing failed.
    at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:805)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
    at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
    at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

java.security.ProviderException: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_USER_NOT_LOGGED_IN
    at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:591)
    at java.security.Signature$Delegate.engineSign(Signature.java:1207)
    at java.security.Signature.sign(Signature.java:579)
    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$SignatureOutputStream.getSignature(JcaContentSignerBuilder.java:158)
    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$1.getSignature(JcaContentSignerBuilder.java:91)
    at org.bouncycastle.cms.SignerInfoGenerator.generate(SignerInfoGenerator.java:220)
    at org.bouncycastle.cms.CMSSignedDataGenerator.generate(CMSSignedDataGenerator.java:177)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:796)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
    at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
    at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

sun.security.pkcs11.wrapper.PKCS11Exception: CKR_USER_NOT_LOGGED_IN
    at sun.security.pkcs11.wrapper.PKCS11.C_SignFinal(Native Method)
    at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:553)
    at java.security.Signature$Delegate.engineSign(Signature.java:1207)
    at java.security.Signature.sign(Signature.java:579)
    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$SignatureOutputStream.getSignature(JcaContentSignerBuilder.java:158)
    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$1.getSignature(JcaContentSignerBuilder.java:91)
    at org.bouncycastle.cms.SignerInfoGenerator.generate(SignerInfoGenerator.java:220)
    at org.bouncycastle.cms.CMSSignedDataGenerator.generate(CMSSignedDataGenerator.java:177)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:796)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
    at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
    at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

In case it matters, Oracle’s jarsigner seems to exhibit the same problem, when one attempts to sign a JAR using key on a PKCS#11 hardware token.
--
Uri Blumenthal


Reply | Threaded
Open this post in threaded view
|

Re: Unable to sign using PKCS#11 tokens

Uri Blumenthal
On Aug 14, 2016, at 18:16 , David Hook <[hidden email]> wrote:
Initially I thought the way to deal with this was with a custom implementation of the ContentSigner interface.

Lord, I hope not.

I'm not sure if you'll be able to use the PKC#11 provider as well - you may need something a little more local to the hardware to guarantee VERIFY PIN happens just be the signature request. Doing this would certainly work.

Looking at the docs for the PKCS11 provider though, I think you should just need to add:

KeyStore ks = KeyStore.getInstance("PKCS11");
ks.load(null, pin);

I think you’re on the right track here. Looking at the other code that succeeds doing (plain) digital signature using PKCS#11 token, I seem to need to build a KeyStore from KeyStore.builder, create AuthProvider from Provider, supply it with the callback that would prompt for the PIN...

to the right place in the JcaContentSignerBuilder as a starting point as it appears it will trigger the VERIFY PIN (although it may do something else next, in which case it's back to the custom implementation). I have to admit I've never tried it - this might work just before the generate() method as well (in which case no changes are required).

I wish somebody else has done it already. ;)

If not - I’ll try that myself.

Thank!


On 14/08/16 22:10, Uri Blumenthal wrote:
I’ve hit an interesting problem. I use PIV tokens (YubiKey NEO devices and DOD CAC). They have four asymmetric key-pair slots:
  1. PIV Authentication - used for TLS client authentication, and smart card login to computers.
  2. Digital Signature - used to digitally sign email, sign documents (like PDF with Adobe Acrobat), sign code.
  3. Key Management - used to encrypt email, documents, and for encrypted filesystems.
  4. Card Authentication - used for door locks and such (like with badge readers that unlock doors and allow building entrance).

The point is that only (2) is appropriate for document signing. And PIV standard states that to use this key (to sign something), the request to sign must be immediately preceded by a token login (VERIFY PIN) command - if any operation with this hardware token is performed between the login and the signature request, the signature request fails, as the appended log shows.

So I’d like a mechanism that would allow the following (you can see what it is by the trace) to work:

net.sf.keystore_explorer.crypto.CryptoException: Block signing failed.
    at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:805)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
    at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
    at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)

java.security.ProviderException: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_USER_NOT_LOGGED_IN
    at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:591)
    at java.security.Signature$Delegate.engineSign(Signature.java:1207)
    at java.security.Signature.sign(Signature.java:579)
    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$SignatureOutputStream.getSignature(JcaContentSignerBuilder.java:158)
    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$1.getSignature(JcaContentSignerBuilder.java:91)
    at org.bouncycastle.cms.SignerInfoGenerator.generate(SignerInfoGenerator.java:220)
    at org.bouncycastle.cms.CMSSignedDataGenerator.generate(CMSSignedDataGenerator.java:177)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:796)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
    at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
    at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
 

sun.security.pkcs11.wrapper.PKCS11Exception: CKR_USER_NOT_LOGGED_IN
    at sun.security.pkcs11.wrapper.PKCS11.C_SignFinal(Native Method)
    at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:553)
    at java.security.Signature$Delegate.engineSign(Signature.java:1207)
    at java.security.Signature.sign(Signature.java:579)
    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$SignatureOutputStream.getSignature(JcaContentSignerBuilder.java:158)
    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$1.getSignature(JcaContentSignerBuilder.java:91)
    at org.bouncycastle.cms.SignerInfoGenerator.generate(SignerInfoGenerator.java:220)
    at org.bouncycastle.cms.CMSSignedDataGenerator.generate(CMSSignedDataGenerator.java:177)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:796)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
    at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
    at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022). . . . . .
--
Uri Blumenthal

Reply | Threaded
Open this post in threaded view
|

Re: Unable to sign using PKCS#11 tokens

Edelhoff, Matthias
In reply to this post by David Hook

Hi Guys,
I habe to agree with David on this. Last year we tried using the p11 provider from the jre and deemed it not production ready. From what I remember you have to use 32 Bit middelware (at least on Windows) and switch on verbose logging. The Second is due to a 10 year Old bug which only enumerates all slots if in verbose mode.

With these two caveats you can use the provider in bc or Basic operations, but have little to no control over the details like Session Handling and pin caching.

There is an Alternative commerical Provider by aiak. In fact the jre Provider is i think an Old Version of this. Anyway the aiak guys also provider a oss thin Java Wrapper around pkcs11, which Can be easily used to build your own signer.

I will only get to some Sample Code on how to use the jre p11 procider  tomorrow, so let me know if you are interested.

Cheers,
Matthias

PS:
I wrote a Bit of gradle Code to automate BC provider signing via p11. So if you just want to use jarsigner there this works quite well, with the above caveats of course..

Am 15. Aug. 2016, um 00:18, David Hook <[hidden email]> schrieb:

Initially I thought the way to deal with this was with a custom implementation of the ContentSigner interface. I'm not sure if you'll be able to use the PKC#11 provider as well - you may need something a little more local to the hardware to guarantee VERIFY PIN happens just be the signature request. Doing this would certainly work.

Looking at the docs for the PKCS11 provider though, I think you should just need to add:

KeyStore ks = KeyStore.getInstance("PKCS11");
ks.load(null, pin);

to the right place in the JcaContentSignerBuilder as a starting point as it appears it will trigger the VERIFY PIN (although it may do something else next, in which case it's back to the custom implementation). I have to admit I've never tried it - this might work just before the generate() method as well (in which case no changes are required).

Regards,

David

On 14/08/16 22:10, Uri Blumenthal wrote:
I’ve hit an interesting problem. I use PIV tokens (YubiKey NEO devices and DOD CAC). They have four asymmetric key-pair slots:
  1. PIV Authentication - used for TLS client authentication, and smart card login to computers.
  2. Digital Signature - used to digitally sign email, sign documents (like PDF with Adobe Acrobat), sign code.
  3. Key Management - used to encrypt email, documents, and for encrypted filesystems.
  4. Card Authentication - used for door locks and such (like with badge readers that unlock doors and allow building entrance).

The point is that only (2) is appropriate for document signing. And PIV standard states that to use this key (to sign something), the request to sign must be immediately preceded by a token login (VERIFY PIN) command - if any operation with this hardware token is performed between the login and the signature request, the signature request fails, as the appended log shows.

So I’d like a mechanism that would allow the following (you can see what it is by the trace) to work:

net.sf.keystore_explorer.crypto.CryptoException: Block signing failed.
    at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:805)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
    at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
    at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

java.security.ProviderException: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_USER_NOT_LOGGED_IN
    at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:591)
    at java.security.Signature$Delegate.engineSign(Signature.java:1207)
    at java.security.Signature.sign(Signature.java:579)
    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$SignatureOutputStream.getSignature(JcaContentSignerBuilder.java:158)
    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$1.getSignature(JcaContentSignerBuilder.java:91)
    at org.bouncycastle.cms.SignerInfoGenerator.generate(SignerInfoGenerator.java:220)
    at org.bouncycastle.cms.CMSSignedDataGenerator.generate(CMSSignedDataGenerator.java:177)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:796)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
    at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
    at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

sun.security.pkcs11.wrapper.PKCS11Exception: CKR_USER_NOT_LOGGED_IN
    at sun.security.pkcs11.wrapper.PKCS11.C_SignFinal(Native Method)
    at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:553)
    at java.security.Signature$Delegate.engineSign(Signature.java:1207)
    at java.security.Signature.sign(Signature.java:579)
    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$SignatureOutputStream.getSignature(JcaContentSignerBuilder.java:158)
    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$1.getSignature(JcaContentSignerBuilder.java:91)
    at org.bouncycastle.cms.SignerInfoGenerator.generate(SignerInfoGenerator.java:220)
    at org.bouncycastle.cms.CMSSignedDataGenerator.generate(CMSSignedDataGenerator.java:177)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:796)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
    at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
    at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

In case it matters, Oracle’s jarsigner seems to exhibit the same problem, when one attempts to sign a JAR using key on a PKCS#11 hardware token.
--
Uri Blumenthal


Reply | Threaded
Open this post in threaded view
|

Re: Unable to sign using PKCS#11 tokens

Andreas Schwier-2
Hi,

we maintain opensc-java at [1], which is the PKCS#11/JNI we use in
OpenSCDP [2].

It's an alternative to IAIK and SUNPKCS11. The module provides a JCE
interface and a direct P11 interface. We commonly use that module to
test PKCS#11 implementations like OpenSC or the PKCS#11 module for our
SmartCard-HSM [3].

Andreas

[1] https://github.com/CardContact/opensc-java
[2] http://www.openscdp.org/
[3] http://www.smartcard-hsm.com/


On 08/15/2016 09:05 AM, Edelhoff, Matthias wrote:

> Hi Guys,
> I habe to agree with David on this. Last year we tried using the p11 provider from the jre and deemed it not production ready. From what I remember you have to use 32 Bit middelware (at least on Windows) and switch on verbose logging. The Second is due to a 10 year Old bug which only enumerates all slots if in verbose mode.
>
> With these two caveats you can use the provider in bc or Basic operations, but have little to no control over the details like Session Handling and pin caching.
>
> There is an Alternative commerical Provider by aiak. In fact the jre Provider is i think an Old Version of this. Anyway the aiak guys also provider a oss thin Java Wrapper around pkcs11, which Can be easily used to build your own signer.
>
> I will only get to some Sample Code on how to use the jre p11 procider  tomorrow, so let me know if you are interested.
>
> Cheers,
> Matthias
>
> PS:
> I wrote a Bit of gradle Code to automate BC provider signing via p11. So if you just want to use jarsigner there this works quite well, with the above caveats of course..
>
> Am 15. Aug. 2016, um 00:18, David Hook <[hidden email]<mailto:[hidden email]>> schrieb:
>
> Initially I thought the way to deal with this was with a custom implementation of the ContentSigner interface. I'm not sure if you'll be able to use the PKC#11 provider as well - you may need something a little more local to the hardware to guarantee VERIFY PIN happens just be the signature request. Doing this would certainly work.
>
> Looking at the docs for the PKCS11 provider though, I think you should just need to add:
>
> KeyStore ks = KeyStore.getInstance("PKCS11");
> ks.load(null, pin);
>
> to the right place in the JcaContentSignerBuilder as a starting point as it appears it will trigger the VERIFY PIN (although it may do something else next, in which case it's back to the custom implementation). I have to admit I've never tried it - this might work just before the generate() method as well (in which case no changes are required).
>
> Regards,
>
> David
>
> On 14/08/16 22:10, Uri Blumenthal wrote:
> I’ve hit an interesting problem. I use PIV tokens (YubiKey NEO<https://www.yubico.com/products/yubikey-hardware/> devices and DOD CAC). They have four asymmetric key-pair slots:
>
>   1.  PIV Authentication - used for TLS client authentication, and smart card login to computers.
>   2.  Digital Signature - used to digitally sign email, sign documents (like PDF with Adobe Acrobat), sign code.
>   3.  Key Management - used to encrypt email, documents, and for encrypted filesystems.
>   4.  Card Authentication - used for door locks and such (like with badge readers that unlock doors and allow building entrance).
>
> The point is that only (2) is appropriate for document signing. And PIV standard states that to use this key (to sign something), the request to sign must be immediately preceded by a token login (VERIFY PIN) command - if any operation with this hardware token is performed between the login and the signature request, the signature request fails, as the appended log shows.
>
> So I’d like a mechanism that would allow the following (you can see what it is by the trace) to work:
>
>
> net.sf.keystore_explorer.crypto.CryptoException: Block signing failed.
>     at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:805)
>     at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
>     at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
>     at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
>     at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
>     at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
>     at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
>     at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
>     at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
>     at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
>     at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
>     at java.awt.Component.processMouseEvent(Component.java:6533)
>     at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
>     at java.awt.Component.processEvent(Component.java:6298)
>     at java.awt.Container.processEvent(Container.java:2236)
>     at java.awt.Component.dispatchEventImpl(Component.java:4889)
>     at java.awt.Container.dispatchEventImpl(Container.java:2294)
>     at java.awt.Component.dispatchEvent(Component.java:4711)
>     at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
>     at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
>     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
>     at java.awt.Container.dispatchEventImpl(Container.java:2280)
>     at java.awt.Window.dispatchEventImpl(Window.java:2746)
>     at java.awt.Component.dispatchEvent(Component.java:4711)
>     at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
>     at java.awt.EventQueue.access$500(EventQueue.java:97)
>     at java.awt.EventQueue$3.run(EventQueue.java:709)
>     at java.awt.EventQueue$3.run(EventQueue.java:703)
>     at java.security.AccessController.doPrivileged(Native Method)
>     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
>     at java.awt.EventQueue$4.run(EventQueue.java:731)
>     at java.awt.EventQueue$4.run(EventQueue.java:729)
>     at java.security.AccessController.doPrivileged(Native Method)
>     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>     at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
>     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
>     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
>     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
>     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
>     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
>     at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
>
> java.security.ProviderException: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_USER_NOT_LOGGED_IN
>     at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:591)
>     at java.security.Signature$Delegate.engineSign(Signature.java:1207)
>     at java.security.Signature.sign(Signature.java:579)
>     at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$SignatureOutputStream.getSignature(JcaContentSignerBuilder.java:158)
>     at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$1.getSignature(JcaContentSignerBuilder.java:91)
>     at org.bouncycastle.cms.SignerInfoGenerator.generate(SignerInfoGenerator.java:220)
>     at org.bouncycastle.cms.CMSSignedDataGenerator.generate(CMSSignedDataGenerator.java:177)
>     at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:796)
>     at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
>     at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
>     at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
>     at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
>     at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
>     at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
>     at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
>     at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
>     at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
>     at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
>     at java.awt.Component.processMouseEvent(Component.java:6533)
>     at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
>     at java.awt.Component.processEvent(Component.java:6298)
>     at java.awt.Container.processEvent(Container.java:2236)
>     at java.awt.Component.dispatchEventImpl(Component.java:4889)
>     at java.awt.Container.dispatchEventImpl(Container.java:2294)
>     at java.awt.Component.dispatchEvent(Component.java:4711)
>     at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
>     at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
>     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
>     at java.awt.Container.dispatchEventImpl(Container.java:2280)
>     at java.awt.Window.dispatchEventImpl(Window.java:2746)
>     at java.awt.Component.dispatchEvent(Component.java:4711)
>     at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
>     at java.awt.EventQueue.access$500(EventQueue.java:97)
>     at java.awt.EventQueue$3.run(EventQueue.java:709)
>     at java.awt.EventQueue$3.run(EventQueue.java:703)
>     at java.security.AccessController.doPrivileged(Native Method)
>     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
>     at java.awt.EventQueue$4.run(EventQueue.java:731)
>     at java.awt.EventQueue$4.run(EventQueue.java:729)
>     at java.security.AccessController.doPrivileged(Native Method)
>     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>     at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
>     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
>     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
>     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
>     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
>     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
>     at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
>
> sun.security.pkcs11.wrapper.PKCS11Exception: CKR_USER_NOT_LOGGED_IN
>     at sun.security.pkcs11.wrapper.PKCS11.C_SignFinal(Native Method)
>     at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:553)
>     at java.security.Signature$Delegate.engineSign(Signature.java:1207)
>     at java.security.Signature.sign(Signature.java:579)
>     at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$SignatureOutputStream.getSignature(JcaContentSignerBuilder.java:158)
>     at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$1.getSignature(JcaContentSignerBuilder.java:91)
>     at org.bouncycastle.cms.SignerInfoGenerator.generate(SignerInfoGenerator.java:220)
>     at org.bouncycastle.cms.CMSSignedDataGenerator.generate(CMSSignedDataGenerator.java:177)
>     at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:796)
>     at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
>     at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
>     at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
>     at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
>     at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
>     at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
>     at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
>     at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
>     at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
>     at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
>     at java.awt.Component.processMouseEvent(Component.java:6533)
>     at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
>     at java.awt.Component.processEvent(Component.java:6298)
>     at java.awt.Container.processEvent(Container.java:2236)
>     at java.awt.Component.dispatchEventImpl(Component.java:4889)
>     at java.awt.Container.dispatchEventImpl(Container.java:2294)
>     at java.awt.Component.dispatchEvent(Component.java:4711)
>     at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
>     at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
>     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
>     at java.awt.Container.dispatchEventImpl(Container.java:2280)
>     at java.awt.Window.dispatchEventImpl(Window.java:2746)
>     at java.awt.Component.dispatchEvent(Component.java:4711)
>     at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
>     at java.awt.EventQueue.access$500(EventQueue.java:97)
>     at java.awt.EventQueue$3.run(EventQueue.java:709)
>     at java.awt.EventQueue$3.run(EventQueue.java:703)
>     at java.security.AccessController.doPrivileged(Native Method)
>     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
>     at java.awt.EventQueue$4.run(EventQueue.java:731)
>     at java.awt.EventQueue$4.run(EventQueue.java:729)
>     at java.security.AccessController.doPrivileged(Native Method)
>     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>     at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
>     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
>     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
>     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
>     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
>     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
>     at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
>
> In case it matters, Oracle’s jarsigner seems to exhibit the same problem, when one attempts to sign a JAR using key on a PKCS#11 hardware token.
> --
> Uri Blumenthal
> [hidden email]<mailto:[hidden email]>
>
>


--

    ---------    CardContact Systems GmbH
   |.##> <##.|   Schülerweg 38
   |#       #|   D-32429 Minden, Germany
   |#       #|   Phone +49 571 56149
   |'##> <##'|   http://www.cardcontact.de
    ---------    Registergericht Bad Oeynhausen HRB 14880
                 Geschäftsführer Andreas Schwier



--

    ---------    CardContact Systems GmbH
   |.##> <##.|   Schülerweg 38
   |#       #|   D-32429 Minden, Germany
   |#       #|   Phone +49 571 56149
   |'##> <##'|   http://www.cardcontact.de
    ---------    Registergericht Bad Oeynhausen HRB 14880
                 Geschäftsführer Andreas Schwier

Reply | Threaded
Open this post in threaded view
|

Re: Unable to sign using PKCS#11 tokens

Uri Blumenthal
In reply to this post by Edelhoff, Matthias
Matthias,

Yes please - it would be quite helpful to see your sample code.

Thanks!

On Aug 15, 2016, at 3:05 , Edelhoff, Matthias <[hidden email]> wrote:

Hi Guys,
I habe to agree with David on this. Last year we tried using the p11 provider from the jre and deemed it not production ready. From what I remember you have to use 32 Bit middelware (at least on Windows) and switch on verbose logging. The Second is due to a 10 year Old bug which only enumerates all slots if in verbose mode.

With these two caveats you can use the provider in bc or Basic operations, but have little to no control over the details like Session Handling and pin caching.

There is an Alternative commerical Provider by aiak. In fact the jre Provider is i think an Old Version of this. Anyway the aiak guys also provider a oss thin Java Wrapper around pkcs11, which Can be easily used to build your own signer.

I will only get to some Sample Code on how to use the jre p11 procider  tomorrow, so let me know if you are interested.

Cheers,
Matthias

PS:
I wrote a Bit of gradle Code to automate BC provider signing via p11. So if you just want to use jarsigner there this works quite well, with the above caveats of course..

Am 15. Aug. 2016, um 00:18, David Hook <[hidden email]> schrieb:

Initially I thought the way to deal with this was with a custom implementation of the ContentSigner interface. I'm not sure if you'll be able to use the PKC#11 provider as well - you may need something a little more local to the hardware to guarantee VERIFY PIN happens just be the signature request. Doing this would certainly work.

Looking at the docs for the PKCS11 provider though, I think you should just need to add:

KeyStore ks = KeyStore.getInstance("PKCS11");
ks.load(null, pin);

to the right place in the JcaContentSignerBuilder as a starting point as it appears it will trigger the VERIFY PIN (although it may do something else next, in which case it's back to the custom implementation). I have to admit I've never tried it - this might work just before the generate() method as well (in which case no changes are required).

Regards,

David

On 14/08/16 22:10, Uri Blumenthal wrote:
I’ve hit an interesting problem. I use PIV tokens (YubiKey NEO devices and DOD CAC). They have four asymmetric key-pair slots:
  1. PIV Authentication - used for TLS client authentication, and smart card login to computers.
  2. Digital Signature - used to digitally sign email, sign documents (like PDF with Adobe Acrobat), sign code.
  3. Key Management - used to encrypt email, documents, and for encrypted filesystems.
  4. Card Authentication - used for door locks and such (like with badge readers that unlock doors and allow building entrance).

The point is that only (2) is appropriate for document signing. And PIV standard states that to use this key (to sign something), the request to sign must be immediately preceded by a token login (VERIFY PIN) command - if any operation with this hardware token is performed between the login and the signature request, the signature request fails, as the appended log shows.

So I’d like a mechanism that would allow the following (you can see what it is by the trace) to work:

net.sf.keystore_explorer.crypto.CryptoException: Block signing failed.
    at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:805)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
    at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
    at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

java.security.ProviderException: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_USER_NOT_LOGGED_IN
    at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:591)
    at java.security.Signature$Delegate.engineSign(Signature.java:1207)
    at java.security.Signature.sign(Signature.java:579)
    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$SignatureOutputStream.getSignature(JcaContentSignerBuilder.java:158)
    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$1.getSignature(JcaContentSignerBuilder.java:91)
    at org.bouncycastle.cms.SignerInfoGenerator.generate(SignerInfoGenerator.java:220)
    at org.bouncycastle.cms.CMSSignedDataGenerator.generate(CMSSignedDataGenerator.java:177)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:796)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
    at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
    at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

sun.security.pkcs11.wrapper.PKCS11Exception: CKR_USER_NOT_LOGGED_IN
    at sun.security.pkcs11.wrapper.PKCS11.C_SignFinal(Native Method)
    at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:553)
    at java.security.Signature$Delegate.engineSign(Signature.java:1207)
    at java.security.Signature.sign(Signature.java:579)
    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$SignatureOutputStream.getSignature(JcaContentSignerBuilder.java:158)
    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$1.getSignature(JcaContentSignerBuilder.java:91)
    at org.bouncycastle.cms.SignerInfoGenerator.generate(SignerInfoGenerator.java:220)
    at org.bouncycastle.cms.CMSSignedDataGenerator.generate(CMSSignedDataGenerator.java:177)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:796)
    at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
    at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
    at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

In case it matters, Oracle’s jarsigner seems to exhibit the same problem, when one attempts to sign a JAR using key on a PKCS#11 hardware token.
--
Uri Blumenthal



--
Uri the Great




Reply | Threaded
Open this post in threaded view
|

Re: Unable to sign using PKCS#11 tokens

Uri Blumenthal
In reply to this post by Andreas Schwier-2
Andreas,

I’d love to try your opensc-java, but cannot even build it on my Mac OS X. I have Maven and Ant installed/configured - but it seems to want some “ivy”?!

How different is your code base compared to https://github.com/OpenSC/OpenSC-Java/tree/master/pkcs11? Because if the latter requires just Maven - I’d much rather stay with it. But if you made significant code improvements, then I’d have no choice.

> On Aug 15, 2016, at 4:35 , Andreas Schwier <[hidden email]> wrote:
>
> Hi,
>
> we maintain opensc-java at [1], which is the PKCS#11/JNI we use in
> OpenSCDP [2].
>
> It's an alternative to IAIK and SUNPKCS11. The module provides a JCE
> interface and a direct P11 interface. We commonly use that module to
> test PKCS#11 implementations like OpenSC or the PKCS#11 module for our
> SmartCard-HSM [3].
>
> Andreas
>
> [1] https://github.com/CardContact/opensc-java
> [2] http://www.openscdp.org/
> [3] http://www.smartcard-hsm.com/
>
>
> On 08/15/2016 09:05 AM, Edelhoff, Matthias wrote:
>> Hi Guys,
>> I habe to agree with David on this. Last year we tried using the p11 provider from the jre and deemed it not production ready. From what I remember you have to use 32 Bit middelware (at least on Windows) and switch on verbose logging. The Second is due to a 10 year Old bug which only enumerates all slots if in verbose mode.
>>
>> With these two caveats you can use the provider in bc or Basic operations, but have little to no control over the details like Session Handling and pin caching.
>>
>> There is an Alternative commerical Provider by aiak. In fact the jre Provider is i think an Old Version of this. Anyway the aiak guys also provider a oss thin Java Wrapper around pkcs11, which Can be easily used to build your own signer.
>>
>> I will only get to some Sample Code on how to use the jre p11 procider  tomorrow, so let me know if you are interested.
>>
>> Cheers,
>> Matthias
>>
>> PS:
>> I wrote a Bit of gradle Code to automate BC provider signing via p11. So if you just want to use jarsigner there this works quite well, with the above caveats of course..
>>
>> Am 15. Aug. 2016, um 00:18, David Hook <[hidden email]<mailto:[hidden email]>> schrieb:
>>
>> Initially I thought the way to deal with this was with a custom implementation of the ContentSigner interface. I'm not sure if you'll be able to use the PKC#11 provider as well - you may need something a little more local to the hardware to guarantee VERIFY PIN happens just be the signature request. Doing this would certainly work.
>>
>> Looking at the docs for the PKCS11 provider though, I think you should just need to add:
>>
>> KeyStore ks = KeyStore.getInstance("PKCS11");
>> ks.load(null, pin);
>>
>> to the right place in the JcaContentSignerBuilder as a starting point as it appears it will trigger the VERIFY PIN (although it may do something else next, in which case it's back to the custom implementation). I have to admit I've never tried it - this might work just before the generate() method as well (in which case no changes are required).
>>
>> Regards,
>>
>> David
>>
>> On 14/08/16 22:10, Uri Blumenthal wrote:
>> I’ve hit an interesting problem. I use PIV tokens (YubiKey NEO<https://www.yubico.com/products/yubikey-hardware/> devices and DOD CAC). They have four asymmetric key-pair slots:
>>
>>  1.  PIV Authentication - used for TLS client authentication, and smart card login to computers.
>>  2.  Digital Signature - used to digitally sign email, sign documents (like PDF with Adobe Acrobat), sign code.
>>  3.  Key Management - used to encrypt email, documents, and for encrypted filesystems.
>>  4.  Card Authentication - used for door locks and such (like with badge readers that unlock doors and allow building entrance).
>>
>> The point is that only (2) is appropriate for document signing. And PIV standard states that to use this key (to sign something), the request to sign must be immediately preceded by a token login (VERIFY PIN) command - if any operation with this hardware token is performed between the login and the signature request, the signature request fails, as the appended log shows.
>>
>> So I’d like a mechanism that would allow the following (you can see what it is by the trace) to work:
>>
>>
>> net.sf.keystore_explorer.crypto.CryptoException: Block signing failed.
>>    at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:805)
>>    at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
>>    at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
>>    at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
>>    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
>>    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
>>    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
>>    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
>>    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
>>    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
>>    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
>>    at java.awt.Component.processMouseEvent(Component.java:6533)
>>    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
>>    at java.awt.Component.processEvent(Component.java:6298)
>>    at java.awt.Container.processEvent(Container.java:2236)
>>    at java.awt.Component.dispatchEventImpl(Component.java:4889)
>>    at java.awt.Container.dispatchEventImpl(Container.java:2294)
>>    at java.awt.Component.dispatchEvent(Component.java:4711)
>>    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
>>    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
>>    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
>>    at java.awt.Container.dispatchEventImpl(Container.java:2280)
>>    at java.awt.Window.dispatchEventImpl(Window.java:2746)
>>    at java.awt.Component.dispatchEvent(Component.java:4711)
>>    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
>>    at java.awt.EventQueue.access$500(EventQueue.java:97)
>>    at java.awt.EventQueue$3.run(EventQueue.java:709)
>>    at java.awt.EventQueue$3.run(EventQueue.java:703)
>>    at java.security.AccessController.doPrivileged(Native Method)
>>    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>>    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
>>    at java.awt.EventQueue$4.run(EventQueue.java:731)
>>    at java.awt.EventQueue$4.run(EventQueue.java:729)
>>    at java.security.AccessController.doPrivileged(Native Method)
>>    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>>    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
>>    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
>>    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
>>    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
>>    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
>>    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
>>    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
>>
>> java.security.ProviderException: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_USER_NOT_LOGGED_IN
>>    at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:591)
>>    at java.security.Signature$Delegate.engineSign(Signature.java:1207)
>>    at java.security.Signature.sign(Signature.java:579)
>>    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$SignatureOutputStream.getSignature(JcaContentSignerBuilder.java:158)
>>    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$1.getSignature(JcaContentSignerBuilder.java:91)
>>    at org.bouncycastle.cms.SignerInfoGenerator.generate(SignerInfoGenerator.java:220)
>>    at org.bouncycastle.cms.CMSSignedDataGenerator.generate(CMSSignedDataGenerator.java:177)
>>    at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:796)
>>    at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
>>    at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
>>    at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
>>    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
>>    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
>>    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
>>    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
>>    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
>>    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
>>    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
>>    at java.awt.Component.processMouseEvent(Component.java:6533)
>>    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
>>    at java.awt.Component.processEvent(Component.java:6298)
>>    at java.awt.Container.processEvent(Container.java:2236)
>>    at java.awt.Component.dispatchEventImpl(Component.java:4889)
>>    at java.awt.Container.dispatchEventImpl(Container.java:2294)
>>    at java.awt.Component.dispatchEvent(Component.java:4711)
>>    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
>>    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
>>    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
>>    at java.awt.Container.dispatchEventImpl(Container.java:2280)
>>    at java.awt.Window.dispatchEventImpl(Window.java:2746)
>>    at java.awt.Component.dispatchEvent(Component.java:4711)
>>    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
>>    at java.awt.EventQueue.access$500(EventQueue.java:97)
>>    at java.awt.EventQueue$3.run(EventQueue.java:709)
>>    at java.awt.EventQueue$3.run(EventQueue.java:703)
>>    at java.security.AccessController.doPrivileged(Native Method)
>>    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>>    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
>>    at java.awt.EventQueue$4.run(EventQueue.java:731)
>>    at java.awt.EventQueue$4.run(EventQueue.java:729)
>>    at java.security.AccessController.doPrivileged(Native Method)
>>    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>>    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
>>    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
>>    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
>>    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
>>    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
>>    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
>>    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
>>
>> sun.security.pkcs11.wrapper.PKCS11Exception: CKR_USER_NOT_LOGGED_IN
>>    at sun.security.pkcs11.wrapper.PKCS11.C_SignFinal(Native Method)
>>    at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:553)
>>    at java.security.Signature$Delegate.engineSign(Signature.java:1207)
>>    at java.security.Signature.sign(Signature.java:579)
>>    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$SignatureOutputStream.getSignature(JcaContentSignerBuilder.java:158)
>>    at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$1.getSignature(JcaContentSignerBuilder.java:91)
>>    at org.bouncycastle.cms.SignerInfoGenerator.generate(SignerInfoGenerator.java:220)
>>    at org.bouncycastle.cms.CMSSignedDataGenerator.generate(CMSSignedDataGenerator.java:177)
>>    at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:796)
>>    at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
>>    at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
>>    at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
>>    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
>>    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
>>    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
>>    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
>>    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
>>    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
>>    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
>>    at java.awt.Component.processMouseEvent(Component.java:6533)
>>    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
>>    at java.awt.Component.processEvent(Component.java:6298)
>>    at java.awt.Container.processEvent(Container.java:2236)
>>    at java.awt.Component.dispatchEventImpl(Component.java:4889)
>>    at java.awt.Container.dispatchEventImpl(Container.java:2294)
>>    at java.awt.Component.dispatchEvent(Component.java:4711)
>>    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
>>    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
>>    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
>>    at java.awt.Container.dispatchEventImpl(Container.java:2280)
>>    at java.awt.Window.dispatchEventImpl(Window.java:2746)
>>    at java.awt.Component.dispatchEvent(Component.java:4711)
>>    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
>>    at java.awt.EventQueue.access$500(EventQueue.java:97)
>>    at java.awt.EventQueue$3.run(EventQueue.java:709)
>>    at java.awt.EventQueue$3.run(EventQueue.java:703)
>>    at java.security.AccessController.doPrivileged(Native Method)
>>    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>>    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
>>    at java.awt.EventQueue$4.run(EventQueue.java:731)
>>    at java.awt.EventQueue$4.run(EventQueue.java:729)
>>    at java.security.AccessController.doPrivileged(Native Method)
>>    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>>    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
>>    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
>>    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
>>    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
>>    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
>>    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
>>    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
>>
>> In case it matters, Oracle’s jarsigner seems to exhibit the same problem, when one attempts to sign a JAR using key on a PKCS#11 hardware token.
>> --
>> Uri Blumenthal
>> [hidden email]<mailto:[hidden email]>
>>
>>
>
>
> --
>
>    ---------    CardContact Systems GmbH
>   |.##> <##.|   Schülerweg 38
>   |#       #|   D-32429 Minden, Germany
>   |#       #|   Phone +49 571 56149
>   |'##> <##'|   http://www.cardcontact.de
>    ---------    Registergericht Bad Oeynhausen HRB 14880
>                 Geschäftsführer Andreas Schwier
>
>
>
> --
>
>    ---------    CardContact Systems GmbH
>   |.##> <##.|   Schülerweg 38
>   |#       #|   D-32429 Minden, Germany
>   |#       #|   Phone +49 571 56149
>   |'##> <##'|   http://www.cardcontact.de
>    ---------    Registergericht Bad Oeynhausen HRB 14880
>                 Geschäftsführer Andreas Schwier
>

--
Uri the Great
[hidden email]




Reply | Threaded
Open this post in threaded view
|

Re: Unable to sign using PKCS#11 tokens

Uri Blumenthal
Andreas,

I’ve installed apache-ivy-2.4.0, but seem to have no luck:

$ ant compile
Buildfile: /Users/uri/src/opensc-java-andreas/build.xml

init:

BUILD FAILED
/Users/uri/src/opensc-java-andreas/build.xml:25: The following error occurred while executing this line:
jar:file:/Library/Java/Extensions/ivy-2.4.0.jar!/org/apache/ivy/ant/antlib.xml:21: typedef A class needed by class org.apache.ivy.ant.IvyAntSettings cannot be found: org/apache/tools/ant/types/DataType
 using the classloader AntClassLoader[]

Total time: 0 seconds
$

Could you please help me to get this thing built on Mac OS X?

Thanks!

> On Aug 15, 2016, at 13:17 , Uri Blumenthal <[hidden email]> wrote:
>
> Andreas,
>
> I’d love to try your opensc-java, but cannot even build it on my Mac OS X. I have Maven and Ant installed/configured - but it seems to want some “ivy”?!
>
> How different is your code base compared to https://github.com/OpenSC/OpenSC-Java/tree/master/pkcs11? Because if the latter requires just Maven - I’d much rather stay with it. But if you made significant code improvements, then I’d have no choice.
>
>> On Aug 15, 2016, at 4:35 , Andreas Schwier <[hidden email]> wrote:
>>
>> Hi,
>>
>> we maintain opensc-java at [1], which is the PKCS#11/JNI we use in
>> OpenSCDP [2].
>>
>> It's an alternative to IAIK and SUNPKCS11. The module provides a JCE
>> interface and a direct P11 interface. We commonly use that module to
>> test PKCS#11 implementations like OpenSC or the PKCS#11 module for our
>> SmartCard-HSM [3].
>>
>> Andreas
>>
>> [1] https://github.com/CardContact/opensc-java
>> [2] http://www.openscdp.org/
>> [3] http://www.smartcard-hsm.com/
>>
>>
>> On 08/15/2016 09:05 AM, Edelhoff, Matthias wrote:
>>> Hi Guys,
>>> I habe to agree with David on this. Last year we tried using the p11 provider from the jre and deemed it not production ready. From what I remember you have to use 32 Bit middelware (at least on Windows) and switch on verbose logging. The Second is due to a 10 year Old bug which only enumerates all slots if in verbose mode.
>>>
>>> With these two caveats you can use the provider in bc or Basic operations, but have little to no control over the details like Session Handling and pin caching.
>>>
>>> There is an Alternative commerical Provider by aiak. In fact the jre Provider is i think an Old Version of this. Anyway the aiak guys also provider a oss thin Java Wrapper around pkcs11, which Can be easily used to build your own signer.
>>>
>>> I will only get to some Sample Code on how to use the jre p11 procider  tomorrow, so let me know if you are interested.
>>>
>>> Cheers,
>>> Matthias
>>>
>>> PS:
>>> I wrote a Bit of gradle Code to automate BC provider signing via p11. So if you just want to use jarsigner there this works quite well, with the above caveats of course..
>>>
>>> Am 15. Aug. 2016, um 00:18, David Hook <[hidden email]<mailto:[hidden email]>> schrieb:
>>>
>>> Initially I thought the way to deal with this was with a custom implementation of the ContentSigner interface. I'm not sure if you'll be able to use the PKC#11 provider as well - you may need something a little more local to the hardware to guarantee VERIFY PIN happens just be the signature request. Doing this would certainly work.
>>>
>>> Looking at the docs for the PKCS11 provider though, I think you should just need to add:
>>>
>>> KeyStore ks = KeyStore.getInstance("PKCS11");
>>> ks.load(null, pin);
>>>
>>> to the right place in the JcaContentSignerBuilder as a starting point as it appears it will trigger the VERIFY PIN (although it may do something else next, in which case it's back to the custom implementation). I have to admit I've never tried it - this might work just before the generate() method as well (in which case no changes are required).
>>>
>>> Regards,
>>>
>>> David
>>>
>>> On 14/08/16 22:10, Uri Blumenthal wrote:
>>> I’ve hit an interesting problem. I use PIV tokens (YubiKey NEO<https://www.yubico.com/products/yubikey-hardware/> devices and DOD CAC). They have four asymmetric key-pair slots:
>>>
>>> 1.  PIV Authentication - used for TLS client authentication, and smart card login to computers.
>>> 2.  Digital Signature - used to digitally sign email, sign documents (like PDF with Adobe Acrobat), sign code.
>>> 3.  Key Management - used to encrypt email, documents, and for encrypted filesystems.
>>> 4.  Card Authentication - used for door locks and such (like with badge readers that unlock doors and allow building entrance).
>>>
>>> The point is that only (2) is appropriate for document signing. And PIV standard states that to use this key (to sign something), the request to sign must be immediately preceded by a token login (VERIFY PIN) command - if any operation with this hardware token is performed between the login and the signature request, the signature request fails, as the appended log shows.
>>>
>>> So I’d like a mechanism that would allow the following (you can see what it is by the trace) to work:
>>>
>>>
>>> net.sf.keystore_explorer.crypto.CryptoException: Block signing failed.
>>>   at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:805)
>>>   at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
>>>   at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
>>>   at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
>>>   at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
>>>   at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
>>>   at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
>>>   at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
>>>   at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
>>>   at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
>>>   at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
>>>   at java.awt.Component.processMouseEvent(Component.java:6533)
>>>   at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
>>>   at java.awt.Component.processEvent(Component.java:6298)
>>>   at java.awt.Container.processEvent(Container.java:2236)
>>>   at java.awt.Component.dispatchEventImpl(Component.java:4889)
>>>   at java.awt.Container.dispatchEventImpl(Container.java:2294)
>>>   at java.awt.Component.dispatchEvent(Component.java:4711)
>>>   at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
>>>   at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
>>>   at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
>>>   at java.awt.Container.dispatchEventImpl(Container.java:2280)
>>>   at java.awt.Window.dispatchEventImpl(Window.java:2746)
>>>   at java.awt.Component.dispatchEvent(Component.java:4711)
>>>   at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
>>>   at java.awt.EventQueue.access$500(EventQueue.java:97)
>>>   at java.awt.EventQueue$3.run(EventQueue.java:709)
>>>   at java.awt.EventQueue$3.run(EventQueue.java:703)
>>>   at java.security.AccessController.doPrivileged(Native Method)
>>>   at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>>>   at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
>>>   at java.awt.EventQueue$4.run(EventQueue.java:731)
>>>   at java.awt.EventQueue$4.run(EventQueue.java:729)
>>>   at java.security.AccessController.doPrivileged(Native Method)
>>>   at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>>>   at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
>>>   at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
>>>   at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
>>>   at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
>>>   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
>>>   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
>>>   at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
>>>
>>> java.security.ProviderException: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_USER_NOT_LOGGED_IN
>>>   at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:591)
>>>   at java.security.Signature$Delegate.engineSign(Signature.java:1207)
>>>   at java.security.Signature.sign(Signature.java:579)
>>>   at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$SignatureOutputStream.getSignature(JcaContentSignerBuilder.java:158)
>>>   at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$1.getSignature(JcaContentSignerBuilder.java:91)
>>>   at org.bouncycastle.cms.SignerInfoGenerator.generate(SignerInfoGenerator.java:220)
>>>   at org.bouncycastle.cms.CMSSignedDataGenerator.generate(CMSSignedDataGenerator.java:177)
>>>   at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:796)
>>>   at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
>>>   at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
>>>   at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
>>>   at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
>>>   at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
>>>   at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
>>>   at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
>>>   at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
>>>   at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
>>>   at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
>>>   at java.awt.Component.processMouseEvent(Component.java:6533)
>>>   at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
>>>   at java.awt.Component.processEvent(Component.java:6298)
>>>   at java.awt.Container.processEvent(Container.java:2236)
>>>   at java.awt.Component.dispatchEventImpl(Component.java:4889)
>>>   at java.awt.Container.dispatchEventImpl(Container.java:2294)
>>>   at java.awt.Component.dispatchEvent(Component.java:4711)
>>>   at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
>>>   at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
>>>   at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
>>>   at java.awt.Container.dispatchEventImpl(Container.java:2280)
>>>   at java.awt.Window.dispatchEventImpl(Window.java:2746)
>>>   at java.awt.Component.dispatchEvent(Component.java:4711)
>>>   at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
>>>   at java.awt.EventQueue.access$500(EventQueue.java:97)
>>>   at java.awt.EventQueue$3.run(EventQueue.java:709)
>>>   at java.awt.EventQueue$3.run(EventQueue.java:703)
>>>   at java.security.AccessController.doPrivileged(Native Method)
>>>   at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>>>   at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
>>>   at java.awt.EventQueue$4.run(EventQueue.java:731)
>>>   at java.awt.EventQueue$4.run(EventQueue.java:729)
>>>   at java.security.AccessController.doPrivileged(Native Method)
>>>   at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>>>   at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
>>>   at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
>>>   at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
>>>   at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
>>>   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
>>>   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
>>>   at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
>>>
>>> sun.security.pkcs11.wrapper.PKCS11Exception: CKR_USER_NOT_LOGGED_IN
>>>   at sun.security.pkcs11.wrapper.PKCS11.C_SignFinal(Native Method)
>>>   at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:553)
>>>   at java.security.Signature$Delegate.engineSign(Signature.java:1207)
>>>   at java.security.Signature.sign(Signature.java:579)
>>>   at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$SignatureOutputStream.getSignature(JcaContentSignerBuilder.java:158)
>>>   at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$1.getSignature(JcaContentSignerBuilder.java:91)
>>>   at org.bouncycastle.cms.SignerInfoGenerator.generate(SignerInfoGenerator.java:220)
>>>   at org.bouncycastle.cms.CMSSignedDataGenerator.generate(CMSSignedDataGenerator.java:177)
>>>   at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:796)
>>>   at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
>>>   at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
>>>   at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
>>>   at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
>>>   at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
>>>   at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
>>>   at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
>>>   at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
>>>   at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
>>>   at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
>>>   at java.awt.Component.processMouseEvent(Component.java:6533)
>>>   at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
>>>   at java.awt.Component.processEvent(Component.java:6298)
>>>   at java.awt.Container.processEvent(Container.java:2236)
>>>   at java.awt.Component.dispatchEventImpl(Component.java:4889)
>>>   at java.awt.Container.dispatchEventImpl(Container.java:2294)
>>>   at java.awt.Component.dispatchEvent(Component.java:4711)
>>>   at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
>>>   at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
>>>   at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
>>>   at java.awt.Container.dispatchEventImpl(Container.java:2280)
>>>   at java.awt.Window.dispatchEventImpl(Window.java:2746)
>>>   at java.awt.Component.dispatchEvent(Component.java:4711)
>>>   at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
>>>   at java.awt.EventQueue.access$500(EventQueue.java:97)
>>>   at java.awt.EventQueue$3.run(EventQueue.java:709)
>>>   at java.awt.EventQueue$3.run(EventQueue.java:703)
>>>   at java.security.AccessController.doPrivileged(Native Method)
>>>   at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>>>   at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
>>>   at java.awt.EventQueue$4.run(EventQueue.java:731)
>>>   at java.awt.EventQueue$4.run(EventQueue.java:729)
>>>   at java.security.AccessController.doPrivileged(Native Method)
>>>   at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>>>   at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
>>>   at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
>>>   at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
>>>   at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
>>>   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
>>>   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
>>>   at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
>>>
>>> In case it matters, Oracle’s jarsigner seems to exhibit the same problem, when one attempts to sign a JAR using key on a PKCS#11 hardware token.
>>> --
>>> Uri Blumenthal
>>> [hidden email]<mailto:[hidden email]>
>>>
>>>
>>
>>
>> --
>>
>>   ---------    CardContact Systems GmbH
>>  |.##> <##.|   Schülerweg 38
>>  |#       #|   D-32429 Minden, Germany
>>  |#       #|   Phone +49 571 56149
>>  |'##> <##'|   http://www.cardcontact.de
>>   ---------    Registergericht Bad Oeynhausen HRB 14880
>>                Geschäftsführer Andreas Schwier
>>
>>
>>
>> --
>>
>>   ---------    CardContact Systems GmbH
>>  |.##> <##.|   Schülerweg 38
>>  |#       #|   D-32429 Minden, Germany
>>  |#       #|   Phone +49 571 56149
>>  |'##> <##'|   http://www.cardcontact.de
>>   ---------    Registergericht Bad Oeynhausen HRB 14880
>>                Geschäftsführer Andreas Schwier
>>
>
> --
> Uri the Great
> [hidden email]
>
>
>
>

--
Uri Blumenthal
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Unable to sign using PKCS#11 tokens

Uri Blumenthal
In reply to this post by Uri Blumenthal

> On Aug 16, 2016, at 18:30 , Uri Blumenthal <[hidden email]> wrote:
>
> On Aug 16, 2016, at 4:28 , Edelhoff, Matthias <[hidden email]> wrote:
>> Hi Uri,
>> Please find some basic sample below. The 32-bit issue I recalled is only relevant for jarsigner
>> not general SunOKCS11 use it seems.
>
> Thank you!
>
>> Please note the comments on absolute paths and backslashes,
>> I am not sure what that translates to on OsX, but I suspect having a sane POSIX compliant
>> Filesystem can only improve matters…
>
> No doubt. ;)
>
>> [1]
>> //NOTE: MEF: SunPKCS11 requires backslashes and an absolute path!
>>                 public String artifactPath = "<absolute path to testartifacts-use backslashes on windows!>”;
>
> What does it mean “path to test artifacts”? What test artifacts? Where/how do I get them?
>
>>                 private String cvP11LibPath = artifactPath+"cvP11.x64.dll";
>>                 private char[] pin = "11111111".toCharArray();
>>                
>>                 @Test
>>                 public void testPKCS11() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
>>                              
>>                                Provider p11Provider = registerP11Provider(cvP11LibPath);
>>                              
>>                                assertTrue(Security.addProvider(p11Provider) > 0);
>>                                KeyStore ks = KeyStore.getInstance("PKCS11");
>>                                ks.load(null, pin);
>>                 }
>>  
>>                 private Provider registerP11Provider(String libPath) throws UnsupportedEncodingException {
>>                                //NOTE: MEF: -Djava.security.debug=sunpkcs11 is needed due to bug : JDK-8039912
>>                                //           see https://bugs.openjdk.java.net/browse/JDK-8039912
>
> Yes, I’ve stumbled upon this problem - and it wasn’t fixed in JDK-1.0.8_102. :-(
>
>>                                String configuration = "name = PKCS11\n"
>>                                                                + "library = "+ libPath + "\n"
>>                                                                + " showInfo=true\n”;
>
> Are both necessary? “showInfo=true” and " -Djava.security.debug=sunpkcs11 “?
>
> Thanks!
>
>>  Von: Uri Blumenthal [mailto:[hidden email]]
>> Gesendet: Montag, 15. August 2016 19:15
>> An: Edelhoff, Matthias <[hidden email]>
>> Cc: [hidden email]
>> Betreff: Re: [dev-crypto] Unable to sign using PKCS#11 tokens
>>  
>> Matthias,
>>  
>> Yes please - it would be quite helpful to see your sample code.
>>  
>> Thanks!
--
Uri Blumenthal
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Unable to sign using PKCS#11 tokens

Andreas Schwier-2
In reply to this post by Uri Blumenthal
Hi Uri,

I've added a build-without-ivy.xml build file (git pull from GITHUB).
You can use that with

ant -f build-without-ivy.xml

Before that you need to download commons-logging-api-1.1.jar to java/lib.

You could also download the Smart Card Shell [1] and take the signed
version of opensc-java.jar from the lib directory. In the same directory
you also find the share objects (libopensc-PKCS11*) and DLLs
(opensc-PKCS11).

Andreas


[1] http://www.openscdp.org/scsh3/download.html

On 08/16/2016 05:47 AM, Uri Blumenthal wrote:

> Andreas,
>
> I’ve installed apache-ivy-2.4.0, but seem to have no luck:
>
> $ ant compile
> Buildfile: /Users/uri/src/opensc-java-andreas/build.xml
>
> init:
>
> BUILD FAILED
> /Users/uri/src/opensc-java-andreas/build.xml:25: The following error occurred while executing this line:
> jar:file:/Library/Java/Extensions/ivy-2.4.0.jar!/org/apache/ivy/ant/antlib.xml:21: typedef A class needed by class org.apache.ivy.ant.IvyAntSettings cannot be found: org/apache/tools/ant/types/DataType
>  using the classloader AntClassLoader[]
>
> Total time: 0 seconds
> $
>
> Could you please help me to get this thing built on Mac OS X?
>
> Thanks!
>
>> On Aug 15, 2016, at 13:17 , Uri Blumenthal <[hidden email]> wrote:
>>
>> Andreas,
>>
>> I’d love to try your opensc-java, but cannot even build it on my Mac OS X. I have Maven and Ant installed/configured - but it seems to want some “ivy”?!
>>
>> How different is your code base compared to https://github.com/OpenSC/OpenSC-Java/tree/master/pkcs11? Because if the latter requires just Maven - I’d much rather stay with it. But if you made significant code improvements, then I’d have no choice.
>>
>>> On Aug 15, 2016, at 4:35 , Andreas Schwier <[hidden email]> wrote:
>>>
>>> Hi,
>>>
>>> we maintain opensc-java at [1], which is the PKCS#11/JNI we use in
>>> OpenSCDP [2].
>>>
>>> It's an alternative to IAIK and SUNPKCS11. The module provides a JCE
>>> interface and a direct P11 interface. We commonly use that module to
>>> test PKCS#11 implementations like OpenSC or the PKCS#11 module for our
>>> SmartCard-HSM [3].
>>>
>>> Andreas
>>>
>>> [1] https://github.com/CardContact/opensc-java
>>> [2] http://www.openscdp.org/
>>> [3] http://www.smartcard-hsm.com/
>>>
>>>
>>> On 08/15/2016 09:05 AM, Edelhoff, Matthias wrote:
>>>> Hi Guys,
>>>> I habe to agree with David on this. Last year we tried using the p11 provider from the jre and deemed it not production ready. From what I remember you have to use 32 Bit middelware (at least on Windows) and switch on verbose logging. The Second is due to a 10 year Old bug which only enumerates all slots if in verbose mode.
>>>>
>>>> With these two caveats you can use the provider in bc or Basic operations, but have little to no control over the details like Session Handling and pin caching.
>>>>
>>>> There is an Alternative commerical Provider by aiak. In fact the jre Provider is i think an Old Version of this. Anyway the aiak guys also provider a oss thin Java Wrapper around pkcs11, which Can be easily used to build your own signer.
>>>>
>>>> I will only get to some Sample Code on how to use the jre p11 procider  tomorrow, so let me know if you are interested.
>>>>
>>>> Cheers,
>>>> Matthias
>>>>
>>>> PS:
>>>> I wrote a Bit of gradle Code to automate BC provider signing via p11. So if you just want to use jarsigner there this works quite well, with the above caveats of course..
>>>>
>>>> Am 15. Aug. 2016, um 00:18, David Hook <[hidden email]<mailto:[hidden email]>> schrieb:
>>>>
>>>> Initially I thought the way to deal with this was with a custom implementation of the ContentSigner interface. I'm not sure if you'll be able to use the PKC#11 provider as well - you may need something a little more local to the hardware to guarantee VERIFY PIN happens just be the signature request. Doing this would certainly work.
>>>>
>>>> Looking at the docs for the PKCS11 provider though, I think you should just need to add:
>>>>
>>>> KeyStore ks = KeyStore.getInstance("PKCS11");
>>>> ks.load(null, pin);
>>>>
>>>> to the right place in the JcaContentSignerBuilder as a starting point as it appears it will trigger the VERIFY PIN (although it may do something else next, in which case it's back to the custom implementation). I have to admit I've never tried it - this might work just before the generate() method as well (in which case no changes are required).
>>>>
>>>> Regards,
>>>>
>>>> David
>>>>
>>>> On 14/08/16 22:10, Uri Blumenthal wrote:
>>>> I’ve hit an interesting problem. I use PIV tokens (YubiKey NEO<https://www.yubico.com/products/yubikey-hardware/> devices and DOD CAC). They have four asymmetric key-pair slots:
>>>>
>>>> 1.  PIV Authentication - used for TLS client authentication, and smart card login to computers.
>>>> 2.  Digital Signature - used to digitally sign email, sign documents (like PDF with Adobe Acrobat), sign code.
>>>> 3.  Key Management - used to encrypt email, documents, and for encrypted filesystems.
>>>> 4.  Card Authentication - used for door locks and such (like with badge readers that unlock doors and allow building entrance).
>>>>
>>>> The point is that only (2) is appropriate for document signing. And PIV standard states that to use this key (to sign something), the request to sign must be immediately preceded by a token login (VERIFY PIN) command - if any operation with this hardware token is performed between the login and the signature request, the signature request fails, as the appended log shows.
>>>>
>>>> So I’d like a mechanism that would allow the following (you can see what it is by the trace) to work:
>>>>
>>>>
>>>> net.sf.keystore_explorer.crypto.CryptoException: Block signing failed.
>>>>   at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:805)
>>>>   at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
>>>>   at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
>>>>   at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
>>>>   at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
>>>>   at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
>>>>   at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
>>>>   at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
>>>>   at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
>>>>   at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
>>>>   at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
>>>>   at java.awt.Component.processMouseEvent(Component.java:6533)
>>>>   at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
>>>>   at java.awt.Component.processEvent(Component.java:6298)
>>>>   at java.awt.Container.processEvent(Container.java:2236)
>>>>   at java.awt.Component.dispatchEventImpl(Component.java:4889)
>>>>   at java.awt.Container.dispatchEventImpl(Container.java:2294)
>>>>   at java.awt.Component.dispatchEvent(Component.java:4711)
>>>>   at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
>>>>   at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
>>>>   at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
>>>>   at java.awt.Container.dispatchEventImpl(Container.java:2280)
>>>>   at java.awt.Window.dispatchEventImpl(Window.java:2746)
>>>>   at java.awt.Component.dispatchEvent(Component.java:4711)
>>>>   at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
>>>>   at java.awt.EventQueue.access$500(EventQueue.java:97)
>>>>   at java.awt.EventQueue$3.run(EventQueue.java:709)
>>>>   at java.awt.EventQueue$3.run(EventQueue.java:703)
>>>>   at java.security.AccessController.doPrivileged(Native Method)
>>>>   at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>>>>   at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
>>>>   at java.awt.EventQueue$4.run(EventQueue.java:731)
>>>>   at java.awt.EventQueue$4.run(EventQueue.java:729)
>>>>   at java.security.AccessController.doPrivileged(Native Method)
>>>>   at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>>>>   at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
>>>>   at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
>>>>   at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
>>>>   at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
>>>>   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
>>>>   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
>>>>   at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
>>>>
>>>> java.security.ProviderException: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_USER_NOT_LOGGED_IN
>>>>   at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:591)
>>>>   at java.security.Signature$Delegate.engineSign(Signature.java:1207)
>>>>   at java.security.Signature.sign(Signature.java:579)
>>>>   at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$SignatureOutputStream.getSignature(JcaContentSignerBuilder.java:158)
>>>>   at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$1.getSignature(JcaContentSignerBuilder.java:91)
>>>>   at org.bouncycastle.cms.SignerInfoGenerator.generate(SignerInfoGenerator.java:220)
>>>>   at org.bouncycastle.cms.CMSSignedDataGenerator.generate(CMSSignedDataGenerator.java:177)
>>>>   at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:796)
>>>>   at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
>>>>   at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
>>>>   at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
>>>>   at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
>>>>   at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
>>>>   at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
>>>>   at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
>>>>   at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
>>>>   at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
>>>>   at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
>>>>   at java.awt.Component.processMouseEvent(Component.java:6533)
>>>>   at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
>>>>   at java.awt.Component.processEvent(Component.java:6298)
>>>>   at java.awt.Container.processEvent(Container.java:2236)
>>>>   at java.awt.Component.dispatchEventImpl(Component.java:4889)
>>>>   at java.awt.Container.dispatchEventImpl(Container.java:2294)
>>>>   at java.awt.Component.dispatchEvent(Component.java:4711)
>>>>   at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
>>>>   at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
>>>>   at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
>>>>   at java.awt.Container.dispatchEventImpl(Container.java:2280)
>>>>   at java.awt.Window.dispatchEventImpl(Window.java:2746)
>>>>   at java.awt.Component.dispatchEvent(Component.java:4711)
>>>>   at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
>>>>   at java.awt.EventQueue.access$500(EventQueue.java:97)
>>>>   at java.awt.EventQueue$3.run(EventQueue.java:709)
>>>>   at java.awt.EventQueue$3.run(EventQueue.java:703)
>>>>   at java.security.AccessController.doPrivileged(Native Method)
>>>>   at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>>>>   at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
>>>>   at java.awt.EventQueue$4.run(EventQueue.java:731)
>>>>   at java.awt.EventQueue$4.run(EventQueue.java:729)
>>>>   at java.security.AccessController.doPrivileged(Native Method)
>>>>   at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>>>>   at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
>>>>   at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
>>>>   at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
>>>>   at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
>>>>   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
>>>>   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
>>>>   at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
>>>>
>>>> sun.security.pkcs11.wrapper.PKCS11Exception: CKR_USER_NOT_LOGGED_IN
>>>>   at sun.security.pkcs11.wrapper.PKCS11.C_SignFinal(Native Method)
>>>>   at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:553)
>>>>   at java.security.Signature$Delegate.engineSign(Signature.java:1207)
>>>>   at java.security.Signature.sign(Signature.java:579)
>>>>   at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$SignatureOutputStream.getSignature(JcaContentSignerBuilder.java:158)
>>>>   at org.bouncycastle.operator.jcajce.JcaContentSignerBuilder$1.getSignature(JcaContentSignerBuilder.java:91)
>>>>   at org.bouncycastle.cms.SignerInfoGenerator.generate(SignerInfoGenerator.java:220)
>>>>   at org.bouncycastle.cms.CMSSignedDataGenerator.generate(CMSSignedDataGenerator.java:177)
>>>>   at net.sf.keystore_explorer.crypto.signing.JarSigner.createSignatureBlock(JarSigner.java:796)
>>>>   at net.sf.keystore_explorer.crypto.signing.JarSigner.sign(JarSigner.java:302)
>>>>   at net.sf.keystore_explorer.gui.actions.SignJarAction.doAction(SignJarAction.java:119)
>>>>   at net.sf.keystore_explorer.gui.actions.KeyStoreExplorerAction.actionPerformed(KeyStoreExplorerAction.java:92)
>>>>   at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
>>>>   at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
>>>>   at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
>>>>   at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
>>>>   at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
>>>>   at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
>>>>   at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
>>>>   at java.awt.Component.processMouseEvent(Component.java:6533)
>>>>   at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
>>>>   at java.awt.Component.processEvent(Component.java:6298)
>>>>   at java.awt.Container.processEvent(Container.java:2236)
>>>>   at java.awt.Component.dispatchEventImpl(Component.java:4889)
>>>>   at java.awt.Container.dispatchEventImpl(Container.java:2294)
>>>>   at java.awt.Component.dispatchEvent(Component.java:4711)
>>>>   at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
>>>>   at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
>>>>   at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
>>>>   at java.awt.Container.dispatchEventImpl(Container.java:2280)
>>>>   at java.awt.Window.dispatchEventImpl(Window.java:2746)
>>>>   at java.awt.Component.dispatchEvent(Component.java:4711)
>>>>   at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
>>>>   at java.awt.EventQueue.access$500(EventQueue.java:97)
>>>>   at java.awt.EventQueue$3.run(EventQueue.java:709)
>>>>   at java.awt.EventQueue$3.run(EventQueue.java:703)
>>>>   at java.security.AccessController.doPrivileged(Native Method)
>>>>   at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>>>>   at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
>>>>   at java.awt.EventQueue$4.run(EventQueue.java:731)
>>>>   at java.awt.EventQueue$4.run(EventQueue.java:729)
>>>>   at java.security.AccessController.doPrivileged(Native Method)
>>>>   at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
>>>>   at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
>>>>   at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
>>>>   at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
>>>>   at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
>>>>   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
>>>>   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
>>>>   at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
>>>>
>>>> In case it matters, Oracle’s jarsigner seems to exhibit the same problem, when one attempts to sign a JAR using key on a PKCS#11 hardware token.
>>>> --
>>>> Uri Blumenthal
>>>> [hidden email]<mailto:[hidden email]>
>>>>
>>>>
>>>
>>>
>>> --
>>>
>>>   ---------    CardContact Systems GmbH
>>>  |.##> <##.|   Schülerweg 38
>>>  |#       #|   D-32429 Minden, Germany
>>>  |#       #|   Phone +49 571 56149
>>>  |'##> <##'|   http://www.cardcontact.de
>>>   ---------    Registergericht Bad Oeynhausen HRB 14880
>>>                Geschäftsführer Andreas Schwier
>>>
>>>
>>>
>>> --
>>>
>>>   ---------    CardContact Systems GmbH
>>>  |.##> <##.|   Schülerweg 38
>>>  |#       #|   D-32429 Minden, Germany
>>>  |#       #|   Phone +49 571 56149
>>>  |'##> <##'|   http://www.cardcontact.de
>>>   ---------    Registergericht Bad Oeynhausen HRB 14880
>>>                Geschäftsführer Andreas Schwier
>>>
>>
>> --
>> Uri the Great
>> [hidden email]
>>
>>
>>
>>
>
> --
> Uri Blumenthal
> [hidden email]
>


--

    ---------    CardContact Systems GmbH
   |.##> <##.|   Schülerweg 38
   |#       #|   D-32429 Minden, Germany
   |#       #|   Phone +49 571 56149
   |'##> <##'|   http://www.cardcontact.de
    ---------    Registergericht Bad Oeynhausen HRB 14880
                 Geschäftsführer Andreas Schwier

Reply | Threaded
Open this post in threaded view
|

Re: Unable to sign using PKCS#11 tokens

Uri Blumenthal
On Aug 17, 2016, at 8:53 , Andreas Schwier <[hidden email]> wrote:
Hi Uri,

I've added a build-without-ivy.xml build file (git pull from GITHUB).
You can use that with

ant -f build-without-ivy.xml

Andreas, thank you!  Yes, now the build works.

Before that you need to download commons-logging-api-1.1.jar to java/lib.

Well, it so happens that I keep commons-logging-1.2 on my classpath. It seemed to work. ;)

You could also download the Smart Card Shell [1] and take the signed
version of opensc-java.jar from the lib directory. In the same directory
you also find the share objects (libopensc-PKCS11*) and DLLs
(opensc-PKCS11).

So in your opinion the following console log indicates correct build completion?

$ ant -f build-without-ivy.xml
Buildfile: /Users/uri/src/opensc-java-andreas/build-without-ivy.xml

compile:
    [javac] /Users/uri/src/opensc-java-andreas/build-without-ivy.xml:30: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 38 source files to /Users/uri/src/opensc-java-andreas/build/bin
    [javac] Note: /Users/uri/src/opensc-java-andreas/java/src/org/opensc/pkcs11/spi/PKCS11SignatureSpi.java uses or overrides a deprecated API.
    [javac] Note: Recompile with -Xlint:deprecation for details.
    [javac] Note: /Users/uri/src/opensc-java-andreas/java/src/org/opensc/pkcs11/PKCS11Provider.java uses unchecked or unsafe operations.
    [javac] Note: Recompile with -Xlint:unchecked for details.

dist:
    [mkdir] Created dir: /Users/uri/src/opensc-java-andreas/build/lib
      [jar] Building jar: /Users/uri/src/opensc-java-andreas/build/lib/opensc-java.jar
     [copy] Copying 5 files to /Users/uri/src/opensc-java-andreas/build/lib
   [delete] Deleting directory /Users/uri/src/opensc-java-andreas/build/bin

BUILD SUCCESSFUL
Total time: 2 seconds
$ ant -f build-without-ivy.xml
Buildfile: /Users/uri/src/opensc-java-andreas/build-without-ivy.xml

compile:
    [javac] /Users/uri/src/opensc-java-andreas/build-without-ivy.xml:30: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 38 source files to /Users/uri/src/opensc-java-andreas/build/bin
    [javac] Note: /Users/uri/src/opensc-java-andreas/java/src/org/opensc/pkcs11/spi/PKCS11SignatureSpi.java uses or overrides a deprecated API.
    [javac] Note: Recompile with -Xlint:deprecation for details.
    [javac] Note: /Users/uri/src/opensc-java-andreas/java/src/org/opensc/pkcs11/PKCS11Provider.java uses unchecked or unsafe operations.
    [javac] Note: Recompile with -Xlint:unchecked for details.

dist:
    [mkdir] Created dir: /Users/uri/src/opensc-java-andreas/build/lib
      [jar] Building jar: /Users/uri/src/opensc-java-andreas/build/lib/opensc-java.jar
     [copy] Copying 5 files to /Users/uri/src/opensc-java-andreas/build/lib
   [delete] Deleting directory /Users/uri/src/opensc-java-andreas/build/bin

BUILD SUCCESSFUL
Total time: 2 seconds
$ ll build/lib
total 880
drwxr-xr-x  8 uri  staff     272 Aug 17 20:10 ./
drwxr-xr-x  3 uri  staff     102 Aug 17 20:10 ../
-rw-r--r--  1 uri  staff   74737 Aug 17 20:10 libopensc-PKCS11-amd64.so
-rw-r--r--  1 uri  staff   76614 Aug 17 20:10 libopensc-PKCS11-i386.so
-rw-r--r--  1 uri  staff   60120 Aug 17 20:10 libopensc-PKCS11-x86_64.dylib
-rw-r--r--  1 uri  staff   58368 Aug 17 20:10 opensc-PKCS11-amd64.dll
-rw-r--r--  1 uri  staff  110592 Aug 17 20:10 opensc-PKCS11-x86.dll
-rw-r--r--  1 uri  staff   59095 Aug 17 20:10 opensc-java.jar
$

The instructions want to set jcesigner.cfg for Ant to sign the JAR for me. But I can sign it myself later on, correct? Also, the jar should have JCE status, so the signing key should be certified by Sun/Oracle, correct?

Would you have some use examples? Or should I take those from the original OpenSC-Java project?
--
Uri Blumenthal

Reply | Threaded
Open this post in threaded view
|

Re: Unable to sign using PKCS#11 tokens

Uri Blumenthal
Andreas,

I’ve found a few problems with the build, and filed issues on your GitHub repo.

In short, (a) the script does not initiate JNI rebuild, but uses the prebuilt copy instead (bad because it got the library path wrong), and (b) the script doesn’t re-create the “java/lib” directory that “ant clean” removes.
--
Uri Blumenthal
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Unable to sign using PKCS#11 tokens

Uri Blumenthal
Andreas,

Trying to test your OpenSC-Java fork (adapting the tests from the original OpenSC/OpenSC-Java/pkcs11-test), I stumbled upon more problems. Not sure whether they’re caused by me not understanding how to use it, or the issues with the library itself. In any case, I’d really appreciate your guidance.

Here are the problems:

1. KeyStore test - retrieves all the certificates (though it assigns to them strange aliases based on CN), but fails to get certificate chain (that call returns null).

Code:
  Certificate certificate = ks.getCertificate(alias); // works
  printCert( (X509Certificate) certificate ); // works
  Certificate [] chain = ks.getCertificateChain(alias); // returns chain==null

2. Signature test fails on the last operation (actually performing signature on the token).

Code:
  Signature sig = Signature.getInstance(“SHA256withRSA”
                                      /* , "OpenSC-PKCS11"
                                        provider*/);
  sig.initSign((PrivateKey)key);
  System.out.println("sig.provider=" +
                     sig.getProvider().getName());

  sig.update(this.testData);
  byte[] signature = sig.sign(); // line #166

Stack trace:
key.class=class org.opensc.pkcs11.wrap.PKCS11NeRSAPrivateKey
sig.provider=OpenSC-PKCS11
Exception in thread "main" java.lang.NullPointerException
        at org.opensc.pkcs11.wrap.PKCS11Session.sign(Unknown Source)
        at org.opensc.pkcs11.spi.PKCS11SignatureSpi.engineSign(Unknown Source)
        at java.security.Signature$Delegate.engineSign(Signature.java:1207)
        at java.security.Signature.sign(Signature.java:579)
        at burrow.pkcs11.PKCS11Test2.testSignature(PKCS11Test2.java:166)
        at burrow.pkcs11.PKCS11Test2.main(PKCS11Test2.java:250)
        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:140)


3. Decryption test fails similarly to above.

Code:
  System.out.println("key.class=" + key.getClass());
  Cipher enc = Cipher.getInstance("RSA/ECB/PKCS1Padding");
  enc.init(Cipher.ENCRYPT_MODE, certificate); // this is the line #206
  byte[] encData = enc.doFinal(this.testData);

Stack trace:
key.class=class org.opensc.pkcs11.wrap.PKCS11NeRSAPrivateKey
Exception in thread "main" java.security.InvalidKeyException: Wrong key usage
        at javax.crypto.Cipher.init(Cipher.java:1712)
        at javax.crypto.Cipher.init(Cipher.java:1618)
        at burrow.pkcs11.PKCS11Test2.testDecryption(PKCS11Test2.java:206)
        at burrow.pkcs11.PKCS11Test2.main(PKCS11Test2.java:244)
        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:140)

Key usage:
ExtendedKeyUsages [
  emailProtection
  1.3.6.1.4.1.311.10.3.4
  anyExtendedKeyUsage
]

[3]: ObjectId: 2.5.29.15 Criticality=true
KeyUsage [
  Key_Encipherment
]


> On Aug 18, 2016, at 23:37 , Uri Blumenthal <[hidden email]> wrote:
>
> Andreas,
>
> I’ve found a few problems with the build, and filed issues on your GitHub repo.
>
> In short, (a) the script does not initiate JNI rebuild, but uses the prebuilt copy instead (bad because it got the library path wrong), and (b) the script doesn’t re-create the “java/lib” directory that “ant clean” removes.
> --
> Uri Blumenthal
> [hidden email]
>

--
Uri Blumenthal
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Unable to sign using PKCS#11 tokens

Andreas Schwier-2
Hi Uri,

it's difficult to tell, what is causing your problem if we don't know
whats keys you have on your device.

Can you run pkcs11-tool --module <modname> -O --login

to see the PKCS11 objects ?

As said before, we are using that module with our SmartCard-HSM and so
far it has served us well.

Regarding your issues

1. getCertificateChain returns null if it detects an error. Can you
enable logging or debug into the PKCS11KeyStoreSpi ?

2. and 3. Can you enable PKCS11SPY from OpenSC to see what PKCS#11 calls
are used and what they report.

Andreas

On 08/21/2016 07:03 AM, Uri Blumenthal wrote:

> Andreas,
>
> Trying to test your OpenSC-Java fork (adapting the tests from the original OpenSC/OpenSC-Java/pkcs11-test), I stumbled upon more problems. Not sure whether they’re caused by me not understanding how to use it, or the issues with the library itself. In any case, I’d really appreciate your guidance.
>
> Here are the problems:
>
> 1. KeyStore test - retrieves all the certificates (though it assigns to them strange aliases based on CN), but fails to get certificate chain (that call returns null).
>
> Code:
>   Certificate certificate = ks.getCertificate(alias); // works
>   printCert( (X509Certificate) certificate ); // works
>   Certificate [] chain = ks.getCertificateChain(alias); // returns chain==null
>
> 2. Signature test fails on the last operation (actually performing signature on the token).
>
> Code:
>   Signature sig = Signature.getInstance(“SHA256withRSA”
>                                       /* , "OpenSC-PKCS11"
>                                         provider*/);
>   sig.initSign((PrivateKey)key);
>   System.out.println("sig.provider=" +
>                      sig.getProvider().getName());
>
>   sig.update(this.testData);
>   byte[] signature = sig.sign(); // line #166
>
> Stack trace:
> key.class=class org.opensc.pkcs11.wrap.PKCS11NeRSAPrivateKey
> sig.provider=OpenSC-PKCS11
> Exception in thread "main" java.lang.NullPointerException
> at org.opensc.pkcs11.wrap.PKCS11Session.sign(Unknown Source)
> at org.opensc.pkcs11.spi.PKCS11SignatureSpi.engineSign(Unknown Source)
> at java.security.Signature$Delegate.engineSign(Signature.java:1207)
> at java.security.Signature.sign(Signature.java:579)
> at burrow.pkcs11.PKCS11Test2.testSignature(PKCS11Test2.java:166)
> at burrow.pkcs11.PKCS11Test2.main(PKCS11Test2.java:250)
> 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:140)
>
>
> 3. Decryption test fails similarly to above.
>
> Code:
>   System.out.println("key.class=" + key.getClass());
>   Cipher enc = Cipher.getInstance("RSA/ECB/PKCS1Padding");
>   enc.init(Cipher.ENCRYPT_MODE, certificate); // this is the line #206
>   byte[] encData = enc.doFinal(this.testData);
>
> Stack trace:
> key.class=class org.opensc.pkcs11.wrap.PKCS11NeRSAPrivateKey
> Exception in thread "main" java.security.InvalidKeyException: Wrong key usage
> at javax.crypto.Cipher.init(Cipher.java:1712)
> at javax.crypto.Cipher.init(Cipher.java:1618)
> at burrow.pkcs11.PKCS11Test2.testDecryption(PKCS11Test2.java:206)
> at burrow.pkcs11.PKCS11Test2.main(PKCS11Test2.java:244)
> 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:140)
>
> Key usage:
> ExtendedKeyUsages [
>   emailProtection
>   1.3.6.1.4.1.311.10.3.4
>   anyExtendedKeyUsage
> ]
>
> [3]: ObjectId: 2.5.29.15 Criticality=true
> KeyUsage [
>   Key_Encipherment
> ]
>
>
>> On Aug 18, 2016, at 23:37 , Uri Blumenthal <[hidden email]> wrote:
>>
>> Andreas,
>>
>> I’ve found a few problems with the build, and filed issues on your GitHub repo.
>>
>> In short, (a) the script does not initiate JNI rebuild, but uses the prebuilt copy instead (bad because it got the library path wrong), and (b) the script doesn’t re-create the “java/lib” directory that “ant clean” removes.
>> --
>> Uri Blumenthal
>> [hidden email]
>>
>
> --
> Uri Blumenthal
> [hidden email]
>


--

    ---------    CardContact Systems GmbH
   |.##> <##.|   Schülerweg 38
   |#       #|   D-32429 Minden, Germany
   |#       #|   Phone +49 571 56149
   |'##> <##'|   http://www.cardcontact.de
    ---------    Registergericht Bad Oeynhausen HRB 14880
                 Geschäftsführer Andreas Schwier

Reply | Threaded
Open this post in threaded view
|

Re: Unable to sign using PKCS#11 tokens

Uri Blumenthal
On Aug 22, 2016, at 2:53 , Andreas Schwier <[hidden email]> wrote:
> Hi Uri,
>
> it's difficult to tell, what is causing your problem if we don't know
> whats keys you have on your device.
> Can you run "pkcs11-tool --module <modname> -O —login" to see the PKCS11 objects ?

I certainly can:

$ pkcs11-tool -O -l
Using slot 0 with a present token (0x0)
Logging in to "PIV Card Holder pin: PIV_II".
Please enter User PIN:
Private Key Object; RSA
  label:      PIV AUTH key
  ID:         01
  Usage:      decrypt, sign, unwrap
Public Key Object; RSA 2048 bits
  label:      PIV AUTH pubkey
  ID:         01
  Usage:      encrypt, verify, wrap
Certificate Object, type = X.509 cert
  label:      Certificate for PIV Authentication
  ID:         01
Private Key Object; RSA
  label:      SIGN key
  ID:         02
  Usage:      decrypt, sign, non-repudiation
  Access:     always authenticate
Public Key Object; RSA 2048 bits
  label:      SIGN pubkey
  ID:         02
  Usage:      encrypt, verify
Certificate Object, type = X.509 cert
  label:      Certificate for Digital Signature
  ID:         02
Private Key Object; RSA
  label:      KEY MAN key
  ID:         03
  Usage:      decrypt, unwrap
Public Key Object; RSA 2048 bits
  label:      KEY MAN pubkey
  ID:         03
  Usage:      encrypt, wrap
Certificate Object, type = X.509 cert
  label:      Certificate for Key Management
  ID:         03
Data object 1614881296
  label:          'Cardholder Fingerprints'
  application:    'Cardholder Fingerprints'
  app_id:         2.16.840.1.101.3.7.2.96.16
  flags:           private
Data object 1614881392
  label:          'Printed Information'
  application:    'Printed Information'
  app_id:         2.16.840.1.101.3.7.2.48.1
  flags:           private
Data object 1614881488
  label:          'Cardholder Facial Image'
  application:    'Cardholder Facial Image'
  app_id:         2.16.840.1.101.3.7.2.96.48
  flags:           private
Private Key Object; RSA
  label:      CARD AUTH key
  ID:         04
  Usage:      sign
Public Key Object; RSA 2048 bits
  label:      CARD AUTH pubkey
  ID:         04
  Usage:      verify
Certificate Object, type = X.509 cert
  label:      Certificate for Card Authentication
  ID:         04
Data object 1614884224
  label:          'Card Capability Container'
  application:    'Card Capability Container'
  app_id:         2.16.840.1.101.3.7.1.219.0
  flags:          <empty>
Data object 1614878464
  label:          'Card Holder Unique Identifier'
  application:    'Card Holder Unique Identifier'
  app_id:         2.16.840.1.101.3.7.2.48.0
  flags:          <empty>
Data object 1614878560
  label:          'Unsigned Card Holder Unique Identifier'
  application:    'Unsigned Card Holder Unique Identifier'
  app_id:         2.16.840.1.101.3.7.2.48.2
  flags:          <empty>
Data object 1614878656
  label:          'X.509 Certificate for PIV Authentication'
  application:    'X.509 Certificate for PIV Authentication'
  app_id:         2.16.840.1.101.3.7.2.1.1
  flags:          <empty>
Data object 1614881584
  label:          'X.509 Certificate for Digital Signature'
  application:    'X.509 Certificate for Digital Signature'
  app_id:         2.16.840.1.101.3.7.2.1.0
  flags:          <empty>
Data object 1614881680
  label:          'X.509 Certificate for Key Management'
  application:    'X.509 Certificate for Key Management'
  app_id:         2.16.840.1.101.3.7.2.1.2
  flags:          <empty>
Data object 1614881776
  label:          'X.509 Certificate for Card Authentication'
  application:    'X.509 Certificate for Card Authentication'
  app_id:         2.16.840.1.101.3.7.2.5.0
  flags:          <empty>
Data object 1614881872
  label:          'Security Object'
  application:    'Security Object'
  app_id:         2.16.840.1.101.3.7.2.144.0
  flags:          <empty>
Data object 1614881968
  label:          'Discovery Object'
  application:    'Discovery Object'
  app_id:         2.16.840.1.101.3.7.2.96.80
  flags:          <empty>
$


> As said before, we are using that module with our SmartCard-HSM and so
> far it has served us well.

I realize that, and am trying to figure why it does not talk to other PIV tokens, such as US DoD CAC and YubiKey. I hope you can help to get this library to work with our tokens, and maybe also to improve some rough edges that I reported.

> Regarding your issues
>
> 1. getCertificateChain returns null if it detects an error. Can you
> enable logging or debug into the PKCS11KeyStoreSpi ?

Will try (not sure I understand what it is that you’re asking, or how to accomplish it).

In the meanwhile I can add one more thing to this:

Alias name: Certificate for Digital Signature
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: . . . . .

As you can see, the chain is of length 1, i.e. it contains only one certificate, not the entire chain.

> 2. and 3. Can you enable PKCS11SPY from OpenSC to see what PKCS#11 calls
> are used and what they report.

I’ve got that - will email you directly, because the files are too large for this mailing list (the server bounces posts larger than 50K). I’ll also email you the source of the program that produced those.

1: C_Initialize
2016-08-22 23:31:13.288
[in] pInitArgs = 0x127aa60b0
     flags: 2
       CKF_OS_LOCKING_OK
Returned:  0 CKR_OK

2: C_GetInfo
2016-08-22 23:31:14.039
[out] pInfo:
      cryptokiVersion:         2.20
      manufacturerID:         'OpenSC Project                  '
      flags:                   0
      libraryDescription:     'OpenSC smartcard framework      '
      libraryVersion:          0.16
Returned:  0 CKR_OK
OpenSC-PKCS11 provider added.

About to test KeyStore...

3: C_GetSlotList
2016-08-22 23:31:14.670
[in] tokenPresent = 0x0
[out] pSlotList:
Count is 1
[out] *pulCount = 0x1
Returned:  0 CKR_OK

4: C_GetSlotList
2016-08-22 23:31:14.672
[in] tokenPresent = 0x0
[out] pSlotList:
Slot 0
[out] *pulCount = 0x1
Returned:  0 CKR_OK

5: C_GetSlotInfo
2016-08-22 23:31:14.673
[in] slotID = 0x0
[out] pInfo:
      slotDescription:        'Yubico Yubikey NEO OTP+U2F+CCID '
                              '                                '
      manufacturerID:         'Yubico                          '
      hardwareVersion:         3.70
      firmwareVersion:         0.0
      flags:                   7
        CKF_TOKEN_PRESENT                
        CKF_REMOVABLE_DEVICE            
        CKF_HW_SLOT                      
Returned:  0 CKR_OK

6: C_GetTokenInfo
2016-08-22 23:31:14.674
[in] slotID = 0x0
[out] pInfo:
      label:                  'PIV Card Holder pin: PIV_II     '
      manufacturerID:         'piv_II                          '
      model:                  'PKCS#15 emulated'
      serialNumber:           '93c28cbc872b0f77'
      ulMaxSessionCount:       0
      ulSessionCount:          0
      ulMaxRwSessionCount:     0
      ulRwSessionCount:        0
      ulMaxPinLen:             8
      ulMinPinLen:             4
      ulTotalPublicMemory:     -1
      ulFreePublicMemory:      -1
      ulTotalPrivateMemory:    -1
      ulFreePrivateMemory:     -1
      hardwareVersion:         0.0
      firmwareVersion:         0.0
      time:                   '                '
      flags:                   40d
        CKF_RNG                          
        CKF_LOGIN_REQUIRED              
        CKF_USER_PIN_INITIALIZED        
        CKF_TOKEN_INITIALIZED            
Returned:  0 CKR_OK

7: C_OpenSession
2016-08-22 23:31:14.690
[in] slotID = 0x0
[in] flags = 0x4
pApplication=0x0
Notify=0x0
[out] *phSession = 0x7fe37a62b610
Returned:  0 CKR_OK

8: C_Login
2016-08-22 23:31:24.781
[in] hSession = 0x7fe37a62b610
[in] userType = CKU_USER
[in] pPin[ulPinLen] 0000700000218b40 / 6
    00000000  31 32 33 34 35 36 37 38     12345678          
Returned:  0 CKR_OK

9: C_FindObjectsInit
2016-08-22 23:31:24.919
[in] hSession = 0x7fe37a62b610
[in] pTemplate[1]:
    CKA_CLASS             CKO_PRIVATE_KEY      
Returned:  0 CKR_OK

10: C_FindObjects
2016-08-22 23:31:24.920
[in] hSession = 0x7fe37a62b610
[in] ulMaxObjectCount = 0x20
[out] ulObjectCount = 0x4
Object 0x7fe37a4054e0 matches
Object 0x7fe37a4056a0 matches
Object 0x7fe37a405870 matches
Object 0x7fe37a405ce0 matches
Returned:  0 CKR_OK

11: C_FindObjectsFinal
2016-08-22 23:31:24.920
[in] hSession = 0x7fe37a62b610
Returned:  0 CKR_OK

12: C_GetAttributeValue
2016-08-22 23:31:24.920
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a4054e0
[in] pTemplate[1]:
    CKA_KEY_TYPE          0000700000218bc8 / 8
[out] pTemplate[1]:
    CKA_KEY_TYPE          CKK_RSA            
Returned:  0 CKR_OK

13: C_GetAttributeValue
2016-08-22 23:31:24.921
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a4054e0
[in] pTemplate[1]:
    CKA_EXTRACTABLE       0000700000218bcf / 1
[out] pTemplate[1]:
    CKA_EXTRACTABLE       False
Returned:  0 CKR_OK

14: C_GetAttributeValue
2016-08-22 23:31:24.921
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a4054e0
[in] pTemplate[1]:
    CKA_ID                0000000000000000 / 0
[out] pTemplate[1]:
    CKA_ID                0000000000000000 / 1
Returned:  0 CKR_OK

15: C_GetAttributeValue
2016-08-22 23:31:24.921
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a4054e0
[in] pTemplate[1]:
    CKA_ID                0000700000218940 / 1
[out] pTemplate[1]:
    CKA_ID                0000700000218940 / 1
    00000000  01                                               .              
Returned:  0 CKR_OK

16: C_GetAttributeValue
2016-08-22 23:31:24.921
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a4054e0
[in] pTemplate[1]:
    CKA_LABEL             0000000000000000 / 0
[out] pTemplate[1]:
    CKA_LABEL             0000000000000000 / 12
Returned:  0 CKR_OK

17: C_GetAttributeValue
2016-08-22 23:31:24.921
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a4054e0
[in] pTemplate[1]:
    CKA_LABEL             0000700000218950 / 12
[out] pTemplate[1]:
    CKA_LABEL             0000700000218950 / 12
    50495620 41555448 206B6579
     P I V .  A U T H  . k e y
Returned:  0 CKR_OK

18: C_GetAttributeValue
2016-08-22 23:31:24.922
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a4054e0
[in] pTemplate[1]:
    CKA_KEY_TYPE          00007000002189d8 / 8
[out] pTemplate[1]:
    CKA_KEY_TYPE          CKK_RSA            
Returned:  0 CKR_OK

19: C_GetAttributeValue
2016-08-22 23:31:24.922
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a4054e0
[in] pTemplate[1]:
    CKA_MODULUS_BITS      00007000002189d8 / 8
[out] pTemplate[1]:
    CKA_MODULUS_BITS      00007000002189d8 / 8
    00000000  00 08 00 00 00 00 00 00                          ........        
Returned:  0 CKR_OK

20: C_GetAttributeValue
2016-08-22 23:31:24.922
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a4054e0
[in] pTemplate[1]:
    CKA_SENSITIVE         0000700000218a5f / 1
[out] pTemplate[1]:
    CKA_SENSITIVE         True
Returned:  0 CKR_OK

21: C_GetAttributeValue
2016-08-22 23:31:24.922
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a4054e0
[in] pTemplate[1]:
    CKA_MODULUS           0000000000000000 / 0
[out] pTemplate[1]:
    CKA_MODULUS           0000000000000000 / 256
Returned:  0 CKR_OK

22: C_GetAttributeValue
2016-08-22 23:31:24.922
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a4054e0
[in] pTemplate[1]:
    CKA_MODULUS           00007000002189e0 / 256
[out] pTemplate[1]:
    CKA_MODULUS           00007000002189e0 / 256
    00000000  9D 78 A2 BF 06 FD 20 19 1B 14 F1 F6 7A BE 1B 01  .x.... .....z...
    00000010  B1 9F E7 EF 82 64 D6 E1 3D 7D 94 E9 86 57 82 F7  .....d..=}...W..
    . . . . .
    000000E0  26 03 40 43 7E D2 9D DA 2D 09 76 89 CE 3E 44 A3  &.@C~...-.v..>D.
    000000F0  F2 55 C6 FA 93 8D 2F B1 F8 F8 82 45 98 FF B1 99  .U..../....E....
Returned:  0 CKR_OK

23: C_GetAttributeValue
2016-08-22 23:31:24.922
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a4056a0
[in] pTemplate[1]:
    CKA_KEY_TYPE          0000700000218bc8 / 8
[out] pTemplate[1]:
    CKA_KEY_TYPE          CKK_RSA            
Returned:  0 CKR_OK

24: C_GetAttributeValue
2016-08-22 23:31:24.922
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a4056a0
[in] pTemplate[1]:
    CKA_EXTRACTABLE       0000700000218bcf / 1
[out] pTemplate[1]:
    CKA_EXTRACTABLE       False
Returned:  0 CKR_OK

25: C_GetAttributeValue
2016-08-22 23:31:24.922
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a4056a0
[in] pTemplate[1]:
    CKA_ID                0000000000000000 / 0
[out] pTemplate[1]:
    CKA_ID                0000000000000000 / 1
Returned:  0 CKR_OK

26: C_GetAttributeValue
2016-08-22 23:31:24.922
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a4056a0
[in] pTemplate[1]:
    CKA_ID                0000700000218940 / 1
[out] pTemplate[1]:
    CKA_ID                0000700000218940 / 1
    00000000  02                                               .              
Returned:  0 CKR_OK

27: C_GetAttributeValue
2016-08-22 23:31:24.922
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a4056a0
[in] pTemplate[1]:
    CKA_LABEL             0000000000000000 / 0
[out] pTemplate[1]:
    CKA_LABEL             0000000000000000 / 8
Returned:  0 CKR_OK

28: C_GetAttributeValue
2016-08-22 23:31:24.922
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a4056a0
[in] pTemplate[1]:
    CKA_LABEL             0000700000218950 / 8
[out] pTemplate[1]:
    CKA_LABEL             0000700000218950 / 8
    5349474E 206B6579
     S I G N  . k e y
Returned:  0 CKR_OK

29: C_GetAttributeValue
2016-08-22 23:31:24.922
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a4056a0
[in] pTemplate[1]:
    CKA_KEY_TYPE          00007000002189d8 / 8
[out] pTemplate[1]:
    CKA_KEY_TYPE          CKK_RSA            
Returned:  0 CKR_OK

30: C_GetAttributeValue
2016-08-22 23:31:24.922
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a4056a0
[in] pTemplate[1]:
    CKA_MODULUS_BITS      00007000002189d8 / 8
[out] pTemplate[1]:
    CKA_MODULUS_BITS      00007000002189d8 / 8
    00000000  00 08 00 00 00 00 00 00                          ........        
Returned:  0 CKR_OK

31: C_GetAttributeValue
2016-08-22 23:31:24.922
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a4056a0
[in] pTemplate[1]:
    CKA_SENSITIVE         0000700000218a5f / 1
[out] pTemplate[1]:
    CKA_SENSITIVE         True
Returned:  0 CKR_OK

32: C_GetAttributeValue
2016-08-22 23:31:24.923
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a4056a0
[in] pTemplate[1]:
    CKA_MODULUS           0000000000000000 / 0
[out] pTemplate[1]:
    CKA_MODULUS           0000000000000000 / 256
Returned:  0 CKR_OK

33: C_GetAttributeValue
2016-08-22 23:31:24.923
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a4056a0
[in] pTemplate[1]:
    CKA_MODULUS           00007000002189e0 / 256
[out] pTemplate[1]:
    CKA_MODULUS           00007000002189e0 / 256
    00000000  BF 03 6F 94 56 56 89 D1 91 8B 1D F5 63 7F 8F 5C  ..o.VV......c.\
    00000010  94 09 0D 67 0D C4 A8 B3 AB 6D 94 4C 03 67 4E 4D  ...g.....m.L.gNM
    . . . . .
    000000F0  52 ED EC EA 97 83 46 D9 0A 34 51 19 60 BD 5E EB  R.....F..4Q.`.^.
Returned:  0 CKR_OK

34: C_GetAttributeValue
2016-08-22 23:31:24.923
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a405870
[in] pTemplate[1]:
    CKA_KEY_TYPE          0000700000218bc8 / 8
[out] pTemplate[1]:
    CKA_KEY_TYPE          CKK_RSA            
Returned:  0 CKR_OK

35: C_GetAttributeValue
2016-08-22 23:31:24.923
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a405870
[in] pTemplate[1]:
    CKA_EXTRACTABLE       0000700000218bcf / 1
[out] pTemplate[1]:
    CKA_EXTRACTABLE       False
Returned:  0 CKR_OK

36: C_GetAttributeValue
2016-08-22 23:31:24.923
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a405870
[in] pTemplate[1]:
    CKA_ID                0000000000000000 / 0
[out] pTemplate[1]:
    CKA_ID                0000000000000000 / 1
Returned:  0 CKR_OK

37: C_GetAttributeValue
2016-08-22 23:31:24.923
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a405870
[in] pTemplate[1]:
    CKA_ID                0000700000218940 / 1
[out] pTemplate[1]:
    CKA_ID                0000700000218940 / 1
    00000000  03                                               .              
Returned:  0 CKR_OK

38: C_GetAttributeValue
2016-08-22 23:31:24.923
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a405870
[in] pTemplate[1]:
    CKA_LABEL             0000000000000000 / 0
[out] pTemplate[1]:
    CKA_LABEL             0000000000000000 / 11
Returned:  0 CKR_OK

39: C_GetAttributeValue
2016-08-22 23:31:24.923
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a405870
[in] pTemplate[1]:
    CKA_LABEL             0000700000218950 / 11
[out] pTemplate[1]:
    CKA_LABEL             0000700000218950 / 11
    4B455920 4D414E20 6B6579
     K E Y .  M A N .  k e y
Returned:  0 CKR_OK

40: C_GetAttributeValue
2016-08-22 23:31:24.923
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a405870
[in] pTemplate[1]:
    CKA_KEY_TYPE          00007000002189d8 / 8
[out] pTemplate[1]:
    CKA_KEY_TYPE          CKK_RSA            
Returned:  0 CKR_OK

41: C_GetAttributeValue
2016-08-22 23:31:24.923
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a405870
[in] pTemplate[1]:
    CKA_MODULUS_BITS      00007000002189d8 / 8
[out] pTemplate[1]:
    CKA_MODULUS_BITS      00007000002189d8 / 8
    00000000  00 08 00 00 00 00 00 00                          ........        
Returned:  0 CKR_OK

42: C_GetAttributeValue
2016-08-22 23:31:24.923
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a405870
[in] pTemplate[1]:
    CKA_SENSITIVE         0000700000218a5f / 1
[out] pTemplate[1]:
    CKA_SENSITIVE         True
Returned:  0 CKR_OK

43: C_GetAttributeValue
2016-08-22 23:31:24.923
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a405870
[in] pTemplate[1]:
    CKA_MODULUS           0000000000000000 / 0
[out] pTemplate[1]:
    CKA_MODULUS           0000000000000000 / 256
Returned:  0 CKR_OK

44: C_GetAttributeValue
2016-08-22 23:31:24.923
[in] hSession = 0x7fe37a62b610
[in] hObject = 0x7fe37a405870
[in] pTemplate[1]:
    CKA_MODULUS           00007000002189e0 / 256
[out] pTemplate[1]:
    CKA_MODULUS           00007000002189e0 / 256
    00000000  AB 9B F4 1C 92 FD 23 DE 9D DF 82 EB 50 BA 37 7E  ......#.....P.7~
    00000010  3A 7E A3 EC 5D FD 82 52 A0 D7 96 68 B5 21 E9 4E  :~..]..R...h.!.N
    . . . . .
    000000F0  A4 7D BD D5 6B 34 1C 95 6E 10 91 4E 36 04 D1 A3  .}..k4..n..N6...
Returned:  0 CKR_OK

. . . . .

key.class=class org.opensc.pkcs11.wrap.PKCS11NeRSAPrivateKey

Decryption test completed.
java.security.InvalidKeyException: Wrong key usage
        at javax.crypto.Cipher.init(Cipher.java:1712)
        at javax.crypto.Cipher.init(Cipher.java:1618)
        at burrow.pkcs11.PKCS11Test2.testDecryption(PKCS11Test2.java:229)
        at burrow.pkcs11.PKCS11Test2.main(PKCS11Test2.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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
. . . . .
About to test Signature...
Testing signature...
certificate=[
[
  Version: V3
  Subject: CN=Uri the Great
  Signature Algorithm: SHA256withRSA, OID = 1.2.840.113549.1.1.11

  Key:  Sun RSA public key, 2048 bits
  modulus: 24113210532847844553279723176113939475545267994931288640601170364043704389641997353636569697791727555996711186383991746940652502906730332838204280253311942253006578377745691734422731788549887719760459058833519479361944614321662284518157589453985226306880622694647137378234682203629046984223244867136994440189815309623711415085811151152222028602469345415640696627993130542558593475693005228712167584921768959266591909683934719965776956311116533693910576584069869172722498558389572660499465716777381845879657467062335517360668147484509715867820489264250545955162631744688000021700234855309513301163127479143820500492011
  public exponent: 65537
  Validity: [From: Tue Jul 19 16:47:34 EDT 2016,
               To: Fri Jul 19 16:47:34 EDT 2019]
  Issuer: CN=Forest CA RSA 4
  SerialNumber: [    578e91a9]

Certificate Extensions: 4
[1]: ObjectId: 2.5.29.19 Criticality=true
BasicConstraints:[
  CA:false
  PathLen: undefined
]

[2]: ObjectId: 2.5.29.37 Criticality=true
ExtendedKeyUsages [
  clientAuth
  codeSigning
  1.3.6.1.4.1.311.10.3.12
  1.2.840.113583.1.1.5
  emailProtection
]

[3]: ObjectId: 2.5.29.15 Criticality=true
KeyUsage [
  DigitalSignature
  Non_repudiation
]

[4]: ObjectId: 2.5.29.17 Criticality=false
SubjectAlternativeName [
  RFC822Name: [hidden email]
]

]
  Algorithm: [SHA256withRSA]
  Signature:
0000: 80 26 B1 ED BF 85 E8 68   5B 1B BC AB 79 57 27 24  .&.....h[...yW'$
0010: 94 EA 12 25 74 A5 2A 0B   EE FB 1E EB 92 78 DF 42  ...%t.*......x.B
. . . . .
0170: 12 59 78 24 3C B7 C4 FE   AC AD 97 E9 C7 83 F0 C9  .Yx$<...........

]
key.class=class org.opensc.pkcs11.wrap.PKCS11NeRSAPrivateKey
sig.provider=OpenSC-PKCS11
. . . . .
107: C_SignInit

2016-08-22 23:31:25.244
About to clean up and tear down OpenSC-PKCS11 provider...
[in] hSession = 0x7fe37a62b610
pMechanism->type=CKM_SHA256_RSA_PKCS          
[in] hKey = 0x7fe37a4056a0
Returned:  0 CKR_OK

108: C_SignUpdate
2016-08-22 23:31:25.244
[in] hSession = 0x7fe37a62b610
[in] pPart[ulPartLen] 0000700000218aa0 / 256
    00000000  93 86 4C ED 2B 92 11 95 EF 49 64 76 1C 2B B8 7F  ..L.+....Idv.+.
    . . . . .
    000000E0  0E 5C AC 40 3F BA 39 C0 3E 07 FE 7D C0 DA C9 BD  .\.@?.9.>..}....
    000000F0  D9 20 D7 BF D1 2B 75 31 F6 3D CF 92 56 C6 22 37  . ...+u1.=..V."7
Returned:  0 CKR_OK

java.lang.NullPointerException
        at org.opensc.pkcs11.wrap.PKCS11Session.sign(Unknown Source)
        at org.opensc.pkcs11.spi.PKCS11SignatureSpi.engineSign(Unknown Source)
        at java.security.Signature$Delegate.engineSign(Signature.java:1207)
        at java.security.Signature.sign(Signature.java:579)
        at burrow.pkcs11.PKCS11Test2.testSignature(PKCS11Test2.java:180)
        at burrow.pkcs11.PKCS11Test2.main(PKCS11Test2.java:286)
        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:140)

> On 08/21/2016 07:03 AM, Uri Blumenthal wrote:
>> Andreas,
>>
>> Trying to test your OpenSC-Java fork (adapting the tests from the original OpenSC/OpenSC-Java/pkcs11-test), I stumbled upon more problems. Not sure whether they’re caused by me not understanding how to use it, or the issues with the library itself. In any case, I’d really appreciate your guidance.
>>
>> Here are the problems:
>>
>> 1. KeyStore test - retrieves all the certificates (though it assigns to them strange aliases based on CN), but fails to get certificate chain (that call returns null).
>>
>> Code:
>>  Certificate certificate = ks.getCertificate(alias); // works
>>  printCert( (X509Certificate) certificate ); // works
>>  Certificate [] chain = ks.getCertificateChain(alias); // returns chain==null
>>
>> 2. Signature test fails on the last operation (actually performing signature on the token).
>>
>> Code:
>>  Signature sig = Signature.getInstance(“SHA256withRSA”
>>                                      /* , "OpenSC-PKCS11"
>>                                        provider*/);
>>  sig.initSign((PrivateKey)key);
>>  System.out.println("sig.provider=" +
>>                     sig.getProvider().getName());
>>
>>  sig.update(this.testData);
>>  byte[] signature = sig.sign(); // line #166
>>
>> Stack trace:
>> key.class=class org.opensc.pkcs11.wrap.PKCS11NeRSAPrivateKey
>> sig.provider=OpenSC-PKCS11
>> Exception in thread "main" java.lang.NullPointerException
>> at org.opensc.pkcs11.wrap.PKCS11Session.sign(Unknown Source)
>> at org.opensc.pkcs11.spi.PKCS11SignatureSpi.engineSign(Unknown Source)
>> at java.security.Signature$Delegate.engineSign(Signature.java:1207)
>> at java.security.Signature.sign(Signature.java:579)
>> at burrow.pkcs11.PKCS11Test2.testSignature(PKCS11Test2.java:166)
>> at burrow.pkcs11.PKCS11Test2.main(PKCS11Test2.java:250)
>> 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:140)
>>
>>
>> 3. Decryption test fails similarly to above.
>>
>> Code:
>>  System.out.println("key.class=" + key.getClass());
>>  Cipher enc = Cipher.getInstance("RSA/ECB/PKCS1Padding");
>>  enc.init(Cipher.ENCRYPT_MODE, certificate); // this is the line #206
>>  byte[] encData = enc.doFinal(this.testData);
>>
>> Stack trace:
>> key.class=class org.opensc.pkcs11.wrap.PKCS11NeRSAPrivateKey
>> Exception in thread "main" java.security.InvalidKeyException: Wrong key usage
>> at javax.crypto.Cipher.init(Cipher.java:1712)
>> at javax.crypto.Cipher.init(Cipher.java:1618)
>> at burrow.pkcs11.PKCS11Test2.testDecryption(PKCS11Test2.java:206)
>> at burrow.pkcs11.PKCS11Test2.main(PKCS11Test2.java:244)
>> 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:140)
>>
>> Key usage:
>> ExtendedKeyUsages [
>>  emailProtection
>>  1.3.6.1.4.1.311.10.3.4
>>  anyExtendedKeyUsage
>> ]
>>
>> [3]: ObjectId: 2.5.29.15 Criticality=true
>> KeyUsage [
>>  Key_Encipherment
>> ]
>>
>>
>>> On Aug 18, 2016, at 23:37 , Uri Blumenthal <[hidden email]> wrote:
>>>
>>> Andreas,
>>>
>>> I’ve found a few problems with the build, and filed issues on your GitHub repo.
>>>
>>> In short, (a) the script does not initiate JNI rebuild, but uses the prebuilt copy instead (bad because it got the library path wrong), and (b) the script doesn’t re-create the “java/lib” directory that “ant clean” removes.

--
Uri Blumenthal
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Unable to sign using PKCS#11 tokens

Andreas Schwier-2
Dear Uri,

I've posted a fix to the opensc-pkcs11 repo that remove the NPE when
doing a sign.

The problem with decryption is actually caused by JUNITs assertEqual()
which does not compare the content of the array but the reference.

When replaced by assertTrue(Arrays.equals(this.testData, origData)) it
worked for me.

I've done a regression test with the other test cases and a
SmartCard-HSM equipped with sample keys and certificates.

Andreas

On 08/23/2016 06:07 AM, Uri Blumenthal wrote:

> On Aug 22, 2016, at 2:53 , Andreas Schwier <[hidden email]> wrote:
>> Hi Uri,
>>
>> it's difficult to tell, what is causing your problem if we don't know
>> whats keys you have on your device.
>> Can you run "pkcs11-tool --module <modname> -O —login" to see the PKCS11 objects ?
>
> I certainly can:
>
> $ pkcs11-tool -O -l
> Using slot 0 with a present token (0x0)
> Logging in to "PIV Card Holder pin: PIV_II".
> Please enter User PIN:
> Private Key Object; RSA
>   label:      PIV AUTH key
>   ID:         01
>   Usage:      decrypt, sign, unwrap
> Public Key Object; RSA 2048 bits
>   label:      PIV AUTH pubkey
>   ID:         01
>   Usage:      encrypt, verify, wrap
> Certificate Object, type = X.509 cert
>   label:      Certificate for PIV Authentication
>   ID:         01
> Private Key Object; RSA
>   label:      SIGN key
>   ID:         02
>   Usage:      decrypt, sign, non-repudiation
>   Access:     always authenticate
> Public Key Object; RSA 2048 bits
>   label:      SIGN pubkey
>   ID:         02
>   Usage:      encrypt, verify
> Certificate Object, type = X.509 cert
>   label:      Certificate for Digital Signature
>   ID:         02
> Private Key Object; RSA
>   label:      KEY MAN key
>   ID:         03
>   Usage:      decrypt, unwrap
> Public Key Object; RSA 2048 bits
>   label:      KEY MAN pubkey
>   ID:         03
>   Usage:      encrypt, wrap
> Certificate Object, type = X.509 cert
>   label:      Certificate for Key Management
>   ID:         03
> Data object 1614881296
>   label:          'Cardholder Fingerprints'
>   application:    'Cardholder Fingerprints'
>   app_id:         2.16.840.1.101.3.7.2.96.16
>   flags:           private
> Data object 1614881392
>   label:          'Printed Information'
>   application:    'Printed Information'
>   app_id:         2.16.840.1.101.3.7.2.48.1
>   flags:           private
> Data object 1614881488
>   label:          'Cardholder Facial Image'
>   application:    'Cardholder Facial Image'
>   app_id:         2.16.840.1.101.3.7.2.96.48
>   flags:           private
> Private Key Object; RSA
>   label:      CARD AUTH key
>   ID:         04
>   Usage:      sign
> Public Key Object; RSA 2048 bits
>   label:      CARD AUTH pubkey
>   ID:         04
>   Usage:      verify
> Certificate Object, type = X.509 cert
>   label:      Certificate for Card Authentication
>   ID:         04
> Data object 1614884224
>   label:          'Card Capability Container'
>   application:    'Card Capability Container'
>   app_id:         2.16.840.1.101.3.7.1.219.0
>   flags:          <empty>
> Data object 1614878464
>   label:          'Card Holder Unique Identifier'
>   application:    'Card Holder Unique Identifier'
>   app_id:         2.16.840.1.101.3.7.2.48.0
>   flags:          <empty>
> Data object 1614878560
>   label:          'Unsigned Card Holder Unique Identifier'
>   application:    'Unsigned Card Holder Unique Identifier'
>   app_id:         2.16.840.1.101.3.7.2.48.2
>   flags:          <empty>
> Data object 1614878656
>   label:          'X.509 Certificate for PIV Authentication'
>   application:    'X.509 Certificate for PIV Authentication'
>   app_id:         2.16.840.1.101.3.7.2.1.1
>   flags:          <empty>
> Data object 1614881584
>   label:          'X.509 Certificate for Digital Signature'
>   application:    'X.509 Certificate for Digital Signature'
>   app_id:         2.16.840.1.101.3.7.2.1.0
>   flags:          <empty>
> Data object 1614881680
>   label:          'X.509 Certificate for Key Management'
>   application:    'X.509 Certificate for Key Management'
>   app_id:         2.16.840.1.101.3.7.2.1.2
>   flags:          <empty>
> Data object 1614881776
>   label:          'X.509 Certificate for Card Authentication'
>   application:    'X.509 Certificate for Card Authentication'
>   app_id:         2.16.840.1.101.3.7.2.5.0
>   flags:          <empty>
> Data object 1614881872
>   label:          'Security Object'
>   application:    'Security Object'
>   app_id:         2.16.840.1.101.3.7.2.144.0
>   flags:          <empty>
> Data object 1614881968
>   label:          'Discovery Object'
>   application:    'Discovery Object'
>   app_id:         2.16.840.1.101.3.7.2.96.80
>   flags:          <empty>
> $
>
>
>> As said before, we are using that module with our SmartCard-HSM and so
>> far it has served us well.
>
> I realize that, and am trying to figure why it does not talk to other PIV tokens, such as US DoD CAC and YubiKey. I hope you can help to get this library to work with our tokens, and maybe also to improve some rough edges that I reported.
>
>> Regarding your issues
>>
>> 1. getCertificateChain returns null if it detects an error. Can you
>> enable logging or debug into the PKCS11KeyStoreSpi ?
>
> Will try (not sure I understand what it is that you’re asking, or how to accomplish it).
>
> In the meanwhile I can add one more thing to this:
>
> Alias name: Certificate for Digital Signature
> Entry type: PrivateKeyEntry
> Certificate chain length: 1
> Certificate[1]:
> Owner: . . . . .
>
> As you can see, the chain is of length 1, i.e. it contains only one certificate, not the entire chain.
>
>> 2. and 3. Can you enable PKCS11SPY from OpenSC to see what PKCS#11 calls
>> are used and what they report.
>
> I’ve got that - will email you directly, because the files are too large for this mailing list (the server bounces posts larger than 50K). I’ll also email you the source of the program that produced those.
>
> 1: C_Initialize
> 2016-08-22 23:31:13.288
> [in] pInitArgs = 0x127aa60b0
>      flags: 2
>        CKF_OS_LOCKING_OK
> Returned:  0 CKR_OK
>
> 2: C_GetInfo
> 2016-08-22 23:31:14.039
> [out] pInfo:
>       cryptokiVersion:         2.20
>       manufacturerID:         'OpenSC Project                  '
>       flags:                   0
>       libraryDescription:     'OpenSC smartcard framework      '
>       libraryVersion:          0.16
> Returned:  0 CKR_OK
> OpenSC-PKCS11 provider added.
>
> About to test KeyStore...
>
> 3: C_GetSlotList
> 2016-08-22 23:31:14.670
> [in] tokenPresent = 0x0
> [out] pSlotList:
> Count is 1
> [out] *pulCount = 0x1
> Returned:  0 CKR_OK
>
> 4: C_GetSlotList
> 2016-08-22 23:31:14.672
> [in] tokenPresent = 0x0
> [out] pSlotList:
> Slot 0
> [out] *pulCount = 0x1
> Returned:  0 CKR_OK
>
> 5: C_GetSlotInfo
> 2016-08-22 23:31:14.673
> [in] slotID = 0x0
> [out] pInfo:
>       slotDescription:        'Yubico Yubikey NEO OTP+U2F+CCID '
>                               '                                '
>       manufacturerID:         'Yubico                          '
>       hardwareVersion:         3.70
>       firmwareVersion:         0.0
>       flags:                   7
>         CKF_TOKEN_PRESENT                
>         CKF_REMOVABLE_DEVICE            
>         CKF_HW_SLOT                      
> Returned:  0 CKR_OK
>
> 6: C_GetTokenInfo
> 2016-08-22 23:31:14.674
> [in] slotID = 0x0
> [out] pInfo:
>       label:                  'PIV Card Holder pin: PIV_II     '
>       manufacturerID:         'piv_II                          '
>       model:                  'PKCS#15 emulated'
>       serialNumber:           '93c28cbc872b0f77'
>       ulMaxSessionCount:       0
>       ulSessionCount:          0
>       ulMaxRwSessionCount:     0
>       ulRwSessionCount:        0
>       ulMaxPinLen:             8
>       ulMinPinLen:             4
>       ulTotalPublicMemory:     -1
>       ulFreePublicMemory:      -1
>       ulTotalPrivateMemory:    -1
>       ulFreePrivateMemory:     -1
>       hardwareVersion:         0.0
>       firmwareVersion:         0.0
>       time:                   '                '
>       flags:                   40d
>         CKF_RNG                          
>         CKF_LOGIN_REQUIRED              
>         CKF_USER_PIN_INITIALIZED        
>         CKF_TOKEN_INITIALIZED            
> Returned:  0 CKR_OK
>
> 7: C_OpenSession
> 2016-08-22 23:31:14.690
> [in] slotID = 0x0
> [in] flags = 0x4
> pApplication=0x0
> Notify=0x0
> [out] *phSession = 0x7fe37a62b610
> Returned:  0 CKR_OK
>
> 8: C_Login
> 2016-08-22 23:31:24.781
> [in] hSession = 0x7fe37a62b610
> [in] userType = CKU_USER
> [in] pPin[ulPinLen] 0000700000218b40 / 6
>     00000000  31 32 33 34 35 36 37 38     12345678          
> Returned:  0 CKR_OK
>
> 9: C_FindObjectsInit
> 2016-08-22 23:31:24.919
> [in] hSession = 0x7fe37a62b610
> [in] pTemplate[1]:
>     CKA_CLASS             CKO_PRIVATE_KEY      
> Returned:  0 CKR_OK
>
> 10: C_FindObjects
> 2016-08-22 23:31:24.920
> [in] hSession = 0x7fe37a62b610
> [in] ulMaxObjectCount = 0x20
> [out] ulObjectCount = 0x4
> Object 0x7fe37a4054e0 matches
> Object 0x7fe37a4056a0 matches
> Object 0x7fe37a405870 matches
> Object 0x7fe37a405ce0 matches
> Returned:  0 CKR_OK
>
> 11: C_FindObjectsFinal
> 2016-08-22 23:31:24.920
> [in] hSession = 0x7fe37a62b610
> Returned:  0 CKR_OK
>
> 12: C_GetAttributeValue
> 2016-08-22 23:31:24.920
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a4054e0
> [in] pTemplate[1]:
>     CKA_KEY_TYPE          0000700000218bc8 / 8
> [out] pTemplate[1]:
>     CKA_KEY_TYPE          CKK_RSA            
> Returned:  0 CKR_OK
>
> 13: C_GetAttributeValue
> 2016-08-22 23:31:24.921
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a4054e0
> [in] pTemplate[1]:
>     CKA_EXTRACTABLE       0000700000218bcf / 1
> [out] pTemplate[1]:
>     CKA_EXTRACTABLE       False
> Returned:  0 CKR_OK
>
> 14: C_GetAttributeValue
> 2016-08-22 23:31:24.921
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a4054e0
> [in] pTemplate[1]:
>     CKA_ID                0000000000000000 / 0
> [out] pTemplate[1]:
>     CKA_ID                0000000000000000 / 1
> Returned:  0 CKR_OK
>
> 15: C_GetAttributeValue
> 2016-08-22 23:31:24.921
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a4054e0
> [in] pTemplate[1]:
>     CKA_ID                0000700000218940 / 1
> [out] pTemplate[1]:
>     CKA_ID                0000700000218940 / 1
>     00000000  01                                               .              
> Returned:  0 CKR_OK
>
> 16: C_GetAttributeValue
> 2016-08-22 23:31:24.921
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a4054e0
> [in] pTemplate[1]:
>     CKA_LABEL             0000000000000000 / 0
> [out] pTemplate[1]:
>     CKA_LABEL             0000000000000000 / 12
> Returned:  0 CKR_OK
>
> 17: C_GetAttributeValue
> 2016-08-22 23:31:24.921
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a4054e0
> [in] pTemplate[1]:
>     CKA_LABEL             0000700000218950 / 12
> [out] pTemplate[1]:
>     CKA_LABEL             0000700000218950 / 12
>     50495620 41555448 206B6579
>      P I V .  A U T H  . k e y
> Returned:  0 CKR_OK
>
> 18: C_GetAttributeValue
> 2016-08-22 23:31:24.922
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a4054e0
> [in] pTemplate[1]:
>     CKA_KEY_TYPE          00007000002189d8 / 8
> [out] pTemplate[1]:
>     CKA_KEY_TYPE          CKK_RSA            
> Returned:  0 CKR_OK
>
> 19: C_GetAttributeValue
> 2016-08-22 23:31:24.922
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a4054e0
> [in] pTemplate[1]:
>     CKA_MODULUS_BITS      00007000002189d8 / 8
> [out] pTemplate[1]:
>     CKA_MODULUS_BITS      00007000002189d8 / 8
>     00000000  00 08 00 00 00 00 00 00                          ........        
> Returned:  0 CKR_OK
>
> 20: C_GetAttributeValue
> 2016-08-22 23:31:24.922
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a4054e0
> [in] pTemplate[1]:
>     CKA_SENSITIVE         0000700000218a5f / 1
> [out] pTemplate[1]:
>     CKA_SENSITIVE         True
> Returned:  0 CKR_OK
>
> 21: C_GetAttributeValue
> 2016-08-22 23:31:24.922
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a4054e0
> [in] pTemplate[1]:
>     CKA_MODULUS           0000000000000000 / 0
> [out] pTemplate[1]:
>     CKA_MODULUS           0000000000000000 / 256
> Returned:  0 CKR_OK
>
> 22: C_GetAttributeValue
> 2016-08-22 23:31:24.922
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a4054e0
> [in] pTemplate[1]:
>     CKA_MODULUS           00007000002189e0 / 256
> [out] pTemplate[1]:
>     CKA_MODULUS           00007000002189e0 / 256
>     00000000  9D 78 A2 BF 06 FD 20 19 1B 14 F1 F6 7A BE 1B 01  .x.... .....z...
>     00000010  B1 9F E7 EF 82 64 D6 E1 3D 7D 94 E9 86 57 82 F7  .....d..=}...W..
>     . . . . .
>     000000E0  26 03 40 43 7E D2 9D DA 2D 09 76 89 CE 3E 44 A3  &.@C~...-.v..>D.
>     000000F0  F2 55 C6 FA 93 8D 2F B1 F8 F8 82 45 98 FF B1 99  .U..../....E....
> Returned:  0 CKR_OK
>
> 23: C_GetAttributeValue
> 2016-08-22 23:31:24.922
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a4056a0
> [in] pTemplate[1]:
>     CKA_KEY_TYPE          0000700000218bc8 / 8
> [out] pTemplate[1]:
>     CKA_KEY_TYPE          CKK_RSA            
> Returned:  0 CKR_OK
>
> 24: C_GetAttributeValue
> 2016-08-22 23:31:24.922
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a4056a0
> [in] pTemplate[1]:
>     CKA_EXTRACTABLE       0000700000218bcf / 1
> [out] pTemplate[1]:
>     CKA_EXTRACTABLE       False
> Returned:  0 CKR_OK
>
> 25: C_GetAttributeValue
> 2016-08-22 23:31:24.922
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a4056a0
> [in] pTemplate[1]:
>     CKA_ID                0000000000000000 / 0
> [out] pTemplate[1]:
>     CKA_ID                0000000000000000 / 1
> Returned:  0 CKR_OK
>
> 26: C_GetAttributeValue
> 2016-08-22 23:31:24.922
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a4056a0
> [in] pTemplate[1]:
>     CKA_ID                0000700000218940 / 1
> [out] pTemplate[1]:
>     CKA_ID                0000700000218940 / 1
>     00000000  02                                               .              
> Returned:  0 CKR_OK
>
> 27: C_GetAttributeValue
> 2016-08-22 23:31:24.922
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a4056a0
> [in] pTemplate[1]:
>     CKA_LABEL             0000000000000000 / 0
> [out] pTemplate[1]:
>     CKA_LABEL             0000000000000000 / 8
> Returned:  0 CKR_OK
>
> 28: C_GetAttributeValue
> 2016-08-22 23:31:24.922
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a4056a0
> [in] pTemplate[1]:
>     CKA_LABEL             0000700000218950 / 8
> [out] pTemplate[1]:
>     CKA_LABEL             0000700000218950 / 8
>     5349474E 206B6579
>      S I G N  . k e y
> Returned:  0 CKR_OK
>
> 29: C_GetAttributeValue
> 2016-08-22 23:31:24.922
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a4056a0
> [in] pTemplate[1]:
>     CKA_KEY_TYPE          00007000002189d8 / 8
> [out] pTemplate[1]:
>     CKA_KEY_TYPE          CKK_RSA            
> Returned:  0 CKR_OK
>
> 30: C_GetAttributeValue
> 2016-08-22 23:31:24.922
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a4056a0
> [in] pTemplate[1]:
>     CKA_MODULUS_BITS      00007000002189d8 / 8
> [out] pTemplate[1]:
>     CKA_MODULUS_BITS      00007000002189d8 / 8
>     00000000  00 08 00 00 00 00 00 00                          ........        
> Returned:  0 CKR_OK
>
> 31: C_GetAttributeValue
> 2016-08-22 23:31:24.922
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a4056a0
> [in] pTemplate[1]:
>     CKA_SENSITIVE         0000700000218a5f / 1
> [out] pTemplate[1]:
>     CKA_SENSITIVE         True
> Returned:  0 CKR_OK
>
> 32: C_GetAttributeValue
> 2016-08-22 23:31:24.923
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a4056a0
> [in] pTemplate[1]:
>     CKA_MODULUS           0000000000000000 / 0
> [out] pTemplate[1]:
>     CKA_MODULUS           0000000000000000 / 256
> Returned:  0 CKR_OK
>
> 33: C_GetAttributeValue
> 2016-08-22 23:31:24.923
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a4056a0
> [in] pTemplate[1]:
>     CKA_MODULUS           00007000002189e0 / 256
> [out] pTemplate[1]:
>     CKA_MODULUS           00007000002189e0 / 256
>     00000000  BF 03 6F 94 56 56 89 D1 91 8B 1D F5 63 7F 8F 5C  ..o.VV......c.\
>     00000010  94 09 0D 67 0D C4 A8 B3 AB 6D 94 4C 03 67 4E 4D  ...g.....m.L.gNM
>     . . . . .
>     000000F0  52 ED EC EA 97 83 46 D9 0A 34 51 19 60 BD 5E EB  R.....F..4Q.`.^.
> Returned:  0 CKR_OK
>
> 34: C_GetAttributeValue
> 2016-08-22 23:31:24.923
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a405870
> [in] pTemplate[1]:
>     CKA_KEY_TYPE          0000700000218bc8 / 8
> [out] pTemplate[1]:
>     CKA_KEY_TYPE          CKK_RSA            
> Returned:  0 CKR_OK
>
> 35: C_GetAttributeValue
> 2016-08-22 23:31:24.923
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a405870
> [in] pTemplate[1]:
>     CKA_EXTRACTABLE       0000700000218bcf / 1
> [out] pTemplate[1]:
>     CKA_EXTRACTABLE       False
> Returned:  0 CKR_OK
>
> 36: C_GetAttributeValue
> 2016-08-22 23:31:24.923
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a405870
> [in] pTemplate[1]:
>     CKA_ID                0000000000000000 / 0
> [out] pTemplate[1]:
>     CKA_ID                0000000000000000 / 1
> Returned:  0 CKR_OK
>
> 37: C_GetAttributeValue
> 2016-08-22 23:31:24.923
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a405870
> [in] pTemplate[1]:
>     CKA_ID                0000700000218940 / 1
> [out] pTemplate[1]:
>     CKA_ID                0000700000218940 / 1
>     00000000  03                                               .              
> Returned:  0 CKR_OK
>
> 38: C_GetAttributeValue
> 2016-08-22 23:31:24.923
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a405870
> [in] pTemplate[1]:
>     CKA_LABEL             0000000000000000 / 0
> [out] pTemplate[1]:
>     CKA_LABEL             0000000000000000 / 11
> Returned:  0 CKR_OK
>
> 39: C_GetAttributeValue
> 2016-08-22 23:31:24.923
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a405870
> [in] pTemplate[1]:
>     CKA_LABEL             0000700000218950 / 11
> [out] pTemplate[1]:
>     CKA_LABEL             0000700000218950 / 11
>     4B455920 4D414E20 6B6579
>      K E Y .  M A N .  k e y
> Returned:  0 CKR_OK
>
> 40: C_GetAttributeValue
> 2016-08-22 23:31:24.923
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a405870
> [in] pTemplate[1]:
>     CKA_KEY_TYPE          00007000002189d8 / 8
> [out] pTemplate[1]:
>     CKA_KEY_TYPE          CKK_RSA            
> Returned:  0 CKR_OK
>
> 41: C_GetAttributeValue
> 2016-08-22 23:31:24.923
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a405870
> [in] pTemplate[1]:
>     CKA_MODULUS_BITS      00007000002189d8 / 8
> [out] pTemplate[1]:
>     CKA_MODULUS_BITS      00007000002189d8 / 8
>     00000000  00 08 00 00 00 00 00 00                          ........        
> Returned:  0 CKR_OK
>
> 42: C_GetAttributeValue
> 2016-08-22 23:31:24.923
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a405870
> [in] pTemplate[1]:
>     CKA_SENSITIVE         0000700000218a5f / 1
> [out] pTemplate[1]:
>     CKA_SENSITIVE         True
> Returned:  0 CKR_OK
>
> 43: C_GetAttributeValue
> 2016-08-22 23:31:24.923
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a405870
> [in] pTemplate[1]:
>     CKA_MODULUS           0000000000000000 / 0
> [out] pTemplate[1]:
>     CKA_MODULUS           0000000000000000 / 256
> Returned:  0 CKR_OK
>
> 44: C_GetAttributeValue
> 2016-08-22 23:31:24.923
> [in] hSession = 0x7fe37a62b610
> [in] hObject = 0x7fe37a405870
> [in] pTemplate[1]:
>     CKA_MODULUS           00007000002189e0 / 256
> [out] pTemplate[1]:
>     CKA_MODULUS           00007000002189e0 / 256
>     00000000  AB 9B F4 1C 92 FD 23 DE 9D DF 82 EB 50 BA 37 7E  ......#.....P.7~
>     00000010  3A 7E A3 EC 5D FD 82 52 A0 D7 96 68 B5 21 E9 4E  :~..]..R...h.!.N
>     . . . . .
>     000000F0  A4 7D BD D5 6B 34 1C 95 6E 10 91 4E 36 04 D1 A3  .}..k4..n..N6...
> Returned:  0 CKR_OK
>
> . . . . .
>
> key.class=class org.opensc.pkcs11.wrap.PKCS11NeRSAPrivateKey
>
> Decryption test completed.
> java.security.InvalidKeyException: Wrong key usage
> at javax.crypto.Cipher.init(Cipher.java:1712)
> at javax.crypto.Cipher.init(Cipher.java:1618)
> at burrow.pkcs11.PKCS11Test2.testDecryption(PKCS11Test2.java:229)
> at burrow.pkcs11.PKCS11Test2.main(PKCS11Test2.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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
> . . . . .
> About to test Signature...
> Testing signature...
> certificate=[
> [
>   Version: V3
>   Subject: CN=Uri the Great
>   Signature Algorithm: SHA256withRSA, OID = 1.2.840.113549.1.1.11
>
>   Key:  Sun RSA public key, 2048 bits
>   modulus: 24113210532847844553279723176113939475545267994931288640601170364043704389641997353636569697791727555996711186383991746940652502906730332838204280253311942253006578377745691734422731788549887719760459058833519479361944614321662284518157589453985226306880622694647137378234682203629046984223244867136994440189815309623711415085811151152222028602469345415640696627993130542558593475693005228712167584921768959266591909683934719965776956311116533693910576584069869172722498558389572660499465716777381845879657467062335517360668147484509715867820489264250545955162631744688000021700234855309513301163127479143820500492011
>   public exponent: 65537
>   Validity: [From: Tue Jul 19 16:47:34 EDT 2016,
>                To: Fri Jul 19 16:47:34 EDT 2019]
>   Issuer: CN=Forest CA RSA 4
>   SerialNumber: [    578e91a9]
>
> Certificate Extensions: 4
> [1]: ObjectId: 2.5.29.19 Criticality=true
> BasicConstraints:[
>   CA:false
>   PathLen: undefined
> ]
>
> [2]: ObjectId: 2.5.29.37 Criticality=true
> ExtendedKeyUsages [
>   clientAuth
>   codeSigning
>   1.3.6.1.4.1.311.10.3.12
>   1.2.840.113583.1.1.5
>   emailProtection
> ]
>
> [3]: ObjectId: 2.5.29.15 Criticality=true
> KeyUsage [
>   DigitalSignature
>   Non_repudiation
> ]
>
> [4]: ObjectId: 2.5.29.17 Criticality=false
> SubjectAlternativeName [
>   RFC822Name: [hidden email]
> ]
>
> ]
>   Algorithm: [SHA256withRSA]
>   Signature:
> 0000: 80 26 B1 ED BF 85 E8 68   5B 1B BC AB 79 57 27 24  .&.....h[...yW'$
> 0010: 94 EA 12 25 74 A5 2A 0B   EE FB 1E EB 92 78 DF 42  ...%t.*......x.B
> . . . . .
> 0170: 12 59 78 24 3C B7 C4 FE   AC AD 97 E9 C7 83 F0 C9  .Yx$<...........
>
> ]
> key.class=class org.opensc.pkcs11.wrap.PKCS11NeRSAPrivateKey
> sig.provider=OpenSC-PKCS11
> . . . . .
> 107: C_SignInit
>
> 2016-08-22 23:31:25.244
> About to clean up and tear down OpenSC-PKCS11 provider...
> [in] hSession = 0x7fe37a62b610
> pMechanism->type=CKM_SHA256_RSA_PKCS          
> [in] hKey = 0x7fe37a4056a0
> Returned:  0 CKR_OK
>
> 108: C_SignUpdate
> 2016-08-22 23:31:25.244
> [in] hSession = 0x7fe37a62b610
> [in] pPart[ulPartLen] 0000700000218aa0 / 256
>     00000000  93 86 4C ED 2B 92 11 95 EF 49 64 76 1C 2B B8 7F  ..L.+....Idv.+.
>     . . . . .
>     000000E0  0E 5C AC 40 3F BA 39 C0 3E 07 FE 7D C0 DA C9 BD  .\.@?.9.>..}....
>     000000F0  D9 20 D7 BF D1 2B 75 31 F6 3D CF 92 56 C6 22 37  . ...+u1.=..V."7
> Returned:  0 CKR_OK
>
> java.lang.NullPointerException
> at org.opensc.pkcs11.wrap.PKCS11Session.sign(Unknown Source)
> at org.opensc.pkcs11.spi.PKCS11SignatureSpi.engineSign(Unknown Source)
> at java.security.Signature$Delegate.engineSign(Signature.java:1207)
> at java.security.Signature.sign(Signature.java:579)
> at burrow.pkcs11.PKCS11Test2.testSignature(PKCS11Test2.java:180)
> at burrow.pkcs11.PKCS11Test2.main(PKCS11Test2.java:286)
> 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:140)
>
>> On 08/21/2016 07:03 AM, Uri Blumenthal wrote:
>>> Andreas,
>>>
>>> Trying to test your OpenSC-Java fork (adapting the tests from the original OpenSC/OpenSC-Java/pkcs11-test), I stumbled upon more problems. Not sure whether they’re caused by me not understanding how to use it, or the issues with the library itself. In any case, I’d really appreciate your guidance.
>>>
>>> Here are the problems:
>>>
>>> 1. KeyStore test - retrieves all the certificates (though it assigns to them strange aliases based on CN), but fails to get certificate chain (that call returns null).
>>>
>>> Code:
>>>  Certificate certificate = ks.getCertificate(alias); // works
>>>  printCert( (X509Certificate) certificate ); // works
>>>  Certificate [] chain = ks.getCertificateChain(alias); // returns chain==null
>>>
>>> 2. Signature test fails on the last operation (actually performing signature on the token).
>>>
>>> Code:
>>>  Signature sig = Signature.getInstance(“SHA256withRSA”
>>>                                      /* , "OpenSC-PKCS11"
>>>                                        provider*/);
>>>  sig.initSign((PrivateKey)key);
>>>  System.out.println("sig.provider=" +
>>>                     sig.getProvider().getName());
>>>
>>>  sig.update(this.testData);
>>>  byte[] signature = sig.sign(); // line #166
>>>
>>> Stack trace:
>>> key.class=class org.opensc.pkcs11.wrap.PKCS11NeRSAPrivateKey
>>> sig.provider=OpenSC-PKCS11
>>> Exception in thread "main" java.lang.NullPointerException
>>> at org.opensc.pkcs11.wrap.PKCS11Session.sign(Unknown Source)
>>> at org.opensc.pkcs11.spi.PKCS11SignatureSpi.engineSign(Unknown Source)
>>> at java.security.Signature$Delegate.engineSign(Signature.java:1207)
>>> at java.security.Signature.sign(Signature.java:579)
>>> at burrow.pkcs11.PKCS11Test2.testSignature(PKCS11Test2.java:166)
>>> at burrow.pkcs11.PKCS11Test2.main(PKCS11Test2.java:250)
>>> 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:140)
>>>
>>>
>>> 3. Decryption test fails similarly to above.
>>>
>>> Code:
>>>  System.out.println("key.class=" + key.getClass());
>>>  Cipher enc = Cipher.getInstance("RSA/ECB/PKCS1Padding");
>>>  enc.init(Cipher.ENCRYPT_MODE, certificate); // this is the line #206
>>>  byte[] encData = enc.doFinal(this.testData);
>>>
>>> Stack trace:
>>> key.class=class org.opensc.pkcs11.wrap.PKCS11NeRSAPrivateKey
>>> Exception in thread "main" java.security.InvalidKeyException: Wrong key usage
>>> at javax.crypto.Cipher.init(Cipher.java:1712)
>>> at javax.crypto.Cipher.init(Cipher.java:1618)
>>> at burrow.pkcs11.PKCS11Test2.testDecryption(PKCS11Test2.java:206)
>>> at burrow.pkcs11.PKCS11Test2.main(PKCS11Test2.java:244)
>>> 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:140)
>>>
>>> Key usage:
>>> ExtendedKeyUsages [
>>>  emailProtection
>>>  1.3.6.1.4.1.311.10.3.4
>>>  anyExtendedKeyUsage
>>> ]
>>>
>>> [3]: ObjectId: 2.5.29.15 Criticality=true
>>> KeyUsage [
>>>  Key_Encipherment
>>> ]
>>>
>>>
>>>> On Aug 18, 2016, at 23:37 , Uri Blumenthal <[hidden email]> wrote:
>>>>
>>>> Andreas,
>>>>
>>>> I’ve found a few problems with the build, and filed issues on your GitHub repo.
>>>>
>>>> In short, (a) the script does not initiate JNI rebuild, but uses the prebuilt copy instead (bad because it got the library path wrong), and (b) the script doesn’t re-create the “java/lib” directory that “ant clean” removes.
>
> --
> Uri Blumenthal
> [hidden email]
>


--

    ---------    CardContact Systems GmbH
   |.##> <##.|   Schülerweg 38
   |#       #|   D-32429 Minden, Germany
   |#       #|   Phone +49 571 56149
   |'##> <##'|   http://www.cardcontact.de
    ---------    Registergericht Bad Oeynhausen HRB 14880
                 Geschäftsführer Andreas Schwier

Reply | Threaded
Open this post in threaded view
|

Re: Unable to sign using PKCS#11 tokens

Uri Blumenthal
On Aug 23, 2016, at 9:47 , Andreas Schwier <[hidden email]> wrote:
>> Dear Uri,
>>
>> I've posted a fix to the opensc-pkcs11 repo that remove the NPE when
>> doing a sign.

> Andreas,  thank you very much! Your fix for signature worked perfectly - see below.
>
>> The problem with decryption is actually caused by JUNITs assertEqual()
>> which does not compare the content of the array but the reference.
>
> Unfortunately, while you are correct (and decryption did have this problem - thank you for the fix!), the code has not reached it yet. It blows before that, trying to instantiate RSA cipher in encryption mode using certificate as a key.
>>
>> When replaced by assertTrue(Arrays.equals(this.testData, origData)) it
>> worked for me.
>
> In my case it does not even live so long as to actually try to decrypt, let alone compare the results (see below). The method name is confusing - it’s named “testDecryption()”, but it attempts to encrypt first, and here it fails to do so.
>
> If you look at the error stated in the exception - it does not seem to accept the certificate as the correct key:
>
> key.class=class org.opensc.pkcs11.wrap.PKCS11NeRSAPrivateKey
> java.security.InvalidKeyException: Wrong key usage
> at javax.crypto.Cipher.init(Cipher.java:1712)
> at javax.crypto.Cipher.init(Cipher.java:1618)
> at burrow.pkcs11.PKCS11Test2.testDecryption(PKCS11Test2.java:238)
> at burrow.pkcs11.PKCS11Test2.main(PKCS11Test2.java:292)
> 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:140)
>
> Lines 237-238 in PKCS11Test2.java are:
> Cipher enc = Cipher.getInstance("RSA/ECB/PKCS1Padding”);
> enc.init(Cipher.ENCRYPT_MODE, certificate);
>
> With the KeyStore - it appears that certificate chain should have been of length 1 (as it only has the head certificate, but no CA chain included), but instead the call returns null-pointer (no chain at all). I think it is a problem.
>
>> I've done a regression test with the other test cases and a
>> SmartCard-HSM equipped with sample keys and certificates.
>
> It’s great, of course - but perhaps you still could help me get the code working with YubiKey and CAC. I think the library (or maybe its C part that’s invoked via JNI) still has a couple of problems, because I can encrypt/decrypt using SunPKCS11 (which I admit is less convenient). (Of course I could also sign and verify with SunPKCS11 - but with your current fix it works equally well with opensc-java, thank you!) Here’s the complete log:
>
> /Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/bin/java -Didea.launcher.port=7538 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA 14.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/lib/tools.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Users/uri/SkyDrive/pkcs11Test/out/production/pkcs11Test:/Library/Java/Extensions/commons-logging-1.2.jar:/Library/Java/Extensions/junit-4.12.jar:/Library/Java/Extensions/bcprov-ext-jdk15on-155.jar:/Library/Java/Extensions/opensc-java.jar:/Applications/IntelliJ IDEA 14.app/Contents/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain burrow.pkcs11.PKCS11Test2
> PKCS11Test2: setting up OpenSC-PKCS11 provider...
> java.library.path = /Users/uri/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
>
>
>
> *************** OpenSC PKCS#11 spy *****************
> Loaded: "/Library/OpenSC/lib/opensc-pkcs11.dylib"
>
> 0: C_GetFunctionList
> 2016-08-23 22:49:15.490
> Returned:  0 CKR_OK
>
> 1: C_Initialize
> 2016-08-23 22:49:15.490
> [in] pInitArgs = 0x11bb730b0
>     flags: 2
>       CKF_OS_LOCKING_OK
> Returned:  0 CKR_OK
>
> 2: C_GetInfo
> 2016-08-23 22:49:16.226
> [out] pInfo:
>      cryptokiVersion:         2.20
>      manufacturerID:         'OpenSC Project                  '
>      flags:                   0
>      libraryDescription:     'OpenSC smartcard framework      '
>      libraryVersion:          0.16
> Returned:  0 CKR_OK
> OpenSC-PKCS11 provider added.
>
> About to test KeyStore...
> Testing KeyStore...
>
> 3: C_GetSlotList
> 2016-08-23 22:49:16.714
> [in] tokenPresent = 0x0
> [out] pSlotList:
> Count is 1
> [out] *pulCount = 0x1
> Returned:  0 CKR_OK
>
> 4: C_GetSlotList
> 2016-08-23 22:49:16.716
> [in] tokenPresent = 0x0
> [out] pSlotList:
> Slot 0
> [out] *pulCount = 0x1
> Returned:  0 CKR_OK
>
> 5: C_GetSlotInfo
> 2016-08-23 22:49:16.717
> [in] slotID = 0x0
> [out] pInfo:
>      slotDescription:        'Yubico Yubikey NEO OTP+U2F+CCID '
>                              '                                '
>      manufacturerID:         'Yubico                          '
>      hardwareVersion:         3.70
>      firmwareVersion:         0.0
>      flags:                   7
>        CKF_TOKEN_PRESENT                
>        CKF_REMOVABLE_DEVICE            
>        CKF_HW_SLOT                      
> Returned:  0 CKR_OK
>
> 6: C_GetTokenInfo
> 2016-08-23 22:49:16.719
> [in] slotID = 0x0
> [out] pInfo:
>      label:                  'PIV Card Holder pin: PIV_II     '
>      manufacturerID:         'piv_II                          '
>      model:                  'PKCS#15 emulated'
>      serialNumber:           ’93. . . . . .77'
>      ulMaxSessionCount:       0
>      ulSessionCount:          0
>      ulMaxRwSessionCount:     0
>      ulRwSessionCount:        0
>      ulMaxPinLen:             8
>      ulMinPinLen:             4
>      ulTotalPublicMemory:     -1
>      ulFreePublicMemory:      -1
>      ulTotalPrivateMemory:    -1
>      ulFreePrivateMemory:     -1
>      hardwareVersion:         0.0
>      firmwareVersion:         0.0
>      time:                   '                '
>      flags:                   40d
>        CKF_RNG                          
>        CKF_LOGIN_REQUIRED              
>        CKF_USER_PIN_INITIALIZED        
>        CKF_TOKEN_INITIALIZED            
> Returned:  0 CKR_OK
>
> 7: C_OpenSession
> 2016-08-23 22:49:16.741
> [in] slotID = 0x0
> [in] flags = 0x4
> pApplication=0x0
> Notify=0x0
> [out] *phSession = 0x7feed0c46850
> Returned:  0 CKR_OK
>
> 8: C_Login
> 2016-08-23 22:49:24.237
> [in] hSession = 0x7feed0c46850
> [in] userType = CKU_USER
> [in] pPin[ulPinLen] . . . . .          
> Returned:  0 CKR_OK
>
> 9: C_FindObjectsInit
> 2016-08-23 22:49:24.275
> [in] hSession = 0x7feed0c46850
> [in] pTemplate[1]:
>    CKA_CLASS             CKO_PRIVATE_KEY      
> Returned:  0 CKR_OK
>
> 10: C_FindObjects
> 2016-08-23 22:49:24.275
> [in] hSession = 0x7feed0c46850
> [in] ulMaxObjectCount = 0x20
> [out] ulObjectCount = 0x4
> Object 0x7feed0f35890 matches
> Object 0x7feed0f37750 matches
> Object 0x7feed0f377b0 matches
> Object 0x7feed0f37810 matches
> Returned:  0 CKR_OK
>
> 11: C_FindObjectsFinal
> 2016-08-23 22:49:24.275
> [in] hSession = 0x7feed0c46850
> Returned:  0 CKR_OK
>
> 12: C_GetAttributeValue
> 2016-08-23 22:49:24.276
> [in] hSession = 0x7feed0c46850
> [in] hObject = 0x7feed0f35890
> [in] pTemplate[1]:
>    CKA_KEY_TYPE          0000700000182bc8 / 8
> [out] pTemplate[1]:
>    CKA_KEY_TYPE          CKK_RSA            
> Returned:  0 CKR_OK
>
> 13: C_GetAttributeValue
> 2016-08-23 22:49:24.276
> [in] hSession = 0x7feed0c46850
> [in] hObject = 0x7feed0f35890
> [in] pTemplate[1]:
>    CKA_EXTRACTABLE       0000700000182bcf / 1
> [out] pTemplate[1]:
>    CKA_EXTRACTABLE       False
> Returned:  0 CKR_OK
>
> 14: C_GetAttributeValue
> 2016-08-23 22:49:24.278
> [in] hSession = 0x7feed0c46850
> [in] hObject = 0x7feed0f35890
> [in] pTemplate[1]:
>    CKA_ID                0000000000000000 / 0
> [out] pTemplate[1]:
>    CKA_ID                0000000000000000 / 1
> Returned:  0 CKR_OK
>
> 15: C_GetAttributeValue
> 2016-08-23 22:49:24.278
> [in] hSession = 0x7feed0c46850
> [in] hObject = 0x7feed0f35890
> [in] pTemplate[1]:
>    CKA_ID                0000700000182940 / 1
> [out] pTemplate[1]:
>    CKA_ID                0000700000182940 / 1
>    00000000  01                                               .              
> Returned:  0 CKR_OK
>
> 16: C_GetAttributeValue
> 2016-08-23 22:49:24.279
> [in] hSession = 0x7feed0c46850
> [in] hObject = 0x7feed0f35890
> [in] pTemplate[1]:
>    CKA_LABEL             0000000000000000 / 0
> [out] pTemplate[1]:
>    CKA_LABEL             0000000000000000 / 12
> Returned:  0 CKR_OK
>
> 17: C_GetAttributeValue
> 2016-08-23 22:49:24.279
> [in] hSession = 0x7feed0c46850
> [in] hObject = 0x7feed0f35890
> [in] pTemplate[1]:
>    CKA_LABEL             0000700000182950 / 12
> [out] pTemplate[1]:
>    CKA_LABEL             0000700000182950 / 12
>    50495620 41555448 206B6579
>     P I V .  A U T H  . k e y
> Returned:  0 CKR_OK
>
> 18: C_GetAttributeValue
> 2016-08-23 22:49:24.280
> [in] hSession = 0x7feed0c46850
> [in] hObject = 0x7feed0f35890
> [in] pTemplate[1]:
>    CKA_KEY_TYPE          00007000001829d8 / 8
> [out] pTemplate[1]:
>    CKA_KEY_TYPE          CKK_RSA            
> Returned:  0 CKR_OK
>
> 19: C_GetAttributeValue
> 2016-08-23 22:49:24.280
> [in] hSession = 0x7feed0c46850
> [in] hObject = 0x7feed0f35890
> [in] pTemplate[1]:
>    CKA_MODULUS_BITS      00007000001829d8 / 8
> [out] pTemplate[1]:
>    CKA_MODULUS_BITS      00007000001829d8 / 8
>    00000000  00 08 00 00 00 00 00 00                          ........        
> Returned:  0 CKR_OK
>
> 20: C_GetAttributeValue
> 2016-08-23 22:49:24.282
> [in] hSession = 0x7feed0c46850
> [in] hObject = 0x7feed0f35890
> [in] pTemplate[1]:
>    CKA_SENSITIVE         0000700000182a5f / 1
> [out] pTemplate[1]:
>    CKA_SENSITIVE         True
> Returned:  0 CKR_OK
>
> 21: C_GetAttributeValue
> 2016-08-23 22:49:24.282
> [in] hSession = 0x7feed0c46850
> [in] hObject = 0x7feed0f35890
> [in] pTemplate[1]:
>    CKA_MODULUS           0000000000000000 / 0
> [out] pTemplate[1]:
>    CKA_MODULUS           0000000000000000 / 256
> Returned:  0 CKR_OK
>
> < removing a bunch of messages/log entries from KeyStore test output>

> 94: C_GetAttributeValue
> 2016-08-23 22:49:24.323
> [in] hSession = 0x7feed0c46850
> [in] hObject = 0x7feed0f39880
> [in] pTemplate[1]:
>    CKA_SUBJECT           0000700000182b30 / 26
> [out] pTemplate[1]:
>    CKA_SUBJECT           0000700000182b30 / 26
>    00000000  30 18 31 16 30 14 06 03 55 04 03 0C 0D 55 72 69  0.1.0...U....Uri
>    00000010  20 74 68 65 20 47 72 65 61 74                     the Great      
>    DN: CN=Uri the Great
> Returned:  0 CKR_OK
>
> 95: C_GetAttributeValue
> 2016-08-23 22:49:24.325
> [in] hSession = 0x7feed0c46850
> [in] hObject = 0x7feed0f39880
> [in] pTemplate[1]:
>    CKA_ISSUER            0000000000000000 / 0
> [out] pTemplate[1]:
>    CKA_ISSUER            0000000000000000 / 28
> Returned:  0 CKR_OK
>
> 96: C_GetAttributeValue
> 2016-08-23 22:49:24.325
> [in] hSession = 0x7feed0c46850
> [in] hObject = 0x7feed0f39880
> [in] pTemplate[1]:
>    CKA_ISSUER            0000700000182b30 / 28
> [out] pTemplate[1]:
>    CKA_ISSUER            0000700000182b30 / 28
>    00000000  30 1A 31 18 30 16 06 03 55 04 03 0C 0F 46 6F 72  0.1.0...U....For
>    00000010  65 73 74 20 43 41 20 52 53 41 20 34              est CA RSA 4    
>    DN: CN=Forest CA RSA 4
> Returned:  0 CKR_OK
>
> 97: C_GetAttributeValue
> 2016-08-23 22:49:24.326
> [in] hSession = 0x7feed0c46850
> [in] hObject = 0x7feed0f39880
> [in] pTemplate[1]:
>    CKA_SERIAL_NUMBER     0000000000000000 / 0
> [out] pTemplate[1]:
>    CKA_SERIAL_NUMBER     0000000000000000 / 6
> Returned:  0 CKR_OK
>
> 98: C_GetAttributeValue
> 2016-08-23 22:49:24.326
> [in] hSession = 0x7feed0c46850
> [in] hObject = 0x7feed0f39880
> [in] pTemplate[1]:
>    CKA_SERIAL_NUMBER     0000700000182b40 / 6
> [out] pTemplate[1]:
>    CKA_SERIAL_NUMBER     0000700000182b40 / 6
>    00000000  02 04 57 8E 92 4A                                ..W..J          
> Returned:  0 CKR_OK
>
> Got aliases...
> 99: C_GetAttributeValue
> alias="CN=Uri the Great_02"
> isKey=true
> isCertificate=true
> 2016-08-23 22:49:24.332
> [in] hSession = 0x7feed0c46850
> [in] hObject = 0x7feed0f38ab0
> [in] pTemplate[1]:
>    CKA_VALUE             0000000000000000 / 0
> [out] pTemplate[1]:
>    CKA_VALUE             0000000000000000 / 943
> Returned:  0 CKR_OK
>
> 100: C_GetAttributeValue
> 2016-08-23 22:49:24.332
> [in] hSession = 0x7feed0c46850
> [in] hObject = 0x7feed0f38ab0
> [in] pTemplate[1]:
>    CKA_VALUE             0000700000182820 / 943
> [out] pTemplate[1]:
>    CKA_VALUE             0000700000182820 / 943
>    00000000  30 82 03 AB 30 82 02 13 A0 03 02 01 02 02 04 57  0...0..........W
>    00000010  8E 91 A9 30 0D 06 09 2A 86 48 86 F7 0D 01 01 0B  ...0...*.H……
>    . . . . .
>    000003A0  59 78 24 3C B7 C4 FE AC AD 97 E9 C7 83 F0 C9     Yx$<...........
> Returned:  0 CKR_OK
> certAlias=CN=Uri the Great_02
> subject=CN=Uri the Great
> issuer=CN=Forest CA RSA 4
> serial=1468961193
> chain is null!
> alias="CN=Uri the Great_03"
> isKey=true
> isCertificate=true
>
> 101: C_GetAttributeValue
> 2016-08-23 22:49:24.360
> [in] hSession = 0x7feed0c46850
> [in] hObject = 0x7feed0f37ad0
> [in] pTemplate[1]:
>    CKA_VALUE             0000000000000000 / 0
> [out] pTemplate[1]:
>    CKA_VALUE             0000000000000000 / 946
> Returned:  0 CKR_OK
>
> 102: C_GetAttributeValue
> 2016-08-23 22:49:24.360
> [in] hSession = 0x7feed0c46850
> [in] hObject = 0x7feed0f37ad0
> [in] pTemplate[1]:
>    CKA_VALUE             0000700000182810 / 946
> [out] pTemplate[1]:
>    CKA_VALUE             0000700000182810 / 946
>    00000000  30 82 03 AE 30 82 02 16 A0 03 02 01 02 02 04 57  0...0..........W
>    . . . . .
>    000003B0  C2 CB                                            ..              
> Returned:  0 CKR_OK
> certAlias=CN=Uri the Great_03
> subject=CN=Uri the Great
> issuer=CN=Forest CA RSA 4
> serial=1468961298
> chain is null!
> alias="CN=Uri the Great"
> isKey=true
> isCertificate=true
>
> 103: C_GetAttributeValue
> 2016-08-23 22:49:24.374
> [in] hSession = 0x7feed0c46850
> [in] hObject = 0x7feed0f374c0
> [in] pTemplate[1]:
>    CKA_VALUE             0000000000000000 / 0
> [out] pTemplate[1]:
>    CKA_VALUE             0000000000000000 / 932
> Returned:  0 CKR_OK
>
> < removed a few output records from KeyStore test>                                    
>                
> Returned:  0 CKR_OK
> certAlias=CN=Uri the Great_04
> subject=CN=Uri the Great
> issuer=CN=Forest CA RSA 4
> serial=1468961354
> chain is null!
> Finished KeyStore test.
>
> KeyStore test completed.
>
>
> About to test Encryption/Decryption...
> Testing Encryption and Decryption...
> certificate=[
> [
>  Version: V3
>  Subject: CN=Uri the Great
>  Signature Algorithm: SHA256withRSA, OID = 1.2.840.113549.1.1.11
>
>  Key:  Sun RSA public key, 2048 bits
>  modulus: 216636537163639295329601575623758746841986454908068652275462888815403561225933774972229679290133275236290322290470422003457837583071875887457816871975943945371505251213. . . . . 70487964992454222247216689479890468213504711905378857438083651226661277624606981419976165303850607087253148979619
>  public exponent: 65537
>  Validity: [From: Tue Jul 19 16:48:53 EDT 2016,
>               To: Fri Jul 19 16:48:53 EDT 2019]
>  Issuer: CN=Forest CA RSA 4
>  SerialNumber: [    578e9212]
>
> Certificate Extensions: 5
> [1]: ObjectId: 2.5.29.19 Criticality=true
> BasicConstraints:[
>  CA:false
>  PathLen: undefined
> ]
>
> [2]: ObjectId: 2.5.29.37 Criticality=false
> ExtendedKeyUsages [
>  emailProtection
>  1.3.6.1.4.1.311.10.3.4
>  anyExtendedKeyUsage
> ]
>
> [3]: ObjectId: 2.5.29.15 Criticality=true
> KeyUsage [
>  Key_Encipherment
> ]
>
> [4]: ObjectId: 2.5.29.17 Criticality=false
> SubjectAlternativeName [
>  RFC822Name: [hidden email]
> ]
>
> [5]: ObjectId: 2.5.29.14 Criticality=false
> SubjectKeyIdentifier [
> KeyIdentifier [
> 0000: 8F B8 1E DA 41 1B F1 F4   94 CB 05 9A F6 4A 0B C0  ....A........J..
> 0010: 73 13 0C 59                                        s..Y
> ]
> ]
>
> ]
>  Algorithm: [SHA256withRSA]
>  Signature:
> 0000: 2E FD C6 58 72 DB B0 4B   A8 64 27 97 75 6E BB 4C  ...Xr..K.d'.un.L
> 0010: E6 D4 3E 24 E0 8E 85 AD   F5 68 BC 92 7B 0B 25 DA  ..>$.....h....%.
> . . . . .
> 0170: E5 DA 76 DD 04 28 FE 82   55 15 F5 B7 B3 97 C2 CB  ..v..(..U.......
>
> ]
> key.class=class org.opensc.pkcs11.wrap.PKCS11NeRSAPrivateKey
> java.security.InvalidKeyException: Wrong key usage
> at javax.crypto.Cipher.init(Cipher.java:1712)
> at javax.crypto.Cipher.init(Cipher.java:1618)
> at burrow.pkcs11.PKCS11Test2.testDecryption(PKCS11Test2.java:238)
> at burrow.pkcs11.PKCS11Test2.main(PKCS11Test2.java:292)
> 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:140)
> Finished Encryption and Decryption test.
>
> Encryption/Decryption test completed.
>
> About to test Signature...
> Testing Signature…
>
> <Signature test passed with flying colors>
>
> Finished Signature test.
>
> Signature test completed.
>
> About to clean up and tear down OpenSC-PKCS11 provider...
>
> 111: C_CloseSession
> 2016-08-23 22:49:25.201
> [in] hSession = 0x7feed0c46850
> Returned:  0 CKR_OK
>
> 112: C_Finalize
> 2016-08-23 22:49:25.201
> Returned:  0 CKR_OK
> Tear-down completed. Exiting.
>
>
> Process finished with exit code 0
> --
> Uri Blumenthal
> [hidden email]
>

--
Uri Blumenthal
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Unable to sign using PKCS#11 tokens

Uri Blumenthal
Dear Andreas,

After more testing I found that RSA encryption does not work unless the Key Usage attribute has “Data Encipherment” specified (which my cert did not have).

Once I addressed that (though I’m very reluctant to add Data Encipherment to the Key Usage) - encryption and decryption started working, confirming what you observed with SmartCard-HSM.

My only two remaining questions (at this time :) are:
  1. How to fix KeyStore access? I’d like to be able to retrieve certificate chain even if it’s of length 1 (rather than receiving null-pointer in return), and I’d like to be able to retrieve certificates by their aliases like “Certificate for Key Management”.
  2. How to rebuild the shared library (libopensc-PKCS11-x86_64.dylib) from the source? It looks like the build-without-ivy.xml script only takes the prebuilt library…

Thanks again! Nice work!


On Aug 23, 2016, at 23:24 , Uri Blumenthal <[hidden email]> wrote:

On Aug 23, 2016, at 9:47 , Andreas Schwier <[hidden email]> wrote:
Dear Uri,

I've posted a fix to the opensc-pkcs11 repo that remove the NPE when
doing a sign.

Andreas,  thank you very much! Your fix for signature worked perfectly - see below.

The problem with decryption is actually caused by JUNITs assertEqual()
which does not compare the content of the array but the reference.

Unfortunately, while you are correct (and decryption did have this problem - thank you for the fix!), the code has not reached it yet. It blows before that, trying to instantiate RSA cipher in encryption mode using certificate as a key.

When replaced by assertTrue(Arrays.equals(this.testData, origData)) it
worked for me.

In my case it does not even live so long as to actually try to decrypt, let alone compare the results (see below). The method name is confusing - it’s named “testDecryption()”, but it attempts to encrypt first, and here it fails to do so. 

If you look at the error stated in the exception - it does not seem to accept the certificate as the correct key:

key.class=class org.opensc.pkcs11.wrap.PKCS11NeRSAPrivateKey
java.security.InvalidKeyException: Wrong key usage
at javax.crypto.Cipher.init(Cipher.java:1712)
at javax.crypto.Cipher.init(Cipher.java:1618)
at burrow.pkcs11.PKCS11Test2.testDecryption(PKCS11Test2.java:238)
at burrow.pkcs11.PKCS11Test2.main(PKCS11Test2.java:292)
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:140)

Lines 237-238 in PKCS11Test2.java are:
Cipher enc = Cipher.getInstance("RSA/ECB/PKCS1Padding”);
enc.init(Cipher.ENCRYPT_MODE, certificate);

With the KeyStore - it appears that certificate chain should have been of length 1 (as it only has the head certificate, but no CA chain included), but instead the call returns null-pointer (no chain at all). I think it is a problem.
--
Uri Blumenthal

Reply | Threaded
Open this post in threaded view
|

Re: Unable to sign using PKCS#11 tokens

Andreas Schwier-2
Correct. Forgot to mention that I replaced "certificate" with
"certificate.getPublicKey()" in enc.init().

Regarding the key store issue I will need to take a closer look what is
going on.

There is currently no cross-platform build for JNI. You need to change
into the desired platform directory (e.g. jni/build/unix/release) and
run ../../../src/script/config.pl to generate makefiles and build the JNI.

Andreas

On 08/24/2016 07:03 AM, Uri Blumenthal wrote:

> Dear Andreas,
>
> After more testing I found that RSA encryption does not work unless the Key Usage attribute has “Data Encipherment” specified (which my cert did not have).
>
> Once I addressed that (though I’m very reluctant to add Data Encipherment to the Key Usage) - encryption and decryption started working, confirming what you observed with SmartCard-HSM.
>
> My only two remaining questions (at this time :) are:
>
>   1.  How to fix KeyStore access? I’d like to be able to retrieve certificate chain even if it’s of length 1 (rather than receiving null-pointer in return), and I’d like to be able to retrieve certificates by their aliases like “Certificate for Key Management”.
>   2.  How to rebuild the shared library (libopensc-PKCS11-x86_64.dylib) from the source? It looks like the build-without-ivy.xml script only takes the prebuilt library…
>
> Thanks again! Nice work!
>
>
> On Aug 23, 2016, at 23:24 , Uri Blumenthal <[hidden email]<mailto:[hidden email]>> wrote:
>
> On Aug 23, 2016, at 9:47 , Andreas Schwier <[hidden email]<mailto:[hidden email]>> wrote:
> Dear Uri,
>
> I've posted a fix to the opensc-pkcs11 repo that remove the NPE when
> doing a sign.
>
> Andreas,  thank you very much! Your fix for signature worked perfectly - see below.
>
> The problem with decryption is actually caused by JUNITs assertEqual()
> which does not compare the content of the array but the reference.
>
> Unfortunately, while you are correct (and decryption did have this problem - thank you for the fix!), the code has not reached it yet. It blows before that, trying to instantiate RSA cipher in encryption mode using certificate as a key.
>
> When replaced by assertTrue(Arrays.equals(this.testData, origData)) it
> worked for me.
>
> In my case it does not even live so long as to actually try to decrypt, let alone compare the results (see below). The method name is confusing - it’s named “testDecryption()”, but it attempts to encrypt first, and here it fails to do so.
>
> If you look at the error stated in the exception - it does not seem to accept the certificate as the correct key:
>
> key.class=class org.opensc.pkcs11.wrap.PKCS11NeRSAPrivateKey
> java.security.InvalidKeyException: Wrong key usage
> at javax.crypto.Cipher.init(Cipher.java:1712)
> at javax.crypto.Cipher.init(Cipher.java:1618)
> at burrow.pkcs11.PKCS11Test2.testDecryption(PKCS11Test2.java:238)
> at burrow.pkcs11.PKCS11Test2.main(PKCS11Test2.java:292)
> 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:140)
>
> Lines 237-238 in PKCS11Test2.java are:
> Cipher enc = Cipher.getInstance("RSA/ECB/PKCS1Padding”);
> enc.init(Cipher.ENCRYPT_MODE, certificate);
>
> With the KeyStore - it appears that certificate chain should have been of length 1 (as it only has the head certificate, but no CA chain included), but instead the call returns null-pointer (no chain at all). I think it is a problem.
> --
> Uri Blumenthal
> [hidden email]<mailto:[hidden email]>
>


--

    ---------    CardContact Systems GmbH
   |.##> <##.|   Schülerweg 38
   |#       #|   D-32429 Minden, Germany
   |#       #|   Phone +49 571 56149
   |'##> <##'|   http://www.cardcontact.de
    ---------    Registergericht Bad Oeynhausen HRB 14880
                 Geschäftsführer Andreas Schwier

Reply | Threaded
Open this post in threaded view
|

Re: Unable to sign using PKCS#11 tokens

Uri Blumenthal
On Aug 24, 2016, at 4:45 , Andreas Schwier <[hidden email]> wrote:
> Correct. Forgot to mention that I replaced "certificate" with
> "certificate.getPublicKey()" in enc.init().

:-)  Yes, that made a big difference. Thank you!

> Regarding the key store issue I will need to take a closer look what is
> going on.

If it helps, your latest code reports more on the problem it thinks it’s having:

10:57:24.191 [main] ERROR org.opensc.pkcs11.spi.PKCS11KeyStoreSpi - Exception caught during analysis of the certificate chain:
java.security.cert.CertificateException: Cannot find the issuing CA for certificate [[
[
  Version: V3
  Subject: CN=Uri the Great
  Signature Algorithm: SHA256withRSA, OID = 1.2.840.113549.1.1.11

  Key:  Sun RSA public key, 2048 bits
  modulus: 19878901573961887352085. . . . .4282869504512800375579936272793
  public exponent: 65537
  Validity: [From: Tue Aug 09 09:52:09 EDT 2016,
               To: Fri Aug 09 09:52:09 EDT 2019]
  Issuer: CN=Forest CA RSA 4
  SerialNumber: [    57a9dfb6]

Certificate Extensions: 4
[1]: ObjectId: 2.5.29.19 Criticality=true
BasicConstraints:[
  CA:false
  PathLen: undefined
]

[2]: ObjectId: 2.5.29.37 Criticality=false
ExtendedKeyUsages [
  clientAuth
  1.3.6.1.4.1.311.20.2.2
]

[3]: ObjectId: 2.5.29.15 Criticality=true
KeyUsage [
  DigitalSignature
]

[4]: ObjectId: 2.5.29.17 Criticality=false
SubjectAlternativeName [
  Other-Name: Unrecognized ObjectIdentifier: 1.3.6.1.4.1.311.20.2.3
]

]
  Algorithm: [SHA256withRSA]
  Signature:
0000: 06 69 12 31 E6 0A 2E 65   AC EB 33 84 96 D9 91 E2  .i.1...e..3.....
0010: 72 2B 40 73 2E 58 DB D1   08 8E 66 19 B8 9C 04 78  [hidden email]....f....x
. . . . .
0170: 9E F9 0F 26 D2 7F 81 F6   3A 0E 22 C0 06 A3 DF B2  ...&....:.".....

]].
        at org.opensc.pkcs11.spi.PKCS11KeyStoreSpi.engineGetCertificateChain(Unknown Source) [opensc-java.jar:?]
        at java.security.KeyStore.getCertificateChain(KeyStore.java:1048) [?:1.8.0_102]
        at burrow.pkcs11.PKCS11Test2.testKeyStore(PKCS11Test2.java:120) [pkcs11Test/:?]
        at burrow.pkcs11.PKCS11Test2.main(PKCS11Test2.java:312) [pkcs11Test/:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_102]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_102]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_102]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_102]
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) [idea_rt.jar:?]
chain is null!


> There is currently no cross-platform build for JNI. You need to change
> into the desired platform directory (e.g. jni/build/unix/release) and
> run ../../../src/script/config.pl to generate makefiles and build the JNI.

Understand, thank you!  Might I suggest that you add this to the README.md file?

Thanks!
--
Uri





smime.p7s (3K) Download Attachment
12