Beispiel #1
0
        /// <summary>
        /// Sends a service request to the smart card and expects to receive data back from the card.
        /// </summary>
        /// <param name="apdu">The SmartCard ADPU command and response object.</param>
        /// <returns>adpu</returns>
        public SCardAdpu Transmit(SCardAdpu apdu)
        {
            if (_hContext == IntPtr.Zero)
            {
                throw new SCardException("No card reader context established.  Must call SCard.EstablishContext() first.");
            }
            if (_hCard == IntPtr.Zero)
            {
                throw new SCardException("Not connected to card. Must call SCard.Connect() method first.");
            }

            byte[] send    = apdu.GetSendBuffer();
            byte[] recv    = new byte[apdu.GetReceiveBufferSize()];
            int    recvLen = recv.Length; // this is an in/out value

            SCARD_IO_REQUEST ioRequest = new SCARD_IO_REQUEST();

            ioRequest.dwProtocol  = (uint)_protocol;
            ioRequest.cbPciLength = 8;
            IntPtr pioRecvPci = IntPtr.Zero;

            Debug.WriteLine("SCardTransmit-ADPU-send: " + ArrayUtils.HexEncode(send));

            int rtn = SCardDll.SCardTransmit(_hCard, ref ioRequest, ref send[0], send.Length, pioRecvPci, ref recv[0], ref recvLen);

            EvalReturnCode(rtn);

            // resize the array if the bytes received are less than the buffer size
            if (recvLen < recv.Length)
            {
                Array.Resize <byte>(ref recv, (int)recvLen);
            }

            Debug.WriteLine("SCardTransmit-ADPU-recv: " + ArrayUtils.HexEncode(recv));

            // update the apdu object with the receive data
            apdu.SetReceiveData(recv);

            return(apdu);
        }
Beispiel #2
0
 //public static extern int SCardTransmit(IntPtr hCard, [In] ref SCard_IO_Request pioSendPci, [MarshalAs(UnmanagedType.LPArray)] byte[] pbSendBuffer, UInt32 cbSendLength, ref IntPtr pioRecvPci, [MarshalAs(UnmanagedType.LPArray)]  byte[] pbRecvBuffer, ref UInt32 pcbRecvLength);
 public static extern int SCardTransmit(IntPtr hCard, ref SCARD_IO_REQUEST pioSendPci, ref byte pbSendBuffer, int cbSendLength, IntPtr pioRecvPci, ref byte pbRecvBuffer, ref int pcbRecvLength);
Beispiel #3
0
 //public static extern int SCardTransmit(IntPtr hCard, [In] ref SCard_IO_Request pioSendPci, [MarshalAs(UnmanagedType.LPArray)] byte[] pbSendBuffer, UInt32 cbSendLength, ref IntPtr pioRecvPci, [MarshalAs(UnmanagedType.LPArray)]  byte[] pbRecvBuffer, ref UInt32 pcbRecvLength);
 public static extern int SCardTransmit(IntPtr hCard, ref SCARD_IO_REQUEST pioSendPci, ref byte pbSendBuffer, int cbSendLength, IntPtr pioRecvPci, ref byte pbRecvBuffer, ref int pcbRecvLength);
Beispiel #4
0
        /// <summary>
        /// Sends a service request to the smart card and expects to receive data back from the card.
        /// </summary>
        /// <param name="apdu">The SmartCard ADPU command and response object.</param>
        /// <returns>adpu</returns>
        public SCardAdpu Transmit(SCardAdpu apdu)
        {
            if (_hContext == IntPtr.Zero)
                throw new SCardException("No card reader context established.  Must call SCard.EstablishContext() first.");
            if (_hCard == IntPtr.Zero)
                throw new SCardException("Not connected to card. Must call SCard.Connect() method first.");

            byte[] send = apdu.GetSendBuffer();
            byte[] recv = new byte[apdu.GetReceiveBufferSize()];
            int recvLen = recv.Length;  // this is an in/out value

            SCARD_IO_REQUEST ioRequest = new SCARD_IO_REQUEST();
            ioRequest.dwProtocol = (uint)_protocol;
            ioRequest.cbPciLength = 8;
            IntPtr pioRecvPci = IntPtr.Zero;

            Debug.WriteLine("SCardTransmit-ADPU-send: " + ArrayUtils.HexEncode(send));

            int rtn = SCardDll.SCardTransmit(_hCard, ref ioRequest, ref send[0], send.Length, pioRecvPci, ref recv[0], ref recvLen);
            EvalReturnCode(rtn);

            // resize the array if the bytes received are less than the buffer size
            if (recvLen < recv.Length)
                Array.Resize<byte>(ref recv, (int)recvLen);

            Debug.WriteLine("SCardTransmit-ADPU-recv: " + ArrayUtils.HexEncode(recv));

            // update the apdu object with the receive data
            apdu.SetReceiveData(recv);

            return apdu;
        }