Exemple #1
0
 internal static extern int     SCardTransmit(UInt32 hCard,
                                              [In] ref SCard_IO_Request pioSendPci,
                                              byte[] pbSendBuffer,
                                              UInt32 cbSendLength,
                                              IntPtr pioRecvPci,
                                              [Out] byte[] pbRecvBuffer,
                                              out UInt32 pcbRecvLength
                                              );
Exemple #2
0
        /// <summary>
        /// Wraps the PCSC function
        /// LONG SCardTransmit(
        ///		SCARDHANDLE hCard,
        ///		LPCSCARD_I0_REQUEST pioSendPci,
        ///		LPCBYTE pbSendBuffer,
        ///		DWORD cbSendLength,
        ///		LPSCARD_IO_REQUEST pioRecvPci,
        ///		LPBYTE pbRecvBuffer,
        ///		LPDWORD pcbRecvLength
        ///	);
        /// </summary>
        /// <param name="ApduCmd">APDUCommand object with the APDU to send to the card</param>
        /// <returns>An APDUResponse object with the response from the card</returns>
        public override APDUResponse Transmit(APDUCommand ApduCmd)
        {
            uint RecvLength = (uint)(ApduCmd.Le + APDUResponse.SW_LENGTH);

            byte[]           ApduBuffer   = null;
            byte[]           ApduResponse = new byte[ApduCmd.Le + APDUResponse.SW_LENGTH];
            SCard_IO_Request ioRequest    = new SCard_IO_Request();

            ioRequest.m_dwProtocol  = m_nProtocol;
            ioRequest.m_cbPciLength = 8;

            // Build the command APDU
            if (ApduCmd.Data == null)
            {
                ApduBuffer = new byte[APDUCommand.APDU_MIN_LENGTH + ((ApduCmd.Le != 0) ? 1 : 0)];

                if (ApduCmd.Le != 0)
                {
                    ApduBuffer[4] = (byte)ApduCmd.Le;
                }
            }
            else
            {
                ApduBuffer = new byte[APDUCommand.APDU_MIN_LENGTH + 1 + ApduCmd.Data.Length];

                for (int nI = 0; nI < ApduCmd.Data.Length; nI++)
                {
                    ApduBuffer[APDUCommand.APDU_MIN_LENGTH + 1 + nI] = ApduCmd.Data[nI];
                }

                ApduBuffer[APDUCommand.APDU_MIN_LENGTH] = (byte)ApduCmd.Data.Length;
            }

            ApduBuffer[0] = ApduCmd.Class;
            ApduBuffer[1] = ApduCmd.Ins;
            ApduBuffer[2] = ApduCmd.P1;
            ApduBuffer[3] = ApduCmd.P2;

            m_nLastError = SCardTransmit(m_hCard, ref ioRequest, ApduBuffer, (uint)ApduBuffer.Length, IntPtr.Zero, ApduResponse, out RecvLength);
            if (m_nLastError != 0)
            {
                string msg = "SCardTransmit error: " + new Win32Exception(m_nLastError).Message;
                throw new Exception(msg);
            }

            byte[] ApduData = new byte[RecvLength];

            for (int nI = 0; nI < RecvLength; nI++)
            {
                ApduData[nI] = ApduResponse[nI];
            }

            return(new APDUResponse(ApduData));
        }
Exemple #3
0
        public byte[] TransmitRaw(byte[] ApduBuffer, byte lengthExpected)
        {
            var ioRequest =
                new SCard_IO_Request
            {
                m_dwProtocol  = m_nProtocol,
                m_cbPciLength = 8
            };

            return(TransmitRaw(ApduBuffer, lengthExpected, ioRequest));
        }
Exemple #4
0
        private byte[] TransmitRaw(byte[] ApduBuffer, byte lengthExpected, SCard_IO_Request ioRequest)
        {
            uint RecvLength = (uint)(lengthExpected + APDUResponse.SW_LENGTH);

            byte[] ApduResponse = new byte[lengthExpected + APDUResponse.SW_LENGTH];

            //Debug send
            Console.Write("Sending: ");
            foreach (var b in ApduBuffer)
            {
                Console.Write(String.Format("{0:X},", b));
            }
            Console.WriteLine();

            m_nLastError = NativeMethods.SCardTransmit(
                m_hCard,
                ref ioRequest,
                ApduBuffer,
                (uint)ApduBuffer.Length,
                IntPtr.Zero,
                ApduResponse,
                out RecvLength);

            if (m_nLastError != 0)
            {
                string msg = "SCardTransmit error: " + m_nLastError;
                throw new Exception(msg);
            }

            byte[] ApduData = new byte[RecvLength];

            for (int nI = 0; nI < RecvLength; nI++)
            {
                ApduData[nI] = ApduResponse[nI];
            }

            //Debug rec
            Console.Write("Received: ");
            foreach (var b in ApduData)
            {
                Console.Write(String.Format("{0:X},", b));
            }
            Console.WriteLine();


            return(ApduData);
        }
        /// <summary>
        /// Wraps the PCSC function
        /// LONG SCardTransmit(
        ///		SCARDHANDLE hCard,
        ///		LPCSCARD_I0_REQUEST pioSendPci,
        ///		LPCBYTE pbSendBuffer,
        ///		DWORD cbSendLength,
        ///		LPSCARD_IO_REQUEST pioRecvPci,
        ///		LPBYTE pbRecvBuffer,
        ///		LPDWORD pcbRecvLength
        ///	);
        /// </summary>
        /// <param name="ApduCmd">APDUCommand object with the APDU to send to the card</param>
        /// <returns>An APDUResponse object with the response from the card</returns>
        public override APDUResponse Transmit(APDUCommand ApduCmd)
        {
            uint	RecvLength = (uint) (ApduCmd.Le + APDUResponse.SW_LENGTH);
            byte[]	ApduBuffer = null;
            byte[]	ApduResponse = new byte[ApduCmd.Le + APDUResponse.SW_LENGTH];
            SCard_IO_Request	ioRequest = new SCard_IO_Request();
            ioRequest.m_dwProtocol = m_nProtocol;
            ioRequest.m_cbPciLength = 8;

            // Build the command APDU
            if (ApduCmd.Data == null)
            {
                ApduBuffer = new byte[APDUCommand.APDU_MIN_LENGTH + ((ApduCmd.Le != 0) ? 1 : 0)];

                if (ApduCmd.Le != 0)
                    ApduBuffer[4] = (byte) ApduCmd.Le;
            }
            else
            {
                ApduBuffer = new byte[APDUCommand.APDU_MIN_LENGTH + 1 + ApduCmd.Data.Length];

                for (int nI = 0; nI < ApduCmd.Data.Length; nI++)
                    ApduBuffer[APDUCommand.APDU_MIN_LENGTH + 1 + nI] = ApduCmd.Data[nI];

                ApduBuffer[APDUCommand.APDU_MIN_LENGTH] = (byte) ApduCmd.Data.Length;
            }

            ApduBuffer[0] = ApduCmd.Class;
            ApduBuffer[1] = ApduCmd.Ins;
            ApduBuffer[2] = ApduCmd.P1;
            ApduBuffer[3] = ApduCmd.P2;

            m_nLastError = SCardTransmit(m_hCard, ref ioRequest, ApduBuffer, (uint) ApduBuffer.Length, IntPtr.Zero, ApduResponse, out RecvLength);
            if (m_nLastError != 0)
            {
                string msg = "SCardTransmit error: " + m_nLastError;
                throw new Exception(msg);
            }

            byte[] ApduData = new byte[RecvLength];

            for (int nI = 0; nI < RecvLength; nI++)
                ApduData[nI] = ApduResponse[nI];

            return new APDUResponse(ApduData);
        }