Beispiel #1
0
        private void loadCapFile(String sAID, CapFile cap, bool includeDebug, bool separateComponents, bool loadParam, bool useHash)
        {
            //if (getRegistry().allAIDs().Contains(cap.getPackageAID()))
            //{
            //    giveStrictWarning("Package with AID " + cap.getPackageAID() + " is already present on card");
            //}
            byte[] hash = useHash ? cap.getLoadFileDataHash("SHA1", includeDebug) : new byte[0];
            int    len  = cap.getCodeLength(includeDebug);

            // FIXME: parameters are optional for load
            byte[] loadParams = loadParam ? new byte[] { (byte)0xEF, 0x04, (byte)0xC6, 0x02, (byte)((len & 0xFF00) >> 8),
                                                         (byte)(len & 0xFF) } : new byte[0];

            ByteArrayOutputStream bo = new ByteArrayOutputStream();

            try
            {
                bo.Write((byte)cap.getPackageAID().getLength());
                bo.Write(cap.getPackageAID().getBytes());

                AID aid = new AID(sAID);
                bo.Write((byte)aid.getLength());
                bo.Write(aid.getBytes());

                bo.Write((byte)hash.Length);
                bo.Write(hash);

                bo.Write((byte)loadParams.Length);
                bo.Write(loadParams);

                bo.Write((byte)0x00); //no load token
            }
            catch (IOException ioe)
            {
                throw new Exception(ioe.Message);
            }

            GPInstallRequest installForLoad = new GPInstallRequest(GPInstructionEnum.Install, bo.ToByteArray(), 0x02, 0x00);
            //System.Diagnostics.Debug.WriteLine(installForLoad.ToPrintString());
            GPInstallResponse response = (GPInstallResponse)SendCommand(installForLoad);

            if (response.SW != (ushort)ISO7816ReturnCodes.SW_NO_ERROR)
            {
                throw new Exception("Install for Load failed");
            }

            List <byte[]> blocks = cap.getLoadBlocks(includeDebug, separateComponents, wrapper.getBlockSize());

            for (int i = 0; i < blocks.Count; i++)
            {
                GPInstallRequest load = new GPInstallRequest(GPInstructionEnum.Load, blocks[i], (byte)((i == (blocks.Count - 1)) ? 0x80 : 0x00), (byte)i);
                //System.Diagnostics.Debug.WriteLine(load.ToPrintString());
                response = (GPInstallResponse)SendCommand(load);
                if (response.SW != (ushort)ISO7816ReturnCodes.SW_NO_ERROR)
                {
                    throw new Exception("Load failed");
                }
            }
        }
Beispiel #2
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);
        }
Beispiel #3
0
 private void loadCapFile(String aid, CapFile cap)
 {
     loadCapFile(aid, cap, false, false, false, false);
 }
Beispiel #4
0
        public void InstallForLoad(String aid, MemoryStream capFile)
        {
            CapFile instcap = new CapFile(capFile);

            loadCapFile(aid, instcap);
        }