Search in sources :

Example 1 with HashResponse

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;
}
Also used : HashResponse(iso.std.iso_iec._24727.tech.schema.HashResponse)

Example 2 with HashResponse

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;
}
Also used : ConnectionHandleType(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType) InappropriateProtocolForActionException(org.openecard.common.sal.exception.InappropriateProtocolForActionException) ECardException(org.openecard.common.ECardException) CardStateEntry(org.openecard.common.sal.state.CardStateEntry) HashResponse(iso.std.iso_iec._24727.tech.schema.HashResponse) SecurityConditionNotSatisfiedException(org.openecard.common.sal.exception.SecurityConditionNotSatisfiedException) SALProtocol(org.openecard.addon.sal.SALProtocol) DIDScopeType(iso.std.iso_iec._24727.tech.schema.DIDScopeType) DIDStructureType(iso.std.iso_iec._24727.tech.schema.DIDStructureType) 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) Publish(org.openecard.common.interfaces.Publish)

Example 3 with HashResponse

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;
}
Also used : HashResponse(iso.std.iso_iec._24727.tech.schema.HashResponse) Hash(iso.std.iso_iec._24727.tech.schema.Hash)

Example 4 with HashResponse

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());
}
Also used : HashResponse(iso.std.iso_iec._24727.tech.schema.HashResponse) Hash(iso.std.iso_iec._24727.tech.schema.Hash) Test(org.testng.annotations.Test)

Example 5 with HashResponse

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;
}
Also used : ConnectionHandleType(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType) HashAlgorithms(org.openecard.crypto.common.HashAlgorithms) CardStateEntry(org.openecard.common.sal.state.CardStateEntry) CryptoMarkerType(org.openecard.crypto.common.sal.did.CryptoMarkerType) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) HashGenerationInfoType(iso.std.iso_iec._24727.tech.schema.HashGenerationInfoType) UnsupportedAlgorithmException(org.openecard.crypto.common.UnsupportedAlgorithmException) ECardException(org.openecard.common.ECardException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ECardException(org.openecard.common.ECardException) HashResponse(iso.std.iso_iec._24727.tech.schema.HashResponse) SignatureAlgorithms(org.openecard.crypto.common.SignatureAlgorithms) UnsupportedAlgorithmException(org.openecard.crypto.common.UnsupportedAlgorithmException) DIDStructureType(iso.std.iso_iec._24727.tech.schema.DIDStructureType) MessageDigest(java.security.MessageDigest)

Aggregations

HashResponse (iso.std.iso_iec._24727.tech.schema.HashResponse)6 Hash (iso.std.iso_iec._24727.tech.schema.Hash)3 ConnectionHandleType (iso.std.iso_iec._24727.tech.schema.ConnectionHandleType)2 DIDStructureType (iso.std.iso_iec._24727.tech.schema.DIDStructureType)2 ECardException (org.openecard.common.ECardException)2 CardStateEntry (org.openecard.common.sal.state.CardStateEntry)2 Test (org.testng.annotations.Test)2 DIDScopeType (iso.std.iso_iec._24727.tech.schema.DIDScopeType)1 HashGenerationInfoType (iso.std.iso_iec._24727.tech.schema.HashGenerationInfoType)1 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 AddonNotFoundException (org.openecard.addon.AddonNotFoundException)1 SALProtocol (org.openecard.addon.sal.SALProtocol)1 ThreadTerminateException (org.openecard.common.ThreadTerminateException)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 PrerequisitesNotSatisfiedException (org.openecard.common.sal.exception.PrerequisitesNotSatisfiedException)1