void scanAndAddBlacklist(IntPtr sdkContext, UInt32 deviceID, List <BS2BlackList> blackList, int amount) { cbCardOnReadyToScan = new API.OnReadyToScan(ReadyToScanForCard); BS2Card card = Util.AllocateStructure <BS2Card>(); BS2BlackList blacklistItem = Util.AllocateStructure <BS2BlackList>(); for (int idx = 0; idx < amount; ++idx) { Console.WriteLine("Trying to scan card."); BS2ErrorCode result = (BS2ErrorCode)API.BS2_ScanCard(sdkContext, deviceID, out card, cbCardOnReadyToScan); if (result != BS2ErrorCode.BS_SDK_SUCCESS) { Console.WriteLine("Got error({0}).", result); return; } else if (Convert.ToBoolean(card.isSmartCard)) { BS2SmartCardData smartCard = Util.ConvertTo <BS2SmartCardData>(card.cardUnion); Array.Copy(smartCard.cardID, blacklistItem.cardID, BS2Envirionment.BS2_CARD_DATA_SIZE); blacklistItem.issueCount = smartCard.header.issueCount; } else { BS2CSNCard csnCard = Util.ConvertTo <BS2CSNCard>(card.cardUnion); Array.Copy(csnCard.data, blacklistItem.cardID, BS2Envirionment.BS2_CARD_DATA_SIZE); blacklistItem.issueCount = 0; //should set count of issue to 0 } bool isDuplicated = false; foreach (BS2BlackList item in blackList) { if (blacklistItem.cardID.SequenceEqual(item.cardID)) { isDuplicated = true; break; } } if (isDuplicated) { Console.WriteLine("Already exist blacklist. try again"); --idx; continue; } blackList.Add(blacklistItem); } cbCardOnReadyToScan = null; }
public void scanCard(IntPtr sdkContext, UInt32 deviceID, bool isMasterDevice) { BS2Card card; cbCardOnReadyToScan = new API.OnReadyToScan(ReadyToScanForCard); Console.WriteLine("Trying to scan card."); BS2ErrorCode result = (BS2ErrorCode)API.BS2_ScanCard(sdkContext, deviceID, out card, cbCardOnReadyToScan); if (result != BS2ErrorCode.BS_SDK_SUCCESS) { Console.WriteLine("Got error({0}).", result); } else { if (Convert.ToBoolean(card.isSmartCard)) { UInt16 hdrCRC; UInt16 cardCRC; BS2SmartCardData smartCard = Util.ConvertTo <BS2SmartCardData>(card.cardUnion); result = computeCRC(smartCard, out hdrCRC, out cardCRC); if (result != BS2ErrorCode.BS_SDK_SUCCESS) { Console.WriteLine("Can't compute CRC16({0})", result); } else if (smartCard.header.hdrCRC != hdrCRC) { Console.WriteLine("Get a header crc mismatch(expected[{0}] computed[{1}])", smartCard.header.hdrCRC, hdrCRC); } else if (smartCard.header.cardCRC != cardCRC) { Console.WriteLine("Get a card crc mismatch(expected[{0}] computed[{1}])", smartCard.header.cardCRC, cardCRC); } else { print(sdkContext, smartCard); } } else { BS2CSNCard csnCard = Util.ConvertTo <BS2CSNCard>(card.cardUnion); print(sdkContext, csnCard); } } cbCardOnReadyToScan = null; }
void print(IntPtr sdkContext, BS2SmartCardData smartCard) { byte[] cardIDArray = new byte[8]; for (int idx = 0; idx < 8; ++idx) { cardIDArray[idx] = smartCard.cardID[BS2Environment.BS2_CARD_DATA_SIZE - idx - 1]; } if (!BitConverter.IsLittleEndian) { Array.Reverse(cardIDArray); } UInt64 cardID = BitConverter.ToUInt64(cardIDArray, 0); BS2CardTypeEnum cardType = (BS2CardTypeEnum)smartCard.header.cardType; Console.WriteLine(">>>> SmartCard type[{0}]", cardType); Console.WriteLine(" |--cardID[{0}]", cardID); Console.WriteLine(" |--numOfTemplate[{0}]", smartCard.header.numOfTemplate); Console.WriteLine(" |--templateSize[{0}]", smartCard.header.templateSize); Console.WriteLine(" |--issueCount[{0}]", smartCard.header.issueCount); Console.WriteLine(" |--duressMask[{0}]", (BS2FingerprintFlagEnum)smartCard.header.duressMask); if (cardType == BS2CardTypeEnum.ACCESS) { Console.WriteLine(" |--accessOnCard"); Console.WriteLine(" | |--accessGroups"); for (int idx = 0; idx < BS2Environment.BS2_SMART_CARD_MAX_ACCESS_GROUP_COUNT; ++idx) { if (smartCard.accessOnData.accessGroupID[idx] != 0) { Console.WriteLine(" | | |--accessGroupID[{0}]", smartCard.accessOnData.accessGroupID[idx]); } else { break; } } Console.WriteLine(" | |--startTime[{0}]", Util.ConvertFromUnixTimestamp(smartCard.accessOnData.startTime).ToString("yyyy-MM-dd HH:mm:ss")); Console.WriteLine(" | |--endTime[{0}]", Util.ConvertFromUnixTimestamp(smartCard.accessOnData.endTime).ToString("yyyy-MM-dd HH:mm:ss")); } }
BS2ErrorCode computeCRC(BS2SmartCardData smartCard, out UInt16 hdrCRC, out UInt16 cardCRC) { BS2ErrorCode result = BS2ErrorCode.BS_SDK_SUCCESS; IntPtr smartCardObj = Marshal.AllocHGlobal(Marshal.SizeOf(smartCard)); int cardTypeOffset = (int)Marshal.OffsetOf(typeof(BS2SmartCardHeader), "cardType"); int cardCRCOffset = (int)Marshal.OffsetOf(typeof(BS2SmartCardHeader), "cardCRC"); IntPtr cardDataObj = smartCardObj + cardTypeOffset; Marshal.StructureToPtr(smartCard, smartCardObj, false); cardCRC = 0xFFFF; hdrCRC = 0xFFFF; result = (BS2ErrorCode)API.BS2_ComputeCRC16CCITT(cardDataObj, (UInt32)(Marshal.SizeOf(typeof(BS2SmartCardData)) - cardTypeOffset), ref cardCRC); if (result == BS2ErrorCode.BS_SDK_SUCCESS) { Marshal.WriteInt16(smartCardObj, cardCRCOffset, (Int16)cardCRC); IntPtr cardCrcObj = smartCardObj + cardCRCOffset; result = (BS2ErrorCode)API.BS2_ComputeCRC16CCITT(cardCrcObj, (UInt32)(Marshal.SizeOf(typeof(BS2SmartCardHeader)) - cardCRCOffset), ref hdrCRC); } Marshal.FreeHGlobal(smartCardObj); return(result); }
public void writeCard(IntPtr sdkContext, UInt32 deviceID, bool isMasterDevice) { BS2SimpleDeviceInfo deviceInfo; BS2SmartCardData smartCard = Util.AllocateStructure <BS2SmartCardData>(); BS2ErrorCode result = (BS2ErrorCode)API.BS2_GetDeviceInfo(sdkContext, deviceID, out deviceInfo); if (result != BS2ErrorCode.BS_SDK_SUCCESS) { Console.WriteLine("Can't get device information(errorCode : {0}).", result); return; } Console.WriteLine("choose the format for the card: [2: SecureCredential(default), 3: AccessOn]"); Console.Write(">>>> "); smartCard.header.cardType = Util.GetInput((byte)BS2CardTypeEnum.SECURE); if (Convert.ToBoolean(deviceInfo.pinSupported)) { Console.WriteLine("Do you want to set pin code? [y/N]"); Console.Write(">>>> "); if (!Util.IsNo()) { Console.WriteLine("Enter the pin code which you want to set"); Console.Write(">>>> "); string pinCodeStr = Console.ReadLine(); IntPtr pinChar = Marshal.StringToHGlobalAnsi(pinCodeStr); IntPtr pinCode = Marshal.AllocHGlobal(BS2Environment.BS2_PIN_HASH_SIZE); //result = (BS2ErrorCode)API.BS2_MakePinCode(sdkContext, pinCodeStr, pinCode); result = (BS2ErrorCode)API.BS2_MakePinCode(sdkContext, pinChar, pinCode); if (result != BS2ErrorCode.BS_SDK_SUCCESS) { Console.WriteLine("Got error({0}).", result); Marshal.FreeHGlobal(pinCode); return; } Marshal.Copy(pinCode, smartCard.credentials.pin, 0, BS2Environment.BS2_PIN_HASH_SIZE); Marshal.FreeHGlobal(pinChar); Marshal.FreeHGlobal(pinCode); } } if (Convert.ToBoolean(deviceInfo.fingerSupported)) { Console.WriteLine("How many fingerprint templates do you want to set? [0(default)-{0}]", BS2Environment.BS2_SMART_CARD_MAX_TEMPLATE_COUNT); Console.Write(">>>> "); smartCard.header.numOfTemplate = Util.GetInput((byte)0); } else { smartCard.header.numOfTemplate = 0; } Array.Clear(smartCard.credentials.templateData, 0, BS2Environment.BS2_SMART_CARD_MAX_TEMPLATE_COUNT * BS2Environment.BS2_FINGER_TEMPLATE_SIZE); if (smartCard.header.numOfTemplate > 0) { Console.WriteLine("Enter the size of template which you want to set. [{0}(default)-{1}]", BS2Environment.BS2_SMART_CARD_MIN_TEMPLATE_SIZE, BS2Environment.BS2_FINGER_TEMPLATE_SIZE); Console.Write(">>>> "); smartCard.header.templateSize = Util.GetInput((UInt16)BS2Environment.BS2_SMART_CARD_MIN_TEMPLATE_SIZE); BS2Fingerprint fingerprint = Util.AllocateStructure <BS2Fingerprint>(); fingerprint.index = 0; UInt32 outquality; for (byte idx = 0; idx < smartCard.header.numOfTemplate; ++idx) { Console.WriteLine("Place your finger on the device for fingerprint template[{0}] extraction", idx); result = (BS2ErrorCode)API.BS2_ScanFingerprintEx(sdkContext, deviceID, ref fingerprint, 0, (UInt32)BS2FingerprintQualityEnum.QUALITY_STANDARD, (byte)BS2FingerprintTemplateFormatEnum.FORMAT_SUPREMA, out outquality, null); if (result != BS2ErrorCode.BS_SDK_SUCCESS) { if (result == BS2ErrorCode.BS_SDK_ERROR_EXTRACTION_LOW_QUALITY || result == BS2ErrorCode.BS_SDK_ERROR_CAPTURE_LOW_QUALITY) { Console.WriteLine("Bad fingerprint quality. Tty agin."); continue; } else { Console.WriteLine("Got error({0}).", result); return; } } // Fixes a problem when issuing AOC cards. //Array.Copy(fingerprint.data, 0, smartCard.credentials.templateData, idx*smartCard.header.templateSize, smartCard.header.templateSize); Array.Copy(fingerprint.data, 0, smartCard.credentials.templateData, idx * BS2Environment.BS2_FINGER_TEMPLATE_SIZE, BS2Environment.BS2_FINGER_TEMPLATE_SIZE); } Console.WriteLine("Is it duress finger? [0 : Normal(default), 1 : Duress]"); Console.Write(">>>> "); smartCard.header.duressMask = Util.GetInput((byte)BS2FingerprintFlagEnum.NORMAL); } else { smartCard.header.templateSize = (UInt16)BS2Environment.BS2_SMART_CARD_MIN_TEMPLATE_SIZE; smartCard.header.duressMask = (byte)BS2FingerprintFlagEnum.NORMAL; } Console.WriteLine("Enter the issue count which you want to set"); Console.Write(">>>> "); smartCard.header.issueCount = (UInt16)Util.GetInput(); Console.WriteLine("Enter the card id which you want to write on the card"); Console.Write(">>>> "); UInt64 cardID = 0; string cardIDStr = Console.ReadLine(); if (cardIDStr.Length > BS2Environment.BS2_CARD_DATA_SIZE) { Console.WriteLine("Card id should less than {0} words.", BS2Environment.BS2_CARD_DATA_SIZE); return; } else if (!UInt64.TryParse(cardIDStr, out cardID) || cardID == 0) { Console.WriteLine("Invalid card id"); return; } else { byte[] cardIDArray = BitConverter.GetBytes(cardID); if (!BitConverter.IsLittleEndian) { Array.Reverse(cardIDArray); } Array.Clear(smartCard.cardID, 0, BS2Environment.BS2_CARD_DATA_SIZE); for (int idx = 0; idx < cardIDArray.Length; ++idx) { smartCard.cardID[BS2Environment.BS2_CARD_DATA_SIZE - idx - 1] = cardIDArray[idx]; } } if ((BS2CardTypeEnum)smartCard.header.cardType == BS2CardTypeEnum.ACCESS) { Console.WriteLine("Enter the ID of the access group which you want to remove: [ID_1,ID_2 ...]"); Console.Write(">>>> "); char[] delimiterChars = { ' ', ',', '.', ':', '\t' }; string[] accessGroupIDs = Console.ReadLine().Split(delimiterChars); int idx = 0; Array.Clear(smartCard.accessOnData.accessGroupID, 0, BS2Environment.BS2_SMART_CARD_MAX_ACCESS_GROUP_COUNT); foreach (string accessGroupID in accessGroupIDs) { if (accessGroupID.Length > 0) { UInt16 item; if (UInt16.TryParse(accessGroupID, out item)) { smartCard.accessOnData.accessGroupID[idx++] = item; if (idx >= BS2Environment.BS2_SMART_CARD_MAX_ACCESS_GROUP_COUNT) { break; } } } } Console.WriteLine("Enter start time which you want to set. [default(Today), yyyy-MM-dd HH:mm:ss]"); Console.Write(">>>> "); if (!Util.GetTimestamp("yyyy-MM-dd HH:mm:ss", 0, out smartCard.accessOnData.startTime)) { return; } Console.WriteLine("Enter end time which you want to set. [default(Today), yyyy-MM-dd HH:mm:ss]"); Console.Write(">>>> "); if (!Util.GetTimestamp("yyyy-MM-dd HH:mm:ss", 0, out smartCard.accessOnData.endTime)) { return; } // card auth mode //Console.WriteLine("Enter card authentication mode which you want to set:"); //Console.WriteLine(" [2: Card only, 3: Card+Biometric, 4: Card+PIN, 5: Card+Biometric/PIN, 6: Card+Biometric+PIN]"); //Console.Write(">>>> "); //smartCard.header.cardAuthMode = Util.GetInput((byte)BS2CardAuthModeEnum.NONE); } result = computeCRC(smartCard, out smartCard.header.hdrCRC, out smartCard.header.cardCRC); if (result != BS2ErrorCode.BS_SDK_SUCCESS) { Console.WriteLine("Can't compute CRC16({0})", result); } else { Console.WriteLine("Trying to write card."); result = (BS2ErrorCode)API.BS2_WriteCard(sdkContext, deviceID, ref smartCard); if (result != BS2ErrorCode.BS_SDK_SUCCESS) { Console.WriteLine("Got error({0}).", result); } } }
extern public static int BS2_WriteCard(IntPtr context, UInt32 deviceId, ref BS2SmartCardData smartCard);