public static CK_SESSION_INFO C_GetSessionInfo(CK_SESSION_HANDLE session)
        {
            CK_SESSION_INFO info = new CK_SESSION_INFO();

            CKR_Exception.check(fl.C_GetSessionInfo(session.Handle, out info), "C_GetSessionInfo");
            return(info);
        }
 public static void C_DestroyObject(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE obj)
 {
     CKR_Exception.check(isWindows ?
                         LibraryWindows.fl.C_DestroyObject(session.Handle, obj.Handle):
                         LibraryUnix.fl.C_DestroyObject(session.Handle, obj.Handle)
                         , "C_DestroyObject");
 }
 public static void C_Initialize()
 {
     CKR_Exception.check(isWindows ?
                         LibraryWindows.fl.C_Initialize(IntPtr.Zero) :
                         LibraryUnix.fl.C_Initialize(IntPtr.Zero)
                         , "C_Initialize");
 }
        public static int C_DigestFinal(CK_SESSION_HANDLE session, byte[] output)
        {
            int outSize = output == null ? 0 : output.Length;

            CKR_Exception.check(fl.C_DigestFinal(session.Handle, output, ref outSize), "C_DigestFinal");
            return(outSize);
        }
        public static void C_Verify(CK_SESSION_HANDLE session, byte[] input, byte[] signature)
        {
            int inSize  = input == null ? 0 : input.Length;
            int sigSize = signature == null ? 0 : signature.Length;

            CKR_Exception.check(fl.C_Verify(session.Handle, input, inSize, signature, sigSize), "C_Verify");
        }
 public static void C_SeedRandom(CK_SESSION_HANDLE session, byte[] data)
 {
     CKR_Exception.check(isWindows ?
                         LibraryWindows.fl.C_SeedRandom(session.Handle, data, (uint)data.Length):
                         LibraryUnix.fl.C_SeedRandom(session.Handle, data, (uint)data.Length)
                         , "C_SeedRandom");
 }
        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_OBJECT_HANDLE[] C_FindObjects(CK_SESSION_HANDLE session, int ulMaxObjectCount)
        {
            uint[]  buf     = null;
            ulong[] bufUnix = null;

            if (isWindows)
            {
                buf = new uint[ulMaxObjectCount];
            }
            else
            {
                bufUnix = new ulong[ulMaxObjectCount];
            }

            int  count     = 0;
            long countUnix = 0;

            CKR_Exception.check(isWindows ?
                                LibraryWindows.fl.C_FindObjects(session.Handle, buf, ulMaxObjectCount, out count):
                                LibraryUnix.fl.C_FindObjects(session.Handle, bufUnix, ulMaxObjectCount, out countUnix)
                                , "C_FindObjectsInit");

            if (!isWindows)
            {
                count = (int)countUnix;
            }
            CK_OBJECT_HANDLE[] res = new CK_OBJECT_HANDLE[count];
            for (int i = 0; i < count; i++)
            {
                res[i] = new CK_OBJECT_HANDLE(isWindows ? buf[i] : (uint)bufUnix[i]);
            }
            return(res);
        }
 public static void C_Logout(CK_SESSION_HANDLE session)
 {
     CKR_Exception.check(isWindows ?
                         LibraryWindows.fl.C_Logout(session.Handle) :
                         LibraryUnix.fl.C_Logout(session.Handle)
                         , "C_Logout");
 }
 public static void C_FindObjectsFinal(CK_SESSION_HANDLE session)
 {
     CKR_Exception.check(isWindows ?
                         LibraryWindows.fl.C_FindObjectsFinal(session.Handle):
                         LibraryUnix.fl.C_FindObjectsFinal(session.Handle)
                         , "C_FindObjectsFinal");
 }
        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 void C_CloseSession(CK_SESSION_HANDLE session)
 {
     CKR_Exception.check(isWindows ?
                         LibraryWindows.fl.C_CloseSession(session.Handle) :
                         LibraryUnix.fl.C_CloseSession(session.Handle)
                         , "C_OpenSession");
 }
        public static void C_GetAttributeValue(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE obj, CK_ATTRIBUTE[] pTemplate)
        {
            CK_ATTRIBUTE.Native t = null;
            IntPtr buf            = IntPtr.Zero;

            try
            {
                bool isKnownSize;
                t = CK_ATTRIBUTE.ToNativeReadSize(pTemplate, out isKnownSize);

                if (!isKnownSize)
                {
                    CKR_Exception.check(isWindows ?
                                        LibraryWindows.fl.C_GetAttributeValue(session.Handle, obj.Handle, t.windows, t.Length):
                                        LibraryUnix.fl.C_GetAttributeValue(session.Handle, obj.Handle, t.unix, t.Length)
                                        , "C_GetAttributeValue(null)");
                }

                CK_ATTRIBUTE.ToNativeRead(t);
                CKR_Exception.check(isWindows ?
                                    LibraryWindows.fl.C_GetAttributeValue(session.Handle, obj.Handle, t.windows, t.Length):
                                    LibraryUnix.fl.C_GetAttributeValue(session.Handle, obj.Handle, t.unix, t.Length)
                                    , "C_GetAttributeValue");
                CK_ATTRIBUTE.FromNative(pTemplate, t);
            }
            finally
            {
                CK_ATTRIBUTE.Free(t);
            }
        }
 public static void C_DigestKey(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE key)
 {
     CKR_Exception.check(isWindows ?
                         LibraryWindows.fl.C_DigestKey(session.Handle, key.Handle):
                         LibraryUnix.fl.C_DigestKey(session.Handle, key.Handle)
                         , "C_DigestKey");
 }
        public static int C_Sign(CK_SESSION_HANDLE session, byte[] input, byte[] output)
        {
            int inSize  = input == null ? 0 : input.Length;
            int outSize = output == null ? 0 : output.Length;

            CKR_Exception.check(fl.C_Sign(session.Handle, input, inSize, output, ref outSize), "C_Sign");
            return(outSize);
        }
 public static void C_Login(CK_SESSION_HANDLE session, uint userType, string pin)
 {
     byte[] bytes = Encoding.UTF8.GetBytes(pin);
     CKR_Exception.check(isWindows ?
                         LibraryWindows.fl.C_Login(session.Handle, userType, bytes, (uint)bytes.Length):
                         LibraryUnix.fl.C_Login(session.Handle, userType, bytes, (uint)bytes.Length)
                         , "C_Login");
 }
 public static void C_FindObjectsInit(CK_SESSION_HANDLE session, CK_ATTRIBUTE[] pTemplate)
 {
     CK_ATTRIBUTE.Native t = CK_ATTRIBUTE.ToNative(pTemplate);
     try { CKR_Exception.check(isWindows ?
                               LibraryWindows.fl.C_FindObjectsInit(session.Handle, t.windows, t.Length):
                               LibraryUnix.fl.C_FindObjectsInit(session.Handle, t.unix, t.Length)
                               , "C_FindObjectsInit"); }
     finally { CK_ATTRIBUTE.Free(t); }
 }
 public static void C_SetAttributeValue(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE obj, CK_ATTRIBUTE[] pTemplate)
 {
     CK_ATTRIBUTE.Native t = CK_ATTRIBUTE.ToNative(pTemplate);
     try { CKR_Exception.check(isWindows ?
                               LibraryWindows.fl.C_SetAttributeValue(session.Handle, obj.Handle, t.windows, t.Length):
                               LibraryUnix.fl.C_SetAttributeValue(session.Handle, obj.Handle, t.unix, t.Length)
                               , "C_SetAttributeValue"); }
     finally { CK_ATTRIBUTE.Free(t); }
 }
        public static CK_OBJECT_HANDLE C_CreateObject(CK_SESSION_HANDLE session, CK_ATTRIBUTE[] pTemplate)
        {
            CK_ATTRIBUTE.Native[] t = CK_ATTRIBUTE.ToNative(pTemplate);
            uint objHandle          = 0;

            try { CKR_Exception.check(fl.C_CreateObject(session.Handle, t, t.Length, out objHandle), "C_CreateObject"); }
            finally { CK_ATTRIBUTE.Free(t); }
            return(new CK_OBJECT_HANDLE(objHandle));
        }
        public static int C_WrapKey(CK_SESSION_HANDLE session, CK_MECHANISM mech, CK_OBJECT_HANDLE key, CK_OBJECT_HANDLE wrappedKey, byte[] output)
        {
            CK_MECHANISM.Native m = new CK_MECHANISM.Native(mech);
            int outSize           = output == null ? 0 : output.Length;

            try { CKR_Exception.check(fl.C_WrapKey(session.Handle, m, key.Handle, wrappedKey.Handle, output, ref outSize), "C_WrapKey"); }
            finally { m.Free(); }
            return(outSize);
        }
        public static void C_VerifyFinal(CK_SESSION_HANDLE session, byte[] signature)
        {
            int sigSize = signature == null ? 0 : signature.Length;

            CKR_Exception.check(isWindows ?
                                LibraryWindows.fl.C_VerifyFinal(session.Handle, signature, sigSize):
                                LibraryUnix.fl.C_VerifyFinal(session.Handle, signature, sigSize)
                                , "C_VerifyFinal");
        }
        public static void C_DigestUpdate(CK_SESSION_HANDLE session, byte[] input)
        {
            int inSize = input == null ? 0 : input.Length;

            CKR_Exception.check(isWindows ?
                                LibraryWindows.fl.C_DigestUpdate(session.Handle, input, inSize):
                                LibraryUnix.fl.C_DigestUpdate(session.Handle, input, inSize)
                                , "C_DigestUpdate");
        }
 public static void C_DigestInit(CK_SESSION_HANDLE session, CK_MECHANISM mech)
 {
     CK_MECHANISM.Native m = new CK_MECHANISM.Native(mech);
     try { CKR_Exception.check(isWindows ?
                               LibraryWindows.fl.C_DigestInit(session.Handle, m.windows):
                               LibraryUnix.fl.C_DigestInit(session.Handle, ref m.unix)
                               , "C_DigestInit"); }
     finally { m.Free(); }
 }
 public static void C_VerifyInit(CK_SESSION_HANDLE session, CK_MECHANISM mech, CK_OBJECT_HANDLE key)
 {
     CK_MECHANISM.Native m = new CK_MECHANISM.Native(mech);
     try { CKR_Exception.check(isWindows ?
                               LibraryWindows.fl.C_VerifyInit(session.Handle, m.windows, key.Handle):
                               LibraryUnix.fl.C_VerifyInit(session.Handle, ref m.unix, key.Handle)
                               , "C_VerifyInit"); }
     finally { m.Free(); }
 }
        public static CK_OBJECT_HANDLE C_GenerateKey(CK_SESSION_HANDLE session, CK_MECHANISM mech, CK_ATTRIBUTE[] pTemplate)
        {
            CK_MECHANISM.Native   m = new CK_MECHANISM.Native(mech);
            CK_ATTRIBUTE.Native[] t = CK_ATTRIBUTE.ToNative(pTemplate);
            uint keyHandle          = 0;

            try { CKR_Exception.check(fl.C_GenerateKey(session.Handle, m, t, t.Length, out keyHandle), "C_GenerateKey"); }
            finally { m.Free(); CK_ATTRIBUTE.Free(t); }
            return(new CK_OBJECT_HANDLE(keyHandle));
        }
 public static byte[] C_GenerateRandom(CK_SESSION_HANDLE session, uint len)
 {
     byte[] dst = new byte[len];
     CKR_Exception.check(isWindows ?
                         LibraryWindows.fl.C_GenerateRandom(session.Handle, dst, len) :
                         LibraryUnix.fl.C_GenerateRandom(session.Handle, dst, len)
                         , "C_GenerateRandom"
                         );
     return(dst);
 }
        public static int C_DigestFinal(CK_SESSION_HANDLE session, byte[] output)
        {
            int  outSize     = output == null ? 0 : output.Length;
            long outSizeUnix = output == null ? 0 : output.Length;

            CKR_Exception.check(isWindows ?
                                LibraryWindows.fl.C_DigestFinal(session.Handle, output, ref outSize):
                                LibraryUnix.fl.C_DigestFinal(session.Handle, output, ref outSizeUnix)
                                , "C_DigestFinal");
            return(isWindows ? outSize : (int)outSizeUnix);
        }
        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_OBJECT_HANDLE C_UnwrapKey(CK_SESSION_HANDLE session, CK_MECHANISM mech, CK_OBJECT_HANDLE key, byte[] input, CK_ATTRIBUTE[] pTemplate)
        {
            CK_MECHANISM.Native   m = new CK_MECHANISM.Native(mech);
            CK_ATTRIBUTE.Native[] t = CK_ATTRIBUTE.ToNative(pTemplate);
            int  inSize             = input == null ? 0 : input.Length;
            uint unwrappedKeyHandle = 0;

            try { CKR_Exception.check(fl.C_UnwrapKey(session.Handle, m, key.Handle, input, inSize, t, t.Length, out unwrappedKeyHandle), "C_UnwrapKey"); }
            finally { m.Free(); CK_ATTRIBUTE.Free(t); }
            return(new CK_OBJECT_HANDLE(unwrappedKeyHandle));
        }