|
| 1 | +using LibLogicalAccess; |
| 2 | +using LibLogicalAccess.Card; |
| 3 | +using LibLogicalAccess.Reader; |
| 4 | +using System; |
| 5 | + |
| 6 | +namespace RWCard_DESFire |
| 7 | +{ |
| 8 | + public partial class AuthWithSAMSample : LLASample |
| 9 | + { |
| 10 | + public AuthWithSAMSample() |
| 11 | + { |
| 12 | + InitializeComponent(); |
| 13 | + } |
| 14 | + |
| 15 | + private DESFireCommands cmd = null; |
| 16 | + |
| 17 | + public byte GetUnlockKeyNo() |
| 18 | + { |
| 19 | + return (byte)nupKeyNo.Value; |
| 20 | + } |
| 21 | + |
| 22 | + public DESFireKey GetUnlockKey() |
| 23 | + { |
| 24 | + var key = new DESFireKey(); |
| 25 | + key.setKeyType(DESFireKeyType.DF_KEY_AES); |
| 26 | + key.fromString(tbxKeyValue.Text); |
| 27 | + return key; |
| 28 | + } |
| 29 | + |
| 30 | + public byte GetKeySlot() |
| 31 | + { |
| 32 | + return (byte)nupKeySlot.Value; |
| 33 | + } |
| 34 | + |
| 35 | + private void btnInit_Click(object sender, System.EventArgs e) |
| 36 | + { |
| 37 | + var samConfig = readerSelectionControl.GetReaderConfiguration(); |
| 38 | + |
| 39 | + var ru = ReaderConfig.getReaderUnit() as ISO7816ReaderUnit; |
| 40 | + if (ru == null) |
| 41 | + throw new Exception("The Reader Unit needs to be of type ISO7816."); |
| 42 | + |
| 43 | + // We let know the contactless reader about the SAM reader settings |
| 44 | + var ruConfig = ru.getConfiguration() as ISO7816ReaderUnitConfiguration; |
| 45 | + ruConfig.setSAMReaderName(samConfig.getReaderUnit().getName()); |
| 46 | + ruConfig.setSAMUnlockKey(GetUnlockKey(), GetUnlockKeyNo()); |
| 47 | + ruConfig.setSAMType("SAM_AV2"); |
| 48 | + ru.setConfiguration(ruConfig); |
| 49 | + |
| 50 | + // This is going to connect/unlock the SAM reader/chip as well |
| 51 | + ru.connectToReader(); |
| 52 | + if (ru.waitInsertion(dfparams.GetTimeout())) |
| 53 | + { |
| 54 | + ru.connect(); |
| 55 | + var chip = ru.getSingleChip(); |
| 56 | + if (chip.getGenericCardType() != "DESFire") |
| 57 | + throw new Exception("DESFire chip required"); |
| 58 | + |
| 59 | + cmd = (chip as DESFireChip).getDESFireCommands(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private void btnRunAuthenticate_Click(object sender, System.EventArgs e) |
| 64 | + { |
| 65 | + cmd.selectApplication(dfparams.GetApplicationID()); |
| 66 | + |
| 67 | + var key = dfparams.GetKey(); |
| 68 | + // Here is where we define to use the SAM for furthers key usage (authenticate/changeKey commands) |
| 69 | + var ks = new SAMKeyStorage(); |
| 70 | + ks.setKeySlot(GetKeySlot()); |
| 71 | + key.setKeyStorage(ks); |
| 72 | + |
| 73 | + cmd.authenticate(dfparams.GetKeyNo(), key); |
| 74 | + } |
| 75 | + |
| 76 | + private void btnUnInit_Click(object sender, System.EventArgs e) |
| 77 | + { |
| 78 | + var ru = ReaderConfig.getReaderUnit(); |
| 79 | + ru.disconnect(); |
| 80 | + ru.waitRemoval(dfparams.GetTimeout()); |
| 81 | + // Will release SAM resources too |
| 82 | + ru.disconnectFromReader(); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments