Example #1
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));
        }
Example #2
0
        /// <summary>
        /// Process ERASE instruction (CC3)
        /// <para>
        /// C-APDU: <code>00 0E {offset} {Lc} {length} </code>
        /// </para>
        /// <list type="table">
        ///     <item>
        ///         <term>offset</term>
        ///         <description>offset of first word of the file coded by P1 P2 (WORDS) to be erased.</description>
        ///     </item>
        ///     <item>
        ///         <term>length</term>
        ///         <description>number of words to be erased.</description>
        ///     </item>
        /// </list>
        /// </summary>
        /// <param name="apdu"></param>
        /// <returns></returns>
        private IFakeCardFeedback ProcessErase(CommandAPDU apdu)
        {
            // TODO: check ==> security of current EF

            byte[] apduBuffer = apdu.GetBuffer();
            apdu.SetIncomingAndReceive();

            // Check if Lc ==2
            short lc = APDUHelpers.getIncomingLength(apdu);

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

            short offset    = Util.getShort(apduBuffer, JavaCard.ISO7816.OFFSET_P1); // in WORDS
            short udcOffset = APDUHelpers.getOffsetCdata(apdu);
            short length    = Util.getShort(apduBuffer, udcOffset);                  // in WORDS

            VerifyOutOfFile(offset, length);

            _currentEF.Erase(offset, length);

            return(FakeCardFeedback.FromSuccess(unchecked ((short)0x9000)));
        }
Example #3
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))));
        }
Example #4
0
        /// <summary>
        /// Process CREATE FILE instruction (E0)
        /// <para>
        /// C-APDU: <code>00 E0 {offset} {Lc} {header}</code>
        /// </para>
        /// <list type="table">
        ///     <item>
        ///         <term>offset</term>
        ///         <description>offset of first word of the file coded by P1 P2 (WORDS).</description>
        ///     </item>
        ///     <item>
        ///         <term>header</term>
        ///         <description>header of the new file, must be word aligned.</description>
        ///     </item>
        /// </list>
        /// </summary>
        /// <param name="apdu"></param>
        /// <returns></returns>
        private IFakeCardFeedback ProcessCreateFile(CommandAPDU apdu)
        {
            byte[] buffer = apdu.GetBuffer();
            apdu.SetIncomingAndReceive();

            short headerOffset = APDUHelpers.getOffsetCdata(apdu);
            short headerLength = APDUHelpers.getIncomingLength(apdu);

            if (headerLength < 4)
            {
                ISOException.throwIt(JavaCard.ISO7816.SW_DATA_INVALID);
            }

            short offset = Util.getShort(buffer, JavaCard.ISO7816.OFFSET_P1);

            if (!_headerParser.Parse(buffer, headerOffset, (short)(headerOffset + headerLength)))
            {
                ISOException.throwIt(JavaCard.ISO7816.SW_DATA_INVALID);
            }
            short size = (short)(_headerParser.bodyLength + (short)(_headerParser.headerLength >> 2));

            File file = null;

            switch (_headerParser.fileType)
            {
            case HeaderParser.FILETYPE_DF:
                file = _currentDF.CreateDedicatedFile(offset, size, buffer, headerOffset, headerLength);
                break;

            case HeaderParser.FILETYPE_EFSZ:
            case HeaderParser.FILETYPE_EFWZ:
                file = _currentDF.CreateElementaryFile(offset, size, buffer, headerOffset, headerLength);
                break;

            default:
                ISOException.throwIt(JavaCard.ISO7816.SW_CONDITIONS_NOT_SATISFIED);
                break;
            }

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

            return(apdu.SetOutgoingAndSend(0, 0));
        }
Example #5
0
        /// <summary>
        /// Process WRITE BINARY instruction (CC3)
        /// <para>
        /// C-APDU: <code>00 B0 {offset} {Lc} {data} </code>
        /// </para>
        /// <list type="table">
        ///     <item>
        ///         <term>offset</term>
        ///         <description>offset of first word of the file coded by P1 P2 (WORDS) to be written.</description>
        ///     </item>
        ///     <item>
        ///         <term>data</term>
        ///         <description>data to be written in file.</description>
        ///     </item>
        /// </list>
        /// </summary>
        /// <param name="apdu"></param>
        /// <returns></returns>
        private IFakeCardFeedback ProcessWrite(CommandAPDU apdu)
        {
            // TODO: check ==> security of current EF

            byte[] apduBuffer = apdu.GetBuffer();
            apdu.SetIncomingAndReceive();

            short offset    = Util.getShort(apduBuffer, JavaCard.ISO7816.OFFSET_P1); // in WORDS
            short length    = APDUHelpers.getIncomingLength(apdu);                   // in BYTES
            short wordCount = (short)((short)(length + 3) / 4);                      // length in WORDS

            // availability check
            VerifyOutOfFile(offset, wordCount);
            if (!_currentEF.IsAvailable(offset, wordCount))
            {
                ISOException.throwIt(JavaCard.ISO7816.SW_WRONG_LENGTH);
            }

            // copy data in a buffer
            byte[] buffer    = JCSystem.makeTransientByteArray((short)(wordCount * 4), JCSystem.CLEAR_ON_DESELECT);
            short  udcOffset = APDUHelpers.getOffsetCdata(apdu);

            Util.arrayCopyNonAtomic(apduBuffer, udcOffset, buffer, 0, length);

            // complete words with FF in buffer
            short iMax = (short)(wordCount * 4);

            for (short i = length; i < iMax; i++)
            {
                buffer[i] = 0xFF;
            }

            // and write data to file
            _currentEF.Write(buffer, 0, offset, wordCount);

            return(FakeCardFeedback.FromSuccess(unchecked ((short)0x9000)));
        }