]> www.average.org Git - YkNeoCR.git/commitdiff
passing data over
authorEugene Crosser <crosser@average.org>
Thu, 2 May 2013 15:59:21 +0000 (19:59 +0400)
committerEugene Crosser <crosser@average.org>
Thu, 2 May 2013 15:59:21 +0000 (19:59 +0400)
src/org/average/nfcauthcr/NFCAuthCRCheck.java
src/org/average/nfcauthcr/NFCAuthCREnroll.java

index 6637c00164221415b39f0a8c99624c23ef6341bd..dc59b69e4391ebec2552a9a96915579b3150617f 100644 (file)
@@ -2,7 +2,6 @@ package org.average.nfcauthcr;
 
 import java.io.IOException;
 
-import android.os.Bundle;
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.app.PendingIntent;
@@ -16,6 +15,8 @@ import android.nfc.tech.IsoDep;
 import android.util.Log;
 import android.widget.Toast;
 
+import org.average.nfcauthcr.NFCAuthCRYubiNeo;
+
 public class NFCAuthCRCheck extends Activity {
 
        private final String TAG = getClass().getName();
@@ -33,19 +34,16 @@ public class NFCAuthCRCheck extends Activity {
                Log.v(TAG, "Starting the work");
 
                Intent intent = getIntent();
-               Bundle extras = intent.getExtras();
                setResult(RESULT_CANCELED);
-               if(swipeDialog != null) {
+               if (swipeDialog != null) {
                        swipeDialog.dismiss();
                        swipeDialog = null;
                }
-               if(extras != null) {
-                       int slot = extras.getInt("slot");
-                       if (slot > 0) {
-                               swipeDialog = makeDialog();
-                               swipeDialog.show();
-                               enableDispatch(slot);
-                       }
+               int slot = intent.getIntExtra("slot", -1);
+               if (slot > 0) {
+                       swipeDialog = makeDialog();
+                       swipeDialog.show();
+                       enableDispatch(slot);
                }
        }
 
@@ -64,29 +62,16 @@ public class NFCAuthCRCheck extends Activity {
        public void onNewIntent(Intent intent) {
                Log.v(TAG, "NFC Intent arrived");
                int slot = intent.getIntExtra("slot", -1);
+               byte[] challenge = intent.getByteArrayExtra("challenge");
                if (slot <= 0) return;
                Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
                if (tag == null) return;
                IsoDep isoTag = IsoDep.get(tag);
-               try {
-                       isoTag.connect();
-                       byte[] resp = isoTag.transceive(selectCommand);
-                       int length = resp.length;
-                       if (resp[length - 2] == (byte)0x90 &&
-                           resp[length - 1] == 0x00) {
-                               doChallengeYubiKey(isoTag, slot);
-                       } else {
-                               Toast.makeText(this, R.string.tag_error,
-                                               Toast.LENGTH_LONG).show();
-                       }
-               } catch (TagLostException e) {
-                       Toast.makeText(this,
-                               R.string.tag_lost, Toast.LENGTH_LONG).show();
-               } catch (IOException e) {
-                       Toast.makeText(this,
-                               getText(R.string.tag_error) +
-                               e.getMessage(),
-                               Toast.LENGTH_LONG).show();
+               byte[] response = doChallengeYubiKey(isoTag, slot, challenge);
+               if (response != null) {
+                       Intent data = getIntent();
+                       data.putExtra("response", response);
+                       setResult(RESULT_OK, data);
                }
                finish();
        }
@@ -140,10 +125,28 @@ public class NFCAuthCRCheck extends Activity {
                }
        }
 
-       private void doChallengeYubiKey(IsoDep isoTag, int slot)
-                                               throws IOException {
-               Intent data = getIntent();
-               data.putExtra("response","<FIXME>real data here");
-               setResult(RESULT_OK, data);
+       private byte[] doChallengeYubiKey(IsoDep isoTag, int slot,
+                                               byte[] challenge) {
+               try {
+                       isoTag.connect();
+                       byte[] resp = isoTag.transceive(selectCommand);
+                       int length = resp.length;
+                       if (resp[length - 2] == (byte)0x90 &&
+                           resp[length - 1] == 0x00) {
+                               return challenge;
+                       } else {
+                               Toast.makeText(this, R.string.tag_error,
+                                               Toast.LENGTH_LONG).show();
+                       }
+               } catch (TagLostException e) {
+                       Toast.makeText(this,
+                               R.string.tag_lost, Toast.LENGTH_LONG).show();
+               } catch (IOException e) {
+                       Toast.makeText(this,
+                               getText(R.string.tag_error) +
+                               e.getMessage(),
+                               Toast.LENGTH_LONG).show();
+               }
+               return null;
        }
 }
index bb6b4f0357118db644ce4e7aace211823aa5be34..daea0d848c0bbe42103db9f6841ca48a3542dc57 100644 (file)
@@ -1,5 +1,7 @@
 package org.average.nfcauthcr;
 
+import java.util.Random;
+
 import android.os.Bundle;
 import android.app.Activity;
 import android.app.AlertDialog;
@@ -76,7 +78,7 @@ public class NFCAuthCREnroll extends Activity {
        public void onEnrollClicked(View view) {
                Log.v(TAG, "Enroll clicked");
                if (slot > 0) {
-                       runChallenge(slot);
+                       runEnrollment(slot);
                } else {
                        showEnrollResult("Must specify which slot to use");
                }
@@ -86,12 +88,15 @@ public class NFCAuthCREnroll extends Activity {
                                        Intent intent) {
                Log.v(TAG, "Got activity result");
                waitingForResult = false;
-               if (resultCode == RESULT_OK) {
-                       String res = intent.getStringExtra("response");
-                       Log.v(TAG, "Response is \"" + res + "\"");
-               } else {
+
+               if (resultCode != RESULT_OK) {
                        Log.v(TAG, "Error result code " + resultCode);
+                       return;
                }
+               byte[] challenge = intent.getByteArrayExtra("challenge");
+               Log.v(TAG, "Challenge is \"" + hex(challenge) + "\"");
+               byte[] response = intent.getByteArrayExtra("response");
+               Log.v(TAG, "Response is  \"" + hex(response) + "\"");
        }
 
        private void showEnrollResult(final String msg) {
@@ -111,11 +116,23 @@ public class NFCAuthCREnroll extends Activity {
                dialog.show();
        }
 
-       private void runChallenge(int slot) {
+       private void runEnrollment(int slot) {
+               Random rng = new Random();
+               byte[] challenge = new byte[63];
+               rng.nextBytes(challenge);
+               Log.v(TAG, "Random challenge: " + hex(challenge));
                Log.v(TAG, "Launching challenging activity");
                Intent crIntent = new Intent(this, NFCAuthCRCheck.class);
                crIntent.putExtra("slot", slot);
+               crIntent.putExtra("challenge", challenge);
                this.startActivityForResult(crIntent, 0);
                waitingForResult = true;
        }
+
+       private String hex(byte[] a) {
+               StringBuilder sb = new StringBuilder();
+               if (a == null) return "<null>";
+               for (byte b: a) sb.append(String.format("%02x", b&0xff));
+               return sb.toString();
+       }
 }