Lösung zu Praktikum 1 -Programmierung eines Java Card Applets- Björn Wontora, Holger Plett, Christian Linke, Karsten Reineck, Sven Siek, Andreas Kupfer
Phasen beim Cardlet Entwurf 1. Funktionen des Applets definieren 2. Applet IDs (AID) an Cardlet und Package zuweisen 3. Klassenstruktur des Cardlet Programms entwerfen 4. Interface zwischen Cardlet und Terminal Programm entwerfen
Funktionen des Cardlets Speicherung von elektronischem Geld Aufladen und Abbuchen von Beträgen Max. Betrag: 30000 Abfragen des Kontostandes Sicherheitsmechanismus zur Authentifizierung PIN Abfrage Begrenzung der Logon-Versuche
Zuweisen der AIDs AID identifiziert ein Cardlet eindeutig Standardisiert nach ISO 7816 Application identifier (AID) National registered application provider (RID) 5 bytes Proprietary application identifier extension (PIX) 0 to 11 bytes RID wird von ISO vergeben PIX wird vom Hersteller vergeben Quelle: How to write a Java Card Applet
Select APDU SELECT APDU command Command APDU CLA INS P1 P2 Lc Data field Le 0x0 0xA4 0x04 0x0 0x08 0xF2, 0x34, 0x12, 0x34, 0x56, 0x10, 0x0, 0x1 N/A Command-Header Select APDU Data Field enthält AID des Cardlets Response APDU Optional data No data Status word 0x9000 0x6999 Meaning of status word Successful processing Applet selection failed: the applet could not be found or selected
Verify APDU VERIFY APDU command Command APDU CLA INS P1 P2 Lc Data field Le 0xB0 0x20 0x0 0x0 Length of the PIN data PIN data N/A Header Verify APDU Data Field enthält die PIN Response APDU Optional data N/A Status word 0x9000 0x6300 Meaning of status word Successful processing Verification failed
Aufbuchen APDU (Credit APDU) CREDIT APDU command Command APDU CLA INS P1 P2 Lc Data field Le 0xB0 0x30 0x0 0x0 1 Credit amount N/A Header Credit APDU Data Field Einzuzahlender Betrag Response APDU Optional data N/A Status word 0x9000 0x6301 0x6A83 Meaning of status word Successful processing PIN verification required Invalid credit amount 0x6A84 Exceed the maximum amount
Kontostand APDU GET BALANCE APDU command Command APDU CLA INS P1 P2 Lc Data field Le 0xB0 0x50 0x0 0x0 N/A N/A 2 Response APDU Optional data Status word Meaning of status word Balance value 0x9000 Successful processing
Quellcode schreiben Beginn der Class Importieren des Frameworks Jedes Applet ist abgeleitet von der Superklasse javacard.framework.applet import javacard.framework.*; import javacardx.framework.*; public class Wallet extends Applet {
Anpassung an APDU-Format Festlegen der Command-Struktur (Code fuer das CLA Byte im APDU Header) final static byte Wallet_CLA =(byte)0xb0; Deklarieren der Instructions (Codes fuer INS Byte im APDU Header) final static byte VERIFY = (byte) 0x20; final static byte CREDIT = (byte) 0x30; final static byte DEBIT = (byte) 0x40; final static byte GET_BALANCE = (byte) 0x50;
Instanzieren des Applets im JCRE Constructor für das Applet private Wallet (byte[] barray, short boffset, byte blength) { pin = new OwnerPIN(0x03, 0x08); value pin.update(barray, boffset, blength); register(); } Install Methode wird von JCRE aufgerufen, um eine Instanz eines Applets zu erzeugen public static void install(byte[] barray, short boffset, byte blength) { new Wallet(bArray, boffset, blength); }
Select Methode Select Methode wird von JCRE aufgerufen, um anzuzeigen, dass das Applet selektiert wurde public boolean select() { if ( pin.gettriesremaining() == 0 ) return false; return true; } Liefert TRUE, wenn das Applet erfolgreich ausgewählt wurde
Behandlung einer APDU APDU Select? Ja Wählt Applet JCRE Nein Process CLA Select? Ja Ende Applet Nein Switch Case (INS) Ende
Process Methode Eingehende APDUs werden von JCRE an die Process Methode übergeben Aufgabe von Switch Statement: Ausführen der Funktion, die durch die APDU aufgerufen wird public void process(apdu apdu) { if ((buffer[iso7816.offset_cla] == 0) && (buffer[iso7816.offset_ins] == (byte)(0xa4))) return; switch (buffer[iso7816.offset_ins]) { case GET_BALANCE: getbalance(apdu); return; case DEBIT: debit(apdu); return; case CREDIT: credit(apdu); return; case VERIFY: verify(apdu); return; } }
Bsp. Get_Balance Methode private void getbalance(apdu apdu) { byte[] buffer = apdu.getbuffer(); Teilt mit, dass Response APDU Daten Feld beinhaltet. short le = apdu.setoutgoing(); Setzt die Größe des Daten Feldes für die Response APDU apdu.setoutgoinglength((byte)2); Schreibt den Aktuellen Kontostand in den APDU Buffer buffer[0] = (byte)(balance >> 8); buffer[1] = (byte)(balance & 0xFF); Sendet den Kontostand an den Kartenleser apdu.sendbytes((short)0, (short)2); }
Fragen 1. Geben sie die 4 Phasen beim Cardlet Entwurf an! 2. Wofür wird die Select-Methode verwendet? 3. Geben sie die Bedeutung der Process Methode?
Literatur Chen, Z.: How to write a Java Card Applet: A developer s guide http://www.javaworld.com/javaworld/jw-07-1999/jw-07-javacard.html