Exemplo n.º 1
0
        private void DoEntryPointBGetApps(string secDomainAID, string masterKey)
        {
            Task.Run(() =>
            {
                try
                {
                    ProtocolActivation_B().ContinueWith((parentTask) =>
                    {
                        try
                        {
                            if (cancellationTokenForPreProcessing.Token.IsCancellationRequested)
                            {
                                cancellationTokenForPreProcessing.Dispose();
                                return;
                            }

                            PersoProcessing pxml = new PersoProcessing(cardQProcessor);
                            GPRegistry reg       = pxml.GetAppList(secDomainAID, masterKey);

                            PersoProcessingOutcome processingOutcome = new PersoProcessingOutcome()
                            {
                                NextProcessState          = EMVPersoPreProcessingStateEnum.EndProcess,
                                UIRequestOnOutcomePresent = true,
                                UserInterfaceRequest      = new UserInterfaceRequest()
                                {
                                    MessageIdentifier = MessageIdentifiersEnum.ClearDisplay, Status = StatusEnum.ReadyToRead
                                },
                                UIRequestOnRestartPresent = false,
                                GPRegistry = reg
                            };

                            OnProcessCompleted(processingOutcome);
                        }
                        catch (Exception ex)
                        {
                            OnExceptionOccured(ex);
                        }
                    }, TaskContinuationOptions.OnlyOnRanToCompletion);
                }
                catch (Exception ex)
                {
                    OnExceptionOccured(ex);
                }
            });
        }
Exemplo n.º 2
0
        public GPRegistry getRegistry()
        {
            GPRegistry registry = new GPRegistry();

            // Issuer security domain
            byte[] data = getConcatenatedStatus(registry, 0x80, new byte[] { 0x4F, 0x00 });
            registry.parse(0x80, data, Kind.IssuerSecurityDomain, spec);

            // Apps and security domains
            data = getConcatenatedStatus(registry, 0x40, new byte[] { 0x4F, 0x00 });
            registry.parse(0x40, data, Kind.Application, spec);

            // Load files
            data = getConcatenatedStatus(registry, 0x20, new byte[] { 0x4F, 0x00 });
            registry.parse(0x20, data, Kind.ExecutableLoadFile, spec);

            return(registry);
        }
Exemplo n.º 3
0
        private byte[] getConcatenatedStatus(GPRegistry reg, byte p1, byte[] data)
        {
            // By default use tags
            byte p2 = (byte)(reg.tags ? 0x02 : 0x00);

            GPGetStatusRequest  cmd      = new GPGetStatusRequest(data, p1, p2);
            GPGetStatusResponse response = (GPGetStatusResponse)SendCommand(cmd);

            // Workaround for legacy cards, like SCE 6.0 FIXME: this does not work properly
            // Find a different way to adjust the response parser without touching the overall spec mode

            // If ISD-s are asked and none is returned, it could be either
            // - SSD
            // - no support for tags
            if (p1 == 0x80 && response.SW == 0x6A86)
            {
                if (p2 == 0x02)
                {
                    // If no support for tags. Re-issue command without requesting tags
                    reg.tags = false;
                    return(getConcatenatedStatus(reg, p1, data));
                }
            }

            int sw = response.SW;

            if ((sw != (int)ISO7816ReturnCodes.SW_NO_ERROR) && (sw != 0x6310))
            {
                // Possible values:
                if (sw == 0x6A88)
                {
                    // No data to report
                    return(response.ResponseData);
                }
                // 0x6A86 - no tags support or ISD asked from SSD
                // 0a6A81 - Same as 6A88 ?
                //logger.warn("GET STATUS failed for " + HexUtils.bin2hex(cmd.getBytes()) + " with " + Integer.toHexString(sw));
                return(response.ResponseData);
            }

            ByteArrayOutputStream bo = new ByteArrayOutputStream();

            try
            {
                bo.Write(response.ResponseData);

                while (response.SW == 0x6310)
                {
                    cmd      = new GPGetStatusRequest(data, p1, (byte)(p2 | 0x01));
                    response = (GPGetStatusResponse)SendCommand(cmd);

                    sw = response.SW;
                    if ((sw != (int)ISO7816ReturnCodes.SW_NO_ERROR) && (sw != 0x6310))
                    {
                        throw new Exception("GET STATUS failed for " + Formatting.ByteArrayToHexString(cmd.Serialize()));
                    }
                    bo.Write(response.ResponseData);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return(bo.ToByteArray());
        }
Exemplo n.º 4
0
        public void installCapFile(MemoryStream capFile)
        {
            //final File capfile;
            //capfile = (File)args.valueOf(OPT_INSTALL);

            CapFile instcap = new CapFile(capFile);

            // Only install if cap contains a single applet
            if (instcap.getAppletAIDs().Count == 0)
            {
                throw new Exception("No applets in CAP");
            }
            if (instcap.getAppletAIDs().Count > 1)
            {
                throw new Exception("CAP contains more than one applet");
            }

            GPRegistry reg   = getRegistry();
            Privileges privs = getInstPrivs(isDefaultApplet, isAppletTerminate);

            // Remove existing default app
            if (doForceInstallApplet && (reg.getDefaultSelectedAID() != null && privs.has(Privilege.CardReset)))
            {
                deleteAID(reg.getDefaultSelectedAID(), false);
            }
            // Remove existing load file
            if (doForceInstallApplet && reg.allPackageAIDs().Contains(instcap.getPackageAID()))
            {
                deleteAID(instcap.getPackageAID(), true);
            }

            try
            {
                loadCapFile("", instcap);
                //System.err.println("CAP loaded");
            }
            catch (Exception e)
            {
                //if (e.sw == 0x6985 || e.sw == 0x6A80)
                //{
                //    System.err.println("Applet loading failed. Are you sure the CAP file (JC version, packages) is compatible with your card?");
                //}
                throw e;
            }

            // Take the applet AID from CAP but allow to override
            AID appaid = instcap.getAppletAIDs()[0];

            //if (args.has(OPT_APPLET))
            //{
            //    appaid = (AID)args.valueOf(OPT_APPLET);
            //}
            //if (args.has(OPT_CREATE))
            //{
            //    appaid = (AID)args.valueOf(OPT_CREATE);
            //}
            if (getRegistry().allAIDs().Contains(appaid))
            {
                //System.err.println("WARNING: Applet " + appaid + " already present on card");
                throw new Exception("Applet " + appaid + " already present on card");
            }
            installAndMakeSelectable(instcap.getPackageAID(), appaid, null, privs, getInstParams(null), null);
        }