1 2 Previous Next 17 Replies Latest reply: Aug 21, 2012 10:18 AM by safarmer RSS

    Create file on javacard

    949970
      Hi
      I want to store a picture on my javacard.
      Is there a way to do it ?
      I think it will be easy if i put the byte[] on a file but i don t know if it s possible to creat a file in a javacard ?
      could i have some help please ?
      thanks
        • 1. Re: Create file on javacard
          safarmer
          Hi,

          Storing an image in javacard is as simple as storing it in a byte array. The difficulty comes with any other requirements you have. With an image you will most likely need to add some kind of apdu command and response chaining.

          An overly simplified version of the applet is:
          import javacard.framework.APDU;
          import javacard.framework.Applet;
          import javacard.framework.ISO7816;
          import javacard.framework.ISOException;
          import javacard.framework.Util;
          import javacardx.apdu.ExtendedLength;
          
          /**
           * Basic applet for reading and writing an image file using extended length APDU's.
           * @author shane
           */
          public class BasicApplet extends Applet implements ExtendedLength {
            private final static byte INS_WRITE_FILE = 0x00;
            private final static byte INS_READ_FILE = 0x01;
          
            private final static short MAX_IMAGE_SIZE = (short) ((10 * 1024) + 2);
            private byte[] imageFile = new byte[MAX_IMAGE_SIZE];
            private final static byte OFF_LEN = 0x00;
            private final static byte OFF_IMAGE = 0x02;
          
            public static void install(byte[] bArray, short bOffset, byte bLength) {
              // GP-compliant JavaCard applet registration
              new BasicApplet().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
            }
          
            public void process(APDU apdu) {
              // Good practice: Return 9000 on SELECT
              if (selectingApplet()) {
                return;
              }
          
              byte[] buf = apdu.getBuffer();
              
              switch (buf[ISO7816.OFFSET_INS]) {
          
                case INS_WRITE_FILE:
                  short inlen = apdu.setIncomingAndReceive();
                  Util.setShort(imageFile, OFF_LEN, inlen);
                  Util.arrayCopyNonAtomic(buf, apdu.getOffsetCdata(), imageFile, OFF_IMAGE, inlen);
                  break;
          
                case INS_READ_FILE:
                  short outlen = Util.getShort(imageFile, OFF_LEN);
                  apdu.setOutgoing();
                  apdu.setOutgoingLength(outlen);
                  apdu.sendBytesLong(imageFile, OFF_IMAGE, outlen);
                  break;
          
                default:
                  // good practice: If you don't know the INStruction, say so:
                  ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
              }
            }
          }
          I am certain this wont meet your requirements yet, but it is a start to learning what you need to solve the problem.

          Disclaimer: the code above has not been tested other than the fact it compiles :)

          - Shane
          • 2. Re: Create file on javacard
            949970
            First, thanks a lot for your answer i didn t know the interface ExtendedLengh but unfortunately
            I have an error when i am trying to use the javacardx package
            i have this error:
            -------------------------------------------------------------------------------------------------------------
            ./helloWorldPackage/HelloWorldApplet.java:72: package javacardx.framework does not exist
            import javacardx.framework.*;
            ^
            1 error

            --------------------------------------------------------------------------------------------

            how should i compile this programe, i use to do somthing like

            javac ./helloWorldPackage/Porteur.java ./helloWorldPackage/HelloWorldApplet.java -classpath /home/dwyane/java_card_kit-2_2_2/lib/api.jar -source 1.3 -target 1.3

            thanks
            • 3. Re: Create file on javacard
              Lyolik
              Hi,

              Dwyane wrote:
              javac ./helloWorldPackage/Porteur.java ./helloWorldPackage/HelloWorldApplet.java -classpath /home/dwyane/java_card_kit-2_2_2/lib/api.jar -source 1.3 -target 1.3
              Try to change the -source and -target parameters to *1.4* or *1.5*.
              • 4. Re: Create file on javacard
                safarmer
                The target and source should not affect build path issues (it would complain about class version if it was the case). The test compile was with a Java 1.3 compiler setting.

                The issue is probably with the compiler class path. It may be better to see if your card supports extended length APDU's first. If it does not then APDU chaining would be more appropriate.

                - Shane
                • 5. Re: Create file on javacard
                  949970
                  thanks for all your answers
                  i have tested for -source 1.3 -target 1.3 -source 1.4 -target 1.4 -source 1.5 -target 1.5
                  but it s the same result , i still geting this error .
                  Realy i don t know if my javacard suport Extendedlengh , but i thing the best way to know is to test it with this applet .
                  For that, i need to know what is wrong with my comand line .
                  i forget to say something , i am using gpshell 1.4.4 , globalplatform 6.0.0 and javacard 2.2.2
                  Why my classpath (javacard 2.2.2) make error when i use javacardx ?

                  thanks i am waiting for other suggestions

                  best regards
                  • 6. Re: Create file on javacard
                    949970
                    I don t know what i have done but it compile right now .
                    but even i emplement extendedLengh if i declare an array of byte bigger than 256 . i receave a statut different of 9000
                    maybe it s mean that my javacard are not compatible for extendedLengh ?
                    If someone have an idea to store a picture without using extendedLengh ?

                    safarmer you told me that APDU chaining would be more appropriate, but i don t understand how ?
                    my problem here is about to store a lot of byte . how could i do that with APDU chaining?
                    Thanks
                    • 7. Re: Create file on javacard
                      safarmer
                      I don t know what i have done but it compile right now .
                      That is good to hear.
                      but even i emplement extendedLengh if i declare an array of byte bigger than 256 . i receave a statut different of 9000
                      How are you running the applet (card or emulator)? What is the status word you receive? Can you post example APDU trace and is the code the same as I posted?
                      maybe it s mean that my javacard are not compatible for extendedLengh ?
                      That is a possibility. I would assume the applet would not load though if you import javacardx.apdu and the card does not support it though.
                      If someone have an idea to store a picture without using extendedLengh ?

                      safarmer you told me that APDU chaining would be more appropriate, but i don t understand how ?
                      my problem here is about to store a lot of byte . how could i do that with APDU chaining?
                      You can search the forum for command and response chaining. I have uploaded code (relatively) recently that you should be able to find.

                      - Shane
                      • 8. Re: Create file on javacard
                        949970
                        Hi shane

                        i am running the applet on a card
                        when i have tried with your code , i get this error when i am trying to upload the applet on the card
                        //////////////////////////////////
                        Response <-- 6985
                        install_for_install_and_make_selectable() returns 0x80206985 (6985: Command not allowed - Conditions of use not satisfied.)
                        /////////////////////////////////

                        and when i replace
                        MAX_IMAGE_SIZE = 1024;
                        private byte[] imageFile = new byte[MAX_IMAGE_SIZE];
                        with
                        private byte[] imageFile = new byte[256];

                        it works (i can upload the applet) . So it s look like my javacard are not compatible .

                        I want to ask , is it possible to create a file on a card . i mean not using javacard applet just send specific apdu from the PC and store data on the card .
                        if it s possible can i have some help please ?

                        i have an id protect card v3
                        JC 2.2.2 / GP 2.1.1 / 72K


                        regards.
                        • 9. Re: Create file on javacard
                          safarmer
                          That seems strange. Can you create a byte[] that is 512 bytes? What happens if you create the byte array explicitly in your constructor?

                          >
                          I want to ask , is it possible to create a file on a card . i mean not using javacard applet just send specific apdu from the PC and store data on the card .
                          if it s possible can i have some help please ?
                          >

                          No. Without an applet you cannot perform any meaningful functions with Java Card. You would need to implement your own applet (or find one somewhere) that manages file system objects.

                          - Shane
                          • 10. Re: Create file on javacard
                            949970
                            i tried to declare the byte array on the constructor but it s change anything .
                            So u told to me even if it s not a javacard i have to put an applet in the smartcard in the way to create a file .
                            so emv card are javacard because he store data on some files on the card ?
                            • 11. Re: Create file on javacard
                              safarmer
                              i tried to declare the byte array on the constructor but it s change anything .
                              Either you are out of space or there is something strange happening that I would not expect.
                              So u told to me even if it s not a javacard i have to put an applet in the smartcard in the way to create a file .
                              Sorry, I must have misread your post. You need to have an applet on a Java Card based card to store files. If you have a native ISO7816 file system card or something like a mifare card then you can store files or blocks of data.
                              so emv card are javacard because he store data on some files on the card ?
                              EMV cards may be Java Card based as you can implement the payment specifications in an applet, or it could be native (C/Assembly).

                              - Shane

                              Edited by: safarmer on Aug 21, 2012 12:32 AM
                              • 12. Re: Create file on javacard
                                949970
                                thanks for your answer

                                i think it s the compatibility of my card and ExtendedLengh it s work just if my byte array < 256 elements.
                                So in my case can i create a file ?
                                if yes how ? if not why ?

                                thanks
                                • 13. Re: Create file on javacard
                                  Lyolik
                                  Dwyane wrote:
                                  i think it s the compatibility of my card and ExtendedLengh it s work just if my byte array < 256 elements.
                                  In case of usage ExtendedLengh the APDU buffer size is 32 KB.
                                  If your card has a 72KB EEPROM therefore you should have ~ 72-32=40KB card memory for your applets.
                                  • 14. Re: Create file on javacard
                                    949970
                                    40KB should be enough for creating a byte array like
                                    byte pic[] = new pic[1024]; but when i try to upload i get error with GPShell
                                    do you have a better way to stor a picture on the card ?
                                    1 2 Previous Next