How to avoid Pkcs11Interop exception Method C_OpenSession returned CKR_TOKEN_NOT_RECOGNIZED

48 Views Asked by At

In a C# desktop app I want to get information about slots and certificates.

To loop through the slots I use the following code. During this loop slot.OpenSession on a slot throws a Method C_OpenSession returned CKR_TOKEN_NOT_RECOGNIZED error.

(1) How can I check or detect that a session cannot be opened?

My assumption (but I do not know) is that it is the "Windows_Hello_for_Business" or the "Microsoft_IFD" card readers on my PC.

(2) is there only a GetSlotList method? If I have a DeviceID such as "\?\SWD#ScDeviceEnum#1_Gemalto_USB_Key_Smart_Card_Reader_0#{deebe6ad-9e01-47e2-a3b2-a66aa2c036c9}" is there any possibility to simply load this?

using (IPkcs11Library Pkcs11Library = PkcsFactories.Pkcs11LibraryFactory.LoadPkcs11Library(PkcsFactories, PKCS11LIBRARYPATH, AppType.MultiThreaded))
{
    List<ISlot> Slots = Pkcs11Library.GetSlotList(SlotsType.WithTokenPresent);
    foreach (ISlot slot in Slots)
    {
        Log($"GetAllAvailableCertificates - Slotid: {slot.SlotId}");
        try
        {
            using (ISession sess = slot.OpenSession(SessionType.ReadOnly))
            {
                
            }
        }
        catch (Exception ex)
        {
            Log($"GetAllAvailableCertificates - Error generally - Message: ", $"{ex}");
        }
    }
}
0

There are 0 best solutions below