-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathStubSmartCard.java
More file actions
361 lines (329 loc) · 9.54 KB
/
StubSmartCard.java
File metadata and controls
361 lines (329 loc) · 9.54 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* **************************************************************************************
* Copyright (c) 2018 Calypso Networks Association https://calypsonet.org/
*
* See the NOTICE file(s) distributed with this work for additional information
* regarding copyright ownership.
*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
************************************************************************************** */
package org.eclipse.keyple.plugin.stub;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
import org.eclipse.keyple.core.plugin.CardIOException;
import org.eclipse.keyple.core.util.Assert;
import org.eclipse.keyple.core.util.HexUtil;
import org.eclipse.keyple.plugin.stub.spi.ApduResponseProviderSpi;
/**
* Simulated smart card that can be inserted into a {@link StubReader}. Use the {@link Builder} to
* create this object
*
* @since 2.0.0
*/
public class StubSmartCard {
private final byte[] powerOnData;
private final String cardProtocol;
private boolean isPhysicalChannelOpen;
private final Map<String, String> hexCommands;
private final ApduResponseProviderSpi apduResponseProvider;
/**
* (private) <br>
* Create a simulated smart card with mandatory parameters The response APDU can be provided using
* <code>apduResponseProvider</code> if it is not null or <code>hexCommands</code> by default.
*
* @param powerOnData (non-nullable) power-on data of the card
* @param cardProtocol (non-nullable) card protocol
* @param hexCommands (non-nullable) set of simulated commands
* @param apduResponseProvider (nullable) an external provider of simulated commands
* @since 2.0.0
*/
private StubSmartCard(
byte[] powerOnData,
String cardProtocol,
Map<String, String> hexCommands,
ApduResponseProviderSpi apduResponseProvider) {
this.powerOnData = powerOnData;
this.cardProtocol = cardProtocol;
this.hexCommands = hexCommands;
this.apduResponseProvider = apduResponseProvider;
isPhysicalChannelOpen = false;
}
/**
* (package-private) <br>
* Gets the card protocol supported by the card
*
* @return A not empty String.
* @since 2.0.0
*/
String getCardProtocol() {
return cardProtocol;
}
/**
* (package-private) <br>
* Get the card power-on data
*
* @return Null if no power-on data are available.
* @since 2.0.0
*/
byte[] getPowerOnData() {
return powerOnData;
}
/**
* (package-private) <br>
* Get the status of the physical channel
*
* @return True if the physical channel is open
* @since 2.0.0
*/
boolean isPhysicalChannelOpen() {
return isPhysicalChannelOpen;
}
/**
* (package-private) <br>
* Open the physical channel of the card
*
* @since 2.0.0
*/
void openPhysicalChannel() {
this.isPhysicalChannelOpen = true;
}
/**
* (package-private) <br>
* Close the physical channel of the card
*
* @since 2.0.0
*/
void closePhysicalChannel() {
this.isPhysicalChannelOpen = false;
}
/**
* (package-private) <br>
* Return APDU Response to APDU Request
*
* @param apduIn commands to be processed
* @return APDU response
* @since 2.0.0
*/
byte[] processApdu(byte[] apduIn) throws CardIOException {
if (apduIn == null || apduIn.length == 0) {
return new byte[0];
}
// convert apduIn to hex
String hexApdu = HexUtil.toHex(apduIn);
if (apduResponseProvider != null) {
String responseFromRequest = apduResponseProvider.getResponseFromRequest(hexApdu);
if (responseFromRequest != null) {
return HexUtil.toByteArray(responseFromRequest);
}
} else if (hexCommands != null) {
// return matching hex response if the provided APDU matches the regex
Pattern p;
for (Map.Entry<String, String> hexCommand : hexCommands.entrySet()) {
p = Pattern.compile(hexCommand.getKey());
if (p.matcher(hexApdu).matches()) {
return HexUtil.toByteArray(hexCommand.getValue());
}
}
}
// throw a CardIOException if not found
throw new CardIOException("No response is available for request: " + hexApdu);
}
/**
* {@inheritDoc}
*
* @since 2.0.0
*/
@Override
public String toString() {
return "StubSmartCard{"
+ "powerOnData="
+ HexUtil.toHex(powerOnData)
+ ", cardProtocol='"
+ cardProtocol
+ '\''
+ ", isPhysicalChannelOpen="
+ isPhysicalChannelOpen
+ ", hexCommands(#)="
+ hexCommands.size()
+ '}';
}
/**
* Creates a builder for the {@link StubSmartCard}
*
* @return Next step of the builder
* @since 2.0.0
*/
public static PowerOnDataStep builder() {
return new Builder();
}
/**
* Builder class for {@link StubSmartCard}.
*
* @since 2.0.0
*/
public static class Builder
implements PowerOnDataStep, ProtocolStep, CommandStep, BuildStep, SimulatedCommandStep {
private byte[] powerOnData;
private String cardProtocol;
private ApduResponseProviderSpi apduResponseProvider;
private final Map<String, String> hexCommands;
private Builder() {
hexCommands = new HashMap<>();
}
/**
* {@inheritDoc}
*
* @since 2.0.0
*/
@Override
public SimulatedCommandStep withSimulatedCommand(String command, String response) {
Assert.getInstance().notNull(command, "command").notNull(response, "response");
// add commands without space
hexCommands.put(command.trim(), response.trim());
return this;
}
/**
* {@inheritDoc}
*
* @since 2.0.0
*/
@Override
public StubSmartCard build() {
return new StubSmartCard(powerOnData, cardProtocol, hexCommands, apduResponseProvider);
}
/**
* {@inheritDoc}
*
* @since 2.0.0
*/
@Override
public ProtocolStep withPowerOnData(byte[] powerOnData) {
this.powerOnData = powerOnData;
return this;
}
/**
* {@inheritDoc}
*
* @since 2.0.0
*/
@Override
public CommandStep withProtocol(String protocol) {
Assert.getInstance().notNull(protocol, "Protocol");
this.cardProtocol = protocol;
return this;
}
/**
* {@inheritDoc}
*
* @since 2.1.0
*/
@Override
public BuildStep withApduResponseProvider(ApduResponseProviderSpi apduResponseProvider) {
this.apduResponseProvider = apduResponseProvider;
return this;
}
}
/**
* Provides a method for defining simulated power-on data for the StubSmartCard to build.
*
* @since 2.0.0
*/
public interface PowerOnDataStep {
/**
* Define simulated power-on data for the {@link StubSmartCard} to build
*
* @param powerOnData (not nullable) byte of array
* @return next step of builder
* @since 2.0.0
*/
ProtocolStep withPowerOnData(byte[] powerOnData);
}
/**
* Provides a method to define the simulated protocol for the StubSmartCard to build.
*
* @since 2.0.0
*/
public interface ProtocolStep {
/**
* Define simulated protocol for the {@link StubSmartCard} to build
*
* @param protocol (not nullable) protocol name
* @return next step of builder
* @since 2.0.0
*/
CommandStep withProtocol(String protocol);
}
/**
* CommandStep interface provides methods to build and configure a StubSmartCard object.
*
* @since 2.0.0
*/
public interface CommandStep {
/**
* Add simulated command/response to the {@link StubSmartCard} to build. Command and response
* should be hexadecimal.
*
* @param command hexadecimal command to respond to (can be a regexp to match multiple apdu)
* @param response hexadecimal response
* @return next step of builder
* @since 2.0.0
*/
SimulatedCommandStep withSimulatedCommand(String command, String response);
/**
* Provide simulated command/response to the {@link StubSmartCard} using a custom provider
* implementing of {@link ApduResponseProviderSpi}.
*
* @param apduResponseProvider hexadecimal command to respond to (can be a regexp to match
* multiple apdu)
* @return next step of builder
* @since 2.1.0
*/
BuildStep withApduResponseProvider(ApduResponseProviderSpi apduResponseProvider);
/**
* Build the {@link StubSmartCard}
*
* @return new instance a StubSmartCard
*/
StubSmartCard build();
}
/**
* Provides methods to add simulated commands and responses to a StubSmartCard builder.
*
* @since 2.1.0
*/
public interface SimulatedCommandStep {
/**
* Add simulated command/response to the {@link StubSmartCard} to build. Command and response
* should be hexadecimal.
*
* @param command hexadecimal command to respond to (can be a regexp to match multiple apdu)
* @param response hexadecimal response
* @return next step of builder
* @since 2.1.0
*/
SimulatedCommandStep withSimulatedCommand(String command, String response);
/**
* Build the {@link StubSmartCard}
*
* @return new instance a StubSmartCard
*/
StubSmartCard build();
}
/**
* Build step for creating a {@link StubSmartCard} instance.
*
* @since 2.1.0
*/
public interface BuildStep {
/**
* Build the {@link StubSmartCard}
*
* @return new instance a StubSmartCard
* @since 2.1.0
*/
StubSmartCard build();
}
}