Hello all...
I've attached an application that demonstrates the aforementioned problem
with "writing" to the GPK8000 card. If someone else could run it to see if
they get the same results it would be greatly appreciated.
Below are the results that I obtained.
Thanks
-- Sean
I am using Borland JBuilder 3.0 running with the SDK 1.3, but I get the
same results if I compile it with the SDK 1.3 itself.
If you look at the code, the last block of code should return the initial
data written to the card followed by zeros to the end of the file, but it
does not. See the hex dump comparison below. Any byte followed by an
asterisk is erroneous.
Again the dump will only look correct if your window can display 110
characters wide in a fixed width font. (try copying to notepad with word
wrap disabled if it doesn't look right)
(and what is this about not being allowed to use HTML posts?? I tried to
use [B] and [TT] and it rejected my post...??)
-----------------------------------------------------------------------------------
Starting smartcard
Creating CardRequest
Waiting for card
Erase Card
Creating DF :3F00:D000
Creating 4096 byte EF :3F00:D000:D001
Verifying initial status of card...
Writing 4096 of zeros to :3F00:D000:D001....
Reading 4096 bytes of data...
Verifying zeroed status of card...
Generating data to write...
Writing 256 bytes of data to :3F00:D000:D001....
Reading 4096 bytes of data...
Verifying status of card...
Writing 3840 bytes of zeroes starting at offset 256 of file :3F00:D000:D001....
Reading 4096 bytes of data...
Verifying data:
------Read Data-------------------------------- ------Original
Data----------------------------
0000: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F --- 00 01 02 03 04
05 06 07 08 09 0A 0B 0C 0D 0E 0F
0016: 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F --- 10 11 12 13 14
15 16 17 18 19 1A 1B 1C 1D 1E 1F
0032: 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F --- 20 21 22 23 24
25 26 27 28 29 2A 2B 2C 2D 2E 2F
0048: 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F --- 30 31 32 33 34
35 36 37 38 39 3A 3B 3C 3D 3E 3F
0064: 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F --- 40 41 42 43 44
45 46 47 48 49 4A 4B 4C 4D 4E 4F
0080: 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F --- 50 51 52 53 54
55 56 57 58 59 5A 5B 5C 5D 5E 5F
0096: 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F --- 60 61 62 63 64
65 66 67 68 69 6A 6B 6C 6D 6E 6F
0112: 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F --- 70 71 72 73 74
75 76 77 78 79 7A 7B 7C 7D 7E 7F
0128: 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F --- 80 81 82 83 84
85 86 87 88 89 8A 8B 8C 8D 8E 8F
0144: 90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F --- 90 91 92 93 94
95 96 97 98 99 9A 9B 9C 9D 9E 9F
0160: A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF --- A0 A1 A2 A3 A4
A5 A6 A7 A8 A9 AA AB AC AD AE AF
0176: B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF --- B0 B1 B2 B3 B4
B5 B6 B7 B8 B9 BA BB BC BD BE BF
0192: C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF --- C0 C1 C2 C3 C4
C5 C6 C7 C8 C9 CA CB CC CD CE CF
0208: D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF --- D0 D1 D2 D3 D4
D5 D6 D7 D8 D9 DA DB DC DD DE DF
0224: E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF --- E0 E1 E2 E3 E4
E5 E6 E7 E8 E9 EA EB EC ED EE EF
0240: F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF --- F0 F1 F2 F3 F4
F5 F6 F7 F8 F9 FA FB FC FD FE FF
0256: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --- 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00
0272: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --- 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00
0288: 00 00 00 00 00 00 00 00 00 00 00 00 2C*2D*2E*2F* --- 00 00 00 00 00
00 00 00 00 00 00 00 00*00*00*00*
0304: 30*31*32*33*34*35*36*37*38*39*3A*3B*3C*3D*3E*3F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0320: 40*41*42*43*44*45*46*47*48*49*4A*4B*4C*4D*4E*4F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0336: 50*51*52*53*54*55*56*57*58*59*5A*5B*5C*5D*5E*5F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0352: 60*61*62*63*64*65*66*67*68*69*6A*6B*6C*6D*6E*6F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0368: 70*71*72*73*74*75*76*77*78*79*7A*7B*7C*7D*7E*7F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0384: 80*81*82*83*84*85*86*87*88*89*8A*8B*8C*8D*8E*8F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0400: 90*91*92*93*94*95*96*97*98*99*9A*9B*9C*9D*9E*9F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0416: A0*A1*A2*A3*A4*A5*A6*A7*A8*A9*AA*AB*AC*AD*AE*AF*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0432: B0*B1*B2*B3*B4*B5*B6*B7*B8*B9*BA*BB*BC*BD*BE*BF*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0448: C0*C1*C2*C3*C4*C5*C6*C7*C8*C9*CA*CB*CC*CD*CE*CF*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0464: D0*D1*D2*D3*D4*D5*D6*D7*D8*D9*DA*DB*DC*DD*DE*DF*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0480: E0*E1*E2*E3*E4*E5*E6*E7*E8*E9*EA*EB*EC*ED*EE*EF*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0496: F0*F1*F2*F3*F4*F5*F6*F7*F8*F9*FA*FB*FC*FD*FE*FF*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0512: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --- 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00
0528: 00 00 00 00 00 00 00 00 00 00 00 00 1C*1D*1E*1F* --- 00 00 00 00 00
00 00 00 00 00 00 00 00*00*00*00*
0544: 20*21*22*23*24*25*26*27*28*29*2A*2B*2C*2D*2E*2F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0560: 30*31*32*33*34*35*36*37*38*39*3A*3B*3C*3D*3E*3F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0576: 40*41*42*43*44*45*46*47*48*49*4A*4B*4C*4D*4E*4F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0592: 50*51*52*53*54*55*56*57*58*59*5A*5B*5C*5D*5E*5F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0608: 60*61*62*63*64*65*66*67*68*69*6A*6B*6C*6D*6E*6F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0624: 70*71*72*73*74*75*76*77*78*79*7A*7B*7C*7D*7E*7F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0640: 80*81*82*83*84*85*86*87*88*89*8A*8B*8C*8D*8E*8F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0656: 90*91*92*93*94*95*96*97*98*99*9A*9B*9C*9D*9E*9F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0672: A0*A1*A2*A3*A4*A5*A6*A7*A8*A9*AA*AB*AC*AD*AE*AF*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0688: B0*B1*B2*B3*B4*B5*B6*B7*B8*B9*BA*BB*BC*BD*BE*BF*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0704: C0*C1*C2*C3*C4*C5*C6*C7*C8*C9*CA*CB*CC*CD*CE*CF*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0720: D0*D1*D2*D3*D4*D5*D6*D7*D8*D9*DA*DB*DC*DD*DE*DF*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0736: E0*E1*E2*E3*E4*E5*E6*E7*E8*E9*EA*EB*EC*ED*EE*EF*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0752: F0*F1*F2*F3*F4*F5*F6*F7*F8*F9*FA*FB*FC*FD*FE*FF*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0768: 00 00 00 00 00 00 00 00 00 00 00 00 0C*0D*0E*0F* --- 00 00 00 00 00
00 00 00 00 00 00 00 00*00*00*00*
0784: 10*11*12*13*14*15*16*17*18*19*1A*1B*1C*1D*1E*1F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0800: 20*21*22*23*24*25*26*27*28*29*2A*2B*2C*2D*2E*2F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0816: 30*31*32*33*34*35*36*37*38*39*3A*3B*3C*3D*3E*3F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0832: 40*41*42*43*44*45*46*47*48*49*4A*4B*4C*4D*4E*4F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0848: 50*51*52*53*54*55*56*57*58*59*5A*5B*5C*5D*5E*5F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0864: 60*61*62*63*64*65*66*67*68*69*6A*6B*6C*6D*6E*6F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0880: 70*71*72*73*74*75*76*77*78*79*7A*7B*7C*7D*7E*7F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0896: 80*81*82*83*84*85*86*87*88*89*8A*8B*8C*8D*8E*8F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0912: 90*91*92*93*94*95*96*97*98*99*9A*9B*9C*9D*9E*9F*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0928: A0*A1*A2*A3*A4*A5*A6*A7*A8*A9*AA*AB*AC*AD*AE*AF*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0944: B0*B1*B2*B3*B4*B5*B6*B7*B8*B9*BA*BB*BC*BD*BE*BF*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0960: C0*C1*C2*C3*C4*C5*C6*C7*C8*C9*CA*CB*CC*CD*CE*CF*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0976: D0*D1*D2*D3*D4*D5*D6*D7*D8*D9*DA*DB*DC*DD*DE*DF*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
0992: E0*E1*E2*E3*E4*E5*E6*E7*E8*E9*EA*EB*EC*ED*EE*EF*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
1008: F0*F1*F2*F3*F4*F5*F6*F7*F8*F9*FA*FB*FC*FD*FE*FF*
--- 00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*00*
1024: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --- 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00
1040: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 --- 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00
1056: 00 00 00 00 00 00 00 00 00 00 00 00 2C*2D*2E*2F* --- 00 00 00 00 00
00 00 00 00 00 00 00 00*00*00*00*
<<-------snip -- pattern repeats------------>>
import opencard.core.service.CardRequest;
import opencard.core.service.SmartCard;
import opencard.opt.iso.fs.CardFile;
import opencard.opt.iso.fs.CardFilePath;
import opencard.opt.iso.fs.FileSystemCardService;
import opencard.opt.security.CredentialBag;
import opencard.core.util.HexString;
import com.gemplus.opencard.service.util.ArrayUtils;
import com.gemplus.opencard.service.gpk.file.GPKFileSystemService;
import com.gemplus.opencard.service.gpk.file.GPKFileUtilityService;
import com.gemplus.opencard.service.gpk.file.FileUtilityCardService;
import com.gemplus.opencard.service.gpk.security.GPKAdministrativeCredential;
import com.gemplus.opencard.service.gpk.security.GPKCredentialStore;
import com.gemplus.opencard.service.gpk.admin.AdministrativeCardService;
import com.gemplus.opencard.service.gpk.GPKObjectRegistry;
import com.gemplus.opencard.service.gpk.GPKProperties;
import com.gemplus.opencard.service.gpk.file.GPKFileInfo;
public class GetAnyCard
{
private static final CardFilePath df_3F00 = new CardFilePath(":3F00");
// Master File (root)
private static final CardFilePath ef_3F01 = new CardFilePath(":3F00:3F01");
// EFKey File
private static final CardFilePath df_D000 = new CardFilePath(":3F00:D000");
private static final CardFilePath ef_D001 = new
CardFilePath(":3F00:D000:D001");
public static final byte[] SYS_KEY = "TEST KEYTEST
KEY".getBytes();
public static void main (String [] args)
{
GPKFileSystemService fss;
GPKFileUtilityService fus;
try
{
System.out.println("\nStarting smartcard");
SmartCard.start();
// wait for a smartcard with file access support
System.out.println("Creating CardRequest");
CardRequest cr = new CardRequest();
System.out.println("Waiting for card");
SmartCard sc = SmartCard.waitForCard(cr);
// Instanciate file CardServices
fss = (GPKFileSystemService)
sc.getCardService(FileSystemCardService.class, true);
fus = (GPKFileUtilityService)
sc.getCardService(FileUtilityCardService.class, true);
int bytesToAllocate = 4096; // a large file
int bytesToFill = 256; // the rest of the bytes will
be zero
int cardFileOffset = 0; // the offset to start writing
byte[] writeData = new byte[bytesToAllocate];
byte[] info;
// Provide credentials for Administrative key :3F00:3F01
CredentialBag bag = new CredentialBag();
GPKCredentialStore store = new GPKCredentialStore();
GPKAdministrativeCredential credential = new
GPKAdministrativeCredential();
store.storeCredential(0, credential);
bag.addCredentialStore(store);
fss.provideCredentials(ef_3F01, bag);
// Erase card
System.out.println("Erase Card");
fus.eraseCard();
// Create Directory File
System.out.println("\nCreating DF " + df_D000);
fus.createDF(df_D000, false, null);
// Create Transparent Data File
System.out.println("Creating " + bytesToAllocate + " byte EF " + ef_D001);
fus.createTransparentFile(ef_D001, bytesToAllocate, null);
// Check to make sure that all of the card is currently zeros
System.out.println("Verifying initial status of card..." );
info = fss.read(ef_D001, cardFileOffset, bytesToAllocate);
for ( int i = 0 ; i < bytesToAllocate ; i++ )
if ( info[i] != 0 )
throw new Exception("Something's not write at position " + i + ",
value = " + info[i]);
System.out.println("Writing " + bytesToAllocate + " bytes of zeros to " +
ef_D001 + "...." );
fss.write(ef_D001, cardFileOffset, writeData, 0, bytesToAllocate );
System.out.println("Reading " + bytesToAllocate + " bytes of data..." );
info = fss.read(ef_D001, cardFileOffset, bytesToAllocate);
// Check to make sure that all of the card is currently zeros
System.out.println("Verifying zeroed status of card..." );
info = fss.read(ef_D001, cardFileOffset, bytesToAllocate);
for ( int i = 0 ; i < bytesToAllocate ; i++ )
if ( info[i] != 0 )
throw new Exception("Something's not write at position " + i + ",
value = " + info[i]);
// Generate data to write
System.out.println("Generating data to write...");
for ( int i = 0x00 ; i < bytesToFill ; i++ )
writeData[i] = (byte)(i & 0x00FF);
System.out.println("Writing " + bytesToFill + " bytes of data to " +
ef_D001 + "...." );
fss.write(ef_D001, cardFileOffset, writeData, 0, bytesToFill );
System.out.println("Reading " + bytesToAllocate + " bytes of data..." );
info = fss.read(ef_D001, cardFileOffset, bytesToAllocate);
// Check to make sure that all of the card is currently zeros
System.out.println("Verifying status of card..." );
info = fss.read(ef_D001, cardFileOffset, bytesToAllocate);
for ( int i = 0 ; i < bytesToAllocate ; i++ )
if ( info[i] != writeData[i] )
throw new Exception("Something's not write at position " + i + ",
value = " + info[i]);
System.out.println("Writing " + (bytesToAllocate-bytesToFill) + " bytes of
zeroes starting at offset " + bytesToFill + " of file " + ef_D001 + "...." );
fss.write(ef_D001, cardFileOffset, writeData, 0,
(bytesToAllocate-bytesToFill) );
System.out.println("Reading " + bytesToAllocate + " bytes of data..." );
info = fss.read(ef_D001, cardFileOffset, bytesToAllocate);
System.out.println("Verifying data: " );
System.out.println(" ------Read Data--------------------------------
------Original Data----------------------------");
DumpCompHex(info, writeData);
SmartCard.shutdown ();
}
catch (Exception e)
{
System.out.print("DOH!: ");
e.printStackTrace();
}
} // main
/** Auxillary string array. */
protected final static String[] hexChars = {"0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "A", "B", "C", "D", "E",
"F"};
static void DumpCompHex ( byte[] a, byte[] b )
{
if ( a.length != b.length )
System.out.println ( "Arrays are not equal in length!" );
int min = (a.length>b.length) ? b.length : a.length;
for ( int i = 0 ; i < min ; )
{
if ( i < 10 ) System.out.print("0");
if ( i < 100 ) System.out.print("0");
if ( i < 1000 ) System.out.print("0");
System.out.print ( i + ": " );
for ( int ia = 0 ; ia < 16 ; ia++ )
{
if ( i+ia < min )
{
System.out.print ( hexChars[((a[i+ia]>>4) & 0x0F)] +
hexChars[(a[i+ia] & 0x0F)] );
if ( a[i+ia] != b[i+ia] )
System.out.print("*");
else
System.out.print(" ");
}
else
{
System.out.print(" ");
}
}
System.out.print(" --- ");
for ( int ia = 0 ; ia < 16 ; ia++ )
{
if ( i+ia < min )
{
System.out.print ( hexChars[((b[i+ia]>>4) & 0x0F)] +
hexChars[(b[i+ia] & 0x0F)] );
if ( a[i+ia] != b[i+ia] )
System.out.print("*");
else
System.out.print(" ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
i+=16;
}
System.out.println();
}
} // GetAnyCard.java