Search in sources :

Example 1 with FCP

use of org.openecard.common.tlv.iso7816.FCP in project open-ecard by ecsec.

the class TinySAL method dataSetSelect.

/**
 * The DataSetSelect function selects a data set in a card application.
 * See BSI-TR-03112-4, version 1.1.2, section 3.4.3.
 *
 * @param request DataSetSelect
 * @return DataSetSelectResponse
 */
@Publish
@Override
public DataSetSelectResponse dataSetSelect(DataSetSelect request) {
    DataSetSelectResponse response = WSHelper.makeResponse(DataSetSelectResponse.class, WSHelper.makeResultOK());
    try {
        ConnectionHandleType connectionHandle = SALUtils.getConnectionHandle(request);
        CardStateEntry cardStateEntry = SALUtils.getCardStateEntry(states, connectionHandle);
        byte[] applicationID = connectionHandle.getCardApplication();
        String dataSetName = request.getDataSetName();
        Assert.assertIncorrectParameter(dataSetName, "The parameter DataSetName is empty.");
        CardInfoWrapper cardInfoWrapper = cardStateEntry.getInfo();
        DataSetInfoType dataSetInfo = cardInfoWrapper.getDataSet(dataSetName, applicationID);
        Assert.assertNamedEntityNotFound(dataSetInfo, "The given DataSet cannot be found.");
        Assert.securityConditionDataSet(cardStateEntry, applicationID, dataSetName, NamedDataServiceActionName.DATA_SET_SELECT);
        byte[] fileID = dataSetInfo.getDataSetPath().getEfIdOrPath();
        byte[] slotHandle = connectionHandle.getSlotHandle();
        CardResponseAPDU result = CardUtils.selectFileWithOptions(env.getDispatcher(), slotHandle, fileID, null, CardUtils.FCP_RESPONSE_DATA);
        FCP fcp = null;
        if (result != null && result.getData().length > 0) {
            try {
                fcp = new FCP(result.getData());
            } catch (TLVException ex) {
                LOG.warn("Invalid FCP received.");
            }
        }
        if (fcp == null) {
            LOG.info("Using fake FCP.");
            fcp = new FCP(createFakeFCP(Arrays.copyOfRange(fileID, fileID.length - 2, fileID.length)));
        }
        cardStateEntry.setFCPOfSelectedEF(fcp);
    } catch (ECardException e) {
        response.setResult(e.getResult());
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        throwThreadKillException(e);
        response.setResult(WSHelper.makeResult(e));
    }
    return response;
}
Also used : ConnectionHandleType(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType) ECardException(org.openecard.common.ECardException) CardStateEntry(org.openecard.common.sal.state.CardStateEntry) FCP(org.openecard.common.tlv.iso7816.FCP) CardInfoWrapper(org.openecard.common.sal.state.cif.CardInfoWrapper) DataSetInfoType(iso.std.iso_iec._24727.tech.schema.DataSetInfoType) CardResponseAPDU(org.openecard.common.apdu.common.CardResponseAPDU) TLVException(org.openecard.common.tlv.TLVException) PrerequisitesNotSatisfiedException(org.openecard.common.sal.exception.PrerequisitesNotSatisfiedException) NameExistsException(org.openecard.common.sal.exception.NameExistsException) AddonNotFoundException(org.openecard.addon.AddonNotFoundException) ThreadTerminateException(org.openecard.common.ThreadTerminateException) ECardException(org.openecard.common.ECardException) NamedEntityNotFoundException(org.openecard.common.sal.exception.NamedEntityNotFoundException) UnknownProtocolException(org.openecard.common.sal.exception.UnknownProtocolException) IncorrectParameterException(org.openecard.common.sal.exception.IncorrectParameterException) InappropriateProtocolForActionException(org.openecard.common.sal.exception.InappropriateProtocolForActionException) TLVException(org.openecard.common.tlv.TLVException) SecurityConditionNotSatisfiedException(org.openecard.common.sal.exception.SecurityConditionNotSatisfiedException) UnknownConnectionHandleException(org.openecard.common.sal.exception.UnknownConnectionHandleException) DataSetSelectResponse(iso.std.iso_iec._24727.tech.schema.DataSetSelectResponse) Publish(org.openecard.common.interfaces.Publish)

Example 2 with FCP

use of org.openecard.common.tlv.iso7816.FCP 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 3 with FCP

use of org.openecard.common.tlv.iso7816.FCP in project open-ecard by ecsec.

the class CardUtils method readFile.

/**
 * Selects and reads a file.
 *
 * @param dispatcher Dispatcher
 * @param slotHandle Slot handle
 * @param fileID File ID
 * @return File content
 * @throws APDUException
 */
@Deprecated
public static byte[] readFile(Dispatcher dispatcher, byte[] slotHandle, byte[] fileID) throws APDUException {
    CardResponseAPDU selectResponse = selectFileWithOptions(dispatcher, slotHandle, fileID, null, FCP_RESPONSE_DATA);
    FCP fcp = null;
    try {
        fcp = new FCP(selectResponse.getData());
    } catch (TLVException e) {
        LOG.warn("Couldn't get File Control Parameters from Select response.", e);
    }
    return readFile(fcp, dispatcher, slotHandle);
}
Also used : FCP(org.openecard.common.tlv.iso7816.FCP) CardResponseAPDU(org.openecard.common.apdu.common.CardResponseAPDU) TLVException(org.openecard.common.tlv.TLVException)

Example 4 with FCP

use of org.openecard.common.tlv.iso7816.FCP in project open-ecard by ecsec.

the class CardUtils method writeFile.

public static void writeFile(Dispatcher dispatcher, byte[] slotHandle, byte[] fileID, byte[] data) throws APDUException {
    CardResponseAPDU selectResponse = selectFile(dispatcher, slotHandle, fileID);
    FCP fcp = null;
    try {
        fcp = new FCP(selectResponse.getData());
    } catch (TLVException e) {
        LOG.warn("Couldn't get File Control Parameters from Select response.", e);
    }
    writeFile(fcp, dispatcher, slotHandle, data);
}
Also used : FCP(org.openecard.common.tlv.iso7816.FCP) CardResponseAPDU(org.openecard.common.apdu.common.CardResponseAPDU) TLVException(org.openecard.common.tlv.TLVException)

Example 5 with FCP

use of org.openecard.common.tlv.iso7816.FCP in project open-ecard by ecsec.

the class PACEProtocol method establish.

@Override
public EstablishChannelResponse establish(EstablishChannel req, Dispatcher dispatcher, UserConsent gui) {
    EstablishChannelResponse response = new EstablishChannelResponse();
    try {
        // Get parameters for the PACE protocol
        PACEInputType paceInput = new PACEInputType(req.getAuthenticationProtocolData());
        byte[] pin;
        byte pinID = paceInput.getPINID();
        byte[] chat = paceInput.getCHAT();
        if (paceInput.getPIN() == null || paceInput.getPIN().isEmpty()) {
            // GUI request
            GUIContentMap content = new GUIContentMap();
            content.add(GUIContentMap.ELEMENT.PIN_ID, pinID);
            PACEUserConsent paceUserConsent = new PACEUserConsent(gui);
            paceUserConsent.show(content);
            pin = ((String) content.get(GUIContentMap.ELEMENT.PIN)).getBytes(PACEConstants.PIN_CHARSET);
        } else {
            pin = paceInput.getPIN().getBytes(PACEConstants.PIN_CHARSET);
        }
        if (pin == null || pin.length == 0) {
            response.setResult(WSHelper.makeResultError(ECardConstants.Minor.IFD.CANCELLATION_BY_USER, "No PIN was entered."));
            return response;
        }
        // Read EF.CardAccess from card
        byte[] slotHandle = req.getSlotHandle();
        CardResponseAPDU resp = CardUtils.selectFileWithOptions(dispatcher, slotHandle, ShortUtils.toByteArray(PACEConstants.EF_CARDACCESS_FID), null, CardUtils.FCP_RESPONSE_DATA);
        FCP efCardAccessFCP = new FCP(TLV.fromBER(resp.getData()));
        byte[] efcadata = CardUtils.readFile(efCardAccessFCP, dispatcher, slotHandle);
        // Parse SecurityInfos and get PACESecurityInfos
        SecurityInfos sis = SecurityInfos.getInstance(efcadata);
        EFCardAccess efca = new EFCardAccess(sis);
        PACESecurityInfos psi = efca.getPACESecurityInfos();
        // Start PACE
        PACEImplementation pace = new PACEImplementation(dispatcher, slotHandle, psi);
        pace.execute(pin, pinID, chat);
        // Establish Secure Messaging channel
        sm = new SecureMessaging(pace.getKeyMAC(), pace.getKeyENC());
        // Create AuthenticationProtocolData (PACEOutputType)
        PACEOutputType paceOutput = paceInput.getOutputType();
        paceOutput.setEFCardAccess(efcadata);
        paceOutput.setCurrentCAR(pace.getCurrentCAR());
        paceOutput.setPreviousCAR(pace.getPreviousCAR());
        paceOutput.setIDPICC(pace.getIDPICC());
        paceOutput.setRetryCounter(pace.getRetryCounter());
        // Create EstablishChannelResponse
        response.setResult(WSHelper.makeResultOK());
        response.setAuthenticationProtocolData(paceOutput.getAuthDataType());
    } catch (UnsupportedEncodingException ex) {
        logger.error(ex.getMessage(), ex);
        response.setResult(WSHelper.makeResultError(ECardConstants.Minor.IFD.IO.UNKNOWN_PIN_FORMAT, "Cannot encode the PIN in " + PACEConstants.PIN_CHARSET + " charset."));
    } catch (ProtocolException ex) {
        logger.error(ex.getMessage(), ex);
        response.setResult(WSHelper.makeResult(ex));
    } catch (Throwable ex) {
        logger.error(ex.getMessage(), ex);
        response.setResult(WSHelper.makeResult(ex));
    }
    return response;
}
Also used : ProtocolException(org.openecard.common.ifd.protocol.exception.ProtocolException) EstablishChannelResponse(iso.std.iso_iec._24727.tech.schema.EstablishChannelResponse) EFCardAccess(org.openecard.crypto.common.asn1.eac.ef.EFCardAccess) PACESecurityInfos(org.openecard.crypto.common.asn1.eac.PACESecurityInfos) SecurityInfos(org.openecard.crypto.common.asn1.eac.SecurityInfos) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PACEInputType(org.openecard.common.ifd.anytype.PACEInputType) FCP(org.openecard.common.tlv.iso7816.FCP) PACESecurityInfos(org.openecard.crypto.common.asn1.eac.PACESecurityInfos) PACEOutputType(org.openecard.common.ifd.anytype.PACEOutputType) CardResponseAPDU(org.openecard.common.apdu.common.CardResponseAPDU) GUIContentMap(org.openecard.ifd.protocol.pace.gui.GUIContentMap)

Aggregations

CardResponseAPDU (org.openecard.common.apdu.common.CardResponseAPDU)5 FCP (org.openecard.common.tlv.iso7816.FCP)5 TLVException (org.openecard.common.tlv.TLVException)4 ConnectionHandleType (iso.std.iso_iec._24727.tech.schema.ConnectionHandleType)1 DataSetInfoType (iso.std.iso_iec._24727.tech.schema.DataSetInfoType)1 DataSetSelectResponse (iso.std.iso_iec._24727.tech.schema.DataSetSelectResponse)1 EstablishChannelResponse (iso.std.iso_iec._24727.tech.schema.EstablishChannelResponse)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 AddonNotFoundException (org.openecard.addon.AddonNotFoundException)1 ECardException (org.openecard.common.ECardException)1 ThreadTerminateException (org.openecard.common.ThreadTerminateException)1 APDUException (org.openecard.common.apdu.exception.APDUException)1 PACEInputType (org.openecard.common.ifd.anytype.PACEInputType)1 PACEOutputType (org.openecard.common.ifd.anytype.PACEOutputType)1 ProtocolException (org.openecard.common.ifd.protocol.exception.ProtocolException)1 Publish (org.openecard.common.interfaces.Publish)1 InappropriateProtocolForActionException (org.openecard.common.sal.exception.InappropriateProtocolForActionException)1 IncorrectParameterException (org.openecard.common.sal.exception.IncorrectParameterException)1 NameExistsException (org.openecard.common.sal.exception.NameExistsException)1 NamedEntityNotFoundException (org.openecard.common.sal.exception.NamedEntityNotFoundException)1