Exemplo n.º 1
0
 public static extern uint CAPI_KEY_GetMeta(StringBuilder pszKeyOn, int ulKeyTarget, ref OPT_ST_USB_META meta);
Exemplo n.º 2
0
 public static extern uint CAPI_KEY_SetMeta(StringBuilder pszKeyOn, int ulKeyTarget, ref OPT_ST_USB_META meta, String pszPIN,ref int ulRetry);
        void FunctionCreateAdmin()
        {
            ulong m_ulRecordCount = 0;
            uint m_ulInfoCount = 0;

            KMS_USER[] m_szInfoUser = new KMS_USER[10];

            KMC_API.SQL_Init(textBoxIP.Text, textBoxPORT.Text, textBoxDBName.Text, textBoxDBPin.Text, "CertificateKMS");

            KMC_API.ulResult = KMC_API.SQL_DataBaseExist();
            if (0 != KMC_API.ulResult)
            {
                return;
            }

            //KMC_API.CAPI_KEY_SetStr("abcdg");

            //StringBuilder buffer = new StringBuilder(300);
            //uint tempPath = KMC_API.CAPI_KEY_GetStr(buffer);

            //string path = buffer.ToString();

            //if (tempPath == 0)
            //{
            //    int errorcode = Marshal.GetLastWin32Error();
            //    Win32Exception win32expection = new Win32Exception(errorcode);
            //}

            KMC_API.ulResult = KMC_API.SQL_QueryUser(textBoxUserName.Text, null, null, 10, 0, ref m_ulRecordCount, ref m_szInfoUser, ref m_ulInfoCount);

            if (0 != KMC_API.ulResult)
            {
                return;
            }

            if (m_ulInfoCount > 0)
            {
                KMC_API.ulResult = (uint)eErrorCode.EStateErrorAddUser;
                goto EXIT;
            }

            StringBuilder strKeyOn = new StringBuilder(1024);

            KMC_API.ulResult = KMC_API.CAPI_KEY_SetPin(strKeyOn, 0, textBoxPinOK.Text, textBoxPinOK.Text);
            if (0 != KMC_API.ulResult)
            {
                goto EXIT;
            }

            OPT_ST_USB_META meta = new OPT_ST_USB_META();

            meta.ulUSBMetaUseType = (uint)OPE_USB_META_USE_TYPE.OPE_USB_META_USE_TYPE_AUTH;

            meta.ulUSBMetaManType = (uint)OPE_USB_META_MAN_TYPE.OPE_USB_META_MAN_TYPE_ADMIN;

            //meta.szName = textBoxUserName.Text;

            string s211 = textBoxUserName.Text;

            meta.szName = new byte[256];

            UTF8Encoding utf8 = new UTF8Encoding();
            byte [] encodedBytes = utf8.GetBytes(textBoxUserName.Text);
            Array.Copy(encodedBytes, 0, meta.szName, 0, encodedBytes.Length);

            int ulRetry = 0;

            KMC_API.ulResult = KMC_API.CAPI_KEY_SetMeta(strKeyOn, 0,ref meta, textBoxPinOK.Text, ref ulRetry);  // 其他USBKey 传输

            if (0 != KMC_API.ulResult)
            {
                goto EXIT;
            }

            OPT_ST_USB_META metaref = new OPT_ST_USB_META();

            KMC_API.ulResult = KMC_API.CAPI_KEY_GetMeta(strKeyOn, 0, ref metaref);  // 其他USBKey 传输

            if (0 != KMC_API.ulResult)
            {
                goto EXIT;
            }

            KMC_API.ulResult = KMC_API.CAPI_KEY_GenKeyPair(strKeyOn, 0, textBoxPinOK.Text, ref ulRetry);

            if (0 != KMC_API.ulResult)
            {
                goto EXIT;
            }

            byte [] btPK = new byte [64];

            KMC_API.ulResult = KMC_API.CAPI_KEY_ExportPK(strKeyOn, 0,1, btPK);

            if (0 != KMC_API.ulResult)
            {
                goto EXIT;
            }

            KMS_USER user = new KMS_USER();
            //// 用户名
            user.strUserName = textBoxUserName.Text;
            //// 用户权限
            user.ulType = OPE_USB_META_MAN_TYPE.OPE_USB_META_MAN_TYPE_ADMIN;
            //
            user.strDepartment = textBoxUnit.Text;
            user.strMobile = textBoxPhone.Text;
            user.strEmail = textBoxEmail.Text;
            user.strNote = textBoxNote.Text;

            user.pubKey.pbValue = btPK;

            KMC_API.ulResult = KMC_API.SQL_AddUser(user);
            if (0 != KMC_API.ulResult)
            {
                goto EXIT;
            }
            EXIT:
            return ;
        }