-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtestCardApp.java
More file actions
170 lines (131 loc) · 4.4 KB
/
Copy pathtestCardApp.java
File metadata and controls
170 lines (131 loc) · 4.4 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
package cccs.on_card.id;
import javacard.framework.*;
import javacard.security.*;
// Applet AID: 0x00:0x01:0x02:0x03:0x04:0x05:0x06:0x07:0x0b:0x1
// I arbitrarily chose the general structure for my Application Identifiers( i.e AIDs )[ all are 10 bytes long ]
// All packages start with the general sequence 0x00:0x01:0x02:0x03:0x04:0x05:0x06:0x07, then individual
// packages are labelled 0x0a, 0x0b, ...( e.g, this is 0x0b, and 0x1 is this particular class's number
public class Identity extends javacard.framework.Applet
{
// define the applet's CLAss byte
final static byte CLA = ( byte )0x04;
// read-only INStruction commands
final static byte GET_STUDENT_ID = ( byte )0x01;
final static byte GET_FIRST_NAME = ( byte )0x02;
final static byte GET_LAST_NAME = ( byte )0x03;
// constants
final static short MAX_ID_SIZE = ( short )11; // *** WATCH THIS!!! **
final static short MAX_NAME_SIZE = ( short )7; // *** WATCH THIS!!! **
//final static short ALL_INFO_SIZE = MAX_ID_SIZE + ( 2 * MAX_NAME_SIZE );
// fields
byte[] studentID = null;
byte[] firstName = null;
byte[] lastName = null;
/**
* Initialize all the fields of this applet
*
* Example input:-
*
* STUDENT_ID : E27-0193/01 : 69 50 55 45 48 49 57 51 47 48 49 : ASCII
* STUDENT_ID : E27-0193/01 : 0x45 0x32 0x37 0x2d 0x30 0x31 0x39 0x33 0x2f 0x30 0x31 : HEX
* FNAME : MICHAEL : 77 73 67 72 65 69 76 0x00 0x00 0x00 : ASCII
* FNAME : MICHAEL : 0x4d 0x49 0x43 0x48 0x41 0x45 0x4c 0x00 0x00 0x00 : HEX
* LNAME : KAMUNGE : 75 65 77 85 78 71 69 0x00 0x00 0x00 : ASCII
* LNAME : KAMUNGE : 0x4b 0x41 0x4d 0x55 0x4e 0x47 0x45 0x00 0x00 0x00 : HEX
*
*
* input : 4532372d303139332f30314d49434841454c0000004b414d554e4745000000
* length : 31 bytes( 0x1F )
*
**/
private Identity( byte[] bArray, short bOffset, byte bLength )
{
studentID = new byte[ MAX_ID_SIZE ];
firstName = new byte[ MAX_NAME_SIZE ];
lastName = new byte[ MAX_NAME_SIZE ];
studentID[ 0 ] = ( byte )0x45;
studentID[ 1 ] = ( byte )0x32;
studentID[ 2 ] = ( byte )0x37;
studentID[ 3 ] = ( byte )0x2d;
studentID[ 4 ] = ( byte )0x30;
studentID[ 5 ] = ( byte )0x31;
studentID[ 6 ] = ( byte )0x39;
studentID[ 7 ] = ( byte )0x33;
studentID[ 8 ] = ( byte )0x2f;
studentID[ 9 ] = ( byte )0x30;
studentID[ 10 ] = ( byte )0x31;
firstName[ 0 ] = ( byte )0x4d;
firstName[ 1 ] = ( byte )0x49;
firstName[ 2 ] = ( byte )0x43;
firstName[ 3 ] = ( byte )0x48;
firstName[ 4 ] = ( byte )0x41;
firstName[ 5 ] = ( byte )0x45;
firstName[ 6 ] = ( byte )0x4c;
lastName[ 0 ] = ( byte )0x4b;
lastName[ 1 ] = ( byte )0x41;
lastName[ 2 ] = ( byte )0x4d;
lastName[ 3 ] = ( byte )0x55;
lastName[ 4 ] = ( byte )0x4e;
lastName[ 5 ] = ( byte )0x47;
lastName[ 6 ] = ( byte )0x45;
register();
} // constructor
/* Installs a new instance of this applet and passes the input to the applet constructor */
public static void install( byte[] bArray, short bOffset, byte bLength )
{
new Identity( bArray, bOffset, bLength );
}
/* The process method which represents all commands */
public void process( APDU apdu ) throws ISOException
{
byte[] buffer = apdu.getBuffer();
if( selectingApplet() )
{
return;
}
if( buffer[ ISO7816.OFFSET_CLA ] != CLA )
{
ISOException.throwIt( ISO7816.SW_CLA_NOT_SUPPORTED );
}
switch( buffer[ ISO7816.OFFSET_INS ] )
{
case GET_STUDENT_ID:
{
getStudentID( apdu );
return;
}
case GET_FIRST_NAME:
{
getFirstName( apdu );
return;
}
case GET_LAST_NAME:
{
getLastName( apdu );
return;
}
default:
{
ISOException.throwIt( ISO7816.SW_INS_NOT_SUPPORTED );
}
} // switch
} // process
private void getLastName( APDU apdu )
{
byte[] buffer = apdu.getBuffer();
Util.arrayCopy( lastName, ( short )0, buffer, ( short )0, MAX_NAME_SIZE );
apdu.setOutgoingAndSend( ( short )0, MAX_NAME_SIZE );
} // getLastName
private void getFirstName( APDU apdu )
{
byte[] buffer = apdu.getBuffer();
Util.arrayCopy( firstName, ( short )0, buffer, ( short )0, MAX_NAME_SIZE );
apdu.setOutgoingAndSend( ( short )0, MAX_NAME_SIZE );
} // getFirstName
private void getStudentID( APDU apdu )
{
byte[] buffer = apdu.getBuffer();
Util.arrayCopy( studentID, ( short )0, buffer, ( short )0, MAX_ID_SIZE );
apdu.setOutgoingAndSend( ( short )0, MAX_ID_SIZE );
} // getStudentID
} // class