Exemplo n.º 1
0
        public void authenticate(byte blockNumber, KEYTYPES keyType, byte KeyNumber)
        {
            if (KeyNumber < 0x00 && KeyNumber > 0x20)
            {
                throw new Exception("Key number is invalid");
            }

            apduCommand = new Apdu();
            apduCommand.setCommand(new byte[] { 0xFF,           //Instruction Class
                                                0x86,           //Instruction Code
                                                0x00,           //RFU
                                                0x00,           //RFU
                                                0x05 });        //Length of authentication data bytes

            //Authentication Data Bytes
            apduCommand.data = new byte[] { 0x01,               //Version
                                            0x00,               //RFU
                                            (byte)blockNumber,  //Block Number
                                            (byte)keyType,      //Key Type
                                            KeyNumber };        //Key Number

            sendCommand();

            if (!apduCommand.statusWordEqualTo(new byte[] { 0x90, 0x00 }))
            {
                throw new CardException("Authenticate failed", apduCommand.statusWord);
            }
        }
Exemplo n.º 2
0
        public void loadAuthKey(KEY_STRUCTURE keyStructure, byte keyNumber, byte[] key)
        {
            if (key.Length != 6)
            {
                throw new Exception("Invalid key length");
            }


            apduCommand = new Apdu();
            apduCommand.setCommand(new byte[] { 0xFF,                   //Instruction Class
                                                0x82,                   //Instruction code
                                                (byte)keyStructure,     //Key Structure
                                                keyNumber,              //Key Number
                                                0x06 });                //Length of key

            //Set key to load
            apduCommand.data = key;

            sendCommand();

            if (!apduCommand.statusWordEqualTo(new byte[] { 0x90, 0x00 }))
            {
                throw new CardException("Load key failed", apduCommand.statusWord);
            }
        }
Exemplo n.º 3
0
        public void updateBinary(byte blockNumber, byte[] data, byte length)
        {
            Apdu apdu;
            int  retCode;

            if (data.Length > 48)
            {
                throw new Exception("Data has invalid length");
            }

            if (length % 16 != 0)
            {
                throw new Exception("Data length must be multiple of 16");
            }

            //if (data.Length != 16)
            //    Array.Resize(ref data, 16);

            apdu = new Apdu();
            apdu.setCommand(new byte[] { 0xFF, 0xD6, 0x00, blockNumber, length });

            apdu.data = new byte[data.Length];
            Array.Copy(data, apdu.data, length);

            pcscConnection.sendCommand(ref apdu);

            if (apdu.statusWord[0] != 0x90)
            {
                throw new CardException("Update failed", apdu.statusWord);
            }
        }
Exemplo n.º 4
0
        public byte[] readBinary(byte blockNumber, byte length)
        {
            Apdu apdu;

            apdu = new Apdu();
            apdu.setCommand(new byte[] { 0xFF, 0xB0, 0x00, blockNumber, length });
            apdu.data           = new byte[0];
            apdu.lengthExpected = length;

            pcscConnection.sendCommand(ref apdu);
            if (apdu.statusWord[0] != 0x90)
            {
                throw new CardException("Read failed", apdu.statusWord);
            }

            return(apdu.response.Take(length).ToArray());
        }
Exemplo n.º 5
0
        public void valueBlock(byte blockNumber, VALUEBLOCKOPERATION transType, int amount)
        {
            Apdu apdu;

            apdu = new Apdu();
            apdu.setCommand(new byte[] { 0xFF, 0xD7, 0x00, blockNumber, 0x05 });

            apdu.data    = new byte[5];
            apdu.data[0] = (byte)transType;
            Array.Copy(Helper.intToByte(amount), 0, apdu.data, 1, 4);

            pcscConnection.sendCommand(ref apdu);

            if (!apdu.statusWordEqualTo(new byte[] { 0x90, 0x00 }))
            {
                throw new CardException("Value block operation failed", apdu.statusWord);
            }
        }
Exemplo n.º 6
0
        public Int32 inquireAmount(byte blockNumber)
        {
            Apdu apdu;

            apdu = new Apdu();
            apdu.setCommand(new byte[] { 0xFF, 0xB1, 0x00, blockNumber, 0x04 });
            apdu.data           = null;
            apdu.lengthExpected = 4;

            pcscConnection.sendCommand(ref apdu);

            if (apdu.statusWord[0] != 0x90)
            {
                throw new CardException("Read value failed", apdu.statusWord);
            }


            return(Helper.byteToInt(apdu.response));
        }
Exemplo n.º 7
0
        public byte[] getAnswerToSelect()
        {
            apduCommand = new Apdu();
            apduCommand.setCommand(new byte[] { 0xFF,
                                                0xCA,
                                                0x01,
                                                0x00,
                                                0x00 });

            apduCommand.lengthExpected = 50;

            sendCommand();

            if (!apduCommand.statusWordEqualTo(new byte[] { 0x90, 0x00 }))
            {
                throw new CardException("Unable to get Answer to Select (ATS)", apduCommand.statusWord);
            }

            return(apduCommand.response);
        }
Exemplo n.º 8
0
        public void restoreAmount(byte sourceBlock, byte targetBlock)
        {
            Apdu apdu;

            apdu = new Apdu();
            apdu.lengthExpected = 2;

            apdu.setCommand(new byte[] { 0xFF, 0xD7, 0x00, sourceBlock, 0x02 });

            apdu.data    = new byte[2];
            apdu.data[0] = 0x03;
            apdu.data[1] = targetBlock;

            pcscConnection.sendCommand(ref apdu);

            if (apdu.statusWord[0] != 0x90)
            {
                throw new CardException("Restore value failed", apdu.statusWord);
            }
        }
Exemplo n.º 9
0
        public byte[] getCardSerialNumber()
        {
            byte[] cardSerial;

            apduCommand = new Apdu();
            apduCommand.setCommand(new byte[] { 0xFF,       //Intruction Class
                                                0xCA,       //Intruction Code
                                                0x00,       //Parameter 1
                                                0x00,       //Parameter 2
                                                0x00 });    //Parameter 3
            apduCommand.lengthExpected = 20;
            sendCommand();

            if (apduCommand.statusWord[0] != 0x90)
            {
                return(null);
            }

            cardSerial = new byte[apduCommand.response.Length];
            Array.Copy(apduCommand.response, cardSerial, cardSerial.Length);

            return(cardSerial);
        }