1

I set up pkcs11Interop Library as follows

Pkcs11Library = PkcsFactories.Pkcs11LibraryFactory.LoadPkcs11Library(PkcsFactories, pkcs11LibraryPath, AppType.MultiThreaded)

I then immediately use a WPF DispatcherTimer which contains the WaitForSlotEvent

        Private Sub CardInsertedTimer_Tick(sender As Object, e As EventArgs)
            Dim eventHappened As Boolean
                Dim id As ULong

                Pkcs11Library.WaitForSlotEvent(WaitType.NonBlocking, eventHappened, id)

            If eventHappened Then
                Debug.Print("CardInsertedEvent - Event - Event: " + eventHappened.ToString + "  ID: " + id.ToString)
            End If
        End Sub

I hope there are no surprises here. If the Slot (which is a USB smart card reader) is removed then I get a C_WaitForSlotEvent returned CKR_DEVICE_ERROR.

  • Is a DispatcherTimer ok for WaitForSlotEvent?
  • What should I do to avoid C_WaitForSlotEvent CKR_DEVICE_ERROR?
  • Does pkcs11Interop handle the adding and removing of slots (ie. SmartCard Readers)?
  • If pkcs11Interop does not handle adding and removing slots is polling the only way and what would be the best to poll?
darbid
  • 2,545
  • 23
  • 55

1 Answers1

2

> Is a DispatcherTimer ok for WaitForSlotEvent?

It should be OK in general. You might also consider using WaitForSlotEvent in blocking mode called from a separate thread.

> What should I do to avoid C_WaitForSlotEvent CKR_DEVICE_ERROR?

You need to ask the vendor of your unmanaged PKCS#11 library.

AFAIK C_WaitForSlotEvent was designed for token/card related events not for slot/reader related events. PKCS#11 specification states that when you add or remove slot/reader you might need to reload PKCS#11 library or even restart OS:

On some platforms, or earlier PKCS11 compliant libraries, it may be necessary to successfully call C_Initialize or to restart the entire system.

> Does pkcs11Interop handle the adding and removing of slots (ie. SmartCard Readers)?

Pkcs11Interop does nothing else but gives you access to unmanaged function C_WaitForSlotEvent described in PKCS#11 specification.

> If pkcs11Interop does not handle adding and removing slots is polling the only way and what would be the best to poll?

See answer to first question.

jariq
  • 11,681
  • 3
  • 33
  • 52
  • Thank you. I am dealing with (Atos) CaedOS v5.3 - do you have any idea on avoiding the error? – darbid Feb 23 '21 at 16:28
  • Avoid removing the reader or catch that specific exception and handle it appropriately in your application. – jariq Feb 23 '21 at 21:34