aboutsummaryrefslogtreecommitdiffstats
path: root/android/app/src/main/java/io/coinos/hce/HceApduService.java
blob: a46e3855b7b73332bf8f65ec96fb0c6a04ca110b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package io.coinos.hce;

import android.nfc.cardemulation.HostApduService;
import android.os.Bundle;
import android.util.Log;

import java.util.Arrays;

/**
 * Emulates an NFC Forum Type 4 Tag containing an NDEF URL record.
 * When a powered NFC reader (or another phone) taps this device,
 * it reads the NDEF message and can open the URL.
 */
public class HceApduService extends HostApduService {
    private static final String TAG = "HceApduService";

    // NDEF Tag Application AID
    private static final byte[] NDEF_AID = hexToBytes("D2760000850101");

    // File IDs
    private static final byte[] CC_FILE_ID = new byte[]{(byte) 0xE1, 0x03};
    private static final byte[] NDEF_FILE_ID = new byte[]{(byte) 0xE1, 0x04};

    // APDU command classes
    private static final byte SELECT_INS = (byte) 0xA4;
    private static final byte READ_BINARY_INS = (byte) 0xB0;

    // Status words
    private static final byte[] SW_OK = new byte[]{(byte) 0x90, 0x00};
    private static final byte[] SW_NOT_FOUND = new byte[]{(byte) 0x6A, (byte) 0x82};
    private static final byte[] SW_FUNC_NOT_SUPPORTED = new byte[]{(byte) 0x6A, (byte) 0x81};

    // Capability Container (15 bytes, read-only tag with 2048-byte NDEF file)
    private static final byte[] CC_FILE = new byte[]{
            0x00, 0x0F,       // CC length
            0x20,             // Mapping version 2.0
            0x00, (byte) 0xF6, // Max R-APDU size (246)
            0x00, 0x00,       // Max C-APDU size (0 = no write)
            0x04, 0x06,       // NDEF File Control TLV (type=4, length=6)
            (byte) 0xE1, 0x04, // NDEF file ID
            0x08, 0x00,       // Max NDEF file size (2048)
            0x00,             // Read access: open
            (byte) 0xFF      // Write access: denied
    };

    private static String currentUrl = "https://coinos.io";

    private enum SelectedFile { NONE, CC, NDEF }
    private SelectedFile selectedFile = SelectedFile.NONE;

    public static void setUrl(String url) {
        currentUrl = url;
        Log.d(TAG, "HCE URL set to: " + url);
    }

    public static String getUrl() {
        return currentUrl;
    }

    @Override
    public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
        if (apdu == null || apdu.length < 4) {
            return SW_FUNC_NOT_SUPPORTED;
        }

        byte ins = apdu[1];

        if (ins == SELECT_INS) {
            return handleSelect(apdu);
        } else if (ins == READ_BINARY_INS) {
            return handleReadBinary(apdu);
        }

        return SW_FUNC_NOT_SUPPORTED;
    }

    private byte[] handleSelect(byte[] apdu) {
        byte p1 = apdu[2];

        // SELECT by AID (P1=0x04)
        if (p1 == 0x04) {
            if (apdu.length >= 12) {
                byte[] aid = Arrays.copyOfRange(apdu, 5, 5 + (apdu[4] & 0xFF));
                if (Arrays.equals(aid, NDEF_AID)) {
                    selectedFile = SelectedFile.NONE;
                    Log.d(TAG, "NDEF application selected");
                    return SW_OK;
                }
            }
            return SW_NOT_FOUND;
        }

        // SELECT by File ID (P1=0x00)
        if (p1 == 0x00 && apdu.length >= 7) {
            byte[] fileId = Arrays.copyOfRange(apdu, 5, 7);
            if (Arrays.equals(fileId, CC_FILE_ID)) {
                selectedFile = SelectedFile.CC;
                Log.d(TAG, "CC file selected");
                return SW_OK;
            } else if (Arrays.equals(fileId, NDEF_FILE_ID)) {
                selectedFile = SelectedFile.NDEF;
                Log.d(TAG, "NDEF file selected");
                return SW_OK;
            }
            return SW_NOT_FOUND;
        }

        return SW_FUNC_NOT_SUPPORTED;
    }

    private byte[] handleReadBinary(byte[] apdu) {
        int offset = ((apdu[2] & 0xFF) << 8) | (apdu[3] & 0xFF);
        int length = apdu[4] & 0xFF;

        byte[] fileData;
        switch (selectedFile) {
            case CC:
                fileData = CC_FILE;
                break;
            case NDEF:
                fileData = buildNdefFile();
                break;
            default:
                return SW_NOT_FOUND;
        }

        if (offset >= fileData.length) {
            return SW_NOT_FOUND;
        }

        int available = fileData.length - offset;
        int readLen = Math.min(length, available);
        byte[] response = new byte[readLen + 2];
        System.arraycopy(fileData, offset, response, 0, readLen);
        response[readLen] = (byte) 0x90;
        response[readLen + 1] = 0x00;
        return response;
    }

    private byte[] buildNdefFile() {
        byte[] ndefMessage = buildNdefUrlRecord(currentUrl);
        // NDEF file = 2-byte length prefix + NDEF message
        byte[] file = new byte[2 + ndefMessage.length];
        file[0] = (byte) ((ndefMessage.length >> 8) & 0xFF);
        file[1] = (byte) (ndefMessage.length & 0xFF);
        System.arraycopy(ndefMessage, 0, file, 2, ndefMessage.length);
        return file;
    }

    private byte[] buildNdefUrlRecord(String url) {
        // Determine URI prefix code
        byte prefixCode = 0x00;
        String payload = url;

        String[][] prefixes = {
                {"https://www.", "\u0002"},
                {"http://www.", "\u0001"},
                {"https://", "\u0004"},
                {"http://", "\u0003"},
        };

        for (String[] p : prefixes) {
            if (url.startsWith(p[0])) {
                prefixCode = (byte) p[1].charAt(0);
                payload = url.substring(p[0].length());
                break;
            }
        }

        byte[] payloadBytes = payload.getBytes();
        int payloadLen = 1 + payloadBytes.length; // prefix code + URI body

        // Short record format NDEF message
        // Flags: MB=1, ME=1, SR=1, TNF=0x01 (Well-Known) = 0xD1
        byte[] record = new byte[4 + payloadLen];
        record[0] = (byte) 0xD1;           // MB|ME|SR|TNF=Well-Known
        record[1] = 0x01;                   // Type length (1 byte for "U")
        record[2] = (byte) payloadLen;       // Payload length
        record[3] = 0x55;                    // Type: "U" (URI)
        record[4] = prefixCode;              // URI prefix code
        System.arraycopy(payloadBytes, 0, record, 5, payloadBytes.length);

        return record;
    }

    @Override
    public void onDeactivated(int reason) {
        Log.d(TAG, "HCE deactivated: " + reason);
        selectedFile = SelectedFile.NONE;
    }

    private static byte[] hexToBytes(String hex) {
        int len = hex.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
                    + Character.digit(hex.charAt(i + 1), 16));
        }
        return data;
    }
}