Exemplo n.º 1
0
        private static TLVList DeserList(tlvXMLType[] serXML)
        {
            TLVList result = new TLVList();

            serXML.ToList().ForEach((x) =>
            {
                TLV tlvToAdd = TLV.Create(x.tag.Name);
                //x.tag.Description = TLVMetaDataSourceSingleton.Instance.DataSource.GetName(tlvToAdd.Tag.TagLable);
                result.AddToList(tlvToAdd);
                if (x.children != null)
                {
                    tlvToAdd.Children.AddListToList(DeserList(x.children));
                }
                else
                {
                    if (x.value != null)
                    {
                        if (x.value.Formatting == FormattingEnum.ASCII)
                        {
                            tlvToAdd.Value = Formatting.ASCIIStringToByteArray(x.value.Value);
                        }
                        else
                        {
                            tlvToAdd.Value = Formatting.HexStringToByteArray(x.value.Value);
                        }
                    }
                    if (x.valueC9 != null)
                    {
                    }
                }
            });
            return(result);
        }
Exemplo n.º 2
0
        public static QRDE Create(EMVQRTagMeta tagMeta, string value, QRDE parent = null)
        {
            if (!tagMeta.DataFormatter.Validate(Formatting.ASCIIStringToByteArray(value)))
            {
                throw new Exception("Invalid data value for tag:" + tagMeta.Name);
            }

            QRDE qde = new QRDE()
            {
                Tag = tagMeta.Tag, Length = value.Length, Value = value
            };

            qde.IsTemplate = QRMetaDataSourceSingleton.Instance.DataSource.IsTemplate(qde.Tag, qde.TagParent);

            if (parent != null)
            {
                if (!parent.IsTemplate)
                {
                    throw new Exception("Cannot add child to non template QDE");
                }

                qde.TagParent = parent.Tag;
                parent.Children.AddToList(qde);
            }
            else
            {
                qde.TagParent = TagId.None;
            }

            return(qde);
        }
Exemplo n.º 3
0
        public EMVSelectPPSERequest(string fileName) : base(EMVInstructionEnum.Select, null,
                                                            (byte)SelectPPSERequest_P1.SelectByName,
                                                            (byte)SelectPPSERequest_P2.FirstOrOnlyOccurrence)
        {
            this.fileName    = fileName;
            ApduResponseType = typeof(EMVSelectPPSEResponse);
            CommandData      = Formatting.ASCIIStringToByteArray(fileName);

            Logger.Log(ToPrintString());
        }
Exemplo n.º 4
0
        public TransactionBase(SPDHTransactionTypeEnum type, string name)
        {
            Header          = new Header();
            MandatoryFidIds = new List <FIDMeta>();
            Fids            = new List <FIDBase>();
            Type            = type;
            Name            = name;

            Header.SetValue(HeaderEntryEnum.DeviceType, Formatting.ASCIIStringToByteArray(SPDHConstants.DialOrLeasedLineTerminalOrNetwork));
            Header.SetValue(HeaderEntryEnum.TransmissionNumber, Formatting.ASCIIStringToByteArray(SPDHConstants.TransmissionNumberNotChecked));
        }
Exemplo n.º 5
0
        public TransactionBase(byte[] raw, ref int pos, SPDHTransactionTypeEnum type)
        {
            Header          = new Header();
            MandatoryFidIds = new List <FIDMeta>();
            Fids            = new List <FIDBase>();
            Type            = type;
            Name            = type.ToString();
            pos             = Deserialize(raw, pos);

            Header.SetValue(HeaderEntryEnum.DeviceType, Formatting.ASCIIStringToByteArray(SPDHConstants.DialOrLeasedLineTerminalOrNetwork));
            Header.SetValue(HeaderEntryEnum.TransmissionNumber, Formatting.ASCIIStringToByteArray(SPDHConstants.TransmissionNumberNotChecked));
        }
Exemplo n.º 6
0
        public void SetHeaderValues(string transactionType, SPDHMessageType messageType, SPDHMessageSubType subType, DateTime datetime, string terminalID, string employeeId)
        {
            Header.SetValue(HeaderEntryEnum.ProcessingFlag1, Formatting.HexStringToByteArray(""));
            Header.SetValue(HeaderEntryEnum.ProcessingFlag2, Formatting.ASCIIStringToByteArray("5"));
            Header.SetValue(HeaderEntryEnum.ProcessingFlag3, Formatting.HexStringToByteArray(""));
            Header.SetValue(HeaderEntryEnum.ResponseCode, Formatting.HexStringToByteArray(""));

            Header.SetValue(HeaderEntryEnum.TransactionCode, Formatting.ASCIIStringToByteArray(transactionType));
            Header.SetValue(HeaderEntryEnum.CurrentDate, Formatting.ASCIIStringToByteArray(Formatting.ConvertDateToString(datetime, "yyMMdd")));
            Header.SetValue(HeaderEntryEnum.CurrentTime, Formatting.ASCIIStringToByteArray(Formatting.ConvertDateToString(datetime, "HHmmss")));
            Header.SetValue(HeaderEntryEnum.MessageType, new byte[] { (byte)messageType });
            Header.SetValue(HeaderEntryEnum.MessageSubtype, new byte[] { (byte)subType });
            Header.SetValue(HeaderEntryEnum.TerminalID, Formatting.ASCIIStringToByteArray(terminalID));
            Header.SetValue(HeaderEntryEnum.EmployeeID, Formatting.ASCIIStringToByteArray(employeeId));
        }
Exemplo n.º 7
0
            public override byte[] Serialize()
            {
                FormatCode     = 0x42;
                FieldSeperator = 0x5E;
                string output = Formatting.ByteArrayToASCIIString(new byte[] { FormatCode })
                                + PAN +
                                Formatting.ByteArrayToASCIIString(new byte[] { FieldSeperator }) +
                                Name +
                                Formatting.ByteArrayToASCIIString(new byte[] { FieldSeperator }) +
                                ExpirationDate +
                                ServiceCode +
                                Formatting.ByteArrayToASCIIString(DiscretionaryData);

                Value = Formatting.ASCIIStringToByteArray(output);
                return(base.Serialize());
            }
Exemplo n.º 8
0
            public override int Deserialize(byte[] rawTlv, int pos)
            {
                FieldSeperator = 0x5E;

                pos = base.Deserialize(rawTlv, pos);

                string input = Formatting.ByteArrayToASCIIString(Value);

                string[] inputs = input.Split(new string[] { Formatting.ByteArrayToASCIIString(new byte[] { FieldSeperator }) }, StringSplitOptions.None);
                PAN               = inputs[0].Substring(1);
                Name              = inputs[1];
                ExpirationDate    = inputs[2].Substring(0, 4);
                ServiceCode       = inputs[2].Substring(4, 3);
                DiscretionaryData = Formatting.ASCIIStringToByteArray(inputs[2].Substring(7, inputs[2].Length - 7));

                return(pos);
            }
            public override int Deserialize(byte[] rawTlv, int pos)
            {
                pos = base.Deserialize(rawTlv, pos);

                string input = Formatting.ByteArrayToHexString(Value);

                CountryCode      = input.Substring(0, 2);
                UniqueIdentifier = Formatting.HexStringToByteArray(input.Substring(2, 2));
                if (!Formatting.GetBitPosition(UniqueIdentifier[0], 8))
                {
                    DeviceType      = Formatting.ASCIIStringToByteArray(input.Substring(4, 2));
                    ProprietaryData = input.Substring(6);
                }
                else
                {
                    ProprietaryData = input.Substring(4);
                }

                return(pos);
            }
Exemplo n.º 10
0
        /*
         * 14.6
         */
        private static SignalsEnum EntryPointRA(Kernel2Database database, CardResponse cardResponse, KernelQ qManager, CardQ cardQManager, TornTransactionLogManager tornTransactionLogManager, Stopwatch sw, PublicKeyCertificateManager publicKeyCertificateManager)
        {
            #region 14.9
            if (!cardResponse.ApduResponse.Succeeded)
            #endregion
            {
                #region 14.10
                CommonRoutines.UpdateErrorIndication(database, cardResponse, L1Enum.NOT_SET, L2Enum.STATUS_BYTES, L3Enum.NOT_SET);
                return(DoInvalidResponse(database, qManager));

                #endregion
            }

            #region 14.11
            bool parsingResult = false;
            if (cardResponse.ApduResponse.ResponseData.Length > 0 && cardResponse.ApduResponse.ResponseData[0] == 0x77)
            {
                EMVComputeCryptographicChecksumResponse response = cardResponse.ApduResponse as EMVComputeCryptographicChecksumResponse;
                parsingResult = database.ParseAndStoreCardResponse(response.ResponseData);
            }
            else
            {
                parsingResult = false;
            }
            #endregion

            #region 14.12
            if (!parsingResult)
            #endregion
            {
                #region 14.13
                CommonRoutines.UpdateErrorIndication(database, cardResponse, L1Enum.NOT_SET, L2Enum.PARSING_ERROR, L3Enum.NOT_SET);
                return(DoInvalidResponse(database, qManager));

                #endregion
            }

            #region 14.12.1
            CommonRoutines.PostUIOnly(database, qManager, KernelMessageidentifierEnum.CLEAR_DISPLAY, KernelStatusEnum.CARD_READ_SUCCESSFULLY, true);
            #endregion

            #region 14.14
            if (database.IsEmpty(EMVTagsEnum.APPLICATION_TRANSACTION_COUNTER_ATC_9F36_KRN.Tag) ||
                database.IsEmpty(EMVTagsEnum.POS_CARDHOLDER_INTERACTION_INFORMATION_DF4B_KRN2.Tag))
            #endregion
            {
                #region 14.17
                CommonRoutines.UpdateErrorIndication(database, cardResponse, L1Enum.NOT_SET, L2Enum.CARD_DATA_MISSING, L3Enum.NOT_SET);
                return(DoInvalidResponse(database, qManager));

                #endregion
            }

            long nUN = 0;
            #region 14.15
            if (database.IsNotEmpty(EMVTagsEnum.CVC3_TRACK2_9F61_KRN2.Tag))
            #endregion
            {
                #region 14.16
                if (database.IsNotEmpty(EMVTagsEnum.TRACK_1_DATA_56_KRN2.Tag) &&
                    (database.IsNotPresent(EMVTagsEnum.CVC3_TRACK1_9F60_KRN2.Tag) ||
                     database.IsEmpty(EMVTagsEnum.CVC3_TRACK1_9F60_KRN2.Tag)))
                #endregion
                {
                    #region 14.17
                    CommonRoutines.UpdateErrorIndication(database, cardResponse, L1Enum.NOT_SET, L2Enum.CARD_DATA_MISSING, L3Enum.NOT_SET);
                    return(DoInvalidResponse(database, qManager));

                    #endregion
                }

                #region 14.20
                byte pcii = database.Get(EMVTagsEnum.POS_CARDHOLDER_INTERACTION_INFORMATION_DF4B_KRN2).Value[1];
                #endregion
                if ((pcii & 0x10) == 0x10)   //OD-CVM verification successful
                {
                    #region 14.24
                    nUN = (nUN + 5) % 10;
                    #endregion
                }
                else
                {
                    #region 14.21
                    long aa   = Formatting.BcdToLong(database.Get(EMVTagsEnum.AMOUNT_AUTHORISED_NUMERIC_9F02_KRN).Value);
                    long rctl = database.ReaderContactlessTransactionLismit;
                    if (aa > rctl)
                    #endregion
                    {
                        #region 14.21.1
                        CommonRoutines.UpdateErrorIndication(database, cardResponse, L1Enum.NOT_SET, L2Enum.CARD_DATA_ERROR, L3Enum.NOT_SET);
                        return(DoInvalidResponse(database, qManager));

                        #endregion
                    }
                    else
                    {
                        #region 14.25
                        nUN = (int)Formatting.ConvertToInt32(database.Get(EMVTagsEnum.UNPREDICTABLE_NUMBER_9F37_KRN).Value);
                        #endregion
                    }
                }

                #region 14.25.1
                database.FailedMSCntr = 0;
                #endregion

                #region 14.26
                TRACK_2_DATA_9F6B_KRN2 t2d = new TRACK_2_DATA_9F6B_KRN2(database);
                ushort t2 = Formatting.ConvertToInt16(database.Get(EMVTagsEnum.NATC_TRACK2_9F67_KRN2).Value);

                TLV       PCVC3_TRACK2_9F65_KRN2       = database.Get(EMVTagsEnum.PCVC3_TRACK2_9F65_KRN2);
                MagBitmap bitmapPCVC3_TRACK2_9F65_KRN2 = new MagBitmap(PCVC3_TRACK2_9F65_KRN2.Value);

                TLV    CVC3_TRACK2_9F61_KRN2 = database.Get(EMVTagsEnum.CVC3_TRACK2_9F61_KRN2);
                ushort cvc3T2AsShort         = Formatting.ConvertToInt16(CVC3_TRACK2_9F61_KRN2.Value.Reverse().ToArray());

                string q2LeastSigDigits = Convert.ToString(cvc3T2AsShort);
                q2LeastSigDigits            = q2LeastSigDigits.Substring(q2LeastSigDigits.Length - bitmapPCVC3_TRACK2_9F65_KRN2.NonZeroCount);
                t2d.Value.DiscretionaryData = Formatting.StringToBcd(bitmapPCVC3_TRACK2_9F65_KRN2.ReplaceValues(Formatting.BcdToString(t2d.Value.DiscretionaryData), q2LeastSigDigits, bitmapPCVC3_TRACK2_9F65_KRN2.NonZeroCount, true), false);

                TLV       PUNATC_TRACK2_9F66_KRN2       = database.Get(EMVTagsEnum.PUNATC_TRACK2_9F66_KRN2);
                MagBitmap bitmapPUNATC_TRACK2_9F66_KRN2 = new MagBitmap(PUNATC_TRACK2_9F66_KRN2.Value);
                uint      unpredInt    = Formatting.ConvertToInt32(database.Get(EMVTagsEnum.UNPREDICTABLE_NUMBER_NUMERIC_9F6A_KRN2).Value.Reverse().ToArray());
                string    unpredString = Convert.ToString(unpredInt);
                unpredString = unpredString.Substring(unpredString.Length - database.NUN);
                t2d.Value.DiscretionaryData = Formatting.StringToBcd(bitmapPUNATC_TRACK2_9F66_KRN2.ReplaceValues(Formatting.BcdToString(t2d.Value.DiscretionaryData), unpredString, database.NUN, true), false);

                if (t2 != 0)
                {
                    TLV    APPLICATION_TRANSACTION_COUNTER_ATC_9F36_KRN = database.Get(EMVTagsEnum.APPLICATION_TRANSACTION_COUNTER_ATC_9F36_KRN);
                    uint   atcAsShort = Formatting.ConvertToInt32(APPLICATION_TRANSACTION_COUNTER_ATC_9F36_KRN.Value.Reverse().ToArray());
                    string atcAsShortLeastSigDigits = Convert.ToString(atcAsShort);
                    atcAsShortLeastSigDigits    = atcAsShortLeastSigDigits.PadLeft(t2, '0').Substring(atcAsShortLeastSigDigits.Length - t2);
                    t2d.Value.DiscretionaryData = Formatting.StringToBcd(bitmapPUNATC_TRACK2_9F66_KRN2.ReplaceValues(Formatting.BcdToString(t2d.Value.DiscretionaryData), atcAsShortLeastSigDigits, t2, false), false);
                }
                #endregion

                #region 14.27
                StringBuilder dd = new StringBuilder(Formatting.BcdToString(t2d.Value.DiscretionaryData));
                dd[dd.Length - 1]           = Convert.ToString(nUN)[0];
                t2d.Value.DiscretionaryData = Formatting.StringToBcd(dd.ToString(), false);
                #endregion

                t2d.Serialize(); //reserialize in case the length of discretionary data changed
                t2d.UpdateDB();

                #region 14.28
                if (database.IsNotEmpty(EMVTagsEnum.TRACK_1_DATA_56_KRN2.Tag))
                #endregion
                {
                    #region 14.29
                    TRACK_1_DATA_56_KRN2 t1d = new TRACK_1_DATA_56_KRN2(database);
                    ushort t1 = Formatting.ConvertToInt16(database.Get(EMVTagsEnum.NATC_TRACK1_9F64_KRN2).Value);

                    TLV       PCVC3_TRACK1_9F62_KRN2       = database.Get(EMVTagsEnum.PCVC3_TRACK1_9F62_KRN2);
                    MagBitmap bitmapPCVC3_TRACK1_9F62_KRN2 = new MagBitmap(PCVC3_TRACK1_9F62_KRN2.Value);

                    TLV    CVC3_TRACK1_9F60_KRN2 = database.Get(EMVTagsEnum.CVC3_TRACK1_9F60_KRN2);
                    ushort cvc3T1AsShort         = Formatting.ConvertToInt16(CVC3_TRACK1_9F60_KRN2.Value.Reverse().ToArray());

                    string q1LeastSigDigits = Convert.ToString(cvc3T1AsShort);
                    q1LeastSigDigits            = q1LeastSigDigits.Substring(q1LeastSigDigits.Length - bitmapPCVC3_TRACK1_9F62_KRN2.NonZeroCount);
                    t1d.Value.DiscretionaryData = Formatting.ASCIIStringToByteArray(bitmapPCVC3_TRACK1_9F62_KRN2.ReplaceValues(Formatting.ByteArrayToASCIIString(t1d.Value.DiscretionaryData), q1LeastSigDigits, bitmapPCVC3_TRACK1_9F62_KRN2.NonZeroCount, true));

                    TLV       PUNATC_TRACK1_9F63_KRN2       = database.Get(EMVTagsEnum.PUNATC_TRACK1_9F63_KRN2);
                    MagBitmap bitmapPUNATC_TRACK1_9F63_KRN2 = new MagBitmap(PUNATC_TRACK1_9F63_KRN2.Value);
                    t1d.Value.DiscretionaryData = Formatting.ASCIIStringToByteArray(bitmapPUNATC_TRACK1_9F63_KRN2.ReplaceValues(Formatting.ByteArrayToASCIIString(t1d.Value.DiscretionaryData), unpredString, database.NUN, true));

                    if (t2 != 0)
                    {
                        TLV    APPLICATION_TRANSACTION_COUNTER_ATC_9F36_KRN = database.Get(EMVTagsEnum.APPLICATION_TRANSACTION_COUNTER_ATC_9F36_KRN);
                        uint   atcAsShort = Formatting.ConvertToInt32(APPLICATION_TRANSACTION_COUNTER_ATC_9F36_KRN.Value.Reverse().ToArray());
                        string atcAsShortLeastSigDigits = Convert.ToString(atcAsShort);
                        atcAsShortLeastSigDigits    = atcAsShortLeastSigDigits.PadLeft(t2, '0').Substring(atcAsShortLeastSigDigits.Length - t1);
                        t2d.Value.DiscretionaryData = Formatting.ASCIIStringToByteArray(bitmapPUNATC_TRACK1_9F63_KRN2.ReplaceValues(Formatting.ByteArrayToASCIIString(t1d.Value.DiscretionaryData), atcAsShortLeastSigDigits, t1, false));
                    }
                    #endregion

                    #region 14.30
                    StringBuilder dd1 = new StringBuilder(Formatting.ByteArrayToASCIIString(t1d.Value.DiscretionaryData));
                    dd1[dd1.Length - 1]         = Convert.ToString(nUN)[0];
                    t1d.Value.DiscretionaryData = Formatting.ASCIIStringToByteArray(dd1.ToString());
                    #endregion

                    t1d.Serialize(); //reserialize in case the length of discretionary data changed
                    t1d.UpdateDB();
                }

                #region 14.32
                Kernel2OutcomeStatusEnum k2OutcomeStatus = Kernel2OutcomeStatusEnum.ONLINE_REQUEST;
                KernelCVMEnum            cvmEnum         = KernelCVMEnum.N_A;
                bool receipt = false;

                pcii = database.Get(EMVTagsEnum.POS_CARDHOLDER_INTERACTION_INFORMATION_DF4B_KRN2).Value[1];
                if ((pcii & 0x10) == 0x10)   //OD-CVM verification successful
                #endregion
                {
                    #region 14.34
                    cvmEnum = KernelCVMEnum.CONFIRMATION_CODE_VERIFIED;
                    long aa   = Formatting.BcdToLong(database.Get(EMVTagsEnum.AMOUNT_AUTHORISED_NUMERIC_9F02_KRN).Value);
                    long rctl = database.ReaderContactlessTransactionLismit;
                    if (aa > rctl)
                    {
                        receipt = true;
                    }
                    #endregion
                }
                else
                {
                    #region 14.33
                    cvmEnum = KernelCVMEnum.NO_CVM;
                    #endregion
                }
                CommonRoutines.CreateMSDiscretionaryDataRecord(database);
                CommonRoutines.CreateMSDataRecord(database);
                return(CommonRoutines.PostOutcomeOnly(database, qManager, k2OutcomeStatus, cvmEnum, receipt));
            }
            else
            {
                #region 14.19.1
                if (((int)Formatting.ConvertToInt32(database.Get(EMVTagsEnum.POS_CARDHOLDER_INTERACTION_INFORMATION_DF4B_KRN2).Value) & 0x00030F) != 0x000000)
                #endregion
                {
                    KernelMessageidentifierEnum k2MessageIdentifier = KernelMessageidentifierEnum.DECLINED;
                    KernelStatusEnum            k2Status            = KernelStatusEnum.READY_TO_READ;
                    byte[] holdTime = new byte[] { 0x00, 0x00, 0x00 };

                    #region 14.22
                    PHONE_MESSAGE_TABLE_DF8131_KRN2 pmt = (PHONE_MESSAGE_TABLE_DF8131_KRN2)database.GetDefault(EMVTagsEnum.PHONE_MESSAGE_TABLE_DF8131_KRN2);
                    foreach (PhoneMessageTableEntry_DF8131 entry in pmt.Value.Entries)
                    {
                        int pcii     = (int)Formatting.ConvertToInt32(database.Get(EMVTagsEnum.POS_CARDHOLDER_INTERACTION_INFORMATION_DF4B_KRN2).Value);
                        int pciMask  = (int)Formatting.ConvertToInt32(entry.PCIIMask);
                        int pciValue = (int)Formatting.ConvertToInt32(entry.PCIIValue);
                        if ((pciMask & pcii) == pciValue)
                        {
                            k2MessageIdentifier = entry.MessageIdentifier;
                            k2Status            = entry.Status;
                            holdTime            = database.GetDefault(EMVTagsEnum.MESSAGE_HOLD_TIME_DF812D_KRN2).Value;
                            break;
                        }
                    }
                    #endregion

                    #region 14.21.1
                    int waitTime = ((2 ^ database.FailedMSCntr) * 300);
                    Task.Delay(TimeSpan.FromMilliseconds(waitTime)).Wait();
                    #endregion

                    #region 14.21.2
                    database.FailedMSCntr = Math.Min(database.FailedMSCntr + 1, 5);
                    #endregion

                    #region 14.23
                    CommonRoutines.CreateMSDiscretionaryDataRecord(database);
                    CommonRoutines.CreateMSDataRecord(database);

                    return(CommonRoutines.PostOutcome(database, qManager,
                                                      k2MessageIdentifier,
                                                      k2Status,
                                                      holdTime,
                                                      Kernel2OutcomeStatusEnum.END_APPLICATION,
                                                      Kernel2StartEnum.B,
                                                      false,
                                                      KernelMessageidentifierEnum.N_A,
                                                      L1Enum.NOT_SET,
                                                      null,
                                                      L2Enum.NOT_SET,
                                                      L3Enum.NOT_SET));

                    #endregion
                }
                else
                {
                    #region 14.19.2.1
                    int waitTime = ((2 ^ database.FailedMSCntr) * 300);
                    Task.Delay(TimeSpan.FromMilliseconds(waitTime)).Wait();
                    #endregion
                    #region 14.19.2.2
                    database.FailedMSCntr = Math.Min(database.FailedMSCntr + 1, 5);
                    #endregion

                    #region 14.19.3
                    CommonRoutines.CreateMSDiscretionaryDataRecord(database);
                    CommonRoutines.CreateMSDataRecord(database);

                    return(CommonRoutines.PostOutcome(database, qManager,
                                                      KernelMessageidentifierEnum.DECLINED,
                                                      KernelStatusEnum.NOT_READY,
                                                      database.GetDefault(EMVTagsEnum.MESSAGE_HOLD_TIME_DF812D_KRN2).Value,
                                                      Kernel2OutcomeStatusEnum.END_APPLICATION,
                                                      Kernel2StartEnum.N_A,
                                                      true,
                                                      KernelMessageidentifierEnum.N_A,
                                                      L1Enum.NOT_SET,
                                                      null,
                                                      L2Enum.NOT_SET,
                                                      L3Enum.NOT_SET));

                    #endregion
                }
            }
        }
Exemplo n.º 11
0
        public void DoPinReponse(string pin)
        {
            if (kernel == null)
            {
                throw new EMVProtocolException("DoPinReponse called without kernel being activated");
            }
            TLVList requestInput = new TLVList();

            requestInput.AddToList(TLV.Create(EMVTagsEnum.TRANSACTION_PERSONAL_IDENTIFICATION_NUMBER_PIN_DATA_99_KRN.Tag, Formatting.ASCIIStringToByteArray(pin)));
            KernelRequest request = new KernelRequest(KernelTerminalReaderServiceRequestEnum.PIN, requestInput);

            kernel.KernelQ.EnqueueToInput(request);
        }
Exemplo n.º 12
0
        public ApproverResponseBase DoAuth(ApproverRequestBase requestIn)
        {
            bool isMagStripe;
            EMVApproverRequest request = ((EMVApproverRequest)requestIn);
            TLV cryptogram             = request.EMV_Data.Children.Get(EMVTagsEnum.CRYPTOGRAM_INFORMATION_DATA_9F27_KRN.Tag);

            if (cryptogram != null)
            {
                isMagStripe = false;
            }
            else
            {
                isMagStripe = true;
            }

            TransactionTypeEnum tt = (TransactionTypeEnum)Formatting.GetEnum(typeof(TransactionTypeEnum), request.EMV_Data.Children.Get(EMVTagsEnum.TRANSACTION_TYPE_9C_KRN.Tag).Value[0]);

            TransactionBase np;

            switch (tt)
            {
            case TransactionTypeEnum.PurchaseGoodsAndServices:
                np = new F00_NormalPurchase();
                break;

            case TransactionTypeEnum.PurchaseWithCashback:
                np = new F08_PurchaseWithCashBack();
                break;

            case TransactionTypeEnum.Refund:
                np = new F12_MerchandiseReturnAdjustment();
                break;

            default:
                throw new Exception("Unimplemented TransactionTypeEnum:" + tt);
            }

            np.SetHeaderValues(SPDHTransactionCode.NormalPurchase, SPDHMessageType.FinancialTransaction, SPDHMessageSubType.Online, DateTime.Now, "300047", "");

            np.Fids.Add(new FID_B_Amount1(Formatting.ConvertToHexAscii(request.EMV_Data.Children.Get(EMVTagsEnum.AMOUNT_AUTHORISED_NUMERIC_9F02_KRN.Tag).Value)));

            FIDBase fb_6 = new FIDBase('6', ' ', new byte[0]);

            np.Fids.Add(fb_6);

            if (tt == TransactionTypeEnum.PurchaseWithCashback || tt == TransactionTypeEnum.Refund)
            {
                np.Fids.Add(new FID_C_Amount2(Formatting.ConvertToHexAscii(request.EMV_Data.Children.Get(EMVTagsEnum.AMOUNT_OTHER_NUMERIC_9F03_KRN.Tag).Value)));
            }
            if (isMagStripe)
            {
                np.Fids.Add(new FID_q_Track2_Customer(Formatting.ConvertToHexAscii(request.EMV_Data.Children.Get(EMVTagsEnum.TRACK_2_DATA_9F6B_KRN2.Tag).Value)));
                fb_6.Children.Add(new FID_6_I_TransactionCurrencyCode(Formatting.ConvertToHexAscii(request.EMV_Data.Children.Get(EMVTagsEnum.TRANSACTION_CURRENCY_CODE_5F2A_KRN.Tag).Value)));
                fb_6.Children.Add(new FID_6_E_POSEntryMode(Formatting.ConvertToHexAscii(new byte[] { 0x91 })));
            }
            else
            {
                np.Fids.Add(new FID_q_Track2_Customer(Formatting.ConvertToHexAscii(request.EMV_Data.Children.Get(EMVTagsEnum.TRACK_2_EQUIVALENT_DATA_57_KRN.Tag).Value)));
                fb_6.Children.Add(new FID_6_I_TransactionCurrencyCode(Formatting.ConvertToHexAscii(request.EMV_Data.Children.Get(EMVTagsEnum.TRANSACTION_CURRENCY_CODE_5F2A_KRN.Tag).Value)));
                //fb_6.Children.Add(new FID_6_E_POSEntryMode(Formatting.ConvertToHexAscii(new byte[] { 0x07 })));
                fb_6.Children.Add(new FID_6_E_POSEntryMode(Formatting.ASCIIStringToByteArray("051")));
            }

            if (!isMagStripe)
            {
                List <byte[]> fidBytes        = new List <byte[]>();
                byte[]        smartCardScheme = new byte[] { 0x30, 0x31 };

                fidBytes.Add(smartCardScheme);
                fidBytes.Add(Formatting.ConvertToHexAscii(request.EMV_Data.Children.Get(EMVTagsEnum.CRYPTOGRAM_INFORMATION_DATA_9F27_KRN.Tag).Value));
                fidBytes.Add(Formatting.ASCIIStringToByteArray(Formatting.ByteArrayToHexString(request.EMV_Data.Children.Get(EMVTagsEnum.TERMINAL_COUNTRY_CODE_9F1A_KRN.Tag).Value).Substring(1)));
                fidBytes.Add(Formatting.ConvertToHexAscii(request.EMV_Data.Children.Get(EMVTagsEnum.TRANSACTION_DATE_9A_KRN.Tag).Value));
                fidBytes.Add(Formatting.ConvertToHexAscii(request.EMV_Data.Children.Get(EMVTagsEnum.APPLICATION_CRYPTOGRAM_9F26_KRN.Tag).Value));
                fidBytes.Add(Formatting.ConvertToHexAscii(request.EMV_Data.Children.Get(EMVTagsEnum.APPLICATION_INTERCHANGE_PROFILE_82_KRN.Tag).Value));
                fidBytes.Add(Formatting.ConvertToHexAscii(request.EMV_Data.Children.Get(EMVTagsEnum.APPLICATION_TRANSACTION_COUNTER_ATC_9F36_KRN.Tag).Value));
                fidBytes.Add(Formatting.ConvertToHexAscii(request.EMV_Data.Children.Get(EMVTagsEnum.UNPREDICTABLE_NUMBER_9F37_KRN.Tag).Value));
                fidBytes.Add(Formatting.ConvertToHexAscii(request.EMV_Data.Children.Get(EMVTagsEnum.TERMINAL_VERIFICATION_RESULTS_95_KRN.Tag).Value));
                fidBytes.Add(Formatting.ConvertToHexAscii(request.EMV_Data.Children.Get(EMVTagsEnum.TRANSACTION_TYPE_9C_KRN.Tag).Value));
                fidBytes.Add(Formatting.ASCIIStringToByteArray(Formatting.ByteArrayToHexString(request.EMV_Data.Children.Get(EMVTagsEnum.TRANSACTION_CURRENCY_CODE_5F2A_KRN.Tag).Value).Substring(1)));
                fidBytes.Add(Formatting.ConvertToHexAscii(request.EMV_Data.Children.Get(EMVTagsEnum.AMOUNT_AUTHORISED_NUMERIC_9F02_KRN.Tag).Value));
                fidBytes.Add(Formatting.ConvertToHexAscii(request.EMV_Data.Children.Get(EMVTagsEnum.ISSUER_APPLICATION_DATA_9F10_KRN.Tag).Value));

                fb_6.Children.Add(new FID_6_O_EMVRequestData(fidBytes.SelectMany(x => x).ToArray()));

                fidBytes = new List <byte[]>();
                fidBytes.Add(smartCardScheme);
                fidBytes.Add(Formatting.ConvertToHexAscii(request.EMV_Data.Children.Get(EMVTagsEnum.APPLICATION_PRIMARY_ACCOUNT_NUMBER_PAN_SEQUENCE_NUMBER_5F34_KRN.Tag).Value));
                fidBytes.Add(Formatting.ConvertToHexAscii(request.EMV_Data.Children.Get(EMVTagsEnum.TERMINAL_TYPE_9F35_KRN.Tag).Value));
                fidBytes.Add(Formatting.ConvertToHexAscii(request.EMV_Data.Children.Get(EMVTagsEnum.CARDHOLDER_VERIFICATION_METHOD_CVM_RESULTS_9F34_KRN.Tag).Value));
                fidBytes.Add(Formatting.ConvertToHexAscii(request.EMV_Data.Children.Get(EMVTagsEnum.APPLICATION_VERSION_NUMBER_TERMINAL_9F09_KRN.Tag).Value));
                fidBytes.Add(Formatting.ConvertToHexAscii(request.EMV_Data.Children.Get(EMVTagsEnum.DEDICATED_FILE_DF_NAME_84_KRN.Tag).Value));

                fb_6.Children.Add(new FID_6_P_EMVAdditionalRequestData(fidBytes.SelectMany(x => x).ToArray()));
            }

            Logger.Log(np.ToPrintString());

            bool check = np.Validate();

            byte[] received;

            using (request.TCPClientStream)
            {
                request.TCPClientStream.Connect(host, port);
                received = TCPIPManager.SendTransaction(request.TCPClientStream, np.Serialize());
            }

            int             pos = 0;
            TransactionBase fb;

            switch (tt)
            {
            case TransactionTypeEnum.PurchaseGoodsAndServices:
                fb = new F00_NormalPurchase();
                break;

            case TransactionTypeEnum.PurchaseWithCashback:
                fb = new F08_PurchaseWithCashBack();
                break;

            case TransactionTypeEnum.Refund:
                fb = new F12_MerchandiseReturnAdjustment();
                break;

            default:
                throw new Exception("Unimplemented TransactionTypeEnum:" + tt);
            }
            pos = fb.Deserialize(received, pos);

            Logger.Log(fb.ToPrintString());

            FIDBase responseMessageFid = fb.FindFid(FidMetaList.ResponseDisplay).Get();
            string  responseMessage    = Formatting.ByteArrayToASCIIString(responseMessageFid.Value);
            bool    responseCode;
            int     responseCodeAsNumber = Convert.ToInt32(Formatting.ByteArrayToASCIIString(fb.Header.GetValue(HeaderEntryEnum.ResponseCode)));

            if (responseCodeAsNumber >= 0 && responseCodeAsNumber <= 10)
            {
                responseCode = true;
            }
            else
            {
                responseCode = false;
            }

            FIDBase responseMessage6QFid    = fb.FindFid(FidMetaList.EMVResponseData).Get();
            string  responseMessage6Q       = Formatting.ByteArrayToASCIIString(responseMessage6QFid.Value);
            string  smartCardSchemeResponse = responseMessage6Q.Substring(0, 2);
            string  authResponseCode        = "";
            string  issuerAuthData;

            if (smartCardSchemeResponse == "00") //schem 1
            {
                issuerAuthData = responseMessage6Q.Substring(2);
            }
            else //01 == schem 2
            {
                authResponseCode = responseMessage6Q.Substring(2, 2);
                issuerAuthData   = responseMessage6Q.Substring(4);
            }

            TLV authcodeTLV = null;

            if (!String.IsNullOrEmpty(authResponseCode))
            {
                authcodeTLV = TLV.Create(EMVTagsEnum.AUTHORISATION_RESPONSE_CODE_8A_KRN.Tag, Formatting.ASCIIStringToByteArray(authResponseCode));
            }

            TLV issuerAuthDataTLV = TLV.Create(EMVTagsEnum.ISSUER_AUTHENTICATION_DATA_91_KRN.Tag, Formatting.HexStringToByteArray(issuerAuthData));

            return(new EMVApproverResponse()
            {
                IsApproved = responseCode,
                ResponseMessage = responseMessage,
                AuthCode_8A = authcodeTLV,
                IssuerAuthData_91 = issuerAuthDataTLV
            });
        }