コード例 #1
0
ファイル: MainWindow.xaml.cs プロジェクト: thebChip/bchip
        private void _monitor_CardInserted(object sender, CardStatusEventArgs e)
        {
            if (e.State == SCRState.Present)
            {
                try
                {
                    BChipSmartCard insertedCard =
                        new BChipSmartCard(e.ReaderName)
                    {
                        ATR           = e.Atr,
                        LastConnected = DateTime.Now,
                        IsConnected   = true
                    };

                    if (insertedCard.Type() == CardType.BChip)
                    {
                        ScanAndLoadConnectedCards(insertedCard);
                    }
                }
                catch (Exception ex)
                {
                    Logger.Log(ex);
                }
            }
        }
コード例 #2
0
ファイル: MainWindow.xaml.cs プロジェクト: thebChip/bchip
        public PageToShow AnalyzeCardData(BChipSmartCard bChipSmartCard)
        {
            if (!bChipSmartCard.IsConnected)
            {
                return(PageToShow.NoCard);
            }

            if (bChipSmartCard.Type() != CardType.BChip || bChipSmartCard.SmartCardData == null)
            {
                return(PageToShow.Unsupported);
            }

            // Analyze card
            try
            {
                BChipMemoryLayout_BCHIP cardMemory = (BChipMemoryLayout_BCHIP)bChipSmartCard.SmartCardData;

                if (cardMemory.IsFormatted)
                {
                    WalletTypeComboBox.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        WalletTypeComboBox.SelectedItem = null;
                    }));
                    return(PageToShow.NotInitialized);
                }

                if (!cardMemory.IsChecksumValid())
                {
                    return(PageToShow.CrcError);
                }

                // All seems good
                return(PageToShow.Ready);
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }

            return(PageToShow.Error);
        }
コード例 #3
0
ファイル: MainWindow.xaml.cs プロジェクト: thebChip/bchip
        private void ScanAndLoadConnectedCards(BChipSmartCard insertedCard = null)
        {
            using (var context = _contextFactory.Establish(SCardScope.System))
            {
                if (insertedCard == null)
                {
                    List <string> readerNames = GetReaderNames().ToList();
                    foreach (string curReader in readerNames)
                    {
                        try
                        {
                            using (var reader =
                                       context.ConnectReader(curReader, SCardShareMode.Shared, SCardProtocol.Any))
                            {
                                insertedCard = new BChipSmartCard(curReader)
                                {
                                    ATR           = reader.GetAttrib(SCardAttribute.AtrString),
                                    LastConnected = DateTime.Now,
                                    IsConnected   = true
                                };
                            }
                            if (insertedCard.Type() != CardType.BChip)
                            {
                                Logger.Log($"Card type was not an expected BChip and skipped ({insertedCard.Type()})");
                                continue;
                            }
                        }
                        // We try to check for an inserted card, eat errors.
                        catch (Exception ex)
                        {
                            if ((uint)ex.HResult == 0x80131500)
                            {
                                // ignore card removed
                                continue;
                            }
                        }
                    }
                }

                if (insertedCard == null)
                {
                    ChangePageUi(PageToShow.NoCard, null);
                }

                if (insertedCard != null)
                {
                    using (var isoReader = new IsoReader(context, insertedCard.ReaderName, SCardShareMode.Shared, SCardProtocol.Any))
                    {
                        try
                        {
                            if (insertedCard.AdpuInterface.RequiresInit)
                            {
                                Response initResponse = isoReader.Transmit(insertedCard.AdpuInterface.InitCard());
                                if (initResponse.SW1 != 0x90)
                                {
                                    Logger.Log("Error initializing smart card reader");
                                }
                            }

                            Response response = isoReader.Transmit(insertedCard.AdpuInterface.ReadCard());
                            if (response.HasData)
                            {
                                byte[] data     = response.GetData();
                                byte[] mlvi     = data.Skip(0x20).Take(BChipMemoryLayout_BCHIP.MLVI_MAX_DATA).ToArray();
                                byte[] carddata = data.Skip(0x28).ToArray();

                                // At this stage, the card has not yet been validated/parsed. Another thread should handle cleanup/de-dupes
                                insertedCard.SmartCardData = new BChipMemoryLayout_BCHIP(mlvi, carddata, PKStatus.NotValidated);
                                LoadedBChips = insertedCard;

                                ChangePageUi(AnalyzeCardData(insertedCard), insertedCard);
                            }
                            else
                            {
                                Logger.Log("Card added had no data to download");
                                // TODO: Add card data could not be loaded (not CRC error)
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.Log(ex);
                        };
                    }
                }
            }
        }