Пример #1
0
        public void TestMethod_MultiThreadTestAuth()
        {
            /*
                    以下用來壓力測試Auth
             */
            this.AuthObjList = new List<iBonAuthenticate>();
            int testCount = 10000;
            Task[] tasks = new Task[testCount];
            for (var i = 0; i < testCount; i++)
            {
                iBonAuthenticate tmp = new iBonAuthenticate();
                tmp.Input_KeyLabel = "2ICH3F000032A";
                tmp.Input_KeyVersion = "0";
                //使用未導出k0的uid來作,所以是01+uid+ICASH+uid+ICASH+uid => "0104873ABA8D2C80494341534804873ABA8D2C80494341534804873ABA8D2C80"
                tmp.Input_BlobValue = this.hexConverter.Hex2Bytes("0104873ABA8D2C80494341534804873ABA8D2C80494341534804873ABA8D2C80");/*"0104214C82583B80494341534804214C82583B80494341534804214C82583B80");*///"0104873ABA8D2C80494341534804873A9B330A45CCB51DDE66FDD7EABD400895");//
                tmp.Input_Enc_RanB = "4EF61041ABE8B0EF8B32A627B19D83AA";//加密的RanB
                tmp.Input_RanA = new byte[] { 0x7A,0x0F,0x1F,0xBC,0xD5,0xFD,0xEA,0x04,
                                                    0x8C,0x9B,0xD7,0x90,0x05,0x0E,0x78,0xA0 };
               tasks[i] = Task.Factory.StartNew((object o)=>{
                    iBonAuthenticate auth = (iBonAuthenticate)o;
                    checkAuth(auth);
                }, tmp);
            }

            Task.WaitAll(tasks);
            Debug.WriteLine("全部認證都測完了");
        }
Пример #2
0
        private void checkAuth(iBonAuthenticate auth)
        {
            string expectedRanBStr = "65ADC0C88F7BFB97430D6F84274FC376";//預期解密的RanB字串
            byte[] expectedRanB = this.hexConverter.Hex2Bytes(expectedRanBStr);//預期的RanB
            string expectedDivKey = /*"EE0E90B75F83D7F2C52D3C5F4CAF20D3";//*/"17AB67F130169FB3C012B2DD17985365";//Kx(從KMS取得的DivKey)
            string expectedEnc_RanAandRanBRol8Str = "B6EE87D2F942E2CB70EF6605CBA463EAF605E369EB6036600C2E6F2D528E475B";//預期的Enc(iv, RanA || RanBRol8)
            byte[] expectedEnc_RanAandRanBRol8 = this.hexConverter.Hex2Bytes(expectedEnc_RanAandRanBRol8Str);

            string expectedEncRanARol8Str = "CADB06F55D182E9CB5DF7B8246C991D1";//E(RanARol8) //文件上的加密過的(RanA左旋 1 byte)
            byte[] expectedEncRanARol8 = this.hexConverter.Hex2Bytes(expectedEncRanARol8Str);

            string expectedSessionKeyStr = "7A0F1FBC65ADC0C8050E78A0274FC376";//預期的SessionKey
            byte[] expectedSessionKey = this.hexConverter.Hex2Bytes(expectedSessionKeyStr);
            Debug.WriteLine("預期DivKey:" + expectedDivKey);//EE0E90B75F83D7F2C52D3C5F4CAF20D3
            //************************************************************
            //開始執行認證
            auth.StartAuthenticate(true);
            //************************************************************
            //檢查解密後的RanB
            for (int i = 0; i < expectedRanB.Length; i++)
            {
                Assert.AreEqual(expectedRanB[i], auth.Output_RanB[i],
                    "預期RanB[" + i + "]:" + expectedRanB[i] + " 不同於實際RanB[" + i + "]:" + auth.Output_RanB[i]);
            }

            //檢查E(iv,RanA||RanBRol8); iv = E(RanB)
            for (int j = 0; j < expectedEnc_RanAandRanBRol8.Length; j++)
            {
                Assert.AreEqual(expectedEnc_RanAandRanBRol8[j], auth.Output_Enc_RanAandRanBRol8[j],
                    "預期Enc_RanAandRanBRol8[" + j + "]:" + expectedEnc_RanAandRanBRol8[j] +
                    " 不同於實際Enc_RanAandRanBRol8[" + j + "]:" + auth.Output_Enc_RanAandRanBRol8[j]);
            }

            //檢查E(iv,RanARol8)
            for (int k = 0; k < expectedEncRanARol8.Length; k++)
            {
                Assert.AreEqual(expectedEncRanARol8[k], auth.Output_Enc_IVandRanARol8[k],
                    "預期Enc_IVandRanARol8[" + k + "]:" + expectedEncRanARol8[k] +
                    " 不同於實際Enc_IVandRanARol8[" + k + "]:" + auth.Output_Enc_IVandRanARol8[k]);
            }

            //檢查最終的 Session Key
            for (int m = 0; m < expectedSessionKey.Length; m++)
            {
                Assert.AreEqual(expectedSessionKey[m], auth.Output_SessionKey[m],
                    "預期Enc_IVandRanARol8[" + m + "]:" + expectedSessionKey[m] +
                    " 不同於實際Enc_IVandRanARol8[" + m + "]:" + auth.Output_SessionKey[m]);
            }
        }
Пример #3
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="absClientRequestHandler"></param>
        public void Handle(AbsClientRequestHandler absClientRequestHandler)
        {
            #region variable
            byte[] receiveBuffer = null;
            int readCount = 0;
            string requestJsonStr = null;
            string outputCmd = null;
            iBonAuthenticate iBonAuthObj = null;
            EskmsPOCO request = null;
            EskmsPOCO response = null;
            string requestCheckErrMsg = null;
            string responseJsonStr = null;
            byte[] responseBytes = null;
            int sendCount = -1;
            #endregion

            try
            {
                receiveBuffer = new byte[0x1000];//4k
                readCount = absClientRequestHandler.ClientSocket.Receive(receiveBuffer, SocketFlags.None);
                if (readCount == 0) { return; }
                    //command 輸出狀態 TODO...
                else if (readCount == 6 && Encoding.UTF8.GetString(receiveBuffer, 0, readCount).ToLower().Contains("status"))
                {
                    outputCmd = "Hello";
                    receiveBuffer = Encoding.UTF8.GetBytes(outputCmd);
                    absClientRequestHandler.ClientSocket.Send(receiveBuffer);
                    return;
                }
                else
                {
                    log.Debug(m => m(">> {0}: {1}", this.GetType().Name, absClientRequestHandler.ClientNo));
                    //resize buffer
                    Array.Resize(ref receiveBuffer, readCount);
                    //casting jsonstring from buffer array
                    requestJsonStr = Encoding.UTF8.GetString(receiveBuffer);
                    log.Debug(m => m("[{0}]Request: {1}", this.GetType().Name, requestJsonStr));
                    request = JsonConvert.DeserializeObject<EskmsPOCO>(requestJsonStr);
                    //檢查Request資料長度(Attribute)
                    request.CheckLength(true, out requestCheckErrMsg);
                    //設定Authenticate參數
                    iBonAuthObj = new iBonAuthenticate()
                    {
                        Input_KeyLabel = request.Input_KeyLabel,
                        Input_KeyVersion = request.Input_KeyVersion,
                        Input_UID = request.Input_UID,
                        Input_Enc_RanB = request.Input_Enc_RanB
                    };
                    log.Debug(m => m("開始執行Authenticate"));
                    iBonAuthObj.StartAuthenticate(true);//會傳送數據到KMS並取回DiverseKey後做運算並將結果寫入Output屬性中

                    //回應資料設定
                    response = new EskmsPOCO()
                    {
                        Input_KeyLabel = request.Input_KeyLabel,
                        Input_KeyVersion = request.Input_KeyVersion,
                        Input_UID = request.Input_UID,
                        Input_Enc_RanB = request.Input_Enc_RanB,
                        Output_RanB = iBonAuthObj.Output_RanB,
                        Output_Enc_RanAandRanBRol8 = iBonAuthObj.Output_Enc_RanAandRanBRol8,
                        Output_Enc_IVandRanARol8 = iBonAuthObj.Output_Enc_IVandRanARol8,
                        Output_RandAStartIndex = iBonAuthObj.Output_RandAStartIndex,
                        Output_SessionKey= iBonAuthObj.Output_SessionKey
                    };
                    responseJsonStr = JsonConvert.SerializeObject(response);
                    responseBytes = Encoding.UTF8.GetBytes(responseJsonStr);
                    log.Debug(m => m("[{0}] Response:{1}", this.GetType().Name, responseJsonStr));
                    sendCount = absClientRequestHandler.ClientSocket.Send(responseBytes);
                    if (sendCount != responseBytes.Length)
                    {
                        log.Error(m => m("異常:送出資料(length:{0}不等於原始資料(length:{1}))", sendCount, responseBytes.Length));
                    }
                    log.Debug(m => m("[{0}] Response End", this.GetType().Name));
                }
            }
            catch (ArgumentOutOfRangeException ex)
            {
                log.Error(m => m("資料檢核失敗:{0}",ex.ToString()));
            }
            catch(JsonException ex)
            {
                log.Error(m => m("Request(JsonString) Parse Request(Object) Failed:{0}", ex.ToString()));
            }
            catch (Exception ex)
            {
                log.Error(m => m("[{0}] Error:{1} {2}", this.GetType().Name, ex.Message, ex.StackTrace));
            }
            finally
            {

                absClientRequestHandler.ServiceState = new State_Exit();
            }
        }