Search in sources :

Example 51 with InvalidAlgorithmParameterException

use of java.security.InvalidAlgorithmParameterException in project robovm by robovm.

the class MyProvider method test_engineInit_02.

/**
     * @throws InvalidAlgorithmParameterException
     * @throws NoSuchAlgorithmException
     * javax.net.ssl.TrustManagerFactorySpi#engineInit(ManagerFactoryParameters spec)
     */
public void test_engineInit_02() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException {
    factory.reset();
    Provider provider = new MyProvider();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance("MyTMF", provider);
    Parameters pr = null;
    try {
        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
        ks.load(null, null);
        pr = new Parameters(ks);
        tmf.init(pr);
    } catch (Exception e) {
        fail("Unexpected exception " + e.toString());
    }
    assertTrue(factory.isEngineInitCalled());
    assertEquals(pr, factory.getSpec());
    factory.reset();
    tmf.init((ManagerFactoryParameters) null);
    assertTrue(factory.isEngineInitCalled());
    assertNull(factory.getSpec());
}
Also used : Parameters(org.apache.harmony.xnet.tests.support.MyTrustManagerFactorySpi.Parameters) ManagerFactoryParameters(javax.net.ssl.ManagerFactoryParameters) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyStore(java.security.KeyStore) KeyStoreException(java.security.KeyStoreException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Provider(java.security.Provider)

Example 52 with InvalidAlgorithmParameterException

use of java.security.InvalidAlgorithmParameterException in project robovm by robovm.

the class TrustManagerFactory2Test method checkResult.

private void checkResult(TrustManagerFactory tmf) throws Exception {
    KeyStore kStore = null;
    ManagerFactoryParameters mfp = null;
    try {
        tmf.init(kStore);
        fail("KeyStoreException must be thrown");
    } catch (KeyStoreException e) {
    }
    try {
        tmf.init(mfp);
        fail("InvalidAlgorithmParameterException must be thrown");
    } catch (InvalidAlgorithmParameterException e) {
    }
    assertNull("getTrustManagers() should return null object", tmf.getTrustManagers());
    try {
        kStore = KeyStore.getInstance(KeyStore.getDefaultType());
        kStore.load(null, null);
    } catch (KeyStoreException e) {
        fail("default keystore is not supported");
        return;
    }
    tmf.init(kStore);
    mfp = (ManagerFactoryParameters) new MyTrustManagerFactorySpi.Parameters(null);
    try {
        tmf.init(mfp);
        fail("RuntimeException must be thrown");
    } catch (RuntimeException e) {
        assertTrue("Incorrect exception", e.getCause() instanceof KeyStoreException);
    }
    mfp = (ManagerFactoryParameters) new MyTrustManagerFactorySpi.Parameters(kStore);
    tmf.init(mfp);
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ManagerFactoryParameters(javax.net.ssl.ManagerFactoryParameters) KeyStoreException(java.security.KeyStoreException) KeyStore(java.security.KeyStore) ManagerFactoryParameters(javax.net.ssl.ManagerFactoryParameters)

Example 53 with InvalidAlgorithmParameterException

use of java.security.InvalidAlgorithmParameterException in project robovm by robovm.

the class myAlgPG method testAlgorithmParameterGenerator12.

/**
     * Test for <code>init(AlgorithmParameterSpec param)</code> and
     * <code>init(AlgorithmParameterSpec param, SecureRandom random<code>
     * methods
     * Assertion: throws InvalidAlgorithmParameterException when param is null
     */
public void testAlgorithmParameterGenerator12() {
    if (!DSASupported) {
        fail(validAlgName + " algorithm is not supported");
        return;
    }
    SecureRandom random = new SecureRandom();
    AlgorithmParameterSpec aps = null;
    AlgorithmParameterGenerator[] apgs = createAPGen();
    assertNotNull("AlgorithmParameterGenerator objects were not created", apgs);
    for (int i = 0; i < apgs.length; i++) {
        try {
            apgs[i].init(aps);
            fail("InvalidAlgorithmParameterException expected for null argument.");
        } catch (InvalidAlgorithmParameterException e) {
        //expected
        }
        try {
            apgs[i].init(aps, random);
            fail("InvalidAlgorithmParameterException expected for null argument.");
        } catch (InvalidAlgorithmParameterException e) {
        //expected
        }
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecureRandom(java.security.SecureRandom) AlgorithmParameterGenerator(java.security.AlgorithmParameterGenerator) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 54 with InvalidAlgorithmParameterException

use of java.security.InvalidAlgorithmParameterException in project robovm by robovm.

the class AlgorithmParameterSignatureHelper method test.

@Override
public void test(AlgorithmParameters parameters) {
    Signature signature = null;
    try {
        signature = Signature.getInstance(algorithmName);
    } catch (NoSuchAlgorithmException e) {
        Assert.fail(e.getMessage());
    }
    T parameterSpec = null;
    try {
        parameterSpec = parameters.getParameterSpec(parameterSpecClass);
    } catch (InvalidParameterSpecException e) {
        Assert.fail(e.getMessage());
    }
    KeyPairGenerator generator = null;
    try {
        generator = KeyPairGenerator.getInstance(algorithmName);
    } catch (NoSuchAlgorithmException e) {
        Assert.fail(e.getMessage());
    }
    try {
        generator.initialize(parameterSpec);
    } catch (InvalidAlgorithmParameterException e) {
        Assert.fail(e.getMessage());
    }
    KeyPair keyPair = generator.genKeyPair();
    try {
        signature.initSign(keyPair.getPrivate());
    } catch (InvalidKeyException e) {
        Assert.fail(e.getMessage());
    }
    try {
        signature.update(plainData.getBytes());
    } catch (SignatureException e) {
        Assert.fail(e.getMessage());
    }
    byte[] signed = null;
    try {
        signed = signature.sign();
    } catch (SignatureException e) {
        Assert.fail(e.getMessage());
    }
    try {
        signature.initVerify(keyPair.getPublic());
    } catch (InvalidKeyException e) {
        Assert.fail(e.getMessage());
    }
    try {
        signature.update(plainData.getBytes());
    } catch (SignatureException e) {
        Assert.fail(e.getMessage());
    }
    try {
        Assert.assertTrue("signature could not be verified", signature.verify(signed));
    } catch (SignatureException e) {
        Assert.fail(e.getMessage());
    }
}
Also used : KeyPair(java.security.KeyPair) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) Signature(java.security.Signature) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) KeyPairGenerator(java.security.KeyPairGenerator) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException)

Example 55 with InvalidAlgorithmParameterException

use of java.security.InvalidAlgorithmParameterException in project robovm by robovm.

the class AlgorithmParameterSymmetricHelper method test.

@Override
public void test(AlgorithmParameters parameters) {
    KeyGenerator generator = null;
    try {
        generator = KeyGenerator.getInstance(algorithmName);
    } catch (NoSuchAlgorithmException e) {
        Assert.fail(e.getMessage());
    }
    generator.init(keySize);
    Key key = generator.generateKey();
    Cipher cipher = null;
    try {
        String transformation = algorithmName;
        if (blockmode != null) {
            transformation += "/" + blockmode;
        }
        cipher = Cipher.getInstance(transformation);
    } catch (NoSuchAlgorithmException e) {
        Assert.fail(e.getMessage());
    } catch (NoSuchPaddingException e) {
        Assert.fail(e.getMessage());
    }
    try {
        cipher.init(Cipher.ENCRYPT_MODE, key, parameters);
    } catch (InvalidKeyException e) {
        Assert.fail(e.getMessage());
    } catch (InvalidAlgorithmParameterException e) {
        Assert.fail(e.getMessage());
    }
    byte[] bs = null;
    try {
        bs = cipher.doFinal(plainData.getBytes());
    } catch (IllegalBlockSizeException e) {
        Assert.fail(e.getMessage());
    } catch (BadPaddingException e) {
        Assert.fail(e.getMessage());
    }
    try {
        cipher.init(Cipher.DECRYPT_MODE, key, parameters);
    } catch (InvalidKeyException e) {
        Assert.fail(e.getMessage());
    } catch (InvalidAlgorithmParameterException e) {
        Assert.fail(e.getMessage());
    }
    byte[] decrypted = null;
    try {
        decrypted = cipher.doFinal(bs);
    } catch (IllegalBlockSizeException e) {
        Assert.fail(e.getMessage());
    } catch (BadPaddingException e) {
        Assert.fail(e.getMessage());
    }
    Assert.assertTrue(Arrays.equals(plainData.getBytes(), decrypted));
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Cipher(javax.crypto.Cipher) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key)

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