Example usage for java.security NoSuchAlgorithmException NoSuchAlgorithmException

List of usage examples for java.security NoSuchAlgorithmException NoSuchAlgorithmException

Introduction

In this page you can find the example usage for java.security NoSuchAlgorithmException NoSuchAlgorithmException.

Prototype

public NoSuchAlgorithmException(Throwable cause) 

Source Link

Document

Creates a NoSuchAlgorithmException with the specified cause and a detail message of (cause==null ?

Usage

From source file:at.gv.egiz.bku.local.stal.LocalSecureViewer.java

private List<HashDataInput> verifyHashDataInput(List<ReferenceType> signedReferences,
        HashDataInput hashDataInput) throws DigestException, NoSuchAlgorithmException, Exception {

    ArrayList<HashDataInput> verifiedHashDataInputs = new ArrayList<HashDataInput>();

    for (ReferenceType signedRef : signedReferences) {
        if (signedRef.getType() == null) {
            log.info("Verifying digest for signed reference {}.", signedRef.getId());

            String signedRefId = signedRef.getId();
            byte[] signedDigest = signedRef.getDigestValue();
            String signedDigestAlg = null;
            if (signedRef.getDigestMethod() != null) {
                signedDigestAlg = signedRef.getDigestMethod().getAlgorithm();
            } else {
                throw new NoSuchAlgorithmException(
                        "Failed to verify digest value for reference " + signedRefId + ": no digest algorithm");
            }//  w w w  .  j  a v a 2s.  c  om

            if (hashDataInput == null) {
                throw new Exception("No hashdata input for reference " + signedRefId + " returned by service");
            }

            byte[] hdi = null;

            try {
                hdi = IOUtils.toByteArray(hashDataInput.getHashDataInput());
            } catch (IOException e) {
                throw new Exception("No hashdata input for reference " + signedRefId + " provided by service.",
                        e);
            }

            String mimeType = hashDataInput.getMimeType();
            String encoding = hashDataInput.getEncoding();
            String filename = hashDataInput.getFilename();

            if (log.isDebugEnabled()) {
                log.debug("Digesting reference " + signedRefId + " (" + mimeType + ";" + encoding + ")");
            }

            byte[] hashDataInputDigest;
            if ((signedRef.getURI() != null) && signedRef.getURI().startsWith("CMSExcludedByteRange:")) {
                String range = signedRef.getURI().substring(21);
                int sep = range.indexOf('-');
                int from = Integer.parseInt(range.substring(0, sep));
                int to = Integer.parseInt(range.substring(sep + 1));

                Arrays.fill(hdi, from, to + 1, (byte) 0);

                byte[] hashData = new byte[hdi.length - ((to + 1) - from)];
                if (from > 0)
                    System.arraycopy(hdi, 0, hashData, 0, from);
                if ((to + 1) < hdi.length)
                    System.arraycopy(hdi, to + 1, hashData, from, hdi.length - (to + 1));
                hashDataInputDigest = digest(hashData, signedDigestAlg);
            } else {
                hashDataInputDigest = digest(hdi, signedDigestAlg);
            }

            log.debug("Comparing digest to claimed digest value for reference {}.", signedRefId);
            if (!Arrays.equals(hashDataInputDigest, signedDigest)) {
                log.error("Bad digest value for reference {}.", signedRefId);
                throw new DigestException("Bad digest value for reference " + signedRefId);
            }

            verifiedHashDataInputs
                    .add(new ByteArrayHashDataInput(hdi, signedRefId, mimeType, encoding, filename));
        }
    }

    return verifiedHashDataInputs;
}

From source file:com.microsoft.azure.keyvault.cryptography.SymmetricKey.java

@Override
public ListenableFuture<Pair<byte[], String>> wrapKeyAsync(final byte[] key, final String algorithm)
        throws NoSuchAlgorithmException {

    if (key == null || key.length == 0) {
        throw new IllegalArgumentException("key");
    }/*from  w  ww  . jav a 2  s . c  o  m*/

    // Interpret the algorithm
    String algorithmName = (Strings.isNullOrWhiteSpace(algorithm)) ? getDefaultKeyWrapAlgorithm() : algorithm;
    Algorithm baseAlgorithm = AlgorithmResolver.Default.get(algorithmName);

    if (baseAlgorithm == null || !(baseAlgorithm instanceof KeyWrapAlgorithm)) {
        throw new NoSuchAlgorithmException(algorithmName);
    }

    KeyWrapAlgorithm algo = (KeyWrapAlgorithm) baseAlgorithm;

    ICryptoTransform transform = null;

    try {
        transform = algo.CreateEncryptor(_key, null, _provider);
    } catch (Exception e) {
        return Futures.immediateFailedFuture(e);
    }

    byte[] encrypted = null;

    try {
        encrypted = transform.doFinal(key);
    } catch (Exception e) {
        return Futures.immediateFailedFuture(e);
    }

    return Futures.immediateFuture(Pair.of(encrypted, algorithmName));
}

From source file:com.microsoft.azure.keyvault.extensions.SymmetricKey.java

@Override
public Future<byte[]> unwrapKeyAsync(final byte[] encryptedKey, final String algorithm)
        throws NoSuchAlgorithmException {

    if (Strings.isNullOrWhiteSpace(algorithm)) {
        throw new IllegalArgumentException("algorithm");
    }/*from   w  w w  .  j  a  va  2 s  .co m*/

    if (encryptedKey == null || encryptedKey.length == 0) {
        throw new IllegalArgumentException("wrappedKey");
    }

    Algorithm baseAlgorithm = AlgorithmResolver.Default.get(algorithm);

    if (baseAlgorithm == null || !(baseAlgorithm instanceof KeyWrapAlgorithm)) {
        throw new NoSuchAlgorithmException(algorithm);
    }

    KeyWrapAlgorithm algo = (KeyWrapAlgorithm) baseAlgorithm;

    ICryptoTransform transform = null;

    try {
        transform = algo.CreateDecryptor(_key, null, _provider);
    } catch (Exception e) {
        return new FutureExecutionException<byte[]>(e);
    }

    byte[] decrypted = null;

    try {
        decrypted = transform.doFinal(encryptedKey);
    } catch (Exception e) {
        return new FutureExecutionException<byte[]>(e);
    }

    return new FutureImmediate<byte[]>(decrypted);
}

From source file:ee.ria.xroad.common.util.CryptoUtils.java

/**
 * Returns the digest/signature algorithm identifier for the given digest/signature algorithm URI.
 * @param algoURI the URI of the algorithm
 * @return the identifier of the algorithm
 * @throws NoSuchAlgorithmException if the algorithm URI is unknown
 *//*w w w  .  j  a va  2s.c  om*/
public static String getAlgorithmId(String algoURI) throws NoSuchAlgorithmException {
    switch (algoURI) {
    case DigestMethod.SHA1:
        return SHA1_ID;
    case MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA224:
        return SHA224_ID;
    case DigestMethod.SHA256:
        return SHA256_ID;
    case MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA384:
        return SHA384_ID;
    case DigestMethod.SHA512:
        return SHA512_ID;
    case ALGO_ID_SIGNATURE_RSA_SHA1:
        return SHA1WITHRSA_ID;
    case ALGO_ID_SIGNATURE_RSA_SHA256:
        return SHA256WITHRSA_ID;
    case ALGO_ID_SIGNATURE_RSA_SHA384:
        return SHA384WITHRSA_ID;
    case ALGO_ID_SIGNATURE_RSA_SHA512:
        return SHA512WITHRSA_ID;
    case ALGO_ID_SIGNATURE_RSA_SHA256_MGF1:
        return SHA256WITHRSAANDMGF1_ID;
    case ALGO_ID_SIGNATURE_RSA_SHA384_MGF1:
        return SHA384WITHRSAANDMGF1_ID;
    case ALGO_ID_SIGNATURE_RSA_SHA512_MGF1:
        return SHA512WITHRSAANDMGF1_ID;
    default:
        throw new NoSuchAlgorithmException("Unknown algorithm URI: " + algoURI);
    }
}

From source file:com.microsoft.azure.keyvault.extensions.RsaKey.java

@Override
public Future<byte[]> unwrapKeyAsync(final byte[] encryptedKey, final String algorithm)
        throws NoSuchAlgorithmException {

    if (encryptedKey == null) {
        throw new IllegalArgumentException("encryptedKey ");
    }/*from  w ww .  j  a v a 2s .com*/

    // Interpret the requested algorithm
    if (Strings.isNullOrWhiteSpace(algorithm)) {
        throw new IllegalArgumentException("algorithm");
    }

    // Interpret the requested algorithm
    Algorithm baseAlgorithm = AlgorithmResolver.Default.get(algorithm);

    if (baseAlgorithm == null || !(baseAlgorithm instanceof AsymmetricEncryptionAlgorithm)) {
        throw new NoSuchAlgorithmException(algorithm);
    }

    AsymmetricEncryptionAlgorithm algo = (AsymmetricEncryptionAlgorithm) baseAlgorithm;

    ICryptoTransform transform;
    Future<byte[]> result;

    try {
        transform = algo.CreateDecryptor(_keyPair);
        result = new FutureDecrypt(transform, encryptedKey);
    } catch (Exception e) {
        result = new FutureExecutionException<byte[]>(e);
    }

    return result;
}

From source file:ddf.security.samlp.SimpleSign.java

public boolean validateSignature(String sigAlg, String queryParamsToValidate, String encodedSignature,
        @Nullable String encodedPublicKey) throws SignatureException {
    if (encodedPublicKey == null) {
        LOGGER.warn(/*from  ww  w  .j  a  v  a2 s  .  co m*/
                "Could not verify the signature of request because there was no signing certificate. Ensure that the IdP Metadata includes a signing certificate.");
        return false;
    }

    try {
        CertificateFactory certificateFactory = CertificateFactory.getInstance("X509");
        Certificate certificate = certificateFactory.generateCertificate(
                new ByteArrayInputStream(Base64.getMimeDecoder().decode(encodedPublicKey)));

        java.security.Signature sig;
        String jceSigAlg = JCEMapper.translateURItoJCEID(sigAlg);

        if (jceSigAlg == null) {
            throw new SignatureException(new NoSuchAlgorithmException(
                    String.format("The Signature Algorithm %s is not supported.", sigAlg)));
        }

        try {
            sig = java.security.Signature.getInstance(jceSigAlg);
        } catch (NoSuchAlgorithmException e) {
            throw new SignatureException(e);
        }

        sig.initVerify(certificate.getPublicKey());
        sig.update(queryParamsToValidate.getBytes(StandardCharsets.UTF_8));
        return sig.verify(Base64.getMimeDecoder().decode(encodedSignature));
    } catch (InvalidKeyException | CertificateException | java.security.SignatureException
            | IllegalArgumentException e) {
        throw new SignatureException(e);
    }
}

From source file:ee.ria.xroad.common.util.CryptoUtils.java

/**
 * @return the signature algorithm identifier for the given digest algorithm id and signing mechanism.
 * @param digestAlgorithmId the digest algorithm id
 * @param signMechanismName the signing mechanism name
 *
 * @throws NoSuchAlgorithmException if the digest algorithm id or signing mechanism is unknown
 *///w ww .  j  a va2 s  .  c o m
public static String getSignatureAlgorithmId(String digestAlgorithmId, String signMechanismName)
        throws NoSuchAlgorithmException {
    switch (signMechanismName) {
    case CKM_RSA_PKCS_NAME:
        switch (digestAlgorithmId) {
        case SHA1_ID:
            return SHA1WITHRSA_ID;
        case SHA256_ID:
            return SHA256WITHRSA_ID;
        case SHA384_ID:
            return SHA384WITHRSA_ID;
        case SHA512_ID:
            return SHA512WITHRSA_ID;
        default:
            throw new NoSuchAlgorithmException("Unknown digest algorithm id: " + digestAlgorithmId);
        }
    case CKM_RSA_PKCS_PSS_NAME:
        switch (digestAlgorithmId) {
        case SHA256_ID:
            return SHA256WITHRSAANDMGF1_ID;
        case SHA384_ID:
            return SHA384WITHRSAANDMGF1_ID;
        case SHA512_ID:
            return SHA512WITHRSAANDMGF1_ID;
        default:
            throw new NoSuchAlgorithmException("Unknown digest algorithm id: " + digestAlgorithmId);
        }
    default:
        throw new NoSuchAlgorithmException("Unknown signing mechanism: " + signMechanismName);
    }
}

From source file:com.microsoft.azure.keyvault.cryptography.SymmetricKey.java

@Override
public ListenableFuture<byte[]> unwrapKeyAsync(final byte[] encryptedKey, final String algorithm)
        throws NoSuchAlgorithmException {

    if (Strings.isNullOrWhiteSpace(algorithm)) {
        throw new IllegalArgumentException("algorithm");
    }/*from ww w  .j  a  v a 2  s . co m*/

    if (encryptedKey == null || encryptedKey.length == 0) {
        throw new IllegalArgumentException("wrappedKey");
    }

    Algorithm baseAlgorithm = AlgorithmResolver.Default.get(algorithm);

    if (baseAlgorithm == null || !(baseAlgorithm instanceof KeyWrapAlgorithm)) {
        throw new NoSuchAlgorithmException(algorithm);
    }

    KeyWrapAlgorithm algo = (KeyWrapAlgorithm) baseAlgorithm;

    ICryptoTransform transform = null;

    try {
        transform = algo.CreateDecryptor(_key, null, _provider);
    } catch (Exception e) {
        return Futures.immediateFailedFuture(e);
    }

    byte[] decrypted = null;

    try {
        decrypted = transform.doFinal(encryptedKey);
    } catch (Exception e) {
        return Futures.immediateFailedFuture(e);
    }

    return Futures.immediateFuture(decrypted);
}

From source file:be.fedict.commons.eid.consumer.BeIDIntegrity.java

private String getDigestAlgo(final int hashSize) throws NoSuchAlgorithmException {
    switch (hashSize) {
    case 20:/*from w  w w . java  2  s . co m*/
        return "SHA-1";
    case 28:
        return "SHA-224";
    case 32:
        return "SHA-256";
    case 48:
        return "SHA-384";
    case 64:
        return "SHA-512";
    }

    throw new NoSuchAlgorithmException(
            "Failed to find guess algorithm for hash size of " + hashSize + " bytes");
}

From source file:be.fedict.eid.applet.service.impl.handler.SignCertificatesDataMessageHandler.java

private String getDigestAlgo(final int hashSize) throws NoSuchAlgorithmException {
    switch (hashSize) {
    case 20:// w w w .  j a  v  a  2s. c o  m
        return "SHA-1";
    case 28:
        return "SHA-224";
    case 32:
        return "SHA-256";
    case 48:
        return "SHA-384";
    case 64:
        return "SHA-512";
    }
    throw new NoSuchAlgorithmException(
            "Failed to find guess algorithm for hash size of " + hashSize + " bytes");
}