-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPurseApplet.java
2748 lines (2478 loc) · 83 KB
/
PurseApplet.java
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 (c) 1999 GEMPLUS group. All Rights Reserved.
*------------------------------------------------------------------------------
* Project name: PACAP - cas d'étude -
*
*
* Platform : Java virtual machine
* Language : JAVA 1.1.x
* Devl tool : Symantec VisualCafe
*
* @author: Thierry Bressure
* @modified Jean Louis Lanet, Abdellah El Marouani, Ludovic Casset & Hugues Martin
* @version 2.0.1
*------------------------------------------------------------------------------
*/
package com.gemplus.pacap.purse;
// temporarily removed by Rodolphe Muller on 05/07/2000
/*
import com.gemplus.pacap.utils.AccessControl;
// removed by Rodolphe Muller on 12/07/2000
import com.gemplus.pacap.utils.AppletState;
import com.gemplus.pacap.utils.AppletStateException;
import com.gemplus.pacap.utils.Date;
import com.gemplus.pacap.utils.DateException;
*/
// temporarily removed by Rodolphe Muller on 05/07/2000
/*
import com.gemplus.pacap.utils.Heure;
import com.gemplus.pacap.utils.HeureException;
import com.gemplus.pacap.utils.PacapKey;
import com.gemplus.pacap.utils.PacapSecureMessaging;
import com.gemplus.pacap.utils.PacapSignature;
*/
import com.gemplus.pacap.utils.Decimal;
import com.gemplus.pacap.utils.DecimalException;
import javacard.framework.AID;
import javacard.framework.Applet;
import javacard.framework.APDU;
import javacard.framework.JCSystem;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.Shareable;
import javacard.framework.Util;
//code added by Nestor CATANO and Marieke HUISMAN
import javacard.framework.SystemException;
import javacard.framework.TransactionException;
import javacard.framework.OwnerPIN;
import javacard.security.CryptoException ;
// added by Rodolphe Muller on 12/07/2000
////import visa.openplatform.OPSystem;
/* Description of the purse applet */
public class PurseApplet extends Applet {
/*@
invariant ADM_INIT_ADMINISTRATIVE_MODE == 0x0A;
invariant ADM_VERIFY_ADM_PIN == 0x0C;
invariant ADM_GET_ASN == 0x0E;
invariant ADM_GET_BALANCE == 0x10;
invariant ADM_GET_CREDIT_AMOUNT_WO_AUT == 0x12;
invariant ADM_SET_CREDIT_AMOUNT_WO_AUT == 0x14;
invariant ADM_GET_CURRENCY_TABLE == 0x16;
invariant ADM_SET_CURRENCY_TABLE == 0x18;
invariant ADM_DEL_CURRENCY_TABLE == 0x1A;
invariant ADM_GET_TRANSACTION_RECORD == 0x1C;
invariant ADM_GET_MAX_TRANSACTION_COUNTER == 0x1E;
invariant ADM_SET_MAX_TRANSACTION_COUNTER == 0x20;
invariant ADM_GET_EXCHANGE_RECORD == 0x22;
invariant ADM_GET_CURRENCY == 0x24;
invariant ADM_GET_EXCHANGE_RATE == 0x26;
invariant ADM_GET_LOYALTIES_TABLE == 0x28;
invariant ADM_DEL_LOYALTIES_TABLE == 0x2A;
invariant ADM_ADD_LOYALTIES_TABLE == 0x2C;
invariant ADM_GET_MAX_BALANCE == 0x2E;
invariant ADM_SET_MAX_BALANCE == 0x30;
invariant ADM_GET_MAX_DEBIT_WO_PIN == 0x32;
invariant ADM_SET_MAX_DEBIT_WO_PIN == 0x34;
invariant ADM_GET_MAX_TRANSACTION_WO_PIN == 0x36;
invariant ADM_GET_EXPIRATION_DATE == 0x38;
invariant ADM_SET_EXPIRATION_DATE == 0x3A;
invariant ADM_SET_DEBIT_KEY == 0x3C;
invariant ADM_SET_CREDIT_KEY == 0x3E;
invariant ADM_SET_PIN_KEY == 0x40;
invariant ADM_GET_STATE == 0x48;
invariant ADM_SET_CREDIT_AUTHORIZATION_KEY == 0x4E;
invariant ADM_SET_CREDIT_SIGNATURE_KEY == 0x50;
invariant ADM_SET_DEBIT_SIGNATURE_KEY == 0x52;
invariant ADM_SET_EXCHANGE_KEY == 0x54;
invariant ADM_SET_EXCHANGE_RATE_KEY == 0x56;
invariant ADM_GET_CONTROL_ACCESS_TABLE == 0x58;
invariant ADM_SET_CONTROL_ACCESS_TABLE == 0x5A;
invariant ADM_SET_ADMINISTRATIVE_KEY == 0x5C;
invariant ADM_SET_SYSTEM_KEY == 0x5E;
invariant ADM_SET_ADMINISTRATIVE_CODE == 0x60;
invariant ADM_SET_MAX_TRANSACTION_WO_PIN == 0x62;
invariant ADM_ADD_CURRENCY_TABLE == 0x64;
invariant ADM_SET_ASN == 0x66;
invariant ADM_SET_STATE == 0x68;
invariant ADM_END_SESSION == 0x6A;
invariant APP_INIT_DEBIT == 0x02;
invariant APP_INIT_CREDIT == 0x04;
invariant APP_INIT_EXCHANGE == 0x06;
invariant APP_INIT_VERIFY_PIN == 0x08;
invariant APP_CREDIT == 0x42;
invariant APP_DEBIT == 0x44;
invariant APP_EXCHANGE_CURRENCY == 0x46;
invariant APP_GET_BALANCE == 0x4A;
invariant APP_VERIFY_PIN == 0x4C;
invariant APPLET_STATE_ERR == 0x9F01;
invariant INCOMING_PARAM_SIZE_ERR == 0x9F02;
invariant OUTGOING_DATA_SIZE_ERR == 0x9F03;
invariant ACCESS_CONDITION_ERR == 0x9F04;
invariant TRANSACTION_COUNTER_OVERFLOW == 0x9F05;
invariant NOT_CURRENT_CURRENCY == 0x9F06;
invariant BAD_VALUE_FOR_AMOUNT == 0x9F07;
invariant BALANCE_OVERFLOW == 0x9F08;
invariant CURRENCY_NOT_SUPPORTED == 0x9F09;
invariant BANK_CERTIFICATE_ERR == 0x9F10;
invariant WRONG_ADMIN_CMD_TYPE_ERR == 0x9F11;
invariant TOO_MUCH_TRANSACTION_WO_PIN == 0x9F12;
invariant LOYALTIES_TABLE_FULL == 0x9F13;
invariant SALERS_TABLE_FULL == 0x9F14;
invariant DECIMAL_OVERFLOW == 0x9F15;
invariant PRIVATE_COMMAND_APDU == 0x80;
invariant SM_COMMAND_APDU == 0x0C;
invariant SYSTEM_SESSION == Security.SYSTEM_SESSION;
invariant ADMINISTRATIVE_SESSION == Security.ADMINISTRATIVE_SESSION;
invariant purse != null ;
invariant responseData != null ;
invariant t1_4 != null ;
invariant t2_4 != null ;
invariant t3_4 != null ;
invariant t4_4 != null ;
invariant t5_8 != null ;
invariant t6_16 != null ;
invariant temp != null ;
invariant decimal1 != null ;
invariant date1 != null ;
invariant heure1 != null ;
*/
//////////////// CONSTANT ATTRIBUTES ////////////////
/************************************************************/
/* administratives and personalisation command identifiers */
/************************************************************/
public static final byte ADM_INIT_ADMINISTRATIVE_MODE = (byte)0x0A;
public static final byte ADM_VERIFY_ADM_PIN = (byte)0x0C;
public static final byte ADM_GET_ASN = (byte)0x0E;
public static final byte ADM_GET_BALANCE = (byte)0x10;
public static final byte ADM_GET_CREDIT_AMOUNT_WO_AUT = (byte)0x12;
public static final byte ADM_SET_CREDIT_AMOUNT_WO_AUT = (byte)0x14;
public static final byte ADM_GET_CURRENCY_TABLE = (byte)0x16;
public static final byte ADM_SET_CURRENCY_TABLE = (byte)0x18;
public static final byte ADM_DEL_CURRENCY_TABLE = (byte)0x1A;
public static final byte ADM_GET_TRANSACTION_RECORD = (byte)0x1C;
public static final byte ADM_GET_MAX_TRANSACTION_COUNTER = (byte)0x1E;
public static final byte ADM_SET_MAX_TRANSACTION_COUNTER = (byte)0x20;
public static final byte ADM_GET_EXCHANGE_RECORD = (byte)0x22;
public static final byte ADM_GET_CURRENCY = (byte)0x24;
public static final byte ADM_GET_EXCHANGE_RATE = (byte)0x26;
public static final byte ADM_GET_LOYALTIES_TABLE = (byte)0x28;
public static final byte ADM_DEL_LOYALTIES_TABLE = (byte)0x2A;
public static final byte ADM_ADD_LOYALTIES_TABLE = (byte)0x2C;
public static final byte ADM_GET_MAX_BALANCE = (byte)0x2E;
public static final byte ADM_SET_MAX_BALANCE = (byte)0x30;
public static final byte ADM_GET_MAX_DEBIT_WO_PIN = (byte)0x32;
public static final byte ADM_SET_MAX_DEBIT_WO_PIN = (byte)0x34;
public static final byte ADM_GET_MAX_TRANSACTION_WO_PIN = (byte)0x36;
public static final byte ADM_GET_EXPIRATION_DATE = (byte)0x38;
public static final byte ADM_SET_EXPIRATION_DATE = (byte)0x3A;
public static final byte ADM_SET_DEBIT_KEY = (byte)0x3C;
public static final byte ADM_SET_CREDIT_KEY = (byte)0x3E;
public static final byte ADM_SET_PIN_KEY = (byte)0x40;
public static final byte ADM_GET_STATE = (byte)0x48;
public static final byte ADM_SET_CREDIT_AUTHORIZATION_KEY = (byte)0x4E;
public static final byte ADM_SET_CREDIT_SIGNATURE_KEY = (byte)0x50;
public static final byte ADM_SET_DEBIT_SIGNATURE_KEY = (byte)0x52;
public static final byte ADM_SET_EXCHANGE_KEY = (byte)0x54;
public static final byte ADM_SET_EXCHANGE_RATE_KEY = (byte)0x56;
public static final byte ADM_GET_CONTROL_ACCESS_TABLE = (byte)0x58;
public static final byte ADM_SET_CONTROL_ACCESS_TABLE = (byte)0x5A;
public static final byte ADM_SET_ADMINISTRATIVE_KEY = (byte)0x5C;
public static final byte ADM_SET_SYSTEM_KEY = (byte)0x5E;
public static final byte ADM_SET_ADMINISTRATIVE_CODE = (byte)0x60;
public static final byte ADM_SET_MAX_TRANSACTION_WO_PIN = (byte)0x62;
public static final byte ADM_ADD_CURRENCY_TABLE = (byte)0x64;
public static final byte ADM_SET_ASN = (byte)0x66;
public static final byte ADM_SET_STATE = (byte)0x68;
public static final byte ADM_END_SESSION = (byte)0x6A;
// command removed by Rodolphe Muller on 12/07/2000
// public static final byte ADM_REGISTER = (byte)0x70;
/***********************************/
/* applicative command identifiers */
/***********************************/
public static final byte APP_INIT_DEBIT = (byte)0x02;
public static final byte APP_INIT_CREDIT = (byte)0x04;
public static final byte APP_INIT_EXCHANGE = (byte)0x06;
public static final byte APP_INIT_VERIFY_PIN = (byte)0x08;
public static final byte APP_CREDIT = (byte)0x42;
public static final byte APP_DEBIT = (byte)0x44;
public static final byte APP_EXCHANGE_CURRENCY = (byte)0x46;
public static final byte APP_GET_BALANCE = (byte)0x4A;
public static final byte APP_VERIFY_PIN = (byte)0x4C;
/**********************************************/
/* List of error codes returned by the applet */
/**********************************************/
/* the applet state can't be initialised or changed because its present
state is invalide */
public static final short APPLET_STATE_ERR = (short)0x9F01;
/* The size of imput data is not correct */
public static final short INCOMING_PARAM_SIZE_ERR = (short)0x9F02;
/* The size of exit data is not correct */
public static final short OUTGOING_DATA_SIZE_ERR = (short)0x9F03;
/* Error launched at the access condition verification */
public static final short ACCESS_CONDITION_ERR = (short)0x9F04;
/* Transaction counter has reached a maximum value */
public static final short TRANSACTION_COUNTER_OVERFLOW = (short)0x9F05;
/* The currency used is not the purse currency */
public static final short NOT_CURRENT_CURRENCY = (short)0x9F06;
/* Error that correspond to a bad value of the amount, for example a debit
with a amount that is nil, negative or superior to the purse balance */
public static final short BAD_VALUE_FOR_AMOUNT = (short)0x9F07;
/* Purse credit attempt causes an balance overflow */
public static final short BALANCE_OVERFLOW = (short)0x9F08;
/* Error caused by the attemp of a devise download to a currency nor supported
by the purse */
public static final short CURRENCY_NOT_SUPPORTED = (short)0x9F09;
/* The exchange rate certificate is false */
public static final short BANK_CERTIFICATE_ERR = (short)0x9F10;
/* The administrative command type is bad */
public static final short WRONG_ADMIN_CMD_TYPE_ERR = (short)0x9F11;
/* Error caused by too many transaction without authentiation */
public static final short TOO_MUCH_TRANSACTION_WO_PIN = (short)0x9F12;
/* Loyalty table is full */
public static final short LOYALTIES_TABLE_FULL = (short)0x9F13;
/* Saler table is full */
public static final short SALERS_TABLE_FULL = (short)0x9F14;
/* Decimal number is too large */
public static final short DECIMAL_OVERFLOW = (short)0x9F15;
/*****************************/
/* Constants for APDU format */
/*****************************/
public static final byte PRIVATE_COMMAND_APDU = (byte)0x80;
public static final byte SM_COMMAND_APDU = (byte)0x0C;
/************************************************************************/
/* Constants of the Purse class repeated in the PurseApplet class so that
the client coudl acess to them. The client application can't acess to
the Purse clas that reprsent the intern behaviour reprsentation of the
applet. The client appication can only acess to the PurseApplet clas.
*/
/************************************************************************/
public static final byte SYSTEM_SESSION = Security.SYSTEM_SESSION;
public static final byte ADMINISTRATIVE_SESSION = Security.ADMINISTRATIVE_SESSION;
//////////////// ATTRIBUTES ////////////////
/* Purse descriptor that have the methods defining the applet behaviour */
private /*@spec_public */ Purse purse;
/* Temporary table used to format response */
private /*@ spec_public */ byte responseData[] =
JCSystem.makeTransientByteArray((short)70, JCSystem.CLEAR_ON_RESET);
/* Temporary tables */
private /*@ spec_public */ byte t1_4[] =
JCSystem.makeTransientByteArray((short)4, JCSystem.CLEAR_ON_DESELECT);
private /*@ spec_public */ byte t2_4[] =
JCSystem.makeTransientByteArray((short)4, JCSystem.CLEAR_ON_DESELECT);
private /*@ spec_public */ byte t3_4[] =
JCSystem.makeTransientByteArray((short)4, JCSystem.CLEAR_ON_DESELECT);
private /*@ spec_public */ byte t4_4[] =
JCSystem.makeTransientByteArray((short)4, JCSystem.CLEAR_ON_DESELECT);
private /*@ spec_public */ byte t5_8[] =
JCSystem.makeTransientByteArray((short)8, JCSystem.CLEAR_ON_DESELECT);
private /*@ spec_public */ byte t6_16[] =
JCSystem.makeTransientByteArray((short)16, JCSystem.CLEAR_ON_DESELECT);
private /*@ spec_public */ byte temp[] =
JCSystem.makeTransientByteArray((short)60, JCSystem.CLEAR_ON_DESELECT);
/* Temporary objects */
private /*@ spec_public */ Decimal decimal1 = new Decimal();
private /*@ spec_public */ Date date1 = new Date();
private /*@ spec_public */ Heure heure1 = new Heure();
// apdu recu par la méthode process
// initialisée par "void process(APDU)" et utlisé
// par les "xxxxxxResp(...,byte [])"
// removed by Rodolphe Muller on 05/05/2000
// for the same reasons that the other applets; we cannot store
// APDU object.
// private APDU apdu = null;
// added by Rodolphe Muller on 05/05/2000
//private byte[] ivNull = new byte[]{0, 0, 0, 0, 0, 0, 0, 0};
/////////////// CONSTRUCTOR ////////////////
/**
* @exception AppletStateException Erreur lors de l'initialisation du
* comportement ( <code> PacapBehavior </code> )
*/
// removed by Rodolphe Muller on 12/07/2000
/*
protected PurseApplet(byte[] aid, short off, byte len)
throws AppletStateException {
super();
decimal1 = new Decimal();X
date1 = new Date();X
heure1 = new Heure();X
purse = new Purse(this, aid, off, len);
}
*/
//////////////// METHODS ///////////////
/*@
requires true ;
ensures true ;
exsures (TransactionException e)
e._reason == TransactionException.BUFFER_FULL
&& JCSystem._transactionDepth == 1;
exsures (SystemException e) e._reason == SystemException.ILLEGAL_AID;
exsures (NullPointerException) false;
exsures (ArrayIndexOutOfBoundsException) false;
*/
public static void install(byte[] bArray, short bOffset, byte bLength)
throws ISOException,
SystemException,
NullPointerException, // MH, not in original source
ArrayIndexOutOfBoundsException,// MH, not in original source
TransactionException // MH, not in original source
// instantiation and registration to the JCRE
// modified by Rodolphe Muller on 12/07/2000
/*
try {
PurseApplet instance = new PurseApplet(bArray, bOffset, bLength);
} catch(AppletStateException ase) {
ISOException.throwIt(APPLET_STATE_ERR);
}
instance.register(bArray,bOffset,bLength);
*/
{
PurseApplet instance = new PurseApplet();
instance.register();
}
/* @return the result of the selection : TRUE if sucess*/
//code modifies by Nestor CATANO
//inclusion of throws clause
/*@
also_modifies purse.securite.terminalSessionType ;
also_modifies purse.securite.accessCondition.condition ;
also_modifies purse.securite.administrativeCode.flags,
purse.securite.administrativeCode.flags[OwnerPIN.VALIDATED],
purse.securite.administrativeCode.triesLeft[0] ;
also_modifies purse.securite.authPin ;
also_ensures purse.securite.terminalSessionType == Security.NOT_INITIALIZED ;
also_ensures purse.securite.accessCondition.condition == AccessCondition.FREE ;
also_ensures purse.securite.administrativeCode.flags != null ;
also_ensures purse.securite.authPin == false ;
also_exsures (SystemException e) e._reason == SystemException.NO_TRANSIENT_SPACE;
*/
public boolean select()
throws SystemException {
// added by Rodolphe Muller
// the instantiation of a Purse object is too big for the current GemXpresso
// transaction buffer so we put it here.
if(purse == null) {
purse = new Purse(this);
}
return purse.select();
}
/* Action to do when the applet is not selecioned */
//code modified by Nestor CATANO
//inclusion of throws clause
/*@
also_modifies purse.securite.terminalSessionType ;
also_modifies purse.securite.administrativeCode.flags,
purse.securite.administrativeCode.flags[OwnerPIN.VALIDATED],
purse.securite.administrativeCode.triesLeft[0] ;
also_ensures purse.securite.terminalSessionType == Security.NOT_INITIALIZED ;
also_ensures purse.securite.administrativeCode.flags != null ;
also_exsures (SystemException e) e._reason == SystemException.NO_TRANSIENT_SPACE;
*/
public void deselect()
throws SystemException {
purse.deselect();
return;
}
/* method invoked by the JCRE so that the applet treat the APDU*/
/*@
//also_requires true ;
//also_ensures true ;
*/
public void process(APDU apdu) throws ISOException {
// removed by Rodolphe Muller on 05/05/2000
// this.apdu = apdu;
switch(apdu.getBuffer()[ISO7816.OFFSET_INS]) {
case APP_INIT_DEBIT:
appInitDebit(apdu);
break;
case APP_INIT_CREDIT:
appInitCredit(apdu);
break;
// re-added by LC 23/11/00
case APP_INIT_EXCHANGE:
appInitExchange(apdu);
break;
case APP_INIT_VERIFY_PIN:
appInitVerifyPin(apdu);
break;
case APP_VERIFY_PIN:
appVerifyPin(apdu);
break;
case APP_CREDIT:
appCredit(apdu);
break;
case APP_DEBIT:
appDebit(apdu);
break;
// re-added by LC 23/11/00
case APP_EXCHANGE_CURRENCY:
appExchangeCurrency(apdu);
break;
case APP_GET_BALANCE:
appGetBalance(apdu);
break;
case ADM_INIT_ADMINISTRATIVE_MODE:
admInitAdministrativeMode(apdu);
break;
case ADM_VERIFY_ADM_PIN:
admVerifyAdmPin(apdu);
break;
case ADM_END_SESSION:
admEndSession(apdu);
break;
case ADM_GET_ASN:
admGetAsn(apdu);
break;
case ADM_SET_ASN:
admSetAsn(apdu);
break;
case ADM_GET_BALANCE:
admGetBalance(apdu);
break;
// re-added by LC 23/11/00
case ADM_GET_CREDIT_AMOUNT_WO_AUT:
admGetCreditAmountWOAut(apdu);
break;
// re-added by LC 23/11/00
case ADM_SET_CREDIT_AMOUNT_WO_AUT:
admSetCreditAmountWOAut(apdu);
break;
// re-added by LC 23/11/00
case ADM_GET_CURRENCY_TABLE:
admGetCurrencyTable(apdu);
break;
// re-added by LC 23/11/00
case ADM_SET_CURRENCY_TABLE:
admSetCurrencyTable(apdu);
break;
// re-added by LC 23/11/00
case ADM_ADD_CURRENCY_TABLE:
admAddCurrencyTable(apdu);
break;
// re-added by LC 23/11/00
case ADM_DEL_CURRENCY_TABLE:
admDelCurrencyTable(apdu);
break;
// re-added by LC 23/11/00
case ADM_GET_TRANSACTION_RECORD:
admGetTransactionRecord(apdu);
break;
// re-added by LC 23/11/00
case ADM_GET_MAX_TRANSACTION_COUNTER:
admGetMaxTransactionCounter(apdu);
break;
// re-added by LC 23/11/00
case ADM_SET_MAX_TRANSACTION_COUNTER:
admSetMaxTransactionCounter(apdu);
break;
// re-added by LC 23/11/00
case ADM_GET_EXCHANGE_RECORD:
admGetExchangeRecord(apdu);
break;
// re-added by LC 23/11/00
case ADM_GET_CURRENCY:
admGetCurrency(apdu);
break;
// re-added by LC 23/11/00
case ADM_GET_EXCHANGE_RATE:
admGetExchangeRate(apdu);
break;
// re-added by LC 23/11/00
case ADM_GET_LOYALTIES_TABLE:
admGetLoyaltiesTable(apdu);
break;
// re-added by LC 23/11/00
case ADM_DEL_LOYALTIES_TABLE :
admDelLoyaltiesTable(apdu);
break;
// re-added by LC 23/11/00
case ADM_ADD_LOYALTIES_TABLE :
admAddLoyaltiesTable(apdu);
break;
// re-added by LC 23/11/00
case ADM_GET_MAX_BALANCE :
admGetMaxBalance(apdu);
break;
// re-added by LC 23/11/00
case ADM_SET_MAX_BALANCE :
admSetMaxBalance(apdu);
break;
// re-added by LC 23/11/00
case ADM_GET_MAX_DEBIT_WO_PIN :
admGetMaxDebitWOPIN(apdu);
break;
// re-added by LC 23/11/00
case ADM_SET_MAX_DEBIT_WO_PIN :
admSetMaxDebitWOPIN(apdu);
break;
// re-added by LC 23/11/00
case ADM_GET_MAX_TRANSACTION_WO_PIN :
admGetMaxTransactionWOPIN(apdu);
break;
// re-added by LC 23/11/00
case ADM_SET_MAX_TRANSACTION_WO_PIN :
admSetMaxTransactionWOPIN(apdu);
break;
case ADM_GET_EXPIRATION_DATE :
admGetExpirationdate(apdu);
break;
case ADM_SET_EXPIRATION_DATE:
admSetExpirationDate(apdu);
break;
case ADM_SET_DEBIT_KEY:
admSetDebitKey(apdu);
break;
case ADM_SET_CREDIT_KEY:
admSetCreditKey(apdu);
break;
case ADM_SET_PIN_KEY:
admSetPINKey(apdu);
break;
case ADM_GET_STATE:
admGetState(apdu);
break;
case ADM_SET_STATE:
admSetState(apdu);
break;
case ADM_SET_CREDIT_AUTHORIZATION_KEY :
admSetCreditAuthorizationKey(apdu);
break;
case ADM_SET_CREDIT_SIGNATURE_KEY :
admSetCreditSignatureKey(apdu);
break;
case ADM_SET_DEBIT_SIGNATURE_KEY :
admSetDebitSignatureKey(apdu);
break;
// re-added by LC 23/11/00
case ADM_SET_EXCHANGE_KEY:
admSetExchangeKey(apdu);
break;
// re-added by LC 23/11/00
case ADM_SET_EXCHANGE_RATE_KEY :
admSetExchangeRateKey(apdu);
break;
// re-added by LC 23/11/00
case ADM_GET_CONTROL_ACCESS_TABLE :
admGetControlAccessTable(apdu);
break;
// re-added by LC 23/11/00
case ADM_SET_CONTROL_ACCESS_TABLE :
admSetControlAccessTable(apdu);
break;
case ISO7816.INS_SELECT:
break;
case ADM_SET_ADMINISTRATIVE_KEY:
admSetAdministrativeKey(apdu);
break;
case ADM_SET_SYSTEM_KEY:
admSetSystemKey(apdu);
break;
case ADM_SET_ADMINISTRATIVE_CODE :
admSetAdministrativeCode(apdu);
break;
// command removed by Rodolphe Muller on 12/07/2000
/*
case ADM_REGISTER :
admRegister(apdu);
break;
*/
default:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
break;
}
}
/* called by the JCRE when an applet call an shareable object
* @param client aid of the client applet
* @param type byte that specify the shared object
* @return the shareable object*/
/*@
//modifies \nothing ;
also_ensures true ;
//exsures (Exception) false ;
*/
public Shareable getShareableInterfaceObject(AID client, byte type) {
// we do treatment in the choice of sharing or not
return purse.getShareableInterfaceObject(client, type);
}
/*************************************************************/
/* Private methods that construct data to send them to purse */
/*************************************************************/
/* initialisation of the debit session
* imput data :
* the debit amount : 4 bytes
* the currency : 1 byte
* the saler identifier : 4 bytes
* the product identifier : 2 bytes
* the terminal random : 4 bytes
* the terminal transaction counter : 4 bytes
* the terminal serial number : 4 bytes */
/*@
modifies purse.securite.State ;
modifies purse.securite.temp[*], purse.securite.SMKey ;
modifies TransactionException.systemInstance._reason,
JCSystem._transactionDepth ;
modifies purse.transactionWOPIN ;
requires true ;
ensures true ;
exsures (ISOException e ) true;
exsures (TransactionException e)
e._reason == TransactionException.BUFFER_FULL
&& JCSystem._transactionDepth == 1 ;
*/
private void appInitDebit(APDU apdu)
throws ISOException,TransactionException, AccessConditionException {
byte[] buffer = apdu.getBuffer();
// verification of the imput data size
verifyAPDU(buffer, (byte)23);
short readCount = apdu.setIncomingAndReceive();
short offset = (short)ISO7816.OFFSET_CDATA;
// Decimal exception added 14/11/00
try{
offset = decimal1.setValue(buffer, offset);
}
catch(DecimalException e){
ISOException.throwIt(DECIMAL_OVERFLOW);
}
byte monnaie = buffer[offset++];
offset += Util.arrayCopyNonAtomic(buffer, offset, t1_4, (short)0, (short)4);
short typeProd = Util.getShort(buffer, offset);
offset += 2;
offset += Util.arrayCopyNonAtomic(buffer, offset, t2_4, (short)0, (short)4);
offset += Util.arrayCopyNonAtomic(buffer, offset, t3_4, (short)0, (short)4);
offset += Util.arrayCopyNonAtomic(buffer, offset, t4_4, (short)0, (short)4);
purse.appInitDebit(decimal1, monnaie, t1_4, typeProd, t2_4, t3_4, t4_4, apdu);
}
/* initialisation of the credit session
* imput data :
*
* the debit amount : 4 bytes
* the currency : 1 byte
* the credit operator identifier : 4 bytes
* the terminal random : 4 bytes
* the terminal transaction counter : 4 bytes
* the terminal serial number : 4 bytes*/
/*@
requires true ;
ensures true ;
*/
private void appInitCredit(APDU apdu) {
byte[] buffer = apdu.getBuffer();
// verification of the imput data size
verifyAPDU(buffer, (byte)21);
short readCount = apdu.setIncomingAndReceive();
short offset = (short)ISO7816.OFFSET_CDATA;
// Decimal exception added 14/11/00
try{
offset = decimal1.setValue(buffer, offset);
}
catch(DecimalException e){
ISOException.throwIt(DECIMAL_OVERFLOW);
}
byte monnaie = buffer[offset++];
offset += Util.arrayCopyNonAtomic(buffer, offset, t1_4, (short)0, (short)4);
offset += Util.arrayCopyNonAtomic(buffer, offset, t2_4, (short)0, (short)4);
offset += Util.arrayCopyNonAtomic(buffer, offset, t3_4, (short)0, (short)4);
offset += Util.arrayCopyNonAtomic(buffer, offset, t4_4, (short)0, (short)4);
purse.appInitCredit(decimal1, monnaie, t1_4, t2_4, t3_4, t4_4, apdu);
}
/* initialisation of the exchange currency session
* imput data :
* the present purse currency : 1 byte
* the destinantion currency : 1 byte
* the change operator identifier : 4 bytes
* the terminal random : 4 bytes
* the terminal transaction counter : 4 bytes
* the terminal serial number : 4 bytes*/
// re-added by LC 23/11/00
/*@
requires true ;
ensures true ;
*/
private void appInitExchange(APDU apdu) {
byte[] buffer = apdu.getBuffer();
// verification of the imput data size
verifyAPDU(buffer, (byte)18);
short readCount = apdu.setIncomingAndReceive();
short offset = (short)ISO7816.OFFSET_CDATA;
byte monnaieCourante = buffer[offset++];
byte nouvelleMonnaie = buffer[offset++];
offset += Util.arrayCopyNonAtomic(buffer, offset, t1_4, (short)0, (short)4);
offset += Util.arrayCopyNonAtomic(buffer, offset, t2_4, (short)0, (short)4);
offset += Util.arrayCopyNonAtomic(buffer, offset, t3_4, (short)0, (short)4);
offset += Util.arrayCopyNonAtomic(buffer, offset, t4_4, (short)0, (short)4);
purse.appInitExchange(
monnaieCourante, nouvelleMonnaie, t1_4, t2_4, t3_4, t4_4, apdu
);
}
/**
* The pin code verification is secure
* La verification du pin est sécurisée.
* imput data :
* the terminal random : 4 octets
* the terminal serial number : 4 octets
*/
/*@
// not checked completely, because ESC/Java times out (> 300 s)
requires true ;
ensures true ;
*/
private void appInitVerifyPin(APDU apdu) {
byte[] buffer = apdu.getBuffer();
// verification of the imput data size
verifyAPDU(buffer, (byte)8);
short readCount = apdu.setIncomingAndReceive();
short offset = (short)ISO7816.OFFSET_CDATA;
offset += Util.arrayCopyNonAtomic(buffer, offset, t1_4, (short)0, (short)4);
offset += Util.arrayCopyNonAtomic(buffer, offset, t2_4, (short)0, (short)4);
purse.appInitVerifyPin(t1_4, t2_4, apdu);
}
/* input data :
* the terminal random number : 2 bytes*/
/*@
// not checked completely, because ESC/Java times out (> 300 s)
requires true ;
ensures true ;
*/
private void appVerifyPin(APDU apdu) {
byte[] buffer = apdu.getBuffer();
verifyAPDU(buffer, (byte)8);
// CardManager.GLOBAL_PIN_SIZE = 8
short readCount = apdu.setIncomingAndReceive();
purse.appVerifyPin(apdu);
}
/* Read the balance of the purse*/
/*@
// not checked completely, because ESC/Java times out (> 300 s)
requires true ;
ensures true ;
*/
private void appGetBalance(APDU apdu){
purse.appGetBalance(apdu);
}
/* confirmation of a debit command
* input data :
* date : 3 bytes : 1 byte for the day
* 1 byte for the month
* 1 byte for the year
* hour : 2 bytes
* signature used at the mutual authentiation : 8 bytes */
/*@
// not checked completely, because ESC/Java times out (> 300 s)
requires true ;
ensures true ;
*/
private void appDebit(APDU apdu) {
byte[] buffer = apdu.getBuffer();
verifyAPDU(buffer, (byte)13);
short readCount = apdu.setIncomingAndReceive();
short offset = (short)ISO7816.OFFSET_CDATA;
try {
byte jj = buffer[offset++];
byte mm = buffer[offset++];
byte aa = buffer[offset++];
date1.setDate(jj,mm,aa);
byte hh = buffer[offset++];
byte mn = buffer[offset++];
heure1.setHeure(hh,mn);
Util.arrayCopyNonAtomic(buffer, offset, t5_8, (short)0, (short)8);
purse.appDebit(date1, heure1, t5_8, apdu);
} catch(DateException de) {
ISOException.throwIt(ISO7816.SW_DATA_INVALID);
} catch(HeureException he) {
ISOException.throwIt(ISO7816.SW_DATA_INVALID);
}
}
/* confirmation of a credit command
* input data :
* date : 3 bytes : 1 byte for the day
* 1 byte for the month
* 1 byte for the year
* hour : 2 bytes
* bank certificate : 8 bytes
* signature used at the mutual authentiation : 8 bytes */
/*@
// not checked completely, because ESC/Java times out (> 300 s)
requires true ;
ensures true ;
*/
private void appCredit(APDU apdu){
byte [] buffer = apdu.getBuffer();
verifyAPDU(buffer, (byte)21);
short readCount = apdu.setIncomingAndReceive();
short offset = (short)ISO7816.OFFSET_CDATA;
try {
byte jj = buffer[offset++];
byte mm = buffer[offset++];
byte aa = buffer[offset++];
date1.setDate(jj, mm, aa);
byte hh = buffer[offset++];
byte mn = buffer[offset++];
heure1.setHeure(hh, mn);
offset += Util.arrayCopyNonAtomic(buffer, offset, temp, (short)0, (short)8);
offset += Util.arrayCopyNonAtomic(buffer, offset, t5_8, (short)0, (short)8);
purse.appCredit(date1, heure1, temp, t5_8, apdu);
} catch(DateException de) {
ISOException.throwIt(ISO7816.SW_DATA_INVALID);
} catch(HeureException he) {
ISOException.throwIt(ISO7816.SW_DATA_INVALID);
}
}
/* confirmation of a currency change that does after an "initExchange" method
* input data :
* date : 3 bytes : 1 byte for the day
* 1 byte for the month
* 1 byte for the year
* hour : 2 bytes
* certificate of purse provider : 23 bytes
* operation type : 1 bytes
* exchange rate (2 short of a Decimal) : 2*2 bytes
* the inverse exchange rate : 2*2 bytes
* the serial purse number : 4 bytes
* transaction counter (short) : 2 bytes
* data previous the certificate signature : 8 bytes
* signature (terminal authentiation) : 8 bytes*/
// re-added by LC 23/11/00
/*@
// not checked completely, because ESC/Java times out (> 300 s)
requires true ;
ensures true ;
*/
private void appExchangeCurrency(APDU apdu) {
byte[] buffer = apdu.getBuffer();
verifyAPDU(buffer, (byte)36);
short readCount = apdu.setIncomingAndReceive();
short offset = (short)ISO7816.OFFSET_CDATA;
try {
byte jj = buffer[offset++];
byte mm = buffer[offset++];
byte aa = buffer[offset++];
date1.setDate(jj, mm, aa);
byte hh = buffer[offset++];
byte mn = buffer[offset++];
heure1.setHeure(hh, mn);
offset += Util.arrayCopyNonAtomic(
buffer, offset, temp, (short)0, (short)23
);
offset += Util.arrayCopyNonAtomic(buffer, offset, t5_8, (short)0, (short)8);
purse.appExchangeCurrency(date1, heure1, temp, t5_8, apdu);
} catch(DateException de) {
ISOException.throwIt(ISO7816.SW_DATA_INVALID);
} catch(HeureException he) {
ISOException.throwIt(ISO7816.SW_DATA_INVALID);
}
}
/* initialisation of an administrative session
* input data :
* serial teminal number : 4 bytes
* terminal administrative session counter : 4 bytes
* terminal random : 4 bytes
* session type : 1 bytes */
/*@
requires true ;
ensures true ;
*/
private void admInitAdministrativeMode(APDU apdu) {
byte[] buffer = apdu.getBuffer();
verifyAPDU(buffer, (byte)13);
short readCount = apdu.setIncomingAndReceive();
Util.arrayCopyNonAtomic(buffer, ISO7816.OFFSET_CDATA, t1_4, (short)0, (short)4);
Util.arrayCopyNonAtomic(
buffer, (short)(ISO7816.OFFSET_CDATA + 4), t3_4,(short)0, (short)4
);
Util.arrayCopyNonAtomic(
buffer, (short)(ISO7816.OFFSET_CDATA + 8), t2_4, (short)0, (short)4
);
byte type = buffer[ISO7816.OFFSET_CDATA + 12];
try {
purse.admInitAdministrativeMode(t1_4, t3_4, t2_4, type, apdu);
} catch(PurseException pe) {
ISOException.throwIt(WRONG_ADMIN_CMD_TYPE_ERR);
}
}
/* input data :
* pin to verify : 2 bytes */
/*@
// not checked completely, because ESC/Java times out (> 300 s)
requires true ;
ensures true ;
*/
private void admVerifyAdmPin(APDU apdu) {
byte[] buffer = apdu.getBuffer();
verifyAPDU(buffer, (byte)2);
short readCount = apdu.setIncomingAndReceive();