Пример #1
0
        public static void LoginIfRequired(CK_SLOT_ID slotId)
        {
            try
            {
                CK_SESSION_INFO session_info = GetSessionInfo();
                CK_TOKEN_INFO   token_info   = GetTokenInfo(slotId);

                if ((token_info.flags & CKF_LOGIN_REQUIRED) == CKF_LOGIN_REQUIRED)
                {
                    if ((token_info.flags & CKF_PROTECTED_AUTHENTICATION_PATH) == CKF_PROTECTED_AUTHENTICATION_PATH)
                    {
                        Login();
                    }
                    else
                    {
                        CkpPromtForm dialog = new CkpPromtForm();
                        if (UIUtil.ShowDialogAndDestroy(dialog) != DialogResult.OK)
                        {
                            return;
                        }

                        Login(dialog.pin);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Пример #2
0
        public static void LoginIfRequired(UInt32 slotId)
        {
            try
            {
                //SessionInfo session_info = session.GetSessionInfo();
                TokenInfo token_info = slots().GetTokenInfo();

                if ((token_info.TokenFlags.Flags & CKF.CKF_LOGIN_REQUIRED) == CKF.CKF_LOGIN_REQUIRED)
                {
                    if ((token_info.TokenFlags.Flags & CKF.CKF_PROTECTED_AUTHENTICATION_PATH) == CKF.CKF_PROTECTED_AUTHENTICATION_PATH)
                    {
                        Login("");
                    }
                    else
                    {
                        CkpPromtForm dialog = new CkpPromtForm();
                        if (UIUtil.ShowDialogAndDestroy(dialog) != DialogResult.OK)
                        {
                            return;
                        }

                        Login(dialog.pin);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
Пример #3
0
        public static void createKeyfile(string label, byte[] data)
        {
            //Slot slot = slots();
            TokenInfo token_info = slots().GetTokenInfo();

            if ((token_info.TokenFlags.Flags & CKF.CKF_LOGIN_REQUIRED) == CKF.CKF_LOGIN_REQUIRED)
            {
                if ((token_info.TokenFlags.Flags & CKF.CKF_PROTECTED_AUTHENTICATION_PATH) == CKF.CKF_PROTECTED_AUTHENTICATION_PATH)
                {
                    Login("");
                }
                else
                {
                    CkpPromtForm dialog = new CkpPromtForm();
                    if (UIUtil.ShowDialogAndDestroy(dialog) != DialogResult.OK)
                    {
                        return;
                    }
                    Login(dialog.pin);
                }
            }
            List <ObjectAttribute> objectAttributes = new List <ObjectAttribute>();

            objectAttributes.Add(new ObjectAttribute(Net.Pkcs11Interop.Common.CKA.CKA_CLASS, (uint)Net.Pkcs11Interop.Common.CKO.CKO_DATA));
            objectAttributes.Add(new ObjectAttribute(Net.Pkcs11Interop.Common.CKA.CKA_TOKEN, new byte[] { 0x01 }));
            objectAttributes.Add(new ObjectAttribute(Net.Pkcs11Interop.Common.CKA.CKA_APPLICATION, "CryptokiKeyProvider"));
            objectAttributes.Add(new ObjectAttribute(Net.Pkcs11Interop.Common.CKA.CKA_LABEL, "CryptokiKeyProvider"));
            objectAttributes.Add(new ObjectAttribute(Net.Pkcs11Interop.Common.CKA.CKA_VALUE, data));
            // Create object
            session.CreateObject(objectAttributes);
            //session.Logout();
        }
Пример #4
0
        public static List <keyfile> read_allkeyfiles(string path)
        {
            List <keyfile> _keyfiles = new List <keyfile>();

            try
            {
                PKCS11_init(path);
                Initialize();
                slotList = GetSlotList();

                if (slotList.Length == 0)
                {
                    throw new Exception("No token available, please insert token.");
                }

                for (uint i = 0; i < slotList.Length; i++)
                {
                    OpenSession(slotList[i]);

                    CK_TOKEN_INFO token_info = GetTokenInfo(slotList[i]);

                    if ((token_info.flags & CKF_LOGIN_REQUIRED) == CKF_LOGIN_REQUIRED)
                    {
                        if ((token_info.flags & CKF_PROTECTED_AUTHENTICATION_PATH) == CKF_PROTECTED_AUTHENTICATION_PATH)
                        {
                            Login();
                        }
                        else
                        {
                            CkpPromtForm dialog = new CkpPromtForm();
                            if (UIUtil.ShowDialogAndDestroy(dialog) != DialogResult.OK)
                            {
                                return(null);
                            }

                            Login(dialog.pin);
                        }
                    }

                    CK_ATTRIBUTE findtemplate = createAttribute(pkcs11.CKA_CLASS, BitConverter.GetBytes(CKO_DATA));

                    FindObjectsInit();
                    List <uint> objects = FindObjects();
                    _keyfiles.AddRange(GetKeyfiles(objects, slotList[i], token_info.label));
                }
            }
            catch (Exception ex)
            {
                throw;
            }

            return(_keyfiles);
        }
Пример #5
0
        public static List <keyfile> read_allkeyfiles(String path)
        {
            init(path);
            List <keyfile> _keyfiles = new List <keyfile>();
            Slot           slot      = slots();
            //Console.WriteLine("Get Slots");
            //SessionInfo session_info = session.GetSessionInfo();
            TokenInfo token_info = slots().GetTokenInfo();

            if ((token_info.TokenFlags.Flags & CKF.CKF_LOGIN_REQUIRED) == CKF.CKF_LOGIN_REQUIRED)
            {
                if ((token_info.TokenFlags.Flags & CKF.CKF_PROTECTED_AUTHENTICATION_PATH) == CKF.CKF_PROTECTED_AUTHENTICATION_PATH)
                {
                    Login("");
                }
                else
                {
                    CkpPromtForm dialog = new CkpPromtForm();
                    if (UIUtil.ShowDialogAndDestroy(dialog) != DialogResult.OK)
                    {
                        return(null);
                    }
                    Login(dialog.pin);
                }
            }
            //Console.WriteLine("Login");
            //using (Session session = slot.OpenSession(false)){
            //	session.Login(Net.Pkcs11Interop.Common.CKU.CKU_USER,password);
            List <ObjectAttribute> objectAttributes = new List <ObjectAttribute>();

            objectAttributes.Add(new ObjectAttribute(Net.Pkcs11Interop.Common.CKA.CKA_CLASS, (uint)Net.Pkcs11Interop.Common.CKO.CKO_DATA));
            objectAttributes.Add(new ObjectAttribute(Net.Pkcs11Interop.Common.CKA.CKA_TOKEN, true));
            List <ObjectHandle> found = session.FindAllObjects(objectAttributes);

            foreach (ObjectHandle hanlder in found)
            {
                List <Net.Pkcs11Interop.Common.CKA> attr = new List <Net.Pkcs11Interop.Common.CKA>();
                attr.Add(Net.Pkcs11Interop.Common.CKA.CKA_LABEL);
                string label = session.GetAttributeValue(hanlder, attr)[0].GetValueAsString();
                _keyfiles.Add(new keyfile {
                    handle     = hanlder,
                    label      = label,
                    slotid     = slot.GetTokenInfo().SlotId,
                    token_name = slot.GetTokenInfo().Label,
                });
            }
            //}
            return(_keyfiles);
        }