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:eu.abc4trust.smartcard.HardwareSmartcard.java

public SmartcardStatusCode issueCredentialOnSmartcard(int pin, byte credID) {
    byte[] credInfo = this.readCredential(pin, credID);
    byte status = credInfo[5];
    if (status == 0) {
        try {//from w w w. java  2 s.  c o m
            //Start commitments
            byte[] data = new byte[5];
            System.arraycopy(this.pinToByteArr(pin), 0, data, 0, 4);
            data[4] = 1; //ProverID - TODO: hardcoded to 1 as of now. Assuming there can be only 1 for the pilot
            ByteBuffer buf = ByteBuffer.allocate(11);
            buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.startCommitments, 0, 0, 5 });
            buf.put(data);
            buf.put((byte) 16);
            buf.position(0);
            if (printInput)
                System.out.println("Input for startCommitments: " + Arrays.toString(buf.array()));
            ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
            System.out.println("Response from startCommitments: " + response);
            System.out.println("And this is the output: " + Arrays.toString(response.getData()));
            byte[] proofSession = response.getData();
            if (this.evaluateStatus(response) != SmartcardStatusCode.OK) {
                return this.evaluateStatus(response);
            }

            //Issue the credential
            buf = ByteBuffer.allocate(14);
            buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, getIssuanceCommitment, 0, 0, 0, 0, 5 });
            buf.put(this.pinToByteArr(pin));
            buf.put(credID);
            buf.put(new byte[] { 0, 0 });
            buf.position(0);

            if (printInput)
                System.out.println("Input for getIssuanceCommitment: " + Arrays.toString(buf.array()));
            response = this.transmitCommand(new CommandAPDU(buf));
            System.out.println("Response from getIssuanceCommitment: " + response);
            if (this.evaluateStatus(response) != SmartcardStatusCode.OK) {
                return this.evaluateStatus(response);
            }

            //Start responses
            data = new byte[4 + 1 + 1 + 16 + 1]; //pin, prooverID, d which is the number of proofs, proofsession and h
            System.arraycopy(this.pinToByteArr(pin), 0, data, 0, 4);
            data[4] = 1; //TODO: ProoverID - Hardcoded for now
            data[5] = 1; //number of proofs - hardcoded to 1 for pilot.
            System.arraycopy(proofSession, 0, data, 6, 16);

            buf = ByteBuffer.allocate(7 + data.length);
            buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.startResponses, 0, 0, 0 });
            buf.put(this.intLengthToShortByteArr(data.length));
            buf.put(data);
            buf.position(0);
            if (printInput)
                System.out.println("Input for startResponses: " + Arrays.toString(buf.array()));
            response = this.transmitCommand(new CommandAPDU(buf));
            System.out.println("Response from startResponses: " + response);
            if (this.evaluateStatus(response) != SmartcardStatusCode.OK) {
                return this.evaluateStatus(response);
            }

            //Set status of cred to 2 - issued finalized
            buf = ByteBuffer.allocate(14);
            buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, getIssuanceResponse, 0, 0, 0, 0, 5 });
            buf.put(this.pinToByteArr(pin));
            buf.put(credID);
            buf.put(new byte[] { 0, 0 });
            buf.position(0);
            if (printInput)
                System.out.println("Input for getIssuanceResponse: " + Arrays.toString(buf.array()));
            response = this.transmitCommand(new CommandAPDU(buf));
            System.out.println("Response from getIssuanceResponse: " + response);
            if (this.evaluateStatus(response) != SmartcardStatusCode.OK) {
                return this.evaluateStatus(response);
            }
            credInfo = this.readCredential(pin, credID);
            status = credInfo[5];
            System.out.println(
                    "After issuing the credential with ID " + credID + ", it now has status: " + status);
        } catch (CardException e) {
            throw new RuntimeException("issueCred on smartcard failed.", e);
        }
    } else {
        System.out.println("Warn: Credential on sc attempted issued, but was already issued");
    }
    return SmartcardStatusCode.OK;
}

From source file:NFC.IsmbSnepConnectionTarget.java

/**
 * Sends and receives APDUs to and from the controller
 *
 * @param instr/*from  ww w .j  av  a2 s  . c  o m*/
 *            Instruction
 * @param param
 *            Payload to send
 *
 * @return The response payload 
 */
private byte[] transceive(byte instr, byte[] payload) throws IsmbSnepException {

    if (ch == null) {
        throw new IsmbSnepException("channel not open");
    }
    int payloadLength = (payload != null) ? payload.length : 0;
    byte[] instruction = { (byte) 0xd4, instr };

    //ACR122 header
    byte[] header = { (byte) 0xff, (byte) 0x00, (byte) 0x00, (byte) 0x00,
            (byte) (instruction.length + payloadLength) };

    /* construct the command */
    byte[] cmd = Util.appendToByteArray(header, instruction, 0, instruction.length);
    cmd = Util.appendToByteArray(cmd, payload);

    if (debugMode)
        Util.debugAPDUs(cmd, null);

    try {
        CommandAPDU c = new CommandAPDU(cmd);
        ResponseAPDU r = ch.transmit(c);

        byte[] ra = r.getBytes();

        if (debugMode)
            Util.debugAPDUs(null, ra);

        /* check whether APDU command was accepted by the Controller */
        if (r.getSW1() == 0x63 && r.getSW2() == 0x27) {
            throw new CardException("wrong checksum from contactless response");
        } else if (r.getSW1() == 0x63 && r.getSW2() == 0x7f) {
            throw new CardException("wrong PN53x command");
        } else if (r.getSW1() != 0x90 && r.getSW2() != 0x00) {
            throw new CardException("unknown error");
        }
        return Util.subByteArray(ra, 2, ra.length - 4);
    } catch (CardException e) {

        throw new IsmbSnepException("problem with transmitting data");
    }
}

From source file:src.eidreader.EstEIDUtil.java

private String extractField(CardChannel channel, byte fieldNumber) throws CardException {
    return EstEIDUtil.bytesToString(EstEIDUtil.sendCommand(channel,
            new CommandAPDU(new byte[] { 0x00, (byte) 0xB2, fieldNumber, 0x04, 0x00 })), ENCODING);
}