Search in sources :

Example 71 with InvalidKeyException

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

the class ExemptionMechanismTest method test_initLjava_security_KeyLjava_security_spec_AlgorithmParameterSpec.

public void test_initLjava_security_KeyLjava_security_spec_AlgorithmParameterSpec() 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, new RSAKeyGenParameterSpec(10, new BigInteger("10")));
    try {
        em.init(key, (AlgorithmParameterSpec) 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, new RSAKeyGenParameterSpec(10, new BigInteger("10")));
        fail("InvalidKeyException expected");
    } catch (InvalidKeyException e) {
    //expected
    }
    try {
        em.init(key, new RSAKeyGenParameterSpec(10, new BigInteger("10")));
        fail("ExemptionMechanismException expected");
    } catch (ExemptionMechanismException e) {
    //expected
    }
}
Also used : SpiEngUtils(org.apache.harmony.security.tests.support.SpiEngUtils) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) SecureRandom(java.security.SecureRandom) InvalidKeyException(java.security.InvalidKeyException) MyExemptionMechanismSpi(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi) ExemptionMechanism(javax.crypto.ExemptionMechanism) MyExemptionMechanismSpi.tmpKey(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi.tmpKey) Provider(java.security.Provider) BigInteger(java.math.BigInteger) KeyGenerator(javax.crypto.KeyGenerator) ExemptionMechanismException(javax.crypto.ExemptionMechanismException) MyExemptionMechanismSpi.tmpKey(org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi.tmpKey) Key(java.security.Key)

Example 72 with InvalidKeyException

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

the class CipherTest method test_initWithKeyAlgorithmParameterSpecSecureRandom.

/**
     * javax.crypto.Cipher#init(int, java.security.Key,
     *        java.security.spec.AlgorithmParameterSpec,
     *        java.security.SecureRandom)
     */
public void test_initWithKeyAlgorithmParameterSpecSecureRandom() throws Exception {
    AlgorithmParameterSpec ap = new IvParameterSpec(IV);
    Cipher cipher = Cipher.getInstance(ALGORITHM_3DES + "/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES, ap, new SecureRandom());
    byte[] cipherIV = cipher.getIV();
    assertTrue("IVs differ", Arrays.equals(cipherIV, IV));
    cipher = Cipher.getInstance("DES/CBC/NoPadding");
    try {
        cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES, ap, new SecureRandom());
        fail();
    } catch (InvalidKeyException expected) {
    }
    cipher = Cipher.getInstance("DES/CBC/NoPadding");
    ap = new RSAKeyGenParameterSpec(10, new BigInteger("10"));
    try {
        cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap, new SecureRandom());
        fail();
    } catch (InvalidAlgorithmParameterException expected) {
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecureRandom(java.security.SecureRandom) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) BigInteger(java.math.BigInteger) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) InvalidKeyException(java.security.InvalidKeyException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 73 with InvalidKeyException

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

the class CipherTest method test_wrap_java_security_Key.

public void test_wrap_java_security_Key() throws Exception {
    AlgorithmParameterSpec ap = new IvParameterSpec(IV);
    Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");
    c.init(Cipher.WRAP_MODE, CIPHER_KEY_DES, ap, new SecureRandom());
    assertNotNull(c.wrap(CIPHER_KEY_DES));
    assertNotNull(c.wrap(CIPHER_KEY_3DES));
    String certName = Support_Resources.getURL("test.cert");
    InputStream is = new URL(certName).openConnection().getInputStream();
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    Certificate cert = cf.generateCertificate(is);
    assertNotNull(c.wrap(cert.getPublicKey()));
    c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.WRAP_MODE, CIPHER_KEY_DES, ap, new SecureRandom());
    try {
        assertNotNull(c.wrap(cert.getPublicKey()));
        fail();
    } catch (IllegalBlockSizeException expected) {
    }
    c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap, new SecureRandom());
    try {
        c.wrap(CIPHER_KEY_DES);
        fail();
    } catch (IllegalStateException expected) {
    }
    c.init(Cipher.WRAP_MODE, CIPHER_KEY_DES, ap, new SecureRandom());
    try {
        c.wrap(new Mock_Key());
        fail();
    } catch (InvalidKeyException expected) {
    }
}
Also used : InputStream(java.io.InputStream) SecureRandom(java.security.SecureRandom) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) InvalidKeyException(java.security.InvalidKeyException) CertificateFactory(java.security.cert.CertificateFactory) URL(java.net.URL) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) Certificate(java.security.cert.Certificate)

Example 74 with InvalidKeyException

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

the class CipherTest method test_initWithKey.

/**
     * javax.crypto.Cipher#init(int, java.security.Key)
     */
public void test_initWithKey() throws Exception {
    Cipher cipher = Cipher.getInstance(ALGORITHM_3DES + "/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES);
    cipher = Cipher.getInstance("DES/CBC/NoPadding");
    try {
        cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES);
        fail();
    } catch (InvalidKeyException expected) {
    }
}
Also used : Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) InvalidKeyException(java.security.InvalidKeyException)

Example 75 with InvalidKeyException

use of java.security.InvalidKeyException 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)

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