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);
}
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;
}
Aggregations