make errro handling via exception
authorEugene Crosser <crosser@average.org>
Fri, 3 May 2013 09:37:05 +0000 (13:37 +0400)
committerEugene Crosser <crosser@average.org>
Fri, 3 May 2013 09:37:05 +0000 (13:37 +0400)
src/org/average/nfcauthcr/Check.java
src/org/average/nfcauthcr/Enroll.java
src/org/average/nfcauthcr/YkNeo.java

index c3738f7fc4f80dbe2c799925d522beaa0ea62153..1fc5ceaf00f36d497b5cae113390f2361d4d7bf6 100644 (file)
@@ -14,6 +14,7 @@ import android.util.Log;
 import android.widget.Toast;
 
 import org.average.nfcauthcr.YkNeo;
 import android.widget.Toast;
 
 import org.average.nfcauthcr.YkNeo;
+import org.average.nfcauthcr.CRException;
 
 public class Check extends Activity {
 
 
 public class Check extends Activity {
 
@@ -61,12 +62,16 @@ public class Check extends Activity {
                Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
                if (tag == null) return;
                IsoDep isoTag = IsoDep.get(tag);
                Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
                if (tag == null) return;
                IsoDep isoTag = IsoDep.get(tag);
-               byte[] response = YkNeo.doChallengeYubiKey(
+               try {
+                       byte[] response = YkNeo.doChallengeYubiKey(
                                                isoTag, slot, challenge);
                                                isoTag, slot, challenge);
-               if (response != null) {
                        Intent data = getIntent();
                        data.putExtra("response", response);
                        setResult(RESULT_OK, data);
                        Intent data = getIntent();
                        data.putExtra("response", response);
                        setResult(RESULT_OK, data);
+               } catch (CRException e) {
+                       Log.v(TAG, e.getMessage());
+                       Toast.makeText(this, e.getMessage(),
+                                       Toast.LENGTH_LONG).show();
                }
                finish();
        }
                }
                finish();
        }
index 9f9d53593da811965c43d9c4ef4167826f35da07..5b125fa490a3d4cb01527eb2a48a576530ba3171 100644 (file)
@@ -127,7 +127,7 @@ public class Enroll extends Activity {
                rng.nextBytes(challenge);
                Log.v(TAG, "Random challenge: " + hex(challenge));
                Log.v(TAG, "Launching challenging activity");
                rng.nextBytes(challenge);
                Log.v(TAG, "Random challenge: " + hex(challenge));
                Log.v(TAG, "Launching challenging activity");
-               Intent crIntent = new Intent(this, NFCAuthCRCheck.class);
+               Intent crIntent = new Intent(this, Check.class);
                crIntent.putExtra("slot", slot);
                crIntent.putExtra("challenge", challenge);
                this.startActivityForResult(crIntent, 0);
                crIntent.putExtra("slot", slot);
                crIntent.putExtra("challenge", challenge);
                this.startActivityForResult(crIntent, 0);
index a291eabe1a4f4131a2764bb880916bba31e05b9f..1537459ba7e7c6ef4513370a844fc8b234194e07 100644 (file)
@@ -7,7 +7,8 @@ import android.nfc.NfcAdapter;
 import android.nfc.Tag;
 import android.nfc.TagLostException;
 import android.nfc.tech.IsoDep;
 import android.nfc.Tag;
 import android.nfc.TagLostException;
 import android.nfc.tech.IsoDep;
-//import android.util.Log;
+
+import org.average.nfcauthcr.CRException;
 
 public class YkNeo {
 
 
 public class YkNeo {
 
@@ -23,18 +24,16 @@ public class YkNeo {
        private static final byte SLOT_CHAL_HMAC2 = 0x38;
 
        public static final byte[] doChallengeYubiKey(IsoDep isoTag, int slot,
        private static final byte SLOT_CHAL_HMAC2 = 0x38;
 
        public static final byte[] doChallengeYubiKey(IsoDep isoTag, int slot,
-                                               byte[] challenge) {
+                                               byte[] challenge)
+                               throws CRException {
                try {
                        isoTag.connect();
                        byte[] resp = isoTag.transceive(selectApdu);
                        int length = resp.length;
                try {
                        isoTag.connect();
                        byte[] resp = isoTag.transceive(selectApdu);
                        int length = resp.length;
-                       //Log.v(TAG, "response to select length is " + length);
                        if (resp[length - 2] != (byte)0x90 ||
                            resp[length - 1] != 0x00) {
                        if (resp[length - 2] != (byte)0x90 ||
                            resp[length - 1] != 0x00) {
-                               //Log.v(TAG, "Wrong response to select");
-                               //Toast.makeText(this, R.string.tag_error,
-                               //              Toast.LENGTH_LONG).show();
-                               return null;
+                               throw new CRException(
+                               "NFC Error code in the response to select");
                        }
                        byte[] crApdu = new byte[69];
                        crApdu[0] = 0x00; // CLA
                        }
                        byte[] crApdu = new byte[69];
                        crApdu[0] = 0x00; // CLA
@@ -52,24 +51,14 @@ public class YkNeo {
                        length = resp.length;
                        if (resp[length - 2] != (byte)0x90 ||
                            resp[length - 1] != 0x00) {
                        length = resp.length;
                        if (resp[length - 2] != (byte)0x90 ||
                            resp[length - 1] != 0x00) {
-                               //Log.v(TAG, "Wrong response to challenge");
-                               //Toast.makeText(this, R.string.tag_error,
-                               //              Toast.LENGTH_LONG).show();
-                               return null;
+                               throw new CRException(
+                               "NFC Error code in the response to CR");
                        }
                        }
-                       //Log.v(TAG, "response to challenge length is " + length);
                        return Arrays.copyOf(resp, length-2);
                } catch (TagLostException e) {
                        return Arrays.copyOf(resp, length-2);
                } catch (TagLostException e) {
-                       //Log.v(TAG, e.getMessage());
-                       //Toast.makeText(this,
-                       //      R.string.tag_lost, Toast.LENGTH_LONG).show();
+                       throw new CRException("NFC connection lost", e);
                } catch (IOException e) {
                } catch (IOException e) {
-                       //Log.v(TAG, e.getMessage());
-                       //Toast.makeText(this,
-                       //      getText(R.string.tag_error) +
-                       //      e.getMessage(),
-                       //      Toast.LENGTH_LONG).show();
+                       throw new CRException("NFC I/O error", e);
                }
                }
-               return null;
        }
 }
        }
 }