Search in sources :

Example 16 with AlgorithmParameters

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

the class EncryptedPrivateKeyInfoTest method testGetAlgParameters01.

/**
     * Test #1 for <code>getAlgParameters()</code> method <br>
     * Assertion: returns the algorithm parameters <br>
     * Test preconditions: test object created using ctor which takes encoded
     * form as the only parameter; encoded form passed contains algorithm
     * parameters encoding <br>
     * Expected: corresponding algorithm parameters must be returned
     *
     * @throws IOException
     */
public final void testGetAlgParameters01() throws IOException {
    boolean performed = false;
    for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
        try {
            EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(EncryptedPrivateKeyInfoData.getValidEncryptedPrivateKeyInfoEncoding(EncryptedPrivateKeyInfoData.algName0[i][0]));
            AlgorithmParameters apar = epki.getAlgParameters();
            if (apar == null) {
                continue;
            }
            // check that method under test returns
            // parameters with the same encoded form
            assertTrue(Arrays.equals(EncryptedPrivateKeyInfoData.getParametersEncoding(EncryptedPrivateKeyInfoData.algName0[i][0]), apar.getEncoded()));
            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 17 with AlgorithmParameters

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

the class EncryptedPrivateKeyInfoTest method testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray1.

/**
     * Test #1 for
     * <code>EncryptedPrivateKeyInfo(java.security.AlgorithmParameters, byte[])
     * </code>
     * constructor <br>
     * Assertion: creates <code>EncryptedPrivateKeyInfo</code> instance <br>
     * Test preconditions: valid parameters passed <br>
     * Expected: must pass without any exceptions
     *
     * @throws IOException
     * @throws NoSuchAlgorithmException
     */
public final void testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray1() throws IOException, NoSuchAlgorithmException {
    AlgorithmParameters ap = null;
    boolean performed = false;
    for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
        try {
            ap = AlgorithmParameters.getInstance(EncryptedPrivateKeyInfoData.algName0[i][0]);
            // use pregenerated AlgorithmParameters encodings
            ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding(EncryptedPrivateKeyInfoData.algName0[i][0]));
            new EncryptedPrivateKeyInfo(ap, EncryptedPrivateKeyInfoData.encryptedData);
            performed = true;
        } catch (NoSuchAlgorithmException allowedFailure) {
        }
    }
    assertTrue("Test not performed", performed);
    ap = new Mock_AlgorithmParameters(null, null, "Wrong alg name");
    try {
        new EncryptedPrivateKeyInfo(ap, EncryptedPrivateKeyInfoData.encryptedData);
        fail("NoSuchAlgorithmException expected");
    } catch (NoSuchAlgorithmException e) {
    //expected
    }
}
Also used : EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 18 with AlgorithmParameters

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

the class EncryptedPrivateKeyInfoTest method testGetEncryptedData03.

/**
     * Test #3 for <code>getEncryptedData()</code> method <br>
     * Assertion: returns the encrypted data <br>
     * Test preconditions: test object created using ctor which takes algorithm
     * parameters and encrypted data as a parameters <br>
     * Expected: the equivalent encrypted data must be returned
     *
     * @throws IOException
     */
public final void testGetEncryptedData03() 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
            // valid encrypted data
            assertTrue(Arrays.equals(EncryptedPrivateKeyInfoData.encryptedData, epki.getEncryptedData()));
            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 19 with AlgorithmParameters

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

the class EncryptedPrivateKeyInfoTest method testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray3.

/**
     * Test #3 for
     * <code>EncryptedPrivateKeyInfo(java.security.AlgorithmParameters, byte[])
     * </code>
     * constructor <br>
     * Assertion: <code>IllegalArgumentException</code>- if encrypted data is
     * empty, i.e. 0-length <br>
     * Test preconditions: pass empty encrypted data <br>
     * Expected: <code>IllegalArgumentException</code>
     *
     * @throws NoSuchAlgorithmException
     * @throws IOException
     */
public final void testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray3() throws Exception {
    try {
        AlgorithmParameters ap = AlgorithmParameters.getInstance("DSA");
        // use pregenerated AlgorithmParameters encodings
        ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding("DSA"));
        new EncryptedPrivateKeyInfo(ap, new byte[] {});
        fail(getName() + ": IllegalArgumentException has not been thrown");
    } catch (IllegalArgumentException ok) {
    }
}
Also used : EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) AlgorithmParameters(java.security.AlgorithmParameters)

Example 20 with AlgorithmParameters

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

the class AlgorithmParameterGeneratorSpiTest method testAlgorithmParameterGeneratorSpi01.

/**
     * Test for <code>AlgorithmParameterGeneratorSpi</code> constructor
     * Assertion: constructs AlgorithmParameterGeneratorSpi
     */
public void testAlgorithmParameterGeneratorSpi01() throws InvalidAlgorithmParameterException {
    MyAlgorithmParameterGeneratorSpi algParGen = new MyAlgorithmParameterGeneratorSpi();
    AlgorithmParameters param = algParGen.engineGenerateParameters();
    assertNull("Not null parameters", param);
    AlgorithmParameterSpec pp = null;
    algParGen.engineInit(pp, new SecureRandom());
    try {
        algParGen.engineInit(pp, null);
        fail("IllegalArgumentException must be thrown");
    } catch (IllegalArgumentException e) {
    }
    algParGen.engineInit(0, null);
    algParGen.engineInit(0, new SecureRandom());
    try {
        algParGen.engineInit(-10, null);
        fail("IllegalArgumentException must be thrown");
    } catch (IllegalArgumentException e) {
    }
    try {
        algParGen.engineInit(-10, new SecureRandom());
        fail("IllegalArgumentException must be thrown");
    } catch (IllegalArgumentException e) {
    }
}
Also used : MyAlgorithmParameterGeneratorSpi(org.apache.harmony.security.tests.support.MyAlgorithmParameterGeneratorSpi) SecureRandom(java.security.SecureRandom) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) 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