Search in sources :

Example 11 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)

Example 12 with EncryptedPrivateKeyInfo

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

the class EncryptedPrivateKeyInfoTest method testEncryptedPrivateKeyInfobyteArray5.

/**
     * Test #5 for <code>EncryptedPrivateKeyInfo(byte[])</code> constructor
     * <br>
     * Assertion: <code>IOException</code> if encoding is wrong <br>
     * Test preconditions: wrong encoding passed as a parameter <br>
     * Expected: <code>IOException</code>
     */
public final void testEncryptedPrivateKeyInfobyteArray5() throws Exception {
    byte[] enc = null;
    try {
        // 1: get valid encoding
        enc = EncryptedPrivateKeyInfoData.getValidEncryptedPrivateKeyInfoEncoding("DSA");
        // ... and corrupt it (set wrong alg OID length)
        enc[9] = (byte) 6;
        new EncryptedPrivateKeyInfo(enc);
        fail(getName() + "(1): IOException has not been thrown");
    } catch (IOException ok) {
    }
    try {
        // 2: get valid encoding
        enc = EncryptedPrivateKeyInfoData.getValidEncryptedPrivateKeyInfoEncoding("DSA");
        // ... and corrupt it (set wrong encrypted data tag)
        enc[307] = (byte) 6;
        new EncryptedPrivateKeyInfo(enc);
        fail(getName() + "(2): IOException has not been thrown");
    } catch (IOException ok) {
    }
    try {
        // 3: get valid encoding
        enc = EncryptedPrivateKeyInfoData.getValidEncryptedPrivateKeyInfoEncoding("DSA");
        // ... and corrupt it (set wrong encrypted data length)
        enc[310] = (byte) 1;
        new EncryptedPrivateKeyInfo(enc);
        fail(getName() + "(3): IOException has not been thrown");
    } catch (IOException ok) {
    }
    try {
        // 4: get valid encoding
        enc = EncryptedPrivateKeyInfoData.getValidEncryptedPrivateKeyInfoEncoding("DSA");
        // ... and corrupt it (set wrong tag for alg params sequence)
        enc[17] = (byte) 0x29;
        EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(enc);
        if (epki.getAlgParameters() == null) {
        // This kind of encoding corruption can
        // be only determined while AlgorithmParameters
        // initialization BUT No AlgorithmParameters instance
        // available for algName0[i][0].
        // So just skip this sub test
        } else {
            fail(getName() + "(4): IOException has not been thrown");
        }
    } catch (IOException ok) {
    }
    try {
        // 5: get valid encoding
        enc = EncryptedPrivateKeyInfoData.getValidEncryptedPrivateKeyInfoEncoding("DSA");
        // ... and corrupt it (set wrong length for alg params sequence)
        enc[20] = (byte) 0x1d;
        new EncryptedPrivateKeyInfo(enc);
        fail(getName() + "(5): IOException has not been thrown");
    } catch (IOException ok) {
    }
    try {
        // 6: get valid encoding
        enc = EncryptedPrivateKeyInfoData.getValidEncryptedPrivateKeyInfoEncoding("DSA");
        // ... and corrupt it (set wrong length for alg params sequence)
        enc[20] = (byte) 0x1f;
        new EncryptedPrivateKeyInfo(enc);
        fail(getName() + "(6): IOException has not been thrown");
    } catch (IOException ok) {
    }
}
Also used : EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) IOException(java.io.IOException)

Example 13 with EncryptedPrivateKeyInfo

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

the class EncryptedPrivateKeyInfoTest method test_ROUNDTRIP_GetKeySpecKey02.

/**
     * Encrypted data contains invalid PKCS8 key info encoding
     */
public final void test_ROUNDTRIP_GetKeySpecKey02() {
    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], privateKeyInfoDamaged, 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());
            }
            try {
                epki.getKeySpec(g.pubK() == null ? g.k() : g.pubK());
                fail(algName[i][0] + ", " + algName[i][1]);
            } catch (InvalidKeyException e) {
            }
            performed = true;
        } catch (TestDataGenerator.AllowedFailure allowedFailure) {
        } catch (NoSuchAlgorithmException allowedFailure) {
        }
    }
    assertTrue("Test not performed", performed);
}
Also used : EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 14 with EncryptedPrivateKeyInfo

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

the class EncryptedPrivateKeyInfoTest method test_ROUNDTRIP_GetKeySpecKeyString01.

/**
     * Encrypted data contains valid PKCS8 key info encoding
     */
public final void test_ROUNDTRIP_GetKeySpecKeyString01() throws Exception {
    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], privateKeyInfo, 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 {
                    PKCS8EncodedKeySpec eks = epki.getKeySpec(g.pubK() == null ? g.k() : g.pubK(), provider[l].getName());
                    if (!Arrays.equals(privateKeyInfo, eks.getEncoded())) {
                        fail(algName[i][0] + " != " + algName[i][1]);
                    }
                } catch (InvalidKeyException e) {
                    fail(algName[i][0] + ", " + algName[i][1] + ": " + e);
                }
                performed = true;
            } catch (NoSuchAlgorithmException allowedFailure) {
            }
        }
    }
    assertTrue("Test not performed", performed);
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 15 with EncryptedPrivateKeyInfo

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

the class EncryptedPrivateKeyInfoTest method testGetAlgParameters01_01.

public final void testGetAlgParameters01_01() throws Exception {
    byte[] validEncodingWithUnknownAlgOID = EncryptedPrivateKeyInfoData.getValidEncryptedPrivateKeyInfoEncoding("DH");
    // correct oid value
    validEncodingWithUnknownAlgOID[18] = 0;
    EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(validEncodingWithUnknownAlgOID);
    assertNull(epki.getAlgParameters());
}
Also used : EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo)

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