Beispiel #1
0
        private async Task <List <ApplicationInformation> > ListPseApplications(Application application)
        {
            var result = new List <ApplicationInformation>();
            var record = 1;

            while (true)
            {
                var res = await card.ReadAsync(application.Template.Sfi, record);

                if (res.Body is null || res.Body.Length < 1)
                {
                    break;
                }
                ;
                var tlv = new Tlv.Tlv(res.Body);
                if (!tlv.ContainsKey(0x70))
                {
                    throw new InvalidOperationException("Invalid Pse Record");
                }
                tlv = new Tlv.Tlv(tlv[0x70]);
                if (!tlv.ContainsKey(0x61))
                {
                    throw new InvalidOperationException("Invalid Pse Record");
                }
                var info = TlvSerializer.Deserialize <ApplicationInformation>(tlv, 0x61);
                result.Add(info);
                record++;
            }

            return(result);
        }
Beispiel #2
0
        public void TestTlvDecode()
        {
            var tlvBytes = Helpers.HexStringToByteArray("9f2701009f360200419f2608c74d18b08248fefc9f10120110201009248400000000000000000029ff");
            var tlv      = new Tlv.Tlv();

            tlv.Decode(tlvBytes);
            Assert.NotEmpty(tlv);
        }
        public void TestDeserialiser()
        {
            var tlvBytes = Helpers.HexStringToByteArray("6F1A840E315041592E5359532E4444463031A5088801015F2D02656E");
            var tlv      = new Tlv.Tlv(tlvBytes);

            var result = TlvSerializer.Deserialize <Application>(tlv, "0x6f");

            Assert.NotNull(result);
        }
Beispiel #4
0
        private Tlv.Tlv BuildDol(DataObjectList dol, Transaction trx)
        {
            var tlv = new Tlv.Tlv();

            foreach (var item in dol)
            {
                switch (item.Key)
                {
                case 0x9F02:
                    tlv[item.Key] = BerHelpers.EncodeUint((ulong)trx.Amount);
                    break;

                case 0x9F03:
                    tlv[item.Key] = BerHelpers.EncodeUint((ulong)trx.AdditionalAmount);
                    break;

                case 0x9F1A:
                    tlv[item.Key] = BerHelpers.EncodeUint((ulong)this.config.Terminal.CurrencyCode);
                    break;

                case 0x95:
                    tlv[item.Key] = BerHelpers.EncodeUint(this.tvr);
                    break;

                case 0x5F2A:
                    tlv[item.Key] = BerHelpers.EncodeUint((ulong)this.config.Terminal.CurrencyCode);
                    break;

                case 0x9A:
                    tlv[item.Key] = BerHelpers.EncodeUint((ulong)trx.Date.Ticks);
                    break;

                case 0x9C:
                    tlv[item.Key] = BerHelpers.EncodeUint((ulong)trx.Type);
                    break;

                case 0x9F37:
                    tlv[item.Key] = GenerateRandomNumber(item.Value);
                    break;

                case 0x9F35:
                    tlv[item.Key] = BerHelpers.EncodeUint((ulong)this.config.Terminal.Type);
                    break;

                case 0x9F45:
                    tlv[item.Key] = dataAuthenticationCode.ToArray();
                    break;

                case 0x9F34:
                    tlv[item.Key] = BerHelpers.EncodeUint(cvr);
                    break;

                default:
                    if (CardInformation.Raw.TryGetValue(item.Key, out byte[] value) || this.ProcessingOptions.Raw.TryGetValue(item.Key, out value))
Beispiel #5
0
        public void TestTlvEncode()
        {
            var tlvBytes = Helpers.HexStringToByteArray("9f2701009f360200419f2608c74d18b08248fefc9f10120110201009248400000000000000000029ff");
            var tlv      = new Tlv.Tlv();

            tlv.Decode(tlvBytes);

            var encoded = tlv.Encode();
            var tlv2    = new Tlv.Tlv();

            tlv2.Decode(encoded);

            Assert.Equal(tlvBytes, encoded);
        }
Beispiel #6
0
        public async Task <ProcessingOptions> GetProcessingAsync(Tlv.Tlv pdol)
        {
            var pdolData = pdol.Encode();
            var res      = await SendApduAsync(new APDUCommand
            {
                Class = 0x80, Instruction = 0xA8, Data = pdolData
            });

            var body = new Tlv.Tlv(res.Body);

            if (body.ContainsKey(0x77))
            {
                return(TlvSerializer.Deserialize <ProcessingOptions>(body, 0x77));
            }

            if (!body.TryGetValue(0x80, out byte[] raw))
Beispiel #7
0
        private async void ProcessCardInformation()
        {
            var groupTlv = new Tlv.Tlv();

            foreach (var app in ProcessingOptions.ApplicationFileList)
            {
                var sdaCount = app.SdaCount;

                for (int i = app.Start; i <= app.End; i++)
                {
                    var record = await card.ReadAsync(app.Sfi, i);

                    var body = new Tlv.Tlv(record.Body);
                    if (!body.ContainsKey(0x70))
                    {
                        throw new InvalidOperationException("malformed application file");
                    }
                    if (sdaCount > 0)
                    {
                        if (app.Sfi <= 10)
                        {
                            sdaData.AddRange(body[0x70]);
                        }
                        else
                        {
                            sdaData.AddRange(record.Body);
                        }
                        sdaCount--;
                    }

                    var template = new Tlv.Tlv(body[0x70]);
                    groupTlv.CopyFrom(template);
                }
            }
            CardInformation = TlvSerializer.Deserialize <CardInformation>(groupTlv);
        }
Beispiel #8
0
        public async void SelectApplicationAsync(byte[] applicationName)
        {
            var pdol = new Tlv.Tlv();
            var app  = await card.SelectApplicationAsync(applicationName, true);

            if (app is null)
            {
                throw new ArgumentException("Application not found");
            }
            if (app.Template.ProcessingObjects != null)
            {
                pdol = BuildDol(app.Template.ProcessingObjects, null);
            }
            else
            {
                pdol[0x83] = new byte[] { };
            }

            var options = await this.card.GetProcessingAsync(pdol);

            this.Application  = app;
            ProcessingOptions = options;
            ProcessCardInformation();
        }
Beispiel #9
0
        public async Task <Application> SelectApplicationAsync(byte[] name, bool first)
        {
            var rsp = await SelectAsync(name, first);

            if (rsp.SW1 == 0x6A && rsp.SW2 == 0x82)
            {
                return(null);
            }
            if (rsp.SW1 != 0x90 && rsp.SW2 != 0x00)
            {
                return(null);
            }

            var tlv = new Tlv.Tlv(rsp.Body);

            try
            {
                return(TlvSerializer.Deserialize <Application>(tlv, 0x6f));
            }
            catch (Exception e)
            {
                throw new InvalidOperationException("Error decoding Tlv");
            }
        }