]> www.average.org Git - YkNeoCR.git/blob - src/org/average/nfcauthcr/NFCAuthCRCheck.java
passing data over
[YkNeoCR.git] / src / org / average / nfcauthcr / NFCAuthCRCheck.java
1 package org.average.nfcauthcr;
2
3 import java.io.IOException;
4
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;
17
18 import org.average.nfcauthcr.NFCAuthCRYubiNeo;
19
20 public class NFCAuthCRCheck extends Activity {
21
22         private final String TAG = getClass().getName();
23
24         private static final byte[] selectCommand =
25                 {0x00, (byte) 0xA4, 0x04, 0x00, 0x07, (byte) 0xA0,
26                  0x00, 0x00, 0x05, 0x27, 0x20, 0x01, 0x00};
27
28         private AlertDialog swipeDialog;
29         private PendingIntent tagIntent;
30
31         @Override
32         protected void onResume() {
33                 super.onResume();
34                 Log.v(TAG, "Starting the work");
35
36                 Intent intent = getIntent();
37                 setResult(RESULT_CANCELED);
38                 if (swipeDialog != null) {
39                         swipeDialog.dismiss();
40                         swipeDialog = null;
41                 }
42                 int slot = intent.getIntExtra("slot", -1);
43                 if (slot > 0) {
44                         swipeDialog = makeDialog();
45                         swipeDialog.show();
46                         enableDispatch(slot);
47                 }
48         }
49
50         @Override
51         protected void onPause() {
52                 super.onPause();
53                 Log.v(TAG, "Finished the work");
54
55                 if(swipeDialog != null) {
56                         swipeDialog.dismiss();
57                         swipeDialog = null;
58                 }
59                 disableDispatch();
60         }
61
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);
75                 }
76                 finish();
77         }
78
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) {
86                                 finish();
87                         }
88                 });
89                 return builder.create();
90         }
91
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);
97                 IntentFilter iso =
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();
103                         finish();
104                         return;
105                 }
106                 if (! adapter.isEnabled()) {
107                         Toast.makeText(this, R.string.nfc_disabled,
108                                                 Toast.LENGTH_LONG).show();
109                         finish();
110                         return;
111                 }
112                 adapter.enableForegroundDispatch(
113                         this, tagIntent, new IntentFilter[] {iso},
114                         new String[][] {new String[] {IsoDep.class.getName()}});
115         }
116
117         private void disableDispatch() {
118                 if (tagIntent != null) {
119                         tagIntent.cancel();
120                         tagIntent = null;
121                 }
122                 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
123                 if (adapter != null) {
124                         adapter.disableForegroundDispatch(this);
125                 }
126         }
127
128         private byte[] doChallengeYubiKey(IsoDep isoTag, int slot,
129                                                 byte[] challenge) {
130                 try {
131                         isoTag.connect();
132                         byte[] resp = isoTag.transceive(selectCommand);
133                         int length = resp.length;
134                         if (resp[length - 2] == (byte)0x90 &&
135                             resp[length - 1] == 0x00) {
136                                 return challenge;
137                         } else {
138                                 Toast.makeText(this, R.string.tag_error,
139                                                 Toast.LENGTH_LONG).show();
140                         }
141                 } catch (TagLostException e) {
142                         Toast.makeText(this,
143                                 R.string.tag_lost, Toast.LENGTH_LONG).show();
144                 } catch (IOException e) {
145                         Toast.makeText(this,
146                                 getText(R.string.tag_error) +
147                                 e.getMessage(),
148                                 Toast.LENGTH_LONG).show();
149                 }
150                 return null;
151         }
152 }