use of iso.std.iso_iec._24727.tech.schema.TransmitResponse in project open-ecard by ecsec.
the class DIDAuthenticateStep method perform.
@Override
public DIDAuthenticateResponse perform(DIDAuthenticate request, Map<String, Object> internalData) {
DIDAuthenticateResponse response = WSHelper.makeResponse(DIDAuthenticateResponse.class, WSHelper.makeResultOK());
char[] rawPIN = null;
try {
ConnectionHandleType connectionHandle = SALUtils.getConnectionHandle(request);
String didName = SALUtils.getDIDName(request);
CardStateEntry cardStateEntry = SALUtils.getCardStateEntry(internalData, connectionHandle);
PINCompareDIDAuthenticateInputType pinCompareInput = new PINCompareDIDAuthenticateInputType(request.getAuthenticationProtocolData());
PINCompareDIDAuthenticateOutputType pinCompareOutput = pinCompareInput.getOutputType();
byte[] cardApplication;
if (request.getDIDScope() != null && request.getDIDScope().equals(DIDScopeType.GLOBAL)) {
cardApplication = cardStateEntry.getInfo().getApplicationIdByDidName(request.getDIDName(), request.getDIDScope());
} else {
cardApplication = connectionHandle.getCardApplication();
}
Assert.securityConditionDID(cardStateEntry, cardApplication, didName, DifferentialIdentityServiceActionName.DID_AUTHENTICATE);
DIDStructureType didStructure = cardStateEntry.getDIDStructure(didName, cardApplication);
PINCompareMarkerType pinCompareMarker = new PINCompareMarkerType(didStructure.getDIDMarker());
byte keyRef = pinCompareMarker.getPINRef().getKeyRef()[0];
byte[] slotHandle = connectionHandle.getSlotHandle();
PasswordAttributesType attributes = pinCompareMarker.getPasswordAttributes();
rawPIN = pinCompareInput.getPIN();
// delete pin from memory of the structure
pinCompareInput.setPIN(null);
byte[] template = new byte[] { 0x00, 0x20, 0x00, keyRef };
byte[] responseCode;
// with [ISO7816-4] (Section 7.5.6).
if (rawPIN == null || rawPIN.length == 0) {
VerifyUser verify = new VerifyUser();
verify.setSlotHandle(slotHandle);
InputUnitType inputUnit = new InputUnitType();
verify.setInputUnit(inputUnit);
PinInputType pinInput = new PinInputType();
inputUnit.setPinInput(pinInput);
pinInput.setIndex(BigInteger.ZERO);
pinInput.setPasswordAttributes(attributes);
verify.setTemplate(template);
VerifyUserResponse verifyR = (VerifyUserResponse) dispatcher.safeDeliver(verify);
WSHelper.checkResult(verifyR);
responseCode = verifyR.getResponse();
} else {
Transmit verifyTransmit = PINUtils.buildVerifyTransmit(rawPIN, attributes, template, slotHandle);
try {
TransmitResponse transResp = (TransmitResponse) dispatcher.safeDeliver(verifyTransmit);
WSHelper.checkResult(transResp);
responseCode = transResp.getOutputAPDU().get(0);
} finally {
// blank PIN APDU
for (InputAPDUInfoType apdu : verifyTransmit.getInputAPDUInfo()) {
byte[] rawApdu = apdu.getInputAPDU();
if (rawApdu != null) {
java.util.Arrays.fill(rawApdu, (byte) 0);
}
}
}
}
CardResponseAPDU verifyResponseAPDU = new CardResponseAPDU(responseCode);
if (verifyResponseAPDU.isWarningProcessed()) {
pinCompareOutput.setRetryCounter(new BigInteger(Integer.toString((verifyResponseAPDU.getSW2() & 0x0F))));
}
cardStateEntry.addAuthenticated(didName, cardApplication);
response.setAuthenticationProtocolData(pinCompareOutput.getAuthDataType());
} catch (ECardException e) {
LOG.error(e.getMessage(), e);
response.setResult(e.getResult());
} catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
LOG.error(e.getMessage(), e);
response.setResult(WSHelper.makeResult(e));
} finally {
if (rawPIN != null) {
Arrays.fill(rawPIN, ' ');
}
}
return response;
}
use of iso.std.iso_iec._24727.tech.schema.TransmitResponse 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);
}
}
use of iso.std.iso_iec._24727.tech.schema.TransmitResponse in project open-ecard by ecsec.
the class WSHelper method checkResult.
public static <T extends ResponseBaseType> T checkResult(@Nonnull T response) throws WSException {
Result r = response.getResult();
if (r.getResultMajor().equals(ECardConstants.Major.ERROR)) {
if (response instanceof TransmitResponse) {
TransmitResponse tr = (TransmitResponse) response;
List<byte[]> rApdus = tr.getOutputAPDU();
if (rApdus.size() < 1) {
throw new WSException(r);
} else {
byte[] apdu = CardResponseAPDU.getTrailer(rApdus.get(rApdus.size() - 1));
String msg = CardCommandStatus.getMessage(apdu);
throw new WSException(msg);
}
} else {
throw new WSException(r);
}
}
return response;
}
use of iso.std.iso_iec._24727.tech.schema.TransmitResponse in project open-ecard by ecsec.
the class AbstractPINAction method recognizeState.
/**
* Recognize the PIN state of the card given through the connection handle.
*
* @param cHandle The connection handle for the card for which the pin state should be recognized.
* @return The recognized State (may be {@code RecognizedState.UNKNOWN}).
*/
protected RecognizedState recognizeState(ConnectionHandleType cHandle) {
Transmit t = new Transmit();
t.setSlotHandle(cHandle.getSlotHandle());
InputAPDUInfoType inputAPDU = new InputAPDUInfoType();
inputAPDU.setInputAPDU(RECOGNIZE_APDU);
t.getInputAPDUInfo().add(inputAPDU);
TransmitResponse response = (TransmitResponse) dispatcher.safeDeliver(t);
byte[] responseAPDU = response.getOutputAPDU().get(0);
RecognizedState state;
if (ByteUtils.compare(RESPONSE_RC3, responseAPDU)) {
state = RecognizedState.PIN_activated_RC3;
} else if (ByteUtils.compare(RESPONSE_DEACTIVATED, responseAPDU)) {
state = RecognizedState.PIN_deactivated;
} else if (ByteUtils.compare(RESPONSE_RC2, responseAPDU)) {
state = RecognizedState.PIN_activated_RC2;
} else if (ByteUtils.compare(RESPONSE_SUSPENDED, responseAPDU)) {
state = RecognizedState.PIN_suspended;
} else if (ByteUtils.compare(RESPONSE_BLOCKED, responseAPDU)) {
state = RecognizedState.PIN_blocked;
} else {
LOG.error("Unhandled response to the PIN state recognition APDU: {}\n");
state = RecognizedState.UNKNOWN;
}
LOG.info("State of the PIN: {}.", state);
return state;
}
use of iso.std.iso_iec._24727.tech.schema.TransmitResponse in project open-ecard by ecsec.
the class CardRecognitionImpl method transmit.
private byte[] transmit(byte[] slotHandle, byte[] input, List<ResponseAPDUType> results) {
Transmit t = new Transmit();
t.setSlotHandle(slotHandle);
InputAPDUInfoType apdu = new InputAPDUInfoType();
apdu.setInputAPDU(input);
for (ResponseAPDUType result : results) {
apdu.getAcceptableStatusCode().add(result.getTrailer());
}
t.getInputAPDUInfo().add(apdu);
TransmitResponse r = (TransmitResponse) env.getDispatcher().safeDeliver(t);
if (checkTransmitResult(r)) {
return r.getOutputAPDU().get(0);
} else {
return null;
}
}
Aggregations