Ejemplo n.º 1
0
        private void SetConfiguration(KeePassRFIDConfig config)
        {
            cbReaderProvider.SelectedItem = config.ReaderProvider;
            cbReaderUnit.SelectedItem     = config.ReaderUnit;
            cbCardType.SelectedItem       = config.CardType;
            switch (config.KeyType)
            {
            case KeyType.NFC:
                rbtnNFC.Checked = true;
                break;

            case KeyType.SecureID:
                rbtnSecureID.Checked = true;
                break;

            case KeyType.OTP:
                rbtnOTP.Checked = true;
                break;

            default:
                rbtnCSN.Checked = true;
                break;
            }
            rbtnChallengeBlank.Checked = (config.Challenge == null);
            tbxChallenge.Text          = config.Challenge == null ? String.Empty : BitConverter.ToString(config.Challenge).Replace("-", String.Empty);
        }
Ejemplo n.º 2
0
        public KeePassRFIDConfig GetConfiguration()
        {
            KeePassRFIDConfig config = new KeePassRFIDConfig();

            config.ReaderProvider = (cbReaderProvider.SelectedIndex > -1) ? cbReaderProvider.SelectedItem.ToString() : String.Empty;
            config.ReaderUnit     = (cbReaderUnit.SelectedIndex > -1) ? cbReaderUnit.SelectedItem.ToString() : String.Empty;
            config.KeyType        = rbtnNFC.Checked ? KeyType.NFC : KeyType.CSN;
            return(config);
        }
Ejemplo n.º 3
0
        private void OnOptions_Click(object sender, EventArgs e)
        {
            OptionsForm options = new OptionsForm();

            if (options.ShowDialog() == DialogResult.OK)
            {
                KeePassRFIDConfig config = options.GetConfiguration();
                KeePassRFIDConfig.SaveToCurrentSession(config);
            }
        }
Ejemplo n.º 4
0
        public override byte[] GetKey(KeyProviderQueryContext ctx)
        {
            byte[] key = null;

            try
            {
                KeePassRFIDConfig rfidConfig = KeePassRFIDConfig.GetFromCurrentSession();
                ChipAction(new Action <Chip>(delegate(Chip chip)
                {
                    if (rfidConfig.KeyType == KeyType.NFC)
                    {
                        // Only tag type 4 supported for now.
                        NFCTagCardService nfcsvc = chip.getService(CardServiceType.CST_NFC_TAG) as NFCTagCardService;
                        if (nfcsvc == null)
                        {
                            throw new KeePassRFIDException(Properties.Resources.UnsupportedNFCTag);
                        }

                        NdefMessage msg = nfcsvc.readNDEF();
                        if (msg.getRecordCount() > 0)
                        {
                            NdefRecordCollection records = msg.getRecords();
                            if (records.Count > 0)
                            {
                                // Always use first record only
                                NdefRecord record = records[0];
                                // Don't care about payload type, use whole payload as the key
                                UCharCollection payload = record.getPayload();
                                if (payload.Count > 0)
                                {
                                    key = payload.ToArray();
                                }
                            }
                        }
                    }
                    else
                    {
                        UCharCollection csn = chip.getChipIdentifier();
                        if (csn.Count > 0)
                        {
                            key = csn.ToArray();
                        }
                    }
                }), rfidConfig);
            }
            catch (KeePassRFIDException ex)
            {
                MessageBox.Show(ex.Message, Properties.Resources.PluginError, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            return(key);
        }
Ejemplo n.º 5
0
 private void OptionsForm_Load(object sender, EventArgs e)
 {
     try
     {
         RefreshReaderProviders();
         SetConfiguration(KeePassRFIDConfig.GetFromCurrentSession());
     }
     catch (COMException)
     {
         MessageBox.Show(Properties.Resources.RFIDInitError, Properties.Resources.PluginError, MessageBoxButtons.OK, MessageBoxIcon.Error);
         this.Close();
     }
 }
Ejemplo n.º 6
0
        private void SetConfiguration(KeePassRFIDConfig config)
        {
            cbReaderProvider.SelectedItem = config.ReaderProvider;
            cbReaderUnit.SelectedItem     = config.ReaderUnit;
            switch (config.KeyType)
            {
            case KeyType.NFC:
                rbtnNFC.Checked = true;
                break;

            default:
                rbtnCSN.Checked = true;
                break;
            }
        }
Ejemplo n.º 7
0
        public static void SaveToCurrentSession(KeePassRFIDConfig config)
        {
            XmlSerializer xser = new XmlSerializer(typeof(KeePassRFIDConfig));

            using (StringWriter sww = new StringWriter())
                using (XmlTextWriter writer = new XmlTextWriter(sww)
                {
                    Formatting = Formatting.Indented
                })
                {
                    xser.Serialize(writer, config);
                    string xml = sww.ToString();
                    File.WriteAllText(GetCurrentSessionConfigPath(true), xml);
                }
        }
Ejemplo n.º 8
0
        public static KeePassRFIDConfig GetFromCurrentSession()
        {
            KeePassRFIDConfig config  = new KeePassRFIDConfig();
            string            xmlfile = GetCurrentSessionConfigPath();

            if (File.Exists(xmlfile))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(KeePassRFIDConfig));
                using (TextReader reader = new StringReader(File.ReadAllText(xmlfile)))
                {
                    config = serializer.Deserialize(reader) as KeePassRFIDConfig;
                }
            }
            return(config);
        }
Ejemplo n.º 9
0
        public KeePassRFIDConfig GetConfiguration()
        {
            KeePassRFIDConfig config = new KeePassRFIDConfig();

            config.ReaderProvider = (cbReaderProvider.SelectedIndex > -1) ? cbReaderProvider.SelectedItem.ToString() : String.Empty;
            config.ReaderUnit     = (cbReaderUnit.SelectedIndex > -1) ? cbReaderUnit.SelectedItem.ToString() : String.Empty;
            config.CardType       = (cbCardType.SelectedIndex > -1) ? cbCardType.SelectedItem.ToString() : String.Empty;
            if (rbtnNFC.Checked)
            {
                config.KeyType = KeyType.NFC;
            }
            else if (rbtnSecureID.Checked)
            {
                config.KeyType = KeyType.SecureID;
            }
            else if (rbtnOTP.Checked)
            {
                config.KeyType = KeyType.OTP;
            }
            else
            {
                config.KeyType = KeyType.CSN;
            }
            if (rbtnChallengeBlank.Checked || String.IsNullOrEmpty(tbxChallenge.Text))
            {
                config.Challenge = null;
            }
            else
            {
                byte[] challenge = new byte[tbxChallenge.Text.Length / 2];
                for (int i = 0; i < tbxChallenge.Text.Length; i += 2)
                {
                    challenge[i / 2] = Convert.ToByte(tbxChallenge.Text.Substring(i, 2), 16);
                }
                config.Challenge = challenge;
            }
            return(config);
        }
Ejemplo n.º 10
0
        public static void ChipAction(Action <Chip> action, KeePassRFIDConfig rfidConfig)
        {
            if (rfidConfig == null)
            {
                rfidConfig = KeePassRFIDConfig.GetFromCurrentSession();
            }

            ReaderProvider readerProvider;

            try
            {
                LibraryManager libmgt = LibraryManager.getInstance();

                readerProvider = libmgt.getReaderProvider(!String.IsNullOrEmpty(rfidConfig.ReaderProvider) ? rfidConfig.ReaderProvider : "PCSC");
                if (readerProvider == null)
                {
                    throw new KeePassRFIDException(Properties.Resources.RFIDConfigurationError);
                }
            }
            catch (LibLogicalAccessException ex)
            {
                throw new KeePassRFIDException(Properties.Resources.RFIDInitError, ex);
            }

            ReaderUnit readerUnit = null;

            if (String.IsNullOrEmpty(rfidConfig.ReaderUnit))
            {
                readerUnit = readerProvider.createReaderUnit();
                if (readerUnit == null)
                {
                    throw new KeePassRFIDException(Properties.Resources.RFIDReaderInitError);
                }
            }
            else
            {
                var readers = readerProvider.getReaderList();
                foreach (ReaderUnit reader in readers)
                {
                    if (reader.getName() == rfidConfig.ReaderUnit)
                    {
                        readerUnit = reader;
                        break;
                    }
                }
            }

            if (readerUnit == null)
            {
                throw new KeePassRFIDException(Properties.Resources.RFIDReaderNotFound);
            }

            try
            {
                if (readerUnit.connectToReader())
                {
                    // Optionally force card type
                    if (!string.IsNullOrEmpty(rfidConfig.CardType))
                    {
                        readerUnit.setCardType(rfidConfig.CardType);
                    }

                    // Don't wait for chip insertion, it must already be on the reader
                    if (readerUnit.waitInsertion(1))
                    {
                        if (readerUnit.connect())
                        {
                            Chip chip = readerUnit.getSingleChip();
                            if (chip != null)
                            {
                                action(chip);
                            }
                            else
                            {
                                ShowError(Properties.Resources.NoChipError);
                            }

                            readerUnit.disconnect();
                        }
                        else
                        {
                            ShowError(Properties.Resources.CardConnectionFailed);
                        }
                        readerUnit.waitRemoval(1);
                    }
                    else
                    {
                        ShowError(Properties.Resources.NoCardInserted);
                    }

                    readerUnit.disconnectFromReader();
                }
                else
                {
                    ShowError(Properties.Resources.ReaderConnectionFailed);
                }
            }
            catch (LibLogicalAccessException ex)
            {
                ShowError(ex.Message);
            }

            GC.KeepAlive(readerUnit);
            GC.KeepAlive(readerProvider);
        }