コード例 #1
0
        public void InstallForPerso(String instanceId)
        {
            AID instance             = new AID(instanceId);
            ByteArrayOutputStream bo = new ByteArrayOutputStream();

            try
            {
                bo.Write((byte)0x00);
                bo.Write((byte)0x00);

                bo.Write((byte)instance.getLength());
                bo.Write(instance.getBytes());

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

            GPInstallRequest install = new GPInstallRequest((byte)InstallRequestP1Enum.LastOrOnlyCommand | (byte)InstallRequestP1Enum.ForPersonalization)
            {
                CommandData = bo.ToByteArray()
            };
            //System.Diagnostics.Debug.WriteLine(install.ToPrintString());
            GPInstallResponse response = (GPInstallResponse)SendCommand(install);

            if (response.SW != (ushort)ISO7816ReturnCodes.SW_NO_ERROR)
            {
                throw new Exception("Install for Perso failed");
            }
        }
コード例 #2
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");
                }
            }
        }
コード例 #3
0
        private void populate_tags(byte[] data, Kind type)
        {
            TLVList tlvList = new TLVList();

            tlvList.Deserialize(data, true);
            foreach (TLV tlv in tlvList)//each E3
            {
                AID aid    = new AID(tlv.Children.Get("4F").Value);
                AID domain = null;
                if (tlv.Children.IsPresent("CC"))
                {
                    domain = new AID(tlv.Children.Get("CC").Value);
                }

                if (type == Kind.ExecutableLoadFile)
                {
                    GPRegistryEntryPkg pkg = new GPRegistryEntryPkg();
                    pkg.setType(type);
                    pkg.setAID(aid);
                    pkg.setDomain(domain);
                    pkg.setVersion(tlv.Children.Get("CE").Value);

                    foreach (TLV tlv84 in tlv.Children)
                    {
                        if (tlv84.Tag.TagLable == "84")
                        {
                            AID a = new AID(tlv84.Value);
                            pkg.addModule(a);
                        }
                    }
                    pkg.setLifeCycle(tlv.Children.Get("9F70").Value[0] & 0xFF);

                    add(pkg);
                }
                else
                {
                    GPRegistryEntryApp app = new GPRegistryEntryApp();
                    app.setType(type);
                    app.setAID(aid);
                    app.setDomain(domain);

                    Privileges privs = Privileges.fromBytes(tlv.Children.Get("C5").Value);
                    app.setPrivileges(privs);

                    if (tlv.Children.IsPresent("C4"))
                    {
                        AID a = new AID(tlv.Children.Get("C4").Value);
                        app.setLoadFile(a);
                    }

                    app.setLifeCycle(tlv.Children.Get("9F70").Value[0] & 0xFF);

                    add(app);
                }
            }
        }
コード例 #4
0
        public void UnLockApp(AID instanceAID)
        {
            GPSetStatusRequest  delete   = new GPSetStatusRequest(instanceAID.getBytes(), false);
            GPSetStatusResponse response = (GPSetStatusResponse)SendCommand(delete);

            if (response.SW != (ushort)ISO7816ReturnCodes.SW_NO_ERROR)
            {
                throw new Exception("UnLock App failed");
            }
        }
コード例 #5
0
        private void installAndMakeSelectable(AID packageAID, AID appletAID, AID instanceAID, Privileges privileges, byte[] installParams, byte[] installToken)
        {
            if (instanceAID == null)
            {
                instanceAID = appletAID;
            }
            //if (getRegistry().allAppletAIDs().Contains(instanceAID))
            //{
            //giveStrictWarning("Instance AID " + instanceAID + " is already present on card");
            //}
            if (installParams == null)
            {
                installParams = new byte[] { (byte)0xC9, 0x00 };
            }
            if (installToken == null)
            {
                installToken = new byte[0];
            }
            byte[] privs             = privileges.toBytes();
            ByteArrayOutputStream bo = new ByteArrayOutputStream();

            try
            {
                bo.Write((byte)packageAID.getLength());
                bo.Write(packageAID.getBytes());

                bo.Write((byte)appletAID.getLength());
                bo.Write(appletAID.getBytes());

                bo.Write((byte)instanceAID.getLength());
                bo.Write(instanceAID.getBytes());

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

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

                bo.Write((byte)installToken.Length);
                bo.Write(installToken);
            }
            catch (IOException ioe)
            {
                throw new Exception(ioe.Message);
            }
            GPInstallRequest install = new GPInstallRequest(GPInstructionEnum.Install, bo.ToByteArray(), 0x0C, 0x00);
            //System.Diagnostics.Debug.WriteLine(install.ToPrintString());
            GPInstallResponse response = (GPInstallResponse)SendCommand(install);

            if (response.SW != (ushort)ISO7816ReturnCodes.SW_NO_ERROR)
            {
                throw new Exception("Install for Install and make selectable failed");
            }
        }
コード例 #6
0
        public void deleteAID(AID aid, bool deleteDeps)
        {
            ByteArrayOutputStream bo = new ByteArrayOutputStream();

            try
            {
                bo.Write((byte)0x4F);
                bo.Write((byte)aid.getLength());
                bo.Write(aid.getBytes());
            }
            catch (IOException ioe)
            {
                throw new Exception(ioe.Message);
            }
            GPInstallRequest  delete   = new GPInstallRequest(GPInstructionEnum.Delete, bo.ToByteArray(), 0x00, (byte)(deleteDeps ? 0x80 : 0x00));
            GPInstallResponse response = (GPInstallResponse)SendCommand(delete);

            if (response.SW != (ushort)ISO7816ReturnCodes.SW_NO_ERROR)
            {
                throw new Exception("Deletion failed");
            }
        }
コード例 #7
0
        public AID getDefaultSelectedPackageAID()
        {
            AID defaultAID = getDefaultSelectedAID();

            if (defaultAID != null)
            {
                foreach (GPRegistryEntryPkg e in allPackages())
                {
                    if (e.getModules().Contains(defaultAID))
                    {
                        return(e.getAID());
                    }
                }
                // Did not get a hit. Loop packages and look for prefixes
                foreach (GPRegistryEntryPkg e in allPackages())
                {
                    if (defaultAID.ToString().StartsWith(e.getAID().ToString()))
                    {
                        return(e.getAID());
                    }
                }
            }
            return(null);
        }
コード例 #8
0
        private CapFile(MemoryStream inval, String packageName)
        {
            ZipArchive zip = new ZipArchive(inval);
            Dictionary <String, byte[]> entries = getEntries(zip);

            if (packageName != null)
            {
                packageName = packageName.Replace('.', '/') + "/javacard/";
            }
            else
            {
                String lookFor = "Header.cap";
                foreach (String s in entries.Keys)
                {
                    if (s.EndsWith(lookFor))
                    {
                        packageName = s.Substring(0, s.LastIndexOf(lookFor));
                        break;
                    }
                }
            }

            // Parse manifest
            //byte[] mf = entries["META-INF/MANIFEST.MF"];
            //entries.Remove("META-INF/MANIFEST.MF");
            //if (mf != null)
            //{
            //    //ByteArrayInputStream mfi = new ByteArrayInputStream(mf);
            //    //manifest = new Manifest(mfi);
            //}

            // Avoid a possible NPE
            if (packageName == null)
            {
                throw new Exception("Could not figure out the package name of the applet!");
            }


            this.packageName = packageName.Substring(0, packageName.LastIndexOf("/javacard/")).Replace('/', '.');
            foreach (String name in componentNames)
            {
                String fullName = packageName + name + ".cap";
                byte[] contents = getEntry(entries, fullName);
                capComponents.Add(name, contents);
            }
            // FIXME: Not existing and not used ZIP elements
            List <List <byte[]> > tables = new List <List <byte[]> >();

            tables.Add(dapBlocks);
            tables.Add(loadTokens);
            tables.Add(installTokens);
            String[] names = { "dap", "lt", "it" };
            for (int i = 0; i < names.Length; i++)
            {
                int index = 0;
                while (true)
                {
                    String fullName = "meta-inf/" + packageName.Replace('/', '-') + names[i] + (index + 1);
                    byte[] contents = getEntry(entries, fullName);
                    if (contents == null)
                    {
                        break;
                    }
                    tables[i].Add(contents);
                    index++;
                }
            }

            zip.Dispose();
            inval.Dispose();

            // Parse package.
            // See JCVM 2.2 spec section 6.3 for offsets.
            byte[] header = capComponents["Header"];
            major_version = header[10];
            minor_version = header[11];
            packageAID    = new AID(header, 13, header[12]);

            // Parse applets
            // See JCVM 2.2 spec section 6.5 for offsets.
            byte[] applet = capComponents["Applet"];
            if (applet != null)
            {
                int offset = 4;
                for (int j = 0; j < (applet[3] & 0xFF); j++)
                {
                    int len = applet[offset++];
                    appletAIDs.Add(new AID(applet, offset, len));
                    // Skip install_method_offset
                    offset += len + 2;
                }
            }
        }
コード例 #9
0
 public void addModule(AID aid)
 {
     modules.Add(aid);
 }
コード例 #10
0
 public void setDomain(AID dom)
 {
     this.domain = dom;
 }
コード例 #11
0
 public void setAID(AID aid)
 {
     this.aid = aid;
 }
コード例 #12
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);
        }