Example usage for javax.smartcardio ResponseAPDU getSW1

List of usage examples for javax.smartcardio ResponseAPDU getSW1

Introduction

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

Prototype

public int getSW1() 

Source Link

Document

Returns the value of the status byte SW1 as a value between 0 and 255.

Usage

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

private SmartcardStatusCode evaluateStatus(ResponseAPDU response) {
    switch (response.getSW1()) {
    case STATUS_OK:
        return SmartcardStatusCode.OK;
    case STATUS_FORBIDDEN:
        return SmartcardStatusCode.FORBIDDEN;
    case STATUS_TOO_LITTLE_DATA:
    case STATUS_NOT_EXACT_DATA_SIZE:
    case STATUS_TOO_SMALL_CHALLENGE:
    case STATUS_RFU:
        return SmartcardStatusCode.BAD_REQUEST;
    case STATUS_ERROR:
        switch (response.getSW2()) {
        case STATUS_BAD_PIN:
        case STATUS_BAD_PUK:
            return SmartcardStatusCode.UNAUTHORIZED;
        case STATUS_CARD_LOCKED:
        case STATUS_CARD_DEAD:
            return SmartcardStatusCode.FORBIDDEN;

        }//from   w  w w. j av a  2 s  .  c o  m
        return SmartcardStatusCode.BAD_REQUEST;
    default:
        return SmartcardStatusCode.BAD_REQUEST;
    }
}

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

@Override
public SmartcardStatusCode storeBlob(int pin, URI uri, SmartcardBlob blob) {
    //this.resetCard();

    String[] forbiddenChars = new String[] { "\u0167", ":", "*", "?", "<", ">", " ", "|" };
    if (uri.toString().contains(":") && !uri.toString().contains("_")) {
        uri = URI.create(uri.toString().replaceAll(":", "_")); //change all ':' to '_'
    } else {//from w w w  .jav  a 2s .  c o m
        for (int i = 0; i < forbiddenChars.length; i++) {
            if (uri.toString().contains(forbiddenChars[i])) {
                throw new RuntimeException(
                        "Cannot store a blob under a URI containing the following char: " + forbiddenChars[i]);
            }
        }
    }
    byte[] uriBytes = null;
    uriBytes = this.uriToByteArr(uri);
    if (uriBytes.length > 199) {
        return SmartcardStatusCode.REQUEST_URI_TOO_LONG;
    }

    // BLOB CACHE!
    blobCache.put(uri, blob);
    blobUrisCache.add(uri);

    //first put data from blob followed by the STORE BLOB command
    this.putData(blob.blob);

    byte[] data = new byte[4 + uriBytes.length];
    System.arraycopy(this.pinToByteArr(pin), 0, data, 0, 4);
    System.arraycopy(uriBytes, 0, data, 4, uriBytes.length);
    ByteBuffer buf = ByteBuffer.allocate(9 + uriBytes.length);
    buf.put(new byte[] { (byte) this.ABC4TRUSTCMD, this.storeBlob, 0, 0, (byte) data.length });
    buf.put(data);
    buf.position(0);
    try {
        if (printInput)
            System.out.println("Input for storeBlob: " + Arrays.toString(buf.array()));
        ResponseAPDU response = this.transmitCommand(new CommandAPDU(buf));
        System.out.println("Response from storeBlob: " + response);
        if ((response.getSW1() != STATUS_OK) && (response.getSW1() != STATUS_BAD_PIN)) {
            throw new InsufficientStorageException("Could not store blob. Response from card: " + response);
        }
        return this.evaluateStatus(response);
    } catch (CardException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:NFC.IsmbSnepConnectionTarget.java

/**
 * Sends and receives APDUs to and from the controller
 *
 * @param instr/*  ww w .  j  a va 2 s . co 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 ResponseAPDU transmit(CommandAPDU commandApdu) throws CardException {
    ResponseAPDU responseApdu = this.cardChannel.transmit(commandApdu);
    if (0x6c == responseApdu.getSW1()) {
        /*// w  w  w .  jav  a 2  s  .  com
         * A minimum delay of 10 msec between the answer 6C xx and the
         * next APDU is mandatory for eID v1.0 and v1.1 cards.
         */
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            throw new RuntimeException("cannot sleep");
        }
        responseApdu = this.cardChannel.transmit(commandApdu);
    }
    return responseApdu;
}