private void WriteDataToTag(Tag tag)
        {
            try
            {
                ndefHandler.SetKeys(AppSettings.Global.PubKey, AppSettings.Global.PrivKey);
                ndefHandler.SetExtraSignDataFromTag(tag);

                byte[]      rawNdefMsg = ndefHandler.GenerateRawNdefMessage(writeData);
                NdefMessage ndefMsg    = new NdefMessage(rawNdefMsg);

                bool?written = WriteNdef(tag, ndefMsg);

                if (written == false)
                {
                    written = FormatNdef(tag, ndefMsg);
                }

                if (written == true)
                {
                    ShowToast("Written " + ndefMsg.ByteArrayLength + " bytes of NDEF data.");
                    Finish();
                }
            }
            finally
            {
                ndefHandler.ClearKeys();
                ndefHandler.ClearExtraSignData();
            }
        }
Exemple #2
0
        public static void SetExtraSignDataFromTag(this NdefHandler handler, Tag tag)
        {
            if (tag == null)
            {
                handler.ClearExtraSignData();
                return;
            }

            byte[]   uid   = tag.GetId();
            string[] techs = tag.GetTechList();

            byte[] nuid = new byte[uid.Length + 1];
            Buffer.BlockCopy(uid, 0, nuid, 0, uid.Length);

            if (techs.Contains(Java.Lang.Class.FromType(typeof(MifareUltralight)).CanonicalName))
            {
                nuid[uid.Length] = 0xAA;
            }
            else if (techs.Contains(Java.Lang.Class.FromType(typeof(MifareClassic)).CanonicalName))
            {
                nuid[uid.Length] = 0xBB;
            }
            else
            {
                nuid = uid;
            }

            handler.SetExtraSignData(nuid);
        }
Exemple #3
0
        private byte[] ProcessUpdate(byte[] apdu)
        {
            int address = ((apdu[2] & 0xFF) << 8) | (apdu[3] & 0xFF);

            if (address < 0 || address >= 1024)
            {
                return(WRONG_PARAMETERS);
            }

            if (address != 0)
            {
                return(WRONG_PARAMETERS);
            }

            int length = apdu[4] & 0xFF;

            if (apdu.Length < length + 5)
            {
                return(STATUS_FAILED);
            }

            if (dataToPublish == null || confirmed)
            {
                return(FILE_NOT_FOUND);
            }

            byte[] challenge = new byte[length];
            Buffer.BlockCopy(apdu, 5, challenge, 0, length);

            try
            {
                ndefHandler.SetKeys(AppSettings.Global.PubKey, AppSettings.Global.PrivKey);
                ndefHandler.SetExtraSignData(challenge);
                ndefHandler.KeepEmptyFields = true;

                ndefData = ndefHandler.GenerateRawNdefMessage(dataToPublish);

                return(STATUS_SUCCESS);
            }
            catch (Exception e)
            {
                ShowToast("Failed preparing HCE data: " + e.Message);
                return(STATUS_FAILED);
            }
            finally
            {
                ndefHandler.ClearKeys();
                ndefHandler.ClearExtraSignData();
            }
        }
        private void ParseNdefIntent(Intent intent)
        {
            var rawMessages = intent.GetParcelableArrayExtra(NfcAdapter.ExtraNdefMessages);

            if (rawMessages == null || rawMessages.Length != 1)
            {
                ShowToast("Can't handle this tag.");
                BackToMain();
                return;
            }

            NdefMessage msg = rawMessages[0] as NdefMessage;
            Tag         tag = intent.GetParcelableExtra(NfcAdapter.ExtraTag) as Tag;

            ndefHandler.SetExtraSignDataFromTag(tag);
            ndefHandler.SetKeys(AppSettings.Global.PubKey, AppSettings.Global.PrivKey);

            try
            {
                tagData = ndefHandler.ParseNdefMessage(msg.ToByteArray());

                if (ndefHandler.HasPubKey() && (tagData.ContainsKey(NdefHandler.SIG_VALID_KEY) || AppSettings.Global.KioskMode))
                {
                    bool sigValid = Convert.ToBoolean(tagData.GetValueOrDefault(NdefHandler.SIG_VALID_KEY, bool.FalseString));
                    if (!sigValid)
                    {
                        ShowToast("Invalid Tag Signature!");
                        BackToMain();
                        return;
                    }
                }

                CheckForSettings();
                UpdateTextView();
            }
            catch (NdefHandlerException e)
            {
                ShowToast(e.Message);
                BackToMain();
            }
            finally
            {
                ndefHandler.ClearExtraSignData();
                ndefHandler.ClearKeys();
            }
        }