Search in sources :

Example 1 with TransmitException

use of org.openecard.ifd.scio.TransmitException in project open-ecard by ecsec.

the class SingleThreadChannel method transmit.

@Nonnull
@Override
public byte[] transmit(@Nonnull byte[] input, @Nonnull List<byte[]> responses) throws TransmitException, SCIOException, IllegalStateException {
    byte[] inputAPDU = input;
    if (isSM()) {
        LOG.debug("Apply secure messaging to APDU: {}", ByteUtils.toHexString(inputAPDU, true));
        inputAPDU = smProtocol.applySM(inputAPDU);
    }
    LOG.debug("Send APDU: {}", ByteUtils.toHexString(inputAPDU, true));
    CardResponseAPDU rapdu = transmit(inputAPDU);
    byte[] result = rapdu.toByteArray();
    LOG.debug("Receive APDU: {}", ByteUtils.toHexString(result, true));
    if (isSM()) {
        result = smProtocol.removeSM(result);
        LOG.debug("Remove secure messaging from APDU: {}", ByteUtils.toHexString(result, true));
    }
    // get status word
    byte[] sw = new byte[2];
    sw[0] = result[result.length - 2];
    sw[1] = result[result.length - 1];
    // return without validation when no expected results given
    if (responses.isEmpty()) {
        return result;
    }
    // verify result
    for (byte[] expected : responses) {
        // AcceptableStatusCode-elements containing only one byte match all status codes starting with this byte
        if (ByteUtils.isPrefix(expected, sw)) {
            return result;
        }
    }
    // not an expected result
    String msg = "The returned status code is not in the list of expected status codes. The returned code is:\n";
    TransmitException tex = new TransmitException(result, msg + CardCommandStatus.getMessage(sw));
    throw tex;
}
Also used : TransmitException(org.openecard.ifd.scio.TransmitException) CardResponseAPDU(org.openecard.common.apdu.common.CardResponseAPDU) Nonnull(javax.annotation.Nonnull)

Aggregations

Nonnull (javax.annotation.Nonnull)1 CardResponseAPDU (org.openecard.common.apdu.common.CardResponseAPDU)1 TransmitException (org.openecard.ifd.scio.TransmitException)1