-1

This is a simple Hello World applet for Javacards :

package helloWorldPackage;

import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.Util;

public class HelloWorldApplet extends Applet {
         private static final byte[] helloWorld = {(byte)'H',(byte)'e',(byte)'l',(byte)'l',(byte)'o',(byte)' ',(byte)'W',(byte)'o',(byte)'r',(byte)'l',(byte)'d',};

         private static final byte HW_CLA = (byte)0x80;
         private static final byte HW_INS = (byte)0x00;

         public static void install(byte[] bArray, short bOffset, byte bLength) {
             new HelloWorldApplet().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
         }

         public void process(APDU apdu) {

             if (selectingApplet()) {
             return;
         }

         byte[] buffer = apdu.getBuffer();
         byte CLA = (byte) (buffer[ISO7816.OFFSET_CLA] & 0xFF);
         byte INS = (byte) (buffer[ISO7816.OFFSET_INS] & 0xFF);

         if (CLA != HW_CLA)
        {
            ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
        }

          switch ( INS ) {
             case HW_INS:
               getHelloWorld( apdu );
               break;
            default:
               ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
         }
   }

  private void getHelloWorld( APDU apdu)
  {
      byte[] buffer = apdu.getBuffer();
      short length = (short) helloWorld.length;

      Util.arrayCopyNonAtomic(helloWorld, (short)0, buffer, (short)0, (short) length);

      apdu.setOutgoingAndSend((short)0, length);
  }
}

The question is "What is the meaning of native methods in java cards?"

In the security Targets of smart cards, it is written that "using Native Methods in applications is prevented." the question is "How I can test it for a specific card?" In the other word, I want you to modify above code and add some native methods to it and let me check if it is possible to convert it to .cap files and upload it on my card or not.

Update :

As dear TonyK said in first comment, presumably my development environment won't compile such a thing, so there is two questions :

  1. What are native methods? Any example?
  2. How I can compile them and convert those programs that have this kind of methods inside to .cap files to try uploading them on card?
TheGoodUser
  • 1,188
  • 4
  • 26
  • 52
  • I think they just mean native methods. This will be difficult to test, because presumably your development environment won't even compile such a thing, which means you can't test it in your card. – TonyK Jan 28 '15 at 08:33
  • @TonyK And what is the meaning of Native Methods? would you please clear it with an example? and "This will be difficult to test", but there must be a way, no? what is that way? Thank you – TheGoodUser Jan 28 '15 at 08:39
  • Native methods are provided by your smartcard vendor with his JavaCard implementation.Example: on some NXP cards you can use class com.nxp.id.jcopx.UtilX, which is not a standard JavaCard class. UtilX uses some hardware coprocessors specific for NXP cards you would not be able to access with your own classes. To compile an applet using UtilX you have to add a special jar file to your classpath. – vojta Jan 28 '15 at 11:02
  • Ok, I agree with Michael Roland's answer... I probably misunderstood what "native" means in the Javacard world... – vojta Jan 28 '15 at 12:23

1 Answers1

2

A native method is a method written in another programming language (i.e. not in Java) that can be invoked by a Java program. For smartcards, a native method is typically written in C code or machine code and is directly executed on the underlying smartcard processor (whereas Java Card applications are executed inside the Java Card virtual machine).

With regard to the Java Card language and the execution of applets loaded onto Java Card using CAP files you don't really have to worry about native methods:

  • The Java Card specification does not support the native keyword, so you cannot declare native methods (see Virtual Machine Specification, Java Card Platform, Version 2.2.2 on page 2-4).
  • The CAP file format does not support native methods, so you cannot upload applets declaring native methods. (see Runtime Environment Specification, Java Card Platform, Version 2.2.2 on page Glossary-6).
Michael Roland
  • 39,663
  • 10
  • 99
  • 206
  • I think this is right -- you can't test it, simply because there is no way to specify a native method in a CAP file. So the Security Target rule is automatically satisfied. – TonyK Jan 28 '15 at 11:16
  • The only way to implement native code under Java Card is the SecureBox concept from NXP. – Paul Bastian Jan 28 '15 at 16:29
  • Correct. OS manufacturers usually provide *proprietary* mechanisms to execute native code in a safe environment. E.g. SecureBox for NXP/JCOP. – Michael Roland Jan 28 '15 at 16:32