コード例 #1
0
        private void GetDongleUserInfo()
        {
            if (this.dongleInfo.IsNullOrEmpty())
            {
                return;
            }

            int size = IntPtrUtil.SizeOf(typeof(DongleUserInfoStru));

            byte[] buffer = new byte[size];

            for (int seq = 0; seq < this.dongleInfo.Length; seq++)
            {
                if (IsEmptyKey(this.dongleInfo[seq]))
                {
                    continue;
                }

                DONGLE_HANDLER __hDongle = -1;

                if (Dongle_Open(ref __hDongle, seq) == SUCC)
                {
                    if (Dongle_ReadFile(__hDongle, DongleConst.USER_INFO_DESCRIPTOR, 0, buffer, buffer.Length) == SUCC)
                    {
                        this.dongleInfo[seq].UserInfo = ParseDongleKeyInfo(buffer);
                    }
                    Dongle_Close(__hDongle);
                }
            }
        }
コード例 #2
0
        private ResultArgs CreateAuthenKey(String userId, byte[] appId, byte[] adminPin)
        {
            IntPtr     pubKey = IntPtrUtil.Create(IntPtrUtil.SizeOf(typeof(RSA_PUBLIC_KEY)));
            IntPtr     priKey = IntPtrUtil.Create(IntPtrUtil.SizeOf(typeof(RSA_PRIVATE_KEY)));
            ResultArgs ret    = new ResultArgs(true);

            try
            {
                this.lastErrorCode = Dongle_RsaGenPubPriKey(this.hDongle, DongleConst.AUTHEN_KEY_DESCRIPTOR, pubKey, priKey);
                LogKey(pubKey, priKey);

                // renew appid
                this.dongleInfo[this.selectedIndex].AppId = this.Encoder.GetString(appId);

                RSA_PUBLIC_KEY pubStru = IntPtrUtil.ToStru <RSA_PUBLIC_KEY>(pubKey);
                ret.Result = new AppAuthenDongleResult()
                {
                    PubKey  = Convert.ToBase64String(pubStru.exponent, 0, DongleConst.RSA_KEY_LEN),
                    Version = this.dongleInfo[this.selectedIndex].Version,
                    AppId   = this.dongleInfo[this.selectedIndex].AppId,
                    UserId  = userId,
                    KeyPwd  = this.Encoder.GetString(adminPin),
                    KeyId   = this.dongleInfo[this.selectedIndex].KeyId
                };
            }
            finally
            {
                IntPtrUtil.Free(ref pubKey);
                IntPtrUtil.Free(ref priKey);

                // reset authen status as anonymous
                Reset();
            }
            return(ret);
        }
コード例 #3
0
        private DongleInfo[] GetDongleInfo(int count)
        {
            List <DongleInfo> keyInfo = new List <DongleInfo>();
            IntPtr            ptr     = IntPtr.Zero;

            try
            {
                int size = IntPtrUtil.SizeOf(typeof(DONGLE_INFO));
                ptr = IntPtrUtil.Create(size * (int)count);
                long __count;
                this.lastErrorCode = Dongle_Enum(ptr, out __count);

                //__count is indeed count of dongle
                for (int i = 0; i < __count; i++)
                {
                    IntPtr      __ptr   = IntPtrUtil.Create(ptr, i * size);
                    DONGLE_INFO devInfo = IntPtrUtil.ToStru <DONGLE_INFO>(__ptr);
                    keyInfo.Add(ParseDongleInfo((short)i, devInfo));
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                return(null);
            }
            finally
            {
                IntPtrUtil.Free(ref ptr);
            }
            return(keyInfo.ToArray());
        }
コード例 #4
0
        private void LogKey(IntPtr pubKey, IntPtr priKey)
        {
            RSA_PUBLIC_KEY  pubStru = IntPtrUtil.ToStru <RSA_PUBLIC_KEY>(pubKey);
            RSA_PRIVATE_KEY priStru = IntPtrUtil.ToStru <RSA_PRIVATE_KEY>(priKey);

            logger.Debug(String.Format("RSA-PRI : bit={0},modulus={1}", priStru.bits, priStru.modulus));
            logger.Debug("RSA-PRI : " + Convert.ToBase64String(priStru.exponent, 0, DongleConst.RSA_KEY_LEN));
            logger.Debug(String.Format("RSA-PUB : bit={0},modulus={1}", priStru.bits, priStru.modulus));
            logger.Debug("RSA-PUB : " + Convert.ToBase64String(priStru.exponent, 0, DongleConst.RSA_KEY_LEN));
        }
コード例 #5
0
        private bool CreateUserInfoFile(DONGLE_HANDLER hDongle)
        {
            DATA_FILE_ATTR dataAttr;

            dataAttr.m_Size = DongleConst.USER_INFO_FILE_LEN;
            //allow anonymous read
            dataAttr.m_Lic.m_Read_Priv = 0;
            //only admin could write
            dataAttr.m_Lic.m_Write_Priv = 2;

            IntPtr ptr = IntPtrUtil.CreateByStru(dataAttr);

            this.lastErrorCode = Dongle_CreateFile(hDongle, RockeyArmFileType.FILE_DATA, DongleConst.USER_INFO_DESCRIPTOR, ptr);
            IntPtrUtil.Free(ref ptr);

            return(IsSucc);
        }
コード例 #6
0
        /// <summary>
        /// create RSA private key file
        /// </summary>
        private bool CreateAuthenKeyFile(DONGLE_HANDLER hDongle)
        {
            PRIKEY_FILE_ATTR priAttr = new PRIKEY_FILE_ATTR();

            priAttr.m_Size             = DongleConst.RSA_KEY_LEN * 8;
            priAttr.m_Type             = RockeyArmFileType.FILE_PRIKEY_RSA;
            priAttr.m_Lic.m_Count      = 0xFFFFFFFF;            // un-limitted
            priAttr.m_Lic.m_IsDecOnRAM = 0;
            priAttr.m_Lic.m_IsReset    = 0;
            priAttr.m_Lic.m_Priv       = 0;

            IntPtr ptr = IntPtrUtil.CreateByStru(priAttr);

            try
            {
                this.lastErrorCode = Dongle_CreateFile(hDongle, RockeyArmFileType.FILE_PRIKEY_RSA, DongleConst.AUTHEN_KEY_DESCRIPTOR, ptr);
            }
            finally
            {
                IntPtrUtil.Free(ref ptr);
            }
            return(IsSucc);
        }
コード例 #7
0
        public bool CreateUserInfo(DONGLE_HANDLER hDongle, DongleUserInfo userInfo)
        {
            if (!CreateUserInfoFile(hDongle))
            {
                return(false);
            }

            DongleUserInfoStru stru = CreateUserInfoStru(userInfo);

            IntPtr ptr = IntPtrUtil.CreateByStru(stru);

            byte[] dest = new byte[IntPtrUtil.SizeOf(stru.GetType())];

            Marshal.Copy(ptr, dest, 0, dest.Length);
            this.lastErrorCode = Dongle_WriteFile(
                hDongle,
                RockeyArmFileType.FILE_DATA,
                DongleConst.USER_INFO_DESCRIPTOR,
                0,
                dest,
                dest.Length);
            IntPtrUtil.Free(ref ptr);
            return(IsSucc);
        }
コード例 #8
0
        /// <summary>
        /// create User root key file,and Key is request 16 bytes at least
        /// </summary>
        private bool CreateUserRootKeyFile(DONGLE_HANDLER hDongle, ushort fileDescriptor, byte[] userRootkey)
        {
            KEY_FILE_ATTR keyAttr = new KEY_FILE_ATTR();

            keyAttr.m_Size           = 16;
            keyAttr.m_Lic.m_Priv_Enc = 0;

            IntPtr ptr = IntPtrUtil.CreateByStru(keyAttr);

            this.lastErrorCode = Dongle_CreateFile(hDongle, RockeyArmFileType.FILE_KEY, fileDescriptor, ptr);
            IntPtrUtil.Free(ref ptr);

            if (!IsSucc)
            {
                return(false);
            }

            // dongle key require 16 bytes key
            logger.Debug(String.Format("Create Key file, descriptor={0}, key={1}", fileDescriptor, BitConverter.ToString(userRootkey)));

            this.lastErrorCode = Dongle_WriteFile(hDongle, RockeyArmFileType.FILE_KEY, fileDescriptor, 0, userRootkey, 16);

            return(IsSucc);
        }