update demo

This commit is contained in:
Michele Balistreri 2019-02-13 12:22:09 +03:00
parent 2df15f388c
commit 067204c7db
1 changed files with 42 additions and 30 deletions

View File

@ -54,7 +54,13 @@ public class MainActivity extends AppCompatActivity {
} else {
Log.i(TAG, "The card has no master key");
}
Log.i(TAG, String.format("Capabilities: %02X", info.getCapabilities()));
Log.i(TAG, "Has Secure Channel: " + info.hasSecureChannelCapability());
Log.i(TAG, "Has Key Management: " + info.hasKeyManagementCapability());
Log.i(TAG, "Has Credentials Management: " + info.hasCredentialsManagementCapability());
Log.i(TAG, "Has NDEF capability: " + info.hasNDEFCapability());
if (info.hasSecureChannelCapability()) {
// In real projects, the pairing key should be saved and used for all new sessions.
cmdSet.autoPair("KeycardTest");
Pairing pairing = cmdSet.getPairing();
@ -68,6 +74,7 @@ public class MainActivity extends AppCompatActivity {
cmdSet.autoOpenSecureChannel();
Log.i(TAG, "Secure channel opened. Getting applet status.");
}
// We send a GET STATUS command, which does not require PIN authentication
ApplicationStatus status = new ApplicationStatus(cmdSet.getStatus(KeycardCommandSet.GET_STATUS_P1_APPLICATION).checkOK().getData());
@ -76,6 +83,7 @@ public class MainActivity extends AppCompatActivity {
Log.i(TAG, "PUK retry counter: " + status.getPUKRetryCount());
Log.i(TAG, "Has master key: " + status.hasMasterKey());
if (info.hasKeyManagementCapability()) {
// A mnemonic can be generated before PIN authentication. Generating a mnemonic does not create keys on the
// card. a subsequent loadKey step must be performed after PIN authentication. In this example we will only
// show how to convert the output of the card to a usable format but won't actually load the key
@ -87,16 +95,19 @@ public class MainActivity extends AppCompatActivity {
Log.i(TAG, "Generated mnemonic phrase: " + mnemonic.toMnemonicPhrase());
Log.i(TAG, "Binary seed: " + Hex.toHexString(mnemonic.toBinarySeed()));
}
if (info.hasCredentialsManagementCapability()) {
// PIN authentication allows execution of privileged commands
cmdSet.verifyPIN("000000").checkAuthOK();
Log.i(TAG, "Pin Verified.");
}
// If the card has no keys, we generate a new set. Keys can also be loaded on the card starting from a binary
// seed generated from a mnemonic phrase. In alternative, we could load the generated keypair as shown in the
// commented line of code.
if (!status.hasMasterKey()) {
if (!status.hasMasterKey() && info.hasKeyManagementCapability()) {
cmdSet.generateKey();
//cmdSet.loadKey(mnemonic.toBIP32KeyPair());
}
@ -127,6 +138,7 @@ public class MainActivity extends AppCompatActivity {
Log.i(TAG, "R: " + Hex.toHexString(signature.getR()));
Log.i(TAG, "S: " + Hex.toHexString(signature.getS()));
if (info.hasSecureChannelCapability()) {
// Cleanup, in a real application you would not unpair and instead keep the pairing key for successive interactions.
// We also remove all other pairings so that we do not fill all slots with failing runs. Again in real application
// this would be a very bad idea to do.
@ -134,7 +146,7 @@ public class MainActivity extends AppCompatActivity {
cmdSet.autoUnpair();
Log.i(TAG, "Unpaired.");
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}