use of org.openecard.common.sal.exception.PrerequisitesNotSatisfiedException in project open-ecard by ecsec.
the class TinySAL method dsiDelete.
/**
* The DSIDelete function deletes a DSI (Data Structure for Interoperability) in the currently selected data set.
* See BSI-TR-03112-4, version 1.1.2, section 3.4.7.
*
* @param request DSIDelete
* @return DSIDeleteResponse
*/
// TODO: rewiew function and add @Publish annotation
@Override
public DSIDeleteResponse dsiDelete(DSIDelete request) {
DSIDeleteResponse response = WSHelper.makeResponse(DSIDeleteResponse.class, WSHelper.makeResultOK());
try {
ConnectionHandleType connectionHandle = SALUtils.getConnectionHandle(request);
CardStateEntry cardStateEntry = SALUtils.getCardStateEntry(states, connectionHandle);
CardInfoWrapper cardInfoWrapper = cardStateEntry.getInfo();
String dsiName = request.getDSIName();
Assert.assertIncorrectParameter(dsiName, "The parameter DSIName is empty.");
if (cardStateEntry.getFCPOfSelectedEF() == null) {
String msg = "No DataSet selected for deleting the DSI " + request.getDSIName();
throw new PrerequisitesNotSatisfiedException(msg);
}
DataSetInfoType dataSet = cardInfoWrapper.getDataSetByDsiName(request.getDSIName());
byte[] fidOrPath = dataSet.getDataSetPath().getEfIdOrPath();
byte[] dataSetFid = new byte[] { fidOrPath[fidOrPath.length - 2], fidOrPath[fidOrPath.length - 1] };
if (!Arrays.equals(dataSetFid, cardStateEntry.getFCPOfSelectedEF().getFileIdentifiers().get(0))) {
String msg = "The wrong DataSet for the deletion of DSI " + request.getDSIName() + " is selected.";
throw new PrerequisitesNotSatisfiedException(msg);
}
DataSetInfoType dSet = cardInfoWrapper.getDataSetByFid(cardStateEntry.getFCPOfSelectedEF().getFileIdentifiers().get(0));
Assert.securityConditionDataSet(cardStateEntry, connectionHandle.getCardApplication(), dSet.getDataSetName(), NamedDataServiceActionName.DSI_DELETE);
DSIType dsi = cardInfoWrapper.getDSIbyName(dsiName);
// We have to define some allowed answers because if the file has an write operation counter we wont get an
// 9000 response.
ArrayList<byte[]> responses = new ArrayList<byte[]>() {
{
add(new byte[] { (byte) 0x90, (byte) 0x00 });
add(new byte[] { (byte) 0x63, (byte) 0xC1 });
add(new byte[] { (byte) 0x63, (byte) 0xC2 });
add(new byte[] { (byte) 0x63, (byte) 0xC3 });
add(new byte[] { (byte) 0x63, (byte) 0xC4 });
add(new byte[] { (byte) 0x63, (byte) 0xC5 });
add(new byte[] { (byte) 0x63, (byte) 0xC6 });
add(new byte[] { (byte) 0x63, (byte) 0xC7 });
add(new byte[] { (byte) 0x63, (byte) 0xC8 });
add(new byte[] { (byte) 0x63, (byte) 0xC9 });
add(new byte[] { (byte) 0x63, (byte) 0xCA });
add(new byte[] { (byte) 0x63, (byte) 0xCB });
add(new byte[] { (byte) 0x63, (byte) 0xCC });
add(new byte[] { (byte) 0x63, (byte) 0xCD });
add(new byte[] { (byte) 0x63, (byte) 0xCE });
add(new byte[] { (byte) 0x63, (byte) 0xCF });
}
};
if (cardStateEntry.getFCPOfSelectedEF().getDataElements().isLinear()) {
EraseRecord rmRecord = new EraseRecord(dsi.getDSIPath().getIndex()[0], EraseRecord.ERASE_JUST_P1);
rmRecord.transmit(env.getDispatcher(), connectionHandle.getSlotHandle(), responses);
} else {
// NOTE: Erase binary allows to erase only everything after the offset or everything in front of the offset.
// currently erasing everything after the offset is used.
EraseBinary rmBinary = new EraseBinary((byte) 0x00, (byte) 0x00, dsi.getDSIPath().getIndex());
rmBinary.transmit(env.getDispatcher(), connectionHandle.getSlotHandle(), responses);
}
} catch (ECardException e) {
LOG.error(e.getMessage(), e);
response.setResult(e.getResult());
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throwThreadKillException(e);
response.setResult(WSHelper.makeResult(e));
}
return response;
}
use of org.openecard.common.sal.exception.PrerequisitesNotSatisfiedException in project open-ecard by ecsec.
the class TinySAL method dsiRead.
/**
* The DSIRead function reads out the content of a specific DSI (Data Structure for Interoperability).
* See BSI-TR-03112-4, version 1.1.2, section 3.4.9.
*
* @param request DSIRead
* @return DSIReadResponse
*/
@Publish
@Override
public DSIReadResponse dsiRead(DSIRead request) {
DSIReadResponse response = WSHelper.makeResponse(DSIReadResponse.class, WSHelper.makeResultOK());
try {
ConnectionHandleType connectionHandle = SALUtils.getConnectionHandle(request);
CardStateEntry cardStateEntry = SALUtils.getCardStateEntry(states, connectionHandle);
byte[] applicationID = cardStateEntry.getCurrentCardApplication().getApplicationIdentifier();
String dsiName = request.getDSIName();
byte[] slotHandle = connectionHandle.getSlotHandle();
Assert.assertIncorrectParameter(dsiName, "The parameter DSIName is empty.");
Assert.securityConditionDataSet(cardStateEntry, applicationID, dsiName, NamedDataServiceActionName.DSI_READ);
if (cardStateEntry.getFCPOfSelectedEF() == null) {
throw new PrerequisitesNotSatisfiedException("No DataSet to read selected.");
}
CardInfoWrapper cardInfoWrapper = cardStateEntry.getInfo();
DataSetInfoType dataSetInfo = cardInfoWrapper.getDataSetByDsiName(dsiName);
if (dataSetInfo == null) {
// there is no data set which contains the given dsi name so the name should be an data set name
dataSetInfo = cardInfoWrapper.getDataSetByName(dsiName);
if (dataSetInfo != null) {
if (!cardStateEntry.getFCPOfSelectedEF().getFileIdentifiers().isEmpty()) {
byte[] path = dataSetInfo.getDataSetPath().getEfIdOrPath();
byte[] fid = Arrays.copyOfRange(path, path.length - 2, path.length);
if (!Arrays.equals(fid, cardStateEntry.getFCPOfSelectedEF().getFileIdentifiers().get(0))) {
String msg = "Wrong DataSet for reading the DSI " + dsiName + " is selected.";
throw new PrerequisitesNotSatisfiedException(msg);
}
}
byte[] fileContent = CardUtils.readFile(cardStateEntry.getFCPOfSelectedEF(), env.getDispatcher(), slotHandle);
response.setDSIContent(fileContent);
} else {
String msg = "The given DSIName does not related to any know DSI or DataSet.";
throw new IncorrectParameterException(msg);
}
} else {
// There exists a data set with the given dsi name
// check whether the correct file is selected
byte[] dataSetPath = dataSetInfo.getDataSetPath().getEfIdOrPath();
byte[] dataSetFID = new byte[] { dataSetPath[dataSetPath.length - 2], dataSetPath[dataSetPath.length - 1] };
if (Arrays.equals(dataSetFID, cardStateEntry.getFCPOfSelectedEF().getFileIdentifiers().get(0))) {
DSIType dsi = cardInfoWrapper.getDSIbyName(dsiName);
PathType dsiPath = dsi.getDSIPath();
if (dsiPath.getTagRef() != null) {
TagRef tagReference = dsiPath.getTagRef();
byte[] tag = tagReference.getTag();
GetData getDataRequest;
if (tag.length == 2) {
getDataRequest = new GetData(GetData.INS_DATA, tag[0], tag[1]);
CardResponseAPDU cardResponse = getDataRequest.transmit(env.getDispatcher(), slotHandle, Collections.EMPTY_LIST);
byte[] responseData = cardResponse.getData();
while (cardResponse.getTrailer()[0] == (byte) 0x61) {
GetResponse allData = new GetResponse();
cardResponse = allData.transmit(env.getDispatcher(), slotHandle, Collections.EMPTY_LIST);
responseData = ByteUtils.concatenate(responseData, cardResponse.getData());
}
response.setDSIContent(responseData);
} else if (tag.length == 1) {
// how to determine Simple- or BER-TLV in this case correctly?
// Now try Simple-TLV first and if it fail try BER-TLV
getDataRequest = new GetData(GetData.INS_DATA, GetData.SIMPLE_TLV, tag[0]);
CardResponseAPDU cardResponse = getDataRequest.transmit(env.getDispatcher(), slotHandle, Collections.EMPTY_LIST);
byte[] responseData = cardResponse.getData();
// just an assumption
if (Arrays.equals(cardResponse.getTrailer(), new byte[] { (byte) 0x6A, (byte) 0x88 })) {
getDataRequest = new GetData(GetData.INS_DATA, GetData.BER_TLV_ONE_BYTE, tag[0]);
cardResponse = getDataRequest.transmit(env.getDispatcher(), slotHandle, Collections.EMPTY_LIST);
responseData = cardResponse.getData();
}
while (cardResponse.getTrailer()[0] == (byte) 0x61) {
GetResponse allData = new GetResponse();
cardResponse = allData.transmit(env.getDispatcher(), slotHandle, Collections.EMPTY_LIST);
responseData = ByteUtils.concatenate(responseData, cardResponse.getData());
}
response.setDSIContent(responseData);
}
} else if (dsiPath.getIndex() != null) {
byte[] index = dsiPath.getIndex();
byte[] length = dsiPath.getLength();
List<byte[]> allowedResponse = new ArrayList<>();
allowedResponse.add(new byte[] { (byte) 0x90, (byte) 0x00 });
allowedResponse.add(new byte[] { (byte) 0x62, (byte) 0x82 });
if (cardStateEntry.getFCPOfSelectedEF().getDataElements().isLinear()) {
// in this case we use the index as record number and the length as length of record
ReadRecord readRecord = new ReadRecord(index[0]);
// NOTE: For record based files TR-0312-4 states to ignore the length field in case of records
CardResponseAPDU cardResponse = readRecord.transmit(env.getDispatcher(), slotHandle, allowedResponse);
response.setDSIContent(cardResponse.getData());
} else {
// in this case we use index as offset and length as the expected length
ReadBinary readBinary = new ReadBinary(ByteUtils.toShort(index), ByteUtils.toShort(length));
CardResponseAPDU cardResponse = readBinary.transmit(env.getDispatcher(), slotHandle, allowedResponse);
response.setDSIContent(cardResponse.getData());
}
} else {
String msg = "The currently selected data set does not contain the DSI with the name " + dsiName;
throw new PrerequisitesNotSatisfiedException(msg);
}
}
}
} catch (ECardException e) {
response.setResult(e.getResult());
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throwThreadKillException(e);
response.setResult(WSHelper.makeResult(e));
}
return response;
}
use of org.openecard.common.sal.exception.PrerequisitesNotSatisfiedException in project open-ecard by ecsec.
the class TinySAL method dsiWrite.
/**
* The DSIWrite function changes the content of a DSI (Data Structure for Interoperability).
* See BSI-TR-03112-4, version 1.1.2, section 3.4.8.
* For clarification this method updates an existing DSI and does not create a new one.
*
* The precondition for this method is that a connection to a card application was established and a data set was
* selected. Furthermore the DSI exists already.
*
* @param request DSIWrite
* @return DSIWriteResponse
*/
@Publish
@Override
public DSIWriteResponse dsiWrite(DSIWrite request) {
DSIWriteResponse response = WSHelper.makeResponse(DSIWriteResponse.class, WSHelper.makeResultOK());
try {
ConnectionHandleType connectionHandle = SALUtils.getConnectionHandle(request);
CardStateEntry cardStateEntry = SALUtils.getCardStateEntry(states, connectionHandle);
byte[] applicationID = connectionHandle.getCardApplication();
String dsiName = request.getDSIName();
byte[] updateData = request.getDSIContent();
Assert.assertIncorrectParameter(dsiName, "The parameter DSIName is empty.");
Assert.assertIncorrectParameter(updateData, "The parameter DSIContent is empty.");
CardInfoWrapper cardInfoWrapper = cardStateEntry.getInfo();
DataSetInfoType dataSetInfo = cardInfoWrapper.getDataSetByDsiName(dsiName);
DSIType dsi = cardInfoWrapper.getDSIbyName(dsiName);
Assert.assertNamedEntityNotFound(dataSetInfo, "The given DSIName cannot be found.");
Assert.securityConditionDataSet(cardStateEntry, applicationID, dsiName, NamedDataServiceActionName.DSI_WRITE);
if (cardStateEntry.getFCPOfSelectedEF() == null) {
throw new PrerequisitesNotSatisfiedException("No EF with DSI selected.");
}
if (!Arrays.equals(dataSetInfo.getDataSetPath().getEfIdOrPath(), cardStateEntry.getFCPOfSelectedEF().getFileIdentifiers().get(0))) {
String msg = "The currently selected data set does not contain the DSI to be updated.";
throw new PrerequisitesNotSatisfiedException(msg);
}
byte[] slotHandle = connectionHandle.getSlotHandle();
if (cardStateEntry.getFCPOfSelectedEF().getDataElements().isTransparent()) {
// currently assuming that the index encodes the offset
byte[] index = dsi.getDSIPath().getIndex();
UpdateBinary updateBin = new UpdateBinary(index[0], index[1], updateData);
updateBin.transmit(env.getDispatcher(), slotHandle);
} else {
// currently assuming that the index encodes the record number
byte index = dsi.getDSIPath().getIndex()[0];
UpdateRecord updateRec = new UpdateRecord(index, updateData);
updateRec.transmit(env.getDispatcher(), slotHandle);
}
} catch (ECardException e) {
response.setResult(e.getResult());
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throwThreadKillException(e);
response.setResult(WSHelper.makeResult(e));
}
return response;
}
use of org.openecard.common.sal.exception.PrerequisitesNotSatisfiedException in project open-ecard by ecsec.
the class TinySAL method dsiList.
/**
* The function DSIList supplies the list of the DSI (Data Structure for Interoperability) which exist in the
* selected data set.
* See BSI-TR-03112-4, version 1.1.2, section 3.4.5. <br>
* <br>
* Prerequisites: <br>
* - a connection to a card application has been established <br>
* - a data set has been selected <br>
*
* @param request DSIList
* @return DSIListResponse
*/
@Publish
@Override
public DSIListResponse dsiList(DSIList request) {
DSIListResponse response = WSHelper.makeResponse(DSIListResponse.class, WSHelper.makeResultOK());
try {
ConnectionHandleType connectionHandle = SALUtils.getConnectionHandle(request);
CardStateEntry cardStateEntry = SALUtils.getCardStateEntry(states, connectionHandle, false);
CardInfoWrapper cardInfoWrapper = cardStateEntry.getInfo();
byte[] cardApplicationID = connectionHandle.getCardApplication();
if (cardStateEntry.getFCPOfSelectedEF() == null) {
throw new PrerequisitesNotSatisfiedException("No EF selected.");
}
DataSetInfoType dataSet = cardInfoWrapper.getDataSetByFid(cardStateEntry.getFCPOfSelectedEF().getFileIdentifiers().get(0));
Assert.securityConditionDataSet(cardStateEntry, cardApplicationID, dataSet.getDataSetName(), NamedDataServiceActionName.DSI_LIST);
DSINameListType dsiNameList = new DSINameListType();
for (DSIType dsi : dataSet.getDSI()) {
dsiNameList.getDSIName().add(dsi.getDSIName());
}
response.setDSINameList(dsiNameList);
} catch (ECardException e) {
response.setResult(e.getResult());
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throwThreadKillException(e);
response.setResult(WSHelper.makeResult(e));
}
return response;
}
use of org.openecard.common.sal.exception.PrerequisitesNotSatisfiedException in project open-ecard by ecsec.
the class TinySAL method dsiCreate.
/**
* The DSICreate function creates a DSI (Data Structure for Interoperability) in the currently selected data set.
* See BSI-TR-03112-4, version 1.1.2, section 3.4.6.
* <br>
* <br>
* Preconditions: <br>
* - Connection to a card application established via CardApplicationConnect <br>
* - A data set has been selected with DataSetSelect <br>
* - The DSI does not exist in the data set. <br>
*
* @param request DSICreate
* @return DSICreateResponse
*/
@Override
public DSICreateResponse dsiCreate(DSICreate request) {
DSICreateResponse response = WSHelper.makeResponse(DSICreateResponse.class, WSHelper.makeResultOK());
try {
ConnectionHandleType connectionHandle = SALUtils.getConnectionHandle(request);
CardStateEntry cardStateEntry = SALUtils.getCardStateEntry(states, connectionHandle);
CardInfoWrapper cardInfoWrapper = cardStateEntry.getInfo();
byte[] cardApplicationID = connectionHandle.getCardApplication();
byte[] dsiContent = request.getDSIContent();
Assert.assertIncorrectParameter(dsiContent, "The parameter DSIContent is empty.");
String dsiName = request.getDSIName();
Assert.assertIncorrectParameter(dsiName, "The parameter DSIName is empty.");
DSIType dsi = cardInfoWrapper.getDSIbyName(dsiName);
if (dsi != null) {
throw new NameExistsException("There is already an DSI with the name " + dsiName + " in the current EF.");
}
byte[] slotHandle = connectionHandle.getSlotHandle();
if (cardStateEntry.getFCPOfSelectedEF() == null) {
throw new PrerequisitesNotSatisfiedException("No data set for writing selected.");
} else {
DataSetInfoType dataSet = cardInfoWrapper.getDataSetByFid(cardStateEntry.getFCPOfSelectedEF().getFileIdentifiers().get(0));
Assert.securityConditionDataSet(cardStateEntry, cardApplicationID, dataSet.getDataSetName(), NamedDataServiceActionName.DSI_CREATE);
DataElements dElements = cardStateEntry.getFCPOfSelectedEF().getDataElements();
if (dElements.isTransparent()) {
WriteBinary writeBin = new WriteBinary(WriteBinary.INS_WRITE_BINARY_DATA, (byte) 0x00, (byte) 0x00, dsiContent);
writeBin.transmit(env.getDispatcher(), slotHandle);
} else if (dElements.isCyclic()) {
WriteRecord writeRec = new WriteRecord((byte) 0x00, WriteRecord.WRITE_PREVIOUS, dsiContent);
writeRec.transmit(env.getDispatcher(), slotHandle);
} else {
WriteRecord writeRec = new WriteRecord((byte) 0x00, WriteRecord.WRITE_LAST, dsiContent);
writeRec.transmit(env.getDispatcher(), slotHandle);
}
}
} catch (ECardException e) {
response.setResult(e.getResult());
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throwThreadKillException(e);
response.setResult(WSHelper.makeResult(e));
}
return response;
}
Aggregations