]> www.average.org Git - YkNeoCR.git/blob - src/org/average/nfcauthcr/Enroll.java
6c104c4ad9513ad22574924fccd89f824a17d033
[YkNeoCR.git] / src / org / average / nfcauthcr / Enroll.java
1 package org.average.nfcauthcr;
2
3 import java.util.Random;
4
5 import android.os.Bundle;
6 import android.app.Activity;
7 import android.app.AlertDialog;
8 import android.preference.PreferenceManager;
9 import android.content.Intent;
10 import android.content.SharedPreferences;
11 import android.content.SharedPreferences.Editor;
12 import android.content.DialogInterface;
13 import android.util.Log;
14 import android.view.View;
15 import android.widget.TextView;
16 import android.widget.RadioButton;
17
18 import org.average.nfcauthcr.TagEvent;
19
20 public class Enroll extends Activity {
21
22         private final String TAG = getClass().getName();
23
24         private static boolean waitingForResult = false;
25         private static SharedPreferences prefs;
26         private static int slot;
27
28         @Override
29         protected void onCreate(Bundle savedInstanceState)
30         {
31                 super.onCreate(savedInstanceState);
32                 Log.v(TAG, "Starting");
33                 prefs = PreferenceManager.getDefaultSharedPreferences(this);
34         }
35
36         @Override
37         protected void onResume() {
38                 super.onResume();
39                 setContentView(R.layout.main);
40                 slot = prefs.getInt("slot_number", -1);
41                 Log.v(TAG, "found saved slot value " + slot);
42                 RadioButton btn = null;
43                 switch (slot) {
44                 case 1: btn = (RadioButton)findViewById(R.id.slot_1);
45                         break;
46                 case 2: btn = (RadioButton)findViewById(R.id.slot_2);
47                         break;
48                 }
49                 if (btn != null) btn.setChecked(true);
50         }
51
52         @Override
53         protected void onPause() {
54                 super.onPause();
55         }
56
57         @Override
58         protected void onStop() {
59                 super.onStop();
60                 Log.v(TAG, "Stop requested");
61         }
62
63         public void onSlotSelectionClicked(View view) {
64                 Log.v(TAG, "Radio Button selected");
65                 if (! ((RadioButton) view).isChecked()) return;
66                 switch(view.getId()) {
67                 case R.id.slot_1: slot=1; break;
68                 case R.id.slot_2: slot=2; break;
69                 }
70                 Editor editor = prefs.edit();
71                 editor.putInt("slot_number", slot);
72                 editor.commit();
73                 Log.v(TAG, "stored slot number " + slot);
74         }
75
76         public void onEnrollClicked(View view) {
77                 Log.v(TAG, "Enroll clicked");
78                 if (slot > 0) {
79                         runEnrollment(slot);
80                 } else {
81                         showEnrollResult(R.string.need_slot);
82                 }
83         }
84
85         public void onActivityResult(int requestCode, int resultCode,
86                                         Intent intent) {
87                 Log.v(TAG, "Got activity result");
88                 waitingForResult = false;
89
90                 if (resultCode != RESULT_OK) {
91                         Log.v(TAG, "Error result code " + resultCode);
92                         return;
93                 }
94                 byte[] challenge = intent.getByteArrayExtra("challenge");
95                 Log.v(TAG, "Challenge is \"" + hex(challenge) + "\"");
96                 byte[] response = intent.getByteArrayExtra("response");
97                 Log.v(TAG, "Response is  \"" + hex(response) + "\"");
98                 Editor editor = prefs.edit();
99                 editor.putString("challenge", hex(challenge));
100                 editor.putString("response", hex(response));
101                 editor.commit();
102                 showEnrollResult(R.string.enroll_success);
103                 Log.v(TAG, "stored new challenge and response");
104         }
105
106         private void showEnrollResult(int messageid) {
107                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
108                 builder.setTitle(R.string.enrollresult);
109                 builder.setMessage(messageid);
110                 builder.setPositiveButton(android.R.string.ok,
111                                 new DialogInterface.OnClickListener() {
112                         public void onClick(DialogInterface dialog, int which) {
113                                 dialog.dismiss();
114                                 if (!waitingForResult) { finish(); }
115                         }
116                 });
117                 AlertDialog dialog = builder.create();
118                 dialog.show();
119         }
120
121         private void runEnrollment(int slot) {
122                 Random rng = new Random();
123                 byte[] challenge = new byte[63];
124                 rng.nextBytes(challenge);
125                 Log.v(TAG, "Random challenge: " + hex(challenge));
126                 Intent crIntent = new Intent(this, TagEvent.class);
127                 crIntent.putExtra("yubikey_neo_slot", slot);
128                 crIntent.putExtra("challenge", challenge);
129                 waitingForResult = true;
130                 this.startActivityForResult(crIntent, 0);
131         }
132
133         private String hex(byte[] a) {
134                 StringBuilder sb = new StringBuilder();
135                 if (a == null) return "<null>";
136                 for (byte b: a) sb.append(String.format("%02x", b&0xff));
137                 return sb.toString();
138         }
139 }