public static CK_SESSION_HANDLE C_OpenSession(CK_SLOT_ID slotID, uint flags = CK.CKF_SERIAL_SESSION | CK.CKF_RW_SESSION)
        {
            uint handle = 0;

            CKR_Exception.check(fl.C_OpenSession(slotID.Id, flags, IntPtr.Zero, IntPtr.Zero, out handle), "C_OpenSession (" + slotID + ")");
            return(new CK_SESSION_HANDLE(handle));
        }
 public static void C_CloseAllSessions(CK_SLOT_ID slot)
 {
     CKR_Exception.check(isWindows ?
                         LibraryWindows.fl.C_CloseAllSessions(slot.Id):
                         LibraryUnix.fl.C_CloseAllSessions(slot.Id)
                         , "C_CloseAllSessions");
 }
        public static CK_MECHANISM_INFO C_GetMechanismInfo(CK_SLOT_ID slot, uint type)
        {
            CK_MECHANISM_INFO info = new CK_MECHANISM_INFO();

            CKR_Exception.check(fl.C_GetMechanismInfo(slot.Id, type, out info), "C_GetMechanismInfo (" + String.Format("{0:X}", type) + ")");
            return(info);
        }
        public static CK_SESSION_HANDLE C_OpenSession(CK_SLOT_ID slotID, uint flags = CK.CKF_SERIAL_SESSION | CK.CKF_RW_SESSION)
        {
            uint  handle     = 0;
            ulong handleUnix = 0;

            CKR_Exception.check(isWindows ?
                                LibraryWindows.fl.C_OpenSession(slotID.Id, flags, IntPtr.Zero, IntPtr.Zero, out handle) :
                                LibraryUnix.fl.C_OpenSession(slotID.Id, flags, IntPtr.Zero, IntPtr.Zero, out handleUnix)
                                , "C_OpenSession (" + slotID + ")");
            return(new CK_SESSION_HANDLE(isWindows ? handle : (uint)handleUnix));
        }
        public static CK_SLOT_ID[] C_GetSlotList(bool present)
        {
            byte tokenPresent = (byte)(present ? 1 : 0);
            int  count        = 0;
            long countUnix    = 0;

            CKR_Exception.check(isWindows ?
                                LibraryWindows.fl.C_GetSlotList(tokenPresent, null, ref count) :
                                LibraryUnix.fl.C_GetSlotList(tokenPresent, null, ref countUnix)
                                , "C_GetSlotList");

            if (!isWindows)
            {
                count = (int)countUnix;
            }
            if (count == 0)
            {
                return(new CK_SLOT_ID[0]);
            }

            uint[]  list     = null;
            ulong[] listUnix = null;

            if (isWindows)
            {
                list = new uint[count];
            }
            else
            {
                listUnix = new ulong[count];
            }

            if (count > 0)
            {
                CKR_Exception.check(isWindows ?
                                    LibraryWindows.fl.C_GetSlotList(tokenPresent, list, ref count):
                                    LibraryUnix.fl.C_GetSlotList(tokenPresent, listUnix, ref countUnix)
                                    , "C_GetSlotList");
            }

            if (!isWindows)
            {
                count = (int)countUnix;
            }
            CK_SLOT_ID[] slots = new CK_SLOT_ID[count];
            for (int i = 0; i < count; i++)
            {
                slots[i] = new CK_SLOT_ID(isWindows ? list[i] : (uint)listUnix[i]);
            }
            return(slots);
        }
 public static CK_MECHANISM_INFO C_GetMechanismInfo(CK_SLOT_ID slot, uint type)
 {
     if (isWindows)
     {
         CK_MECHANISM_INFO.NativeWindows info = new CK_MECHANISM_INFO.NativeWindows();
         CKR_Exception.check(LibraryWindows.fl.C_GetMechanismInfo(slot.Id, type, out info), "C_GetMechanismInfo (" + String.Format("{0:X}", type) + ")");
         return(new CK_MECHANISM_INFO(info));
     }
     else
     {
         CK_MECHANISM_INFO.NativeUnix info = new CK_MECHANISM_INFO.NativeUnix();
         CKR_Exception.check(LibraryUnix.fl.C_GetMechanismInfo(slot.Id, type, out info), "C_GetMechanismInfo (" + String.Format("{0:X}", type) + ")");
         return(new CK_MECHANISM_INFO(info));
     }
 }
 public static CK_TOKEN_INFO C_GetTokenInfo(CK_SLOT_ID slot)
 {
     if (isWindows)
     {
         CK_TOKEN_INFO.NativeWindows native = new CK_TOKEN_INFO.NativeWindows();
         CKR_Exception.check(LibraryWindows.fl.C_GetTokenInfo(slot.Id, out native), "C_GetTokenInfo");
         return(new CK_TOKEN_INFO(native));
     }
     else
     {
         CK_TOKEN_INFO.NativeUnix native = new CK_TOKEN_INFO.NativeUnix();
         CKR_Exception.check(LibraryUnix.fl.C_GetTokenInfo(slot.Id, out native), "C_GetTokenInfo");
         return(new CK_TOKEN_INFO(native));
     }
 }
        public static uint[] C_GetMechanismList(CK_SLOT_ID slot)
        {
            int count = 0;

            CKR_Exception.check(fl.C_GetMechanismList(slot.Id, null, ref count), "C_GetMechanismList");
            if (count == 0)
            {
                return(new uint[0]);
            }

            uint[] list = new uint[count];
            if (count > 0)
            {
                CKR_Exception.check(fl.C_GetMechanismList(slot.Id, list, ref count), "C_GetMechanismList");
            }
            return(list);
        }
        public static uint[] C_GetMechanismList(CK_SLOT_ID slot)
        {
            int  count     = 0;
            long countUnix = 0;

            CKR_Exception.check(isWindows ?
                                LibraryWindows.fl.C_GetMechanismList(slot.Id, null, ref count):
                                LibraryUnix.fl.C_GetMechanismList(slot.Id, null, ref countUnix)
                                , "C_GetMechanismList");
            if (count == 0)
            {
                return(new uint[0]);
            }

            if (!isWindows)
            {
                count = (int)countUnix;
            }
            uint[] list = new uint[count];

            ulong[] listUnix = null;
            if (!isWindows)
            {
                listUnix = new ulong[count];
            }

            if (count > 0)
            {
                CKR_Exception.check(isWindows ?
                                    LibraryWindows.fl.C_GetMechanismList(slot.Id, list, ref count) :
                                    LibraryUnix.fl.C_GetMechanismList(slot.Id, listUnix, ref countUnix)
                                    , "C_GetMechanismList");
            }

            if (!isWindows)
            {
                for (int i = 0; i < count; i++)
                {
                    list[i] = (uint)listUnix[i];
                }
            }

            return(list);
        }
        public static CK_SLOT_ID[] C_GetSlotList(bool present)
        {
            byte tokenPresent = (byte)(present ? 1 : 0);
            int  count        = 0;

            CKR_Exception.check(fl.C_GetSlotList(tokenPresent, null, ref count), "C_GetSlotList");
            if (count == 0)
            {
                return(new CK_SLOT_ID[0]);
            }

            uint[] list = new uint[count];
            if (count > 0)
            {
                CKR_Exception.check(fl.C_GetSlotList(tokenPresent, list, ref count), "C_GetSlotList");
            }

            CK_SLOT_ID[] slots = new CK_SLOT_ID[count];
            for (int i = 0; i < count; i++)
            {
                slots[i] = new CK_SLOT_ID(list[i]);
            }
            return(slots);
        }
 public static void C_CloseAllSessions(CK_SLOT_ID slot)
 {
     CKR_Exception.check(fl.C_CloseAllSessions(slot.Id), "C_CloseAllSessions");
 }
 public static CK_TOKEN_INFO C_GetTokenInfo(CK_SLOT_ID slot)
 {
     CK_TOKEN_INFO.Native native = new CK_TOKEN_INFO.Native();
     CKR_Exception.check(fl.C_GetTokenInfo(slot.Id, out native), "C_GetTokenInfo");
     return(new CK_TOKEN_INFO(native));
 }