Repository navigation
Expand file tree
/
Copy pathEMVSession.java
More file actions
1534 lines (1229 loc) · 67.2 KB
/
Copy pathEMVSession.java
File metadata and controls
1534 lines (1229 loc) · 67.2 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2010 sasc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sasc.emv;
import sasc.smartcard.common.SessionProcessingEnv;
import sasc.smartcard.common.SmartCard;
import sasc.util.Log;
import sasc.iso7816.SmartCardException;
import sasc.iso7816.BERTLV;
import sasc.iso7816.AID;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.security.auth.callback.PasswordCallback;
import sasc.smartcard.common.CardScanner;
import sasc.iso7816.ShortFileIdentifier;
import sasc.iso7816.TLVException;
import sasc.iso7816.TLVUtil;
import sasc.terminal.CardResponse;
import sasc.terminal.TerminalException;
import sasc.terminal.CardConnection;
import sasc.util.Util;
/**
* Holds EMV session related information
*
* @author sasc
*/
public class EMVSession {
private SmartCard card = null;
private CardConnection terminal;
private boolean contextInitialized = false;
public static EMVSession startSession(SmartCard card, CardConnection terminal) {
if (card == null || terminal == null) {
throw new IllegalArgumentException("Arguments cannot be null");
}
return new EMVSession(card, terminal);
}
private EMVSession(SmartCard card, CardConnection terminal) {
this.card = card;
this.terminal = terminal;
}
public SmartCard getCard() {
return card;
}
/**
* Initializes the card by reading all Global data and FCI/DDF
* "1PAY.SYS.DDF01" (and some other data outside of the EMV spec)
*/
public void initContext() throws TerminalException {
if (contextInitialized) {
throw new SmartCardException("EMV context already initalized.");
}
byte[] command;
int SW1;
int SW2;
/*
* The terminal has a list containing the EMVApplication Identifier
* (AID) of every EMV application that it is configured to support, and
* the terminal must generate a candidate list of applications that are
* supported by both the terminal and card. The terminal may attempt to
* obtain a directory listing of all card applications from the card's
* PSE. If this is not supported or fails to find a match, the terminal
* must iterate through its list asking the card whether it supports
* each individual AID.
*
* If there are multiple applications in the completed candidate list,
* or the application requires it, then the cardholder will be asked to
* choose an application; otherwise it may be automatically selected
*/
//First, try the "Payment System Directory selection method".
//if that fails, try direct selection by using a terminal resident list of supported AIDs
Log.commandHeader("SELECT FILE 1PAY.SYS.DDF01 to get the PSE directory");
command = EMVAPDUCommands.selectPSE();
CardResponse selectPSEdirResponse = EMVUtil.sendCmd(terminal, command);
//Example result from the command above:
//6f 20 //FCI Template
// 84 0e //DF Name
// 31 50 41 59 2e 53 59 53 2e 44 44 46 30 31
// a5 0e //FCI Proprietary Template
// 88 01 //SFI of the Directory Elementary File
// 02
// 5f 2d 04 //Language Preference
// 6e 6f 65 6e
// 9f 11 01 //Issuer Code Table Index
// 01 (=ISO 8859-1)
SW1 = (byte) selectPSEdirResponse.getSW1();
SW2 = (byte) selectPSEdirResponse.getSW2();
if (SW1 == (byte) 0x90 && SW2 == (byte) 0x00) {
//PSE is available
try{
DDF pseDDF = EMVUtil.parseFCIDDF(selectPSEdirResponse.getData(), card);
card.setType(SmartCard.Type.CONTACTED);
getCard().setPSE(pseDDF);
if(pseDDF.getSFI() != null) {
readPSERecords(pseDDF.getSFI());
}
}catch(TLVException tlvex){
Log.debug(Util.getStackTrace(tlvex));
}
}
// Some cards have both PSE and PPSE, where the PSE might not list any SFI or applications
if (card.getEmvApplications().isEmpty()) {
//try to select the PPSE (Proximity Payment System Environment) 2PAY.SYS.DDF01
Log.commandHeader("SELECT FILE 2PAY.SYS.DDF01 to get the PPSE directory");
command = EMVAPDUCommands.selectPPSE();
CardResponse selectPPSEdirResponse = EMVUtil.sendCmd(terminal, command);
SW1 = (byte) selectPPSEdirResponse.getSW1();
SW2 = (byte) selectPPSEdirResponse.getSW2();
if (SW1 == (byte) 0x90 && SW2 == (byte) 0x00) {
//PPSE is available
try{
DDF ppseDDF = EMVUtil.parseFCIDDF(selectPPSEdirResponse.getData(), card);
card.setType(SmartCard.Type.CONTACTLESS);
getCard().setPSE(ppseDDF);
if (ppseDDF.getSFI() != null) {
readPSERecords(ppseDDF.getSFI());
}
}catch(TLVException tlvex){
Log.debug(Util.getStackTrace(tlvex));
}
}
}
if (card.getEmvApplications().isEmpty()) { //No PSE/PPSE found, or no apps listed
//An ICC need not contain PSE, or the PSE might not contain any applications.
//Direct selection of EMVApplication might be used on some cards.
//PSE alternatives (EMV book 1 page 161)
if(!card.allKnownAidsProbed()){
Log.info("No PSE found. Using direct selection by AID to generate candidate list");
//TODO only probe for known EMV AIDs
SessionProcessingEnv sessionEnv = new SessionProcessingEnv();
sessionEnv.setProbeAllKnownAIDs(true);
CardScanner scanner = new CardScanner(getCard(), terminal, sessionEnv);
scanner.probeAllKnownAIDs();
}
}
contextInitialized = true;
}
private void readPSERecords(ShortFileIdentifier shortFileIdentifier) throws TerminalException {
byte[] command;
byte SW1;
byte SW2;
try {
int sfi = shortFileIdentifier.getValue();
byte recordNum = 1;
do {
Log.commandHeader("Send READ RECORD to read all records in SFI " + sfi);
command = EMVAPDUCommands.readRecord((int) recordNum, sfi);
CardResponse readRecordResponse = EMVUtil.sendCmd(terminal, command);
//Example Response from the command above:
//70 23
// 61 21
// 4f 07 //AID
// a0 00 00 00 03 10 10
// 50 04 //Application Label
// 56 49 53 41 (=VISA)
// 9f 12 0c //Application Preferred Name
// 56 49 53 41 20 43 6c 61 73 73 69 63 (=VISA Classic)
// 87 01 //Application priority indicator
// 02
SW1 = (byte) readRecordResponse.getSW1();
SW2 = (byte) readRecordResponse.getSW2();
if (SW1 == (byte) 0x90 && SW2 == (byte) 0x00) {
EMVUtil.parsePSERecord(readRecordResponse.getData(), card);
}
recordNum++;
} while (SW1 == (byte) 0x90 && SW2 == (byte) 0x00); //while SW1SW2 != 6a83
} catch(TLVException tlvex) {
Log.debug(Util.getStackTrace(tlvex));
}
}
public void selectApplication(EMVApplication app) throws TerminalException {
if (app == null) {
throw new IllegalArgumentException("Parameter 'app' cannot be null");
}
if (!contextInitialized) {
throw new SmartCardException("Card not initialized. Call initCard() first");
}
EMVApplication currentSelectedApp = card.getSelectedApplication();
if (currentSelectedApp != null && app.getAID().equals(currentSelectedApp.getAID())) {
throw new SmartCardException("Application already selected. AID: " + app.getAID());
}
AID aid = app.getAID();
byte[] command;
Log.commandHeader("Select application by AID");
command = EMVAPDUCommands.selectByDFName(aid.getAIDBytes());
CardResponse selectAppResponse = EMVUtil.sendCmd(terminal, command);
if (selectAppResponse.getSW() == SW.SELECTED_FILE_INVALIDATED.getSW()) {
//App blocked
Log.info("Application BLOCKED");
//TODO abort execution if app blocked?
//throw new SmartCardException("EMVApplication " + Util.byteArrayToHexString(aid.getAIDBytes()) + " blocked");
}
EMVUtil.parseFCIADF(selectAppResponse.getData(), app);
//Example Response from previous command:
// 6f 37
// 84 07 //AID
// a0 00 00 00 03 10 10
// a5 2c
// 50 04 //Application Label (= VISA)
// 56 49 53 41
// 87 01 //Application priority indicator
// 02
// 9f 38 06 //PDOL
// 9f 1a
// 02
// 5f 2a
// 02
// 5f 2d 04 //Language Preference
// 6e 6f 65 6e (= no en)
// 9f 11 01 //Issuer code table index
// 01
// 9f 12 0c //Application Preferred name
// 56 49 53 41 20 43 6c 61 73 73 69 63 (= VISA Classic)
//The card supplies the PDOL (if present) to the terminal as part of the FCI
//provided in response to the SELECT FILE (Application Definition File) command
//If PDOL present, the ICC requires parameters from the Terminal.
//In this specific example:
//9F1A = Indicates the country code of the terminal, represented according to ISO 3166
//578 Norway = 0x0578
//5F2A = Transaction Currency Code: Indicates the currency code of the transaction according to ISO 4217 (numeric 3)
//NORWAY Norwegian Krone NOK 578 == 0x0578
//PDOL response data (used in the GET PROCESSING OPTS command) = 05 78 05 78
getCard().setSelectedApplication(app);
}
public void initiateApplicationProcessing() throws TerminalException {
EMVApplication app = card.getSelectedApplication();
if (app == null) {
throw new SmartCardException("No application selected. Call selectApplication(Application) first");
}
if (app.isInitializedOnICC()) {
throw new SmartCardException("Application already initialized for processing. AID=" + app.getAID());
}
// The terminal shall set all bits in the Transaction Status Information (TSI)
// and the Terminal Verification Results (TVR) to 0
EMVTerminal.resetTVR();
app.getTransactionStatusInformation().reset();
byte[] command;
int SW1;
int SW2;
Log.commandHeader("Send GET PROCESSING OPTIONS command");
//If the PDOL does not exist, the GET PROCESSING OPTIONS command uses a command data field of '8300',
//indicating that the length of the value field in the command data is zero.
DOL pdol = app.getPDOL();
command = EMVAPDUCommands.getProcessingOpts(pdol, app);
CardResponse getProcessingOptsResponse = EMVUtil.sendCmd(terminal, command);
SW1 = (byte) getProcessingOptsResponse.getSW1();
SW2 = (byte) getProcessingOptsResponse.getSW2();
//After receiving the GET PROCESSING OPTIONS C-APDU, the card application checks whether the flow conditions
//needed to process this command are fulfilled.
//-First, it checks that there is currently an application selected in the card
//-Second, the card checks that this is the first time in the current card session that the terminal issues
// the GET PROCESSING OPTIONS command
//
//If any of these conditions are not respected, the card responds with SW1SW2=6985 ("Command not allowed; conditions of use not satisfied")
if (getProcessingOptsResponse.getSW() == SW.COMMAND_NOT_ALLOWED_CONDITIONS_OF_USE_NOT_SATISFIED.getSW()){
//SW = '6985' (Conditions of use not satisfied), indicates that the transaction cannot be performed with this application.
//The terminal shall eliminate the current application from consideration and return to the Application Selection function to select another application.
Log.info("Application did not accept the PDOL returned by the terminal!");
}else if (SW1 == (byte) 0x90 && SW2 == (byte) 0x00) {
//The format of the response message is given in EMV 4.2 book 3, section 6.5.8.
EMVUtil.parseProcessingOpts(getProcessingOptsResponse.getData(), app);
if(app.getApplicationInterchangeProfile() == null || app.getApplicationFileLocator() == null){
throw new SmartCardException("GPO response did not contain AIP and AFL");
}
app.setInitializedOnICC();
//read all the records indicated in the AFL
for (ApplicationElementaryFile aef : app.getApplicationFileLocator().getApplicationElementaryFiles()) {
int startRecordNumber = aef.getStartRecordNumber();
int endRecordNumber = aef.getEndRecordNumber();
for (int recordNum = startRecordNumber; recordNum <= endRecordNumber; recordNum++) {
Log.commandHeader("Send READ RECORD to read SFI " + aef.getSFI().getValue() + " record " + recordNum);
command = EMVAPDUCommands.readRecord(recordNum, aef.getSFI().getValue());
CardResponse readAppDataResponse = EMVUtil.sendCmd(terminal, command);
SW1 = (byte) readAppDataResponse.getSW1();
SW2 = (byte) readAppDataResponse.getSW2();
if (SW1 == (byte) 0x90 && SW2 == (byte) 0x00) {
EMVUtil.parseAppRecord(readAppDataResponse.getData(), app);
boolean isInvolvedInOfflineDataAuthentication = (recordNum - startRecordNumber + 1) <= aef.getNumRecordsInvolvedInOfflineDataAuthentication();
Record record = new Record(readAppDataResponse.getData(), recordNum, isInvolvedInOfflineDataAuthentication);
aef.setRecord(recordNum, record);
} else {
//Any SW1 SW2 other than '9000' passed to the application layer as a result
//of reading any record shall cause the transaction to be terminated [spec]
throw new SmartCardException("Reading application data failed for SFI " + aef.getSFI().getValue() + " Record Number: " + recordNum);
}
}
}
app.setAllAppRecordsInAFLRead();
//TODO
//When any mandatory data object is missing, the terminal terminates the transaction.
//When an optional data object that is required because of the existence of other data
//objects or that is required to support functions that must be performed due to the
//setting of bits in the Application Interchange Profile is missing,
//the terminal shall set the "ICC data missing" indicator in the Terminal Verification Results (TVR) to 1
}
}
private enum State {
SELECTED, APPLICATION_PROCESSING_INITIATED, TRANSACTION_PROCESSING_PREPARED, GENERATE_AC_PERFORMED, TRANSACTION_POST_PROCESSING_PERFORMED
}
private void verifyProcessingStateMinimum(EMVApplication app, State state) {
}
private void verifyProcessingStateExact(EMVApplication app, State state) {
if (app == null) {
throw new SmartCardException("No application selected. Call selectApplication(Application) and initializeApplicationProcessing() first");
}
}
//TODO
//figure 6 transaction flow example
public void prepareTransactionProcessing() throws TerminalException {
EMVApplication app = card.getSelectedApplication();
verifyAppInitialized(app);
verifyAllAppRecordsInAFLRead(app);
//verifyGenerateAC_NOT_performed(app);
//TODO
verifyProcessingStateExact(app, State.APPLICATION_PROCESSING_INITIATED);
//10.3 Offline Data Authentication
performOfflineDataAuthentication(app);
//10.4 Processing restrictions
processApplicationRestrictions(app);
//10.5 Cardholder Verification (offline/online PIN)
if(app.getApplicationInterchangeProfile().isCardholderVerificationSupported()) {
performCardholderVerification(app);
} else {
//TODO Set CVM Results to "3F0000" - "No CVM performed"
}
//10.6 Terminal Risk Management
// 10.6.1 Floor Limits
// 10.6.2 Random Transaction Selection
// 10.6.3 Velocity Checking
//To better control local risk management, terminals may perform terminal
//risk management even when the "Terminal risk management is to be performed"
//bit in the Application Interchange Profile is set to 0
if(app.getApplicationInterchangeProfile().isTerminalRiskManagementToBePerformed() || EMVTerminal.getPerformTerminalRiskManagement()) {
performTerminalRiskManagement(app);
}
//10.7 Terminal Action Analysis
performTerminalActionAnalysis(app);
//TODO section 10.8
//Next: perform transaction
}
private void performOfflineDataAuthentication(EMVApplication app) throws TerminalException {
//10.3 Offline Data Authentication
//The terminal shall perform offline data authentication in any order
//after Read Application Data but before completion of the terminal action analysis
//Availability of data in the ICC to support offline data authentication is optional;
//its presence is indicated in the Application Interchange Profile.
//If both the terminal and the ICC support offline data authentication,
//the terminal shall perform this function.
//Depending on the capabilities of the card and the terminal, SDA or DDA or CDA is performed
//SDA authenticates static data put into the card by the issuer.
//DDA and CDA authenticate ICC-resident data, data from the terminal, and the card itself.
//TODO verify for TLV encoded records:
//If the records read for offline data authentication are not TLV-coded
//with tag equal to '70' then offline data authentication shall be considered
//to have been performed and to have failed; that is, the terminal shall
//set the "Offline data authentication was performed" bit in the TSI to 1,
//and shall set the appropriate "SDA failed" or "DDA failed" or "CDA failed" bit in the TVR.
//Building of the input list for offline data authentication is considered
//the first step in the offline data authentication process.
//If the input cannot be built because of a violation of one of the above
//rules but offline data authentication should be performed according to the
//"Conditions of Execution" above, offline data authentication shall be
//considered to have been performed and to have failed; that is,
//the terminal shall set the "Offline data authentication was performed"
//bit in the TSI to 1 and shall set the appropriate "SDA failed" or
//"DDA failed" or "CDA failed" bit in the TVR.
if(app.getApplicationInterchangeProfile().isCDASupported() && EMVTerminal.isCDASupported(app)) {
//TODO we (the terminal) do not support CDA yet
//Note:
//Although the terminal shall commence performing CDA before completion
//of Terminal Action Analysis, the terminal will not normally finish
//performing CDA until after it has received the response to the GENERATE AC command.
//(This is a necessary consequence of the design of CDA.)
processCDA(app);
} else if(app.getApplicationInterchangeProfile().isDDASupported() && EMVTerminal.isDDASupported(app)) {
internalAuthenticate(app);
app.getTransactionStatusInformation().setOfflineDataAuthenticationWasPerformed(true);
} else if(app.getApplicationInterchangeProfile().isSDASupported() && EMVTerminal.isSDASupported(app)) {
if(app.getSignedStaticApplicationData() == null || !app.getSignedStaticApplicationData().validate()){
EMVTerminal.getTerminalVerificationResults().setDDAFailed(true);
}
app.getTransactionStatusInformation().setOfflineDataAuthenticationWasPerformed(true);
} else {
//If neither SDA nor DDA nor CDA is performed, the terminal shall set the
//"Offline data authentication was not performed" bit in the TVR to 1.
EMVTerminal.getTerminalVerificationResults().setOfflineDataAuthenticationWasNotPerformed(true);
}
}
private void processCDA(EMVApplication app) throws TerminalException {
//Check if records read for offline data authentication are TLV-coded
//TODO
}
/**
* 10.4 Processing restrictions
*
* May be performed at any time after Read Application Data and prior to completion
* of the terminal action analysis
*
* The purpose of the Processing Restrictions function is to determine the degree
* of compatibility of the application in the terminal with the application in the
* ICC and to make any necessary adjustments, including possible rejection of the transaction.
*
* The Processing Restrictions function comprises the following compatibility checks:
* - Application Version Number
* - Application Usage Control
* - Application Effective/Expiration Dates Checking
*
* The terminal shall always execute this function
*
* @param app the application to process
* @throws TerminalException
*/
private void processApplicationRestrictions(EMVApplication app) throws TerminalException {
//10.4.1 Application Version Number
//The application within both the terminal and the ICC shall maintain an
//Application Version Number assigned by the payment system.
//The terminal shall use the version number in the ICC to ensure compatibility.
//If the Application Version Number is not present in the ICC,
//the terminal shall presume the terminal and ICC application versions are compatible,
//and transaction processing shall continue.
//If the Application Version Number is present in the ICC, it shall be compared
//to the Application Version Number maintained in the terminal.
//If they are different, the terminal shall set the
//"ICC and terminal have different application versions" bit in the TVR to 1.
if(app.getApplicationVersionNumber() != -1
&& EMVTerminal.getSupportedApplicationVersionNumber(app) != app.getApplicationVersionNumber()) {
EMVTerminal.getTerminalVerificationResults().setICCAndTerminalHaveDifferentApplicationVersions(true);
}
//10.4.2 Application Usage Control
//The Application Usage Control indicates restrictions limiting the
//application geographically or to certain types of transactions.
//If this data object is present, the terminal shall make the following checks:
//- If the transaction is being conducted at an ATM, the "Valid at ATMs"
// bit must be on in Application Usage Control.
//- If the transaction is not being conducted at an ATM, the
// "Valid at terminals other than ATMs" bit must be on in Application Usage Control.
//
//If the Application Usage Control and Issuer Country Code are both present
//in the ICC, the terminal shall make the checks described in Table 32
if(app.getApplicationUsageControl() != null) {
if(EMVTerminal.isATM() && !app.getApplicationUsageControl().validAtATMs()
|| !EMVTerminal.isATM() && !app.getApplicationUsageControl().validAtTerminalsOtherThanATMs()){
EMVTerminal.getTerminalVerificationResults().setRequestedServiceNotAllowedForCardProduct(true);
}
if(app.getIssuerCountryCode() != -1) {
int issuerCountryCode = app.getIssuerCountryCode();
//TODO table 32
}
//If any of the above tests fail, the terminal shall set the
//"Requested service not allowed for card product" bit in the TVR to 1.
}
//10.4.3 Application Effective/Expiration Dates Checking
//If the Application Effective Date is present in the ICC, the terminal
//shall check that the current date is greater than or equal to the
//Application Effective Date. If it is not, the terminal shall set the
//"Application not yet effective" bit in the TVR to 1.
//The terminal shall check that the current date is less than or equal
//to the Application Expiration Date. If it is not, the terminal shall
//set the "Expired application" bit in the TVR to 1.
Date currentDate = EMVTerminal.getCurrentDate();
Date effectiveDate = app.getEffectiveDate();
if(effectiveDate != null && currentDate.before(effectiveDate)) {
EMVTerminal.getTerminalVerificationResults().setApplicationNotYetEffective(true);
}
Date expirationDate = app.getExpirationDate();
if(expirationDate != null && currentDate.after(expirationDate)) {
EMVTerminal.getTerminalVerificationResults().setExpiredApplication(true);
}
}
/**
* May be performed any time after Read Application Data and before completion of the terminal action analysis
*
* Card holder verification is performed to ensure that the person presenting
* the ICC is the person to whom the application in the card was issued.
*
* @param app
* @throws TerminalException
*/
private void performCardholderVerification(EMVApplication app) throws TerminalException {
//TODO finish this
//see page 108-112
//The terminal shall use the cardholder verification related data in the
//ICC to determine whether one of the issuer-specified cardholder
//verification methods (CVMs) shall be executed.
//If the CVM List is not present in the ICC, the terminal shall terminate
//cardholder verification without setting the "Cardholder verification was performed" bit in the TSI.
//Note:
//A CVM List with no Cardholder Verification Rules is considered to be the same as a CVM List not being present.
CVMList cvmList = app.getCVMList();
if(cvmList == null || cvmList.getRules().isEmpty()) {
EMVTerminal.getTerminalVerificationResults().setICCDataMissing(true);
//TODO Set CVM Results to "3F0000" - "No CVM performed"
return;
}
//If the CVM List is present in the ICC, the terminal shall process each
//rule in the order in which it appears in the list according to the
//following specifications.
//Cardholder verification is completed when any one CVM is successfully
//performed or when the list is exhausted.
try{
for(CVRule rule : cvmList.getRules()){
//Check condition code (second byte)
//If any of the following is true:
//- the conditions expressed in the second byte of a CV Rule are not satisfied, or
//- data required by the condition (for example, the Application Currency Code
// or Amount, Authorised) is not present, or
//- the CVM Condition Code is outside the range of codes understood
// by the terminal (which might occur if the terminal application
// program is at a different version level than the ICC application),
//then the terminal shall bypass the rule and proceed to the next.
if(!EMVTerminal.isCVMConditionSatisfied(rule)){ //Checks all 3
continue;
}
//The conditions expressed in the second byte of the CV Rule are satisfied.
//The terminal next checks whether it recognises the CVM coded in the first byte of the CV Rule
if(EMVTerminal.isCVMRecognized(app, rule)) {
//determine whether the terminal supports the CVM
if(EMVTerminal.isCVMSupported(rule)){
//If the CVM is supported, the terminal shall attempt to perform it
if(rule.isPinRelated() && !EMVTerminal.getDoVerifyPinIfRequired()){
//If the terminal bypassed PIN entry at the direction of either the merchant or the cardholder:
//Terminal shall set the "PIN entry required, PIN pad present, but PIN was not entered" bit in the TVR to 1.
//The terminal shall consider this CVM unsuccessful and shall continue cardholder
//verification processing in accordance with the card's CVM List
EMVTerminal.getTerminalVerificationResults().setPinEntryRequired_PINPadPresent_ButPINWasNotEntered(true);
if(!rule.applySucceedingCVRuleIfThisCVMIsUnsuccessful()) {
EMVTerminal.getTerminalVerificationResults().setCardholderVerificationWasNotSuccessful(true);
return;
}
continue;
}
switch(rule.getRule()){
case NO_CVM_REQUIRED:
return;
case FAIL_PROCESSING:
//If the CVM just processed was "Fail CVM Processing", the terminal shall
//set the "Cardholder verification was not successful" bit in the TVR (b8 of byte 3)
//to 1 and no further CVMs shall be processed regardless of the
//setting of b7 of byte 1 in the first byte of the CV Rule
EMVTerminal.getTerminalVerificationResults().setCardholderVerificationWasNotSuccessful(true);
return;
case SIGNATURE_ON_PAPER:
if(EMVTerminal.hasSignatureOnPaper()) {
return;
}
break;
case ENCIPHERED_PIN_VERIFIED_BY_ICC_AND_SIGNATURE_ON_PAPER:
if(!EMVTerminal.hasSignatureOnPaper() && processVerifyPIN(true)) {
return;
}
break;
case PLAINTEXT_PIN_VERIFIED_BY_ICC_AND_SIGNATURE_ON_PAPER:
if(EMVTerminal.hasSignatureOnPaper() && processVerifyPIN(false)) {
return;
}
break;
case ENCIPHERED_PIN_VERIFIED_BY_ICC:
if(processVerifyPIN(true)){
return;
}
break;
case PLAINTEXT_PIN_VERIFIED_BY_ICC:
if(processVerifyPIN(false)) {
return;
}
break;
case ENCIPHERED_PIN_VERIFIED_ONLINE:
if(EMVTerminal.verifyEncipheredPinOnline()) {
EMVTerminal.getTerminalVerificationResults().setOnlinePINEntered(true);
return;
}
break;
case RESERVED_FOR_USE_BY_THE_INDIVIDUAL_PAYMENT_SYSTEMS:
//TODO
case RESERVED_FOR_USE_BY_THE_ISSUER:
//TODO
case NOT_AVAILABLE_FOR_USE:
//TODO fail
}
} else {
if(rule.isPinRelated()){
//In case the CVM was PIN-related, then in addition the terminal shall set the
//"PIN entry required and PIN pad not present or not working" bit (b5 of byte 3) of the TVR to 1
EMVTerminal.getTerminalVerificationResults().setPinEntryRequiredAndPINPadNotPresentOrNotWorking(true);
}
}
} else {
//If the CVM is not recognised, the terminal shall set the "Unrecognised CVM"
//bit in the TVR (b7 of byte 3) to 1 and processing continues at step 2.
EMVTerminal.getTerminalVerificationResults().setUnrecognisedCVM(true);
}
//Step 2
//The CVM was not recognised, was not supported, or failed.
//Check if we should try next rule
if(!rule.applySucceedingCVRuleIfThisCVMIsUnsuccessful()){
EMVTerminal.getTerminalVerificationResults().setCardholderVerificationWasNotSuccessful(true);
return;
}
}
} finally {
//When cardholder verification is completed, the terminal shall:
//- set the CVM Results according to Book 4 section 6.3.4.5 (TODO)
//- set the Cardholder verification was performed" bit in the TSI to 1.
app.getTransactionStatusInformation().setCardholderVerificationWasPerformed(true);
}
//All cv rules have been processed and failed
EMVTerminal.getTerminalVerificationResults().setCardholderVerificationWasNotSuccessful(true);
}
public void performTransaction() throws TerminalException {
EMVApplication app = card.getSelectedApplication();
//verifyPrepareTransactionProcessingPerformed()
verifyProcessingStateMinimum(app, State.TRANSACTION_PROCESSING_PREPARED);
//(Generate AC)
//10.8 Card Action Analysis (After Generate AC)
}
/**
* Terminal risk management is that portion of risk management performed by
* the terminal to protect the acquirer, issuer, and system from fraud.
* It provides positive issuer authorization for high-value transactions and
* ensures that transactions initiated from ICCs go online periodically to
* protect against threats that might be undetectable in an offline environment.
*
* The result of terminal risk management is the setting of appropriate bits in the TVR.
*
* Terminal risk management may be performed at any time after Read
* Application Data but before issuing the first GENERATE AC command.
*
*/
private void performTerminalRiskManagement(EMVApplication app) throws TerminalException {
//10.6 Terminal Risk Management
// 10.6.1 Floor Limits
// 10.6.2 Random Transaction Selection
// 10.6.3 Velocity Checking
try {
checkFloorLimits(app);
checkForRandomTransactionSelection(app);
if(app.getLowerConsecutiveOfflineLimit() != -1 && app.getUpperConsecutiveOfflineLimit() != -1) {
//If both the Lower Consecutive Offline Limit (tag '9F14') and
//Upper Consecutive Offline Limit (tag '9F23') exist,
//the terminal shall perform velocity checking.
performVelocityCheck(app);
}
} finally {
//Upon completion of terminal risk management, the terminal shall set the
//"Terminal risk management was performed" bit in the TSI to 1
app.getTransactionStatusInformation().setTerminalRiskManagementWasPerformed(true);
}
}
/**
* 10.6.1 Floor Limits
* To prevent split sales, the terminal may have a transaction log of approved
* transactions stored in the terminal consisting of at least the Application PAN
* and transaction amount and possibly the Application PAN Sequence Number and Transaction Date.
*
* @param app
* @throws TerminalException
*/
private void checkFloorLimits(EMVApplication app) throws TerminalException {
//During terminal risk management floor limit checking, the terminal checks
//the transaction log (if available) to determine if there is a log entry
//with the same Application PAN, and, optionally, the same Application PAN Sequence Number.
//If there are several log entries with the same PAN, the terminal selects
//the most recent entry. The terminal adds the Amount, Authorised for the
//current transaction to the amount stored in the log for that PAN to
//determine if the sum exceeds the Terminal Floor Limit. If the sum is
//greater than or equal to the Terminal Floor Limit, the terminal shall
//set the "Transaction exceeds floor limit" bit in the TVR to 1
if(!app.isTransactionLogProcessed()) {
checkForTransactionLogRecords();
}
TransactionLog transactionLog = app.getTransactionLog();
boolean panFound = false;
if(transactionLog != null && !transactionLog.isEmpty()) {
List<TransactionLog.Record> logRecords = transactionLog.getRecords();
for(int i = logRecords.size()-1; i >= 0; i--) {
TransactionLog.Record logRecord = logRecords.get(i);
//TODO
// if(logRecord.getPAN().equals(app.getPAN()) {
// panFound = true;
// //add the Amount, Authorised for the current transaction to
// //the amount stored in the log for that PAN to determine if
// //the sum exceeds the Terminal Floor Limit.
// if(sum >= terminalFloorLimit) {
// EMVTerminal.getTerminalVerificationResults().setTransactionExceedsFloorLimit(true);
// }
//
// break;
// }
}
}
if(!panFound){
//If the terminal does not have a transaction log available or if there
//is no log entry with the same PAN, the Amount, Authorised is compared
//to the appropriate floor limit. If the amount authorised is equal to
//or greater than the floor limit, the terminal sets the
//"Transaction exceeds floor limit" bit to 1 in the TVR
//TODO
// if(transactionAmount > floorLimit) {
// EMVTerminal.getTerminalVerificationResults().setTransactionExceedsFloorLimit(true);
// }
}
}
/**
* 10.6.2 Random Transaction Selection
*
* @param app
* @throws TerminalException
*/
private void checkForRandomTransactionSelection(EMVApplication app) throws TerminalException {
//Not implemented
//If the transaction is selected through the process described in this section, the
//terminal shall set the "Transaction selected randomly for online processing" bit in
//the TVR to 1.
// if(transactionRandomlySelected) {
// EMVTerminal.getTerminalVerificationResults().setTransactionSelectedRandomlyForOnlineProcessing(true);
// }
}
/**
* 10.6.3: Velocity Checking
* The purpose of velocity checking is to allow an issuer to request that,
* after a certain number of consecutive offline transactions (the Lower Consecutive Offline Limit),
* transactions should be completed online.
* However, if the terminal is incapable of going online, transactions may
* still be completed offline until a second (Upper Consecutive Offline Limit)
* limit is reached. After the upper limit is reached, the recommendation
* of the issuer might be to reject any transaction that cannot be completed online.
* Once a transaction has been completed online with successful issuer authentication,
* the count begins anew, so that transactions may be processed offline
* until the lower limit is again reached.
*
* @param app
* @throws TerminalException
*/
private void performVelocityCheck(EMVApplication app) throws TerminalException {
//The ATC and Last Online ATC Register shall be read from the ICC using GET DATA commands.
readATCData(app);
int atc = app.getATC();
int lastOnlineAtc = app.getLastOnlineATC();
if(lastOnlineAtc == 0) {
EMVTerminal.getTerminalVerificationResults().setNewCard(true);
}
//If either of the required data objects is not returned by the ICC in response to the GET DATA command,
//or if the value of the ATC is less than or equal to the value in the Last Online ATC Register, the terminal shall:
//- Set both the "Lower consecutive offline limit exceeded" and the "Upper consecutive offline limit exceeded" bits in the TVR to 1.
//- Not set the "New card" indicator in the TVR unless the Last Online ATC Register is returned and equals zero.
//- End velocity checking for this transaction.
if(atc == -1 || lastOnlineAtc == -1 || atc <= lastOnlineAtc){
EMVTerminal.getTerminalVerificationResults().setLowerConsecutiveOfflineLimitExceeded(true);
EMVTerminal.getTerminalVerificationResults().setUpperConsecutiveOfflineLimitExceeded(true);
return;
}
//If the required data objects are available, the terminal shall compare the
//difference between the ATC and the Last Online ATC Register with the
//Lower Consecutive Offline Limit to see if the limit has been exceeded.
//If the difference is equal to the Lower Consecutive Offline Limit,
//this means that the limit has not yet been exceeded. If the limit has been exceeded,
//the terminal shall set the "Lower consecutive offline limit exceeded" bit in the TVR to 1
//and also compare the difference with the Upper Consecutive Offline Limit
//to see if the upper limit has been exceeded. If it has, the terminal shall set the
//"Upper consecutive offline limit exceeded" bit in the TVR to 1.
//The terminal shall also check the Last Online ATC Register for a zero value.
//If it is zero, the terminal shall set the "New card" bit in the TVR to 1.
int diff = atc - lastOnlineAtc;
if(diff > app.getLowerConsecutiveOfflineLimit()) {
EMVTerminal.getTerminalVerificationResults().setLowerConsecutiveOfflineLimitExceeded(true);
if(diff > app.getUpperConsecutiveOfflineLimit()) {
EMVTerminal.getTerminalVerificationResults().setUpperConsecutiveOfflineLimitExceeded(true);
}
}
}
/**
* 10.7 Terminal Action Analysis
*
* Once terminal risk management and application functions related to a normal
* offline transaction have been completed, the terminal makes the first decision
* as to whether the transaction should be approved offline, declined offline, or transmitted online.
* - If the outcome of this decision process is to proceed offline, the
* terminal issues a GENERATE AC command to ask the ICC to return a TC.
* - If the outcome of the decision is to go online, the terminal issues
* a GENERATE AC command to ask the ICC for an Authorization Request Cryptogram (ARQC).
* - If the decision is to reject the transaction, the terminal issues
* a GENERATE AC to ask for an Application Authentication Cryptogram (AAC).
*
* An offline decision made here is not final. If the terminal asks for a TC
* from the ICC, the ICC, as a result of card risk management, may return an ARQC or AAC
*
* @param app
* @throws TerminalException
*/
private void performTerminalActionAnalysis(EMVApplication app) throws TerminalException {
//TODO
}
/**
* A convenience method for reading ATC data manually
*
*/