Example #1
0
        /// <summary>Initializes a new instance of the <see cref="ResponseApdu" /> class.</summary>
        /// <param name="response">The response as byte array that shall be parsed.</param>
        /// <param name="length">The size of the response.</param>
        /// <param name="isoCase">The ISO case that was used when sending the <see cref="CommandApdu" />.</param>
        /// <param name="protocol">The communication protocol.</param>
        /// <param name="copy">If <see langword="true" /> the bytes of the supplied response will be copied.</param>
        /// <exception cref="ArgumentOutOfRangeException">If <paramref name="length" /> is greater than the <paramref name="response" /> size.</exception>
        public ResponseApdu(byte[] response, int length, IsoCase isoCase, SCardProtocol protocol, bool copy)
        {
            if (length < 0 ||
                (response == null && length > 0) ||
                (response != null && response.Length < length))
            {
                throw new ArgumentOutOfRangeException(nameof(length));
            }

            if (copy)
            {
                if (response != null)
                {
                    FullApdu = new byte[length];
                    Array.Copy(response, FullApdu, length);
                }
            }
            else
            {
                FullApdu = response;
            }

            Length   = length;
            Case     = isoCase;
            Protocol = protocol;
        }
Example #2
0
        public ResponseApdu(byte[] response, int length, IsoCase isoCase, SCardProtocol proto, bool copy)
        {
            if (length < 0 ||
                (response == null && length > 0) ||
                (response.Length < length))
            {
                throw new ArgumentOutOfRangeException("length");
            }

            if (copy)
            {
                if (response != null)
                {
                    this.response = new byte[length];
                    Array.Copy(response, this.response, length);
                }
            }
            else
            {
                this.response = response;
            }
            this.length  = length;
            this.isocase = isoCase;
            this.proto   = proto;
        }
Example #3
0
        public ResponseApdu(byte[] response, IsoCase isoCase, SCardProtocol proto)
        {
            this.response = response;
            if (response != null)
                length = response.Length;

            this.isocase = isoCase;
            this.proto = proto;
        }
Example #4
0
        /// <summary>Initializes a new instance of the <see cref="ResponseApdu" /> class.</summary>
        /// <param name="response">The response as byte array that shall be parsed.</param>
        /// <param name="isoCase">The ISO case that was used when sending the <see cref="CommandApdu" />.</param>
        /// <param name="protocol">The communication protocol.</param>
        public ResponseApdu(byte[] response, IsoCase isoCase, SCardProtocol protocol) {
            _response = response;

            if (response != null) {
                _length = response.Length;
            }

            Case = isoCase;
            Protocol = protocol;
        }
Example #5
0
        public ResponseApdu(byte[] response, IsoCase isoCase, SCardProtocol proto)
        {
            this.response = response;
            if (response != null)
            {
                length = response.Length;
            }

            this.isocase = isoCase;
            this.proto   = proto;
        }
        public ResponseApdu(byte[] response, int length, IsoCase isoCase, SCardProtocol proto)
        {
            if (length < 0 ||
                (response == null && length > 0) ||
                (response.Length < length))
                throw new ArgumentOutOfRangeException("length");

            this.response = response;
            this.length = length;
            this.isocase = isoCase;
            this.proto = proto;
        }
Example #7
0
        /// <summary>Initializes a new instance of the <see cref="ResponseApdu" /> class.</summary>
        /// <param name="response">The response as byte array that shall be parsed.</param>
        /// <param name="length">The size of the response.</param>
        /// <param name="isoCase">The ISO case that was used when sending the <see cref="CommandApdu" />.</param>
        /// <param name="protocol">The communication protocol.</param>
        /// <exception cref="ArgumentOutOfRangeException">If <paramref name="length" /> is greater than the <paramref name="response" /> size.</exception>
        public ResponseApdu(byte[] response, int length, IsoCase isoCase, SCardProtocol protocol) {
            if (length < 0 ||
                (response == null && length > 0) ||
                (response != null && response.Length < length)) {
                throw new ArgumentOutOfRangeException("length");
            }

            _response = response;
            _length = length;
            Case = isoCase;
            Protocol = protocol;
        }
Example #8
0
        /// <summary>Initializes a new instance of the <see cref="ResponseApdu" /> class.</summary>
        /// <param name="response">The response as byte array that shall be parsed.</param>
        /// <param name="isoCase">The ISO case that was used when sending the <see cref="CommandApdu" />.</param>
        /// <param name="protocol">The communication protocol.</param>
        public ResponseApdu(byte[] response, IsoCase isoCase, SCardProtocol protocol)
        {
            FullApdu = response;

            if (response != null)
            {
                Length = response.Length;
            }

            Case     = isoCase;
            Protocol = protocol;
        }
Example #9
0
        public ResponseApdu(byte[] response, int length, IsoCase isoCase, SCardProtocol proto)
        {
            if (length < 0 ||
                (response == null && length > 0) ||
                (response.Length < length))
            {
                throw new ArgumentOutOfRangeException("length");
            }

            this.response = response;
            this.length   = length;
            this.isocase  = isoCase;
            this.proto    = proto;
        }
Example #10
0
        /// <summary>Initializes a new instance of the <see cref="ResponseApdu" /> class.</summary>
        /// <param name="response">The response as byte array that shall be parsed.</param>
        /// <param name="length">The size of the response.</param>
        /// <param name="isoCase">The ISO case that was used when sending the <see cref="CommandApdu" />.</param>
        /// <param name="protocol">The communication protocol.</param>
        /// <exception cref="ArgumentOutOfRangeException">If <paramref name="length" /> is greater than the <paramref name="response" /> size.</exception>
        public ResponseApdu(byte[] response, int length, IsoCase isoCase, SCardProtocol protocol)
        {
            if (length < 0 ||
                (response == null && length > 0) ||
                (response != null && response.Length < length))
            {
                throw new ArgumentOutOfRangeException(nameof(length));
            }

            FullApdu = response;
            Length   = length;
            Case     = isoCase;
            Protocol = protocol;
        }
Example #11
0
        /// <summary>Initializes a new instance of the <see cref="ResponseApdu" /> class.</summary>
        /// <param name="response">The response as byte array that shall be parsed.</param>
        /// <param name="isoCase">The ISO case that was used when sending the <see cref="CommandApdu" />.</param>
        /// <param name="protocol">The communication protocol.</param>
        /// <param name="copy">If <see langword="true" /> the bytes of the supplied response will be copied.</param>
        public ResponseApdu(byte[] response, IsoCase isoCase, SCardProtocol protocol, bool copy) {
            Case = isoCase;
            Protocol = protocol;

            if (response == null) {
                return;
            }

            if (copy) {
                Length = response.Length;
                FullApdu = new byte[Length];
                Array.Copy(response, FullApdu, Length);
            } else {
                FullApdu = response;
                Length = response.Length;
            }
        }
Example #12
0
        /// <summary>Initializes a new instance of the <see cref="ResponseApdu" /> class.</summary>
        /// <param name="response">The response as byte array that shall be parsed.</param>
        /// <param name="isoCase">The ISO case that was used when sending the <see cref="CommandApdu" />.</param>
        /// <param name="protocol">The communication protocol.</param>
        /// <param name="copy">If <see langword="true" /> the bytes of the supplied response will be copied.</param>
        public ResponseApdu(byte[] response, IsoCase isoCase, SCardProtocol protocol, bool copy) {
            Case = isoCase;
            Protocol = protocol;

            if (response == null) {
                return;
            }

            if (copy) {
                _length = response.Length;
                _response = new byte[_length];
                Array.Copy(response, _response, _length);
            } else {
                _response = response;
                _length = response.Length;
            }
        }
Example #13
0
        private ResponseApdu _SimpleTransmit(byte[] cmdApdu, int cmdApduLength, IsoCase isoCase, SCardProtocol proto, SCardPCI recvPci, ref byte[] recvBuf, ref int recvBufLength)
        {
            SCardError sc      = SCardError.UnknownError;
            bool       cmdSent = false;

            do
            {
                // send Command APDU to the card
                sc = reader.Transmit(
                    SCardPCI.GetPci(reader.ActiveProtocol),
                    cmdApdu,
                    cmdApduLength,
                    recvPci,
                    recvBuf,
                    ref recvBufLength);

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

                    // 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)
            {
                ResponseApdu respApdu = new ResponseApdu(recvBuf, recvBufLength, isoCase, proto);
                return(respApdu);
            }

            // An error occurred, throw exception..
            ThrowExceptionOnSCardError(sc);
            return(null);
        }
Example #14
0
        /// <summary>Initializes a new instance of the <see cref="ResponseApdu" /> class.</summary>
        /// <param name="response">The response as byte array that shall be parsed.</param>
        /// <param name="length">The size of the response.</param>
        /// <param name="isoCase">The ISO case that was used when sending the <see cref="CommandApdu" />.</param>
        /// <param name="protocol">The communication protocol.</param>
        /// <param name="copy">If <see langword="true" /> the bytes of the supplied response will be copied.</param>
        /// <exception cref="ArgumentOutOfRangeException">If <paramref name="length" /> is greater than the <paramref name="response" /> size.</exception>
        public ResponseApdu(byte[] response, int length, IsoCase isoCase, SCardProtocol protocol, bool copy) {
            if (length < 0 ||
                (response == null && length > 0) ||
                (response != null && response.Length < length)) {
                throw new ArgumentOutOfRangeException("length");
            }

            if (copy) {
                if (response != null) {
                    _response = new byte[length];
                    Array.Copy(response, _response, length);
                }
            } else {
                _response = response;
            }
            _length = length;
            Case = isoCase;
            Protocol = protocol;
        }
Example #15
0
        public ResponseApdu(byte[] response, IsoCase isoCase, SCardProtocol proto, bool copy)
        {
            this.isocase = isoCase;
            this.proto = proto;

            if (response == null)
                return;
            if (copy)
            {
                length = response.Length;
                this.response = new byte[length];
                Array.Copy(response, this.response, length);
            }
            else
            {
                this.response = response;
                length = response.Length;
            }
        }
Example #16
0
        public ResponseApdu(byte[] response, IsoCase isoCase, SCardProtocol proto, bool copy)
        {
            this.isocase = isoCase;
            this.proto   = proto;

            if (response == null)
            {
                return;
            }
            if (copy)
            {
                length        = response.Length;
                this.response = new byte[length];
                Array.Copy(response, this.response, length);
            }
            else
            {
                this.response = response;
                length        = response.Length;
            }
        }
Example #17
0
        /// <summary>Initializes a new instance of the <see cref="ResponseApdu" /> class.</summary>
        /// <param name="response">The response as byte array that shall be parsed.</param>
        /// <param name="isoCase">The ISO case that was used when sending the <see cref="CommandApdu" />.</param>
        /// <param name="protocol">The communication protocol.</param>
        /// <param name="copy">If <see langword="true" /> the bytes of the supplied response will be copied.</param>
        public ResponseApdu(byte[] response, IsoCase isoCase, SCardProtocol protocol, bool copy)
        {
            Case     = isoCase;
            Protocol = protocol;

            if (response == null)
            {
                return;
            }

            if (copy)
            {
                _length   = response.Length;
                _response = new byte[_length];
                Array.Copy(response, _response, _length);
            }
            else
            {
                _response = response;
                _length   = response.Length;
            }
        }
Example #18
0
        /// <summary>Initializes a new instance of the <see cref="ResponseApdu" /> class.</summary>
        /// <param name="response">The response as byte array that shall be parsed.</param>
        /// <param name="isoCase">The ISO case that was used when sending the <see cref="CommandApdu" />.</param>
        /// <param name="protocol">The communication protocol.</param>
        /// <param name="copy">If <see langword="true" /> the bytes of the supplied response will be copied.</param>
        public ResponseApdu(byte[] response, IsoCase isoCase, SCardProtocol protocol, bool copy)
        {
            Case     = isoCase;
            Protocol = protocol;

            if (response == null)
            {
                return;
            }

            if (copy)
            {
                Length   = response.Length;
                FullApdu = new byte[Length];
                Array.Copy(response, FullApdu, Length);
            }
            else
            {
                FullApdu = response;
                Length   = response.Length;
            }
        }
        private static byte[] sendRequest(SCardReader rfidReader, SCardError sc, SCardPCI receivePci,
                                          IntPtr sendPci, CommandApdu apdu, ResponseApdu responseApdu, IsoCase isoCase)
        {
            sc = rfidReader.BeginTransaction();
            if (sc != SCardError.Success)
            {
                return(null);
            }

            receivePci = new SCardPCI();
            sendPci    = SCardPCI.GetPci(rfidReader.ActiveProtocol);

            var receiveBuffer = new byte[256];
            var command       = apdu.ToArray();

            sc = rfidReader.Transmit(
                sendPci,
                command,
                receivePci,
                ref receiveBuffer);

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

            responseApdu = new ResponseApdu(receiveBuffer, isoCase, rfidReader.ActiveProtocol);

            if (!responseApdu.HasData)
            {
                return(null);
            }

            return((byte[])responseApdu.GetData());
        }
Example #20
0
 /// <summary>Initializes a new instance of the <see cref="CommandApdu" /> class.</summary>
 /// <param name="isoCase">The ISO case to use.</param>
 /// <param name="protocol">The protocol.</param>
 public CommandApdu(IsoCase isoCase, SCardProtocol protocol) {
     Case = isoCase;
     Protocol = protocol;
 }
Example #21
0
        public ResponseApdu(byte[] response, int length, IsoCase isoCase, SCardProtocol proto, bool copy)
        {
            if (length < 0 ||
                (response == null && length > 0) ||
                (response.Length < length))
                throw new ArgumentOutOfRangeException("length");

            if (copy)
            {
                if (response != null)
                {
                    this.response = new byte[length];
                    Array.Copy(response, this.response, length);
                }
            }
            else
            {
                this.response = response;
            }
            this.length = length;
            this.isocase = isoCase;
            this.proto = proto;
        }
Example #22
0
 public CommandApdu ConstructCommandApdu(IsoCase isoCase)
 {
     return new CommandApdu(isoCase, ActiveProtocol);
 }
Example #23
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..
		    sc.Throw();
		    return null;
        }
Example #24
0
 public CommandApdu(IsoCase isocase, SCardProtocol protocol)
 {
     this.isocase = isocase;
     this.proto   = protocol;
 }
Example #25
0
 /// <summary>Constructs a command APDU using the active protocol of the reader.</summary>
 /// <param name="isoCase">The ISO case that shall be used for this command.</param>
 /// <returns>An empty command APDU.</returns>
 public virtual CommandApdu ConstructCommandApdu(IsoCase isoCase)
 {
     return(new CommandApdu(isoCase, ActiveProtocol));
 }
Example #26
0
        private ResponseApdu _SimpleTransmit(byte[] cmdApdu, int cmdApduLength, IsoCase isoCase, SCardProtocol proto, SCardPCI recvPci, ref byte[] recvBuf, ref int recvBufLength)
        {
            SCardError sc = SCardError.UnknownError;
            bool cmdSent = false;

            do
            {
                // send Command APDU to the card
                sc = reader.Transmit(
                    SCardPCI.GetPci(reader.ActiveProtocol),
                    cmdApdu,
                    cmdApduLength,
                    recvPci,
                    recvBuf,
                    ref recvBufLength);

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

                    // 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)
            {
                ResponseApdu respApdu = new ResponseApdu(recvBuf, recvBufLength, isoCase, proto);
                return respApdu;
            }

            // An error occurred, throw exception..
            ThrowExceptionOnSCardError(sc);
            return null;
        }
Example #27
0
            public CommandApdu InitApdu(bool hasData)
            {
                IsoCase commandCase = hasData ? IsoCase.Case4Short : IsoCase.Case2Short;

                return(new CommandApdu(commandCase, Reader.ActiveProtocol));
            }
Example #28
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));
        }
Example #29
0
 /// <summary>Initializes a new instance of the <see cref="CommandApdu" /> class.</summary>
 /// <param name="isoCase">The ISO case to use.</param>
 /// <param name="protocol">The protocol.</param>
 public CommandApdu(IsoCase isoCase, SCardProtocol protocol)
 {
     Case     = isoCase;
     Protocol = protocol;
 }
Example #30
0
 public CommandApdu(IsoCase isocase, SCardProtocol protocol)
 {
     this.isocase = isocase;
     this.proto = protocol;
 }