Esempio n. 1
0
        public FileSystem(short size, byte dfMax, byte efMax)
        {
            _dfMax = dfMax;
            _efMax = efMax;

            // Allocation of data memory
            memory     = new byte[(short)(size << 2)];
            attributes = new byte[size];
            Erase(0, (short)(size << 2));

            // Allocation of DF files structures
            dedicatedFiles = new DedicatedFile[_dfMax];
            byte i;

            for (i = 0; i < _dfMax; i++)
            {
                dedicatedFiles[i] = new DedicatedFile(this);
            }

            // Allocation of EF files structures
            elementaryFiles = new ElementaryFile[_efMax];
            for (i = 0; i < _efMax; i++)
            {
                elementaryFiles[i] = new ElementaryFile(this);
            }
        }
Esempio n. 2
0
        public JavaCardApplet()
        {
            _masterFile = new DedicatedFile(new FileSystem(Constants.FILESYSTEM_SIZE, Constants.DF_MAX, Constants.EF_MAX));
            _masterFile.Setup(null, 0, Constants.FILESYSTEM_SIZE, Constants.MF_HEADER, 0, (short)Constants.MF_HEADER.Length);

            _headerParser = new HeaderParser();
            _currentDF    = _masterFile;
            _currentEF    = null;

            // Initialize structure for TB100
            _masterFile.CreateElementaryFile(0x0000, 0x0006, new byte[] { 0x17, 0xFF, 0x06, 0xE4 }, 0x0000, 0x0004);
            _masterFile.CreateElementaryFile(0x0006, 0x0005, new byte[] { 0x1F, 0x6C, 0x05, 0x70 }, 0x0000, 0x0004);
            _masterFile.CreateElementaryFile(0x000B, 0x0005, new byte[] { 0x0E, 0x2F, 0xF5, 0xCE }, 0x0000, 0x0004);
            _masterFile.CreateElementaryFile(0x0010, 0x0003, new byte[] { 0x0E, 0x10, 0x03, 0xDF }, 0x0000, 0x0004);
            _masterFile.CreateDedicatedFile(0x0013, 0x0022, new byte[] { 0x7F, 0x00, 0x00, 0x22, 0xFF, 0xFF, 0xFE, 0x62 }, 0x0000, 0x0008);
            ElementaryFile ef0E2F = (ElementaryFile)_masterFile.FindFileByFileId(0x0E2F);

            ef0E2F.Write(new byte[] { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 }, 0, 0x0002, 2);
            ElementaryFile ef0E10 = (ElementaryFile)_masterFile.FindFileByFileId(0x0E10);

            ef0E10.Write(new byte[] { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30 }, 0, 0x0000, 2);
            DedicatedFile df7F00 = (DedicatedFile)_masterFile.FindFileByFileId(0x7F00);

            df7F00.CreateElementaryFile(0x0000, 0x0010, new byte[] { 0x6F, 0x01, 0x00, 0x10, 0xFF, 0xFF, 0xFF, 0x83 }, 0, 0x0008);
        }
Esempio n. 3
0
        /// <summary>
        /// Process DELETE FILE instruction (E4)
        /// <para>
        /// C-APDU: <code>00 E4 00 00 02 {fid}</code>
        /// </para>
        /// </summary>
        /// <param name="apdu"></param>
        /// <returns></returns>
        private IFakeCardFeedback ProcessDeleteFile(CommandAPDU apdu)
        {
            byte[] buffer = apdu.GetBuffer();
            _ = apdu.SetIncomingAndReceive();

            short udcOffset = APDUHelpers.getOffsetCdata(apdu);
            short lc        = APDUHelpers.getIncomingLength(apdu);

            if (lc != 2)
            {
                ISOException.throwIt(JavaCard.ISO7816.SW_WRONG_P1P2);
            }

            short fid = Util.getShort(buffer, udcOffset);

            if (_currentDF.DeleteFile(fid) == false)
            {
                ISOException.throwIt(JavaCard.ISO7816.SW_FILE_NOT_FOUND);
            }

            if (_currentEF != null && fid == _currentEF.GetFileId())
            {
                _currentEF = null;
            }

            return(apdu.SetOutgoingAndSend(0, 0));
        }
Esempio n. 4
0
        /// <summary>
        /// Process SELECT instruction (CC4).
        /// <para>
        /// C-APDU: <code>00 A4 00 00 02 {FID} {Le}</code>
        /// </para>
        /// <para>
        /// R-APDU: <code>{offset} {size} {header}</code>
        /// </para>
        /// <list type="table">
        ///     <item>
        ///         <term>offset</term>
        ///         <description>offset of first word of the file, 2 bytes (WORDS).</description>
        ///     </item>
        ///     <item>
        ///         <term>size</term>
        ///         <description>size of the body of the file (WORDS).</description>
        ///     </item>
        /// </list>
        /// </summary>
        /// <param name="apdu"></param>
        /// <returns></returns>
        private IFakeCardFeedback ProcessSelect(CommandAPDU apdu)
        {
            byte[] buffer = apdu.GetBuffer();
            apdu.SetIncomingAndReceive();

            short udcOffset = APDUHelpers.getOffsetCdata(apdu);
            short lc        = (short)apdu.Lc;

            if (lc != 2)
            {
                ISOException.throwIt(JavaCard.ISO7816.SW_WRONG_LENGTH);
            }

            short fid = Util.getShort(buffer, udcOffset);

            File file;

            if (fid == 0x3F00)
            {
                file = _masterFile;
            }
            else
            {
                file = _currentDF.FindFileByFileId(fid);
            }

            if (file == null)
            {
                ISOException.throwIt(JavaCard.ISO7816.SW_FILE_NOT_FOUND);
            }

            // Update current DF / EF
            if (file.IsDF())
            {
                _currentDF = (DedicatedFile)file;
                _currentEF = null;
            }
            else
            {
                _currentEF = (ElementaryFile)file;
            }

            // Build and send R-APDU
            short headerSize = file.GetHeaderSize();

            Util.setShort(buffer, 0, (short)(file._inParentBodyOffset >> 2));
            Util.setShort(buffer, 2, (short)(file.GetLength() - headerSize));
            file.GetHeader(buffer, 4);

            // TODO Automagically adds 9000

            return(apdu.SetOutgoingAndSend(0, (short)(4 + (headerSize << 2))));
        }