Search in sources :

Example 6 with EncryptedPrivateKeyInfo

use of javax.crypto.EncryptedPrivateKeyInfo in project robovm by robovm.

the class EncryptedPrivateKeyInfoTest method testEncryptedPrivateKeyInfobyteArray6.

/**
     * Test #6 for <code>EncryptedPrivateKeyInfo(byte[])</code> constructor
     * <br>
     * Assertion: byte array is copied to prevent subsequent modification <br>
     * Test preconditions: valid array passed then modified <br>
     * Expected: getEncoded(), invoked after above modification, must return
     * array as it was before the modification
     *
     * @throws IOException
     */
public final void testEncryptedPrivateKeyInfobyteArray6() throws Exception {
    byte[] encoded = EncryptedPrivateKeyInfoData.getValidEncryptedPrivateKeyInfoEncoding("DSA");
    byte[] encodedCopy = encoded.clone();
    // pass valid array
    EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(encodedCopy);
    // modify array passed
    encodedCopy[9] = (byte) 6;
    // check that internal state has not been affected
    assertTrue(Arrays.equals(encoded, epki.getEncoded()));
}
Also used : EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo)

Example 7 with EncryptedPrivateKeyInfo

use of javax.crypto.EncryptedPrivateKeyInfo in project robovm by robovm.

the class EncryptedPrivateKeyInfoTest method test_ROUNDTRIP_GetKeySpecKeyProvider02.

/**
     * Encrypted data contains invalid PKCS8 key info encoding
     */
public final void test_ROUNDTRIP_GetKeySpecKeyProvider02() {
    boolean performed = false;
    for (int i = 0; i < algName.length; i++) {
        for (int l = 0; l < provider.length; l++) {
            if (provider[l] == null) {
                continue;
            }
            TestDataGenerator g;
            try {
                // generate test data
                g = new TestDataGenerator(algName[i][0], algName[i][1], privateKeyInfoDamaged, provider[l]);
            } catch (TestDataGenerator.AllowedFailure allowedFailure) {
                continue;
            }
            try {
                // create test object
                EncryptedPrivateKeyInfo epki;
                if (g.ap() == null) {
                    epki = new EncryptedPrivateKeyInfo(algName[i][0], g.ct());
                } else {
                    epki = new EncryptedPrivateKeyInfo(g.ap(), g.ct());
                }
                try {
                    epki.getKeySpec(g.pubK() == null ? g.k() : g.pubK(), provider[l]);
                    fail(algName[i][0] + ", " + algName[i][1]);
                } catch (InvalidKeyException e) {
                }
                performed = true;
            } catch (NoSuchAlgorithmException allowedFailure) {
            }
        }
    }
    assertTrue("Test not performed", performed);
}
Also used : EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 8 with EncryptedPrivateKeyInfo

use of javax.crypto.EncryptedPrivateKeyInfo in project robovm by robovm.

the class EncryptedPrivateKeyInfoTest method testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray2.

/**
     * Test #2 for
     * <code>EncryptedPrivateKeyInfo(java.security.AlgorithmParameters, byte[])
     * </code>
     * constructor <br>
     * Assertion: <code>NullPointerException</code>- if the specified
     * algorithm parameters or encrypted data is <code>null</code><br>
     * Test preconditions: pass <code>null</code> as algorithm parameters then
     * as encrypted data <br>
     * Expected: <code>NullPointerException</code> in both cases
     *
     * @throws NoSuchAlgorithmException
     * @throws IOException
     */
public final void testEncryptedPrivateKeyInfoAlgorithmParametersbyteArray2() throws NoSuchAlgorithmException, IOException {
    // 1: pass null as AlgorithmParameters
    try {
        new EncryptedPrivateKeyInfo((AlgorithmParameters) null, EncryptedPrivateKeyInfoData.encryptedData);
        fail(getName() + ": NullPointerException has not been thrown");
    } catch (NullPointerException ok) {
    }
    // 2: pass null as encrypted data
    try {
        AlgorithmParameters ap = AlgorithmParameters.getInstance("DSA");
        // use pregenerated AlgorithmParameters encodings
        ap.init(EncryptedPrivateKeyInfoData.getParametersEncoding("DSA"));
        new EncryptedPrivateKeyInfo(ap, null);
        fail(getName() + ": NullPointerException has not been thrown");
    } catch (NullPointerException ok) {
    }
}
Also used : EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) AlgorithmParameters(java.security.AlgorithmParameters)

Example 9 with EncryptedPrivateKeyInfo

use of javax.crypto.EncryptedPrivateKeyInfo 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 10 with EncryptedPrivateKeyInfo

use of javax.crypto.EncryptedPrivateKeyInfo 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)

Aggregations

EncryptedPrivateKeyInfo (javax.crypto.EncryptedPrivateKeyInfo)40 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)26 AlgorithmParameters (java.security.AlgorithmParameters)10 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)10 InvalidKeyException (java.security.InvalidKeyException)7 SecretKey (javax.crypto.SecretKey)7 PBEKeySpec (javax.crypto.spec.PBEKeySpec)7 SecretKeyFactory (javax.crypto.SecretKeyFactory)6 Cipher (javax.crypto.Cipher)5 Key (java.security.Key)4 KeyFactory (java.security.KeyFactory)3 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 KeyStore (java.security.KeyStore)2 PrivateKey (java.security.PrivateKey)2 CertificateFactory (java.security.cert.CertificateFactory)2 X509Certificate (java.security.cert.X509Certificate)2 File (java.io.File)1