]> www.average.org Git - YkNeoCR.git/blob - src/org/average/nfcauthcr/NFCAuthCRYubiNeo.java
67596e603993d481cc226da9c54dba4495e40d39
[YkNeoCR.git] / src / org / average / nfcauthcr / NFCAuthCRYubiNeo.java
1 package org.average.nfcauthcr;
2
3 import java.io.IOException;
4 import java.util.Arrays;
5
6 import android.nfc.NfcAdapter;
7 import android.nfc.Tag;
8 import android.nfc.TagLostException;
9 import android.nfc.tech.IsoDep;
10 import android.util.Log;
11
12 import org.average.nfcauthcr.NFCAuthCRYubiNeo;
13
14 public class NFCAuthCRYubiNeo {
15
16         private static final String TAG = "NFCAuthCRYubiNeo";
17
18         // Is it CCID APDU? ISO 7816-4?
19
20         // 00 A4 04 00 xx AID - GlobalPlatform - SELECT
21         // Le send data = 07: A0 00 00 05 27 20 01
22         // Le recv data = 00
23         private static final byte[] selectCommand =
24                 {0x00, (byte) 0xA4, 0x04, 0x00, 0x07, (byte) 0xA0,
25                  0x00, 0x00, 0x05, 0x27, 0x20, 0x01, 0x00};
26
27         private static final byte[] crCommand =
28                 {0x00, 0x01, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00};
29
30         private static final byte SLOT_CHAL_HMAC1 = 0x30;
31         private static final byte SLOT_CHAL_HMAC2 = 0x38;
32
33         public static final byte[] doChallengeYubiKey(IsoDep isoTag, int slot,
34                                                 byte[] challenge) {
35                 try {
36                         isoTag.connect();
37                         byte[] resp = isoTag.transceive(selectCommand);
38                         int length = resp.length;
39                         Log.v(TAG, "response to select length is " + length);
40                         if (resp[length - 2] != (byte)0x90 ||
41                             resp[length - 1] != 0x00) {
42                                 Log.v(TAG, "Wrong response to select");
43                                 //Toast.makeText(this, R.string.tag_error,
44                                 //              Toast.LENGTH_LONG).show();
45                                 return null;
46                         }
47                         byte[] apdu = new byte[69];
48                         apdu[0] = 0x00; // CLA
49                         apdu[1] = 0x01; // INS
50                         switch (slot) {
51                         case 1: apdu[2] = SLOT_CHAL_HMAC1; break; // P1
52                         case 2: apdu[2] = SLOT_CHAL_HMAC2; break; // P1
53                         }
54                         apdu[3] = 0x00; // P2
55                         apdu[4] = 63;   // Lc
56                         System.arraycopy(challenge, 0, apdu, 5,
57                                                         challenge.length);
58                         apdu[apdu.length-1] = 22;
59                         resp = isoTag.transceive(apdu);
60                         length = resp.length;
61                         if (resp[length - 2] != (byte)0x90 ||
62                             resp[length - 1] != 0x00) {
63                                 Log.v(TAG, "Wrong response to challenge");
64                                 //Toast.makeText(this, R.string.tag_error,
65                                 //              Toast.LENGTH_LONG).show();
66                                 return null;
67                         }
68                         Log.v(TAG, "response to challenge length is " + length);
69                         return Arrays.copyOf(resp, length-2);
70                 } catch (TagLostException e) {
71                         Log.v(TAG, e.getMessage());
72                         //Toast.makeText(this,
73                         //      R.string.tag_lost, Toast.LENGTH_LONG).show();
74                 } catch (IOException e) {
75                         Log.v(TAG, e.getMessage());
76                         //Toast.makeText(this,
77                         //      getText(R.string.tag_error) +
78                         //      e.getMessage(),
79                         //      Toast.LENGTH_LONG).show();
80                 }
81                 return null;
82         }
83 }