public bool DecValueCounter(ref ushort counter, ref LIB_ERR error) { bool Result = false; counter = 0x00; error = LIB_ERR.NO_ERROR; byte[] capdu = new byte[] { 0xFF, 0xC2, 0x00, 0x01, 0x0C, 0x5F, 0x46, 0x04, 0x40, 0x42, 0x0F, 0x00, 0x95, 0x03, 0xD0, 0x01, 0x00 }; byte[] rapdu = new byte[32]; try { Result = (reader.Transmit(capdu, ref rapdu) == SCardError.Success); Result = Result & (rapdu.Length >= 20) && (rapdu[12] == 0x97) && (rapdu[13] == 0x04); if (Result) { counter = BitConverter.ToUInt16(rapdu, 14); } } catch { Result = false; } if (!Result) { error = LIB_ERR.ACS_DEC_VALUE_COUNTER; } return(Result); }
public bool SetValueCounter(ushort counter, ref LIB_ERR error) { bool Result = false; error = LIB_ERR.NO_ERROR; byte[] value = new byte[2]; RPTUtils.LEU16(counter, ref value, 0); byte[] capdu = new byte[] { 0xFF, 0xC2, 0x00, 0x01, 0x13, 0x5F, 0x46, 0x04, 0x40, 0x42, 0x0F, 0x00, 0x95, 0x0A, 0xA1, 0x22, value[0], (byte)(value[0] ^ 0xFF), value[1], 0x00, 0xFF, 0xFF, 0xFF, 0xFF }; byte[] rapdu = new byte[32]; try { Result = (reader.Transmit(capdu, ref rapdu) == SCardError.Success); Result = Result && (rapdu.Length >= 17) && (rapdu[12] == 0x97) && (rapdu[13] == 0x01) && (rapdu[14] == ACK); } catch { Result = false; } if (!Result) { error = LIB_ERR.CRT571_SET_VALUE_COUNTER; } return(Result); }
public bool WriteSectorData(byte pageNo, byte[] data, ref LIB_ERR error) { bool Result = true; error = LIB_ERR.NO_ERROR; byte[] capdu = new byte[] { 0xFF, 0xC2, 0x00, 0x01, 0x0F, 0x5F, 0x46, 0x04, 0x40, 0x42, 0x0F, 0x00, 0x95, 0x06, 0xA2, pageNo, 0x00, 0x00, 0x00, 0x00 }; Array.Copy(data, 0, capdu, capdu.Length - data.Length, data.Length); byte[] rapdu = new byte[32]; try { Result = (reader.Transmit(capdu, ref rapdu) == SCardError.Success) && (rapdu.Length >= 14 + 2) && (rapdu[14] == ACK) && (rapdu[rapdu.Length - 2] == 0x90) && (rapdu[rapdu.Length - 1] == 0x00); } catch { Result = false; } if (!Result) { error = LIB_ERR.ACS_WRITE_SECTOR_DATA; } return(Result); }
public bool Access(byte[] password, ref LIB_ERR error) { bool Result = false; error = LIB_ERR.NO_ERROR; byte[] capdu = new byte[] { 0xFF, 0xC2, 0x00, 0x01, 0x0E, 0x5F, 0x46, 0x04, 0x40, 0x42, 0x0F, 0x00, 0x95, 0x05, 0xB2, 0x00, 0x00, 0x00, 0x00 }; Array.Copy(password, 0, capdu, capdu.Length - password.Length, password.Length); byte[] rapdu = new byte[32]; try { Result = (reader.Transmit(capdu, ref rapdu) == SCardError.Success); Result = Result && (rapdu.Length >= 17) && (rapdu[12] == 0x97) && (rapdu[13] == 0x01) && (rapdu[14] == ACK); } catch { Result = false; } if (!Result) { error = LIB_ERR.ACS_ACCESS_DENIED; } return(Result); }
public bool StartTransparentSession(ref LIB_ERR error) { bool Result = false; error = LIB_ERR.NO_ERROR; byte[] capdu = new byte[7] { 0xFF, 0xC2, 0x00, 0x00, 0x02, 0x81, 0x00 }; byte[] rapdu = new byte[32]; try { SCardError ret = reader.Transmit(capdu, ref rapdu); Result = (ret == SCardError.Success) && (rapdu.Length == 7) && (rapdu[rapdu.Length - 2] == 0x90) && (rapdu[rapdu.Length - 1] == 0x00); } catch (Exception E) { Result = false; } if (!Result) { error = LIB_ERR.ACS_START_TRANSPARENT_SESSION; } return(Result); }
public bool ReadSectorData(byte pageNo, ref byte[] data, ref LIB_ERR error) { bool Result = true; error = LIB_ERR.NO_ERROR; byte[] capdu = new byte[] { 0xFF, 0xC2, 0x00, 0x01, 0x0B, 0x5F, 0x46, 0x04, 0x40, 0x42, 0x0F, 0x00, 0x95, 0x02, 0x31, pageNo }; byte[] rapdu = new byte[32]; try { Result = (reader.Transmit(capdu, ref rapdu) == SCardError.Success) && (rapdu.Length >= 14 + rptc.PAGE_LEN + 2) && (rapdu[13] == ACK) && (rapdu[rapdu.Length - 2] == 0x90) && (rapdu[rapdu.Length - 1] == 0x00); if (Result) { Array.Copy(rapdu, 14, data, 0, rptc.PAGE_LEN); } } catch { Result = false; } if (!Result) { error = LIB_ERR.ACS_READ_SECTOR_DATA; } return(Result); }
public bool IsCardOnRFCardOperationPosition() { bool Result = true; SCardError ret = reader.Connect(readers[indexReader], SCardShareMode.Shared, SCardProtocol.T1); Result = (ret == SCardError.Success) && reader.IsConnected; byte[] uid = new byte[7]; byte uidLen = 0; LIB_ERR error = LIB_ERR.NO_ERROR; if (Result) { Result = GetSerialNumber(ref uid, ref uidLen, ref error); Result = Result && (error == LIB_ERR.NO_ERROR); } else { error = LIB_ERR.CONNECT_CARD_FAILED; } return(Result); }
public bool CardConnect(ref byte[] uid, ref byte uidLen, ref LIB_ERR error) { bool Result = true; try { SCardError ret = reader.Connect(readers[indexReader], SCardShareMode.Shared, SCardProtocol.T1); Result = (ret == SCardError.Success) && reader.IsConnected; } catch { Result = false; } if (Result) { Result = GetSerialNumber(ref uid, ref uidLen, ref error) && StartTransparentSession(ref error) && ActivateISO14443_3A(ref error); } else { error = LIB_ERR.CONNECT_CARD_FAILED; } return(Result); }
public bool GetSAMSerialNo(ref byte[] samId, ref LIB_ERR errSam) { bool result = false; try { result = rpt.GetSAMSerialNo(ref samId, ref errSam); } catch (Exception ex) { } return(result); }
public bool Personalize(out byte[] uid, RPTPersoSector persoSector) { bool ret = false; byte[] cardUid = null; byte cardUidLen = 0; LIB_ERR err = LIB_ERR.NO_ERROR; ret = rpt.Do(persoSector, ref cardUid, ref cardUidLen, ref err); uid = new byte[cardUidLen]; Array.Copy(cardUid, uid, uid.Length); return(ret); }
public bool ActivateISO14443_3A(ref LIB_ERR error) { bool Result = false; error = LIB_ERR.NO_ERROR; byte[] capdu = new byte[9] { 0xFF, 0xC2, 0x00, 0x02, 0x04, 0x8F, 0x02, 0x00, 0x03 }; byte[] rapdu = new byte[64]; try { SCardError ret = reader.Transmit(capdu, ref rapdu); Result = (ret == SCardError.Success) && (rapdu.Length > 7) && (rapdu[rapdu.Length - 2] == 0x90) && (rapdu[rapdu.Length - 1] == 0x00); } catch (Exception E) { Result = false; } if (Result) { capdu = new byte[15] { 0xFF, 0xC2, 0x00, 0x01, 0x0A, 0x90, 0x02, 0x10, 0x00, 0xFF, 0x6E, 0x03, 0x07, 0x01, 0x0A }; Array.Resize <byte>(ref rapdu, 128); try { SCardError ret = reader.Transmit(capdu, ref rapdu); Result = (ret == SCardError.Success) && (rapdu.Length == 7) && (rapdu[rapdu.Length - 2] == 0x90) && (rapdu[rapdu.Length - 1] == 0x00); } catch (Exception E) { Result = false; } } if (!Result) { error = LIB_ERR.ACS_ACTIVATE_ISO14443_3A; } return(Result); }
public bool GetSerialNumber(ref byte[] uid, ref byte uidLen, ref LIB_ERR error) { bool Result = false; error = LIB_ERR.NO_ERROR; uidLen = 0; byte[] capdu = new byte[5] { 0xFF, 0xCA, 0x00, 0x00, 0x00 }; byte[] rapdu = new byte[32]; try { SCardError ret = reader.Transmit(capdu, ref rapdu); Result = (ret == SCardError.Success) && ((rapdu.Length == 4 + 2) || (rapdu.Length == 7 + 2)) && (rapdu[rapdu.Length - 2] == 0x90) && (rapdu[rapdu.Length - 1] == 0x00); if (Result) { uidLen = Convert.ToByte(rapdu.Length - 2); uid = new byte[uidLen]; Array.Copy(rapdu, 0, uid, 0, uidLen); } else { error = LIB_ERR.GET_CARD_UID_FAILED; } } catch (Exception E) { error = LIB_ERR.GET_CARD_UID_FAILED; Result = false; } return(Result); }
public static LIB_ERR SAMToLibErr(SAM_OPR_ERR err) { LIB_ERR Result = LIB_ERR.UNKNOWN; switch (err) { case SAM_OPR_ERR.NO_ERROR: Result = LIB_ERR.NO_ERROR; break; case SAM_OPR_ERR.UNLOCK_PART_1: Result = LIB_ERR.SAM_UNLOCK_PART_1; break; case SAM_OPR_ERR.UNLOCK_PART_2: Result = LIB_ERR.SAM_UNLOCK_PART_2; break; case SAM_OPR_ERR.UNLOCK_PART_3: Result = LIB_ERR.SAM_UNLOCK_PART_3; break; case SAM_OPR_ERR.UNLOCK_MAC_VERIFY: Result = LIB_ERR.SAM_UNLOCK_MAC_VERIFY; break; case SAM_OPR_ERR.ACTIVATE_OFFLINE_KEY: Result = LIB_ERR.SAM_ACTIVATE_OFFLINE_KEY; break; case SAM_OPR_ERR.GENERATE_MAC: Result = LIB_ERR.SAM_GENERATE_MAC; break; case SAM_OPR_ERR.ENCHIPHER_OFFLINE_DATA: Result = LIB_ERR.SAM_ENCHIPHER_OFFLINE_DATA; break; case SAM_OPR_ERR.DECHIPER_OFFLINE_DATA: Result = LIB_ERR.SAM_DECHIPER_OFFLINE_DATA; break; default: Result = LIB_ERR.UNKNOWN; break; } return Result; }
public bool SetPassword(byte[] password, ref LIB_ERR error) { bool Result = false; error = LIB_ERR.NO_ERROR; byte[] capdu = new byte[] { 0xFF, 0xC2, 0x00, 0x01, 0x0E, 0x5F, 0x46, 0x04, 0x40, 0x42, 0x0F, 0x00, 0x95, 0x05, 0xB1, 0x00, 0x00, 0x00, 0x00 }; Array.Copy(password, 0, capdu, capdu.Length - password.Length, password.Length); byte[] rapdu = new byte[32]; try { Result = (reader.Transmit(capdu, ref rapdu) == SCardError.Success); Result = Result && (rapdu.Length >= 22) && (rapdu[12] == 0x97) && (rapdu[13] == 0x06); if (Result) { byte[] rpassword = new byte[password.Length]; Array.Copy(rapdu, 14, rpassword, 0, password.Length); Result = password.SequenceEqual(rpassword); } } catch { Result = false; } if (!Result) { error = LIB_ERR.ACS_SET_PASSWORD; } return(Result); }
public bool Init(ref LIB_ERR error) { bool Result = true; try { context = new SCardContext(); context.Establish(SCardScope.System); string[] groups = context.GetReaderGroups(); Result = (groups != null) && (groups.Length > 0); if (Result) { readers = context.GetReaders(groups); Result = (readers != null) && (readers.Length > 0); } if (!Result) { error = LIB_ERR.THERE_IS_NO_PCSC_READER; } if (Result) { reader = new SCardReader(context); indexReader = Array.FindIndex(readers, element => element.Contains("PICC")); Result = (indexReader > -1); if (!Result) { error = LIB_ERR.CONNECT_READER_FAILED; } } if (Result) { sam = new SCardReader(context); indexSAM = Array.FindIndex(readers, element => element.Contains("SAM")); Result = (indexSAM > -1); if (Result) { SCardError ret = sam.Connect(readers[indexSAM], SCardShareMode.Shared, SCardProtocol.T0 | SCardProtocol.T1); Result = ((ret == SCardError.Success) && sam.IsConnected); } if (!Result) { error = LIB_ERR.CONNECT_SAM_FAILED; } } } catch (Exception E) { Result = false; error = LIB_ERR.GET_READERS_FAILED; } return(Result); }