iso7816 1.0.20 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +159 -38
- package/dist/command-apdu.d.ts +66 -0
- package/dist/command-apdu.d.ts.map +1 -0
- package/dist/command-apdu.js +96 -0
- package/dist/command-apdu.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/iso7816-application.d.ts +91 -0
- package/dist/iso7816-application.d.ts.map +1 -0
- package/dist/iso7816-application.js +146 -0
- package/dist/iso7816-application.js.map +1 -0
- package/dist/response-apdu.d.ts +66 -0
- package/dist/response-apdu.d.ts.map +1 -0
- package/dist/response-apdu.js +146 -0
- package/dist/response-apdu.js.map +1 -0
- package/package.json +41 -15
- package/.babelrc +0 -3
- package/.npmignore +0 -1
- package/demo/iso7816-demo.js +0 -45
- package/docs/scr3500-collapsed.JPG +0 -0
- package/docs/scr3500-expanded.JPG +0 -0
- package/lib/command-apdu.js +0 -86
- package/lib/iso7816-application.js +0 -96
- package/lib/response-apdu.js +0 -93
package/README.md
CHANGED
|
@@ -1,63 +1,184 @@
|
|
|
1
|
-
#
|
|
1
|
+
# ISO 7816
|
|
2
2
|
|
|
3
|
+
A library for building and parsing ISO 7816 APDU commands and responses.
|
|
3
4
|
|
|
4
|
-
|
|
5
|
+
This library is card reader agnostic - it works with any transport that can send/receive byte arrays. The examples below use the [smartcard](https://www.npmjs.com/package/smartcard) package, but you can use any PC/SC library or even a custom transport.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install iso7816
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Requirements
|
|
14
|
+
|
|
15
|
+
- Node.js 18.0.0 or higher
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
### With smartcard package
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install iso7816 smartcard
|
|
23
|
+
```
|
|
5
24
|
|
|
6
25
|
```javascript
|
|
7
|
-
|
|
8
|
-
|
|
26
|
+
import { Devices } from 'smartcard';
|
|
27
|
+
import iso7816 from 'iso7816';
|
|
9
28
|
|
|
10
|
-
|
|
11
|
-
console.info(`Device '${reader.name}' activated`);
|
|
12
|
-
});
|
|
29
|
+
const devices = new Devices();
|
|
13
30
|
|
|
14
|
-
|
|
15
|
-
console.
|
|
16
|
-
});
|
|
31
|
+
devices.on('card-inserted', async ({ reader, card }) => {
|
|
32
|
+
console.log(`Card inserted into '${reader.name}'`);
|
|
17
33
|
|
|
18
|
-
|
|
19
|
-
console.info(`Card removed from '${reader.name}' `);
|
|
20
|
-
});
|
|
34
|
+
const application = iso7816(card);
|
|
21
35
|
|
|
22
|
-
|
|
23
|
-
|
|
36
|
+
try {
|
|
37
|
+
// Select PSE (Payment System Environment)
|
|
38
|
+
const response = await application.selectFile([
|
|
39
|
+
0x31, 0x50, 0x41, 0x59, 0x2e, 0x53, 0x59, 0x53, 0x2e, 0x44, 0x44, 0x46, 0x30, 0x31,
|
|
40
|
+
]);
|
|
41
|
+
|
|
42
|
+
console.log(`Response: ${response.getStatus().meaning}`);
|
|
43
|
+
|
|
44
|
+
if (response.isOk()) {
|
|
45
|
+
// Read a record
|
|
46
|
+
const record = await application.readRecord(1, 1);
|
|
47
|
+
console.log(`Record: ${record.toString()}`);
|
|
48
|
+
}
|
|
49
|
+
} catch (error) {
|
|
50
|
+
console.error('Error:', error.message);
|
|
51
|
+
}
|
|
24
52
|
});
|
|
25
53
|
|
|
26
|
-
|
|
27
|
-
console.
|
|
54
|
+
devices.on('error', (error) => {
|
|
55
|
+
console.error(`Error: ${error.message}`);
|
|
28
56
|
});
|
|
29
57
|
|
|
58
|
+
devices.start();
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### With custom transport
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
import iso7816, { createCommandApdu, createResponseApdu } from 'iso7816';
|
|
65
|
+
|
|
66
|
+
// Any object with a transmit method works
|
|
67
|
+
const card = {
|
|
68
|
+
atr: Buffer.from([0x3b, 0x00]),
|
|
69
|
+
async transmit(command: Buffer): Promise<Buffer> {
|
|
70
|
+
// Send command to your card reader and return response
|
|
71
|
+
return yourCardReader.send(command);
|
|
72
|
+
},
|
|
73
|
+
};
|
|
30
74
|
|
|
31
|
-
|
|
75
|
+
const application = iso7816(card);
|
|
76
|
+
const response = await application.selectFile([0xa0, 0x00, 0x00]);
|
|
77
|
+
```
|
|
32
78
|
|
|
33
|
-
|
|
79
|
+
### Building APDUs directly
|
|
34
80
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
.selectFile([0x31, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31])
|
|
38
|
-
.then(function (response) {
|
|
39
|
-
console.info(`Select PSE Response: '${response}'`);
|
|
40
|
-
}).catch(function (error) {
|
|
41
|
-
console.error('Error:', error, error.stack);
|
|
42
|
-
});
|
|
81
|
+
```typescript
|
|
82
|
+
import { createCommandApdu, createResponseApdu } from 'iso7816';
|
|
43
83
|
|
|
84
|
+
// Build a SELECT command
|
|
85
|
+
const command = createCommandApdu({
|
|
86
|
+
cla: 0x00,
|
|
87
|
+
ins: 0xa4,
|
|
88
|
+
p1: 0x04,
|
|
89
|
+
p2: 0x00,
|
|
90
|
+
data: [0xa0, 0x00, 0x00],
|
|
44
91
|
});
|
|
45
92
|
|
|
93
|
+
console.log(command.toString()); // "00a4040003a0000000"
|
|
94
|
+
console.log(command.toBuffer()); // <Buffer 00 a4 04 00 03 a0 00 00 00>
|
|
46
95
|
|
|
96
|
+
// Parse a response
|
|
97
|
+
const response = createResponseApdu(Buffer.from([0x90, 0x00]));
|
|
98
|
+
console.log(response.isOk()); // true
|
|
99
|
+
console.log(response.getStatus()); // { code: '9000', meaning: 'Normal processing' }
|
|
47
100
|
```
|
|
48
101
|
|
|
102
|
+
## API
|
|
103
|
+
|
|
104
|
+
### `iso7816(card)`
|
|
105
|
+
|
|
106
|
+
Create an ISO 7816 application instance.
|
|
107
|
+
|
|
108
|
+
- `card` - Any object with `transmit(buffer: Buffer): Promise<Buffer>` method
|
|
109
|
+
|
|
110
|
+
Returns an `Iso7816` instance.
|
|
111
|
+
|
|
112
|
+
### `application.selectFile(bytes, p1?, p2?)`
|
|
49
113
|
|
|
50
|
-
|
|
114
|
+
Select a file on the card.
|
|
51
115
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
116
|
+
- `bytes` - Array of bytes representing the file identifier (e.g., AID)
|
|
117
|
+
- `p1` - Optional P1 parameter (default: `0x04`)
|
|
118
|
+
- `p2` - Optional P2 parameter (default: `0x00`)
|
|
119
|
+
|
|
120
|
+
### `application.readRecord(sfi, record)`
|
|
121
|
+
|
|
122
|
+
Read a record from a file.
|
|
123
|
+
|
|
124
|
+
- `sfi` - Short File Identifier
|
|
125
|
+
- `record` - Record number
|
|
126
|
+
|
|
127
|
+
### `application.getData(p1, p2)`
|
|
128
|
+
|
|
129
|
+
Get data from the card.
|
|
130
|
+
|
|
131
|
+
### `application.getResponse(length)`
|
|
132
|
+
|
|
133
|
+
Get additional response bytes.
|
|
134
|
+
|
|
135
|
+
### `application.issueCommand(commandApdu, maxRetries?)`
|
|
136
|
+
|
|
137
|
+
Issue a raw APDU command. When the card answers `61xx` (more data available), this sends GET RESPONSE until the card has sent everything, and returns all the data with the final status word.
|
|
138
|
+
|
|
139
|
+
- `commandApdu` - A `CommandApdu` instance
|
|
140
|
+
- `maxRetries` - Maximum retries for wrong length responses (default: `3`)
|
|
141
|
+
|
|
142
|
+
### `createCommandApdu(options)`
|
|
143
|
+
|
|
144
|
+
Create a command APDU.
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
const apdu = createCommandApdu({
|
|
148
|
+
cla: 0x00, // Class byte
|
|
149
|
+
ins: 0xa4, // Instruction byte
|
|
150
|
+
p1: 0x04, // Parameter 1
|
|
151
|
+
p2: 0x00, // Parameter 2
|
|
152
|
+
data: [...], // Optional command data, up to 65535 bytes
|
|
153
|
+
le: 0x00, // Optional expected response length, 0-65536
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
apdu.toString(); // Hex string
|
|
157
|
+
apdu.toBuffer(); // Node.js Buffer
|
|
158
|
+
apdu.toByteArray(); // number[]
|
|
159
|
+
apdu.setLe(0x10); // Update expected length
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
The command always includes Le. `le: 0`, the default, asks for the maximum: 256 bytes, or 65536 in the extended form. The command uses the short form when the data is at most 255 bytes and `le` is at most 256, and the ISO 7816-4 extended length form otherwise.
|
|
163
|
+
|
|
164
|
+
A value that can't be encoded, such as a `p1` of 256 or a negative `le`, throws a `RangeError` instead of being silently truncated.
|
|
165
|
+
|
|
166
|
+
### `createResponseApdu(buffer)`
|
|
167
|
+
|
|
168
|
+
Parse a response APDU.
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
const response = createResponseApdu(buffer);
|
|
172
|
+
|
|
173
|
+
response.isOk(); // true if status is 9000
|
|
174
|
+
response.getStatusCode(); // "9000"
|
|
175
|
+
response.getStatus(); // { code: "9000", meaning: "Normal processing" }
|
|
176
|
+
response.getBuffer(); // Raw buffer
|
|
177
|
+
response.toString(); // Hex string
|
|
178
|
+
response.hasMoreBytesAvailable(); // true if 61xx status
|
|
179
|
+
response.isWrongLength(); // true if 6cxx status
|
|
180
|
+
```
|
|
56
181
|
|
|
57
|
-
|
|
58
|
-
<img src="docs/scr3500-collapsed.JPG" width=600 style="margin:1rem;" />
|
|
59
|
-
</div>
|
|
182
|
+
## License
|
|
60
183
|
|
|
61
|
-
|
|
62
|
-
<img src="docs/scr3500-expanded.JPG" width=600 style="margin:1rem;" />
|
|
63
|
-
</div>
|
|
184
|
+
MIT
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for creating a CommandApdu
|
|
3
|
+
*/
|
|
4
|
+
export interface CommandApduOptions {
|
|
5
|
+
/** Class byte (0-255) */
|
|
6
|
+
cla: number;
|
|
7
|
+
/** Instruction byte (0-255) */
|
|
8
|
+
ins: number;
|
|
9
|
+
/** Parameter 1 (0-255) */
|
|
10
|
+
p1: number;
|
|
11
|
+
/** Parameter 2 (0-255) */
|
|
12
|
+
p2: number;
|
|
13
|
+
/** Command data, up to 65535 bytes. More than 255 bytes uses the extended length form. */
|
|
14
|
+
data?: number[];
|
|
15
|
+
/**
|
|
16
|
+
* Expected response length (Ne), from 0 to 65536. The default, 0, asks for the maximum: 256,
|
|
17
|
+
* or 65536 in the extended form. More than 256 uses the extended length form.
|
|
18
|
+
*/
|
|
19
|
+
le?: number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* APDU Command class for building ISO 7816 command APDUs.
|
|
23
|
+
*
|
|
24
|
+
* The command always includes Le. It uses the short form when the data is at most 255 bytes and
|
|
25
|
+
* Le is at most 256, and the extended form otherwise (ISO/IEC 7816-4):
|
|
26
|
+
*
|
|
27
|
+
* short CLA INS P1 P2 [Lc data] Le
|
|
28
|
+
* extended CLA INS P1 P2 00 Le1 Le2 (no data)
|
|
29
|
+
* CLA INS P1 P2 00 Lc1 Lc2 data Le1 Le2 (with data)
|
|
30
|
+
*
|
|
31
|
+
* A value that can't be encoded throws a RangeError instead of being truncated. The full
|
|
32
|
+
* specification is in command-apdu.properties.test.ts.
|
|
33
|
+
*/
|
|
34
|
+
export declare class CommandApdu {
|
|
35
|
+
private readonly _header;
|
|
36
|
+
private readonly _data;
|
|
37
|
+
private _bytes;
|
|
38
|
+
constructor(options: CommandApduOptions);
|
|
39
|
+
/**
|
|
40
|
+
* Get the bytes array (read-only copy)
|
|
41
|
+
*/
|
|
42
|
+
get bytes(): number[];
|
|
43
|
+
/**
|
|
44
|
+
* Convert to hex string representation
|
|
45
|
+
*/
|
|
46
|
+
toString(): string;
|
|
47
|
+
/**
|
|
48
|
+
* Get bytes as array
|
|
49
|
+
*/
|
|
50
|
+
toByteArray(): number[];
|
|
51
|
+
/**
|
|
52
|
+
* Convert to Node.js Buffer
|
|
53
|
+
*/
|
|
54
|
+
toBuffer(): Buffer;
|
|
55
|
+
/**
|
|
56
|
+
* Update expected response length (Le). Re-encodes the command, so a Le over 256 switches
|
|
57
|
+
* it to the extended form. Throws a RangeError, leaving the command unchanged, if `le` is
|
|
58
|
+
* not an integer from 0 to 65536.
|
|
59
|
+
*/
|
|
60
|
+
setLe(le: number): void;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Factory function to create a CommandApdu
|
|
64
|
+
*/
|
|
65
|
+
export default function createCommandApdu(options: CommandApduOptions): CommandApdu;
|
|
66
|
+
//# sourceMappingURL=command-apdu.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"command-apdu.d.ts","sourceRoot":"","sources":["../src/command-apdu.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,kBAAkB;IAC/B,yBAAyB;IACzB,GAAG,EAAE,MAAM,CAAC;IACZ,+BAA+B;IAC/B,GAAG,EAAE,MAAM,CAAC;IACZ,0BAA0B;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,0BAA0B;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,0FAA0F;IAC1F,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB;;;OAGG;IACH,EAAE,CAAC,EAAE,MAAM,CAAC;CACf;AAKD;;;;;;;;;;;;GAYG;AACH,qBAAa,WAAW;IACpB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAW;IACnC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAW;IACjC,OAAO,CAAC,MAAM,CAAW;gBAEb,OAAO,EAAE,kBAAkB;IAmBvC;;OAEG;IACH,IAAI,KAAK,IAAI,MAAM,EAAE,CAEpB;IAED;;OAEG;IACH,QAAQ,IAAI,MAAM;IAIlB;;OAEG;IACH,WAAW,IAAI,MAAM,EAAE;IAIvB;;OAEG;IACH,QAAQ,IAAI,MAAM;IAIlB;;;;OAIG;IACH,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI;CAG1B;AA6BD;;GAEG;AACH,MAAM,CAAC,OAAO,UAAU,iBAAiB,CAAC,OAAO,EAAE,kBAAkB,GAAG,WAAW,CAElF"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
const MAX_DATA_LENGTH = 65535;
|
|
2
|
+
const MAX_LE = 65536;
|
|
3
|
+
/**
|
|
4
|
+
* APDU Command class for building ISO 7816 command APDUs.
|
|
5
|
+
*
|
|
6
|
+
* The command always includes Le. It uses the short form when the data is at most 255 bytes and
|
|
7
|
+
* Le is at most 256, and the extended form otherwise (ISO/IEC 7816-4):
|
|
8
|
+
*
|
|
9
|
+
* short CLA INS P1 P2 [Lc data] Le
|
|
10
|
+
* extended CLA INS P1 P2 00 Le1 Le2 (no data)
|
|
11
|
+
* CLA INS P1 P2 00 Lc1 Lc2 data Le1 Le2 (with data)
|
|
12
|
+
*
|
|
13
|
+
* A value that can't be encoded throws a RangeError instead of being truncated. The full
|
|
14
|
+
* specification is in command-apdu.properties.test.ts.
|
|
15
|
+
*/
|
|
16
|
+
export class CommandApdu {
|
|
17
|
+
_header;
|
|
18
|
+
_data;
|
|
19
|
+
_bytes;
|
|
20
|
+
constructor(options) {
|
|
21
|
+
const { cla, ins, p1, p2, le = 0 } = options;
|
|
22
|
+
const data = options.data ?? [];
|
|
23
|
+
this._header = [
|
|
24
|
+
checkByte('cla', cla),
|
|
25
|
+
checkByte('ins', ins),
|
|
26
|
+
checkByte('p1', p1),
|
|
27
|
+
checkByte('p2', p2),
|
|
28
|
+
];
|
|
29
|
+
this._data = Array.from(data, (b, i) => checkByte(`data[${i}]`, b));
|
|
30
|
+
if (this._data.length > MAX_DATA_LENGTH) {
|
|
31
|
+
throw new RangeError(`data is ${this._data.length} bytes; the maximum is ${MAX_DATA_LENGTH}`);
|
|
32
|
+
}
|
|
33
|
+
this._bytes = encode(this._header, this._data, checkLe(le));
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Get the bytes array (read-only copy)
|
|
37
|
+
*/
|
|
38
|
+
get bytes() {
|
|
39
|
+
return [...this._bytes];
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Convert to hex string representation
|
|
43
|
+
*/
|
|
44
|
+
toString() {
|
|
45
|
+
return Buffer.from(this._bytes).toString('hex');
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Get bytes as array
|
|
49
|
+
*/
|
|
50
|
+
toByteArray() {
|
|
51
|
+
return this._bytes;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Convert to Node.js Buffer
|
|
55
|
+
*/
|
|
56
|
+
toBuffer() {
|
|
57
|
+
return Buffer.from(this._bytes);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Update expected response length (Le). Re-encodes the command, so a Le over 256 switches
|
|
61
|
+
* it to the extended form. Throws a RangeError, leaving the command unchanged, if `le` is
|
|
62
|
+
* not an integer from 0 to 65536.
|
|
63
|
+
*/
|
|
64
|
+
setLe(le) {
|
|
65
|
+
this._bytes = encode(this._header, this._data, checkLe(le));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function checkByte(name, value) {
|
|
69
|
+
if (!Number.isInteger(value) || value < 0 || value > 0xff) {
|
|
70
|
+
throw new RangeError(`${name} must be a byte (0-255), got ${value}`);
|
|
71
|
+
}
|
|
72
|
+
return value;
|
|
73
|
+
}
|
|
74
|
+
function checkLe(le) {
|
|
75
|
+
if (!Number.isInteger(le) || le < 0 || le > MAX_LE) {
|
|
76
|
+
throw new RangeError(`le must be an integer from 0 to ${MAX_LE}, got ${le}`);
|
|
77
|
+
}
|
|
78
|
+
return le;
|
|
79
|
+
}
|
|
80
|
+
/** Encodes a validated command. An Le of 256 (short) or 65536 (extended) encodes as zeros. */
|
|
81
|
+
function encode(header, data, le) {
|
|
82
|
+
const nc = data.length;
|
|
83
|
+
if (nc <= 255 && le <= 256) {
|
|
84
|
+
const lc = nc > 0 ? [nc, ...data] : [];
|
|
85
|
+
return [...header, ...lc, le & 0xff];
|
|
86
|
+
}
|
|
87
|
+
const lc = nc > 0 ? [0x00, nc >> 8, nc & 0xff, ...data] : [0x00];
|
|
88
|
+
return [...header, ...lc, (le >> 8) & 0xff, le & 0xff];
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Factory function to create a CommandApdu
|
|
92
|
+
*/
|
|
93
|
+
export default function createCommandApdu(options) {
|
|
94
|
+
return new CommandApdu(options);
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=command-apdu.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"command-apdu.js","sourceRoot":"","sources":["../src/command-apdu.ts"],"names":[],"mappings":"AAqBA,MAAM,eAAe,GAAG,KAAK,CAAC;AAC9B,MAAM,MAAM,GAAG,KAAK,CAAC;AAErB;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,WAAW;IACH,OAAO,CAAW;IAClB,KAAK,CAAW;IACzB,MAAM,CAAW;IAEzB,YAAY,OAA2B;QACnC,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,CAAC,EAAE,GAAG,OAAO,CAAC;QAC7C,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,EAAE,CAAC;QAEhC,IAAI,CAAC,OAAO,GAAG;YACX,SAAS,CAAC,KAAK,EAAE,GAAG,CAAC;YACrB,SAAS,CAAC,KAAK,EAAE,GAAG,CAAC;YACrB,SAAS,CAAC,IAAI,EAAE,EAAE,CAAC;YACnB,SAAS,CAAC,IAAI,EAAE,EAAE,CAAC;SACtB,CAAC;QACF,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,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|
2
|
+
* ISO 7816 Smartcard Communication Library
|
|
3
|
+
*/
|
|
4
|
+
export { default, Iso7816, Instructions, Card } from './iso7816-application.js';
|
|
5
|
+
export { CommandApdu, CommandApduOptions, default as createCommandApdu } from './command-apdu.js';
|
|
6
|
+
export { ResponseApdu, Status, default as createResponseApdu } from './response-apdu.js';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,0BAA0B,CAAC;AAChF,OAAO,EAAE,WAAW,EAAE,kBAAkB,EAAE,OAAO,IAAI,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAClG,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,OAAO,IAAI,kBAAkB,EAAE,MAAM,oBAAoB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ISO 7816 Smartcard Communication Library
|
|
3
|
+
*/
|
|
4
|
+
export { default, Iso7816, Instructions } from './iso7816-application.js';
|
|
5
|
+
export { CommandApdu, default as createCommandApdu } from './command-apdu.js';
|
|
6
|
+
export { ResponseApdu, default as createResponseApdu } from './response-apdu.js';
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAQ,MAAM,0BAA0B,CAAC;AAChF,OAAO,EAAE,WAAW,EAAsB,OAAO,IAAI,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAClG,OAAO,EAAE,YAAY,EAAU,OAAO,IAAI,kBAAkB,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { CommandApdu } from './command-apdu.js';
|
|
2
|
+
import { ResponseApdu } from './response-apdu.js';
|
|
3
|
+
/**
|
|
4
|
+
* ISO 7816 instruction codes
|
|
5
|
+
*/
|
|
6
|
+
export declare const Instructions: {
|
|
7
|
+
readonly APPEND_RECORD: 226;
|
|
8
|
+
readonly ENVELOPE: 194;
|
|
9
|
+
readonly ERASE_BINARY: 14;
|
|
10
|
+
readonly EXTERNAL_AUTHENTICATE: 130;
|
|
11
|
+
readonly GET_CHALLENGE: 132;
|
|
12
|
+
readonly GET_DATA: 202;
|
|
13
|
+
readonly GET_RESPONSE: 192;
|
|
14
|
+
readonly INTERNAL_AUTHENTICATE: 136;
|
|
15
|
+
readonly MANAGE_CHANNEL: 112;
|
|
16
|
+
readonly PUT_DATA: 218;
|
|
17
|
+
readonly READ_BINARY: 176;
|
|
18
|
+
readonly READ_RECORD: 178;
|
|
19
|
+
readonly SELECT_FILE: 164;
|
|
20
|
+
readonly UPDATE_BINARY: 214;
|
|
21
|
+
readonly UPDATE_RECORD: 220;
|
|
22
|
+
readonly VERIFY: 32;
|
|
23
|
+
readonly WRITE_BINARY: 208;
|
|
24
|
+
readonly WRITE_RECORD: 210;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Card interface for any transport that can send/receive APDUs
|
|
28
|
+
*/
|
|
29
|
+
export interface Card {
|
|
30
|
+
/** Card ATR (Answer To Reset) */
|
|
31
|
+
atr: Buffer;
|
|
32
|
+
/** Transmit APDU command to card */
|
|
33
|
+
transmit(data: Buffer | number[]): Promise<Buffer>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* ISO 7816 Application class for APDU communication
|
|
37
|
+
*/
|
|
38
|
+
export declare class Iso7816 {
|
|
39
|
+
private readonly _card;
|
|
40
|
+
constructor(card: Card);
|
|
41
|
+
/**
|
|
42
|
+
* Get the underlying card
|
|
43
|
+
*/
|
|
44
|
+
get card(): Card;
|
|
45
|
+
/**
|
|
46
|
+
* Issue a raw APDU command. When the card answers 61xx, sends GET RESPONSE until it has
|
|
47
|
+
* everything, and returns all the data with the final status word.
|
|
48
|
+
* @param commandApdu The command to send
|
|
49
|
+
* @param maxRetries Maximum number of retries for wrong length responses (default: 3)
|
|
50
|
+
* @returns Promise resolving to the response
|
|
51
|
+
* @throws Error if maximum retries exceeded
|
|
52
|
+
*/
|
|
53
|
+
issueCommand(commandApdu: CommandApdu, maxRetries?: number): Promise<ResponseApdu>;
|
|
54
|
+
/**
|
|
55
|
+
* Select a file on the smartcard
|
|
56
|
+
* @param bytes File identifier bytes (e.g., AID)
|
|
57
|
+
* @param p1 P1 parameter (default: 0x04)
|
|
58
|
+
* @param p2 P2 parameter (default: 0x00)
|
|
59
|
+
* @returns Promise resolving to the response
|
|
60
|
+
*/
|
|
61
|
+
selectFile(bytes: number[], p1?: number, p2?: number): Promise<ResponseApdu>;
|
|
62
|
+
/**
|
|
63
|
+
* Get additional response bytes
|
|
64
|
+
* @param length Number of bytes to retrieve
|
|
65
|
+
* @returns Promise resolving to the response
|
|
66
|
+
*/
|
|
67
|
+
getResponse(length: number): Promise<ResponseApdu>;
|
|
68
|
+
/**
|
|
69
|
+
* Read a record from a file
|
|
70
|
+
* @param sfi Short File Identifier
|
|
71
|
+
* @param record Record number
|
|
72
|
+
* @returns Promise resolving to the response
|
|
73
|
+
*/
|
|
74
|
+
readRecord(sfi: number, record: number): Promise<ResponseApdu>;
|
|
75
|
+
/**
|
|
76
|
+
* Get data from the card
|
|
77
|
+
* @param p1 P1 parameter
|
|
78
|
+
* @param p2 P2 parameter
|
|
79
|
+
* @returns Promise resolving to the response
|
|
80
|
+
*/
|
|
81
|
+
getData(p1: number, p2: number): Promise<ResponseApdu>;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Factory function to create an Iso7816 application
|
|
85
|
+
* @param card A card object from the smartcard package
|
|
86
|
+
* @returns An Iso7816 application instance
|
|
87
|
+
*/
|
|
88
|
+
export default function createIso7816(card: Card): Iso7816;
|
|
89
|
+
export { CommandApdu, CommandApduOptions } from './command-apdu.js';
|
|
90
|
+
export { ResponseApdu, Status } from './response-apdu.js';
|
|
91
|
+
//# sourceMappingURL=iso7816-application.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"iso7816-application.d.ts","sourceRoot":"","sources":["../src/iso7816-application.ts"],"names":[],"mappings":"AAAA,OAA0B,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACnE,OAA2B,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEtE;;GAEG;AACH,eAAO,MAAM,YAAY;;;;;;;;;;;;;;;;;;;CAmBf,CAAC;AAEX;;GAEG;AACH,MAAM,WAAW,IAAI;IACjB,iCAAiC;IACjC,GAAG,EAAE,MAAM,CAAC;IACZ,oCAAoC;IACpC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CACtD;AAED;;GAEG;AACH,qBAAa,OAAO;IAChB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAO;gBAEjB,IAAI,EAAE,IAAI;IAItB;;OAEG;IACH,IAAI,IAAI,IAAI,IAAI,CAEf;IAED;;;;;;;OAOG;IACG,YAAY,CAAC,WAAW,EAAE,WAAW,EAAE,UAAU,GAAE,MAAU,GAAG,OAAO,CAAC,YAAY,CAAC;IAgC3F;;;;;;OAMG;IACG,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,GAAE,MAAa,EAAE,EAAE,GAAE,MAAa,GAAG,OAAO,CAAC,YAAY,CAAC;IAY9F;;;;OAIG;IACG,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IAYxD;;;;;OAKG;IACG,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IAYpE;;;;;OAKG;IACG,OAAO,CAAC,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;CAW/D;AAED;;;;GAIG;AACH,MAAM,CAAC,OAAO,UAAU,aAAa,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAEzD;AAGD,OAAO,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import createCommandApdu from './command-apdu.js';
|
|
2
|
+
import createResponseApdu from './response-apdu.js';
|
|
3
|
+
/**
|
|
4
|
+
* ISO 7816 instruction codes
|
|
5
|
+
*/
|
|
6
|
+
export const Instructions = {
|
|
7
|
+
APPEND_RECORD: 0xe2,
|
|
8
|
+
ENVELOPE: 0xc2,
|
|
9
|
+
ERASE_BINARY: 0x0e,
|
|
10
|
+
EXTERNAL_AUTHENTICATE: 0x82,
|
|
11
|
+
GET_CHALLENGE: 0x84,
|
|
12
|
+
GET_DATA: 0xca,
|
|
13
|
+
GET_RESPONSE: 0xc0,
|
|
14
|
+
INTERNAL_AUTHENTICATE: 0x88,
|
|
15
|
+
MANAGE_CHANNEL: 0x70,
|
|
16
|
+
PUT_DATA: 0xda,
|
|
17
|
+
READ_BINARY: 0xb0,
|
|
18
|
+
READ_RECORD: 0xb2,
|
|
19
|
+
SELECT_FILE: 0xa4,
|
|
20
|
+
UPDATE_BINARY: 0xd6,
|
|
21
|
+
UPDATE_RECORD: 0xdc,
|
|
22
|
+
VERIFY: 0x20,
|
|
23
|
+
WRITE_BINARY: 0xd0,
|
|
24
|
+
WRITE_RECORD: 0xd2,
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* ISO 7816 Application class for APDU communication
|
|
28
|
+
*/
|
|
29
|
+
export class Iso7816 {
|
|
30
|
+
_card;
|
|
31
|
+
constructor(card) {
|
|
32
|
+
this._card = card;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Get the underlying card
|
|
36
|
+
*/
|
|
37
|
+
get card() {
|
|
38
|
+
return this._card;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Issue a raw APDU command. When the card answers 61xx, sends GET RESPONSE until it has
|
|
42
|
+
* everything, and returns all the data with the final status word.
|
|
43
|
+
* @param commandApdu The command to send
|
|
44
|
+
* @param maxRetries Maximum number of retries for wrong length responses (default: 3)
|
|
45
|
+
* @returns Promise resolving to the response
|
|
46
|
+
* @throws Error if maximum retries exceeded
|
|
47
|
+
*/
|
|
48
|
+
async issueCommand(commandApdu, maxRetries = 3) {
|
|
49
|
+
let retries = 0;
|
|
50
|
+
const execute = async () => {
|
|
51
|
+
const resp = await this._card.transmit(commandApdu.toBuffer());
|
|
52
|
+
const response = createResponseApdu(resp);
|
|
53
|
+
if (response.hasMoreBytesAvailable()) {
|
|
54
|
+
// Keep any data that came with this response, then add the rest. getResponse
|
|
55
|
+
// follows further 61xx responses the same way.
|
|
56
|
+
const rest = await this.getResponse(response.numberOfBytesAvailable());
|
|
57
|
+
const data = response.getBuffer().subarray(0, -2);
|
|
58
|
+
return createResponseApdu(Buffer.concat([data, rest.getBuffer()]));
|
|
59
|
+
}
|
|
60
|
+
else if (response.isWrongLength()) {
|
|
61
|
+
if (retries >= maxRetries) {
|
|
62
|
+
throw new Error(`Maximum retries (${maxRetries}) exceeded for wrong length response`);
|
|
63
|
+
}
|
|
64
|
+
retries++;
|
|
65
|
+
// SW2 of 00 means exactly 256. setLe(0) would ask for the maximum, which is
|
|
66
|
+
// 65536 for an extended command.
|
|
67
|
+
commandApdu.setLe(response.correctLength() || 256);
|
|
68
|
+
return execute();
|
|
69
|
+
}
|
|
70
|
+
return response;
|
|
71
|
+
};
|
|
72
|
+
return execute();
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Select a file on the smartcard
|
|
76
|
+
* @param bytes File identifier bytes (e.g., AID)
|
|
77
|
+
* @param p1 P1 parameter (default: 0x04)
|
|
78
|
+
* @param p2 P2 parameter (default: 0x00)
|
|
79
|
+
* @returns Promise resolving to the response
|
|
80
|
+
*/
|
|
81
|
+
async selectFile(bytes, p1 = 0x04, p2 = 0x00) {
|
|
82
|
+
return this.issueCommand(createCommandApdu({
|
|
83
|
+
cla: 0x00,
|
|
84
|
+
ins: Instructions.SELECT_FILE,
|
|
85
|
+
p1,
|
|
86
|
+
p2,
|
|
87
|
+
data: bytes,
|
|
88
|
+
}));
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Get additional response bytes
|
|
92
|
+
* @param length Number of bytes to retrieve
|
|
93
|
+
* @returns Promise resolving to the response
|
|
94
|
+
*/
|
|
95
|
+
async getResponse(length) {
|
|
96
|
+
return this.issueCommand(createCommandApdu({
|
|
97
|
+
cla: 0x00,
|
|
98
|
+
ins: Instructions.GET_RESPONSE,
|
|
99
|
+
p1: 0x00,
|
|
100
|
+
p2: 0x00,
|
|
101
|
+
le: length,
|
|
102
|
+
}));
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Read a record from a file
|
|
106
|
+
* @param sfi Short File Identifier
|
|
107
|
+
* @param record Record number
|
|
108
|
+
* @returns Promise resolving to the response
|
|
109
|
+
*/
|
|
110
|
+
async readRecord(sfi, record) {
|
|
111
|
+
return this.issueCommand(createCommandApdu({
|
|
112
|
+
cla: 0x00,
|
|
113
|
+
ins: Instructions.READ_RECORD,
|
|
114
|
+
p1: record,
|
|
115
|
+
p2: (sfi << 3) + 4,
|
|
116
|
+
le: 0,
|
|
117
|
+
}));
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Get data from the card
|
|
121
|
+
* @param p1 P1 parameter
|
|
122
|
+
* @param p2 P2 parameter
|
|
123
|
+
* @returns Promise resolving to the response
|
|
124
|
+
*/
|
|
125
|
+
async getData(p1, p2) {
|
|
126
|
+
return this.issueCommand(createCommandApdu({
|
|
127
|
+
cla: 0x00,
|
|
128
|
+
ins: Instructions.GET_DATA,
|
|
129
|
+
p1,
|
|
130
|
+
p2,
|
|
131
|
+
le: 0,
|
|
132
|
+
}));
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Factory function to create an Iso7816 application
|
|
137
|
+
* @param card A card object from the smartcard package
|
|
138
|
+
* @returns An Iso7816 application instance
|
|
139
|
+
*/
|
|
140
|
+
export default function createIso7816(card) {
|
|
141
|
+
return new Iso7816(card);
|
|
142
|
+
}
|
|
143
|
+
// Re-export types for convenience
|
|
144
|
+
export { CommandApdu } from './command-apdu.js';
|
|
145
|
+
export { ResponseApdu } from './response-apdu.js';
|
|
146
|
+
//# sourceMappingURL=iso7816-application.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"iso7816-application.js","sourceRoot":"","sources":["../src/iso7816-application.ts"],"names":[],"mappings":"AAAA,OAAO,iBAAkC,MAAM,mBAAmB,CAAC;AACnE,OAAO,kBAAoC,MAAM,oBAAoB,CAAC;AAEtE;;GAEG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG;IACxB,aAAa,EAAE,IAAI;IACnB,QAAQ,EAAE,IAAI;IACd,YAAY,EAAE,IAAI;IAClB,qBAAqB,EAAE,IAAI;IAC3B,aAAa,EAAE,IAAI;IACnB,QAAQ,EAAE,IAAI;IACd,YAAY,EAAE,IAAI;IAClB,qBAAqB,EAAE,IAAI;IAC3B,cAAc,EAAE,IAAI;IACpB,QAAQ,EAAE,IAAI;IACd,WAAW,EAAE,IAAI;IACjB,WAAW,EAAE,IAAI;IACjB,WAAW,EAAE,IAAI;IACjB,aAAa,EAAE,IAAI;IACnB,aAAa,EAAE,IAAI;IACnB,MAAM,EAAE,IAAI;IACZ,YAAY,EAAE,IAAI;IAClB,YAAY,EAAE,IAAI;CACZ,CAAC;AAYX;;GAEG;AACH,MAAM,OAAO,OAAO;IACC,KAAK,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C,OAAO,UAAU,aAAa,CAAC,IAAU;IAC5C,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;AAC7B,CAAC;AAED,kCAAkC;AAClC,OAAO,EAAE,WAAW,EAAsB,MAAM,mBAAmB,CAAC;AACpE,OAAO,EAAE,YAAY,EAAU,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Status information from a response
|
|
3
|
+
*/
|
|
4
|
+
export interface Status {
|
|
5
|
+
/** 4-character hex status code (e.g., "9000") */
|
|
6
|
+
code: string;
|
|
7
|
+
/** Human-readable meaning of the status */
|
|
8
|
+
meaning: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* APDU Response class for parsing ISO 7816 response APDUs
|
|
12
|
+
*/
|
|
13
|
+
export declare class ResponseApdu {
|
|
14
|
+
private readonly _buffer;
|
|
15
|
+
private readonly _data;
|
|
16
|
+
constructor(buffer: Buffer);
|
|
17
|
+
/**
|
|
18
|
+
* Get SW1 (first status byte) as 2-character hex string
|
|
19
|
+
*/
|
|
20
|
+
private getSW1;
|
|
21
|
+
/**
|
|
22
|
+
* Get SW2 (second status byte) as 2-character hex string
|
|
23
|
+
*/
|
|
24
|
+
private getSW2;
|
|
25
|
+
/**
|
|
26
|
+
* Get status code and meaning
|
|
27
|
+
*/
|
|
28
|
+
getStatus(): Status;
|
|
29
|
+
/**
|
|
30
|
+
* Get 4-character hex status code (SW1 + SW2)
|
|
31
|
+
*/
|
|
32
|
+
getStatusCode(): string;
|
|
33
|
+
/**
|
|
34
|
+
* Check if status is 9000 (success)
|
|
35
|
+
*/
|
|
36
|
+
isOk(): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Get the raw buffer
|
|
39
|
+
*/
|
|
40
|
+
getBuffer(): Buffer;
|
|
41
|
+
/**
|
|
42
|
+
* Check if more bytes are available (61xx status)
|
|
43
|
+
*/
|
|
44
|
+
hasMoreBytesAvailable(): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Get number of additional bytes available (from 61xx status)
|
|
47
|
+
*/
|
|
48
|
+
numberOfBytesAvailable(): number;
|
|
49
|
+
/**
|
|
50
|
+
* Check if wrong length was specified (6cxx status)
|
|
51
|
+
*/
|
|
52
|
+
isWrongLength(): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Get the correct length from 6cxx response
|
|
55
|
+
*/
|
|
56
|
+
correctLength(): number;
|
|
57
|
+
/**
|
|
58
|
+
* Convert to hex string
|
|
59
|
+
*/
|
|
60
|
+
toString(): string;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Factory function to create a ResponseApdu
|
|
64
|
+
*/
|
|
65
|
+
export default function createResponseApdu(buffer: Buffer): ResponseApdu;
|
|
66
|
+
//# sourceMappingURL=response-apdu.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"response-apdu.d.ts","sourceRoot":"","sources":["../src/response-apdu.ts"],"names":[],"mappings":"AAsDA;;GAEG;AACH,MAAM,WAAW,MAAM;IACnB,iDAAiD;IACjD,IAAI,EAAE,MAAM,CAAC;IACb,2CAA2C;IAC3C,OAAO,EAAE,MAAM,CAAC;CACnB;AAED;;GAEG;AACH,qBAAa,YAAY;IACrB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;gBAEnB,MAAM,EAAE,MAAM;IAQ1B;;OAEG;IACH,OAAO,CAAC,MAAM;IAId;;OAEG;IACH,OAAO,CAAC,MAAM;IAId;;OAEG;IACH,SAAS,IAAI,MAAM;IAYnB;;OAEG;IACH,aAAa,IAAI,MAAM;IAIvB;;OAEG;IACH,IAAI,IAAI,OAAO;IAIf;;OAEG;IACH,SAAS,IAAI,MAAM;IAInB;;OAEG;IACH,qBAAqB,IAAI,OAAO;IAIhC;;OAEG;IACH,sBAAsB,IAAI,MAAM;IAIhC;;OAEG;IACH,aAAa,IAAI,OAAO;IAIxB;;OAEG;IACH,aAAa,IAAI,MAAM;IAIvB;;OAEG;IACH,QAAQ,IAAI,MAAM;CAGrB;AAED;;GAEG;AACH,MAAM,CAAC,OAAO,UAAU,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY,CAEvE"}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ISO 7816 status code mappings (ordered from most specific to least specific)
|
|
3
|
+
*/
|
|
4
|
+
const statusCodes = [
|
|
5
|
+
// Specific status codes first
|
|
6
|
+
[/^9000$/, 'Normal processing'],
|
|
7
|
+
[/^6200$/, 'No information given'],
|
|
8
|
+
[/^6281$/, 'Part of return data may be corrupted'],
|
|
9
|
+
[/^6282$/, 'End of file/record reached before reading Le bytes'],
|
|
10
|
+
[/^6283$/, 'Selected file invalidated'],
|
|
11
|
+
[/^6284$/, 'FCI not formatted correctly'],
|
|
12
|
+
[/^6285$/, 'File control info not in required format'],
|
|
13
|
+
[/^6286$/, 'Unsuccessful writing'],
|
|
14
|
+
[/^6300$/, 'Authentication failed'],
|
|
15
|
+
[/^6381$/, 'Last write filled up file'],
|
|
16
|
+
[/^6382$/, 'Execution successful after retry'],
|
|
17
|
+
[/^6500$/, 'No information given'],
|
|
18
|
+
[/^6581$/, 'Memory failure'],
|
|
19
|
+
[/^6700$/, 'Wrong length'],
|
|
20
|
+
[/^6800$/, 'No information given'],
|
|
21
|
+
[/^6881$/, 'Logical channel not supported'],
|
|
22
|
+
[/^6882$/, 'Secure messaging not supported'],
|
|
23
|
+
[/^6981$/, 'Command incompatible with file structure'],
|
|
24
|
+
[/^6982$/, 'Security status not satisfied'],
|
|
25
|
+
[/^6983$/, 'Authentication method blocked'],
|
|
26
|
+
[/^6984$/, 'Referenced data invalidated'],
|
|
27
|
+
[/^6985$/, 'Conditions of use not satisfied'],
|
|
28
|
+
[/^6986$/, 'Command not allowed (no current EF)'],
|
|
29
|
+
[/^6a80$/, 'Incorrect parameters in command data field'],
|
|
30
|
+
[/^6a81$/, 'Function not supported'],
|
|
31
|
+
[/^6a82$/, 'File not found'],
|
|
32
|
+
[/^6a83$/, 'Record not found'],
|
|
33
|
+
[/^6a84$/, 'Not enough memory space'],
|
|
34
|
+
[/^6a85$/, 'Lc inconsistent with TLV structure'],
|
|
35
|
+
[/^6a86$/, 'Incorrect parameters P1-P2'],
|
|
36
|
+
[/^6a87$/, 'Lc inconsistent with P1-P2'],
|
|
37
|
+
[/^6a88$/, 'Referenced data not found'],
|
|
38
|
+
// Generic status codes (wildcards) last
|
|
39
|
+
[/^61(.{2})$/, 'More data available'],
|
|
40
|
+
[/^62(.{2})$/, 'Warning processing'],
|
|
41
|
+
[/^63(.{2})$/, 'Warning processing'],
|
|
42
|
+
[/^64(.{2})$/, 'Execution error'],
|
|
43
|
+
[/^65(.{2})$/, 'Execution error'],
|
|
44
|
+
[/^66(.{2})$/, 'Reserved for future use'],
|
|
45
|
+
[/^68(.{2})$/, 'Functions in CLA not supported'],
|
|
46
|
+
[/^69(.{2})$/, 'Command not allowed'],
|
|
47
|
+
[/^6a(.{2})$/, 'Wrong parameters P1-P2'],
|
|
48
|
+
[/^6b(.{2})$/, 'Wrong parameters'],
|
|
49
|
+
[/^6c(.{2})$/, 'Wrong length Le'],
|
|
50
|
+
[/^6d(.{2})$/, 'Instruction not supported'],
|
|
51
|
+
[/^6e(.{2})$/, 'Class not supported'],
|
|
52
|
+
[/^6f(.{2})$/, 'No precise diagnosis'],
|
|
53
|
+
];
|
|
54
|
+
/**
|
|
55
|
+
* APDU Response class for parsing ISO 7816 response APDUs
|
|
56
|
+
*/
|
|
57
|
+
export class ResponseApdu {
|
|
58
|
+
_buffer;
|
|
59
|
+
_data;
|
|
60
|
+
constructor(buffer) {
|
|
61
|
+
if (buffer.length < 2) {
|
|
62
|
+
throw new Error('Response APDU must contain at least 2 bytes (SW1 and SW2)');
|
|
63
|
+
}
|
|
64
|
+
this._buffer = buffer;
|
|
65
|
+
this._data = buffer.toString('hex');
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Get SW1 (first status byte) as 2-character hex string
|
|
69
|
+
*/
|
|
70
|
+
getSW1() {
|
|
71
|
+
return this._data.slice(-4, -2);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Get SW2 (second status byte) as 2-character hex string
|
|
75
|
+
*/
|
|
76
|
+
getSW2() {
|
|
77
|
+
return this._data.slice(-2);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Get status code and meaning
|
|
81
|
+
*/
|
|
82
|
+
getStatus() {
|
|
83
|
+
const statusCode = this.getStatusCode();
|
|
84
|
+
for (const [pattern, meaning] of statusCodes) {
|
|
85
|
+
if (pattern.test(statusCode)) {
|
|
86
|
+
return { code: statusCode, meaning };
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return { code: statusCode, meaning: 'Unknown' };
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Get 4-character hex status code (SW1 + SW2)
|
|
93
|
+
*/
|
|
94
|
+
getStatusCode() {
|
|
95
|
+
return this._data.slice(-4);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Check if status is 9000 (success)
|
|
99
|
+
*/
|
|
100
|
+
isOk() {
|
|
101
|
+
return this.getStatusCode() === '9000';
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Get the raw buffer
|
|
105
|
+
*/
|
|
106
|
+
getBuffer() {
|
|
107
|
+
return this._buffer;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Check if more bytes are available (61xx status)
|
|
111
|
+
*/
|
|
112
|
+
hasMoreBytesAvailable() {
|
|
113
|
+
return this.getSW1() === '61';
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Get number of additional bytes available (from 61xx status)
|
|
117
|
+
*/
|
|
118
|
+
numberOfBytesAvailable() {
|
|
119
|
+
return parseInt(this.getSW2(), 16);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Check if wrong length was specified (6cxx status)
|
|
123
|
+
*/
|
|
124
|
+
isWrongLength() {
|
|
125
|
+
return this.getSW1() === '6c';
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Get the correct length from 6cxx response
|
|
129
|
+
*/
|
|
130
|
+
correctLength() {
|
|
131
|
+
return parseInt(this.getSW2(), 16);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Convert to hex string
|
|
135
|
+
*/
|
|
136
|
+
toString() {
|
|
137
|
+
return this._data;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Factory function to create a ResponseApdu
|
|
142
|
+
*/
|
|
143
|
+
export default function createResponseApdu(buffer) {
|
|
144
|
+
return new ResponseApdu(buffer);
|
|
145
|
+
}
|
|
146
|
+
//# sourceMappingURL=response-apdu.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"response-apdu.js","sourceRoot":"","sources":["../src/response-apdu.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,GAA4B;IACzC,8BAA8B;IAC9B,CAAC,QAAQ,EAAE,mBAAmB,CAAC;IAC/B,CAAC,QAAQ,EAAE,sBAAsB,CAAC;IAClC,CAAC,QAAQ,EAAE,sCAAsC,CAAC;IAClD,CAAC,QAAQ,EAAE,oDAAoD,CAAC;IAChE,CAAC,QAAQ,EAAE,2BAA2B,CAAC;IACvC,CAAC,QAAQ,EAAE,6BAA6B,CAAC;IACzC,CAAC,QAAQ,EAAE,0CAA0C,CAAC;IACtD,CAAC,QAAQ,EAAE,sBAAsB,CAAC;IAClC,CAAC,QAAQ,EAAE,uBAAuB,CAAC;IACnC,CAAC,QAAQ,EAAE,2BAA2B,CAAC;IACvC,CAAC,QAAQ,EAAE,kCAAkC,CAAC;IAC9C,CAAC,QAAQ,EAAE,sBAAsB,CAAC;IAClC,CAAC,QAAQ,EAAE,gBAAgB,CAAC;IAC5B,CAAC,QAAQ,EA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S;QACL,OAAO,IAAI,CAAC,OAAO,CAAC;IACxB,CAAC;IAED;;OAEG;IACH,qBAAqB;QACjB,OAAO,IAAI,CAAC,MAAM,EAAE,KAAK,IAAI,CAAC;IAClC,CAAC;IAED;;OAEG;IACH,sBAAsB;QAClB,OAAO,QAAQ,CAAC,IAAI,CAAC,MAAM,EAAE,EAAE,EAAE,CAAC,CAAC;IACvC,CAAC;IAED;;OAEG;IACH,aAAa;QACT,OAAO,IAAI,CAAC,MAAM,EAAE,KAAK,IAAI,CAAC;IAClC,CAAC;IAED;;OAEG;IACH,aAAa;QACT,OAAO,QAAQ,CAAC,IAAI,CAAC,MAAM,EAAE,EAAE,EAAE,CAAC,CAAC;IACvC,CAAC;IAED;;OAEG;IACH,QAAQ;QACJ,OAAO,IAAI,CAAC,KAAK,CAAC;IACtB,CAAC;CACJ;AAED;;GAEG;AACH,MAAM,CAAC,OAAO,UAAU,kBAAkB,CAAC,MAAc;IACrD,OAAO,IAAI,YAAY,CAAC,MAAM,CAAC,CAAC;AACpC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,13 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "iso7816",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "",
|
|
5
|
-
"
|
|
3
|
+
"version": "3.0.0",
|
|
4
|
+
"description": "ISO 7816 APDU command and response handling library",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
6
17
|
"scripts": {
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
"
|
|
18
|
+
"build": "tsc",
|
|
19
|
+
"clean": "rm -rf dist",
|
|
20
|
+
"prebuild": "npm run clean",
|
|
21
|
+
"test": "vitest run",
|
|
22
|
+
"test:watch": "vitest",
|
|
23
|
+
"test:coverage": "vitest run --coverage",
|
|
24
|
+
"typecheck": "tsc --noEmit",
|
|
25
|
+
"lint": "eslint src/",
|
|
26
|
+
"format": "prettier --write src/",
|
|
27
|
+
"format:check": "prettier --check src/",
|
|
28
|
+
"prepublishOnly": "npm run build",
|
|
29
|
+
"release:patch": "npm run lint && npm run typecheck && npm test && npm run build && npm version patch && git push --follow-tags && npm publish"
|
|
30
|
+
},
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">=20.0.0"
|
|
11
33
|
},
|
|
12
34
|
"author": "tomkp <tom@tomkp.com>",
|
|
13
35
|
"keywords": [
|
|
@@ -18,7 +40,8 @@
|
|
|
18
40
|
"iso-7816",
|
|
19
41
|
"chip-and-pin",
|
|
20
42
|
"emv",
|
|
21
|
-
"
|
|
43
|
+
"esm",
|
|
44
|
+
"typescript"
|
|
22
45
|
],
|
|
23
46
|
"license": "MIT",
|
|
24
47
|
"bugs": {
|
|
@@ -29,13 +52,16 @@
|
|
|
29
52
|
"url": "https://github.com/tomkp/iso7816.git"
|
|
30
53
|
},
|
|
31
54
|
"homepage": "https://github.com/tomkp/iso7816",
|
|
32
|
-
"dependencies": {
|
|
33
|
-
"apdu": "^0.0.3",
|
|
34
|
-
"card-reader": "^1.0.3",
|
|
35
|
-
"hexify": "^1.0.1"
|
|
36
|
-
},
|
|
37
55
|
"devDependencies": {
|
|
38
|
-
"
|
|
39
|
-
"
|
|
56
|
+
"@eslint/js": "^9.39.2",
|
|
57
|
+
"@types/node": "^22.0.0",
|
|
58
|
+
"@vitest/coverage-v8": "^4.0.16",
|
|
59
|
+
"eslint": "^9.39.2",
|
|
60
|
+
"eslint-config-prettier": "^10.1.8",
|
|
61
|
+
"fast-check": "^4.10.2",
|
|
62
|
+
"prettier": "^3.7.4",
|
|
63
|
+
"typescript": "^5.7.0",
|
|
64
|
+
"typescript-eslint": "^8.50.1",
|
|
65
|
+
"vitest": "^4.0.16"
|
|
40
66
|
}
|
|
41
67
|
}
|
package/.babelrc
DELETED
package/.npmignore
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
src/
|
package/demo/iso7816-demo.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
var devices = require('card-reader');
|
|
2
|
-
var iso7816 = require('../lib/iso7816-application');
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
devices.on('device-activated', function (event) {
|
|
6
|
-
console.log(`Device '${event.reader.name}' activated, devices: ${devices.listDevices()}`);
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
devices.on('device-deactivated', function (event) {
|
|
10
|
-
console.log(`Device '${event.reader.name}' deactivated, devices: ${devices.listDevices()}`);
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
devices.on('card-removed', function (event) {
|
|
14
|
-
console.log(`Card removed from '${event.reader.name}' `);
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
devices.on('command-issued', function (event) {
|
|
18
|
-
console.log(`Command '${event.command}' issued to '${event.reader.name}' `);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
devices.on('response-received', function (event) {
|
|
22
|
-
console.log(`Response '${event.response}' received from '${event.reader.name}' in response to '${event.command}'`);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
devices.on('error', function (event) {
|
|
26
|
-
console.log(`Error '${event.error}' received`);
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
devices.on('card-inserted', function (event) {
|
|
30
|
-
|
|
31
|
-
console.log(`List devices: ${devices.listDevices()}`);
|
|
32
|
-
|
|
33
|
-
var reader = event.reader;
|
|
34
|
-
console.log(`Card inserted into '${reader.name}', atr: '${event.status.atr.toString('hex')}'`);
|
|
35
|
-
|
|
36
|
-
var application = iso7816(devices, reader);
|
|
37
|
-
application
|
|
38
|
-
.selectFile([0x31, 0x50, 0x41, 0x59, 0x2E, 0x53, 0x59, 0x53, 0x2E, 0x44, 0x44, 0x46, 0x30, 0x31])
|
|
39
|
-
.then(function (response) {
|
|
40
|
-
console.info(`Select PSE Response: '${response}'`);
|
|
41
|
-
}).catch(function (error) {
|
|
42
|
-
console.error('Error:', error, error.stack);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
});
|
|
Binary file
|
|
Binary file
|
package/lib/command-apdu.js
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var _hexify = require("hexify");
|
|
4
|
-
|
|
5
|
-
var _hexify2 = _interopRequireDefault(_hexify);
|
|
6
|
-
|
|
7
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
8
|
-
|
|
9
|
-
function CommandApdu(obj) {
|
|
10
|
-
|
|
11
|
-
var size = obj.size;
|
|
12
|
-
var cla = obj.cla;
|
|
13
|
-
var ins = obj.ins;
|
|
14
|
-
var p1 = obj.p1;
|
|
15
|
-
var p2 = obj.p2;
|
|
16
|
-
var data = obj.data;
|
|
17
|
-
var le = obj.le || 0;
|
|
18
|
-
var lc = void 0;
|
|
19
|
-
|
|
20
|
-
// case 1
|
|
21
|
-
if (!size && !data && !le) {
|
|
22
|
-
//le = -1;
|
|
23
|
-
//console.info('case 1');
|
|
24
|
-
size = 4;
|
|
25
|
-
}
|
|
26
|
-
// case 2
|
|
27
|
-
else if (!size && !data) {
|
|
28
|
-
//console.info('case 2');
|
|
29
|
-
size = 4 + 2;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// case 3
|
|
33
|
-
else if (!size && !le) {
|
|
34
|
-
//console.info('case 3');
|
|
35
|
-
size = data.length + 5 + 4;
|
|
36
|
-
//le = -1;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// case 4
|
|
40
|
-
else if (!size) {
|
|
41
|
-
//console.info('case 4');
|
|
42
|
-
size = data.length + 5 + 4;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// set data
|
|
46
|
-
if (data) {
|
|
47
|
-
lc = data.length;
|
|
48
|
-
} else {
|
|
49
|
-
//lc = 0;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
this.bytes = [];
|
|
53
|
-
this.bytes.push(cla);
|
|
54
|
-
this.bytes.push(ins);
|
|
55
|
-
this.bytes.push(p1);
|
|
56
|
-
this.bytes.push(p2);
|
|
57
|
-
|
|
58
|
-
if (data) {
|
|
59
|
-
this.bytes.push(lc);
|
|
60
|
-
this.bytes = this.bytes.concat(data);
|
|
61
|
-
}
|
|
62
|
-
this.bytes.push(le);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
CommandApdu.prototype.toString = function () {
|
|
66
|
-
return _hexify2.default.toHexString(bytes);
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
CommandApdu.prototype.toByteArray = function () {
|
|
70
|
-
return this.bytes;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
CommandApdu.prototype.toBuffer = function () {
|
|
74
|
-
return new Buffer(this.bytes);
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
CommandApdu.prototype.setLe = function (le) {
|
|
78
|
-
this.bytes.pop();
|
|
79
|
-
this.bytes.push(le);
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
function create(obj) {
|
|
83
|
-
return new CommandApdu(obj);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
module.exports = create;
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var _commandApdu = require('./command-apdu');
|
|
4
|
-
|
|
5
|
-
var _commandApdu2 = _interopRequireDefault(_commandApdu);
|
|
6
|
-
|
|
7
|
-
var _responseApdu = require('./response-apdu');
|
|
8
|
-
|
|
9
|
-
var _responseApdu2 = _interopRequireDefault(_responseApdu);
|
|
10
|
-
|
|
11
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
12
|
-
|
|
13
|
-
var ins = {
|
|
14
|
-
APPEND_RECORD: 0xE2,
|
|
15
|
-
ENVELOPE: 0xC2,
|
|
16
|
-
ERASE_BINARY: 0x0E,
|
|
17
|
-
EXTERNAL_AUTHENTICATE: 0x82,
|
|
18
|
-
GET_CHALLENGE: 0x84,
|
|
19
|
-
GET_DATA: 0xCA,
|
|
20
|
-
GET_RESPONSE: 0xC0,
|
|
21
|
-
INTERNAL_AUTHENTICATE: 0x88,
|
|
22
|
-
MANAGE_CHANNEL: 0x70,
|
|
23
|
-
PUT_DATA: 0xDA,
|
|
24
|
-
READ_BINARY: 0xB0,
|
|
25
|
-
READ_RECORD: 0xB2,
|
|
26
|
-
SELECT_FILE: 0xA4,
|
|
27
|
-
UPDATE_BINARY: 0xD6,
|
|
28
|
-
UPDATE_RECORD: 0xDC,
|
|
29
|
-
VERIFY: 0x20,
|
|
30
|
-
WRITE_BINARY: 0xD0,
|
|
31
|
-
WRITE_RECORD: 0xD2
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
function Iso7816(devices, cardReader) {
|
|
35
|
-
this.devices = devices;
|
|
36
|
-
this.cardReader = cardReader;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
Iso7816.prototype.issueCommand = function (commandApdu) {
|
|
40
|
-
var _this = this;
|
|
41
|
-
|
|
42
|
-
//console.log(`Iso7816.issueCommand '${commandApdu}' `);
|
|
43
|
-
return this.devices.issueCommand(this.cardReader, commandApdu.toBuffer()).then(function (resp) {
|
|
44
|
-
var response = (0, _responseApdu2.default)(resp);
|
|
45
|
-
//console.log(`status code '${response.statusCode()}'`);
|
|
46
|
-
if (response.hasMoreBytesAvailable()) {
|
|
47
|
-
//console.log(`has '${response.numberOfBytesAvailable()}' more bytes available`);
|
|
48
|
-
return _this.getResponse(response.numberOfBytesAvailable());
|
|
49
|
-
} else if (response.isWrongLength()) {
|
|
50
|
-
//console.log(`'le' should be '${response.correctLength()}' bytes`);
|
|
51
|
-
commandApdu.setLe(response.correctLength());
|
|
52
|
-
return _this.issueCommand(commandApdu);
|
|
53
|
-
}
|
|
54
|
-
//console.log(`return response '${response}' `);
|
|
55
|
-
return response;
|
|
56
|
-
});
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
Iso7816.prototype.selectFile = function (bytes, p1, p2) {
|
|
60
|
-
//console.log(`Iso7816.selectFile, file='${bytes}'`);
|
|
61
|
-
return this.issueCommand((0, _commandApdu2.default)({
|
|
62
|
-
cla: 0x00,
|
|
63
|
-
ins: ins.SELECT_FILE,
|
|
64
|
-
p1: p1 || 0x04,
|
|
65
|
-
p2: p2 || 0x00,
|
|
66
|
-
data: bytes
|
|
67
|
-
}));
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
Iso7816.prototype.getResponse = function (length) {
|
|
71
|
-
//console.log(`Iso7816.getResponse, length='${length}'`);
|
|
72
|
-
return this.issueCommand((0, _commandApdu2.default)({
|
|
73
|
-
cla: 0x00,
|
|
74
|
-
ins: ins.GET_RESPONSE,
|
|
75
|
-
p1: 0x00,
|
|
76
|
-
p2: 0x00,
|
|
77
|
-
le: length
|
|
78
|
-
}));
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
Iso7816.prototype.readRecord = function (sfi, record) {
|
|
82
|
-
//console.log(`Iso7816.readRecord, sfi='${sfi}', record=${record}`);
|
|
83
|
-
return this.issueCommand((0, _commandApdu2.default)({
|
|
84
|
-
cla: 0x00,
|
|
85
|
-
ins: ins.READ_RECORD,
|
|
86
|
-
p1: record,
|
|
87
|
-
p2: (sfi << 3) + 4,
|
|
88
|
-
le: 0
|
|
89
|
-
}));
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
function create(devices, cardReader) {
|
|
93
|
-
return new Iso7816(devices, cardReader);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
module.exports = create;
|
package/lib/response-apdu.js
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var statusCodes = {
|
|
4
|
-
'^9000$': 'Normal processing',
|
|
5
|
-
'^61(.{2})$': 'Normal processing, (sw2 indicates the number of response bytes still available)',
|
|
6
|
-
'^62(.{2})$': 'Warning processing',
|
|
7
|
-
'^6200$': 'no info',
|
|
8
|
-
'^6281$': 'Part of return data may be corrupted',
|
|
9
|
-
'^6282$': 'end of file/record reached before reading le bytes',
|
|
10
|
-
'^6283$': 'ret data may contain structural info',
|
|
11
|
-
'^6284$': 'selected file is invalidated',
|
|
12
|
-
'^6285$': 'file control info not in required format',
|
|
13
|
-
'^6286$': 'unsuccessful writing',
|
|
14
|
-
'^63(.{2})$': 'Warning processing',
|
|
15
|
-
'^6300$': 'no info',
|
|
16
|
-
'^6381$': 'last write filled up file',
|
|
17
|
-
'^6382$': 'execution successful after retry',
|
|
18
|
-
// c0 least significant nibble is a counter....
|
|
19
|
-
// .. ..valued from 0 to 15
|
|
20
|
-
// cf
|
|
21
|
-
'^64(.{2})$': 'Execution error',
|
|
22
|
-
'^65(.{2})$': 'Execution error',
|
|
23
|
-
'^6500$': 'no info',
|
|
24
|
-
'^6581$': 'memory failure',
|
|
25
|
-
'^66(.{2})$': 'Reserved for future use',
|
|
26
|
-
'^6700$': 'Wrong length',
|
|
27
|
-
'^68(.{2})$': 'Checking error: functions in CLA not supported (see sw2)',
|
|
28
|
-
'^6800$': 'no info',
|
|
29
|
-
'^6881$': 'logical channel not supported',
|
|
30
|
-
'^6882$': 'secure messaging not supported',
|
|
31
|
-
'^69(.{2})$': 'Checking error: command not allowed (see sw2)',
|
|
32
|
-
'^6a(.{2})$': 'Checking error: wrong parameters (p1 or p2) (see sw2)',
|
|
33
|
-
'^6b(.{2})$': 'Checking error: wrong parameters',
|
|
34
|
-
'^6c(.{2})$': 'Checking error: wrong length (sw2 indicates correct length for le)',
|
|
35
|
-
'^6d(.{2})$': 'Checking error: wrong ins',
|
|
36
|
-
'^6e(.{2})$': 'Checking error: class not supported',
|
|
37
|
-
'^6f(.{2})$': 'Checking error: no precise diagnosis'
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
function ResponseApdu(buffer) {
|
|
41
|
-
this.buffer = buffer;
|
|
42
|
-
this.data = buffer.toString('hex');
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
ResponseApdu.prototype.getStatus = function () {
|
|
46
|
-
var statusCode = this.getStatusCode();
|
|
47
|
-
var meaning = 'Unknown';
|
|
48
|
-
for (var prop in statusCodes) {
|
|
49
|
-
if (statusCodes.hasOwnProperty(prop)) {
|
|
50
|
-
var result = statusCodes[prop];
|
|
51
|
-
if (statusCode.match(prop)) {
|
|
52
|
-
meaning = result;
|
|
53
|
-
break;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return {
|
|
58
|
-
code: statusCode,
|
|
59
|
-
meaning: meaning
|
|
60
|
-
};
|
|
61
|
-
};
|
|
62
|
-
ResponseApdu.prototype.getStatusCode = function () {
|
|
63
|
-
return this.data.substr(-4);
|
|
64
|
-
};
|
|
65
|
-
ResponseApdu.prototype.isOk = function () {
|
|
66
|
-
return this.getStatusCode() === '9000';
|
|
67
|
-
};
|
|
68
|
-
ResponseApdu.prototype.buffer = function () {
|
|
69
|
-
return this.buffer;
|
|
70
|
-
};
|
|
71
|
-
ResponseApdu.prototype.hasMoreBytesAvailable = function () {
|
|
72
|
-
return this.data.substr(-4, 2) === '61';
|
|
73
|
-
};
|
|
74
|
-
ResponseApdu.prototype.numberOfBytesAvailable = function () {
|
|
75
|
-
var hexLength = this.data.substr(-2, 2);
|
|
76
|
-
return parseInt(hexLength, 16);
|
|
77
|
-
};
|
|
78
|
-
ResponseApdu.prototype.isWrongLength = function () {
|
|
79
|
-
return this.data.substr(-4, 2) === '6c';
|
|
80
|
-
};
|
|
81
|
-
ResponseApdu.prototype.correctLength = function () {
|
|
82
|
-
var hexLength = this.data.substr(-2, 2);
|
|
83
|
-
return parseInt(hexLength, 16);
|
|
84
|
-
};
|
|
85
|
-
ResponseApdu.prototype.toString = function () {
|
|
86
|
-
return this.data;
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
function create(buffer) {
|
|
90
|
-
return new ResponseApdu(buffer);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
module.exports = create;
|