private byte[] SendCommand(byte[] command)
        {
            byte[] responseBuffer = new byte[256];

            error = reader.Transmit(intPtr, command, ref responseBuffer);
            HandleError();

            ResponseApdu responseApdu = new ResponseApdu(responseBuffer, IsoCase.Case2Short, reader.ActiveProtocol);

            if (responseApdu.SW1.Equals((byte)SW1Code.NormalDataResponse))
            {
                command        = apdu.GetResponse().Concat(new byte[] { responseApdu.SW2 }).ToArray();
                responseBuffer = new byte[258];

                error = reader.Transmit(intPtr, command, ref responseBuffer);
                HandleError();

                if (responseBuffer.Length - responseApdu.SW2 == 2)
                {
                    return(responseBuffer.Take(responseBuffer.Length - 2).ToArray());
                }
            }

            return(responseBuffer);
        }
Ejemplo n.º 2
0
        private ResponseApdu SimpleTransmit(byte[] commandApdu, int commandApduLength, IsoCase isoCase,
                                            SCardProtocol protocol, SCardPCI receivePci, ref byte[] receiveBuffer, ref int receiveBufferLength)
        {
            SCardError sc;
            var        cmdSent = false;

            do
            {
                // send Command APDU to the card
                sc = _reader.Transmit(
                    SCardPCI.GetPci(_reader.ActiveProtocol),
                    commandApdu,
                    commandApduLength,
                    receivePci,
                    receiveBuffer,
                    ref receiveBufferLength);

                // Do we need to resend the command APDU?
                if (sc == SCardError.InsufficientBuffer &&
                    receiveBuffer.Length < receiveBufferLength)
                {
                    // The response buffer was too small.
                    receiveBuffer = new byte[receiveBufferLength];

                    // Shall we wait until we re-send we APDU?
                    if (RetransmitWaitTime > 0)
                    {
                        Thread.Sleep(RetransmitWaitTime);
                    }
                }
                else
                {
                    cmdSent = true;
                }
            } while (cmdSent == false);

            if (sc == SCardError.Success)
            {
                return(new ResponseApdu(receiveBuffer, receiveBufferLength, isoCase, protocol));
            }

            // An error occurred, throw exception..
            ThrowExceptionOnSCardError(sc);
            return(null);
        }
Ejemplo n.º 3
0
        public IBinary Transmit(IBinary rawCommandApdu)
        {
            Debug.WriteLine("Read TreadID " + Thread.CurrentThread.ManagedThreadId);
            var receiveBuffer = new byte[1024 + _responseApduTrailerLength];
            var receivePci    = new SCardPCI();
            var sendPci       = SCardPCI.GetPci(SCardProtocol.T1);

            var sc = _reader.Transmit(
                sendPci,
                rawCommandApdu.Bytes(),
                receivePci,
                ref receiveBuffer
                );

            if (sc != SCardError.Success)
            {
                throw new Exception("Error: " + SCardHelper.StringifyError(sc));
            }
            return(new Binary(receiveBuffer));
        }
Ejemplo n.º 4
0
        private ResponseApdu SimpleTransmit(byte[] commandApdu, int commandApduLength, IsoCase isoCase,
                                            SCardProtocol protocol, SCardPCI receivePci, byte[] receiveBuffer, int receiveBufferLength)
        {
            SCardError sc;

            do
            {
                // send Command APDU to the card
                sc = _reader.Transmit(
                    SCardPCI.GetPci(_reader.ActiveProtocol),
                    commandApdu,
                    commandApduLength,
                    receivePci,
                    receiveBuffer,
                    ref receiveBufferLength);

                // Do we need to resend the command APDU?
                if (sc.HasInsufficientBuffer() && receiveBuffer.Length < receiveBufferLength)
                {
                    // The response buffer was too small.
                    receiveBuffer = new byte[receiveBufferLength];

                    // Shall we wait until we re-send we APDU?
                    if (RetransmitWaitTime > 0)
                    {
                        Thread.Sleep(RetransmitWaitTime);
                    }
                }
                else
                {
                    break;
                }
            } while (true);

            if (sc != SCardError.Success)
            {
                sc.Throw();
            }

            return(new ResponseApdu(receiveBuffer, receiveBufferLength, isoCase, protocol));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Transmits an <see cref="APDU"/> to the device and receives an <see cref="APDUResponse"/> back from it.
        /// </summary>
        /// <param name="apdu">APDU to send to the device</param>
        /// <param name="check">Status code to check for</param>
        /// <returns>Returns the parsed <see cref="APDUResponse"/> from the device response.</returns>
        /// <exception cref="UnexpectedResponseException">Thrown if write is unsuccessful or device returns a status code not matching <paramref name="check"/>.</exception>
        public APDUResponse SendAPDU(APDU apdu, APDUResponse.StatusWord?check = APDUResponse.StatusWord.SUCCESS)
        {
            if (apdu == null)
            {
                throw new ArgumentNullException();
            }

            byte[]     buffer   = new byte[256];
            SCardError writeres = Reader.Transmit(apdu.ToBytes(), ref buffer);

            if (writeres != SCardError.Success)
            {
                throw new UnexpectedResponseException("Device returned a non-success status code.");
            }

            APDUResponse res = new APDUResponse(buffer);

            if (check != null && check != res.SW)
            {
                throw new UnexpectedResponseException("Unexpected response from device.");
            }

            return(res);
        }
Ejemplo n.º 6
0
        public SCardError Transmit(IntPtr sendPci, byte[] sendBuffer, SCardPCI receivePci, ref byte[] receiveBuffer)
        {
            Debug.WriteLine("TreadID " + Thread.CurrentThread.ManagedThreadId);
            var sce = _reader.Transmit(
                sendPci,
                sendBuffer,
                receivePci,
                ref receiveBuffer
                );

            var claName = String.Empty;
            var mode    = "Unprotected";

            try
            {
                var claNamesDictionary = ((InstructionCode[])Enum.GetValues(typeof(InstructionCode)))
                                         .Zip(
                    Enum.GetNames(typeof(InstructionCode)),
                    (first, second) => new { Value = (int)first, Name = second }
                    )
                                         .ToDictionary((item) => item.Value, item => item.Name);

                var claValue = new Hex(new Binary(sendBuffer.Skip(1).Take(1).ToArray())).ToInt();
                claName = claNamesDictionary[claValue];
                if (sendBuffer.First() == 0x0C)
                {
                    mode = "Protected";
                }
            }
            catch (Exception ex)
            {
                Console.Write(
                    "\n{0} : {1}\n",
                    ex.Message,
                    new Hex(new Binary(sendBuffer.Skip(1).Take(1).ToArray()))
                    );
            }

            try
            {
                Console.WriteLine(
                    "\n{5}_{3}:\n CAPDU: {0}\n RAPDU: {2}\nSW1SW2: {4}\n    Le: {1}\n",
                    new Hex(
                        new Binary(sendBuffer)
                        ),
                    receiveBuffer.Length,
                    new Hex(
                        new Binary(receiveBuffer)
                        ),
                    claName,
                    new Hex(new Binary(receiveBuffer.Reverse().Take(2).Reverse())),
                    mode
                    );
            }
            catch (Exception e)
            {
                Console.WriteLine("Wrong Length for Hex");
            }

            return(sce);
        }