Example usage for java.security InvalidAlgorithmParameterException InvalidAlgorithmParameterException

List of usage examples for java.security InvalidAlgorithmParameterException InvalidAlgorithmParameterException

Introduction

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

Prototype

public InvalidAlgorithmParameterException(Throwable cause) 

Source Link

Document

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

Usage

From source file:org.apache.jcp.xml.dsig.internal.dom.DOMHMACSignatureMethod.java

void checkParams(SignatureMethodParameterSpec params) throws InvalidAlgorithmParameterException {
    if (params != null) {
        if (!(params instanceof HMACParameterSpec)) {
            throw new InvalidAlgorithmParameterException("params must be of type HMACParameterSpec");
        }/*from   w ww .j  av a2s .co m*/
        outputLength = ((HMACParameterSpec) params).getOutputLength();
        outputLengthSet = true;
        if (log.isDebugEnabled()) {
            log.debug("Setting outputLength from HMACParameterSpec to: " + outputLength);
        }
    }
}

From source file:org.apache.jcp.xml.dsig.internal.dom.DOMSignatureMethod.java

/**
 * Creates a <code>DOMSignatureMethod</code>.
 *
 * @param params the algorithm-specific params (may be <code>null</code>)
 * @throws InvalidAlgorithmParameterException
 *          if the parameters are not//  w w  w  .j av  a  2s  .  c  o m
 *          appropriate for this signature method
 */
DOMSignatureMethod(AlgorithmParameterSpec params) throws InvalidAlgorithmParameterException {
    if (params != null && !(params instanceof SignatureMethodParameterSpec)) {
        throw new InvalidAlgorithmParameterException("params must be of type SignatureMethodParameterSpec");
    }
    checkParams((SignatureMethodParameterSpec) params);
    this.params = (SignatureMethodParameterSpec) params;
}

From source file:org.cesecore.keys.util.KeyStoreTools.java

private void generateEC(final String name, final String keyEntryName)
        throws InvalidAlgorithmParameterException {
    if (log.isTraceEnabled()) {
        log.trace(">generate EC: curve name " + name + ", keyEntryName " + keyEntryName);
    }/*from  w ww .j a  va2 s. c o  m*/
    // Generate the EC Keypair
    KeyPairGenerator kpg;
    try {
        kpg = KeyPairGenerator.getInstance("EC", this.providerName);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("Algorithm " + "EC" + "was not recognized.", e);
    } catch (NoSuchProviderException e) {
        throw new IllegalStateException("BouncyCastle was not found as a provider.", e);
    }
    try {
        Provider prov = Security.getProvider(this.providerName);
        if (StringUtils.contains(prov.getClass().getName(), "iaik")) {
            throw new InvalidAlgorithmParameterException("IAIK ECC key generation not implemented.");
            /*
            ECDSAPrivateKey privateKeyTemplate = new ECDSAPrivateKey();
            privateKeyTemplate.getSign().setBooleanValue(Boolean.TRUE);
            privateKeyTemplate.getToken().setBooleanValue(Boolean.FALSE);
                    
            ECDSAPublicKey publicKeyTemplate = new ECDSAPublicKey();
            publicKeyTemplate.getVerify().setBooleanValue(Boolean.TRUE);
            publicKeyTemplate.getToken().setBooleanValue(Boolean.FALSE);
                    
            ObjectID eccCurveObjectID = new ObjectID(objectID);
            publicKeyTemplate.getEcdsaParams().setByteArrayValue(DerCoder.encode(eccCurveObjectID));
                    
            PKCS11KeyPairGenerationSpec keyPairGenerationSpec =
               new PKCS11KeyPairGenerationSpec(tokenManager, publicKeyTemplate, privateKeyTemplate, 
               PKCS11Spec.USE_READ_WRITE_SESSION, PKCS11Spec.USE_USER_SESSION);
                    
            keyPairGenerator.initialize(keyPairGenerationSpec);
            */
        } else {
            ECGenParameterSpec ecSpec = new ECGenParameterSpec(name);
            if (StringUtils.equals(name, "implicitlyCA")) {
                log.debug("Generating implicitlyCA encoded ECDSA key pair");
                // If the keySpec is null, we have "implicitlyCA" defined EC parameters
                // The parameters were already installed when we installed the provider
                // We just make sure that ecSpec == null here
                ecSpec = null;
            }
            kpg.initialize(ecSpec);
        }
    } catch (InvalidAlgorithmParameterException e) {
        log.debug("EC name " + name + " not supported.");
        throw e;
    }
    generateKeyPair(kpg, keyEntryName, "SHA1withECDSA");
    if (log.isTraceEnabled()) {
        log.trace("<generate: curve name " + name + ", keyEntryName " + keyEntryName);
    }
}

From source file:org.cesecore.keys.util.KeyTools.java

/**
 * Generates a keypair//from w w w  . j  a  va  2 s . co m
 * 
 * @param keySpec
 *            string specification of keys to generate, typical value is 2048 for RSA keys,
 *            1024 for DSA keys, secp256r1 for ECDSA keys, or null if algspec is to be used.
 * @param algSpec
 *            AlgorithmParameterSpec of keys to generate, typically an EXParameterSpec for EC keys, or null if keySpec is to be used.
 * @param keyAlg
 *            algorithm of keys to generate, typical value is RSA, DSA or ECDSA, see AlgorithmConstants.KEYALGORITHM_XX
 * 
 * @see org.cesecore.certificates.util.core.model.AlgorithmConstants
 * @see org.bouncycastle.asn1.x9.X962NamedCurves
 * @see org.bouncycastle.asn1.nist.NISTNamedCurves
 * @see org.bouncycastle.asn1.sec.SECNamedCurves
 * 
 * @return KeyPair the generated keypair
 * @throws InvalidAlgorithmParameterException
 * @see org.cesecore.certificates.util.AlgorithmConstants#KEYALGORITHM_RSA
 */
public static KeyPair genKeys(final String keySpec, final AlgorithmParameterSpec algSpec, final String keyAlg)
        throws InvalidAlgorithmParameterException {
    if (log.isTraceEnabled()) {
        log.trace(">genKeys(" + keySpec + ", " + keyAlg + ")");
    }

    KeyPairGenerator keygen;
    try {
        keygen = KeyPairGenerator.getInstance(keyAlg, BouncyCastleProvider.PROVIDER_NAME);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("Algorithm " + keyAlg + "was not recognized.", e);
    } catch (NoSuchProviderException e) {
        throw new IllegalStateException("BouncyCastle was not found as a provider.", e);
    }
    if (StringUtils.equals(keyAlg, AlgorithmConstants.KEYALGORITHM_ECDSA)) {
        AlgorithmParameterSpec ecSpec = null;
        if ((keySpec != null) && !StringUtils.equals(keySpec, "implicitlyCA")) {
            log.debug("Generating named curve ECDSA key pair: " + keySpec);
            // We have EC keys
            ECGenParameterSpec bcSpec = new ECGenParameterSpec(keySpec);
            keygen.initialize(bcSpec, new SecureRandom());
            // The old code should work in BC v1.50b6 and later, but in vesions prior to that the below produces a key with explicit parameter encoding instead of named curves.
            // There is a test for this in KeyToolsTest.testGenKeysECDSAx9
            //                ecSpec = ECNamedCurveTable.getParameterSpec(keySpec);
            //                if (ecSpec == null) {
            //                    throw new InvalidAlgorithmParameterException("keySpec " + keySpec + " is invalid for ECDSA.");
            //                }
            //                keygen.initialize(ecSpec, new SecureRandom());
        } else if (algSpec != null) {
            log.debug("Generating ECDSA key pair from AlgorithmParameterSpec: " + algSpec);
            ecSpec = algSpec;
            keygen.initialize(ecSpec, new SecureRandom());
        } else if (StringUtils.equals(keySpec, "implicitlyCA")) {
            log.debug("Generating implicitlyCA encoded ECDSA key pair");
            // If the keySpec is null, we have "implicitlyCA" defined EC parameters
            // The parameters were already installed when we installed the provider
            // We just make sure that ecSpec == null here
            keygen.initialize(ecSpec, new SecureRandom());
        } else {
            throw new InvalidAlgorithmParameterException("No keySpec no algSpec and no implicitlyCA specified");
        }
    } else if (keyAlg.equals(AlgorithmConstants.KEYALGORITHM_ECGOST3410)) {
        AlgorithmParameterSpec ecSpec = null;
        if (keySpec != null) {
            log.debug("Generating keys from given key specifications : " + keySpec);
            ecSpec = ECGOST3410NamedCurveTable.getParameterSpec(keySpec);
            if (ecSpec == null)
                throw new InvalidAlgorithmParameterException(
                        "Key specification " + keySpec + " is invalid for ECGOST3410");
        } else if (algSpec != null) {
            log.debug("Generating keys from given algorithm parameters : " + algSpec);
            ecSpec = algSpec;
        } else {
            throw new InvalidAlgorithmParameterException("No key or algorithm specifications");
        }
        keygen.initialize(ecSpec, new SecureRandom());
    } else if (keyAlg.equals(AlgorithmConstants.KEYALGORITHM_DSTU4145)) {
        AlgorithmParameterSpec ecSpec = null;
        if (keySpec != null) {
            log.debug("Generating keys from given key specifications : " + keySpec);
            ecSpec = dstuOidToAlgoParams(keySpec);
            if (ecSpec == null)
                throw new InvalidAlgorithmParameterException(
                        "Key specification " + keySpec + " is invalid for DSTU4145");
        } else if (algSpec != null) {
            log.debug("Generating keys from given algorithm parameters : " + algSpec);
            ecSpec = algSpec;
        } else {
            throw new InvalidAlgorithmParameterException("No key or algorithm specifications");
        }
        keygen.initialize(ecSpec, new SecureRandom());
    } else if (keySpec.startsWith("DSA")) {
        // DSA key with "DSA" in keyspec
        final int keysize = Integer.parseInt(keySpec.substring(3));
        keygen.initialize(keysize);
    } else {
        // RSA or DSA key where keyspec is simply the key length
        final int keysize = Integer.parseInt(keySpec);
        keygen.initialize(keysize);
    }

    final KeyPair keys = keygen.generateKeyPair();

    if (log.isDebugEnabled()) {
        final PublicKey pk = keys.getPublic();
        final int len = getKeyLength(pk);
        log.debug("Generated " + keys.getPublic().getAlgorithm() + " keys with length " + len);
    }
    log.trace("<genKeys()");
    return keys;
}

From source file:org.ejbca.util.keystore.KeyStoreContainerBase.java

/** 
 * @see org.ejbca.util.keystore.KeyStoreContainer#generate(java.lang.String, java.lang.String)
 *//*from w w w  .ja  v a 2 s.  com*/
private byte[] generateEC(final String name, final String keyEntryName) throws Exception {
    if (log.isTraceEnabled()) {
        log.trace(">generate EC: curve name " + name + ", keyEntryName " + keyEntryName);
    }
    // Generate the EC Keypair
    final KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", this.providerName);
    try {
        Provider prov = Security.getProvider(this.providerName);
        if (StringUtils.contains(prov.getClass().getName(), "iaik")) {
            throw new InvalidAlgorithmParameterException("IAIK ECC key generation not implemented.");
            /*
            ECDSAPrivateKey privateKeyTemplate = new ECDSAPrivateKey();
            privateKeyTemplate.getSign().setBooleanValue(Boolean.TRUE);
            privateKeyTemplate.getToken().setBooleanValue(Boolean.FALSE);
                    
            ECDSAPublicKey publicKeyTemplate = new ECDSAPublicKey();
            publicKeyTemplate.getVerify().setBooleanValue(Boolean.TRUE);
            publicKeyTemplate.getToken().setBooleanValue(Boolean.FALSE);
                    
            ObjectID eccCurveObjectID = new ObjectID(objectID);
            publicKeyTemplate.getEcdsaParams().setByteArrayValue(DerCoder.encode(eccCurveObjectID));
                    
            PKCS11KeyPairGenerationSpec keyPairGenerationSpec =
               new PKCS11KeyPairGenerationSpec(tokenManager, publicKeyTemplate, privateKeyTemplate, 
               PKCS11Spec.USE_READ_WRITE_SESSION, PKCS11Spec.USE_USER_SESSION);
                    
            keyPairGenerator.initialize(keyPairGenerationSpec);
            */
        } else {
            kpg.initialize(new ECGenParameterSpec(name));
        }
    } catch (InvalidAlgorithmParameterException e) {
        log.debug("EC name " + name + " not supported.");
        throw e;
    }
    final byte result[] = generate(kpg, keyEntryName, "SHA1withECDSA");
    if (log.isTraceEnabled()) {
        log.trace("<generate: curve name " + name + ", keyEntryName " + keyEntryName);
    }
    return result;
}

From source file:org.ejbca.util.keystore.KeyTools.java

/**
 * Generates a keypair/*from w  ww . ja va2  s.com*/
 *
 * @param keySpec string specification of keys to generate, typical value is 1024 for RSA or DSA keys, or prime192v1 for ECDSA keys or null of algspec is to be used.
 * @param algSpec AlgorithmParameterSpec of keys to generate, typically an EXParameterSpec for EC keys, or null if keySpec is to be used.
 * @param keyAlg algorithm of keys to generate, typical value is RSA, DSA or ECDSA, see AlgorithmConstants.KEYALGORITHM_XX
 * 
 * @see org.ejbca.core.model.AlgorithmConstants
 * @see org.bouncycastle.asn1.x9.X962NamedCurves
 * @see org.bouncycastle.asn1.nist.NISTNamedCurves
 * @see org.bouncycastle.asn1.sec.SECNamedCurves
 * 
 * @return KeyPair the generated keypair
 * @throws InvalidAlgorithmParameterException 
 * @see org.ejbca.core.model.AlgorithmConstants#KEYALGORITHM_RSA
 */
public static KeyPair genKeys(final String keySpec, final AlgorithmParameterSpec algSpec, final String keyAlg)
        throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
    if (log.isTraceEnabled()) {
        log.trace(">genKeys(" + keySpec + ", " + keyAlg + ")");
    }

    final KeyPairGenerator keygen = KeyPairGenerator.getInstance(keyAlg, "BC");
    if (StringUtils.equals(keyAlg, AlgorithmConstants.KEYALGORITHM_ECDSA)) {
        AlgorithmParameterSpec ecSpec = null;
        if ((keySpec != null) && !StringUtils.equals(keySpec, "implicitlyCA")) {
            log.debug("Generating named curve ECDSA key pair: " + keySpec);
            // We have EC keys
            ecSpec = ECNamedCurveTable.getParameterSpec(keySpec);
            if (ecSpec == null) {
                throw new InvalidAlgorithmParameterException("keySpec " + keySpec + " is invalid for ECDSA.");
            }
        } else if (algSpec != null) {
            log.debug("Generating ECDSA key pair from AlgorithmParameterSpec: " + algSpec);
            ecSpec = algSpec;
        } else if (StringUtils.equals(keySpec, "implicitlyCA")) {
            log.debug("Generating implicitlyCA encoded ECDSA key pair");
            // If the keySpec is null, we have "implicitlyCA" defined EC parameters
            // The parameters were already installed when we installed the provider
            // We just make sure that ecSpec == null here
        } else {
            throw new InvalidAlgorithmParameterException("No keySpec no algSpec and no implicitlyCA specified");
        }
        keygen.initialize(ecSpec, new SecureRandom());
    } else {
        // RSA or DSA keys
        final int keysize = Integer.parseInt(keySpec);
        keygen.initialize(keysize);
    }

    final KeyPair keys = keygen.generateKeyPair();

    if (log.isDebugEnabled()) {
        final PublicKey pk = keys.getPublic();
        final int len = getKeyLength(pk);
        log.debug("Generated " + keys.getPublic().getAlgorithm() + " keys with length " + len);
    }
    log.trace("<genKeys()");
    return keys;
}

From source file:org.opensc.pkcs11.spi.PKCS11KeyPairGeneratorSpi.java

@Override
public void initialize(AlgorithmParameterSpec params, SecureRandom random)
        throws InvalidAlgorithmParameterException {
    LoadStoreParameter loadStoreParameter;

    if ("RSA".equals(this.algorithm)) {
        if (!(params instanceof PKCS11RSAKeyPairGenParameterSpec))
            throw new InvalidAlgorithmParameterException(
                    "RSA AlgorithmParameterSpec must be of type PKCS11RSAKeyPairGenParameterSpec.");

        PKCS11RSAKeyPairGenParameterSpec rsaSpec = (PKCS11RSAKeyPairGenParameterSpec) params;

        this.generator = new PKCS11RSAKeyPairGenerator(rsaSpec);
        loadStoreParameter = rsaSpec.getLoadStoreParameter();
    } else if ("DSA".equals(this.algorithm)) {
        if (!(params instanceof PKCS11DSAKeyPairGenParameterSpec))
            throw new InvalidAlgorithmParameterException(
                    "DSA AlgorithmParameterSpec must be of type PKCS11DSAKeyPairGenParameterSpec.");

        PKCS11DSAKeyPairGenParameterSpec dsaSpec = (PKCS11DSAKeyPairGenParameterSpec) params;
        this.generator = new PKCS11DSAKeyPairGenerator(dsaSpec);
        loadStoreParameter = dsaSpec.getLoadStoreParameter();
    } else//from   www . j a  va2s.  c o  m
        throw new InvalidAlgorithmParameterException("Algorithm " + this.algorithm + " is not supported.");

    try {
        if (this.sessionStore != null) {
            if (this.needToCloseSesionStore)
                this.sessionStore.close();
        }

        if (loadStoreParameter instanceof PKCS11SessionStore) {
            this.sessionStore = (PKCS11SessionStore) loadStoreParameter;
            this.needToCloseSesionStore = false;
        } else {
            this.sessionStore = new PKCS11SessionStore();
            this.needToCloseSesionStore = true;
            this.sessionStore.open(this.provider, loadStoreParameter);
        }

    } catch (PKCS11Exception e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}