//this method will open a reader instance and read the nfc device with //cmd as its input and the command to run. it will return a string of the result. //@params cmd - the command to run private string readNFC(CmdApdu cmd) { PCSCReader reader = new PCSCReader(); string payload = ""; try { reader.Connect(); reader.ActivateCard(GS.SCard.Const.SCARD_SHARE_MODE.Exclusive, GS.SCard.Const.SCARD_PROTOCOL.Tx); RespApdu respApdu = reader.Exchange(cmd, 0x9000); if (respApdu.SW1SW2 == 0x9000) { payload = Encoding.UTF8.GetString(respApdu.Data); } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { reader.Disconnect(); } return(payload); }
/// <summary> /// The function Exchange sends a command APDU to a smart card and returns the response ADPU from the card. /// </summary> /// <param name="cmdApdu">The command ADPU.</param> /// <param name="expectedSW1SW2">The expected status word SW1SW2.</param> /// <returns>The response APDU.</returns> public RespApdu Exchange(CmdApdu cmdApdu, ushort?expectedSW1SW2) { byte[] responseBuffer = this.Exchange(cmdApdu.GetBytes(), expectedSW1SW2); RespApdu respApdu = new RespApdu(responseBuffer); return(respApdu); }
/// <summary> /// The function Exchange sends a command APDU to a smart card and returns the response ADPU from the card. /// </summary> /// <param name="sendBuffer">The command ADPU.</param> /// <param name="sendLength">Length of the command ADPU.</param> /// <param name="responseBuffer">The response APDU.</param> /// <param name="responseLength">Length of the response APDU.</param> /// <param name="expectedSW1SW2">The expected status word SW1SW2.</param> public void Exchange(byte[] sendBuffer, int sendLength, out byte[] responseBuffer, out int responseLength, ushort?expectedSW1SW2) { responseBuffer = null; CmdApdu cmdApud; if (sendBuffer.Length == sendLength) { cmdApud = new CmdApdu(sendBuffer); } else { byte[] baSendTemp = new byte[sendLength]; Array.Copy(sendBuffer, baSendTemp, sendLength); cmdApud = new CmdApdu(baSendTemp); } int respBufferSize = 2; if ((cmdApud.Le != null)) { respBufferSize = (int)cmdApud.Le + 2; } byte[] baTempResp = new byte[respBufferSize]; responseLength = baTempResp.Length; this.Exchange(sendBuffer, sendLength, baTempResp, ref responseLength, expectedSW1SW2); responseBuffer = new byte[responseLength]; Array.Copy(baTempResp, responseBuffer, responseLength); }
//This method is what is in change of authenticating a user //@params receives session details like username and password //@return is a boolean value: true if conditions are satisfied, false otherwise public BooleanResult AuthenticateUser(SessionProperties properties) { //object that will passed to windows authentication UserInformation userInfo = properties.GetTrackedSingle <UserInformation>(); //Aid selection for getting token from unlocked phone byte[] tokenAID = { 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0 }; CmdApdu selectApplication = new CmdApdu(); selectApplication.CLA = 0x00; selectApplication.INS = 0xA4; selectApplication.P1 = 0x04; selectApplication.P2 = 0x00; selectApplication.Data = tokenAID; selectApplication.Le = 0x00; string userTokenInput = readNFC(selectApplication); //get device id string devId = readDevId(); //check if the devid is registered to any account if (validDevId(devId)) { //get token info and verify string userToken = getUserToken(devId); Boolean validToken = verifyToken(userTokenInput, userToken); //get username via device id userInfo.Username = EncryptDecrypt.Decrypt(getUserName(devId)); //check if the token is associated to an account if (validToken) { //check if pincode matches db pincode with the same devid if (validPin(userInfo.Password, userToken)) { //get account details to send to winlogon userInfo.Password = getUserPin(userToken); // Successful authentication DBLogger(userInfo.Username, "Sucessful Login"); return(new BooleanResult() { Success = true }); } } // Authentication failure DBLogger(userInfo.Username, "Unsucessful Login attempt"); } return(new BooleanResult() { Success = false, Message = "Incorrect credentials." }); }
private void BtnPhoneKey_Click(object sender, EventArgs e) { //Aid selection for getting token from unlocked phone byte[] tokenAID = { 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0 }; CmdApdu selectApplication = new CmdApdu(); selectApplication.CLA = 0x00; selectApplication.INS = 0xA4; selectApplication.P1 = 0x04; selectApplication.P2 = 0x00; selectApplication.Data = tokenAID; selectApplication.Le = 0x00; txtBoxPhoneKey.Text = readNFC(selectApplication); }
private void Button1_Click(object sender, EventArgs e) { CmdApdu uid = new CmdApdu(); uid.CLA = 0xFF; uid.INS = 0xCA; uid.P1 = 0x01; uid.P2 = 0x00; uid.Le = 0x04; string devId = readNFC(uid); txtBoxDevId.Text = devId; }
//this method will get the devid from the nfc device private string readDevId() { CmdApdu uid = new CmdApdu(); uid.CLA = 0xFF; uid.INS = 0xCA; uid.P1 = 0x01; uid.P2 = 0x00; uid.Le = 0x04; string devId = readNFC(uid); return(devId); }
//note 2 self ushort? and Nullable<ushort> are the same public RespApdu Exchange(CmdApdu cmdApdu, Nullable<ushort> expectedSW1SW2) { byte[] responseBuffer = this.Exchange(cmdApdu.GetBytes(), expectedSW1SW2); RespApdu respApdu = new RespApdu(responseBuffer); return respApdu; }
/// <summary> /// The function Exchange sends a command APDU to a smart card and returns the response ADPU from the card. /// </summary> /// <param name="cmdApdu">The command ADPU.</param> /// <returns>The response APDU.</returns> public RespApdu Exchange(CmdApdu cmdApdu) { return this.Exchange(cmdApdu, null); }
/// <summary> /// The function Exchange sends a command APDU to a smart card and returns the response ADPU from the card. /// </summary> /// <param name="sendBuffer">The command ADPU.</param> /// <param name="sendLength">Length of the command ADPU.</param> /// <param name="responseBuffer">The response APDU.</param> /// <param name="responseLength">Length of the response APDU.</param> /// <param name="expectedSW1SW2">The expected status word SW1SW2.</param> public void Exchange( byte[] sendBuffer, int sendLength, out byte[] responseBuffer, out int responseLength, ushort? expectedSW1SW2 ) { responseBuffer = null; CmdApdu cmdApud; if (sendBuffer.Length == sendLength) { cmdApud = new CmdApdu( sendBuffer ); } else { byte[] baSendTemp = new byte[sendLength]; Array.Copy(sendBuffer, baSendTemp, sendLength); cmdApud = new CmdApdu(baSendTemp); } int respBufferSize = 2; if((cmdApud.Le != null) ) { respBufferSize = (int)cmdApud.Le + 2; } byte[] baTempResp = new byte[respBufferSize]; responseLength = baTempResp.Length; this.Exchange(sendBuffer, sendLength, baTempResp, ref responseLength, expectedSW1SW2); responseBuffer = new byte[responseLength]; Array.Copy(baTempResp, responseBuffer, responseLength); }
/// <summary> /// The function Exchange sends a command APDU to a smart card and returns the response ADPU from the card. /// </summary> /// <param name="cmdApdu">The command ADPU.</param> /// <returns>The response APDU.</returns> public RespApdu Exchange(CmdApdu cmdApdu) { return(this.Exchange(cmdApdu, null)); }