Exemplo n.º 1
0
        static int Main(string[] args)
        {
            LogManager.InstantiateLogManager();
            _aes_master_key = "";
            //_aes_new_master_key = "";
            _aes_base_key = "";
            _aes_owner_divert_root_key = "";
            _iso_enable = false;
            Logger.ReadArgs(args);

            //Logger.ConsoleLevel = Logger.Level.Info;
            Console.Title = string.Format("{0} {1} v.{2}", Application.CompanyName, Application.ProductName, Application.ProductVersion);

            if (args.Length > 0)
            {
                if (!ParseArgs(args))
                {
                    LogManager.DoLogOperation("[ERROR] Fails to parse argument !!!");

                    Console.WriteLine("Hit any key to exit.");
                    Console.ReadKey();
                    return(ERROR_PARSE_ERROR);
                }
            }
            else
            {
                goto done;
            }


done:

            m_ReaderList = new List <string>();
            int i = 0;

            if (SCARD.Readers == null)
            {
                return(ERROR_READER_ERROR);
            }
            for (i = 0; i < SCARD.Readers.Length - 1; i++)
            {
                m_ReaderList.Add(SCARD.Readers[i]);
            }

            if (SCARD.Readers.Length == 0)
            {
                LogManager.DoLogOperation("[ERROR] No PCSC reader available !!!");
                return(ERROR_READER_ERROR);
            }
            m_ReaderList.Add(SCARD.Readers[SCARD.Readers.Length - 1]);

            #region format
            if ((_actions_to_do & ActionToDo.actFormat) != 0)
            {
                LogManager.DoLogOperation("[INFO] Formatting Desfire Card...");
                if (_reader_id == -1)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a reader. Add --reader=X to console command.");
                    return(ERROR_READER_ERROR);
                }

                if (_aes_master_key.Length != 32)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a base key. Add --master-key=XXXX...X to console command.");
                    return(ERROR_PARSE_ERROR);
                }
                if (InsertCard() == ERROR_NO_ERROR)
                {
                    m_isoWrapping = DueInfoDesfireEv2.DF_ISO_WRAPPING_CARD;
                    m_Desfire     = new DueInfoDesfireEv2(m_hCard, m_isoWrapping);

                    m_Desfire.AesKeyMaster = BinConvert.HexToBytes(_aes_master_key);
                    if (m_Desfire.Format())
                    {
                        LogManager.DoLogOperation("[INFO] DUEINFO Done ...");
                    }

                    EjectCard();
                }
            }
            #endregion

            #region new
            if ((_actions_to_do & ActionToDo.actNew) != 0)
            {
                LogManager.DoLogOperation("[INFO] Creating new DUEINFO Desfire Card...");
                if (_reader_id == -1)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a reader. Add --reader=X to console command.");
                    return(ERROR_READER_ERROR);
                }

                if (_aes_base_key.Length != 32)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set DUEINFO base key for diversification. Add --dueinfo-key=XXXX...X to console command.");
                    return(ERROR_PARSE_ERROR);
                }

                if (_aes_master_key.Length != 32)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a base key. Add --master-key=XXXX...X to console command.");
                    return(ERROR_PARSE_ERROR);
                }

                if (InsertCard() == ERROR_NO_ERROR)
                {
                    m_isoWrapping = DueInfoDesfireEv2.DF_ISO_WRAPPING_CARD;
                    m_Desfire     = new DueInfoDesfireEv2(m_hCard, m_isoWrapping);

                    m_Desfire.AesKeyMaster = BinConvert.HexToBytes(_aes_master_key);
                    //m_Desfire.AesNewKeyMaster = BinConvert.HexToBytes(_aes_new_master_key);
                    m_Desfire.AesKeyDueInfo = BinConvert.HexToBytes(_aes_base_key);
                    m_Desfire.Escn          = BinConvert.HexToBytes(_escn_id);
                    if (m_Desfire.Create())
                    {
                        LogManager.DoLogOperation("[INFO] DUEINFO Done ...");
                    }

                    EjectCard();
                }
            }
            #endregion
            #region erase
            else if ((_actions_to_do & ActionToDo.actDisable) != 0)
            {
                LogManager.DoLogOperation("[INFO] Erasing DUEINFO Desfire Card...");
                if (_reader_id == -1)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a reader. Add --reader=X to console command.");
                    return(ERROR_READER_ERROR);
                }
                if (InsertCard() == ERROR_NO_ERROR)
                {
                    m_isoWrapping = DueInfoDesfireEv2.DF_ISO_WRAPPING_CARD;
                    m_Desfire     = new DueInfoDesfireEv2(m_hCard, m_isoWrapping);

                    if (_aes_master_key.Length == 32)
                    {
                        m_Desfire.AesKeyMaster = BinConvert.HexToBytes(_aes_master_key);

                        if (m_Desfire.Disable())
                        {
                            LogManager.DoLogOperation("[INFO] DUEINFO is disabled ...");
                        }
                    }

                    EjectCard();
                }
            }
            #endregion

            #region read
            else if ((_actions_to_do & ActionToDo.actRead) != 0)
            {
                LogManager.DoLogOperation("[INFO] Reading DUEINFO Desfire Card...");
                if (_reader_id == -1)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a reader. Add --reader=X to console command.");
                    return(ERROR_READER_ERROR);
                }
                if (InsertCard() == ERROR_NO_ERROR)
                {
                    if (_iso_enable == false)
                    {
                        m_isoWrapping = DueInfoDesfireEv2.DF_ISO_WRAPPING_CARD;
                        m_Desfire     = new DueInfoDesfireEv2(m_hCard, m_isoWrapping);

                        if (m_Desfire.Read(ref _escn, ref _signature, ref _certificate))
                        {
                            LogManager.DoLogOperation("[INFO] Read Done ...");
                        }
                    }
                    else
                    {
                        if (Iso_Read(ref _escn, ref _signature, ref _certificate))
                        {
                            LogManager.DoLogOperation("[INFO] Read Done ...");
                        }
                    }

                    EjectCard();
                }
            }
            #endregion

            #region check
            else if ((_actions_to_do & ActionToDo.actCheck) != 0)
            {
                LogManager.DoLogOperation("[INFO] Checking DUEINFO Desfire Card...");

                if (_reader_id == -1)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a reader. Add --reader=X to console command.");
                    return(ERROR_READER_ERROR);
                }

                /* read data from card */
                if (InsertCard() == ERROR_NO_ERROR)
                {
                    m_isoWrapping           = DueInfoDesfireEv2.DF_ISO_WRAPPING_CARD;
                    m_Desfire               = new DueInfoDesfireEv2(m_hCard, m_isoWrapping);
                    m_Desfire.AesKeyDueInfo = BinConvert.HexToBytes(_aes_base_key);

                    if (m_Desfire.Read(ref _escn, ref _signature, ref _certificate))
                    {
                        LogManager.DoLogOperation("[INFO] Read Done ...");
                        if (m_Desfire.Check(_escn, _signature, _certificate))
                        {
                            LogManager.DoLogOperation("[INFO] Card is from DUEINFO ...");
                        }
                        else
                        {
                            LogManager.DoLogOperation("ERROR Card is out of DUEINFO ...");
                        }
                    }

                    EjectCard();
                }
            }
            #endregion
            #region diversification
            else if ((_actions_to_do & ActionToDo.actDiversification) != 0)
            {
                LogManager.DoLogOperation("[INFO] Check AES 128 diversification...");

                if (_aes_base_key.Length != 32)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a base key. Add --base=XXXX...X to console command.");
                    return(ERROR_READER_ERROR);
                }
                if (_escn_id.Length == 0)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set ESCn. Add --escn=XX.. to console command.");
                    return(ERROR_READER_ERROR);
                }
                byte[] diversified_key   = new byte[16];
                byte[] data_to_diversify = BinConvert.HexToBytes(_escn_id);
                DueInfo.Diversification.Diversification_AES128(BinConvert.HexToBytes(_aes_base_key),
                                                               data_to_diversify, data_to_diversify.Length, ref diversified_key);
            }
            #endregion

            #region load_aes_master_keys
            if ((_actions_to_do & ActionToDo.actLoadMasterKey) != 0)
            {
                LogManager.DoLogOperation("[INFO] Load Master and DAM keys...");
                if (_reader_id == -1)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a reader. Add --reader=X to console command.");
                    return(ERROR_READER_ERROR);
                }

                if (_aes_master_key.Length != 32)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a base key. Add --master-key=XXXX...X to console command.");
                    return(ERROR_PARSE_ERROR);
                }


                if (_aes_owner_divert_root_key.Length != 32)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a root key for diversification. Add --base=XXXX...X to console command.");
                    return(ERROR_READER_ERROR);
                }

                if (_card_uid.Length == 0)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set card uid for diversification. Add --uid=XXXX...X to console command.");
                    return(ERROR_READER_ERROR);
                }

                byte[] UID = BinConvert.HexToBytes(_card_uid);

                if (InsertCard() == ERROR_NO_ERROR)
                {
                    m_isoWrapping = DueInfoDesfireEv2.DF_ISO_WRAPPING_CARD;
                    m_Desfire     = new DueInfoDesfireEv2(m_hCard, m_isoWrapping);

                    m_Desfire.AesKeyMaster = BinConvert.HexToBytes(_aes_master_key);

                    byte[] diversified_key = new byte[16];

                    m_Desfire.DAMAuthKey = new byte[16];// BinConvert.HexToBytes(_dam_auth_key);
                    DueInfo.Diversification.Diversification_AES128(
                        BinConvert.HexToBytes(_aes_owner_divert_root_key),
                        UID, UID.Length, ref diversified_key);
                    Array.Copy(diversified_key, 0, m_Desfire.DAMAuthKey, 0, 16);
                    LogManager.DoLogOperation("[INFO] PICCDAMAuthKey " + BinConvert.ToHex(m_Desfire.DAMAuthKey));

                    byte[] data_to_diversify = BinConvert.HexToBytes(_card_uid);
                    data_to_diversify[0] = 0x00;
                    m_Desfire.DAMENCKey  = new byte[16]; //BinConvert.HexToBytes(_dam_enc_key);
                    DueInfo.Diversification.Diversification_AES128(
                        BinConvert.HexToBytes(_aes_owner_divert_root_key),
                        data_to_diversify, data_to_diversify.Length, ref diversified_key);
                    Array.Copy(diversified_key, 0, m_Desfire.DAMENCKey, 0, 16);
                    LogManager.DoLogOperation("[INFO] PICCDAMEncKey " + BinConvert.ToHex(m_Desfire.DAMENCKey));

                    data_to_diversify    = BinConvert.HexToBytes(_card_uid);
                    data_to_diversify[0] = 0xFF;

                    m_Desfire.DAMMACKey = new byte[16]; //BinConvert.HexToBytes(_dam_mac_key);
                    DueInfo.Diversification.Diversification_AES128(
                        BinConvert.HexToBytes(_aes_owner_divert_root_key),
                        data_to_diversify, data_to_diversify.Length, ref diversified_key);
                    Array.Copy(diversified_key, 0, m_Desfire.DAMMACKey, 0, 16);
                    LogManager.DoLogOperation("[INFO] PICCDAMMACKey " + BinConvert.ToHex(m_Desfire.DAMMACKey));

                    /* we do this to be sure that masker key is AES type, needed for PICCDAMKEY */
                    if (m_Desfire.ChangeMasterKeyToAES())
                    {
                        LogManager.DoLogOperation("[INFO] Master key set to AES ...");
                        if (m_Desfire.LoadPICCDAMKeys())
                        {
                            LogManager.DoLogOperation("[INFO] PICC DAM keys loaded ...");
                        }
                    }

                    EjectCard();
                }
            }
            #endregion
            #region request_web
            if ((_actions_to_do & ActionToDo.actDammac) != 0)
            {
                LogManager.DoLogOperation("[INFO] Create DAMMAC...");
                if (_reader_id == -1)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a reader. Add --reader=X to console command.");
                    return(ERROR_READER_ERROR);
                }
                if (_dam_auth_key.Length != 32)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set PICCDAMAUTHKEY. Add --master-key=XXXX...X to console command.");
                    return(ERROR_PARSE_ERROR);
                }

                if (InsertCard() == ERROR_NO_ERROR)
                {
                    m_isoWrapping = DueInfoDesfireEv2.DF_ISO_WRAPPING_CARD;
                    m_Desfire     = new DueInfoDesfireEv2(m_hCard, m_isoWrapping);

                    m_Desfire.DAMAuthKey = BinConvert.HexToBytes(_dam_auth_key);

                    LogManager.DoLogOperation("[INFO] Create DAMMAC ...");
                    if (m_Desfire.CreateDammacRequest())
                    {
                        LogManager.DoLogOperation("[INFO] DAMMAC JSON REQUEST is created ...");
                    }

                    EjectCard();
                }
            }

            #endregion request_web
            #region dam
            if ((_actions_to_do & ActionToDo.actNewDam) != 0)
            {
                LogManager.DoLogOperation("[INFO] Create delegated application...");
                if (_reader_id == -1)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a reader. Add --reader=X to console command.");
                    return(ERROR_READER_ERROR);
                }
                if (_dam_auth_key.Length != 32)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set DAMAuthKey. Add --x-damauth-key=XXXX...X to console command.");
                    return(ERROR_PARSE_ERROR);
                }

                if (_dam_enck.Length == 0)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set EncK. Add --n-enck=XXXX...X to console command.");
                    return(ERROR_PARSE_ERROR);
                }
                if (_dam_dammac.Length != 16)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set DAMMAC. Add --q-dammac=XXXX...X to console command.");
                    return(ERROR_PARSE_ERROR);
                }


                if (InsertCard() == ERROR_NO_ERROR)
                {
                    m_isoWrapping = DueInfoDesfireEv2.DF_ISO_WRAPPING_CARD;
                    m_Desfire     = new DueInfoDesfireEv2(m_hCard, m_isoWrapping);

                    m_Desfire.DAMAuthKey = BinConvert.HexToBytes(_dam_auth_key);
                    m_Desfire.EncK       = BinConvert.HexToBytes(_dam_enck);
                    m_Desfire.DAMMAC     = BinConvert.HexToBytes(_dam_dammac);

                    LogManager.DoLogOperation("[INFO] Create DAM ...");
                    if (m_Desfire.AddDelegatedApplication())
                    {
                        LogManager.DoLogOperation("[INFO] DAM is created ...");
                    }

                    EjectCard();
                }
            }
            if ((_actions_to_do & ActionToDo.actNewDamAlone) != 0)
            {
                LogManager.DoLogOperation("[INFO] Create delegated application alone...");
                if (_reader_id == -1)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a reader. Add --reader=X to console command.");
                    return(ERROR_READER_ERROR);
                }

                if (_dam_auth_key.Length != 32)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set DAMAuthKey. Add --x-damauth-key=XXXX...X to console command.");
                    return(ERROR_PARSE_ERROR);
                }

                if (_dam_mac_key.Length != 32)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a DAMMAC key. Add --y-dammac-key=XXXX...X to console command.");
                    return(ERROR_PARSE_ERROR);
                }
                if (_dam_enc_key.Length != 32)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a Encryption key. Add --z-damenc-key=XXXX...X to console command.");
                    return(ERROR_PARSE_ERROR);
                }
                if (InsertCard() == ERROR_NO_ERROR)
                {
                    m_isoWrapping = DueInfoDesfireEv2.DF_ISO_WRAPPING_CARD;
                    m_Desfire     = new DueInfoDesfireEv2(m_hCard, m_isoWrapping);

                    m_Desfire.DAMAuthKey = BinConvert.HexToBytes(_dam_auth_key);
                    m_Desfire.DAMENCKey  = BinConvert.HexToBytes(_dam_enc_key);
                    m_Desfire.DAMMACKey  = BinConvert.HexToBytes(_dam_mac_key);

                    LogManager.DoLogOperation("[INFO] Create DAM ...");
                    if (m_Desfire.AddDelegatedApplicationWithoutWebService())
                    {
                        LogManager.DoLogOperation("[INFO] DAM is created ...");
                    }

                    EjectCard();
                }
            }
            if ((_actions_to_do & ActionToDo.actFreeDam) != 0)
            {
                LogManager.DoLogOperation("[INFO] Free delegated application...");
                if (_reader_id == -1)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a reader. Add --reader=X to console command.");
                    return(ERROR_READER_ERROR);
                }


                if (_dam_auth_key.Length != 32)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set DAMAuthKey. Add --x-damauth-key=XXXX...X to console command.");
                    return(ERROR_PARSE_ERROR);
                }

                if (InsertCard() == ERROR_NO_ERROR)
                {
                    m_isoWrapping = DueInfoDesfireEv2.DF_ISO_WRAPPING_CARD;
                    m_Desfire     = new DueInfoDesfireEv2(m_hCard, m_isoWrapping);

                    m_Desfire.AesKeyMaster = BinConvert.HexToBytes(_aes_master_key);
                    m_Desfire.DAMAuthKey   = BinConvert.HexToBytes(_dam_auth_key);

                    LogManager.DoLogOperation("[INFO] Delete DAM ...");
                    if (m_Desfire.FormatDelegatedApplication())
                    {
                        LogManager.DoLogOperation("[INFO] DAM is formatted ...");
                    }

                    EjectCard();
                }
            }
            if ((_actions_to_do & ActionToDo.actEraseDam) != 0)
            {
                LogManager.DoLogOperation("[INFO] Erase delegated application...");
                if (_reader_id == -1)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a reader. Add --reader=X to console command.");
                    return(ERROR_READER_ERROR);
                }

                if (_aes_master_key.Length != 32)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a base key. Add --master-key=XXXX...X to console command.");
                    return(ERROR_PARSE_ERROR);
                }

                if (InsertCard() == ERROR_NO_ERROR)
                {
                    m_isoWrapping = DueInfoDesfireEv2.DF_ISO_WRAPPING_CARD;
                    m_Desfire     = new DueInfoDesfireEv2(m_hCard, m_isoWrapping);

                    m_Desfire.AesKeyMaster = BinConvert.HexToBytes(_aes_master_key);

                    LogManager.DoLogOperation("[INFO] Delete DAM ...");
                    if (m_Desfire.EraseDelegatedApplication())
                    {
                        LogManager.DoLogOperation("[INFO] DAM is deleted ...");
                    }

                    EjectCard();
                }
            }

            #endregion
            #region list
            if ((_actions_to_do & ActionToDo.actList) != 0)
            {
                LogManager.DoLogOperation("[INFO]List PCSC readers...");
                for (i = 0; i < m_ReaderList.Count; i++)
                {
                    LogManager.DoLogOperation(string.Format("[INFO]\t{0:X02}\t{1}", i, m_ReaderList[i]));
                }
            }
            #endregion
            #region pause
            if ((_actions_to_do & ActionToDo.actPause) != 0)
            {
                Console.WriteLine("Hit any key to exit.");
                Console.ReadKey();
            }
            #endregion
            return(ERROR_NO_ERROR);
        }
Exemplo n.º 2
0
        static int Main(string[] args)
        {
            LogManager.InstantiateLogManager();
            _aes_master_key = "";
            _aes_base_key   = "";
            _iso_enable     = false;
            Logger.ReadArgs(args);

            //Logger.ConsoleLevel = Logger.Level.Info;
            Console.Title = string.Format("{0} {1} v.{2}", Application.CompanyName, Application.ProductName, Application.ProductVersion);

            if (args.Length > 0)
            {
                if (!ParseArgs(args))
                {
                    LogManager.DoLogOperation("[ERROR] Fails to parse argument !!!");

                    Console.WriteLine("Hit any key to exit.");
                    Console.ReadKey();
                    return(ERROR_PARSE_ERROR);
                }
            }
            else
            {
                goto done;
            }


done:

            m_ReaderList = new List <string>();
            int i = 0;

            if (SCARD.Readers == null)
            {
                return(ERROR_READER_ERROR);
            }
            for (i = 0; i < SCARD.Readers.Length - 1; i++)
            {
                m_ReaderList.Add(SCARD.Readers[i]);
            }

            if (SCARD.Readers.Length == 0)
            {
                LogManager.DoLogOperation("[ERROR] No PCSC reader available !!!");
                return(ERROR_READER_ERROR);
            }
            m_ReaderList.Add(SCARD.Readers[SCARD.Readers.Length - 1]);

            #region new
            if ((_actions_to_do & ActionToDo.actNew) != 0)
            {
                LogManager.DoLogOperation("[INFO] Creating new DUEINFO Desfire Card...");
                if (_reader_id == -1)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a reader. Add --reader=X to console command.");
                    return(ERROR_READER_ERROR);
                }

                if (_aes_base_key.Length != 32)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set DUEINFO base key for diversification. Add --dueinfo-key=XXXX...X to console command.");
                    return(ERROR_PARSE_ERROR);
                }

                if (_aes_master_key.Length != 32)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a base key. Add --master-key=XXXX...X to console command.");
                    return(ERROR_PARSE_ERROR);
                }

                if (InsertCard() == ERROR_NO_ERROR)
                {
                    m_isoWrapping = DueInfoDesfire.DF_ISO_WRAPPING_CARD;
                    m_Desfire     = new DueInfoDesfire(m_hCard, m_isoWrapping);

                    m_Desfire.AesKeyMaster  = BinConvert.HexToBytes(_aes_master_key);
                    m_Desfire.AesKeyDueInfo = BinConvert.HexToBytes(_aes_base_key);
                    m_Desfire.Escn          = BinConvert.HexToBytes(_escn_id);
                    if (m_Desfire.Create())
                    {
                        LogManager.DoLogOperation("[INFO] DUEINFO Done ...");
                    }

                    EjectCard();
                }
            }
            #endregion
            #region erase
            else if ((_actions_to_do & ActionToDo.actDisable) != 0)
            {
                LogManager.DoLogOperation("[INFO] Erasing DUEINFO Desfire Card...");
                if (_reader_id == -1)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a reader. Add --reader=X to console command.");
                    return(ERROR_READER_ERROR);
                }
                if (InsertCard() == ERROR_NO_ERROR)
                {
                    m_isoWrapping = DueInfoDesfire.DF_ISO_WRAPPING_CARD;
                    m_Desfire     = new DueInfoDesfire(m_hCard, m_isoWrapping);

                    if (_aes_master_key.Length == 32)
                    {
                        m_Desfire.AesKeyMaster = BinConvert.HexToBytes(_aes_master_key);

                        if (m_Desfire.Disable())
                        {
                            LogManager.DoLogOperation("[INFO] DUEINFO is disabled ...");
                        }
                    }

                    EjectCard();
                }
            }
            #endregion

            #region read
            else if ((_actions_to_do & ActionToDo.actRead) != 0)
            {
                LogManager.DoLogOperation("[INFO] Reading DUEINFO Desfire Card...");
                if (_reader_id == -1)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a reader. Add --reader=X to console command.");
                    return(ERROR_READER_ERROR);
                }
                if (InsertCard() == ERROR_NO_ERROR)
                {
                    if (_iso_enable == false)
                    {
                        m_isoWrapping = DueInfoDesfire.DF_ISO_WRAPPING_CARD;
                        m_Desfire     = new DueInfoDesfire(m_hCard, m_isoWrapping);

                        if (m_Desfire.Read(ref _escn, ref _signature, ref _certificate))
                        {
                            LogManager.DoLogOperation("[INFO] Read Done ...");
                        }
                    }
                    else
                    {
                        if (Iso_Read(ref _escn, ref _signature, ref _certificate))
                        {
                            LogManager.DoLogOperation("[INFO] Read Done ...");
                        }
                    }

                    EjectCard();
                }
            }
            #endregion

            #region check
            else if ((_actions_to_do & ActionToDo.actCheck) != 0)
            {
                LogManager.DoLogOperation("[INFO] Checking DUEINFO Desfire Card...");

                if (_reader_id == -1)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a reader. Add --reader=X to console command.");
                    return(ERROR_READER_ERROR);
                }

                /* read data from card */
                if (InsertCard() == ERROR_NO_ERROR)
                {
                    m_isoWrapping           = DueInfoDesfire.DF_ISO_WRAPPING_CARD;
                    m_Desfire               = new DueInfoDesfire(m_hCard, m_isoWrapping);
                    m_Desfire.AesKeyDueInfo = BinConvert.HexToBytes(_aes_base_key);

                    if (m_Desfire.Read(ref _escn, ref _signature, ref _certificate))
                    {
                        LogManager.DoLogOperation("[INFO] Read Done ...");
                        if (m_Desfire.Check(_escn, _signature, _certificate))
                        {
                            LogManager.DoLogOperation("[INFO] Card is from DUEINFO ...");
                        }
                        else
                        {
                            LogManager.DoLogOperation("ERROR Card is out of DUEINFO ...");
                        }
                    }

                    EjectCard();
                }
            }
            #endregion
            #region diversification
            else if ((_actions_to_do & ActionToDo.actDiversification) != 0)
            {
                LogManager.DoLogOperation("[INFO] Check AES 128 diversification...");

                if (_aes_base_key.Length != 32)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set a base key. Add --base=XXXX...X to console command.");
                    return(ERROR_READER_ERROR);
                }
                if (_escn_id.Length == 0)
                {
                    LogManager.DoLogOperation("[WARNING] You have to set ESCn. Add --escn=XX.. to console command.");
                    return(ERROR_READER_ERROR);
                }
                byte[] diversified_key   = new byte[16];
                byte[] data_to_diversify = BinConvert.HexToBytes(_escn_id);
                DueInfo.Diversification.Diversification_AES128(BinConvert.HexToBytes(_aes_base_key),
                                                               data_to_diversify, data_to_diversify.Length, ref diversified_key);
            }
            #endregion


            #region list
            if ((_actions_to_do & ActionToDo.actList) != 0)
            {
                LogManager.DoLogOperation("[INFO]List PCSC readers...");
                for (i = 0; i < m_ReaderList.Count; i++)
                {
                    LogManager.DoLogOperation(string.Format("[INFO]\t{0:X02}\t{1}", i, m_ReaderList[i]));
                }
            }
            #endregion
            #region pause
            if ((_actions_to_do & ActionToDo.actPause) != 0)
            {
                Console.WriteLine("Hit any key to exit.");
                Console.ReadKey();
            }
            #endregion
            return(ERROR_NO_ERROR);
        }