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:be.fedict.hsm.model.KeyStoreSingletonBean.java

/**
 * Sign the given digest value./*from  ww  w .j  a  v  a2s  .c  o  m*/
 * 
 * @param keyStoreId
 * @param keyStoreAlias
 * @param digestAlgo
 * @param digestValue
 * @return the signature, or <code>null</code> in case something went wrong.
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 * @throws IOException
 * @throws SignatureException
 */
@Lock(LockType.READ)
public byte[] sign(long keyStoreId, String keyStoreAlias, String digestAlgo, byte[] digestValue)
        throws NoSuchAlgorithmException, InvalidKeyException, IOException, SignatureException {
    Map<String, PrivateKeyEntry> keyStoreKeys = this.privateKeyEntries.get(keyStoreId);
    if (null == keyStoreKeys) {
        LOG.error("unknown key store: " + keyStoreId);
        return null;
    }
    PrivateKeyEntry privateKeyEntry = keyStoreKeys.get(keyStoreAlias);
    if (null == privateKeyEntry) {
        LOG.error("private key for alias not available: " + keyStoreAlias);
        return null;
    }
    PrivateKey privateKey = privateKeyEntry.getPrivateKey();
    Signature signature = Signature.getInstance("NONEwithRSA");
    signature.initSign(privateKey);

    ByteArrayOutputStream digestInfo = new ByteArrayOutputStream();
    byte[] digestInfoPrefix = digestInfoPrefixes.get(digestAlgo);
    if (null == digestInfoPrefix) {
        throw new NoSuchAlgorithmException(digestAlgo);
    }
    digestInfo.write(digestInfoPrefix);
    digestInfo.write(digestValue);

    signature.update(digestInfo.toByteArray());

    return signature.sign();
}

From source file:be.fedict.hsm.jca.HSMProxyKeyStore.java

@Override
public void engineLoad(LoadStoreParameter param)
        throws IOException, NoSuchAlgorithmException, CertificateException {
    if (null == param) {
        throw new NoSuchAlgorithmException("requires a load parameter");
    }//from   w  w  w.  java  2 s  .  c  om
    this.keyStoreParameter = (HSMProxyKeyStoreParameter) param;
}

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

@Override
public Future<byte[]> decryptAsync(final byte[] ciphertext, final byte[] iv, final byte[] authenticationData,
        final byte[] authenticationTag, final String algorithm) throws NoSuchAlgorithmException {

    if (Strings.isNullOrWhiteSpace(algorithm)) {
        throw new IllegalArgumentException("algorithm");
    }//from   w  ww.j  a v  a2  s . c om

    if (ciphertext == null) {
        throw new IllegalArgumentException("ciphertext");
    }

    if (iv == null) {
        throw new IllegalArgumentException("iv");
    }

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

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

    SymmetricEncryptionAlgorithm algo = (SymmetricEncryptionAlgorithm) baseAlgorithm;

    ICryptoTransform transform = null;

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

    byte[] result = null;

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

    if (transform instanceof IAuthenticatedCryptoTransform) {

        IAuthenticatedCryptoTransform authenticatedTransform = (IAuthenticatedCryptoTransform) transform;

        if (authenticationData == null || authenticationTag == null) {
            throw new IllegalArgumentException(
                    "AuthenticatingCryptoTransform requires authenticationData and authenticationTag");
        }

        if (!sequenceEqualConstantTime(authenticationTag, authenticatedTransform.getTag())) {
            throw new IllegalArgumentException("Data is not authentic");
        }
    }

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

From source file:be.fedict.hsm.model.SignatureServiceBean.java

@Override
public Certificate[] getCertificateChain(String alias) throws NoSuchAlgorithmException {
    Principal callerPrincipal = this.sessionContext.getCallerPrincipal();
    LOG.debug("caller principal: " + callerPrincipal.getName());
    long appId = Long.parseLong(callerPrincipal.getName());
    ApplicationKeyId applicationKeyId = new ApplicationKeyId(appId, alias);
    ApplicationKeyEntity applicationKeyEntity = this.entityManager.find(ApplicationKeyEntity.class,
            applicationKeyId);//from   www  . ja  v a2  s  .  c  om
    if (null == applicationKeyEntity) {
        throw new NoSuchAlgorithmException("unknown key alias: " + alias);
    }
    String keyStoreAlias = applicationKeyEntity.getKeyStoreKeyAlias();
    long keyStoreId = applicationKeyEntity.getKeyStore().getId();
    Certificate[] certificateChain = this.keyStoreSingletonBean.getCertificateChain(keyStoreId, keyStoreAlias);
    return certificateChain;
}

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

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

    if (key == null) {
        throw new IllegalArgumentException("key");
    }/*  w  w w.  j a  va 2s.com*/

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

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

    AsymmetricEncryptionAlgorithm algo = (AsymmetricEncryptionAlgorithm) baseAlgorithm;

    ICryptoTransform transform;
    ListenableFuture<Pair<byte[], String>> result;

    try {
        transform = algo.CreateEncryptor(_keyPair, _provider);
        result = Futures.immediateFuture(Pair.of(transform.doFinal(key), algorithmName));
    } catch (Exception e) {
        result = Futures.immediateFailedFuture(e);
    }

    return result;
}

From source file:at.gv.egiz.bku.slcommands.impl.cms.Signature.java

public Signature(CMSDataObjectOptionalMetaType dataObject, String structure, X509Certificate signingCertificate,
        Date signingTime, URLDereferencer urlDereferencer, boolean useStrongHash)
        throws NoSuchAlgorithmException, CertificateEncodingException, CertificateException,
        X509ExtensionException, InvalidParameterException, CodingException, SLCommandException, IOException,
        CMSException {//from w  ww . j a  v a2  s  .  c o m
    int mode = structure.equalsIgnoreCase("enveloping") ? SignedData.IMPLICIT : SignedData.EXPLICIT;
    if (dataObject.getContent() != null) {
        String filename = null;
        byte[] dataToBeSigned = getContent(dataObject, urlDereferencer);
        this.signedData = new SignedData(dataToBeSigned, mode);
        MetaInfoType metaInfo = dataObject.getMetaInfo();
        if (metaInfo != null) {
            this.mimeType = metaInfo.getMimeType();
            filename = metaInfo.getDescription(); // security layer doesn't specify explicit filename property for
                                                  // single signature requests
        }
        hashDataInput = new CMSHashDataInput(signedDocument, mimeType);
        hashDataInput.setFilename(filename);
    } else {
        DigestAndRefType digestAndRef = dataObject.getDigestAndRef();
        DigestMethodType digestMethod = digestAndRef.getDigestMethod();

        hashDataInput = new ReferencedHashDataInput(dataObject.getMetaInfo().getMimeType(), urlDereferencer,
                digestAndRef.getReference(), dataObject.getExcludedByteRange());

        try {
            digestAlgorithm = getAlgorithmID(digestMethod.getAlgorithm());
        } catch (URISyntaxException e) {
            //TODO: choose proper execption
            throw new NoSuchAlgorithmException(e);
        }
        digestValue = digestAndRef.getDigestValue();
        this.signedData = new SignedData(ObjectID.pkcs7_data);
    }
    setAlgorithmIDs(signingCertificate, useStrongHash);
    createSignerInfo(signingCertificate);
    setSignerCertificate(signingCertificate);
    this.mimeType = dataObject.getMetaInfo().getMimeType();

    setAttributes(this.mimeType, signingCertificate, signingTime);
}

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

/**
 * @return the digest algorithm identifier for the given algorithm id.
 * @param signatureAlgorithm the algorithm id
 *
 * @throws NoSuchAlgorithmException if the algorithm id is unknown
 *///from  w  w w .j ava2s.  com
public static String getDigestAlgorithmId(String signatureAlgorithm) throws NoSuchAlgorithmException {
    switch (signatureAlgorithm) {
    case SHA1WITHRSA_ID:
        return SHA1_ID;
    case SHA256WITHRSA_ID: // fall through
    case SHA256WITHRSAANDMGF1_ID:
        return SHA256_ID;
    case SHA384WITHRSA_ID: // fall through
    case SHA384WITHRSAANDMGF1_ID:
        return SHA384_ID;
    case SHA512WITHRSA_ID: // fall through
    case SHA512WITHRSAANDMGF1_ID:
        return SHA512_ID;
    default:
        throw new NoSuchAlgorithmException("Unkown signature algorithm id: " + signatureAlgorithm);
    }
}

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

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

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

    // 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;
    ListenableFuture<byte[]> result;

    try {
        transform = algo.CreateDecryptor(_keyPair, _provider);
        result = Futures.immediateFuture(transform.doFinal(encryptedKey));
    } catch (Exception e) {
        result = Futures.immediateFailedFuture(e);
    }

    return result;
}

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

@Override
public ListenableFuture<byte[]> decryptAsync(final byte[] ciphertext, final byte[] iv,
        final byte[] authenticationData, final byte[] authenticationTag, final String algorithm)
        throws NoSuchAlgorithmException {

    if (Strings.isNullOrWhiteSpace(algorithm)) {
        throw new IllegalArgumentException("algorithm");
    }//from   ww  w  . j a va2  s . c o m

    if (ciphertext == null) {
        throw new IllegalArgumentException("ciphertext");
    }

    if (iv == null) {
        throw new IllegalArgumentException("iv");
    }

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

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

    SymmetricEncryptionAlgorithm algo = (SymmetricEncryptionAlgorithm) baseAlgorithm;

    ICryptoTransform transform = null;

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

    byte[] result = null;

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

    return Futures.immediateFuture(result);
}

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

@Override
public Future<byte[]> decryptAsync(final byte[] ciphertext, final byte[] iv, final byte[] authenticationData,
        final byte[] authenticationTag, final String algorithm) throws NoSuchAlgorithmException {

    if (ciphertext == null) {
        throw new IllegalArgumentException("ciphertext");
    }//from  w ww.java2 s  .com

    // Interpret the requested algorithm
    if (Strings.isNullOrWhiteSpace(algorithm)) {
        throw new IllegalArgumentException("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, ciphertext);
    } catch (Exception e) {
        result = new FutureExecutionException<byte[]>(e);
    }

    return result;
}