/// <summary> /// class constructor. /// </summary> /// <param name="card"> /// smart card object /// </param> /// <param name="connection"> /// connection object to the smard card /// </param> public IccDetection(SmartCard card, SmartCardConnection connection) { smartCard = card; connectionObject = connection; PcscDeviceClass = DeviceClass.Unknown; PcscCardName = Pcsc.CardName.Unknown; }
public static Stream Select(SmartCard card, SelectType selectType, byte[] file, SelectFileOccurrence fileOccurrence, SelectFileControlInformation fileControlInformation, int expectedLength) { Command cmd = new Command(); cmd.Instruction = 0xA4; cmd.Parameter1 = (byte)selectType; cmd.Parameter2 = (byte)((byte)fileOccurrence | (byte)fileControlInformation); cmd.Data = file; cmd.ResponseLength = expectedLength; byte[] command = cmd.ToArray(); byte[] buffer = new byte[256]; //Console.Write("Sending command {0}...", BitConverter.ToString(command)); int len = card.Transmit(command, buffer); //Console.WriteLine(BitConverter.ToString(buffer, 0, len)); CommandStatus status = GetStatus(buffer, len); if (!status.IsNormal()) throw status.ToException(); Stream result = new MemoryStream(); result.Write(buffer, 0, len - 2); while (status.Sw1 == 0x61) { throw new NotImplementedException("Reading of remaining length not implemented"); } return result; }
public static Stream ReadBinary(SmartCard card, int offset, int expectedLength) { if ((offset < 0) || (offset > 0x7FFF)) throw new ArgumentOutOfRangeException("offset"); Command cmd = new Command(); cmd.Instruction = 0xB0; cmd.Parameter1 = (byte)((offset >> 8) & 0x7F); cmd.Parameter2 = (byte)offset; cmd.ResponseLength = expectedLength; byte[] command = cmd.ToArray(); byte[] buffer = new byte[expectedLength + 2]; //Console.Write("Sending command {0}...", BitConverter.ToString(command)); int len = card.Transmit(command, buffer); //Console.WriteLine(BitConverter.ToString(buffer, 0, len)); CommandStatus status = GetStatus(buffer, len); if (!status.IsNormal()) throw status.ToException(); Stream result = new MemoryStream(); result.Write(buffer, 0, len - 2); while (status.Sw1 == 0x61) { throw new NotImplementedException("Reading of remaining length not implemented"); } result.Position = 0; return result; }
/// <summary> /// Sample code to hande a couple of different cards based on the identification process /// </summary> /// <returns>None</returns> private async Task HandleCard(SmartCard card) { try { // Clear the messages MainPage.Current.NotifyUser(String.Empty, NotifyType.StatusMessage, true); // Connect to the card using (SmartCardConnection connection = await card.ConnectAsync()) { // Try to identify what type of card it was IccDetection cardIdentification = new IccDetection(card, connection); await cardIdentification.DetectCardTypeAync(); LogMessage("Connected to card\r\nPC/SC device class: " + cardIdentification.PcscDeviceClass.ToString()); LogMessage("Card name: " + cardIdentification.PcscCardName.ToString()); LogMessage("ATR: " + BitConverter.ToString(cardIdentification.Atr)); if ((cardIdentification.PcscDeviceClass == Pcsc.Common.DeviceClass.StorageClass) && (cardIdentification.PcscCardName == Pcsc.CardName.MifareUltralightC || cardIdentification.PcscCardName == Pcsc.CardName.MifareUltralight || cardIdentification.PcscCardName == Pcsc.CardName.MifareUltralightEV1)) { // Handle MIFARE Ultralight MifareUltralight.AccessHandler mifareULAccess = new MifareUltralight.AccessHandler(connection); // Each read should get us 16 bytes/4 blocks, so doing // 4 reads will get us all 64 bytes/16 blocks on the card for (byte i = 0; i < 4; i++) { byte[] response = await mifareULAccess.ReadAsync((byte)(4 * i)); LogMessage("Block " + (4 * i).ToString() + " to Block " + (4 * i + 3).ToString() + " " + BitConverter.ToString(response)); } byte[] responseUid = await mifareULAccess.GetUidAsync(); LogMessage("UID: " + BitConverter.ToString(responseUid)); } else if (cardIdentification.PcscDeviceClass == Pcsc.Common.DeviceClass.MifareDesfire) { // Handle MIFARE DESfire Desfire.AccessHandler desfireAccess = new Desfire.AccessHandler(connection); Desfire.CardDetails desfire = await desfireAccess.ReadCardDetailsAsync(); LogMessage("DesFire Card Details: " + Environment.NewLine + desfire.ToString()); } else if (cardIdentification.PcscDeviceClass == Pcsc.Common.DeviceClass.StorageClass && cardIdentification.PcscCardName == Pcsc.CardName.FeliCa) { // Handle Felica LogMessage("Felica card detected"); var felicaAccess = new Felica.AccessHandler(connection); var uid = await felicaAccess.GetUidAsync(); LogMessage("UID: " + BitConverter.ToString(uid)); } else if (cardIdentification.PcscDeviceClass == Pcsc.Common.DeviceClass.StorageClass && (cardIdentification.PcscCardName == Pcsc.CardName.MifareStandard1K || cardIdentification.PcscCardName == Pcsc.CardName.MifareStandard4K)) { // Handle MIFARE Standard/Classic LogMessage("MIFARE Standard/Classic card detected"); var mfStdAccess = new MifareStandard.AccessHandler(connection); var uid = await mfStdAccess.GetUidAsync(); LogMessage("UID: " + BitConverter.ToString(uid)); ushort maxAddress = 0; switch (cardIdentification.PcscCardName) { case Pcsc.CardName.MifareStandard1K: maxAddress = 0x3f; break; case Pcsc.CardName.MifareStandard4K: maxAddress = 0xff; break; } await mfStdAccess.LoadKeyAsync(MifareStandard.DefaultKeys.FactoryDefault); for (ushort address = 0; address <= maxAddress; address++) { var response = await mfStdAccess.ReadAsync(address, Pcsc.GeneralAuthenticate.GeneralAuthenticateKeyType.MifareKeyA); LogMessage("Block " + address.ToString() + " " + BitConverter.ToString(response)); } } else if (cardIdentification.PcscDeviceClass == Pcsc.Common.DeviceClass.StorageClass && (cardIdentification.PcscCardName == Pcsc.CardName.ICODE1 || cardIdentification.PcscCardName == Pcsc.CardName.ICODESLI || cardIdentification.PcscCardName == Pcsc.CardName.iCodeSL2)) { // Handle ISO15693 LogMessage("ISO15693 card detected"); var iso15693Access = new Iso15693.AccessHandler(connection); var uid = await iso15693Access.GetUidAsync(); LogMessage("UID: " + BitConverter.ToString(uid)); } else { // Unknown card type // Note that when using the XDE emulator the card's ATR and type is not passed through, so we'll // end up here even for known card types if using the XDE emulator // Some cards might still let us query their UID with the PC/SC command, so let's try: var apduRes = await connection.TransceiveAsync(new Pcsc.GetUid()); if (!apduRes.Succeeded) { LogMessage("Failure getting UID of card, " + apduRes.ToString()); } else { LogMessage("UID: " + BitConverter.ToString(apduRes.ResponseData)); } } } } catch(Exception ex) { LogMessage("Exception handling card: " + ex.ToString(), NotifyType.ErrorMessage); } }
/// <summary> /// Sample code to hande a couple of different cards based on the identification process /// </summary> /// <returns>None</returns> private async Task HandleCard(SmartCard card) { try { // Clear the messages MainPage.Current.NotifyUser(String.Empty, NotifyType.StatusMessage, true); // Connect to the card var connection = await card.ConnectAsync(); // Try to identify what type of card it was IccDetection cardIdentification = new IccDetection(card, connection); await cardIdentification.DetectCardTypeAync(); LogMessage("Connected to card\r\nPC/SC device class: " + cardIdentification.PcscDeviceClass.ToString()); LogMessage("Card name: " + cardIdentification.PcscCardName.ToString()); if ((cardIdentification.PcscDeviceClass == Pcsc.Common.DeviceClass.StorageClass) && (cardIdentification.PcscCardName == Pcsc.CardName.MifareUltralightC || cardIdentification.PcscCardName == Pcsc.CardName.MifareUltralight || cardIdentification.PcscCardName == Pcsc.CardName.MifareUltralightEV1)) { // Handle MIFARE Ultralight MifareUltralight.AccessHandler mifareULAccess = new MifareUltralight.AccessHandler(connection); // Each read should get us 16 bytes/4 blocks, so doing // 4 reads will get us all 64 bytes/16 blocks on the card for (byte i = 0; i < 4; i++) { byte[] response = await mifareULAccess.ReadAsync((byte)(4 * i)); LogMessage("Block " + (4 * i).ToString() + " to Block " + (4 * i + 3).ToString() + " " + BitConverter.ToString(response)); } byte[] responseUid = await mifareULAccess.GetUidAsync(); LogMessage("UID: " + BitConverter.ToString(responseUid)); } else if (cardIdentification.PcscDeviceClass == Pcsc.Common.DeviceClass.MifareDesfire) { // Handle MIFARE DESfire Desfire.AccessHandler desfireAccess = new Desfire.AccessHandler(connection); Desfire.CardDetails desfire = await desfireAccess.ReadCardDetailsAsync(); LogMessage("DesFire Card Details: " + Environment.NewLine + desfire.ToString()); } else if (cardIdentification.PcscDeviceClass == Pcsc.Common.DeviceClass.StorageClass && cardIdentification.PcscCardName == Pcsc.CardName.FeliCa) { // Handle Felica LogMessage("Felica card detected"); var felicaAccess = new Felica.AccessHandler(connection); var uid = await felicaAccess.GetUidAsync(); LogMessage("UID: " + BitConverter.ToString(uid)); } else if (cardIdentification.PcscDeviceClass == Pcsc.Common.DeviceClass.StorageClass && (cardIdentification.PcscCardName == Pcsc.CardName.MifareStandard1K || cardIdentification.PcscCardName == Pcsc.CardName.MifareStandard4K)) { // Handle MIFARE Standard/Classic LogMessage("MIFARE Standard/Classic card detected"); var mfStdAccess = new MifareStandard.AccessHandler(connection); var uid = await mfStdAccess.GetUidAsync(); LogMessage("UID: " + BitConverter.ToString(uid)); ushort maxAddress = 0; switch (cardIdentification.PcscCardName) { case Pcsc.CardName.MifareStandard1K: maxAddress = 0x3f; break; case Pcsc.CardName.MifareStandard4K: maxAddress = 0xff; break; } await mfStdAccess.LoadKeyAsync(MifareStandard.DefaultKeys.FactoryDefault); for (ushort address = 0; address <= maxAddress; address++) { var response = await mfStdAccess.ReadAsync(address, Pcsc.GeneralAuthenticate.GeneralAuthenticateKeyType.MifareKeyA); LogMessage("Block " + address.ToString() + " " + BitConverter.ToString(response)); } } else if (cardIdentification.PcscDeviceClass == Pcsc.Common.DeviceClass.StorageClass && (cardIdentification.PcscCardName == Pcsc.CardName.ICODE1 || cardIdentification.PcscCardName == Pcsc.CardName.ICODESLI || cardIdentification.PcscCardName == Pcsc.CardName.iCodeSL2)) { // Handle ISO15693 LogMessage("ISO15693 card detected"); var iso15693Access = new Iso15693.AccessHandler(connection); var uid = await iso15693Access.GetUidAsync(); LogMessage("UID: " + BitConverter.ToString(uid)); } connection.Dispose(); } catch(Exception ex) { LogMessage("Exception handling card: " + ex.ToString(), NotifyType.ErrorMessage); } }
private async Task HandleCard(SmartCard card) { try { connection = await card.ConnectAsync(); IccDetection cardIdentification = new IccDetection(card, connection); await cardIdentification.DetectCardTypeAync(); LogMessage("Connected to card\r\nPC/SC device class: " + cardIdentification.PcscDeviceClass.ToString()); LogMessage("Card name: " + cardIdentification.PcscCardName.ToString()); LogMessage("ATR: " + BitConverter.ToString(cardIdentification.Atr)); if ((cardIdentification.PcscDeviceClass == Pcsc.Common.DeviceClass.StorageClass) && (cardIdentification.PcscCardName == Pcsc.CardName.MifareUltralightC || cardIdentification.PcscCardName == Pcsc.CardName.MifareUltralight || cardIdentification.PcscCardName == Pcsc.CardName.MifareUltralightEV1)) { MifareUltralight.AccessHandler mifareULAccess = new MifareUltralight.AccessHandler(connection); byte[] responseUid = await mifareULAccess.GetUidAsync(); LogMessage("UID: " + BitConverter.ToString(responseUid)); if (!BinaryDump && !ByteArrayEqual(responseUid, AmiiboUID)) //I used this because for some reasons responseUid == AmiiboUID was always false { MessageDialog dialog = new MessageDialog("The dump UID and the amiibo UID don't match"); LogMessage("Amiibo UID: " + BitConverter.ToString(responseUid) + " != Dump UID: " + BitConverter.ToString(AmiiboUID)); await dialog.ShowAsync(); return; } LogMessage("Using key: " + BitConverter.ToString(AmiiboPass)); byte[] Authresponse = await mifareULAccess.TransparentExchangeAsync(new byte[] { 0x1B, AmiiboPass[0], AmiiboPass[1], AmiiboPass[2], AmiiboPass[3] }); //PWD_AUTH LogMessage("Auth sent !"); LogMessage("Auth Response length: " + Authresponse.Length.ToString()); if (Authresponse.Length == 0) { LogMessage("No response from Amiibo, can't restore dump !"); MessageDialog dlg = new MessageDialog("No response from Amiibo, wrong password ?"); await dlg.ShowAsync(); return; } LogMessage("Auth response: " + BitConverter.ToString(Authresponse)); //Using page layout from: https://www.3dbrew.org/wiki/Amiibo#Page_layout #region WritePages mifareULAccess.WriteAsync(0x4, getNBytes(AmiiboDump,0x10)); LogMessage("Page 0x4 wrote !"); mifareULAccess.WriteAsync(0x5, getNBytes(AmiiboDump, 0x14)); LogMessage("Page 0x5 wrote !"); mifareULAccess.WriteAsync(0x6, getNBytes(AmiiboDump, 0x18)); LogMessage("Page 0x6 wrote !"); mifareULAccess.WriteAsync(0x7, getNBytes(AmiiboDump, 0x1C)); LogMessage("Page 0x7 wrote !"); mifareULAccess.WriteAsync(0x8, getNBytes(AmiiboDump, 0x20)); LogMessage("Page 0x8 wrote !"); mifareULAccess.WriteAsync(0x9, getNBytes(AmiiboDump, 0x24)); LogMessage("Page 0x9 wrote !"); mifareULAccess.WriteAsync(0xA, getNBytes(AmiiboDump, 0x28)); LogMessage("Page 0xA wrote !"); mifareULAccess.WriteAsync(0xB, getNBytes(AmiiboDump, 0x2C)); LogMessage("Page 0xB wrote !"); mifareULAccess.WriteAsync(0xC, getNBytes(AmiiboDump, 0x30)); LogMessage("Page 0xC wrote !"); mifareULAccess.WriteAsync(0x20, getNBytes(AmiiboDump, 0x80)); LogMessage("Page 0x20 wrote !"); mifareULAccess.WriteAsync(0x21, getNBytes(AmiiboDump, 0x84)); LogMessage("Page 0x21 wrote !"); mifareULAccess.WriteAsync(0x22, getNBytes(AmiiboDump, 0x88)); LogMessage("Page 0x22 wrote !"); mifareULAccess.WriteAsync(0x23, getNBytes(AmiiboDump, 0x8C)); LogMessage("Page 0x23 wrote !"); mifareULAccess.WriteAsync(0x24, getNBytes(AmiiboDump, 0x90)); LogMessage("Page 0x24 wrote !"); mifareULAccess.WriteAsync(0x25, getNBytes(AmiiboDump, 0x94)); LogMessage("Page 0x25 wrote !"); mifareULAccess.WriteAsync(0x26, getNBytes(AmiiboDump, 0x98)); LogMessage("Page 0x26 wrote !"); mifareULAccess.WriteAsync(0x27, getNBytes(AmiiboDump, 0x9c)); LogMessage("Page 0x27 wrote !");//Until here i manually wrote the write addreses to test if the writing works, but i'm too lazy to replace those arleady working functions with a cycle for (int i = 0; i < 0x5A; i++) mifareULAccess.WriteAsync((byte)(0x28/*Page*/ + i), getNBytes(AmiiboDump, 0xA0 /*ADDR of page 0x28*/ + (i*4))); LogMessage("Page 0x28 to 0x81 wrote !"); #endregion LogMessage("Dump restored :) !"); MessageDialog msg = new MessageDialog("Dump restored !"); await msg.ShowAsync(); } else { LogMessage("This tag is not an Amiibo !"); MessageDialog msg = new MessageDialog("This tag is not an Amiibo !"); await msg.ShowAsync(); var apduRes = await connection.TransceiveAsync(new Pcsc.GetUid()); if (!apduRes.Succeeded) { LogMessage("Failure getting UID of card, " + apduRes.ToString()); } else { LogMessage("UID: " + BitConverter.ToString(apduRes.ResponseData)); } } connection.Dispose(); } catch (Exception ex) { connection.Dispose(); LogMessage("Exception handling card: " + ex.ToString()); MessageDialog msg = new MessageDialog("Exception handling card: " + ex.ToString()); await msg.ShowAsync(); } }
private async Task HandleCard(SmartCard card) { try { connection = await card.ConnectAsync(); IccDetection cardIdentification = new IccDetection(card, connection); await cardIdentification.DetectCardTypeAync(); LogMessage("Connected to card\r\nPC/SC device class: " + cardIdentification.PcscDeviceClass.ToString()); LogMessage("Card name: " + cardIdentification.PcscCardName.ToString()); LogMessage("ATR: " + BitConverter.ToString(cardIdentification.Atr)); if ((cardIdentification.PcscDeviceClass == Pcsc.Common.DeviceClass.StorageClass) && (cardIdentification.PcscCardName == Pcsc.CardName.MifareUltralightC || cardIdentification.PcscCardName == Pcsc.CardName.MifareUltralight || cardIdentification.PcscCardName == Pcsc.CardName.MifareUltralightEV1)) { MifareUltralight.AccessHandler mifareULAccess = new MifareUltralight.AccessHandler(connection); List<byte> dumpTmp = new List<byte>(); byte[] responseUid = await mifareULAccess.GetUidAsync(); dumpTmp.AddRange(new byte[] { 0x41, 0x6D, 0x69, 0x69, 0x67, 0x6F, 0x44, 0x75, 0x6D, 0x70, 0x00, 0x00, 0x00, 0x00 }); //MAGIC LogMessage("UID: " + BitConverter.ToString(responseUid)); dumpTmp.AddRange(responseUid); dumpTmp.AddRange(new byte[4]); for (byte i = 0; i < 33; i++) { byte[] response = await mifareULAccess.ReadAsync((byte)(4 * i)); dumpTmp.AddRange(response); LogMessage("Block " + (4 * i).ToString() + " to Block " + (4 * i + 3).ToString() + " " + BitConverter.ToString(response)); } LogMessage("Dump ended :) !"); dumpTmp.AddRange(new byte[4] { 0x5F, 0x00, 0x00, 0x00 }); Dump = dumpTmp.ToArray(); MessageDialog msg = new MessageDialog("Dump completed !"); await msg.ShowAsync(); } else { LogMessage("This tag is not an Amiibo and can't be dumped!"); MessageDialog msg = new MessageDialog("This tag is not an Amiibo and can't be dumped !"); await msg.ShowAsync(); var apduRes = await connection.TransceiveAsync(new Pcsc.GetUid()); if (!apduRes.Succeeded) { LogMessage("Failure getting UID of card, " + apduRes.ToString()); } else { LogMessage("UID: " + BitConverter.ToString(apduRes.ResponseData)); } } connection.Dispose(); } catch (Exception ex) { connection.Dispose(); LogMessage("Exception handling card: " + ex.ToString()); MessageDialog msg = new MessageDialog("Exception handling card: " + ex.ToString()); await msg.ShowAsync(); } }
static void Main() { SimulatedReaders = new Dictionary<string, ReaderSettings>(); try { RegistryKey regKey = Registry.CurrentUser.OpenSubKey(@"Software\VirtualSmartCard\RemoteReaders"); foreach (var name in regKey.GetSubKeyNames()) { var readerKey = regKey.OpenSubKey(name); var reader = ReaderSettings.GetSettings(readerKey); SimulatedReaders[reader.Name] = reader; readerKey.Close(); } regKey.Close(); } catch {} SmartCard sc = new SmartCard(); var s = sc.ListReaders(); foreach (String r in s) { ReaderSettings settings = null; sc.Connect(r, SmartCard.share.SCARD_SHARE_DIRECT, SmartCard.protocol.SCARD_PROTOCOL_UNDEFINED); try { byte[] isSimulated = sc.GetAttrib(0x7a009); if (isSimulated != null) { int readerType = isSimulated[0]; if (readerType == 0) { var pipeSettings = new PipeReaderSettings(); byte[] data; pipeSettings.PipeName = Encoding.ASCII.GetString((data = sc.GetAttrib(0x07a00a)), 0, data.Length - 1); pipeSettings.EventPipeName = Encoding.ASCII.GetString((data = sc.GetAttrib(0x07a00b)), 0, data.Length - 1); pipeSettings.Host = "."; settings = pipeSettings; } else if (readerType == 1) { var tcpSettings = new TcpIpReaderSettings(); byte[] data; tcpSettings.Port = ((data = sc.GetAttrib(0x07a00c))[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24)); tcpSettings.EventPort = ((data = sc.GetAttrib(0x07a00d))[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24)); tcpSettings.Host = "127.0.0.1"; settings = tcpSettings; } } settings.Name = r; settings.IsRemote = false; SimulatedReaders[r] = settings; } catch { } finally { sc.Disconnect(SmartCard.disposition.SCARD_LEAVE_CARD); } } ScanForCards(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Main()); }
public MiFareWinRTCardReader(SmartCard smartCard, IReadOnlyCollection<SectorKeySet> keys) : base(keys) { this.smartCard = smartCard; initialization = Initialize(); }
public static Stream ReadBinary(SmartCard card, int fileId, int offset, int expectedLength) { if ((fileId < 0) || (fileId > 0x1F)) throw new ArgumentOutOfRangeException("id"); if ((offset < 0) || (offset > 0xFF)) throw new ArgumentOutOfRangeException("offset"); Command cmd = new Command(); cmd.Instruction = 0xB0; cmd.Parameter1 = (byte)((1 << 7) | (fileId & 0x1F)); cmd.Parameter2 = (byte)offset; cmd.ResponseLength = (expectedLength <= 0) ? 0xFF : expectedLength; byte[] command = cmd.ToArray(); byte[] buffer = new byte[((expectedLength <= 0) ? 256 : expectedLength) + 2]; //Console.Write("Sending command {0}...", BitConverter.ToString(command)); int len = card.Transmit(command, buffer); //Console.WriteLine(BitConverter.ToString(buffer, 0, len)); CommandStatus status = GetStatus(buffer, len); if (!status.IsNormal()) throw status.ToException(); Stream result = new MemoryStream(); result.Write(buffer, 0, len - 2); while (status.Sw1 == 0x61) { if (buffer.Length < status.Sw2 + 2) buffer = new byte[status.Sw2 + 2]; cmd = new Command(); cmd.Instruction = 0xC0; cmd.ResponseLength = status.Sw2; command = cmd.ToArray(); len = card.Transmit(command, buffer); status = GetStatus(buffer, len); if (!status.IsNormal()) throw status.ToException(); result.Write(buffer, 0, len - 2); } result.Position = 0; return result; }