public async Task <bool> Login(int sector, InternalKeyType key)
        {
            var keyTypeToUse = key;

            byte[] keyToUse;

            if (key == InternalKeyType.KeyDefaultF)
            {
                // If it's a default request, load the default key and use KeyA
                keyToUse     = DefaultKey;
                keyTypeToUse = InternalKeyType.KeyA;
            }
            else
            {
                //try to find the right key for the sector
                if (!keyMap.TryGetValue(new SectorKey(sector, key), out keyToUse))
                {
                    return(false); // No provided key type for the sector
                }
            }

            var gaKeyType = (GeneralAuthenticate.GeneralAuthenticateKeyType)keyTypeToUse;

            // Get the first block for the sector
            var blockNumber = SectorToBlock(sector, 0);

            // see if we have the key loaded already
            byte location;

            if (!keyToLocationMap.TryGetValue(keyToUse, out location))
            {
                location = nextKeySlot;
                nextKeySlot++;
                keyToLocationMap[keyToUse] = location;

                // Load the key to the location
                var r = await TransceiveAsync(new LoadKey(keyToUse, location));

                if (!r.Succeeded)
                {
                    return(false); // could not load the key
                }
            }

            var res = await TransceiveAsync(new PcSc.MiFareStandard.GeneralAuthenticate(blockNumber, location, gaKeyType));

            return(res.Succeeded);
        }
Beispiel #2
0
        public async Task <bool> Login(int sector, InternalKeyType key)
        {
            LoadKeys.LoadKeysStorageType storage = LoadKeys.LoadKeysStorageType.NonVolatile;
            if (this.GetType() == typeof(MiFareWin32CardReader) &&
                (this as MiFareWin32CardReader).SmartCard.ReaderName.Contains("1252"))
            {
                storage = LoadKeys.LoadKeysStorageType.Volatile;
            }

            var keyTypeToUse = key;

            byte[] keyToUse;

            if (key == InternalKeyType.KeyDefaultF)
            {
                // If it's a default request, load the default key and use KeyA
                keyToUse     = DefaultKey;
                keyTypeToUse = InternalKeyType.KeyA;
            }
            else
            {
                //try to find the right key for the sector
                if (!keyMap.TryGetValue(new SectorKey(sector, key), out keyToUse))
                {
                    return(false); // No provided key type for the sector
                }
            }

            var gaKeyType = (GeneralAuthenticate.GeneralAuthenticateKeyType)keyTypeToUse;

            // Get the first block for the sector
            var blockNumber = SectorToBlock(sector, 0);

            // if reader have only one KeySlot - not store key slots to collection (not cache)
            byte location = 0;

            if (this.GetType() == typeof(MiFareWin32CardReader) &&
                (this as MiFareWin32CardReader).SmartCard.ReaderName.Contains("FEIG"))
            {
                var r = await TransceiveAsync(new LoadKey(keyToUse, location, storage));

                if (!r.Succeeded)
                {
                    return(false); // could not load the key
                }
            }
            // see if we have the key loaded already
            else if (!keyToLocationMap.TryGetValue(keyToUse, out location))
            {
                location = nextKeySlot;
                nextKeySlot++;
                keyToLocationMap[keyToUse] = location;

                // Load the key to the location
                var r = await TransceiveAsync(new LoadKey(keyToUse, location, storage));

                if (!r.Succeeded)
                {
                    return(false); // could not load the key
                }
            }

            if (storage == LoadKeys.LoadKeysStorageType.Volatile)
            {
                ApduResponse res;
                for (int z = 0; z < 5; z++)
                {
                    res = await TransceiveAsync(new PcSc.MiFareStandard.GeneralAuthenticate(blockNumber, location, gaKeyType));

                    if (res.Succeeded)
                    {
                        return(true);
                    }
                    System.Threading.Thread.Sleep(500);
                }
                return(false);
            }
            else
            {
                return((await TransceiveAsync(new PcSc.MiFareStandard.GeneralAuthenticate(blockNumber, location, gaKeyType))).Succeeded);
            }

            //return res.Succeeded;
        }
Beispiel #3
0
 public SectorKey(int sector, InternalKeyType keyType)
 {
     Sector  = sector;
     KeyType = keyType;
 }