All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.licel.jcardsim.samples.DualInterfaceApplet Maven / Gradle / Ivy

There is a newer version: 3.0.6.0
Show newest version
/*
 * Copyright 2014 Robert Bachmann
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.licel.jcardsim.samples;

import javacard.framework.*;


/**
 * Dual interface applet.
 *
 * 

Supported APDUs:

* *
    *
  • CLA=0x80 INS=0 read value
  • *
  • * CLA=0x80 INS=2 store value from CData. * Only works on contacted interface *
  • *
  • CLA=0x80 INS=4 get interface information (protocol byte)
  • *
*/ public class DualInterfaceApplet extends BaseApplet { private static final byte CLA = (byte) 0x80; private static final byte INS_READ = 0; private static final byte INS_WRITE = 2; private static final byte INS_INFO = 4; private static final byte CLA_MASK = (byte) 0xF0; private static final byte _0 = 0; private static final byte _1 = 1; private final byte[] store; private short storeLen = 0; public static void install(byte[] bArray, short bOffset, byte bLength) { new DualInterfaceApplet().register(); } protected DualInterfaceApplet() { store = new byte[255]; } public void process(APDU apdu) { byte[] buffer = apdu.getBuffer(); final short readCount = apdu.setIncomingAndReceive(); final short lc = apdu.getIncomingLength(); final short offsetCData = apdu.getOffsetCdata(); short read = readCount; while(read < lc) { read += apdu.receiveBytes(read); } if (selectingApplet()) { return; } if ((buffer[ISO7816.OFFSET_CLA] & CLA_MASK) != CLA) { ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED); } switch (buffer[ISO7816.OFFSET_INS]) { case INS_READ: { Util.arrayCopyNonAtomic(store, _0, buffer, _0, storeLen); apdu.setOutgoingAndSend(_0, storeLen); break; } case INS_WRITE: { if (isContacted()) { storeLen = lc; Util.arrayCopyNonAtomic(buffer, offsetCData, store, _0, storeLen); } else { ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED); } break; } case INS_INFO: { buffer[0] = APDU.getProtocol(); apdu.setOutgoingAndSend(_0, (short) 1); break; } default: ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED); } } public boolean isContacted() { return APDU.getProtocol() == APDU.PROTOCOL_T0 || APDU.getProtocol() == APDU.PROTOCOL_T1; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy