Esempio n. 1
0
        public CardDetails ReadCardDetails()
        {
            DesfireCommand desfireCommand = new DesfireCommand()
            {
                Command = (byte)DesfireCommand.CommandType.GetVersion,
                Data    = null
            };
            DesfireResponse desfireRes = SendCommand(desfireCommand) as DesfireResponse;

            if (!desfireRes.SubsequentFrame || desfireRes.ResponseData.Length != 7)
            {
                return(null);
            }

            CardDetails card = new CardDetails()
            {
                HardwareVendorID     = desfireRes.ResponseData[0],
                HardwareType         = desfireRes.ResponseData[1],
                HardwareSubType      = desfireRes.ResponseData[2],
                HardwareMajorVersion = desfireRes.ResponseData[3],
                HardwareMinorVersion = desfireRes.ResponseData[4],
                HardwareStorageSize  = desfireRes.ResponseData[5],
                HardwareProtocolType = desfireRes.ResponseData[6]
            };

            desfireCommand = new DesfireCommand()
            {
                Command = (byte)DesfireCommand.CommandType.GetAdditionalFrame
            };
            desfireRes = SendCommand(desfireCommand) as DesfireResponse;

            if (!desfireRes.SubsequentFrame || desfireRes.ResponseData.Length != 7)
            {
                // Not expected
                return(null);
            }
            card.SoftwareVendorID     = desfireRes.ResponseData[0];
            card.SoftwareType         = desfireRes.ResponseData[1];
            card.SoftwareSubType      = desfireRes.ResponseData[2];
            card.SoftwareMajorVersion = desfireRes.ResponseData[3];
            card.SoftwareMinorVersion = desfireRes.ResponseData[4];
            card.SoftwareStorageSize  = desfireRes.ResponseData[5];
            card.SoftwareProtocolType = desfireRes.ResponseData[6];

            desfireRes = SendCommand(desfireCommand) as DesfireResponse;

            if (!desfireRes.Succeeded || desfireRes.ResponseData.Length != 14)
            {
                // Not expected
                return(null);
            }

            card.UID = new byte[7];
            System.Buffer.BlockCopy(desfireRes.ResponseData, 0, card.UID, 0, 7);

            card.ProductionBatchNumber = new byte[5];
            System.Buffer.BlockCopy(desfireRes.ResponseData, 7, card.ProductionBatchNumber, 0, 5);

            card.WeekOfProduction = desfireRes.ResponseData[12];
            card.YearOfProduction = desfireRes.ResponseData[13];

            return(card);
        }
        private void DoEntryPointB(DesFireTransactionTypeEnum desFireTransactionType)
        {
            Task.Run(() =>
            {
                try
                {
                    ProtocolActivation_B().ContinueWith((parentTask) =>
                    {
                        try
                        {
                            if (cancellationTokenForPreProcessing.Token.IsCancellationRequested)
                            {
                                cancellationTokenForPreProcessing.Dispose();
                                return;
                            }

                            DesfireAccessHandler desfireAccess;
                            byte[] sessionKey = new byte[0];
                            switch (desFireTransactionType)
                            {
                            case DesFireTransactionTypeEnum.ProcessTransaction:
                                desfireAccess       = new DesfireAccessHandler(cardQProcessor);
                                CardDetails desfire = desfireAccess.ReadCardDetails();
                                Logger.Log("DesFire Card Details:  " + Environment.NewLine + desfire.ToString());

                                //desfireAccess.SelectApplication(new byte[] { 0x00, 0x00, 0x01 }); //select installed application

                                //sessionKey = desfireAccess.AuthenticateAES();
                                //Logger.Log("Session Key:  " + Formatting.ByteArrayToHexString(sessionKey));

                                //byte[] dataWrittenAndReadBack = desfireAccess.WriteData(sessionKey, 0x01, 0x08);
                                //Logger.Log("Data Written:  " + Formatting.ByteArrayToHexString(dataWrittenAndReadBack));

                                OnProcessCompleted(new DesfireTerminalProcessingOutcome()
                                {
                                    CardDetails               = desfire,
                                    NextProcessState          = EMVTerminalPreProcessingStateEnum.EndProcess,
                                    UIRequestOnOutcomePresent = true,
                                    UserInterfaceRequest      = new UserInterfaceRequest()
                                    {
                                        MessageIdentifier = MessageIdentifiersEnum.ClearDisplay,
                                        Status            = StatusEnum.ReadyToRead
                                    }
                                });
                                break;

                            case DesFireTransactionTypeEnum.InstallApp:
                                desfireAccess = new DesfireAccessHandler(cardQProcessor);
                                //auth to PICC application and get session key
                                //PICC master key setting default does not require authentication to be done before creating an application
                                //we will eventually change this
                                desfireAccess.SelectApplication(new byte[] { 0x00, 0x00, 0x00 });     //select pic application

                                //sessionKey = desfireAccess.AuthenticateAES();
                                //Logger.Log("Session Key:  " + Formatting.ByteArrayToHexString(sessionKey));

                                desfireAccess.GetKeyVersion(0x00);
                                desfireAccess.GetKeySettings();
                                desfireAccess.GetApplicationIDs();
                                desfireAccess.GetFileIDs();

                                desfireAccess.CreateApplication(new byte[] { 0x00, 0x00, 0x01 });
                                desfireAccess.SelectApplication(new byte[] { 0x00, 0x00, 0x01 });     //select installed application
                                desfireAccess.CreateFile(0x01);

                                OnProcessCompleted(new TerminalProcessingOutcome()
                                {
                                    NextProcessState          = EMVTerminalPreProcessingStateEnum.EndProcess,
                                    UIRequestOnOutcomePresent = true,
                                    UserInterfaceRequest      = new UserInterfaceRequest()
                                    {
                                        MessageIdentifier = MessageIdentifiersEnum.ClearDisplay,
                                        Status            = StatusEnum.ReadyToRead
                                    }
                                });
                                break;

                            default:
                                throw new DesFireException("Unrecognised desfire transaction type: " + desFireTransactionType);
                            }
                        }
                        catch (Exception ex)
                        {
                            OnExceptionOccured(ex);
                        }
                    }, TaskContinuationOptions.OnlyOnRanToCompletion);
                }
                catch (Exception ex)
                {
                    OnExceptionOccured(ex);
                }
            });
        }