Search in sources :

Example 36 with NoSuchProviderException

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

the class SecureRandomTest method testGetInstanceStringString.

/*
     * Class under test for SecureRandom getInstance(String, String)
     */
public final void testGetInstanceStringString() throws Exception {
    SecureRandom sr = SecureRandom.getInstance("someRandom", "SRProvider");
    if (sr.getProvider() != p || !"someRandom".equals(sr.getAlgorithm())) {
        fail("getInstance failed");
    }
    try {
        SecureRandom r = SecureRandom.getInstance("anotherRandom", "SRProvider");
        fail("expected NoSuchAlgorithmException");
    } catch (NoSuchAlgorithmException e) {
    // ok
    } catch (NoSuchProviderException e) {
        fail("unexpected: " + e);
    } catch (IllegalArgumentException e) {
        fail("unexpected: " + e);
    } catch (NullPointerException e) {
        fail("unexpected: " + e);
    }
    try {
        SecureRandom r = SecureRandom.getInstance("someRandom", "UnknownProvider");
        fail("expected NoSuchProviderException");
    } catch (NoSuchProviderException e) {
    // ok
    } catch (NoSuchAlgorithmException e) {
        fail("unexpected: " + e);
    } catch (IllegalArgumentException e) {
        fail("unexpected: " + e);
    } catch (NullPointerException e) {
        fail("unexpected: " + e);
    }
    try {
        SecureRandom r = SecureRandom.getInstance("someRandom", (String) null);
        fail("expected IllegalArgumentException");
    } catch (NoSuchProviderException e) {
        fail("unexpected: " + e);
    } catch (NoSuchAlgorithmException e) {
        fail("unexpected: " + e);
    } catch (IllegalArgumentException e) {
    // ok
    } catch (NullPointerException e) {
        fail("unexpected: " + e);
    }
    try {
        SecureRandom r = SecureRandom.getInstance(null, "SRProvider");
        fail("expected NullPointerException");
    } catch (NoSuchProviderException e) {
        fail("unexpected: " + e);
    } catch (NoSuchAlgorithmException e) {
        fail("unexpected: " + e);
    } catch (IllegalArgumentException e) {
        fail("unexpected: " + e);
    } catch (NullPointerException e) {
    // ok
    }
}
Also used : SecureRandom(java.security.SecureRandom) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 37 with NoSuchProviderException

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

the class X509CertificateObject method verify.

public final void verify(PublicKey key) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException {
    Signature signature;
    String sigName = X509SignatureUtil.getSignatureName(c.getSignatureAlgorithm());
    try {
        signature = Signature.getInstance(sigName, BouncyCastleProvider.PROVIDER_NAME);
    } catch (Exception e) {
        signature = Signature.getInstance(sigName);
    }
    checkSignature(key, signature);
}
Also used : Signature(java.security.Signature) DERBitString(org.bouncycastle.asn1.DERBitString) DEROctetString(org.bouncycastle.asn1.DEROctetString) DERIA5String(org.bouncycastle.asn1.DERIA5String) ASN1String(org.bouncycastle.asn1.ASN1String) CertificateExpiredException(java.security.cert.CertificateExpiredException) SignatureException(java.security.SignatureException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) CertificateEncodingException(java.security.cert.CertificateEncodingException) CertificateNotYetValidException(java.security.cert.CertificateNotYetValidException) CertificateParsingException(java.security.cert.CertificateParsingException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) UnknownHostException(java.net.UnknownHostException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 38 with NoSuchProviderException

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

the class SealedObjectTest method testGetObject3.

/**
     * getObject(Key key, String provider) method testing. Tests if the proper
     * exception is thrown in the case of incorrect input parameters and if the
     * object sealed with encryption algorithm can be retrieved by specifying
     * the cryptographic key and provider name.
     */
public void testGetObject3() throws Exception {
    try {
        new SealedObject("secret string", new NullCipher()).getObject(new SecretKeySpec(new byte[] { 0, 0, 0 }, "algorithm"), null);
        fail("IllegalArgumentException should be thrown in the case of " + "null provider.");
    } catch (IllegalArgumentException e) {
    }
    try {
        new SealedObject("secret string", new NullCipher()).getObject(new SecretKeySpec(new byte[] { 0, 0, 0 }, "algorithm"), "");
        fail("IllegalArgumentException should be thrown in the case of " + "empty provider.");
    } catch (IllegalArgumentException e) {
    }
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    Key key = kg.generateKey();
    Cipher cipher = Cipher.getInstance("DES");
    String provider = cipher.getProvider().getName();
    cipher.init(Cipher.ENCRYPT_MODE, key);
    String secret = "secret string";
    SealedObject so = new SealedObject(secret, cipher);
    cipher.init(Cipher.DECRYPT_MODE, key);
    assertEquals("The returned object does not equals to the " + "original object.", secret, so.getObject(key, provider));
    kg = KeyGenerator.getInstance("DESede");
    key = kg.generateKey();
    try {
        so.getObject(key, provider);
        fail("InvalidKeyException expected");
    } catch (InvalidKeyException e) {
    //expected
    }
    try {
        so.getObject(key, "Wrong provider name");
        fail("NoSuchProviderException expected");
    } catch (NoSuchProviderException e) {
    //expected
    }
}
Also used : NullCipher(javax.crypto.NullCipher) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SealedObject(javax.crypto.SealedObject) Cipher(javax.crypto.Cipher) NullCipher(javax.crypto.NullCipher) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key)

Example 39 with NoSuchProviderException

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

the class X509CertificateTest method testVerifyPublicKey.

/**
     * @throws SignatureException
     * @throws NoSuchProviderException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws CertificateException
     * {@link Certificate#verify(PublicKey)}
     */
@SideEffect("Destroys MD5 provider, hurts succeeding tests")
public void testVerifyPublicKey() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException, SignatureException, CertificateException {
    // Preconditions
    assertNotNull(javaxCert.getPublicKey());
    assertNotNull(javaxSSCert.getPublicKey());
    // here not self signed:
    try {
        javaxCert.verify(javaxCert.getPublicKey());
    } catch (SignatureException e) {
    // ok
    }
    PublicKey k = javaxCert.getPublicKey();
    MyModifiablePublicKey changedEncoding = new MyModifiablePublicKey(k);
    changedEncoding.setEncoding(new byte[javaxCert.getEncoded().length - 1]);
    try {
        javaxCert.verify(tbt_cert.getPublicKey());
    } catch (InvalidKeyException e) {
    // ok
    }
    try {
        javaxCert.verify(null);
    } catch (Exception e) {
    // ok
    }
    try {
        javaxCert.verify(changedEncoding);
        fail("Exception expected");
    } catch (Exception e) {
    // ok
    }
// following test doesn't work because the algorithm is derived from
// somewhere else.
// MyModifiablePublicKey changedAlgo = new MyModifiablePublicKey(k);
// changedAlgo.setAlgorithm("MD5withBla");
// try {
//     javaxCert.verify(changedAlgo);
//     fail("Exception expected");
// } catch (SignatureException e) {
//     // ok
// }
// Security.removeProvider(mySSProvider.getName());
// try {
//     javaxSSCert.verify(javaxSSCert.getPublicKey());
// } catch (NoSuchProviderException e) {
//     // ok
// }
// Security.addProvider(mySSProvider);
// must always evaluate true for self signed
// javaxSSCert.verify(javaxSSCert.getPublicKey());
}
Also used : PublicKey(java.security.PublicKey) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) CertificateExpiredException(javax.security.cert.CertificateExpiredException) CertificateNotYetValidException(javax.security.cert.CertificateNotYetValidException) CertificateEncodingException(javax.security.cert.CertificateEncodingException) CertificateException(javax.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException) SideEffect(dalvik.annotation.SideEffect)

Example 40 with NoSuchProviderException

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

the class TrustManagerFactory2Test method test_getInstanceLjava_lang_StringLjava_lang_String.

/**
     * Test for <code>getInstance(String algorithm, String provider)</code>
     * method
     * Assertions:
     * throws NullPointerException when algorithm is null;
     * throws NoSuchAlgorithmException when algorithm is not correct;
     * throws IllegalArgumentException when provider is null or empty;
     * throws NoSuchProviderException when provider is available;
     * returns TrustManagerFactory object
     */
public void test_getInstanceLjava_lang_StringLjava_lang_String() throws Exception {
    try {
        TrustManagerFactory.getInstance(null, mProv.getName());
        fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
    } catch (NoSuchAlgorithmException e) {
    } catch (NullPointerException e) {
    }
    for (int i = 0; i < invalidValues.length; i++) {
        try {
            TrustManagerFactory.getInstance(invalidValues[i], mProv.getName());
            fail("NoSuchAlgorithmException must be thrown (algorithm: ".concat(invalidValues[i]).concat(")"));
        } catch (NoSuchAlgorithmException e) {
        }
    }
    String prov = null;
    for (int i = 0; i < validValues.length; i++) {
        try {
            TrustManagerFactory.getInstance(validValues[i], prov);
            fail("IllegalArgumentException must be thrown when provider is null (algorithm: ".concat(invalidValues[i]).concat(")"));
        } catch (IllegalArgumentException e) {
        }
        try {
            TrustManagerFactory.getInstance(validValues[i], "");
            fail("IllegalArgumentException must be thrown when provider is empty (algorithm: ".concat(invalidValues[i]).concat(")"));
        } catch (IllegalArgumentException e) {
        }
    }
    for (int i = 0; i < validValues.length; i++) {
        for (int j = 1; j < invalidValues.length; j++) {
            try {
                TrustManagerFactory.getInstance(validValues[i], invalidValues[j]);
                fail("NoSuchProviderException must be thrown (algorithm: ".concat(invalidValues[i]).concat(" provider: ").concat(invalidValues[j]).concat(")"));
            } catch (NoSuchProviderException e) {
            }
        }
    }
    TrustManagerFactory tmf;
    for (int i = 0; i < validValues.length; i++) {
        tmf = TrustManagerFactory.getInstance(validValues[i], mProv.getName());
        assertTrue("Not instanceof TrustManagerFactory object", tmf instanceof TrustManagerFactory);
        assertEquals("Incorrect algorithm", tmf.getAlgorithm(), validValues[i]);
        assertEquals("Incorrect provider", tmf.getProvider().getName(), mProv.getName());
        checkResult(tmf);
    }
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException)

Aggregations

NoSuchProviderException (java.security.NoSuchProviderException)97 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)70 InvalidKeyException (java.security.InvalidKeyException)31 IOException (java.io.IOException)29 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)20 CertificateException (java.security.cert.CertificateException)19 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)14 Cipher (javax.crypto.Cipher)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 KeyStoreException (java.security.KeyStoreException)12 X509Certificate (java.security.cert.X509Certificate)12 BadPaddingException (javax.crypto.BadPaddingException)12 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)12 SignatureException (java.security.SignatureException)11 SecretKey (javax.crypto.SecretKey)10 CertificateFactory (java.security.cert.CertificateFactory)9 CertificateEncodingException (java.security.cert.CertificateEncodingException)8 IvParameterSpec (javax.crypto.spec.IvParameterSpec)8 KeyStore (java.security.KeyStore)7 Provider (java.security.Provider)7