Search in sources :

Example 6 with APDUException

use of org.openecard.common.apdu.exception.APDUException in project open-ecard by ecsec.

the class ChipAuthentication method mseSetAT.

/**
 * Initializes the Chip Authentication protocol.
 * Sends an MSE:Set AT APDU. (Protocol step 1)
 * See BSI-TR-03110, version 2.10, part 3, B.11.1.
 *
 * @param oID Chip Authentication object identifier
 * @param keyID Key identifier
 * @throws ProtocolException
 */
public void mseSetAT(byte[] oID, byte[] keyID) throws ProtocolException {
    try {
        CardCommandAPDU mseSetAT = new MSESetATCA(oID, keyID);
        mseSetAT.transmit(dispatcher, slotHandle);
    } catch (APDUException e) {
        throw new ProtocolException(e.getResult());
    }
}
Also used : CardCommandAPDU(org.openecard.common.apdu.common.CardCommandAPDU) ProtocolException(org.openecard.common.sal.protocol.exception.ProtocolException) APDUException(org.openecard.common.apdu.exception.APDUException) MSESetATCA(org.openecard.sal.protocol.eac.apdu.MSESetATCA)

Example 7 with APDUException

use of org.openecard.common.apdu.exception.APDUException in project open-ecard by ecsec.

the class ChipAuthentication method readEFCardSecurity.

/**
 * Reads the EFCardSecurity from the card.
 *
 * @return EFCardSecurtiy
 * @throws ProtocolException Thrown in case there is a problem reading the file.
 */
public byte[] readEFCardSecurity() throws ProtocolException {
    try {
        byte[] file = ShortUtils.toByteArray(EACConstants.EF_CARDSECURITY_FID);
        CardResponseAPDU resp = CardUtils.selectFileWithOptions(dispatcher, slotHandle, file, null, CardUtils.FCP_RESPONSE_DATA);
        FCP efCardSecurityFCP = new FCP(TLV.fromBER(resp.getData()));
        byte[] efCardSecurity = CardUtils.readFile(efCardSecurityFCP, dispatcher, slotHandle);
        return efCardSecurity;
    } catch (APDUException ex) {
        throw new ProtocolException(ex.getResult());
    } catch (TLVException ex) {
        throw new ProtocolException("Failed to parse FCP.", ex);
    }
}
Also used : ProtocolException(org.openecard.common.sal.protocol.exception.ProtocolException) FCP(org.openecard.common.tlv.iso7816.FCP) APDUException(org.openecard.common.apdu.exception.APDUException) CardResponseAPDU(org.openecard.common.apdu.common.CardResponseAPDU) TLVException(org.openecard.common.tlv.TLVException)

Example 8 with APDUException

use of org.openecard.common.apdu.exception.APDUException in project open-ecard by ecsec.

the class ChipAuthentication method generalAuthenticate.

/**
 * Performs a General Authenticate.
 * Sends an General Authenticate APDU. (Protocol step 2)
 * See BSI-TR-03110, version 2.10, part 3, B.11.2.
 *
 * @param key Ephemeral Public Key
 * @return Response APDU
 * @throws ProtocolException
 */
public byte[] generalAuthenticate(byte[] key) throws ProtocolException {
    try {
        if (key[0] != (byte) 0x04) {
            key = ByteUtils.concatenate((byte) 0x04, key);
        }
        CardCommandAPDU generalAuthenticate = new GeneralAuthenticate((byte) 0x80, key);
        CardResponseAPDU response = generalAuthenticate.transmit(dispatcher, slotHandle);
        return response.getData();
    } catch (APDUException e) {
        throw new ProtocolException(e.getResult());
    }
}
Also used : CardCommandAPDU(org.openecard.common.apdu.common.CardCommandAPDU) ProtocolException(org.openecard.common.sal.protocol.exception.ProtocolException) APDUException(org.openecard.common.apdu.exception.APDUException) CardResponseAPDU(org.openecard.common.apdu.common.CardResponseAPDU) GeneralAuthenticate(org.openecard.common.apdu.GeneralAuthenticate)

Example 9 with APDUException

use of org.openecard.common.apdu.exception.APDUException in project open-ecard by ecsec.

the class CardCommandAPDU method transmit.

/**
 * Transmit the APDU.
 *
 * @param dispatcher Dispatcher
 * @param slotHandle Slot handle
 * @param responses List of positive responses
 * @return Response APDU
 * @throws APDUException
 */
public CardResponseAPDU transmit(Dispatcher dispatcher, byte[] slotHandle, List<byte[]> responses) throws APDUException {
    Transmit t;
    TransmitResponse tr = null;
    try {
        if (responses != null) {
            t = makeTransmit(slotHandle, responses);
        } else {
            t = makeTransmit(slotHandle);
        }
        tr = (TransmitResponse) dispatcher.safeDeliver(t);
        WSHelper.checkResult(tr);
        CardResponseAPDU responseAPDU = new CardResponseAPDU(tr);
        return responseAPDU;
    } catch (WSException ex) {
        throw new APDUException(ex, tr);
    } catch (Exception ex) {
        throw new APDUException(ex);
    }
}
Also used : Transmit(iso.std.iso_iec._24727.tech.schema.Transmit) APDUException(org.openecard.common.apdu.exception.APDUException) TransmitResponse(iso.std.iso_iec._24727.tech.schema.TransmitResponse) WSException(org.openecard.common.WSHelper.WSException) WSException(org.openecard.common.WSHelper.WSException) IOException(java.io.IOException) APDUException(org.openecard.common.apdu.exception.APDUException)

Example 10 with APDUException

use of org.openecard.common.apdu.exception.APDUException in project open-ecard by ecsec.

the class CardUtils method readFile.

/**
 * Reads a file.
 *
 * @param dispatcher Dispatcher
 * @param slotHandle Slot handle
 * @param fcp File Control Parameters
 * @return File content
 * @throws APDUException
 */
public static byte[] readFile(FCP fcp, Dispatcher dispatcher, byte[] slotHandle) throws APDUException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // Read 255 bytes per APDU
    byte length = (byte) 0xFF;
    // -1 indicates I don't know
    short numToRead = -1;
    if (fcp != null) {
        Long fcpNumBytes = fcp.getNumBytes();
        if (fcpNumBytes != null) {
            // more than short is not possible and besides that very unrealistic
            numToRead = fcpNumBytes.shortValue();
            // reduce readout size
            if (numToRead < 255) {
                length = (byte) numToRead;
            }
        }
    }
    boolean isRecord = isRecordEF(fcp);
    // records start at index 1
    byte i = (byte) (isRecord ? 1 : 0);
    short numRead = 0;
    try {
        CardResponseAPDU response;
        byte[] trailer;
        int lastNumRead = 0;
        boolean goAgain;
        do {
            if (!isRecord) {
                CardCommandAPDU readBinary = new ReadBinary(numRead, length);
                // 0x6A84 code for the estonian identity card. The card returns this code
                // after the last read process.
                response = readBinary.transmit(dispatcher, slotHandle, CardCommandStatus.response(0x9000, 0x6282, 0x6A84, 0x6A83, 0x6A86, 0x6B00));
            } else {
                CardCommandAPDU readRecord = new ReadRecord((byte) i);
                response = readRecord.transmit(dispatcher, slotHandle, CardCommandStatus.response(0x9000, 0x6282, 0x6A84, 0x6A83));
            }
            trailer = response.getTrailer();
            if (!Arrays.equals(trailer, new byte[] { (byte) 0x6A, (byte) 0x84 }) && !Arrays.equals(trailer, new byte[] { (byte) 0x6A, (byte) 0x83 }) && !Arrays.equals(trailer, new byte[] { (byte) 0x6A, (byte) 0x86 })) {
                byte[] data = response.getData();
                // some cards are just pure shit and return 9000 when no bytes have been read
                baos.write(data);
                lastNumRead = data.length;
                numRead += lastNumRead;
            }
            i++;
            // update length value
            goAgain = response.isNormalProcessed() && lastNumRead != 0 || (Arrays.equals(trailer, new byte[] { (byte) 0x62, (byte) 0x82 }) && isRecord);
            if (goAgain && numToRead != -1) {
                // we have a limit, enforce it
                short remainingBytes = (short) (numToRead - numRead);
                if (remainingBytes <= 0) {
                    goAgain = false;
                } else if (remainingBytes < 255) {
                    // update length when we reached the area below 255
                    length = (byte) remainingBytes;
                }
            }
        } while (goAgain);
        baos.close();
    } catch (IOException e) {
        throw new APDUException(e);
    }
    return baos.toByteArray();
}
Also used : CardCommandAPDU(org.openecard.common.apdu.common.CardCommandAPDU) APDUException(org.openecard.common.apdu.exception.APDUException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ReadBinary(org.openecard.common.apdu.ReadBinary) ReadRecord(org.openecard.common.apdu.ReadRecord) CardResponseAPDU(org.openecard.common.apdu.common.CardResponseAPDU)

Aggregations

APDUException (org.openecard.common.apdu.exception.APDUException)18 CardCommandAPDU (org.openecard.common.apdu.common.CardCommandAPDU)12 ProtocolException (org.openecard.common.sal.protocol.exception.ProtocolException)7 CardResponseAPDU (org.openecard.common.apdu.common.CardResponseAPDU)6 GeneralSecurityException (java.security.GeneralSecurityException)5 GeneralAuthenticate (org.openecard.common.apdu.GeneralAuthenticate)5 ProtocolException (org.openecard.common.ifd.protocol.exception.ProtocolException)5 EstablishChannelResponse (iso.std.iso_iec._24727.tech.schema.EstablishChannelResponse)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 StepActionResult (org.openecard.gui.executor.StepActionResult)3 CardApplicationDisconnect (iso.std.iso_iec._24727.tech.schema.CardApplicationDisconnect)2 DestroyChannel (iso.std.iso_iec._24727.tech.schema.DestroyChannel)2 Disconnect (iso.std.iso_iec._24727.tech.schema.Disconnect)2 IOException (java.io.IOException)2 WSHelper (org.openecard.common.WSHelper)2 WSException (org.openecard.common.WSHelper.WSException)2 ExecutionResults (org.openecard.gui.executor.ExecutionResults)2 PACEKey (org.openecard.ifd.protocol.pace.crypto.PACEKey)2 IFDException (org.openecard.ifd.scio.IFDException)2 ControlIFDResponse (iso.std.iso_iec._24727.tech.schema.ControlIFDResponse)1