Search in sources :

Example 66 with AlgorithmParameters

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

the class EncryptedPrivateKeyInfoTest method testGetAlgParameters04.

/**
     * Test #4 for <code>getAlgParameters()</code> method <br>
     * Assertion: returns the algorithm parameters <br>
     * Test preconditions: test object created using ctor which takes
     * AlgorithmParameters and encrypted data as a parameters; <br>
     * Expected: the same algorithm parameters as ones passed to the ctor must be
     * returned
     *
     * @throws IOException
     */
public final void testGetAlgParameters04() throws IOException {
    boolean performed = false;
    for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
        try {
            AlgorithmParameters ap = AlgorithmParameters.getInstance(EncryptedPrivateKeyInfoData.algName0[i][0]);
            // use pregenerated AlgorithmParameters encodings
            ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding(EncryptedPrivateKeyInfoData.algName0[i][0]));
            EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(ap, EncryptedPrivateKeyInfoData.encryptedData);
            // check that method under test returns
            // the same parameters instance
            assertSame(ap, epki.getAlgParameters());
            performed = true;
        } catch (NoSuchAlgorithmException allowedFailure) {
        }
    }
    assertTrue("Test not performed", performed);
}
Also used : EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 67 with AlgorithmParameters

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

the class AlgorithmParametersTest method test_getEncodedLjava_lang_String.

/**
     * java.security.AlgorithmParameters#getEncoded(String)
     */
public void test_getEncodedLjava_lang_String() throws Exception {
    final byte[] enc = new byte[] { 0x02, 0x01, 0x03 };
    final String strFormatParam = "format";
    MyAlgorithmParameters paramSpi = new MyAlgorithmParameters() {

        protected byte[] engineGetEncoded(String format) throws IOException {
            assertEquals(strFormatParam, format);
            return enc;
        }
    };
    AlgorithmParameters params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    //
    try {
        params.getEncoded(strFormatParam);
        fail("should not get encoded from un-initialized instance");
    } catch (IOException e) {
    // expected
    }
    //
    // test: corresponding spi method is invoked
    //
    params.init(new MyAlgorithmParameterSpec());
    assertSame(enc, params.getEncoded(strFormatParam));
    //
    // test: if format param is null
    // Regression test for HARMONY-2680
    //
    paramSpi = new MyAlgorithmParameters() {

        protected byte[] engineGetEncoded(String format) throws IOException {
            // null is passed to spi-provider
            assertNull(format);
            return enc;
        }
    };
    params = new DummyAlgorithmParameters(paramSpi, p, "algorithm");
    params.init(new MyAlgorithmParameterSpec());
    assertSame(enc, params.getEncoded(null));
}
Also used : IOException(java.io.IOException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 68 with AlgorithmParameters

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

the class AlgorithmParametersTest method testAlgorithmParametersConst.

/**
     * Test for <code>AlgorithmParameters</code> constructor
     * Assertion: returns AlgorithmParameters object
     */
public void testAlgorithmParametersConst() throws Exception {
    AlgorithmParametersSpi spi = new MyAlgorithmParameters();
    AlgorithmParameters ap = new myAlgP(spi, p, "ABC");
    checkUnititialized(ap);
    ap.init(new byte[6], "aaa");
    checkAP(ap, p);
    //NULL parameters
    try {
        ap = new myAlgP(null, null, null);
    } catch (Exception e) {
        fail("Exception should be not thrown");
    }
}
Also used : AlgorithmParametersSpi(java.security.AlgorithmParametersSpi) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) NoSuchProviderException(java.security.NoSuchProviderException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 69 with AlgorithmParameters

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

the class AlgorithmParametersTest method test_getInstanceLjava_lang_String.

/**
     * java.security.AlgorithmParameters#getInstance(String)
     */
public void test_getInstanceLjava_lang_String() {
    String[] str = { "", "qwertyu", "!@#$%^&*()" };
    try {
        AlgorithmParameters ap = AlgorithmParameters.getInstance("ABC");
        checkUnititialized(ap);
        ap.init(new MyAlgorithmParameterSpec());
        checkAP(ap, p);
    } catch (Exception e) {
        fail("Unexpected exception");
    }
    for (int i = 0; i < str.length; i++) {
        try {
            AlgorithmParameters ap = AlgorithmParameters.getInstance(str[i]);
            fail("NoSuchAlgorithmException was not thrown for parameter " + str[i]);
        } catch (NoSuchAlgorithmException nsae) {
        //expected
        }
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) NoSuchProviderException(java.security.NoSuchProviderException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 70 with AlgorithmParameters

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

the class AlgorithmParametersTest method test_getInstanceLjava_lang_StringLjava_security_Provider.

/**
     * java.security.AlgorithmParameters#getInstance(String, Provider)
     */
public void test_getInstanceLjava_lang_StringLjava_security_Provider() {
    String[] alg = { "", "qwertyu", "!@#$%^&*()" };
    Provider pp = null;
    try {
        AlgorithmParameters ap = AlgorithmParameters.getInstance("ABC", p);
        checkUnititialized(ap);
        ap.init(new byte[6], "aaa");
        checkAP(ap, p);
    } catch (Exception e) {
        fail("Unexpected exception");
    }
    for (int i = 0; i < alg.length; i++) {
        try {
            AlgorithmParameters ap = AlgorithmParameters.getInstance(alg[i], p);
            fail("NoSuchAlgorithmException was not thrown for parameter " + alg[i]);
        } catch (NoSuchAlgorithmException nsae) {
        //expected
        } catch (Exception e) {
            fail("Incorrect exception " + e + " was thrown for " + alg[i]);
        }
    }
    try {
        AlgorithmParameters ap = AlgorithmParameters.getInstance("ABC", pp);
        fail("IllegalArgumentException was not thrown for NULL provider");
    } catch (IllegalArgumentException iae) {
    //expected
    } catch (Exception e) {
        fail("Incorrect exception " + e + " was thrown");
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) NoSuchProviderException(java.security.NoSuchProviderException) Provider(java.security.Provider) AlgorithmParameters(java.security.AlgorithmParameters)

Aggregations

AlgorithmParameters (java.security.AlgorithmParameters)107 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)39 IOException (java.io.IOException)31 InvalidParameterSpecException (java.security.spec.InvalidParameterSpecException)22 Cipher (javax.crypto.Cipher)22 SecretKey (javax.crypto.SecretKey)18 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)13 NoSuchProviderException (java.security.NoSuchProviderException)12 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)11 Key (java.security.Key)11 SecureRandom (java.security.SecureRandom)10 EncryptedPrivateKeyInfo (javax.crypto.EncryptedPrivateKeyInfo)10 InvalidKeyException (java.security.InvalidKeyException)8 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)8 UnrecoverableKeyException (java.security.UnrecoverableKeyException)7 KeyPair (java.security.KeyPair)6 KeyPairGenerator (java.security.KeyPairGenerator)6 AlgorithmId (sun.security.x509.AlgorithmId)6 Nullable (android.annotation.Nullable)5 Asn1Integer (com.android.hotspot2.asn1.Asn1Integer)5