protected static byte[] ReadBinary(SCardChannel channel, ushort offset, ushort length) { CAPDU capdu = new CAPDU(0x00, 0xB0, (byte)(offset / 0x0100), (byte)(offset % 0x0100), (byte)length); Trace.WriteLine("< " + capdu.AsString(" ")); RAPDU rapdu = channel.Transmit(capdu); if (rapdu == null) { Trace.WriteLine("ReadBinary " + String.Format("{0:X4}", offset) + "," + String.Format("{0:X2}", (byte)length) + " error " + channel.LastError + " (" + channel.LastErrorAsString + ")"); return(null); } Trace.WriteLine("> " + rapdu.AsString(" ")); if (rapdu.SW != 0x9000) { Trace.WriteLine("ReadBinary " + String.Format("{0:X4}", offset) + "," + String.Format("{0:X2}", (byte)length) + " failed " + rapdu.SWString + " (" + SCARD.CardStatusWordsToString(rapdu.SW) + ")"); return(null); } if (rapdu.hasData) { return(rapdu.data.GetBytes()); } return(null); }
void CardConnectedCallback(SCardChannel cardChannel) { /* The CardConnected function is called as a delegate (callback) by the SCardReader object */ /* within its backgroung thread. Therefore it is not allowed to use the UI objects. */ if (!GoogleVasLicense.ReadDeviceId(cardChannel)) { Logger.Error("Read device ID error"); ShowResult("Not a SpringCard device?", null, true); return; } if (!GoogleVasLicense.LoadCollectorId(config.CollectorId_4)) { Logger.Error("Wrong Collector ID"); ShowResult("Wrong Collector ID?", null, true); return; } if (!GoogleVasLicense.Allowed(out string msg)) { Logger.Error("Not allowed to execute"); ShowResult(string.Format("Not allowed to execute ({0})", msg), null, true); return; } Logger.Trace("Card connected - Trying to run the transaction"); GoogleVasTerminal terminal = new GoogleVasTerminal(config); if (terminal.DoTransaction(cardChannel, out GoogleVasData data, out GoogleVasError error)) { ShowResult("Message is OK", data, true); }
void CardConnectedCallback(SCardChannel cardChannel) { /* The CardConnected function is called as a delegate (callback) by the SCardReader object */ /* within its backgroung thread. Therefore it is not allowed to use the UI objects. */ Logger.Trace("Card connected - Trying to run the transaction"); if (!AppleVasLicense.ReadDeviceId(cardChannel)) { Logger.Error("Read device ID error"); ShowResult("Not a SpringCard device?", null, true); return; } if (!AppleVasLicense.Allowed(out string msg2)) { Logger.Error("Not allowed to execute"); ShowResult(string.Format("Not allowed to execute ({0})", msg2), null, true); return; } AppleVasTerminal terminal = new AppleVasTerminal(); foreach (AppleVasConfig merchConfig in appleConfig.Merchants) { terminal.AddConfig(merchConfig); } if (terminal.DoTransaction(cardChannel, out AppleVasData data, out AppleVasError error)) { ShowResult(null, data, true); }
public static new bool Recognize(SCardChannel channel) { bool write_protected = false; ushort max_le = 0; ushort max_lc = 0; ushort ndef_file_id = 0; long ndef_file_size = 0; if (!IsDesfireEV1(channel)) { Trace.WriteLine("The card is not a Desfire EV1"); return(false); } if (!NfcTagType4.Recognize(channel, ref write_protected, ref max_le, ref max_lc, ref ndef_file_id, ref ndef_file_size)) { /* Failed to recognize a Type 4 card, but anyway a Desfire EV1 may be formatted later on */ Trace.WriteLine("The card is a Desfire EV1, it may become a type 4 Tag"); } else { Trace.WriteLine("The card is a Desfire EV1, already formatted as type 4 Tag"); } return(true); }
void BtCreateNfcTagClick(object sender, EventArgs e) { int read = 0; for (int i = 0; i < SCARD.Readers.Length; i++) { if (SCARD.Readers[i].Equals(reader.Name)) { read = i; } } DesfireFormatForm format = new DesfireFormatForm(read); format.ShowDialog(); if (format.format_ok) { Trace.WriteLine("Formating ok"); cardchannel = new SCardChannel(reader); if (cardchannel.Connect()) { Trace.WriteLine("Connected to the card"); cardthread = new Thread(card_read_proc); cardthread.Start(); } } else { Trace.WriteLine("Formating ko"); } }
/// <summary> /// Callback used when the reader's status change /// As this method is called from a thread, you can't directly modify the user interface /// </summary> /// <param name="readerState"></param> /// <param name="cardAtr"></param> private void readerStatusChanged(uint readerState, CardBuffer cardAtr) { // When you are in a thread you can't directly modify the user interface if (InvokeRequired) { this.BeginInvoke(new readerStatusChangedInvoker(readerStatusChanged), readerState, cardAtr); return; } btnRead.Enabled = false; txtAsciiContent.Text = ""; txtFinalStatus.Text = ""; txtHexData.Text = ""; lblCardAtr.Text = ""; lblStatus.Text = SCARD.ReaderStatusToString(readerState); if (cardAtr != null) { lblCardAtr.Text = cardAtr.AsString(" "); channel = new SCardChannel(reader); if (!channel.Connect()) { lblStatus.Text = "Error, can't connect to the card"; return; } CAPDU capdu = new CAPDU(0xFF, 0xCA, 0x00, 0x00); // Command sent to the reader RAPDU rapdu = channel.Transmit(capdu); // Response sent from card if (rapdu.SW != 0x9000) // Something failed { lblStatus.Text = "Get UID APDU failed!"; return; } btnRead.Enabled = true; } }
protected static byte[] GetUid(SCardChannel channel) { CAPDU capdu = new CAPDU(0xFF, 0xCA, 0x00, 0x00, 0x00); Trace.WriteLine("< " + capdu.AsString(" ")); RAPDU rapdu = null; rapdu = channel.Transmit(capdu); if (rapdu == null) { Trace.WriteLine("Error '" + channel.LastErrorAsString + "' while getting the card's UID"); return(null); } Trace.WriteLine("> " + rapdu.AsString(" ")); if (rapdu.SW != 0x9000) { Trace.WriteLine("Bad status word " + rapdu.SWString + " while getting the card's UID"); return(null); } if (!rapdu.hasData) { Trace.WriteLine("Empty response"); return(null); } return(rapdu.data.GetBytes()); }
public static bool Recognize(SCardChannel channel, ref bool formatted, ref bool formattable, ref bool write_protected) { byte[] header = ReadBinary(channel, 0, READ_4_PAGES); if (header == null) { Trace.WriteLine("Failed to read pages 0-3"); return(false); } if ((header[12] == 0) && (header[13] == 0) && (header[14] == 0) && (header[15] == 0)) { /* The OTP bits are blank, assume the card is an unformatted type 2 Tag */ Trace.WriteLine("OTP are blank"); formatted = false; formattable = true; write_protected = false; return(true); } if (header[12] != NFC_FORUM_MAGIC_NUMBER) { /* The OTP bits contain something else */ Trace.WriteLine("OTP are not blank"); formatted = false; formattable = false; write_protected = true; return(false); } /* The OTP bits contain the NFC NDEF MAGIC NUMBER, so this is a formatted type 2 Tag */ Trace.WriteLine("OTP = NFC Forum magic number"); formatted = true; formattable = false; write_protected = true; if ((header[13] & 0xF0) != (NFC_FORUM_VERSION_NUMBER & 0xF0)) { Trace.WriteLine("Version mismatch in OTP"); return(false); } if ((header[15] & 0xF0) == 0) { Trace.WriteLine("Free read access"); } else { Trace.WriteLine("No read access"); return(false); } if ((header[15] & 0x0F) == 0) { Trace.WriteLine("Free write access"); write_protected = false; } else { Trace.WriteLine("No write access"); } return(true); }
private void btnRead_Click(object sender, EventArgs e) { txtInfoReader.Text = ""; if (cbReaders.Items.Count == 0) { return; } if (cbReaders.SelectedIndex == -1) { return; } string readerName = this.cbReaders.GetItemText(cbReaders.SelectedItem); if (readerName.Trim() == "") { return; } SCardChannel channel = new SCardChannel(readerName); channel.Protocol = SCARD.PROTOCOL_NONE; channel.ShareMode = SCARD.SHARE_DIRECT; if (!channel.Connect()) { return; } CardBuffer r; // If you want to make the reader beep and change the LEDs color //"FFF00000031C0260", // Beep //"FFF00000041E967D96", // <= Pink ledsLEDS - To come back to normal LEDs => FFF00000011E string[] apdus = new string[] { "582001", // Vendor's Name "582002", // Product's name "582003", // Product's Serial number "582004", // USB vendor ID and product ID "582005", // Product's version "582010", // NXP MfRCxxx product code "582011", // Gemalto Gemcore product name and version "5821", // Name of the current slot "582100", // Name of slot 0 "582101", // Name of slot 1 }; int line = 0; foreach (string apdu in apdus) { r = channel.Control(new CardBuffer(apdu)); if ((r != null) && (r.Length >= 1) && (r.GetByte(0) == 0x00)) { txtInfoReader.Text += new String(r.GetChars(1, -1)) + System.Environment.NewLine; } line++; } channel.DisconnectLeave(); }
public static NfcTagType4Desfire Read(SCardChannel channel) { NfcTagType4Desfire t = new NfcTagType4Desfire(channel); t.Read(); return(t); }
public MemoryCardWithoutSectors(MainForm _MF, SCardChannel Channel) : base(_MF, Channel) { Recognize_with_ATR(); if (!Read(false)) { throw new System.ApplicationException(); } }
public MifareSector(MainForm _MF, int _sector, byte[] data, SCardChannel channel) { MF = _MF; sector = _sector; _channel = channel; PopulateBlocks(data); }
/**f* SpringCard.NFC/NfcTagType2.Recognize * * NAME * NfcTagType2.Recognize * * SYNOPSIS * public static bool Recognize(SCardChannel channel) * * DESCRIPTION * Returns true if the card on the reader is a NFC Forum type 2 Tag * * SEE ALSO * NfcTagType2.Create * **/ public static bool Recognize(SCardChannel channel) { bool formatted = false; bool formattable = false; bool write_protected = false; return(Recognize(channel, ref formatted, ref formattable, ref write_protected)); }
public void Setup() { SmartCard = new SCardChannel("OMNIKEY CardMan 3x21 0"); Assert.IsTrue(SmartCard.CardPresent, "Nincs kártya bedugva."); SmartCard.ShareMode = SCARD.SHARE_SHARED; SmartCard.Protocol = (uint)(SCARD.PROTOCOL_T0); SmartCard.Connect(); }
public void ClenUp() { if (SmartCard != null) { SmartCard.Disconnect(1); SmartCard = null; } }
public static bool IsDesfireEV1(SCardChannel channel) { bool is_desfire_ev1 = false; CAPDU capdu = new CAPDU(0x90, 0x60, 0x00, 0x00, 0x00); Trace.WriteLine("< " + capdu.AsString(" ")); RAPDU rapdu = channel.Transmit(capdu); Trace.WriteLine("> " + rapdu.AsString(" ")); if (rapdu.SW != 0x91AF) { Trace.WriteLine("Desfire GetVersion function failed"); return(false); } if (rapdu.GetByte(3) > 0) { Trace.WriteLine("This is a Desfire EV1"); is_desfire_ev1 = true; } else { Trace.WriteLine("This is a Desfire EV0"); } capdu = new CAPDU(0x90, 0xAF, 0x00, 0x00, 0x00); Trace.WriteLine("< " + capdu.AsString(" ")); rapdu = channel.Transmit(capdu); Trace.WriteLine("> " + rapdu.AsString(" ")); if (rapdu.SW != 0x91AF) { Trace.WriteLine("Desfire GetVersion(2) function failed"); return(false); } capdu = new CAPDU(0x90, 0xAF, 0x00, 0x00, 0x00); Trace.WriteLine("< " + capdu.AsString(" ")); rapdu = channel.Transmit(capdu); Trace.WriteLine("> " + rapdu.AsString(" ")); if (rapdu.SW != 0x9100) { Trace.WriteLine("Desfire GetVersion(3) function failed"); return(false); } return(is_desfire_ev1); }
/**m* SpringCardNFC/NfcTag.Recognize * * SYNOPSIS * public static bool Recognize(SCardChannel cardchannel, out NfcTag tag, out string msg) * * DESCRIPTION * Determines if the card on the reader is a NFC Forum compliant tag * It first checks the ATR to determine if the card can be type 2 or * a type 4 and tries to recognize the content of the tag. * It creates either a NfcTagType2 or a NfcTagType4 and returns true if the tag is recognized. * It returns false if the card is not a NFC Forum tag * * SEE ALSO * NfcTagType2.RecognizeAtr * NfcTagType2.Recognize * NfcTagType2.Create * NfcTagType4.Recognize * NfcTagType4.Create * * **/ public static bool Recognize(SCardChannel cardchannel, out NfcTag tag, out string msg, out bool Desfire_formatable) { bool res = false; msg = ""; tag = null; Desfire_formatable = false; if (NfcTagType2.RecognizeAtr(cardchannel)) { Trace.WriteLine("Based on the ATR, this card is likely to be a NFC type 2 Tag"); if (NfcTagType2.Recognize(cardchannel)) { Trace.WriteLine("This card is actually a NFC type 2 Tag"); tag = NfcTagType2.Create(cardchannel); if (tag == null) { msg = "An error has occured while reading the Tag's content"; } res = true; } else { Trace.WriteLine("Based on its content, this card is not a NFC type 2 Tag, sorry"); msg = "From the ATR it may be a NFC type 2 Tag, but the content is invalid"; } } else if (NfcTagType4.Recognize(cardchannel)) { Trace.WriteLine("This card is a NFC type 4 Tag"); tag = NfcTagType4.Create(cardchannel); if (tag == null) { msg = "An error has occured while reading the Tag's content"; } res = true; } else #if (NFC_TAGS_DESFIRE) if (NfcTagType4Desfire.Recognize(cardchannel)) { msg = "A DESFire EV1 card has been detected.\nIt may be formatted into a type 4 Tag."; Desfire_formatable = true; res = false; } else #endif { msg = "Unrecognized or unsupported card"; tag = null; } return(res); }
/**f* SpringCardNFC/NfcTagType4.Recognize * * NAME * NfcTagType4.Recognize * * SYNOPSIS * public static bool Recognize(SCardChannel channel) * * DESCRIPTION * Return true if the card on the reader is a NFC Forum type 4 Tag * * SEE ALSO * NfcTagType4.Create * **/ public static bool Recognize(SCardChannel channel) { bool write_protected = false; ushort max_le = 0; ushort max_lc = 0; ushort ndef_file_id = 0; long ndef_file_size = 0; return(Recognize(channel, ref write_protected, ref max_le, ref max_lc, ref ndef_file_id, ref ndef_file_size)); }
/**m* SpringCard.NFC/NfcTagType2.Create * * NAME * NfcTagType2.Create * * SYNOPSIS * public static NfcTagType2 Create(SCardChannel channel) * * DESCRIPTION * Instanciates a new NfcTagType2 object for this card * * SEE ALSO * NfcTagType2.Recognize * **/ public static NfcTagType2 Create(SCardChannel channel) { NfcTagType2 t = new NfcTagType2(channel); if (!t.Read()) { return(null); } return(t); }
void OnError(string msg) { Logger.Trace("Error: " + msg); ClearDisplay(); MessageBox.Show(this, msg); if (cardChannel != null) { cardChannel.DisconnectLeave(); cardChannel = null; } }
protected static bool WriteBinary(SCardChannel channel, ushort address, byte[] data) { if (data == null) { return(false); } if (data.Length != 4) { Trace.WriteLine("Type 2 Tag: Write Binary accepts only 4B"); return(false); } CAPDU capdu = new CAPDU(0xFF, 0xD6, (byte)(address / 0x0100), (byte)(address % 0x0100), data); Trace.WriteLine("< " + capdu.AsString(" ")); RAPDU rapdu = null; for (int retry = 0; retry < 4; retry++) { rapdu = channel.Transmit(capdu); if (rapdu == null) { break; } if ((rapdu.SW != 0x6F01) && (rapdu.SW != 0x6F02) && (rapdu.SW != 0x6F0B)) { break; } Thread.Sleep(15); } if (rapdu == null) { Trace.WriteLine("Error '" + channel.LastErrorAsString + "' while writing the card"); return(false); } Trace.WriteLine("> " + rapdu.AsString(" ")); if (rapdu.SW != 0x9000) { Trace.WriteLine("Bad status word " + rapdu.SWString + " while writing the card"); return(false); } return(true); }
public bool Connect(uint ShareMode, uint Protocol) { if (channel != null) { /* Drop existing connection */ /* ------------------------ */ if (channel.Connected) { channel.DisconnectReset(); } channel = null; } /* Instanciate a new connection */ /* ---------------------------- */ channel = new SCardChannel(ReaderName); channel.ShareMode = ShareMode; channel.Protocol = Protocol; if (ShareMode == SCARD.SHARE_DIRECT) { /* DIRECT mode opens Control page */ /* ------------------------------ */ tabPages.SelectedIndex = 1; hexBoxCCtrl.Focus(); btnControl.Visible = true; } else { /* SHARED or EXCLUSIVE mode opens Transmit page */ /* -------------------------------------------- */ tabPages.SelectedIndex = 0; hexBoxCApdu.Focus(); btnTransmit.Visible = true; } if (!channel.Connect()) { ShowError(); return(false); } ShowSuccess(); eCardAtr.Text = channel.CardAtr.AsString(); eProtocol.Text = channel.ProtocolAsString; eMode.Text = channel.ShareModeAsString; return(true); }
public SCardCalypso(SCardChannel scard) { _ctx = CreateContext(); _rc = CardBindPcsc(_ctx, scard.hCard); if (_rc == 0) { _rc = CardActivate(_ctx, null, 0); if (_rc == 0) { _activated = true; } } }
private void Form1_FormClosed(object sender, FormClosedEventArgs e) { // Stop properly if (channel != null) { channel.Disconnect(); channel = null; } if (reader != null) { reader.StopMonitor(); reader = null; } }
void MainFormClosed(object sender, FormClosedEventArgs e) { if (cardchannel != null) { cardchannel.Disconnect(); cardchannel = null; } if (reader != null) { reader.StopMonitor(); reader = null; } SystemConsole.Free(); }
/* * SCardDisconnect * --------------- */ public void Disconnect(uint Disposition) { eCardAtr.Text = ""; eProtocol.Text = ""; eMode.Text = ""; if ((channel != null) && !channel.Disconnect(Disposition)) { ShowError(); } else { ShowSuccess(); } channel = null; }
public void ClenUp() { if (SmartCard != null) { SmartCard.Disconnect(1); SmartCard = null; } if (Network != null) { Network.SaveFrameLog("D:\\" + this.GetType().FullName + ".csv"); CanLink.Close(); Network = null; CanLink.Disconnect(); CanLink.Dispose(); } }
protected static byte[] ReadBinary(SCardChannel channel, ushort address, byte length) { CAPDU capdu = new CAPDU(0xFF, 0xB0, (byte)(address / 0x0100), (byte)(address % 0x0100), length); Trace.WriteLine("< " + capdu.AsString(" ")); RAPDU rapdu = null; for (int retry = 0; retry < 4; retry++) { rapdu = channel.Transmit(capdu); if (rapdu == null) { break; } if ((rapdu.SW != 0x6F01) && (rapdu.SW != 0x6F02) && (rapdu.SW != 0x6F0B)) { break; } Thread.Sleep(10); } if (rapdu == null) { Trace.WriteLine("Error '" + channel.LastErrorAsString + "' while reading the card"); return(null); } Trace.WriteLine("> " + rapdu.AsString(" ")); if (rapdu.SW != 0x9000) { Trace.WriteLine("Bad status word " + rapdu.SWString + " while reading the card"); return(null); } if (!rapdu.hasData) { Trace.WriteLine("Empty response"); return(null); } return(rapdu.data.GetBytes()); }
public void Setup() { SmartCard = new SCardChannel("OMNIKEY CardMan 3x21 0"); Assert.IsTrue(SmartCard.CardPresent, "Nincs kártya bedugva."); SmartCard.ShareMode = SCARD.SHARE_SHARED; SmartCard.Protocol = (uint)(SCARD.PROTOCOL_T1); SmartCard.Connect(); UInt32 txId = 0x38DAEEFB; UInt32 rxId = 0x38DAFBEE; UInt32 baudRate = 250000; CanLink = new CanBusLink(txId, rxId, baudRate); CanLink.Connect(); CanLink.BusTerminationEnable = true; CanLink.Open(); Network = new Iso15765NetwrorkLayer(CanLink); }
static public int InsertCard() { m_hCard = new SCardChannel(m_ReaderList[_reader_id]); if (m_hCard == null) { return(ERROR_INSERT_CARD); } if (!m_hCard.ConnectExclusive()) { LogManager.DoLogOperation("[ERROR] can't connect to the card"); //m_hCard.Dispose(); return(ERROR_INSERT_CARD); } LogManager.DoLogOperation("[INFO] Connected to the card"); return(ERROR_NO_ERROR); }