Search in sources :

Example 66 with InvalidKeyException

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

the class EncryptedPrivateKeyInfoTest method test_ROUNDTRIP_GetKeySpecKeyString02.

/**
     * Encrypted data contains invalid PKCS8 key info encoding
     */
public final void test_ROUNDTRIP_GetKeySpecKeyString02() 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], 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].getName());
                    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 67 with InvalidKeyException

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

the class EncryptedPrivateKeyInfoTest method test_ROUNDTRIP_GetKeySpecKey01.

/**
     * Encrypted data contains valid PKCS8 key info encoding
     */
public final void test_ROUNDTRIP_GetKeySpecKey01() {
    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());
            }
            try {
                PKCS8EncodedKeySpec eks = epki.getKeySpec(g.pubK() == null ? g.k() : g.pubK());
                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 (TestDataGenerator.AllowedFailure allowedFailure) {
        } 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 68 with InvalidKeyException

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

the class ExemptionMechanismSpiTest method testExemptionMechanismSpi01.

/**
     * Test for <code>ExemptionMechanismSpi</code> constructor Assertion:
     * constructs ExemptionMechanismSpi
     * @throws Exception
     */
public void testExemptionMechanismSpi01() throws Exception {
    Mock_ExemptionMechanismSpi emSpi = new Mock_ExemptionMechanismSpi() {
    };
    int len = MyExemptionMechanismSpi.getLength();
    byte[] bbRes = emSpi.engineGenExemptionBlob();
    assertEquals("Incorrect length", bbRes.length, len);
    assertEquals("Incorrect result", emSpi.engineGenExemptionBlob(new byte[1], len), len);
    assertEquals("Incorrect output size", 10, emSpi.engineGetOutputSize(100));
    Key key = null;
    AlgorithmParameters params = null;
    AlgorithmParameterSpec parSpec = null;
    try {
        emSpi.engineInit(key);
        fail("InvalidKeyException must be thrown");
    } catch (InvalidKeyException e) {
    }
    try {
        emSpi.engineInit(key, params);
        fail("InvalidKeyException must be thrown");
    } catch (InvalidKeyException e) {
    }
    try {
        emSpi.engineInit(key, parSpec);
        fail("InvalidKeyException must be thrown");
    } catch (InvalidKeyException e) {
    }
    key = ((MyExemptionMechanismSpi) emSpi).new tmp1Key("Proba", new byte[0]);
    try {
        emSpi.engineInit(key);
        fail("ExemptionMechanismException must be thrown");
    } catch (ExemptionMechanismException e) {
    }
    try {
        emSpi.engineInit(key, params);
        fail("ExemptionMechanismException must be thrown");
    } catch (ExemptionMechanismException e) {
    }
    try {
        emSpi.engineInit(key, parSpec);
        fail("ExemptionMechanismException must be thrown");
    } catch (ExemptionMechanismException e) {
    }
    key = ((MyExemptionMechanismSpi) emSpi).new tmpKey("Proba", new byte[0]);
    emSpi.engineInit(key);
    emSpi.engineInit(key, AlgorithmParameters.getInstance("DH"));
    emSpi.engineInit(key, new RSAKeyGenParameterSpec(10, new BigInteger("10")));
    assertEquals("Incorrect result", 10, emSpi.engineGetOutputSize(100));
}
Also used : RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) BigInteger(java.math.BigInteger) InvalidKeyException(java.security.InvalidKeyException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) ExemptionMechanismException(javax.crypto.ExemptionMechanismException) Key(java.security.Key) AlgorithmParameters(java.security.AlgorithmParameters)

Example 69 with InvalidKeyException

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

the class ExemptionMechanismTest method testExemptionMechanism.

/**
     * Test for <code>ExemptionMechanism</code> constructor
     * Assertion: creates new object using provider and mechanism name
     */
public void testExemptionMechanism() throws Exception {
    Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", "Provider for ExemptionMechanism testing", srvExemptionMechanism.concat(".").concat(defaultAlg), ExemptionMechanismProviderClass);
    ExemptionMechanismSpi spi = new MyExemptionMechanismSpi();
    ExemptionMechanism em = new ExemptionMechanism(spi, mProv, defaultAlg) {
    };
    assertEquals("Incorrect provider", em.getProvider(), mProv);
    assertEquals("Incorrect algorithm", em.getName(), defaultAlg);
    try {
        em.init(null);
        fail("InvalidKeyException must be thrown");
    } catch (InvalidKeyException e) {
    }
    try {
        em.getOutputSize(100);
        fail("IllegalStateException must be thrown");
    } catch (IllegalStateException e) {
    }
    em = new ExemptionMechanism(null, null, null) {
    };
    assertNull("Incorrect mechanism", em.getName());
    assertNull("Incorrect provider", em.getProvider());
    try {
        em.init(null);
        fail("NullPointerException must be thrown");
    } catch (NullPointerException e) {
    }
    try {
        em.getOutputSize(100);
        fail("IllegalStateException must be thrown");
    } catch (IllegalStateException e) {
    }
}
Also used : ExemptionMechanismSpi(javax.crypto.ExemptionMechanismSpi) MyExemptionMechanismSpi(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi) SpiEngUtils(org.apache.harmony.security.tests.support.SpiEngUtils) InvalidKeyException(java.security.InvalidKeyException) MyExemptionMechanismSpi(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi) ExemptionMechanism(javax.crypto.ExemptionMechanism) Provider(java.security.Provider)

Example 70 with InvalidKeyException

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

the class ExemptionMechanismTest method test_initLjava_security_KeyLjava_security_AlgorithmParameters.

public void test_initLjava_security_KeyLjava_security_AlgorithmParameters() throws Exception {
    Provider mProv = (new SpiEngUtils()).new MyProvider("MyExMechProvider", "Provider for ExemptionMechanism testing", srvExemptionMechanism.concat(".").concat(defaultAlg), ExemptionMechanismProviderClass);
    ExemptionMechanism em = new ExemptionMechanism(new MyExemptionMechanismSpi(), mProv, defaultAlg) {
    };
    Key key = new MyExemptionMechanismSpi().new tmpKey("Proba", new byte[0]);
    em.init(key, AlgorithmParameters.getInstance("DES"));
    try {
        em.init(key, (AlgorithmParameters) null);
        fail("InvalidAlgorithmParameterException expected");
    } catch (InvalidAlgorithmParameterException e) {
    //expected
    }
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    kg.init(56, new SecureRandom());
    key = kg.generateKey();
    try {
        em.init(null, AlgorithmParameters.getInstance("DES"));
        fail("InvalidKeyException expected");
    } catch (InvalidKeyException e) {
    //expected
    }
    try {
        em.init(key, AlgorithmParameters.getInstance("DES"));
        fail("ExemptionMechanismException expected");
    } catch (ExemptionMechanismException e) {
    //expected
    }
}
Also used : SpiEngUtils(org.apache.harmony.security.tests.support.SpiEngUtils) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecureRandom(java.security.SecureRandom) InvalidKeyException(java.security.InvalidKeyException) MyExemptionMechanismSpi(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi) KeyGenerator(javax.crypto.KeyGenerator) ExemptionMechanismException(javax.crypto.ExemptionMechanismException) ExemptionMechanism(javax.crypto.ExemptionMechanism) MyExemptionMechanismSpi.tmpKey(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi.tmpKey) Key(java.security.Key) MyExemptionMechanismSpi.tmpKey(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi.tmpKey) Provider(java.security.Provider)

Aggregations

InvalidKeyException (java.security.InvalidKeyException)499 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)263 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)124 SignatureException (java.security.SignatureException)95 IOException (java.io.IOException)94 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)93 BadPaddingException (javax.crypto.BadPaddingException)89 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)87 Cipher (javax.crypto.Cipher)77 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)63 SecretKeySpec (javax.crypto.spec.SecretKeySpec)63 Signature (java.security.Signature)58 SecretKey (javax.crypto.SecretKey)50 PublicKey (java.security.PublicKey)49 PrivateKey (java.security.PrivateKey)47 CertificateException (java.security.cert.CertificateException)46 Mac (javax.crypto.Mac)44 IvParameterSpec (javax.crypto.spec.IvParameterSpec)41 NoSuchProviderException (java.security.NoSuchProviderException)39 KeyStoreException (java.security.KeyStoreException)33