Example #1
0
        public override void ValidateAidList(ConfigSerializer serializer)
        {
            try
            {
                if (serializer != null)
                {
                    byte [][]   keys = null;
                    RETURN_CODE rt   = IDT_Augusta.SharedController.emv_retrieveAIDList(ref keys);

                    if (rt == RETURN_CODE.RETURN_CODE_DO_SUCCESS)
                    {
                        Debug.WriteLine("VALIDATE AID LIST ----------------------------------------------------------------------");

                        // Get Configuration File AID List
                        AIDList aid = serializer.GetAIDList();

                        List <Aid> AidList = new List <Aid>();

                        foreach (byte[] aidName in keys)
                        {
                            bool delete = true;
                            bool found  = false;
                            bool update = false;
                            KeyValuePair <string, Dictionary <string, string> > cfgCurrentItem = new KeyValuePair <string, Dictionary <string, string> >();
                            string devAidName = BitConverter.ToString(aidName).Replace("-", string.Empty);

                            Debug.WriteLine("AID: {0} ===============================================", (object)devAidName);

                            // Is this item in the approved list?
                            foreach (var cfgItem in aid.Aid)
                            {
                                cfgCurrentItem = cfgItem;
                                if (cfgItem.Key.Equals(devAidName, StringComparison.CurrentCultureIgnoreCase))
                                {
                                    found = true;
                                    byte[] value = null;

                                    rt = IDT_Augusta.SharedController.emv_retrieveApplicationData(aidName, ref value);

                                    if (rt == RETURN_CODE.RETURN_CODE_DO_SUCCESS)
                                    {
                                        Dictionary <string, Dictionary <string, string> > dict = Common.processTLV(value);

                                        // Compare values and replace if not the same
                                        foreach (Dictionary <string, string> devCollection in dict.Where(x => x.Key.Equals("unencrypted")).Select(x => x.Value))
                                        {
                                            foreach (var cfgTag in cfgItem.Value)
                                            {
                                                bool   tagfound    = false;
                                                string cfgTagName  = cfgTag.Key;
                                                string cfgTagValue = cfgTag.Value;
                                                foreach (var devTag in devCollection)
                                                {
                                                    // Matching TAGNAME: compare keys
                                                    if (devTag.Key.Equals(cfgTag.Key, StringComparison.CurrentCultureIgnoreCase))
                                                    {
                                                        tagfound = true;
                                                        //Debug.Write("key: " + devTag.Key);
                                                        update = !cfgTag.Value.Equals(devTag.Value, StringComparison.CurrentCultureIgnoreCase);

                                                        // Compare value and fix it if mismatched
                                                        if (cfgTag.Value.Equals(devTag.Value, StringComparison.CurrentCultureIgnoreCase))
                                                        {
                                                            //Debug.WriteLine("TAG: {0} FOUND AND IT MATCHES", (object) cfgTagName.PadRight(6,' '));
                                                            //Debug.WriteLine(" matches value: {0}", (object) devTag.Value);
                                                        }
                                                        else
                                                        {
                                                            Debug.WriteLine("TAG: {0} FOUND AND IT DOES NOT match value: {1}!={2}", cfgTagName.PadRight(6, ' '), cfgTag.Value, devTag.Value);
                                                        }
                                                        break;
                                                    }
                                                }
                                                // No need to continue validating the remaing tags
                                                if (!tagfound || update)
                                                {
                                                    break;
                                                }
                                            }
                                        }
                                    }

                                    delete = false;

                                    if (update)
                                    {
                                        byte[] tagCfgName = Device_IDTech.HexStringToByteArray(cfgCurrentItem.Key);

                                        List <byte[]> collection = new List <byte[]>();
                                        foreach (var item in cfgCurrentItem.Value)
                                        {
                                            string  payload = string.Format("{0}{1:X2}{2}", item.Key, item.Value.Length / 2, item.Value).ToUpper();
                                            byte [] bytes   = Device_IDTech.HexStringToByteArray(payload);
                                            collection.Add(bytes);
                                        }
                                        var     flattenedList = collection.SelectMany(bytes => bytes);
                                        byte [] tagCfgValue   = flattenedList.ToArray();
                                        Aid     cfgAid        = new Aid(tagCfgName, tagCfgValue);
                                        AidList.Add(cfgAid);
                                    }
                                }
                            }

                            // DELETE THIS AID
                            if (delete)
                            {
                                Debug.WriteLine("AID: {0} - DELETE (NOT FOUND)", (object)devAidName.PadRight(14, ' '));
                                byte[] tagName = Device_IDTech.HexStringToByteArray(devAidName);
                                rt = IDT_Augusta.SharedController.emv_removeApplicationData(tagName);
                                if (rt == RETURN_CODE.RETURN_CODE_DO_SUCCESS)
                                {
                                    Debug.WriteLine("AID: {0} DELETED", (object)devAidName.PadRight(6, ' '));
                                }
                            }
                            else if (!found)
                            {
                                byte[] tagCfgName = Device_IDTech.HexStringToByteArray(cfgCurrentItem.Key);

                                List <byte[]> collection = new List <byte[]>();
                                foreach (var item in cfgCurrentItem.Value)
                                {
                                    string  payload = string.Format("{0}{1:X2}{2}", item.Key, item.Value.Length / 2, item.Value).ToUpper();
                                    byte [] bytes   = Device_IDTech.HexStringToByteArray(payload);
                                    collection.Add(bytes);
                                }
                                var     flattenedList = collection.SelectMany(bytes => bytes);
                                byte [] tagCfgValue   = flattenedList.ToArray();
                                Aid     cfgAid        = new Aid(tagCfgName, tagCfgValue);
                                AidList.Add(cfgAid);
                            }
                        }

                        // Add missing AID(s)
                        foreach (var aidElement in AidList)
                        {
                            byte [] aidName  = aidElement.GetAidName();
                            byte [] aidValue = aidElement.GetAidValue();
                            rt = IDT_Augusta.SharedController.emv_setApplicationData(aidName, aidValue);
                            if (rt == RETURN_CODE.RETURN_CODE_DO_SUCCESS)
                            {
                                string cfgTagName  = BitConverter.ToString(aidName).Replace("-", string.Empty);
                                string cfgTagValue = BitConverter.ToString(aidValue).Replace("-", string.Empty);
                                Debug.WriteLine("AID: {0} UPDATED WITH VALUE: {1}", cfgTagName.PadRight(6, ' '), cfgTagValue);
                            }
                        }
                    }
                    else
                    {
                        Debug.WriteLine("TERMINAL DATA: emv_retrieveAIDList() - ERROR={0}", rt);
                    }
                }
            }
            catch (Exception exp)
            {
                Debug.WriteLine("device: ValidateAidList() - exception={0}", (object)exp.Message);
            }
        }
Example #2
0
        public override void ValidateCapKList(ConfigSerializer serializer)
        {
            try
            {
                if (serializer != null)
                {
                    byte []     keys = null;
                    RETURN_CODE rt   = IDT_Augusta.SharedController.emv_retrieveCAPKList(ref keys);

                    if (rt == RETURN_CODE.RETURN_CODE_NO_CA_KEY)
                    {
                        keys = new byte[0];
                        rt   = RETURN_CODE.RETURN_CODE_DO_SUCCESS;
                    }
                    if (rt == RETURN_CODE.RETURN_CODE_DO_SUCCESS)
                    {
                        Debug.WriteLine("VALIDATE CAPK LIST ----------------------------------------------------------------------");

                        // Get Configuration File AID List
                        CAPKList capK = serializer.GetCapKList();

                        List <Capk>   CapKList  = new List <Capk>();
                        List <byte[]> capkNames = new List <byte[]>();

                        // Convert array to array of arrays
                        for (int i = 0; i < keys.Length; i += 6)
                        {
                            byte[] result = new byte[6];
                            Array.Copy(keys, i, result, 0, 6);
                            capkNames.Add(result);
                        }

                        foreach (byte[] capkName in capkNames)
                        {
                            bool delete = true;
                            bool found  = false;
                            bool update = false;
                            KeyValuePair <string, CAPK> cfgCurrentItem = new KeyValuePair <string, CAPK>();
                            string devCapKName = BitConverter.ToString(capkName).Replace("-", string.Empty);

                            Debug.WriteLine("CAPK: {0} ===============================================", (object)devCapKName);

                            // Is this item in the approved list?
                            foreach (var cfgItem in capK.CAPK)
                            {
                                cfgCurrentItem = cfgItem;
                                string devRID  = cfgItem.Value.RID;
                                string devIdx  = cfgItem.Value.Index;
                                string devItem = devRID + devIdx;
                                if (devItem.Equals(devCapKName, StringComparison.CurrentCultureIgnoreCase))
                                {
                                    found = true;
                                    byte[] value = null;
                                    Capk   capk  = null;

                                    rt = IDT_Augusta.SharedController.emv_retrieveCAPK(capkName, ref value);

                                    if (rt == RETURN_CODE.RETURN_CODE_DO_SUCCESS)
                                    {
                                        capk = new Capk(value);

                                        // compare modulus
                                        string modulus = cfgItem.Value.Modulus;
                                        update = !modulus.Equals(capk.GetModulus(), StringComparison.CurrentCultureIgnoreCase);
                                        if (!update)
                                        {
                                            // compare exponent
                                            string exponent = cfgItem.Value.Exponent;
                                            update = !exponent.Equals(capk.GetExponent(), StringComparison.CurrentCultureIgnoreCase);
                                        }
                                    }

                                    delete = false;

                                    if (update && capk != null)
                                    {
                                        CapKList.Add(capk);
                                    }
                                    else
                                    {
                                        Debug.WriteLine("    : UP-TO-DATE");
                                    }
                                }
                            }

                            // DELETE CAPK(s)
                            if (delete)
                            {
                                byte[] tagName = Device_IDTech.HexStringToByteArray(devCapKName);
                                rt = IDT_Augusta.SharedController.emv_removeCAPK(tagName);
                                if (rt == RETURN_CODE.RETURN_CODE_DO_SUCCESS)
                                {
                                    Debug.WriteLine("CAPK: {0} DELETED (NOT FOUND)", (object)devCapKName);
                                }
                                else
                                {
                                    Debug.WriteLine("CAPK: {0} DELETE FAILED, ERROR={1}", devCapKName, rt);
                                }
                            }
                            else if (!found)
                            {
                                byte[] tagCfgName = Device_IDTech.HexStringToByteArray(cfgCurrentItem.Key);

                                List <byte[]> collection = new List <byte[]>();
                                string        payload    = string.Format("{0}{1}{2}{3}{4}{5}{6:X2}{7:X2}{8}",
                                                                         cfgCurrentItem.Value.RID, cfgCurrentItem.Value.Index,
                                                                         _HASH_SHA1_ID_STR, _ENC_RSA_ID_STR,
                                                                         cfgCurrentItem.Value.Checksum, cfgCurrentItem.Value.Exponent,
                                                                         (cfgCurrentItem.Value.Modulus.Length / 2) % 256, (cfgCurrentItem.Value.Modulus.Length / 2) / 256,
                                                                         cfgCurrentItem.Value.Modulus);
                                byte[] tagCfgValue = Device_IDTech.HexStringToByteArray(payload);
                                Capk   cfgCapK     = new Capk(tagCfgValue);
                                CapKList.Add(cfgCapK);
                            }
                        }

                        // Add/Update CAPK(s)
                        foreach (var capkElement in CapKList)
                        {
                            //capkElement.ShowCapkValues();
                            byte [] capkValue = capkElement.GetCapkValue();
                            rt = IDT_Augusta.SharedController.emv_setCAPK(capkValue);
                            if (rt == RETURN_CODE.RETURN_CODE_DO_SUCCESS)
                            {
                                Debug.WriteLine("CAPK: {0} UPDATED", (object)capkElement.GetCapkName());
                            }
                            else
                            {
                                Debug.WriteLine("CAPK: {0} FAILED TO UPDATE - ERROR={1}", capkElement.GetCapkName(), rt);
                            }
                        }
                    }
                    else
                    {
                        Debug.WriteLine("CAPK: emv_retrieveCAPKList() - ERROR={0}", rt);
                    }
                }
            }
            catch (Exception exp)
            {
                Debug.WriteLine("device: ValidateAidList() - exception={0}", (object)exp.Message);
            }
        }
Example #3
0
        public override void ValidateTerminalData(ConfigSerializer serializer)
        {
            try
            {
                if (serializer != null)
                {
                    byte []     tlv = null;
                    RETURN_CODE rt  = IDT_Augusta.SharedController.emv_retrieveTerminalData(ref tlv);

                    if (rt == RETURN_CODE.RETURN_CODE_DO_SUCCESS)
                    {
                        Debug.WriteLine("VALIDATE TERMINAL DATA ----------------------------------------------------------------------");

                        // Get Configuration File AID List
                        SortedDictionary <string, string> cfgTerminalData      = serializer.GetTerminalData(serialNumber, EMVKernelVer);
                        Dictionary <string, Dictionary <string, string> > dict = Common.processTLV(tlv);

                        bool update = false;

                        // TAGS from device
                        foreach (Dictionary <string, string> devCollection in dict.Where(x => x.Key.Equals("unencrypted")).Select(x => x.Value))
                        {
                            foreach (var devTag in devCollection)
                            {
                                string devTagName  = devTag.Key;
                                string cfgTagValue = "";
                                bool   tagfound    = false;
                                bool   tagmatch    = false;
                                foreach (var cfgTag in cfgTerminalData)
                                {
                                    // Matching TAGNAME: compare keys
                                    if (devTag.Key.Equals(cfgTag.Key, StringComparison.CurrentCultureIgnoreCase))
                                    {
                                        tagfound = true;
                                        //Debug.Write("key: " + devTag.Key);

                                        // Compare value
                                        if (cfgTag.Value.Equals(devTag.Value, StringComparison.CurrentCultureIgnoreCase))
                                        {
                                            tagmatch = true;
                                            //Debug.WriteLine(" matches value: {0}", (object) devTag.Value);
                                        }
                                        else
                                        {
                                            //Debug.WriteLine(" DOES NOT match value: {0}!={1}", devTag.Value, cfgTag.Value);
                                            cfgTagValue = cfgTag.Value;
                                            update      = true;
                                        }
                                        break;
                                    }
                                    if (tagfound)
                                    {
                                        break;
                                    }
                                }
                                if (tagfound)
                                {
                                    Debug.WriteLine("TAG: {0} FOUND AND IT {1}", devTagName.PadRight(6, ' '), (tagmatch ? "MATCHES" : "DOES NOT MATCH"));
                                    if (!tagmatch)
                                    {
                                        Debug.WriteLine("{0}!={1}", devTag.Value, cfgTagValue);
                                    }
                                }
                                else
                                {
                                    Debug.WriteLine("TAG: {0} NOT FOUND", (object)devTagName.PadRight(6, ' '));
                                    update = true;
                                }
                            }
                        }

                        // Update Terminal Data
                        if (update)
                        {
                            TerminalSettings termsettings = serializer.GetTerminalSettings();
                            string           workerstr    = termsettings.MajorConfiguration;
                            string           majorcfgstr  = Regex.Replace(workerstr, "[^0-9.]", string.Empty);
                            int majorcfgint = 5;
                            if (Int32.TryParse(majorcfgstr, out majorcfgint))
                            {
                                rt = IDT_Augusta.SharedController.emv_setTerminalMajorConfiguration(majorcfgint);
                                if (rt == RETURN_CODE.RETURN_CODE_DO_SUCCESS)
                                {
                                    try
                                    {
                                        List <byte[]> collection = new List <byte[]>();
                                        foreach (var item in cfgTerminalData)
                                        {
                                            byte [] bytes   = null;
                                            string  payload = string.Format("{0}{1:X2}{2}", item.Key, item.Value.Length / 2, item.Value).ToUpper();
                                            if (System.Text.RegularExpressions.Regex.IsMatch(item.Value, @"[g-zG-Z\x20\x2E]+"))
                                            {
                                                List <byte> byteArray = new List <byte>();
                                                byteArray.AddRange(Device_IDTech.HexStringToByteArray(item.Key));
                                                byte [] item1   = Encoding.ASCII.GetBytes(item.Value);
                                                byte    itemLen = Convert.ToByte(item1.Length);
                                                byte [] item2   = new byte[] { itemLen };
                                                byteArray.AddRange(item2);
                                                byteArray.AddRange(item1);
                                                bytes = new byte[byteArray.Count];
                                                byteArray.CopyTo(bytes);
                                                //Logger.debug( "device: ValidateTerminalData() DATA={0}", BitConverter.ToString(bytes).Replace("-", string.Empty));
                                            }
                                            else
                                            {
                                                bytes = Device_IDTech.HexStringToByteArray(payload);
                                            }
                                            collection.Add(bytes);
                                        }
                                        var     flattenedList = collection.SelectMany(bytes => bytes);
                                        byte [] terminalData  = flattenedList.ToArray();
                                        rt = IDT_Augusta.SharedController.emv_setTerminalData(terminalData);
                                        if (rt != RETURN_CODE.RETURN_CODE_DO_SUCCESS)
                                        {
                                            Debug.WriteLine("emv_setTerminalData() error: {0}", rt);
                                            Logger.error("device: ValidateTerminalData() error={0} DATA={1}", rt, BitConverter.ToString(terminalData).Replace("-", string.Empty));
                                        }
                                    }
                                    catch (Exception exp)
                                    {
                                        Debug.WriteLine("device: ValidateTerminalData() - exception={0}", (object)exp.Message);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        Debug.WriteLine("TERMINAL DATA: emv_retrieveTerminalData() - ERROR={0}", rt);
                    }
                }
            }
            catch (Exception exp)
            {
                Debug.WriteLine("device: ValidateTerminalData() - exception={0}", (object)exp.Message);
            }
        }