Search in sources :

Example 1 with HashAlgorithms

use of org.openecard.crypto.common.HashAlgorithms in project open-ecard by ecsec.

the class SmartCardCredentialFactory method convertSignatureAlgorithm.

@Nullable
private static SignatureAndHashAlgorithm convertSignatureAlgorithm(SignatureAlgorithms alg) {
    HashAlgorithms hashAlg = alg.getHashAlg();
    KeyTypes keyType = alg.getKeyType();
    short hash;
    if (hashAlg != null) {
        switch(hashAlg) {
            case CKM_SHA_1:
                hash = HashAlgorithm.sha1;
                break;
            case CKM_SHA224:
                hash = HashAlgorithm.sha224;
                break;
            case CKM_SHA256:
                hash = HashAlgorithm.sha256;
                break;
            case CKM_SHA384:
                hash = HashAlgorithm.sha384;
                break;
            case CKM_SHA512:
                hash = HashAlgorithm.sha512;
                break;
            default:
                throw new IllegalArgumentException("Unsupported hash algorithm selected.");
        }
    } else {
        return null;
    }
    short sig;
    switch(keyType) {
        case CKK_RSA:
            sig = SignatureAlgorithm.rsa;
            break;
        case CKK_EC:
            sig = SignatureAlgorithm.ecdsa;
            break;
        default:
            throw new IllegalArgumentException("Unsupported signature algorithm selected.");
    }
    return new SignatureAndHashAlgorithm(hash, sig);
}
Also used : HashAlgorithms(org.openecard.crypto.common.HashAlgorithms) KeyTypes(org.openecard.crypto.common.KeyTypes) SignatureAndHashAlgorithm(org.openecard.bouncycastle.tls.SignatureAndHashAlgorithm) Nullable(javax.annotation.Nullable)

Example 2 with HashAlgorithms

use of org.openecard.crypto.common.HashAlgorithms 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

HashAlgorithms (org.openecard.crypto.common.HashAlgorithms)2 ConnectionHandleType (iso.std.iso_iec._24727.tech.schema.ConnectionHandleType)1 DIDStructureType (iso.std.iso_iec._24727.tech.schema.DIDStructureType)1 HashGenerationInfoType (iso.std.iso_iec._24727.tech.schema.HashGenerationInfoType)1 HashResponse (iso.std.iso_iec._24727.tech.schema.HashResponse)1 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Nullable (javax.annotation.Nullable)1 SignatureAndHashAlgorithm (org.openecard.bouncycastle.tls.SignatureAndHashAlgorithm)1 ECardException (org.openecard.common.ECardException)1 CardStateEntry (org.openecard.common.sal.state.CardStateEntry)1 KeyTypes (org.openecard.crypto.common.KeyTypes)1 SignatureAlgorithms (org.openecard.crypto.common.SignatureAlgorithms)1 UnsupportedAlgorithmException (org.openecard.crypto.common.UnsupportedAlgorithmException)1 CryptoMarkerType (org.openecard.crypto.common.sal.did.CryptoMarkerType)1