Search in sources :

Example 1 with MasterFile

use of org.openecard.common.apdu.Select.MasterFile in project open-ecard by ecsec.

the class CardUtils method selectFileWithOptions.

/**
 * Select a file with different options.
 *
 * @param dispatcher The Dispatcher for dispatching of the card commands.
 * @param slotHandle The SlotHandle which identifies the card terminal.
 * @param fileIdOrPath File identifier or path to the file to select.
 * @param responses List of byte arrays with the trailers which should not thrown as errors.
 * @param resultType Int value which indicates whether the select should be performed with a request of the FCP, FCI,
 * FMD or without any data. There are four public variables available in this class to use.
 * @return A CardResponseAPDU object with the requested response data.
 * @throws APDUException Thrown if the selection of a file failed.
 */
public static CardResponseAPDU selectFileWithOptions(Dispatcher dispatcher, byte[] slotHandle, byte[] fileIdOrPath, List<byte[]> responses, int resultType) throws APDUException {
    Select selectFile;
    CardResponseAPDU result = null;
    // respect the possibility that fileID could be a path
    int i = 0;
    while (i < fileIdOrPath.length) {
        if (fileIdOrPath[i] == (byte) 0x3F && fileIdOrPath[i + 1] == (byte) 0x00 && i == 0 && i + 1 == 1) {
            selectFile = new MasterFile();
            i = i + 2;
        } else if (i == fileIdOrPath.length - 2) {
            selectFile = new Select.ChildFile(new byte[] { fileIdOrPath[i], fileIdOrPath[i + 1] });
            switch(resultType) {
                case 0:
                    // do nothing except of break 0x0C is the initialization value of P2
                    break;
                case 1:
                    selectFile.setFCP();
                    break;
                case 2:
                    selectFile.setFCI();
                    break;
                case 3:
                    selectFile.setFMD();
                    break;
                default:
                    throw new APDUException("There is no value assoziated with the returnType value " + resultType);
            }
            i = i + 2;
        } else {
            selectFile = new Select.ChildDirectory(new byte[] { fileIdOrPath[i], fileIdOrPath[i + 1] });
            i = i + 2;
        }
        if (responses == null) {
            // not all cards, e.g. Estonian id card, support P1 = 00 and DataFile filled with MF Fid so work around this
            if (i == 2 && fileIdOrPath[0] == (byte) 0x3F && fileIdOrPath[1] == (byte) 0x00) {
                responses = new ArrayList<>();
                responses.add(new byte[] { (byte) 0x90, (byte) 0x00 });
                responses.add(new byte[] { (byte) 0x67, (byte) 0x00 });
                responses.add(new byte[] { (byte) 0x6A, (byte) 0x86 });
            }
            result = selectFile.transmit(dispatcher, slotHandle, responses);
            if (!Arrays.equals(result.getTrailer(), new byte[] { (byte) 0x90, (byte) 0x00 }) && i == 2 && fileIdOrPath[0] == (byte) 0x3F && fileIdOrPath[1] == (byte) 0x00) {
                selectFile = new Select((byte) 0x00, (byte) 0x0c);
                result = selectFile.transmit(dispatcher, slotHandle, responses);
                // so lets try selection by path
                if (!Arrays.equals(result.getTrailer(), new byte[] { (byte) 0x90, (byte) 0x00 }) && fileIdOrPath.length > 2) {
                    selectFile = new Select.AbsolutePath(fileIdOrPath);
                    result = selectFile.transmit(dispatcher, slotHandle);
                    if (Arrays.equals(result.getTrailer(), TrailerConstants.Success.OK())) {
                        return result;
                    }
                }
            }
        } else {
            result = selectFile.transmit(dispatcher, slotHandle, responses);
        }
    }
    return result;
}
Also used : APDUException(org.openecard.common.apdu.exception.APDUException) Select(org.openecard.common.apdu.Select) CardResponseAPDU(org.openecard.common.apdu.common.CardResponseAPDU) MasterFile(org.openecard.common.apdu.Select.MasterFile)

Example 2 with MasterFile

use of org.openecard.common.apdu.Select.MasterFile in project open-ecard by ecsec.

the class CardUtils method selectApplicationByFID.

/**
 * Select an application by it's file identifier.
 *
 * @param dispatcher The message dispatcher for the interaction with the card.
 * @param slotHandle
 * @param fileID File identitfier of an application or a path to the application.
 * @return The {@link CardResponseAPDU} from the last select which means the select of the application to select.
 * @throws APDUException
 */
public static CardResponseAPDU selectApplicationByFID(Dispatcher dispatcher, byte[] slotHandle, byte[] fileID) throws APDUException {
    Select selectApp;
    CardResponseAPDU result = null;
    // respect the possibility that fileID could be a path
    int i = 0;
    while (i < fileID.length) {
        if (fileID[i] == (byte) 0x3F && fileID[i + 1] == (byte) 0x00 && i == 0 && i + 1 == 1) {
            selectApp = new MasterFile();
            i = i + 2;
        } else {
            selectApp = new Select.ChildDirectory(new byte[] { fileID[i], fileID[i + 1] });
            selectApp.setLE((byte) 0xFF);
            selectApp.setFCP();
            i = i + 2;
        }
        result = selectApp.transmit(dispatcher, slotHandle);
    }
    return result;
}
Also used : Select(org.openecard.common.apdu.Select) CardResponseAPDU(org.openecard.common.apdu.common.CardResponseAPDU) MasterFile(org.openecard.common.apdu.Select.MasterFile)

Aggregations

Select (org.openecard.common.apdu.Select)2 MasterFile (org.openecard.common.apdu.Select.MasterFile)2 CardResponseAPDU (org.openecard.common.apdu.common.CardResponseAPDU)2 APDUException (org.openecard.common.apdu.exception.APDUException)1