Example usage for javax.smartcardio CommandAPDU CommandAPDU

List of usage examples for javax.smartcardio CommandAPDU CommandAPDU

Introduction

In this page you can find the example usage for javax.smartcardio CommandAPDU CommandAPDU.

Prototype

public CommandAPDU(ByteBuffer apdu) 

Source Link

Document

Creates a CommandAPDU from the ByteBuffer containing the complete APDU contents (header and body).

Usage

From source file:net.java.jless.smartcard.SCIOSmartcard.java

/** {@inheritDoc} */
public APDURes transmit(byte[] apdu) throws SmartcardException {
    try {/*from  ww  w .  jav  a 2  s .c  om*/
        CommandAPDU req = new CommandAPDU(apdu);
        long start = 0;
        if (debug && log.isDebugEnabled()) {
            start = System.currentTimeMillis();
            log.debug("apdu > (len=" + apdu.length + ") " + Hex.b2s(apdu));
        }
        APDURes res = new APDURes(card.getBasicChannel().transmit(req).getBytes());
        if (debug && log.isDebugEnabled()) {
            long timeTaken = System.currentTimeMillis() - start;
            log.debug("apdu < (len=" + res.getBytes().length + ", time=" + timeTaken + " ms) "
                    + Hex.b2s(res.getBytes()));
        }
        return res;
    } catch (CardException e) {
        throw new SmartcardException(e);
    }
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

public int getMode() {
    try {//from   w  ww  . j  a  v a2 s.  c  o m
        ByteBuffer buf = ByteBuffer.allocate(5);
        buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.getMode, 0, 0, 1 });
        buf.position(0);
        System.out.println("Input to GetMode: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Reponse from getMode: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            return response.getData()[0];
        }
    } catch (CardException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return -1;
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

public SmartcardStatusCode setRootMode(byte[] accesscode) {
    try {/*  w  ww .  ja v a 2 s . c om*/
        ByteBuffer buf = ByteBuffer.allocate(13);
        buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.setRootMode, 0, 0, 8 });
        buf.put(accesscode);
        buf.position(0);
        System.out.println("Input to setRootMode: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("response from setRootMode: " + response);
        return this.evaluateStatus(response);
    } catch (CardException e) {
        return SmartcardStatusCode.NOT_FOUND;
    }
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

public SmartcardStatusCode setWorkingMode() {
    ByteBuffer buf = ByteBuffer.allocate(4);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.setWorkingMode, 0, 0 });
    buf.position(0);/*from www .j av  a2 s .c  o m*/
    try {
        System.out.println("Input for setWorkingMode: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("response from setWorkingMode: " + response);
        return this.evaluateStatus(response);
    } catch (CardException e) {
        return SmartcardStatusCode.NOT_FOUND;
    }
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

private void putData(byte[] data) {
    try {/*  w  w w  . j  a v a 2 s  . co m*/
        ByteBuffer buf = ByteBuffer.allocate(7 + data.length);
        buf.put((byte) this.ABC4TRUSTCMD);
        buf.put(this.putData);
        buf.put(new byte[] { 0, 0, 0 });
        buf.put(this.intLengthToShortByteArr(data.length));
        buf.put(data);
        buf.position(0);
        System.out.println("Input to PutData: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from putData: " + response);
    } catch (CardException e) {
        e.printStackTrace();
    }
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

@Override
public RSAVerificationKey readAuthenticationKey(int pin, int keyID) {
    byte[] data = new byte[5];
    System.arraycopy(this.pinToByteArr(pin), 0, data, 0, 4);
    data[4] = (byte) keyID;
    ByteBuffer buffer = ByteBuffer.allocate(14);
    buffer.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.readAuthenticationKey, 0, 0, 0, 0, 5 });
    buffer.put(data);// w ww.j a  v  a 2s .c o m
    buffer.put(new byte[] { 0, 0 });
    buffer.position(0);
    try {
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buffer));
        System.out.println("Response from readAuthenticationKey: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            RSAVerificationKey vkey = new RSAVerificationKey();
            vkey.n = new BigInteger(1, response.getData());
            return vkey;
        }
        return null;
    } catch (CardException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

/**
 * //  w  ww  .j av  a2 s . c o m
 * @param mode
 * @param component
 * @param groupID 
 * @param compType (0: mod, 1: order, 2: co-factor)
 * @param rootKey
 * @return
 * @throws CardException
 */
private SmartcardStatusCode setGroupComponent(int mode, byte[] component, int groupID, int compType,
        RSAKeyPair rootKey) throws CardException {
    component = removeSignBit(component);

    byte[] data = new byte[2];
    data[0] = (byte) groupID;
    data[1] = (byte) compType;
    if (mode == 1) {
        this.putData(component);
    } else {
        System.out.println("Can only use setGroupComponent in root mode");
        return SmartcardStatusCode.UNAUTHORIZED;
    }
    ByteBuffer buf = ByteBuffer.allocate(7);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.setGroupComponent, 0, 0, 2, (byte) groupID,
            (byte) compType });
    buf.position(0);
    System.out.println("Input for set Group Component: " + Arrays.toString(buf.array()));
    ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
    System.out.println("Response from setGroupComponent: " + response);
    return this.evaluateStatus(response);
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

private SmartcardStatusCode setGenerator(int mode, byte[] g, int groupID, int genID, RSAKeyPair rootKey)
        throws CardException {
    g = this.removeSignBit(g);

    byte[] data = new byte[2];
    data[0] = (byte) groupID;
    data[1] = (byte) genID;
    if (mode == 1) {
        this.putData(g);
    } else {//w  w w. j av a  2  s.  c o m
        System.out.println("Can only use setGenerator in root mode");
        return SmartcardStatusCode.UNAUTHORIZED;
    }
    ByteBuffer buf = ByteBuffer.allocate(7);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.setGenerator, 0, 0, 2, (byte) groupID, (byte) genID });
    buf.position(0);
    System.out.println("Input for set Generator: " + Arrays.toString(buf.array()));
    ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
    System.out.println("Response from setGenerator: " + response);
    return this.evaluateStatus(response);
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

private SmartcardStatusCode setCounter(int counterID, int keyID, int index, int threshold, byte[] cursor,
        RSAKeyPair rootKey) {//from www.  j  av  a 2 s.c o  m
    if (cursor.length != 4) {
        throw new RuntimeException("Cursor should be of length 4");
    }
    byte[] data = new byte[8];
    data[0] = (byte) counterID;
    data[1] = (byte) keyID;
    data[2] = (byte) index;
    data[3] = (byte) threshold;
    System.arraycopy(cursor, 0, data, 4, 4);
    try {
        int mode = this.getMode();
        if (mode == 2) {
            System.out.println("Can only use setCounter in root mode");
            return SmartcardStatusCode.UNAUTHORIZED;
        }
        ByteBuffer buf = ByteBuffer.allocate(13);
        buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.setCounter, 0, 0, 8 });
        buf.put(data);
        buf.position(0);
        System.out.println("Input for setCounter: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from setCounter: " + response);
        return this.evaluateStatus(response);
    } catch (CardException e) {
        return SmartcardStatusCode.NOT_FOUND;
    }
}

From source file:eu.abc4trust.smartcard.HardwareSmartcard.java

public byte[] getChallenge(int size) {
    //TODO: Make this work for challenge sizes of 256 (or 0)
    if ((size > 256) || (size < 1)) {
        System.err.println("Argument 'size' for getChallenge should be in the range [1,256]");
        return null;
    }/*from w  w  w.ja  v  a 2  s.co m*/
    try {
        int realSize = size;
        if (realSize == 256) {
            realSize = 0;
        }
        ByteBuffer buf = ByteBuffer.allocate(7);
        System.out.println("Le: " + (byte) size);
        buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.getChallenge, 0, 0, 1, (byte) realSize, 0 });
        buf.position(0);
        System.out.println("Input for getChallenge: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from getChallenge: " + response);
        if (this.evaluateStatus(response) == SmartcardStatusCode.OK) {
            return response.getData();
        } else {
            return null;
        }
    } catch (CardException e) {
        return null;
    }
}