Esempio n. 1
0
        public static async Task <byte[]> TransmitApduAsync(SmartCardConnection connection, byte[] ApduCommand)
        {
            IBuffer command  = CryptographicBuffer.CreateFromByteArray(ApduCommand);
            IBuffer response = await connection.TransmitAsync(command);

            return(response.ToArray());
        }
Esempio n. 2
0
        private async Task <byte[]> ReadFile(byte lenght)
        {
            var result = await connection.TransmitAsync(Command.GetReadFileCommand(lenght).AsBuffer());

            CryptographicBuffer.CopyToByteArray(result, out byte[] readResponse);

            if (readResponse?.Length == 2 &&
                readResponse[0] == StatusCode.READ_FILE_LE_INCORRECT)
            {
                //Getting back 0x6C, Length is in second byte
                return(await ReadFile(readResponse[1]));
            }
            else
            {
                return(readResponse);
            }
        }
Esempio n. 3
0
        /// <summary>
        ///     Extension method to SmartCardConnection class similar to Transmit asyc method, however it accepts PCSC SDK
        ///     commands.
        /// </summary>
        /// <param name="apduCommand">
        ///     APDU command object to send to the ICC
        /// </param>
        /// <param name="connection">
        ///     SmartCardConnection object
        /// </param>
        /// <returns>APDU response object of type defined by the APDU command object</returns>
        public static async Task <Iso7816.ApduResponse> TransceiveAsync(this SmartCardConnection connection, ApduCommand apduCommand)
        {
            var apduRes = (Iso7816.ApduResponse)Activator.CreateInstance(apduCommand.ApduResponseType);

            var responseBuf = await connection.TransmitAsync(apduCommand.GetBuffer().AsBuffer());

            apduRes.ExtractResponse(responseBuf.ToArray());

            return(apduRes);
        }
Esempio n. 4
0
        /// <summary>
        /// Extension method to SmartCardConnection class similar to Transmit asyc method, however it accepts PCSC SDK commands.
        /// </summary>
        /// <param name="apduCommand">
        /// APDU command object to send to the ICC
        /// </param>
        /// <param name="connection">
        /// SmartCardConnection object
        /// </param>
        /// <returns>APDU response object of type defined by the APDU command object</returns>
        public static async Task <Iso7816.ApduResponse> TransceiveAsyncOriginal(this SmartCardConnection connection, Iso7816.ApduCommand apduCommand)
        {
            Iso7816.ApduResponse apduRes = Activator.CreateInstance(apduCommand.ApduResponseType) as Iso7816.ApduResponse;

            IBuffer responseBuf = await connection.TransmitAsync(apduCommand.GetBuffer());

            apduRes.ExtractResponse(responseBuf);

            return(apduRes);
        }
Esempio n. 5
0
        public static async Task <Iso7816.ApduResponse> TransceiveAsync(this SmartCardConnection connection, Iso7816.ApduCommand apduCommand)
        {
            Iso7816.ApduResponse apduRes = Activator.CreateInstance(apduCommand.ApduResponseType) as Iso7816.ApduResponse;

            IBuffer cmdBuffer = apduCommand.GetBuffer();

            DeviceServerApp.Logger.Information("Transmitting <" + cmdBuffer.ToString() + ">");
            IBuffer responseBuf = await connection.TransmitAsync(cmdBuffer);   // <- throws exception (0x80004004 (E_ABORT))

            apduRes.ExtractResponse(responseBuf);

            return(apduRes);
        } // TransceiveAsync
Esempio n. 6
0
        /// <summary>
        /// Extension method to SmartCardConnection class similar to Transmit asyc method, however it accepts PCSC SDK commands.
        /// </summary>
        /// <param name="apduCommand">
        /// APDU command object to send to the ICC
        /// </param>
        /// <param name="connection">
        /// SmartCardConnection object
        /// </param>
        /// <returns>APDU response object of type defined by the APDU command object</returns>
        internal static IAsyncOperation <Iso7816.ApduResponse> TransceiveAsync(this SmartCardConnection connection, Iso7816.ApduCommand apduCommand)
        {
            return(AsyncInfo.Run(async(cancel) =>
            {
                Iso7816.ApduResponse apduRes = Activator.CreateInstance(apduCommand.ApduResponseType) as Iso7816.ApduResponse;

                IBuffer responseBuf = await connection.TransmitAsync(apduCommand.GetBuffer());

                apduRes.ExtractResponse(responseBuf);

                return apduRes;
            }));
        }
Esempio n. 7
0
        private static async Task <byte[]> Get7ByteCardUID(CardAddedEventArgs args)
        {
            byte[] byteReadUID = new byte[] { (byte)0xFF, (byte)0xCA,
                                              (byte)0x00, (byte)0x00, (byte)0x00 };

            SmartCardConnection cardConnection = await args.SmartCard.ConnectAsync();

            IBuffer bufferResultUID = await cardConnection.TransmitAsync(byteReadUID.AsBuffer());

            CryptographicBuffer.CopyToByteArray(bufferResultUID, out byte[] byteResultUID);

            return(byteResultUID);
        }
        /// <summary>
        /// Reads the travel card data from HSL Mifare DESFire card.
        /// </summary>
        /// <param name="card">The card to try to read.</param>
        /// <returns>A deserialized Travel Card object if the card was valid, otherwise null.</returns>
        public static async Task <TravelCard> ReadTravelCardAsync(SmartCard card)
        {
            using (SmartCardConnection connection = await card.ConnectAsync())
            {
                byte[] selection = (await connection.TransmitAsync(HslCommands.SelectHslCommand.AsBuffer())).ToArray();
                if (selection != null &&
                    selection.Length > 0 &&
                    selection.SequenceEqual(HslCommands.OkResponse))
                {
                    // Travel card info bytes
                    byte[] appInfo     = null;
                    byte[] periodPass  = null;
                    byte[] storedValue = null;
                    byte[] eTicket     = null;
                    byte[] history     = null;

                    // Temporary containers for history chunks
                    byte[] hist1 = new byte[2];
                    byte[] hist2 = new byte[2];

                    appInfo     = (await connection.TransmitAsync(HslCommands.ReadAppInfoCommand.AsBuffer())).ToArray();
                    periodPass  = (await connection.TransmitAsync(HslCommands.ReadPeriodPassCommand.AsBuffer())).ToArray();
                    storedValue = (await connection.TransmitAsync(HslCommands.ReadStoredValueCommand.AsBuffer())).ToArray();
                    eTicket     = (await connection.TransmitAsync(HslCommands.ReadETicketCommand.AsBuffer())).ToArray();
                    hist1       = (await connection.TransmitAsync(HslCommands.ReadHistoryCommand.AsBuffer())).ToArray();

                    // If we have more history, the last two bytes of the history array will contain the MORE_DATA bytes.
                    if (hist1.Skip(Math.Max(0, hist1.Length - 2)).ToArray() == HslCommands.MoreData)
                    {
                        hist2 = (await connection.TransmitAsync(HslCommands.ReadNextCommand.AsBuffer())).ToArray();
                    }

                    // Combine the two history chunks into a single array, minus their last two MORE_DATA bytes
                    history = hist1.Take(hist1.Length - 2)
                              .Concat(hist2.Take(hist2.Length - 2)).ToArray();

                    var rawCard = new RawTravelCard(appInfo, periodPass, storedValue, eTicket, history);
                    if (rawCard.Status == CardStatus.HslCardDataFailure)
                    {
                        return(null);
                    }
                    else
                    {
                        return(new TravelCard(rawCard));
                    }
                }
                else
                {
                    return(null);
                }
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Click handler for the 'TransmitAPDU' button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void Transmit_Click(object sender, RoutedEventArgs e)
        {
            if (!rootPage.ValidateTPMSmartCard())
            {
                rootPage.NotifyUser("Use Scenario One to create a TPM virtual smart card.", NotifyType.ErrorMessage);
                return;
            }

            Button b = sender as Button;

            b.IsEnabled = false;

            try
            {
                SmartCard card = await rootPage.GetSmartCard();

                IBuffer result = null;

                using (SmartCardConnection connection = await card.ConnectAsync())
                {
                    // Read EF.ATR file
                    // The command is meant specifically for GIDS cards
                    // (such as TPM VSCs), and will fail on other types.
                    byte[] readEfAtrBytes = { 0x00, 0xCB, 0x2F, 0x01, 0x02, 0x5C, 0x00, 0xFF };

                    IBuffer readEfAtr = CryptographicBuffer.CreateFromByteArray(readEfAtrBytes);

                    result = await connection.TransmitAsync(readEfAtr);

                    rootPage.NotifyUser("Response: " + CryptographicBuffer.EncodeToHexString(result), NotifyType.StatusMessage);
                }
            }
            catch (Exception ex)
            {
                rootPage.NotifyUser("Transmitting APDU to card failed with exception: " + ex.ToString(), NotifyType.ErrorMessage);
            }
            finally
            {
                b.IsEnabled = true;
            }
        }
Esempio n. 10
0
        public async Task <byte[]> TransmitAsync(byte[] inputData)
        {
            IBuffer responseBuf;

            byte[] output;
            using (DataWriter writer = new DataWriter())
            {
                writer.WriteBytes(inputData);
                try
                {
                    responseBuf = await connection.TransmitAsync(writer.DetachBuffer());
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            using (DataReader reader = DataReader.FromBuffer(responseBuf))
            {
                output = new byte[responseBuf.Length];
                reader.ReadBytes(output);
            }
            return(output);
        }
Esempio n. 11
0
 public async Task <byte[]> Transcieve(byte[] buffer)
 {
     return((await _backingConnection.TransmitAsync(buffer.AsBuffer())).ToArray());
 }