简体   繁体   中英

How to try using Native Methods in Java card applets?

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?

A native method is a method written in another programming language (ie 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM