Search in sources :

Example 31 with EncryptedPrivateKeyInfo

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

the class EncryptedPrivateKeyInfoTest method testGetEncoded03.

/**
     * Test #3 for <code>getEncoded()</code> method <br>
     * Assertion: returns the ASN.1 encoding of this object <br>
     * Test preconditions: test object created using ctor which takes algorithm
     * name and encrypted data as a parameters <br>
     * Expected: equivalent encoded form (without alg params) must be returned
     *
     * @throws IOException
     */
public final void testGetEncoded03() 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 encoded form
            assertTrue(Arrays.equals(EncryptedPrivateKeyInfoData.getValidEncryptedPrivateKeyInfoEncoding(EncryptedPrivateKeyInfoData.algName0[i][0]), epki.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 32 with EncryptedPrivateKeyInfo

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

the class EncryptedPrivateKeyInfoTest method test_ROUNDTRIP_GetKeySpecCipher01.

/**
     * Encrypted data contains valid PKCS8 key info encoding
     */
public final void test_ROUNDTRIP_GetKeySpecCipher01() {
    boolean performed = false;
    for (int i = 0; i < algName.length; i++) {
        try {
            // generate test data
            TestDataGenerator g = new TestDataGenerator(algName[i][0], algName[i][1], privateKeyInfo, null);
            // 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());
            }
            // call methods under test
            try {
                PKCS8EncodedKeySpec eks = epki.getKeySpec(g.c());
                if (!Arrays.equals(privateKeyInfo, eks.getEncoded())) {
                    fail(algName[i][0] + " != " + algName[i][1]);
                }
            } catch (InvalidKeySpecException e) {
                fail(algName[i][0] + ", " + algName[i][1] + e + "\n");
            }
            performed = true;
        } catch (TestDataGenerator.AllowedFailure allowedFailure) {
        } catch (NoSuchAlgorithmException allowed) {
        }
    }
    assertTrue("Test not performed", performed);
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 33 with EncryptedPrivateKeyInfo

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

the class EncryptedPrivateKeyInfoTest method testGetEncryptedData04.

/**
     * Test #4 for <code>getEncryptedData()</code> method <br>
     * Assertion: returns a new array each time this method is called <br>
     * Test preconditions: test object created using ctor which takes algorithm
     * name and encrypted data as a parameters <br>
     * Expected: refs to encrypted data byte array passed to the ctor and
     * returned by the method under test must be different
     */
public final void testGetEncryptedData04() {
    boolean performed = false;
    for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
        try {
            EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(EncryptedPrivateKeyInfoData.algName0[i][0], EncryptedPrivateKeyInfoData.encryptedData);
            // check that method under test returns
            // new array each time
            byte[] ecd1 = epki.getEncryptedData();
            byte[] ecd2 = epki.getEncryptedData();
            assertNotSame(EncryptedPrivateKeyInfoData.encryptedData, ecd1);
            assertNotSame(EncryptedPrivateKeyInfoData.encryptedData, ecd2);
            assertNotSame(ecd1, ecd2);
            performed = true;
        } catch (NoSuchAlgorithmException allowedFailure) {
        }
    }
    assertTrue("Test not performed", performed);
}
Also used : EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 34 with EncryptedPrivateKeyInfo

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

the class EncryptedPrivateKeyInfoTest method testGetEncoded04.

/**
     * Test #4 for <code>getEncoded()</code> method <br>
     * Assertion: returns a new array each time this method is called <br>
     * Test preconditions: test object created using ctor which takes algorithm
     * name and encrypted data as a parameters <br>
     * Expected: several refs to byte array returned by the method under test
     * must be different
     *
     * @throws IOException
     */
public final void testGetEncoded04() throws IOException {
    boolean performed = false;
    for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
        try {
            EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(EncryptedPrivateKeyInfoData.algName0[i][0], EncryptedPrivateKeyInfoData.encryptedData);
            // check that method under test returns
            // new array each time
            byte[] ec1 = epki.getEncoded();
            byte[] ec2 = epki.getEncoded();
            byte[] ec3 = epki.getEncoded();
            assertNotSame(ec1, ec2);
            assertNotSame(ec2, ec3);
            assertNotSame(ec1, ec3);
            performed = true;
        } catch (NoSuchAlgorithmException allowedFailure) {
        }
    }
    assertTrue("Test not performed", performed);
}
Also used : EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 35 with EncryptedPrivateKeyInfo

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

the class EncryptedPrivateKeyInfoTest method testGetKeySpecKeyString01.

public final void testGetKeySpecKeyString01() throws Exception {
    boolean performed = false;
    for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
        try {
            EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(EncryptedPrivateKeyInfoData.algName0[i][0], EncryptedPrivateKeyInfoData.encryptedData);
            try {
                // check that method under test throws NPE
                epki.getKeySpec((Key) null, "SomeProviderName");
                fail(getName() + "NullPointerException has not been thrown");
            } catch (NullPointerException ok) {
            }
            try {
                epki.getKeySpec(new Key() {

                    public String getAlgorithm() {
                        return "alg";
                    }

                    public String getFormat() {
                        return "fmt";
                    }

                    public byte[] getEncoded() {
                        return new byte[] {};
                    }
                }, "StrangeProviderName");
                fail(getName() + "NoSuchProviderException has not been thrown");
            } catch (NoSuchProviderException ok) {
            //expected
            }
            try {
                // check that method under test throws NPE
                epki.getKeySpec(new Key() {

                    public String getAlgorithm() {
                        return "alg";
                    }

                    public String getFormat() {
                        return "fmt";
                    }

                    public byte[] getEncoded() {
                        return new byte[] {};
                    }
                }, (String) null);
                fail(getName() + "NullPointerException has not been thrown");
            } catch (NullPointerException ok) {
            }
            performed = true;
        } catch (NoSuchAlgorithmException allowedFailure) {
        }
    }
    assertTrue("Test not performed", performed);
}
Also used : EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException) Key(java.security.Key)

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