예제 #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 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");
            }
        }
예제 #4
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");
            }
        }
예제 #5
0
        public static void GPPersoFromXmlTest(string xml)
        {
            perso perso = XMLUtil <perso> .Deserialize(xml);

            perso.application.ToList().ForEach(app =>
            {
                installType it         = app.commands.install;
                INSTALL_PARAM_C9_GP ip = null;
                if (it.tokens != null)
                {
                    if (it.tokens.tlvxml != null)
                    {
                        ip             = new INSTALL_PARAM_C9_GP();
                        valueC9Type c9 = it.tokens.tlvxml.valueC9;
                        if (c9.C9_ApplicationInstanceSpecified)
                        {
                            ip.Value.ApplicationInstance = MapC9_ApplicationInstanceEnum(c9.C9_ApplicationInstance);
                        }
                        if (c9.C9_PinSharingSpecified)
                        {
                            ip.Value.PinSharing = MapC9_PinSharingEnum(c9.C9_PinSharing);
                        }
                        if (c9.C9_InterfacesAvailableSpecified)
                        {
                            ip.Value.InterfacesAvailable = MapC9_InterfacesAvailableEnum(c9.C9_InterfacesAvailable);
                        }
                    }
                }

                GPInstallRequestDataForInstall irfi = new GPInstallRequestDataForInstall()
                {
                    ExecutableLoadFileAID = it.ExecutableLoadFileAID,
                    ExecutableModuleAID   = it.ExecutableModuleAID,
                    ApplicationAID        = it.ApplicationAID,
                    Privileges            = Formatting.HexStringToByteArray(it.Privileges),
                    InstallToken          = Formatting.HexStringToByteArray(it.InstallToken),
                    InstallParamC9        = ip,
                };

                //select
                //auth
                GPInstallRequest ir = new GPInstallRequest((byte)InstallRequestP1Enum.LastOrOnlyCommand | (byte)InstallRequestP1Enum.ForMakeSelectable | (byte)InstallRequestP1Enum.ForInstall)
                {
                    CommandData = irfi.Serialize()
                };
                System.Diagnostics.Debug.WriteLine(ir.ToPrintString());

                //select
                //auth
                app.commands.storeData.ToList().ForEach(command =>
                {
                    GPStoreData sd = new GPStoreData
                    {
                        DGI = Formatting.HexStringToByteArray(command.DGI)
                    };
                    if (command.data != null)
                    {
                        sd.DataBytes = Formatting.HexStringToByteArray(command.data);
                    }
                    else
                    {
                        sd.Data = DeserList(command.tlvxml);
                    }
                    GPStoreDataReqest sdr = new GPStoreDataReqest
                    {
                        CommandData = sd.Serialize()
                    };
                    System.Diagnostics.Debug.WriteLine(sdr.ToPrintString());
                });
            });
        }