11 Replies Latest reply: Jan 27, 2013 5:28 PM by 807105 RSS

    Newbie question about JCWDE

    807105
      Hello,

      I'm sending that message because I'm starting with java card applets and I'm a bit confused about the testing.
      I have the eclipse 3.2 and I installed succesfully the eclipsejcde with java card kit 2.2.2 and I started a java card project and used a simple applet (hello world).specifying the AID for the project and applet.

      Then I created the .cap with the convert option from JCDE and then generated the scripts but I'm not sure the APDU I have to send to interact with the applet using JCWDE.

      The applet I'm using is the following:
      package hello;
      import javacard.framework.APDU;
      import javacard.framework.Applet;
      import javacard.framework.ISO7816;
      import javacard.framework.ISOException;
      import javacard.framework.Util;

      public class HelloWorld extends Applet
      {
      /** The String "Hello" */
      private final static byte[] hello =
      { 0x48, 0x65, 0x6c, 0x6c, 0x6f } ;

      /** Basic Java Card applet registration */
      public static void install(byte[] bArray, short bOffset, byte bLength) {
      new HelloWorld().register();
           }

      /**
      * Method that processes APDUs. The only accepted command is
      * <code>00 40 00 00 00</code>, which returns the string "Hello".
      * All other commands (except SELECT) are rejected.
      */
           public void process(APDU apdu) {
                // Good practice: Return 9000 on SELECT
                if (selectingApplet()) {
                     return;
                }

                byte[] buf = apdu.getBuffer();

      switch (buf[ISO7816.OFFSET_INS]) {
                case (byte) 0x40:
      Util.arrayCopy(
      hello,(byte)0,buf,ISO7816.OFFSET_CDATA,(byte)5);
      apdu.setOutgoingAndSend(
      ISO7816.OFFSET_CDATA,(byte)5);
      break;
                default:
                     // good practice: If you don't know the INStruction, say so:
                     ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
                }
           }
      }

      And the script this one:

      powerup;
      // Select the installer applet
      0x00 0xA4 0x04 0x00 0x09 0xa0 0x00 0x00 0x00 0x62 0x03 0x01 0x08 0x01 0x7F;
      // create HelloWorld applet
      0x80 0xB8 0x00 0x00 0xd 0xb 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x00 0x00 0x00 0x7F;

      // select?¿
      0x00 0x40 0x00 0x00 0x00 0x7F;
      powerdown;

      The responses from the JCWDE:

      Java Card 2.2.2 APDU Tool, Version 1.3
      Copyright 2005 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.
      Opening connection to localhost on port 9025.
      Connected.
      Received ATR = 0x3b 0xf0 0x11 0x00 0xff 0x00
      CLA: 00, INS: a4, P1: 04, P2: 00, Lc: 09, a0, 00, 00, 00, 62, 03, 01, 08, 01, Le: 00, SW1: 90, SW2: 00
      CLA: 80, INS: b8, P1: 00, P2: 00, Lc: 0d, 0b, 01, 02, 03, 04, 05, 06, 07, 08, 09, 00, 00, 00, Le: 0b, 01, 02, 03, 04, 05, 06, 07, 08, 09, 00, 00, SW1: 90, SW2: 00
      CLA: 00, INS: 40, P1: 00, P2: 00, Lc: 00, Le: 00, SW1: 6d, SW2: 00

      Thanks for your help.
        • 1. Re: Newbie question about JCWDE
          Hana Bizhani
          Hi,

          I tried sending APDU to the applet with netbeans, it doesn't return error, this is the console:
          send 0x00 0xA4 0x04 0x00 0X07 0X58 0X27 0XC0 0XCC 0X37 0XC9 0X9A
          CLA: 00, INS: a4, P1: 04, P2: 00, Lc: 07, 58, 27, c0, cc, 37, c9, 9a, Le: 00, SW1: 90, SW2: 00
          
          send 0x00 0x40 0x00 0x00 0x00
          CLA: 00, INS: 40, P1: 00, P2: 00, Lc: 00, Le: 05, 48, 65, 6c, 6c, 6f, SW1: 90, SW2: 00
          I didn't send any create applet APDU (80 b8)

          Regards,
          Hana
          • 2. Re: Newbie question about JCWDE
            807105
            Hello Hana,

            I think you are right, to simulate is not necessary to send the create applet but I'm doing something wrong. I analized the structure of the scripts that jcde is using, for example if I execute the create applet script:

            powerup;
            // Select the installer applet
            0x00 0xA4 0x04 0x00 0x09 0xa0 0x00 0x00 0x00 0x62 0x03 0x01 0x08 0x01 0x7F;
            // create HelloWorld applet
            0x80 0xB8 0x00 0x00 0xd 0xb 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x00 0x00 0x00 0x7F;
            powerdown;

            It returns me:

            Connected.
            Received ATR = 0x3b 0xf0 0x11 0x00 0xff 0x00
            CLA: 00, INS: a4, P1: 04, P2: 00, Lc: 09, a0, 00, 00, 00, 62, 03, 01, 08, 01, Le: 00, SW1: 90, SW2: 00
            CLA: 80, INS: b8, P1: 00, P2: 00, Lc: 0d, 0b, 01, 02, 03, 04, 05, 06, 07, 08, 09, 00, 00, 00, Le: 0b, 01, 02, 03, 04, 05, 06, 07, 08, 09, 00, 00, SW1: 90, SW2: 00

            Comparing the second send line it is using the following structure CLA, INS, P1,P2,LC,LE,DATA containing the DATA + the LE field??¿¿, and finaly the 7F that i don't know what it means.

            Thats not the tipical APDU structure no? Following that to send 0x00 0x40 0x00 0x00 0x00 i would have to send 0x00 0x40 0x00 0x00 0x01 0x05 because I'm specting a 5 bytes response corresponding to 05, 48, 65, 6c, 6c, 6f??

            I'm really confused, thanks for your answers.
            • 3. Re: Newbie question about JCWDE
              807105
              Hello Hanna,

              I solved it... xD. In JCDWE from Eclipse I have for each test I'm doing create the applet into the simulator, select it and then interact with it.

              I used these commands:

              // create HelloWorld applet
              0x80 0xB8 0x00 0x00 0xd 0xb 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x00 0x00 0x00 0x7F;
              // select HelloWorld applet
              0x00 0xA4 0x04 0x00 0xb 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x00 0x00 0x7F;
              //interact
              0x00 0x40 0x00 0x00 0x00 0x7F;

              But I would like to understand the meaning of 0x7F.

              The create uses the AID of the applet as I can see and the class and instruction are predefined as the select, that with the A4 uses the AID installed, but why that last byte?

              Thanks
              • 4. Re: Newbie question about JCWDE
                Hana Bizhani
                Hi,

                1)
                Let's take a look at this command
                 CLA: 80, INS: b8, P1: 00, P2: 00, Lc: 0d, 0b, 01, 02, 03, 04, 05, 06, 07, 08, 09, 00, 00, 00, Le: 0b, 01, 02, 03, 04, 05, 06, 07, 08, 09, 00, 00, SW1: 90, SW2: 00 
                As you said all parts are right until LC, LC=0d means that the data part is 13 bytes which is (0b, 01, 02, 03, 04, 05, 06, 07, 08, 09, 00, 00, 00),
                to this end is the APDU request part. each request has a response. you can see the APDU response from Le to the end.
                Le=0b means the data part of the response APDU is 11 bytes length, which is (01, 02, 03, 04, 05, 06, 07, 08, 09, 00, 00) and SW1 and SW2 is the status of the response which is 90 00.

                2)
                APDU tool add 0x7F at the end of each APDU, you don't need to add it when you wanna send that APDU, just drop it and send the other parts,
                As I said before just send linke this:
                send 0x00 0xA4 0x04 0x00 0X07 0X58 0X27 0XC0 0XCC 0X37 0XC9 0X9A
                CLA: 00, INS: a4, P1: 04, P2: 00, Lc: 07, 58, 27, c0, cc, 37, c9, 9a, Le: 00, SW1: 90, SW2: 00
                 
                send 0x00 0x40 0x00 0x00 0x00
                CLA: 00, INS: 40, P1: 00, P2: 00, Lc: 00, Le: 05, 48, 65, 6c, 6c, 6f, SW1: 90, SW2: 00
                It's clear that there's no 0x7F in my request APDUs.

                3)
                sending a request like this:
                send 0x00 0x40 0x00 0x00 0x00 
                has two kinds of response (I think based on GP mode, but I'm not sure)
                response 1) 05, 48, 65, 6c, 6c, 6f, with SW1: 90, SW2: 00
                response 2) SW1: 6C , SW2 : 05

                the second response is not error, it represents that there is 5 bytes of data to show, then you can send GET DATA command in order to read the data
                in these kind of responses you should send
                send 0x00 0x40 0x00 0x00 0x05 
                in order to read data directly

                Regards,
                Hana
                • 5. Re: Newbie question about JCWDE
                  807105
                  Thank you very much Hana, now I can understand that.

                  As you said when I will have to interact with the java card I won't have to put the 0x7f, but that simulator forces me to finish every APDU with that byte, if not it returns an error.
                  • 6. Re: Newbie question about JCWDE
                    Hana Bizhani
                    Hi,

                    Is there any problem now?
                    I don't know the last sentence is a question or not?!

                    try your code with netbeans 6.9, it doesn't need any setup, once you install it you can create a new Javacard project and test it with its simulator


                    Regards,
                    Hana
                    • 7. Re: Newbie question about JCWDE
                      807105
                      Yes yes the question was solved jejej. One only thing with netbeans I can create a java card project but is it posible to generate .cap like JCDE?

                      Thanks!
                      • 8. Re: Newbie question about JCWDE
                        Hana Bizhani
                        Hi again,

                        I think you mean how to create a cap file with netbeans in order to install that on a real card, if so there's the solution,
                        Every javacard project in netbeans has .cap file in build folder,
                        but the .cap file differ based on the type of the project,
                        I mean if you create your applet to run on netbeans simulator, then you can not use that .cap file to install on real card,
                        so to create .cap file for real card, try to create a new java project (not javacard project)
                        then try this link to develop your applet :
                         https://minotaur.fi.muni.cz:8443/~xsvenda/docuwiki/doku.php?id=public:smartcard:javacardcompilation 
                        Let me know if I misunderstood your question

                        Regards,
                        Hana
                        • 9. Re: Newbie question about JCWDE
                          807105
                          No, You gave me a solution thanks :).

                          By the way I'm trying to set up that enviroment but there's something that confuses me. In you link it says:

                          Java Card Development Kit 2.1.2 (http://www.oracle.com/technetwork/java/javacard/downloads/index-jsp-138488.html#devkit). NOTE: most of the current cards doesn't support JavaCard 2.2.2 therefore you cannot use JCDK 2.2.2.

                          But few lines later, it says Create a “lib” directory in the project root folder and copy Java Card Ant Tasks “jctasks.jar” into this directory.

                          I have downloaded the java card development kit 2.1.2, and there isn't the jctasks.jar. Is it into the 2.2.2. It is not important to put that jar from the 2.2.2 version to 2.1.2?
                          • 10. Re: Newbie question about JCWDE
                            Hana Bizhani
                            Hi,

                            It is mentioned in the text :
                            Java Card Ant Tasks (binary and docs can be found in Java Card Development Kit 2.2.2, subdirectory “ant-tasks”). Note: Java card Ant Tasks are not included in JCDK 2.1.2, therefore you have to copy them from JCDK 2.2.2 - but converter must be from JCDK 2.1.2 (unless your card supports JC 2.2.2.)
                            You can use Netbeans to create your cap file and use other shells like GPShell or GPJ or Kona Roaster to load and install it onto the card,
                            In this case there's no need to do ant task and gpshell steps in that link.

                            Regards
                            Hana
                            • 11. Re: Newbie question about JCWDE
                              807105
                              Hello,

                              Finally i downloaded the ubuntu image with all the necessary programs. I tried and I managed to install a program into my jcop 31v2.2 card using the install_JCOP.txt script, but I don't know why I'm not able to do the same with a JCOP 41 card i have.

                              The script is this one:

                              #
                              # Install script for JCOP cards with mother key
                              #
                              # Tested with:
                              # NXP JCOP 41 v2.2.1 72k
                              # NXP JCOP 21 V2.2 36K

                              mode_211
                              enable_trace
                              establish_context
                              card_connect -readerNumber 1

                              select -AID a000000003000000
                              open_sc -security 1 -keyind 0 -keyver 0 -mac_key 404142434445464748494a4b4c4d4e4f -enc_key 404142434445464748494a4b4c4d4e4f
                              #open_sc -scp 2 -scpimpl 0x15 -security 1 -keyind 0 -keyver 0 -mac_key 404142434445464748494a4b4c4d4e4f -enc_key 404142434445464748494a4b4c4d4e4f // Open secure channel

                              delete -AID ${jc.applet.AID_GPShell}
                              delete -AID ${jc.package.AID_GPShell}

                              #install -file ${build.classes.dir}/${jc.package.path}/javacard/${jc.package.shortName}.cap -nvDataLimit 1000 -instParam C90145 -priv 2
                              #install -file ${build.classes.dir}/${jc.package.path}/javacard/${jc.package.shortName}.cap -sdAID a000000003000000 -nvCodeLimit 4000 -priv 0
                              install -file ${build.classes.dir}/${jc.package.path}/javacard/${jc.package.shortName}.cap -nvDataLimit 2000 -instParam 00 -priv 2

                              # test selection
                              select -AID ${jc.applet.AID_GPShell}

                              card_disconnect
                              release_context

                              The response it throws is the following one, as an answer for the install line from the java card.

                              select_application() returns 0x80216a82 (6a82: the application to be selected could not be found).

                              It seems like the manager to install the applet is not related with that instruction?