1 package org.average.nfcauthcr;
3 import java.io.IOException;
5 import android.app.Activity;
6 import android.app.AlertDialog;
7 import android.app.PendingIntent;
8 import android.content.DialogInterface;
9 import android.content.Intent;
10 import android.content.IntentFilter;
11 import android.nfc.NfcAdapter;
12 import android.nfc.Tag;
13 import android.nfc.TagLostException;
14 import android.nfc.tech.IsoDep;
15 import android.util.Log;
16 import android.widget.Toast;
18 import org.average.nfcauthcr.NFCAuthCRYubiNeo;
20 public class NFCAuthCRCheck extends Activity {
22 private final String TAG = getClass().getName();
24 private static final byte[] selectCommand =
25 {0x00, (byte) 0xA4, 0x04, 0x00, 0x07, (byte) 0xA0,
26 0x00, 0x00, 0x05, 0x27, 0x20, 0x01, 0x00};
28 private AlertDialog swipeDialog;
29 private PendingIntent tagIntent;
32 protected void onResume() {
34 Log.v(TAG, "Starting the work");
36 Intent intent = getIntent();
37 setResult(RESULT_CANCELED);
38 if (swipeDialog != null) {
39 swipeDialog.dismiss();
42 int slot = intent.getIntExtra("slot", -1);
44 swipeDialog = makeDialog();
51 protected void onPause() {
53 Log.v(TAG, "Finished the work");
55 if(swipeDialog != null) {
56 swipeDialog.dismiss();
62 public void onNewIntent(Intent intent) {
63 Log.v(TAG, "NFC Intent arrived");
64 int slot = intent.getIntExtra("slot", -1);
65 byte[] challenge = intent.getByteArrayExtra("challenge");
66 if (slot <= 0) return;
67 Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
68 if (tag == null) return;
69 IsoDep isoTag = IsoDep.get(tag);
70 byte[] response = doChallengeYubiKey(isoTag, slot, challenge);
71 if (response != null) {
72 Intent data = getIntent();
73 data.putExtra("response", response);
74 setResult(RESULT_OK, data);
79 private AlertDialog makeDialog() {
80 AlertDialog.Builder builder = new AlertDialog.Builder(this);
81 builder.setTitle(R.string.challenging);
82 builder.setMessage(R.string.swipe);
83 builder.setOnCancelListener(
84 new DialogInterface.OnCancelListener() {
85 public void onCancel(DialogInterface dialog) {
89 return builder.create();
92 private void enableDispatch(int slot) {
93 Intent intent = getIntent();
94 intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
95 intent.putExtra("slot", slot);
96 tagIntent = PendingIntent.getActivity(this, 0, intent, 0);
98 new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
99 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
100 if (adapter == null) {
101 Toast.makeText(this, R.string.no_nfc,
102 Toast.LENGTH_LONG).show();
106 if (! adapter.isEnabled()) {
107 Toast.makeText(this, R.string.nfc_disabled,
108 Toast.LENGTH_LONG).show();
112 adapter.enableForegroundDispatch(
113 this, tagIntent, new IntentFilter[] {iso},
114 new String[][] {new String[] {IsoDep.class.getName()}});
117 private void disableDispatch() {
118 if (tagIntent != null) {
122 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
123 if (adapter != null) {
124 adapter.disableForegroundDispatch(this);
128 private byte[] doChallengeYubiKey(IsoDep isoTag, int slot,
132 byte[] resp = isoTag.transceive(selectCommand);
133 int length = resp.length;
134 if (resp[length - 2] == (byte)0x90 &&
135 resp[length - 1] == 0x00) {
138 Toast.makeText(this, R.string.tag_error,
139 Toast.LENGTH_LONG).show();
141 } catch (TagLostException e) {
143 R.string.tag_lost, Toast.LENGTH_LONG).show();
144 } catch (IOException e) {
146 getText(R.string.tag_error) +
148 Toast.LENGTH_LONG).show();