Example #1
0
        public byte[] Sign(byte[] message)
        {
            int cnt = engine.ReaderCount;

            if (cnt == 0)
            {
                return(null);
            }

            byte[] digest = null;
            digest = ComputeDigest(new Sha256Digest(), message);

            for (int i = 0; i < cnt; i++)
            {
                using (CardReader reader = engine.GetReader(i))
                {
                    if (reader != null)
                    {
                        using (Card card = reader.GetCard(true))
                        {
                            if (card != null)
                            {
                                if (card.EidCard)
                                {
                                    return(Encryption.GenerateNonRepudiationSignature(reader, this.pin, digest, digest.Length));
                                }
                            }
                        }
                    }
                }
            }

            return(null);
        }
Example #2
0
        static void Main(string[] args)
        {
            Swelio.Engine.Manager engine = new  Manager();
            engine.Active = true;

            using (CardReader reader = engine.GetReader())
            {
                if (reader != null)
                {
                    using (Card card = reader.GetCard(true))
                    {
                        if (card != null)
                        {
                            var guid     = Guid.NewGuid();
                            var guidData = guid.ToByteArray();
                            using (SHA1Managed sha1 = new SHA1Managed())
                            {
                                var sha1hash      = sha1.ComputeHash(guidData);
                                var encryptedData = Encryption.GenerateNonRepudiationSignature(reader, "1234", sha1hash, sha1hash.Length);

                                if (encryptedData != null)
                                {
                                    Console.WriteLine("Signed data: " + Convert.ToBase64String(encryptedData, Base64FormattingOptions.None) + Environment.NewLine);
                                }
                                else
                                {
                                    Console.WriteLine("Could not sign data");
                                }
                            }
                        }
                    }
                }
            }
        }
Example #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            CardReader reader = engine.GetReader();

            if (reader != null)
            {
                Card card = reader.GetCard();
                if (card != null)
                {
                    card.DisplayCertificate(CertificateType.AuthenticationCertificate);
                }
            }
        }
Example #4
0
        public byte[] Sign(byte[] message)
        {
            int cnt = engine.ReaderCount;

            if (cnt == 0)
            {
                return(null);
            }

            byte[] digest = null;

            if (this.appletVersion >= 0x18)
            {
                digest = ComputeDigest(new Sha256Digest(), message);
            }
            else
            {
                digest = ComputeDigest(new Sha256Digest(), message);
            }

            for (int i = 0; i < cnt; i++)
            {
                using (CardReader reader = engine.GetReader(i))
                {
                    if (reader != null)
                    {
                        using (Card card = reader.GetCard(true))
                        {
                            if (card != null)
                            {
                                if (card.EidCard)
                                {
                                    var signature = Encryption.GenerateNonRepudiationSignature(reader, this.pin, digest, digest.Length);
                                    if (signature == null)
                                    {
                                        Console.WriteLine("Problem with generation of non-repudiation signature. Check swelio32/64.dll version");
                                        throw new ArgumentNullException("NonRepudiationSignature");
                                    }
                                    return(signature);
                                }
                            }
                        }
                    }
                }
            }

            return(null);
        }
Example #5
0
        public byte[] GetSigningCertificate()
        {
            bool canRead = (readerIndex > -1);

            if (!canRead)
            {
                canRead = IsCardInserted;
            }

            if (canRead)
            {
                CreateCacheFolder();
                string fileName = Path.Combine(cacheFolder, serialNumber + ".crt");
                if (FileOperations.FileExists(fileName))
                {
                    return(File.ReadAllBytes(fileName));
                }
                else
                {
                    CardReader reader = engine.GetReader(readerIndex);
                    if (reader == null)
                    {
                        return(null);
                    }

                    Card card = reader.GetCard(true);

                    if (card == null)
                    {
                        return(null);
                    }

                    byte[] result = card.ReadCertificate(CertificateType.NonRepudiationCertificate);
                    if (result != null)
                    {
                        File.WriteAllBytes(fileName, result);
                    }
                    return(result);
                }
            }
            else
            {
                return(null);
            }
        }
Example #6
0
        public bool CheckPassword(string password)
        {
            byte[] key = HexHelper.FromHexString(password);
            if (key.Length != 6)
            {
                throw (new Exception("Password error in (6 bytes): " + password));
            }

            _Config.KeysOne = key;
            //_Config.KeysZero = key;

            ICard ic;

            if (_Reader.GetCard(out ic, _Card.Atr, _Config))
            {
                CardMifare card = (CardMifare)ic;
                if (card.Sectors[AttackInSectorNum].DataBlocks[(byte)AttackInBlock].IsReaded)
                {
                    return(true);
                }
            }

            return(false);
        }
Example #7
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                return;
            }

            Swelio.Engine.Manager cardManager = new Manager();
            cardManager.Active = true;
            if (cardManager.ReaderCount > 0)
            {
                CardReader reader = cardManager.GetReader(0);
                if (reader != null)
                {
                    Card card = reader.GetCard();
                    if (card != null)
                    {
                        byte[] tempBytes     = card.EncodePhoto();
                        string imageAsString = Encoding.UTF8.GetString(tempBytes);
                        Console.WriteLine(imageAsString);
                    }
                }
            }
        }
Example #8
0
        public List <byte[]> GetAllCertificates()
        {
            List <byte[]> certificates = new List <byte[]>();

            bool canRead = (readerIndex > -1);

            if (!canRead)
            {
                canRead = IsCardInserted;
            }

            if (canRead)
            {
                CreateCacheFolder();

                string fileName = Path.Combine(cacheFolder, serialNumber + ".ca");
                if (FileOperations.FileExists(fileName))
                {
                    certificates.Add(File.ReadAllBytes(fileName));
                }
                else
                {
                    CardReader reader = engine.GetReader(readerIndex);
                    if (reader != null)
                    {
                        Card card = reader.GetCard(true);

                        if (card != null)
                        {
                            byte[] result = card.ReadCertificate(CertificateType.CaCertificate);
                            if (result != null)
                            {
                                File.WriteAllBytes(fileName, result);
                                certificates.Add(result);
                            }
                        }
                    }
                }


                fileName = Path.Combine(cacheFolder, serialNumber + ".root");
                if (FileOperations.FileExists(fileName))
                {
                    certificates.Add(File.ReadAllBytes(fileName));
                }
                else
                {
                    CardReader reader = engine.GetReader(readerIndex);
                    if (reader != null)
                    {
                        Card card = reader.GetCard(true);

                        if (card != null)
                        {
                            byte[] result = card.ReadCertificate(CertificateType.RootCaCertificate);
                            if (result != null)
                            {
                                File.WriteAllBytes(fileName, result);
                                certificates.Add(result);
                            }
                        }
                    }
                }
            }

            return(certificates);
        }
Example #9
0
        bool InternalRun(bool check)
        {
            byte[] file = System.IO.File.ReadAllBytes(File.FullName);

            using (CardReaderCollection cardReaderCollections = new CardReaderCollection())
                using (CardReader reader = cardReaderCollections[Target.Name])
                {
                    switch (reader.Connect())
                    {
                    case EConnection.Error:
                    {
                        WriteError("Error while connect to " + reader.Name);
                        return(false);
                    }

                    case EConnection.NotCard:
                    {
                        WriteError("Card not present, please insert a card");
                        return(false);
                    }
                    }

                    ICard ic;
                    if (!reader.GetCard(out ic, null))
                    {
                        WriteError("Error while getting card");
                        return(false);
                    }

                    if (ic == null)
                    {
                        WriteError("Please insert a card");
                        return(false);
                    }

                    WriteInfo("Card id ..........", ic.Id.ToString(), ConsoleColor.Cyan);
                    WriteInfo("Card type ........", ic.Type.ToString(), ConsoleColor.Cyan);

                    if (!(ic is CardMifare))
                    {
                        WriteError("Card not is mifare");
                        return(false);
                    }

                    CardMifare card = (CardMifare)ic;
                    WriteInfo("Mifare type ......", card.MifareType.ToString(), ConsoleColor.Cyan);
                    card.InitCard();

                    switch (card.MifareType)
                    {
                    case CardMifare.EMifareType.Classic1K:
                    {
                        int dSec   = card.Sectors[0].DataBlocks[0].Data.Length;
                        int length = card.Sectors.Length * (dSec * 4);       //6 key a / 6 keyb / 4 rights

                        if (file.Length != length)
                        {
                            WriteError("File length must be " + StringHelper.Convert2KbWithBytes(length));
                            return(false);
                        }
                        WriteInfo("Total sectors ....", card.Sectors.Length.ToString(), ConsoleColor.Cyan);

                        length = 0;
                        foreach (CardMifare.Sector sector in card.Sectors)
                        {
                            if (OnlySectors != null && OnlySectors.Count > 0 && !OnlySectors.Contains(sector.SectorNum))
                            {
                                length += dSec * (sector.DataBlocks.Length + 1);
                                WriteInfo("Checking sector " + sector.SectorNum.ToString().PadLeft(2, '0'), "Passed", ConsoleColor.Yellow);
                                continue;
                            }

                            byte[] keyA = new byte[6], keyB = new byte[6];

                            Array.Copy(file, length + (dSec * 3), keyA, 0, 6);
                            Array.Copy(file, length + (dSec * 3) + 10, keyB, 0, 6);

                            ConfigMifareRead cfg = new ConfigMifareRead()
                            {
                                KeysOne = keyA, KeysZero = keyB
                            };

                            ConfigMifareReadSector sec = cfg.ReadSectors[sector.SectorNum];
                            sec.Login = new LoginMifareMethod()
                            {
                                KeyNum  = KeyType == ConfigMifareRead.EKeyType.A ? ConfigMifareRead.EKeyNum.One : ConfigMifareRead.EKeyNum.Zero,
                                KeyType = KeyType
                            };
                            sec.ReadDataBlockEnd   = ConfigMifareReadSector.EBlockRange.DataBlock03;
                            sec.ReadDataBlockStart = ConfigMifareReadSector.EBlockRange.DataBlock01;
                            sec.ReadTrailBlock     = false;

                            ICard c2;
                            if (!reader.GetCard(out c2, cfg))
                            {
                                WriteError("Error reading sector " + sector.SectorNum.ToString() + " Blocks 1-3");
                                return(false);
                            }

                            CardMifare        card2   = (CardMifare)c2;
                            CardMifare.Sector sector2 = card2.Sectors[sector.SectorNum];

                            bool equal = true;
                            foreach (CardMifare.Block block in sector2.DataBlocks)
                            {
                                // Check block
                                bool blockEqual = true;
                                for (int x = 0, m = block.Data.Length; x < m; x++)
                                {
                                    if (block.Data[x] != file[x + length])
                                    {
                                        blockEqual = false; break;
                                    }
                                }

                                if (!blockEqual)
                                {
                                    equal = false;
                                    if (!check)
                                    {
                                        // Write block
                                        byte[] data = new byte[block.Data.Length];
                                        Array.Copy(file, length, data, 0, data.Length);
                                        if (!block.WriteInBlock(reader, cfg, data))
                                        {
                                            WriteError("Error writting sector " + sector2.SectorNum.ToString() + " block " + block.BlockNum.ToString());
                                        }
                                    }
                                }
                                length += block.Data.Length;
                            }

                            // Info
                            if (!equal)
                            {
                                WriteInfo("Checking sector " + sector.SectorNum.ToString().PadLeft(2, '0'), check ? "Different" : "Modified", ConsoleColor.Red);
                            }
                            else
                            {
                                WriteInfo("Checking sector " + sector.SectorNum.ToString().PadLeft(2, '0'), "Equal", ConsoleColor.Green);
                            }

                            length += dSec;       //6 key a / 6 keyb / 4 rights
                        }

                        return(true);
                    }

                    default:
                    {
                        WriteError("Card " + card.MifareType.ToString() + " not implemented");
                        return(false);
                    }
                    }
                }
        }
Example #10
0
        static void Main(string[] args)
        {
            Card card = null;

            Manager engine = new Manager();

            engine.Active = true;

            if (engine.ReaderCount == 0)
            {
                Console.WriteLine("No card reader detected");
                return;
            }

            Console.WriteLine("Available readers:");
            for (int i = 0; i < engine.ReaderCount; i++)
            {
                Console.WriteLine(engine.GetReaderName(i));
            }

            Console.WriteLine("Please insert ID card in the reader and press Enter");
            Console.ReadLine();
            CardReader reader = engine.GetReader(0);

            if (reader != null)
            {
                reader.ActivateCard();
                card = reader.GetCard();
                if (card != null)
                {
                    Identity identity = card.ReadIdentity();
                    if (identity != null)
                    {
                        Console.WriteLine("Welcome {0} {1}", identity.FirstName1, identity.Surname);
                    }

                    //string csv = card.Save(ExportFormat.Csv, FileEncoding.Unicode);
                    //Console.WriteLine(csv);
                }
                reader.DeactivateCard();
            }


            //Card insertion and removal test
            card = reader.GetCard();
            if (reader.CardPresent)
            {
                card = reader.GetCard();
            }

            Console.WriteLine("Please remove ID card from the reader and press Enter");
            Console.ReadLine();

            if (card.Inserted)
            {
                Console.WriteLine("The card is still inserted in the reader");
            }

            Console.WriteLine();
            byte[] md5sum = Encryption.GetMD5("message digest");
            for (byte j = 0; j < 16; j++)
            {
                Console.Write("{0:X2}", md5sum[j]);
            }
            Console.WriteLine();


            engine.Dispose();
            Console.WriteLine("Press Enter to quit...");
            Console.ReadLine();
        }
Example #11
0
        bool InternalRun(bool check)
        {
            using (CardReaderCollection cardReaderCollections = new CardReaderCollection())
                using (CardReader reader = cardReaderCollections[Target.Name])
                {
                    switch (reader.Connect())
                    {
                    case EConnection.Error:
                    {
                        WriteError("Error while connect to " + reader.Name);
                        return(false);
                    }

                    case EConnection.NotCard:
                    {
                        WriteError("Card not present, please insert a card");
                        return(false);
                    }
                    }

                    ICard ic;
                    if (!reader.GetCard(out ic, null))
                    {
                        WriteError("Error while getting card");
                        return(false);
                    }

                    if (ic == null)
                    {
                        WriteError("Please insert a card");
                        return(false);
                    }

                    WriteInfo("Card id ..........", ic.Id.ToString(), ConsoleColor.Cyan);
                    WriteInfo("Card type ........", ic.Type.ToString(), ConsoleColor.Cyan);

                    if (!(ic is CardMifare))
                    {
                        WriteError("Card not is mifare");
                        return(false);
                    }

                    CardMifare card = (CardMifare)ic;
                    WriteInfo("Mifare type ......", card.MifareType.ToString(), ConsoleColor.Cyan);
                    card.InitCard();

                    switch (card.MifareType)
                    {
                    case CardMifare.EMifareType.Classic1K:
                    {
                        if (Id == null || Id.Length != 4)
                        {
                            WriteError("Id must be 4 byte length (00:01:02:03:04:05:06:07)");
                            return(false);
                        }

                        if (!check)
                        {
                            ConfigMifareRead cfg = new ConfigMifareRead();
                            cfg.KeysOne = Password;
                            cfg.ReadSectors[0].Login = new LoginMifareMethod()
                            {
                                KeyNum  = ConfigMifareRead.EKeyNum.One,
                                KeyType = KeyType
                            };

                            cfg.ReadSectors[0].ReadDataBlockStart = ConfigMifareReadSector.EBlockRange.DataBlock01;
                            cfg.ReadSectors[0].ReadDataBlockEnd   = ConfigMifareReadSector.EBlockRange.DataBlock03;
                            cfg.ReadSectors[0].ReadTrailBlock     = true;

                            if (reader.GetCard(out ic, cfg))
                            {
                                card = (CardMifare)ic;

                                Array.Copy(Id, 0, card.Sectors[0].DataBlocks[0].Data, 0, Id.Length);
                                if (card.Sectors[0].DataBlocks[0].WriteInBlock(reader, cfg, card.Sectors[0].DataBlocks[0].Data))
                                {
                                    WriteInfo("Sucessfull write!");
                                    return(true);
                                }
                                else
                                {
                                    WriteError("Error while writting sector, check the password");
                                }
                            }

                            return(false);
                        }

                        return(true);
                    }

                    default:
                    {
                        WriteError("Card " + card.MifareType.ToString() + " not implemented");
                        return(false);
                    }
                    }
                }
        }
Example #12
0
        public bool PreRun()
        {
            PostRun();

            _Config.ReadSectors[AttackInSectorNum].ReadDataBlockStart = AttackInBlock;
            _Config.ReadSectors[AttackInSectorNum].ReadDataBlockEnd   = AttackInBlock;
            _Config.ReadSectors[AttackInSectorNum].ReadTrailBlock     = false;
            _Config.ReadSectors[AttackInSectorNum].Login = new LoginMifareMethod()
            {
                KeyNum  = ConfigMifareRead.EKeyNum.One,
                KeyType = KeyType
            };

            if (_Targets == null)
            {
                _Targets = MifareRestoreClone.LoadReaders();
            }

            if (_Targets == null)
            {
                WriteError("Not readers detected");
                return(false);
            }

            if (Reader < 0 || Reader >= _Targets.Length)
            {
                WriteInfo("Please select one of this readers in 'Reader' option");

                int ix = 0;
                foreach (Target t in _Targets)
                {
                    WriteInfo(ix.ToString() + " - " + t.Name);
                    ix++;
                }

                return(false);
            }

            _Readers = new CardReaderCollection();
            _Reader  = _Readers[_Targets[0].Name];

            if (_Readers == null)
            {
                WriteError("Not readers detected");
                return(false);
            }

            switch (_Reader.Connect())
            {
            case EConnection.Ok: break;

            case EConnection.NotCard:
            {
                WriteError("Not card present, please insert a card");
                break;
            }
            }

            ICard ic;

            if (!_Reader.GetCard(out ic, null))
            {
                WriteError("Error while getting card");
                return(false);
            }

            if (ic == null)
            {
                WriteError("Please insert a card");
                return(false);
            }

            WriteInfo("Card id ..........", ic.Id.ToString(), ConsoleColor.Cyan);
            WriteInfo("Card type ........", ic.Type.ToString(), ConsoleColor.Cyan);

            if (!(ic is CardMifare))
            {
                WriteError("Card not is mifare");
                return(false);
            }

            _Card = (CardMifare)ic;
            WriteInfo("Mifare type ......", _Card.MifareType.ToString(), ConsoleColor.Cyan);
            _Card.InitCard();

            return(true);
        }