public byte[] readBlock(byte block) { byte[] d1 = new Byte[] { 0xD4, 0x40, 0x01, 0x30, block }; APDUCommand a1 = new APDUCommand(0xFF, 0x00, 0x00, 0x00, d1, 0x05); try{ APDUResponse r1 = this.iCard.Transmit(a1); }catch (System.Exception) { return(null); } //Status code 61 15 APDUCommand a2 = new APDUCommand(0xFF, 0xC0, 0x00, 0x00, null, 0x15); APDUResponse r2; try{ r2 = this.iCard.Transmit(a2); }catch (System.Exception) { return(null); } //Status Code ?? _connected = false; return(r2.Data); }
public bool authenticate(byte[] key, byte block) { byte[] card_uid = uid(); if (card_uid == null) { return(false); } //Send authentication command byte[] d1 = new byte[] { 0xD4, 0x40, 0x01, 0x60, block, key[0], key[1], key[2], key[3], key[4], key[5], card_uid[0], card_uid[1], card_uid[2], card_uid[3] }; APDUCommand a1 = new APDUCommand(0xFF, 0x00, 0x00, 0x00, d1, 0x0F); try{ APDUResponse r1 = this.iCard.Transmit(a1); }catch (System.Exception) { return(false); } //Status code 61 05 is valid //Read response APDUCommand a2 = new APDUCommand(0xFF, 0xC0, 0x00, 0x00, null, 0x05); try{ APDUResponse r2 = this.iCard.Transmit(a2); }catch (System.Exception) { return(false); } //Status code 90 00 is valid, else error return(true); }
private void btnTransmit_Click(object sender, System.EventArgs e) { try { APDUResponse apduResp = null; if (checkBoxEnterAPDUManually.Checked) { apduResp = apduPlayer.ProcessCommand(textClass.Text, textIns.Text, BuildAPDUParameters()); } else { apduResp = apduPlayer.ProcessCommand((string)comboApdu.SelectedItem, BuildAPDUParameters()); } DisplayLog(apduPlayer.Log); textDOut.Text = (apduResp.Data != null) ? ByteArray.ToString(apduResp.Data) : string.Empty; statusBarPanel_Sw.Text = string.Format("{0:X04}", apduResp.Status); statusBarPanel_Info.Text = "Command sent"; } catch (SmartCardException exSC) { statusBarPanel_Info.Text = exSC.Message; } catch (Exception ex) { statusBarPanel_Info.Text = ex.Message; } }
/// <summary> /// Считывает число из 4 байт карты /// </summary> private int ReadData() { APDUCommand apduCmd2 = new APDUCommand(0xFF, 0xb0, 0x00, 0x01, null, 0x10); APDUResponse apduResp2 = _card.Transmit(apduCmd2); byte[] data = { apduResp2.Data[0], apduResp2.Data[1], apduResp2.Data[2], apduResp2.Data[3] }; return(BitConverter.ToInt32(data, 0)); }
public bool disconnect() { APDUCommand c1 = new APDUCommand(0xFF, 0x00, 0x00, 0x00, new byte[] { 0xD4, 0x44, 0x01 }, 0x03); APDUResponse r1 = iCard.Transmit(c1); APDUCommand c2 = new APDUCommand(0xFF, 0xC0, 0x00, 0x00, null, 0x05); APDUResponse r2 = iCard.Transmit(c2); this.iCard.Disconnect(DISCONNECT.Reset); _connected = false; return(true); }
private uint GetCardId(string reader) { _native.Connect(reader, SHARE.Shared, PROTOCOL.T0orT1); var getUid = new byte[] { 0xFF, (byte)Command.GetData, 0x00, 0x00, 0x00 }; var response = new APDUResponse(_native.TransmitRaw(getUid, 0xFF)); _native.Disconnect(DISCONNECT.Unpower); response.ThrowError(); var id = BitConverter.ToUInt32(response.Data, 0); return(id); }
/// <summary> /// Записывает число в 4 байта карты /// </summary> private int WriteData(int num) { byte[] numData = BitConverter.GetBytes(num); byte[] data = { numData[0], numData[1], numData[2], numData[3], 0x00, 0x00, 0x00, numData[0], numData[1], numData[2], 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; APDUCommand apduCmd4 = new APDUCommand(0xFF, 0xd6, 0x00, 0x01, data, 0x10); APDUResponse apduResp4 = _card.Transmit(apduCmd4); return(num); }
public byte[] readBlock(byte block) { //Read block APDUCommand cc3 = new APDUCommand(0xFF, 0xB0, 0x00, 0x08, null, 0x10); try{ APDUResponse rr3 = this.iCard.Transmit(cc3); return(rr3.Data); }catch (System.Exception) { Debug.WriteLine("ACR112_v2:: Transmit Error, " + cc3.ToString()); return(null); } return(null); }
public void select() { //Firmware //APDUCommand c_f = new APDUCommand( 0xFF, 0x00, 0x48, 0x00, null, 0x00); //APDUResponse r_f = this.iCard.Transmit(c_f); //Select? APDUCommand cc0 = new APDUCommand(0xFF, 0xCA, 0x00, 0x00, null, 0x04); try{ APDUResponse rr0 = this.iCard.Transmit(cc0); }catch (System.Exception) { Debug.WriteLine("ACR112_v2:: Transmit Error, " + cc0.ToString()); } }
public bool authenticate(byte[] key, byte block) { //Load authentication APDUCommand cc1 = new APDUCommand(0xFF, 0x82, 0x00, 0x00, key, 0x06); try{ APDUResponse rr1 = this.iCard.Transmit(cc1); }catch (System.Exception) { Debug.WriteLine("ACR112_v2:: Transmit Error, " + cc1.ToString()); return(false); } //Authenticate APDUCommand cc2 = new APDUCommand(0xFF, 0x86, 0x00, 0x00, new byte[] { 0x01, 0x00, block, 0x60, 0x00 }, 0x05); try{ APDUResponse rr2 = this.iCard.Transmit(cc2); }catch (System.Exception) { Debug.WriteLine("ACR112_v2:: Transmit Error, " + cc2.ToString()); return(false); } return(true); }
public Task <APDUResponse> ExchangeAPDU(APDUCommand apdu) { int bytesWritten; var errorCode = daplugWriter.Write(apdu.ToByteArray(), 5000, out bytesWritten); if (errorCode != ErrorCode.None) { throw new DaplugCommunicationException("Error while sendind command: " + errorCode + ". " + UsbDevice.LastErrorString); } int bytesRead; byte[] readBuffer = new byte[512]; var receiveError = daplugReader.Read(readBuffer, 1000, out bytesRead); if (receiveError != ErrorCode.None) { throw new DaplugCommunicationException("Error while receiving response: " + receiveError + ". " + UsbDevice.LastErrorString); } Array.Resize(ref readBuffer, bytesRead); APDUResponse response; //if response starts with 0x61, the next byte is the response length if (readBuffer[0] == 0x61) { byte[] responseData = new byte[readBuffer[1] + 2]; //we add 2 to the response length to account for the SW bytes Array.Copy(readBuffer, 2, responseData, 0, responseData.Length); response = new APDUResponse(responseData); } //else the response constains only the 2 SW bytes else { response = new APDUResponse(readBuffer); } return(Task.FromResult(response)); }
static void Main(string[] args) { try { int nbRecords = 10; string PIN = "31323334FFFFFFFF"; bool bPin = false; if (args.Length != 0) { for (int nI = 0; nI < args.Length; nI++) { if (args[nI] == "P") { bPin = true; PIN = FormatPIN(args[++nI]); } else { nbRecords = int.Parse(args[nI]); } } } APDUResponse apduResp = null; CardNative iCard = new CardNative(); string[] readers = iCard.ListReaders(); iCard.Connect(readers[0], SHARE.Shared, PROTOCOL.T0orT1); APDUPlayer player = new APDUPlayer(iCard); player.LoadAPDUFile(Properties.Settings.Default.DataPath + ApduFile); player.LoadSequenceFile(Properties.Settings.Default.DataPath + SequenceFile); SequenceParameter seqParam = new SequenceParameter(); // Process Apdu: VerifyCHV if (bPin) { Console.WriteLine("Sequence: Verify CHV1"); seqParam.Add("PIN", PIN); apduResp = player.ProcessSequence("Verify CHV1", seqParam); Console.WriteLine(apduResp.ToString()); } if (!bPin || (bPin && apduResp.Status == 0x9000)) { for (int nI = 1; nI <= nbRecords; nI++) { seqParam.Clear(); seqParam.Add("Record", nI.ToString()); //Console.WriteLine(string.Format("Read ADN, Record={0}", nI)); apduResp = player.ProcessSequence("Read ADN", seqParam); PhoneNumber phone = new PhoneNumber(apduResp.Data); Console.WriteLine("ADN n°" + nI.ToString()); Console.WriteLine(phone.ToString()); Console.WriteLine(); //Console.WriteLine(apduResp.ToString()); } } APDULogList log = player.Log; Console.WriteLine("Log:"); APDULog[] arrayLog = log.ToArray(); for (int nI = 0; nI < log.Count; nI++) { Console.WriteLine(arrayLog[nI].ToString()); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
private void ReadCard(object o) { UseHourglass(true); string selectedReader = o as string; UpdateStatusLabel("Reading card..."); TreeView treeView = new TreeView(); TreeNode cardNode = new TreeNode("Card"); cardNode.ImageIndex = 0; cardNode.SelectedImageIndex = 0; treeView.Nodes.Add(cardNode); // Tree nodes TreeNode pseNode = null; TreeNode fciNode = null; ASN1 fci = null; List <byte[]> pseIdentifiers = new List <byte[]>(); List <byte[]> applicationIdentifiers = new List <byte[]>(); ASCIIEncoding encoding = new ASCIIEncoding(); APDUCommand apdu = null; APDUResponse response = null; bool pseFound = false; if (!skipPSEToolStripMenuItem.Checked) { pseIdentifiers.Add(encoding.GetBytes("1PAY.SYS.DDF01")); pseIdentifiers.Add(encoding.GetBytes("2PAY.SYS.DDF01")); } try { // Now lets process all Payment System Environments if (pseIdentifiers.Count > 0) { cardReader.Connect(selectedReader); foreach (byte[] pse in pseIdentifiers) { apdu = new APDUCommand(0x00, 0xA4, 0x04, 0x00, pse, (byte)pse.Length); response = cardReader.Transmit(apdu); // Get response nescesary if (response.SW1 == 0x61) { apdu = new APDUCommand(0x00, 0xC0, 0x00, 0x00, null, response.SW2); response = cardReader.Transmit(apdu); } // PSE application read found ok if (response.SW1 == 0x90) { pseFound = true; pseNode = new TreeNode(String.Format("Application {0}", encoding.GetString(pse))); pseNode.ImageIndex = 1; pseNode.SelectedImageIndex = 1; pseNode.Tag = pse; cardNode.Nodes.Add(pseNode); fciNode = new TreeNode("File Control Information"); fciNode.ImageIndex = 3; fciNode.SelectedImageIndex = 3; fciNode.Tag = "fci"; pseNode.Nodes.Add(fciNode); fci = new ASN1(response.Data); AddRecordNodes(fci, fciNode); byte sfi = new ASN1(response.Data).Find(0x88).Value[0]; byte recordNumber = 0x01; byte p2 = (byte)((sfi << 3) | 4); TreeNode efDirNode = new TreeNode(String.Format("EF Directory - {0:X2}", sfi)); efDirNode.ImageIndex = 2; efDirNode.SelectedImageIndex = 2; efDirNode.Tag = sfi; pseNode.Nodes.Add(efDirNode); while (response.SW1 != 0x6A && response.SW2 != 0x83) { apdu = new APDUCommand(0x00, 0xB2, recordNumber, p2, null, 0x00); response = cardReader.Transmit(apdu); // Retry with correct length if (response.SW1 == 0x6C) { apdu = new APDUCommand(0x00, 0xB2, recordNumber, p2, null, response.SW2); response = cardReader.Transmit(apdu); } if (response.SW1 == 0x61) { apdu = new APDUCommand(0x00, 0xC0, 0x00, 0x00, null, response.SW2); response = cardReader.Transmit(apdu); } if (response.Data != null) { TreeNode recordNode = new TreeNode(String.Format("Record - {0:X2}", recordNumber)); recordNode.ImageIndex = 4; recordNode.SelectedImageIndex = 4; recordNode.Tag = recordNumber; efDirNode.Nodes.Add(recordNode); ASN1 aef = new ASN1(response.Data); AddRecordNodes(aef, recordNode); foreach (ASN1 appTemplate in aef) { // Check we really have an Application Template if (appTemplate.Tag[0] == 0x61) { applicationIdentifiers.Add(appTemplate.Find(0x4f).Value); } } } recordNumber++; } } if (pseFound) { break; } } cardReader.Disconnect(); } // We couldn't read the AID's from the PSE, so we'll just try querying all ADI's we know about if (!pseFound) { // From http://www.darkc0de.com/others/ChAP.py applicationIdentifiers.Add(Helpers.HexStringToBytes("A000000003")); // VISA applicationIdentifiers.Add(Helpers.HexStringToBytes("A0000000031010")); // VISA Debit/Credit applicationIdentifiers.Add(Helpers.HexStringToBytes("A000000003101001")); // VISA Credit applicationIdentifiers.Add(Helpers.HexStringToBytes("A000000003101002")); // VISA Debit applicationIdentifiers.Add(Helpers.HexStringToBytes("A0000000032010")); // VISA Electron applicationIdentifiers.Add(Helpers.HexStringToBytes("A0000000033010")); // VISA Interlink applicationIdentifiers.Add(Helpers.HexStringToBytes("A0000000038010")); // VISA Plus applicationIdentifiers.Add(Helpers.HexStringToBytes("A000000003999910")); // VISA ATM applicationIdentifiers.Add(Helpers.HexStringToBytes("A0000000041010")); // Mastercard applicationIdentifiers.Add(Helpers.HexStringToBytes("A0000000048010")); // Cirrus applicationIdentifiers.Add(Helpers.HexStringToBytes("A0000000043060")); // Maestro applicationIdentifiers.Add(Helpers.HexStringToBytes("A0000000050001")); // Maestro UK applicationIdentifiers.Add(Helpers.HexStringToBytes("A00000002401")); // Self Service applicationIdentifiers.Add(Helpers.HexStringToBytes("A000000025")); // American Express applicationIdentifiers.Add(Helpers.HexStringToBytes("A000000025010104")); // American Express applicationIdentifiers.Add(Helpers.HexStringToBytes("A000000025010701")); // ExpressPay applicationIdentifiers.Add(Helpers.HexStringToBytes("A0000000291010")); // Link applicationIdentifiers.Add(Helpers.HexStringToBytes("B012345678")); // Maestro TEST applicationIdentifiers.Add(Helpers.HexStringToBytes("A0000000651010")); // JCB } // Now lets process all of the AID's we found if (applicationIdentifiers.Count > 0) { foreach (byte[] AID in applicationIdentifiers) { List <ApplicationFileLocator> applicationFileLocators = new List <ApplicationFileLocator>(); StringBuilder sb = new StringBuilder(); cardReader.Connect(selectedReader); // Select AID apdu = new APDUCommand(0x00, 0xA4, 0x04, 0x00, AID, (byte)AID.Length); response = cardReader.Transmit(apdu); // Get response nescesary if (response.SW1 == 0x61) { apdu = new APDUCommand(0x00, 0xC0, 0x00, 0x00, null, response.SW2); response = cardReader.Transmit(apdu); } // Application not found if (response.SW1 == 0x6A && response.SW2 == 0x82) { continue; } if (response.SW1 == 0x90) { foreach (byte b in AID) { sb.AppendFormat("{0:X2}", b); } TreeNode applicationNode = new TreeNode(String.Format("Application {0}", sb.ToString())); applicationNode.ImageIndex = 1; applicationNode.SelectedImageIndex = 1; applicationNode.Tag = AID; cardNode.Nodes.Add(applicationNode); fciNode = new TreeNode("File Control Information"); fciNode.ImageIndex = 3; fciNode.SelectedImageIndex = 3; fciNode.Tag = "fci"; applicationNode.Nodes.Add(fciNode); fci = new ASN1(response.Data); AddRecordNodes(fci, fciNode); // Get processing options (with empty PDOL) apdu = new APDUCommand(0x80, 0xA8, 0x00, 0x00, new byte[] { 0x83, 0x00 }, 0x02); response = cardReader.Transmit(apdu); // Get response nescesary if (response.SW1 == 0x61) { apdu = new APDUCommand(0x00, 0xC0, 0x00, 0x00, null, response.SW2); response = cardReader.Transmit(apdu); } if (response.SW1 == 0x90) { ASN1 template = new ASN1(response.Data); ASN1 aip = null; ASN1 afl = null; // Primative response (Template Format 1) if (template.Tag[0] == 0x80) { byte[] tempAIP = new byte[2]; Buffer.BlockCopy(template.Value, 0, tempAIP, 0, 2); aip = new ASN1(0x82, tempAIP); byte[] tempAFL = new byte[template.Length - 2]; Buffer.BlockCopy(template.Value, 2, tempAFL, 0, template.Length - 2); afl = new ASN1(0x94, tempAFL); } // constructed data object response (Template Format 2) if (template.Tag[0] == 0x77) { aip = template.Find(0x82); afl = template.Find(0x94); } // Chop up AFL's for (int i = 0; i < afl.Length; i += 4) { byte[] AFL = new byte[4]; Buffer.BlockCopy(afl.Value, i, AFL, 0, 4); ApplicationFileLocator fileLocator = new ApplicationFileLocator(AFL); applicationFileLocators.Add(fileLocator); } TreeNode aipaflNode = new TreeNode("Application Interchange Profile - Application File Locator"); aipaflNode.ImageIndex = 3; aipaflNode.SelectedImageIndex = 3; aipaflNode.Tag = "aip"; applicationNode.Nodes.Add(aipaflNode); ASN1 aipafl = new ASN1(response.Data); AddRecordNodes(aipafl, aipaflNode); foreach (ApplicationFileLocator file in applicationFileLocators) { int r = file.FirstRecord;// +afl.OfflineRecords; // We'll read SDA records too int lr = file.LastRecord; byte p2 = (byte)((file.SFI << 3) | 4); TreeNode efNode = new TreeNode(String.Format("Elementary File - {0:X2}", file.SFI)); efNode.ImageIndex = 2; efNode.SelectedImageIndex = 2; efNode.Tag = file.SFI; applicationNode.Nodes.Add(efNode); while (r <= lr) { apdu = new APDUCommand(0x00, 0xB2, (byte)r, p2, null, 0x00); response = cardReader.Transmit(apdu); // Retry with correct length if (response.SW1 == 0x6C) { apdu = new APDUCommand(0x00, 0xB2, (byte)r, p2, null, response.SW2); response = cardReader.Transmit(apdu); } TreeNode recordNode = new TreeNode(String.Format(" Record - {0:X2}", r)); if (r <= file.OfflineRecords) { recordNode.ImageIndex = 5; recordNode.SelectedImageIndex = 5; } else { recordNode.ImageIndex = 4; recordNode.SelectedImageIndex = 4; } recordNode.Tag = r; efNode.Nodes.Add(recordNode); ASN1 record = new ASN1(response.Data); AddRecordNodes(record, recordNode); r++; } } //IEnumerable<XElement> tags = tagsDocument.Descendants().Where(el => el.Name == "Tag"); //foreach (XElement element in tags) //{ // string tag = element.Attribute("Tag").Value; // // Only try GET_DATA on two byte tags // if (tag.Length == 4) // { // byte p1 = byte.Parse(tag.Substring(0, 2), NumberStyles.HexNumber); // byte p2 = byte.Parse(tag.Substring(2, 2), NumberStyles.HexNumber); // apdu = new APDUCommand(0x80, 0xCA, p1, p2, null, 0); // response = cardReader.Transmit(apdu); // if (response.SW1 == 0x90) // { // Debug.WriteLine(response.ToString()); // } // } //} apdu = new APDUCommand(0x80, 0xCA, 0x9f, 0x13, null, 0); response = cardReader.Transmit(apdu); Debug.WriteLine(response.ToString()); apdu = new APDUCommand(0x80, 0xCA, 0x9f, 0x17, null, 0); response = cardReader.Transmit(apdu); Debug.WriteLine(response.ToString()); apdu = new APDUCommand(0x80, 0xCA, 0x9f, 0x36, null, 0); response = cardReader.Transmit(apdu); Debug.WriteLine(response.ToString()); } else { // Unexpected status word UpdateStatusLabel(String.Format("Unexpected response from GET PROCESSING OPTIONS command: 0x{0:X2}{1:X2}", response.SW1, response.SW2)); } } else { // Unexpected status word UpdateStatusLabel(String.Format("Unexpected response from SELECT command: 0x{0:X2}{1:X2}", response.SW1, response.SW2)); } cardReader.Disconnect(); } } treeViewData.Invoke(new UpdateTreeViewDelegate(UpdateTreeView), new object[] { treeView }); } catch (PCSCException ex) { UpdateStatusLabel(ex.Message); return; } finally { UseHourglass(false); UpdateStatusLabel("Ready"); } }
/// <summary> /// Авторизация карты /// </summary> private void Authorize() { byte[] data = { 0x01, 0x00, 0x01, 0x60, 0x00 }; APDUCommand apduCmd1 = new APDUCommand(0xFF, 0x86, 0x00, 0x00, data, 0x05); APDUResponse apduResp1 = _card.Transmit(apduCmd1); }
/// <summary> /// Загрузка ключа FFFFFFFFFFFF /// </summary> private void LoadKey() { byte[] data = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; APDUCommand apduCmd = new APDUCommand(0xFF, 0x82, 0x20, 0x00, data, 0x06); APDUResponse apduResp = _card.Transmit(apduCmd); }