use of iso.std.iso_iec._24727.tech.schema.HashResponse in project open-ecard by ecsec.
the class MiddlewareSAL method hash.
@Override
public HashResponse hash(Hash parameters) {
HashResponse response = WSHelper.makeResponse(HashResponse.class, WSHelper.makeResultOK());
// bouncy the message because I assume the hash is calculated by the sign function
response.setHash(parameters.getMessage());
return response;
}
use of iso.std.iso_iec._24727.tech.schema.HashResponse in project open-ecard by ecsec.
the class TinySAL method hash.
/**
* The Hash function calculates the hash value of a transmitted message.
* See BSI-TR-03112-4, version 1.1.2, section 3.5.4.
*
* @param request Hash
* @return HashResponse
*/
@Publish
@Override
public HashResponse hash(Hash request) {
HashResponse response = WSHelper.makeResponse(HashResponse.class, WSHelper.makeResultOK());
try {
ConnectionHandleType connectionHandle = SALUtils.getConnectionHandle(request);
CardStateEntry cardStateEntry = SALUtils.getCardStateEntry(states, connectionHandle, false);
byte[] applicationID = cardStateEntry.getCurrentCardApplication().getApplicationIdentifier();
String didName = SALUtils.getDIDName(request);
byte[] message = request.getMessage();
Assert.assertIncorrectParameter(message, "The parameter Message is empty.");
DIDScopeType didScope = request.getDIDScope();
if (didScope == null) {
didScope = DIDScopeType.LOCAL;
}
if (didScope.equals(DIDScopeType.LOCAL)) {
byte[] necesssaryApp = cardStateEntry.getInfo().getApplicationIdByDidName(didName, didScope);
if (!Arrays.equals(necesssaryApp, applicationID)) {
String msg = "Wrong application for executing Hash with the specified DID " + didName + ".";
throw new SecurityConditionNotSatisfiedException(msg);
}
}
DIDStructureType didStructure = cardStateEntry.getDIDStructure(didName, didScope);
Assert.assertNamedEntityNotFound(didStructure, "The given DIDName cannot be found.");
String protocolURI = didStructure.getDIDMarker().getProtocol();
SALProtocol protocol = getProtocol(connectionHandle, request.getDIDScope(), protocolURI);
if (protocol.hasNextStep(FunctionType.Hash)) {
response = protocol.hash(request);
removeFinishedProtocol(connectionHandle, protocolURI, protocol);
} else {
throw new InappropriateProtocolForActionException("Hash", protocol.toString());
}
} 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 iso.std.iso_iec._24727.tech.schema.HashResponse in project open-ecard by ecsec.
the class DidInfo method hash.
public byte[] hash(byte[] data) throws WSHelper.WSException {
if (!isCryptoDid()) {
throw new IllegalStateException("Hash called for a DID which is not a Generic Crypto DID.");
}
Hash hashReq = new Hash();
hashReq.setMessage(data);
hashReq.setDIDName(didTarget.getDIDName());
hashReq.setDIDScope(DIDScopeType.LOCAL);
hashReq.setConnectionHandle(didInfos.getHandle(application));
HashResponse res = (HashResponse) didInfos.getDispatcher().safeDeliver(hashReq);
WSHelper.checkResult(res);
byte[] digest = res.getHash();
return digest;
}
use of iso.std.iso_iec._24727.tech.schema.HashResponse in project open-ecard by ecsec.
the class TinySALTest method testHash.
/**
* Test of hash method, of class TinySAL.
*/
@Test(enabled = TESTS_ENABLED)
public void testHash() {
System.out.println("hash");
Hash parameters = new Hash();
HashResponse result = instance.hash(parameters);
assertEquals(ECardConstants.Major.ERROR, result.getResult().getResultMajor());
}
use of iso.std.iso_iec._24727.tech.schema.HashResponse in project open-ecard by ecsec.
the class HashStep method perform.
@Override
public HashResponse perform(Hash request, Map<String, Object> internalData) {
HashResponse response = WSHelper.makeResponse(HashResponse.class, WSHelper.makeResultOK());
try {
ConnectionHandleType connectionHandle = SALUtils.getConnectionHandle(request);
String didName = SALUtils.getDIDName(request);
CardStateEntry cardStateEntry = SALUtils.getCardStateEntry(internalData, connectionHandle);
DIDStructureType didStructure = SALUtils.getDIDStructure(request, didName, cardStateEntry, connectionHandle);
CryptoMarkerType cryptoMarker = new CryptoMarkerType(didStructure.getDIDMarker());
HashGenerationInfoType hashInfo = cryptoMarker.getHashGenerationInfo();
if (hashInfo != null) {
if (hashInfo == HashGenerationInfoType.NOT_ON_CARD) {
String algId = cryptoMarker.getAlgorithmInfo().getAlgorithmIdentifier().getAlgorithm();
SignatureAlgorithms alg = SignatureAlgorithms.fromAlgId(algId);
HashAlgorithms hashAlg = alg.getHashAlg();
if (hashAlg == null) {
String msg = String.format("Algorithm %s does not specify a Hash algorithm.", algId);
LOG.error(msg);
String minor = ECardConstants.Minor.App.INCORRECT_PARM;
response.setResult(WSHelper.makeResultError(minor, msg));
} else {
// calculate hash
MessageDigest md = MessageDigest.getInstance(hashAlg.getJcaAlg());
md.update(request.getMessage());
byte[] digest = md.digest();
response.setHash(digest);
}
} else {
// TODO: implement hashing on card
String msg = String.format("Unsupported Hash generation type (%s) requested.", hashInfo);
LOG.error(msg);
String minor = ECardConstants.Minor.SAL.INAPPROPRIATE_PROTOCOL_FOR_ACTION;
response.setResult(WSHelper.makeResultError(minor, msg));
}
} else {
// no hash alg specified, this is an error
String msg = String.format("No Hash generation type specified in CIF.");
LOG.error(msg);
String minor = ECardConstants.Minor.SAL.INAPPROPRIATE_PROTOCOL_FOR_ACTION;
response.setResult(WSHelper.makeResultError(minor, msg));
}
} catch (ECardException e) {
response.setResult(e.getResult());
} catch (UnsupportedAlgorithmException | NoSuchAlgorithmException ex) {
} catch (Exception e) {
LOG.warn(e.getMessage(), e);
response.setResult(WSHelper.makeResult(e));
}
return response;
}
Aggregations