Search in sources :

Example 91 with InvalidAlgorithmParameterException

use of java.security.InvalidAlgorithmParameterException in project jdk8u_jdk by JetBrains.

the class RevocationChecker method init.

void init(TrustAnchor anchor, ValidatorParams params) throws CertPathValidatorException {
    RevocationProperties rp = getRevocationProperties();
    URI uri = getOcspResponder();
    responderURI = (uri == null) ? toURI(rp.ocspUrl) : uri;
    X509Certificate cert = getOcspResponderCert();
    responderCert = (cert == null) ? getResponderCert(rp, params.trustAnchors(), params.certStores()) : cert;
    Set<Option> options = getOptions();
    for (Option option : options) {
        switch(option) {
            case ONLY_END_ENTITY:
            case PREFER_CRLS:
            case SOFT_FAIL:
            case NO_FALLBACK:
                break;
            default:
                throw new CertPathValidatorException("Unrecognized revocation parameter option: " + option);
        }
    }
    softFail = options.contains(Option.SOFT_FAIL);
    // set mode, only end entity flag
    if (legacy) {
        mode = (rp.ocspEnabled) ? Mode.PREFER_OCSP : Mode.ONLY_CRLS;
        onlyEE = rp.onlyEE;
    } else {
        if (options.contains(Option.NO_FALLBACK)) {
            if (options.contains(Option.PREFER_CRLS)) {
                mode = Mode.ONLY_CRLS;
            } else {
                mode = Mode.ONLY_OCSP;
            }
        } else if (options.contains(Option.PREFER_CRLS)) {
            mode = Mode.PREFER_CRLS;
        }
        onlyEE = options.contains(Option.ONLY_END_ENTITY);
    }
    if (legacy) {
        crlDP = rp.crlDPEnabled;
    } else {
        crlDP = true;
    }
    ocspResponses = getOcspResponses();
    ocspExtensions = getOcspExtensions();
    this.anchor = anchor;
    this.params = params;
    this.certStores = new ArrayList<>(params.certStores());
    try {
        this.certStores.add(CertStore.getInstance("Collection", new CollectionCertStoreParameters(params.certificates())));
    } catch (InvalidAlgorithmParameterException | NoSuchAlgorithmException e) {
        // ignore and continue
        if (debug != null) {
            debug.println("RevocationChecker: " + "error creating Collection CertStore: " + e);
        }
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) URI(java.net.URI)

Example 92 with InvalidAlgorithmParameterException

use of java.security.InvalidAlgorithmParameterException in project jdk8u_jdk by JetBrains.

the class TestSameBuffer method runTest.

public void runTest(String algo, String mo, String pad) throws Exception {
    Cipher ci = null;
    byte[] iv = null;
    AlgorithmParameterSpec aps = null;
    SecretKey key = null;
    try {
        // Initialization
        Random rdm = new Random();
        byte[] plainText = new byte[128];
        rdm.nextBytes(plainText);
        // keep the plain text
        byte[] tmpText = new byte[plainText.length];
        for (int i = 0; i < plainText.length; i++) {
            tmpText[i] = plainText[i];
        }
        ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
        KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);
        kg.init(KEY_LENGTH);
        key = kg.generateKey();
        // encrypt
        ci.init(Cipher.ENCRYPT_MODE, key);
        int offset = ci.update(plainText, 0, plainText.length, plainText, 0);
        ci.doFinal(plainText, offset);
        if (!mo.equalsIgnoreCase("ECB")) {
            iv = ci.getIV();
            aps = new IvParameterSpec(iv);
        } else {
            aps = null;
        }
        ci.init(Cipher.DECRYPT_MODE, key, aps);
        byte[] recoveredText = new byte[ci.getOutputSize(plainText.length)];
        ci.doFinal(plainText, 0, plainText.length, recoveredText);
        // Comparison
        if (!java.util.Arrays.equals(tmpText, recoveredText)) {
            System.out.println("Original: ");
            dumpBytes(plainText);
            System.out.println("Recovered: ");
            dumpBytes(recoveredText);
            throw new RuntimeException("Original text is not equal with recovered text, with mode:" + mo);
        }
    } catch (NoSuchAlgorithmException e) {
        //CFB7 and CFB150 are for negative testing
        if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("CFB150")) {
            System.out.println("Unexpected NoSuchAlgorithmException with mode: " + mo);
            throw new RuntimeException("Test failed!");
        }
    } catch (NoSuchProviderException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | ShortBufferException | IllegalBlockSizeException | BadPaddingException e) {
        System.out.println("Test failed!");
        throw e;
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKey(javax.crypto.SecretKey) Random(java.util.Random) ShortBufferException(javax.crypto.ShortBufferException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NoSuchProviderException(java.security.NoSuchProviderException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) KeyGenerator(javax.crypto.KeyGenerator)

Example 93 with InvalidAlgorithmParameterException

use of java.security.InvalidAlgorithmParameterException in project jdk8u_jdk by JetBrains.

the class Padding method runTest.

public void runTest(String algo, String mo, String pad) throws Exception {
    Cipher ci = null;
    byte[] iv = null;
    AlgorithmParameterSpec aps = null;
    SecretKey key = null;
    try {
        Random rdm = new Random();
        byte[] plainText;
        ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
        KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);
        kg.init(KEY_LENGTH);
        key = kg.generateKey();
        for (int i = 0; i < 15; i++) {
            plainText = new byte[1600 + i + 1];
            rdm.nextBytes(plainText);
            if (!mo.equalsIgnoreCase("GCM")) {
                ci.init(Cipher.ENCRYPT_MODE, key, aps);
            } else {
                ci.init(Cipher.ENCRYPT_MODE, key);
            }
            byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
            int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
            ci.doFinal(cipherText, offset);
            if (!mo.equalsIgnoreCase("ECB")) {
                iv = ci.getIV();
                aps = new IvParameterSpec(iv);
            } else {
                aps = null;
            }
            if (!mo.equalsIgnoreCase("GCM")) {
                ci.init(Cipher.DECRYPT_MODE, key, aps);
            } else {
                ci.init(Cipher.DECRYPT_MODE, key, ci.getParameters());
            }
            byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
            int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
            byte[] tmp = new byte[len];
            for (int j = 0; j < len; j++) {
                tmp[j] = recoveredText[j];
            }
            if (!java.util.Arrays.equals(plainText, tmp)) {
                System.out.println("Original: ");
                dumpBytes(plainText);
                System.out.println("Recovered: ");
                dumpBytes(tmp);
                throw new RuntimeException("Original text is not equal with recovered text, with mode:" + mo);
            }
        }
    } catch (NoSuchAlgorithmException e) {
        //CFB7 and OFB150 are for negative testing
        if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("OFB150")) {
            System.out.println("Unexpected NoSuchAlgorithmException with mode: " + mo);
            throw new RuntimeException("Test failed!");
        }
    } catch (NoSuchProviderException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | ShortBufferException | IllegalBlockSizeException | BadPaddingException e) {
        System.out.println("Test failed!");
        throw e;
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKey(javax.crypto.SecretKey) Random(java.util.Random) ShortBufferException(javax.crypto.ShortBufferException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NoSuchProviderException(java.security.NoSuchProviderException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) KeyGenerator(javax.crypto.KeyGenerator)

Example 94 with InvalidAlgorithmParameterException

use of java.security.InvalidAlgorithmParameterException in project jdk8u_jdk by JetBrains.

the class TestAESCipher method runTest.

public void runTest(String algo, String mo, String pad) throws Exception {
    Cipher ci = null;
    byte[] iv = null;
    AlgorithmParameterSpec aps = null;
    SecretKey key = null;
    try {
        // Initialization
        Random rdm = new Random();
        byte[] plainText = new byte[128];
        rdm.nextBytes(plainText);
        ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
        KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);
        kg.init(KEY_LENGTH);
        key = kg.generateKey();
        // encrypt
        if (!mo.equalsIgnoreCase("GCM")) {
            ci.init(Cipher.ENCRYPT_MODE, key, aps);
        } else {
            ci.init(Cipher.ENCRYPT_MODE, key);
        }
        byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
        int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
        ci.doFinal(cipherText, offset);
        if (!mo.equalsIgnoreCase("ECB")) {
            iv = ci.getIV();
            aps = new IvParameterSpec(iv);
        } else {
            aps = null;
        }
        if (!mo.equalsIgnoreCase("GCM")) {
            ci.init(Cipher.DECRYPT_MODE, key, aps);
        } else {
            ci.init(Cipher.DECRYPT_MODE, key, ci.getParameters());
        }
        byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
        int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
        byte[] tmp = new byte[len];
        System.arraycopy(recoveredText, 0, tmp, 0, len);
        // Comparison
        if (!java.util.Arrays.equals(plainText, tmp)) {
            System.out.println("Original: ");
            dumpBytes(plainText);
            System.out.println("Recovered: ");
            dumpBytes(tmp);
            throw new RuntimeException("Original text is not equal with recovered text, with mode:" + mo);
        }
    } catch (NoSuchAlgorithmException e) {
        //CFB7 and OFB150 are for negative testing
        if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("OFB150")) {
            System.out.println("Unexpected NoSuchAlgorithmException with mode: " + mo);
            throw new RuntimeException("Test failed!");
        }
    } catch (NoSuchProviderException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | ShortBufferException | IllegalBlockSizeException | BadPaddingException e) {
        System.out.println("Test failed!");
        throw e;
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKey(javax.crypto.SecretKey) Random(java.util.Random) ShortBufferException(javax.crypto.ShortBufferException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NoSuchProviderException(java.security.NoSuchProviderException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) KeyGenerator(javax.crypto.KeyGenerator)

Example 95 with InvalidAlgorithmParameterException

use of java.security.InvalidAlgorithmParameterException in project jdk8u_jdk by JetBrains.

the class DHKeyAgreement method engineInit.

/**
     * Initializes this key agreement with the given key, set of
     * algorithm parameters, and source of randomness.
     *
     * @param key the party's private information. For example, in the case
     * of the Diffie-Hellman key agreement, this would be the party's own
     * Diffie-Hellman private key.
     * @param params the key agreement parameters
     * @param random the source of randomness
     *
     * @exception InvalidKeyException if the given key is
     * inappropriate for this key agreement, e.g., is of the wrong type or
     * has an incompatible algorithm type.
     * @exception InvalidAlgorithmParameterException if the given parameters
     * are inappropriate for this key agreement.
     */
protected void engineInit(Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    // ignore "random" parameter, because our implementation does not
    // require any source of randomness
    generateSecret = false;
    init_p = null;
    init_g = null;
    if ((params != null) && !(params instanceof DHParameterSpec)) {
        throw new InvalidAlgorithmParameterException("Diffie-Hellman parameters expected");
    }
    if (!(key instanceof javax.crypto.interfaces.DHPrivateKey)) {
        throw new InvalidKeyException("Diffie-Hellman private key " + "expected");
    }
    javax.crypto.interfaces.DHPrivateKey dhPrivKey;
    dhPrivKey = (javax.crypto.interfaces.DHPrivateKey) key;
    // initialized ones
    if (params != null) {
        init_p = ((DHParameterSpec) params).getP();
        init_g = ((DHParameterSpec) params).getG();
    }
    BigInteger priv_p = dhPrivKey.getParams().getP();
    BigInteger priv_g = dhPrivKey.getParams().getG();
    if (init_p != null && priv_p != null && !(init_p.equals(priv_p))) {
        throw new InvalidKeyException("Incompatible parameters");
    }
    if (init_g != null && priv_g != null && !(init_g.equals(priv_g))) {
        throw new InvalidKeyException("Incompatible parameters");
    }
    if ((init_p == null && priv_p == null) || (init_g == null && priv_g == null)) {
        throw new InvalidKeyException("Missing parameters");
    }
    init_p = priv_p;
    init_g = priv_g;
    // store the x value
    this.x = dhPrivKey.getX();
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) BigInteger(java.math.BigInteger) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)394 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)216 InvalidKeyException (java.security.InvalidKeyException)206 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)130 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)114 BadPaddingException (javax.crypto.BadPaddingException)112 Cipher (javax.crypto.Cipher)101 IvParameterSpec (javax.crypto.spec.IvParameterSpec)100 IOException (java.io.IOException)74 SecretKeySpec (javax.crypto.spec.SecretKeySpec)58 NoSuchProviderException (java.security.NoSuchProviderException)56 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)49 CertificateException (java.security.cert.CertificateException)45 KeyStoreException (java.security.KeyStoreException)43 SecureRandom (java.security.SecureRandom)37 SecretKey (javax.crypto.SecretKey)34 BigInteger (java.math.BigInteger)31 KeyPairGenerator (java.security.KeyPairGenerator)27 UnrecoverableKeyException (java.security.UnrecoverableKeyException)27 X509Certificate (java.security.cert.X509Certificate)27