Search in sources :

Example 6 with InvalidParameterSpecException

use of java.security.spec.InvalidParameterSpecException in project robovm by robovm.

the class CipherSpi method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    AlgorithmParameterSpec paramSpec = null;
    if (params != null) {
        try {
            paramSpec = params.getParameterSpec(OAEPParameterSpec.class);
        } catch (InvalidParameterSpecException e) {
            throw new InvalidAlgorithmParameterException("cannot recognise parameters: " + e.toString(), e);
        }
    }
    engineParams = params;
    engineInit(opmode, key, paramSpec, random);
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) OAEPParameterSpec(javax.crypto.spec.OAEPParameterSpec)

Example 7 with InvalidParameterSpecException

use of java.security.spec.InvalidParameterSpecException in project robovm by robovm.

the class InvalidParameterSpecExceptionTest method testInvalidParameterSpecException01.

/**
     * Test for <code>InvalidParameterSpecException()</code> constructor
     * Assertion: constructs InvalidParameterSpecException with no detail
     * message
     */
public void testInvalidParameterSpecException01() {
    InvalidParameterSpecException tE = new InvalidParameterSpecException();
    assertNull("getMessage() must return null.", tE.getMessage());
    assertNull("getCause() must return null", tE.getCause());
}
Also used : InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException)

Example 8 with InvalidParameterSpecException

use of java.security.spec.InvalidParameterSpecException in project robovm by robovm.

the class AlgorithmParametersTest method test_getParameterSpecLjava_lang_Class.

/**
     * java.security.AlgorithmParameters#getParameterSpec(Class)
     */
public void test_getParameterSpecLjava_lang_Class() throws Exception {
    final MyAlgorithmParameterSpec myParamSpec = new MyAlgorithmParameterSpec();
    MyAlgorithmParameters paramSpi = new MyAlgorithmParameters() {

        protected AlgorithmParameterSpec engineGetParameterSpec(Class paramSpec) {
            return myParamSpec;
        }
    };
    AlgorithmParameters params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    //
    try {
        params.getParameterSpec(null);
        fail("No expected InvalidParameterSpecException");
    } catch (InvalidParameterSpecException e) {
    // expected
    }
    try {
        params.getParameterSpec(MyAlgorithmParameterSpec.class);
        fail("No expected InvalidParameterSpecException");
    } catch (InvalidParameterSpecException e) {
    // expected
    }
    //
    // test: corresponding spi method is invoked
    //
    params.init(new MyAlgorithmParameterSpec());
    assertSame(myParamSpec, params.getParameterSpec(MyAlgorithmParameterSpec.class));
    //
    // test: if paramSpec is null
    // Regression test for HARMONY-2733
    //
    paramSpi = new MyAlgorithmParameters() {

        protected AlgorithmParameterSpec engineGetParameterSpec(Class paramSpec) {
            // null is passed to spi-provider
            assertNull(paramSpec);
            return null;
        }
    };
    params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init(new MyAlgorithmParameterSpec());
    assertNull(params.getParameterSpec(null));
}
Also used : InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) AlgorithmParameters(java.security.AlgorithmParameters)

Example 9 with InvalidParameterSpecException

use of java.security.spec.InvalidParameterSpecException in project robovm by robovm.

the class AlgorithmParametersTest method test_initLjava_security_spec_AlgorithmParameterSpec.

/**
     * java.security.AlgorithmParameters#init(java.security.spec.AlgorithmParameterSpec)
     */
public void test_initLjava_security_spec_AlgorithmParameterSpec() throws Exception {
    //
    // test: corresponding spi method is invoked
    //
    final MyAlgorithmParameterSpec spec = new MyAlgorithmParameterSpec();
    MyAlgorithmParameters paramSpi = new MyAlgorithmParameters() {

        protected void engineInit(AlgorithmParameterSpec paramSpec) throws InvalidParameterSpecException {
            assertSame(spec, paramSpec);
            runEngineInit_AlgParamSpec = true;
        }
    };
    AlgorithmParameters params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init(spec);
    assertTrue(paramSpi.runEngineInit_AlgParamSpec);
    //
    try {
        params.init(spec);
        fail("No expected InvalidParameterSpecException");
    } catch (InvalidParameterSpecException e) {
    // expected
    }
    params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init(new byte[0]);
    try {
        params.init(spec);
        fail("No expected InvalidParameterSpecException");
    } catch (InvalidParameterSpecException e) {
    // expected
    }
    params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init(new byte[0], "format");
    try {
        params.init(spec);
        fail("No expected InvalidParameterSpecException");
    } catch (InvalidParameterSpecException e) {
    // expected
    }
    //
    // test: if paramSpec is null
    //
    paramSpi = new MyAlgorithmParameters() {

        protected void engineInit(AlgorithmParameterSpec paramSpec) throws InvalidParameterSpecException {
            // null is passed to spi-provider
            assertNull(paramSpec);
            runEngineInit_AlgParamSpec = true;
        }
    };
    params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init((AlgorithmParameterSpec) null);
    assertTrue(paramSpi.runEngineInit_AlgParamSpec);
}
Also used : InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) AlgorithmParameters(java.security.AlgorithmParameters)

Example 10 with InvalidParameterSpecException

use of java.security.spec.InvalidParameterSpecException 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)

Aggregations

InvalidParameterSpecException (java.security.spec.InvalidParameterSpecException)54 AlgorithmParameters (java.security.AlgorithmParameters)19 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)15 IvParameterSpec (javax.crypto.spec.IvParameterSpec)14 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)11 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)11 InvalidKeyException (java.security.InvalidKeyException)6 DSAParameterSpec (java.security.spec.DSAParameterSpec)6 OAEPParameterSpec (javax.crypto.spec.OAEPParameterSpec)6 Nullable (android.annotation.Nullable)5 ProviderException (java.security.ProviderException)5 DSAParams (java.security.interfaces.DSAParams)4 BadPaddingException (javax.crypto.BadPaddingException)4 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)4 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)4 IOException (java.io.IOException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 BigInteger (java.math.BigInteger)3 NoSuchProviderException (java.security.NoSuchProviderException)3 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)3