Search in sources :

Example 6 with NoSuchProviderException

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

the class PKCS10CertificationRequest method verify.

/**
     * verify the request using the passed in public key and the provider..
     */
public boolean verify(PublicKey pubKey, String provider) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, SignatureException {
    Signature sig;
    try {
        if (provider == null) {
            sig = Signature.getInstance(getSignatureName(sigAlgId));
        } else {
            sig = Signature.getInstance(getSignatureName(sigAlgId), provider);
        }
    } catch (NoSuchAlgorithmException e) {
        //
        if (oids.get(sigAlgId.getObjectId()) != null) {
            String signatureAlgorithm = (String) oids.get(sigAlgId.getObjectId());
            if (provider == null) {
                sig = Signature.getInstance(signatureAlgorithm);
            } else {
                sig = Signature.getInstance(signatureAlgorithm, provider);
            }
        } else {
            throw e;
        }
    }
    setSignatureParameters(sig, sigAlgId.getParameters());
    sig.initVerify(pubKey);
    try {
        sig.update(reqInfo.getEncoded(ASN1Encoding.DER));
    } catch (Exception e) {
        throw new SignatureException("exception encoding TBS cert request - " + e);
    }
    return sig.verify(sigBits.getBytes());
}
Also used : Signature(java.security.Signature) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DERBitString(org.bouncycastle.asn1.DERBitString) SignatureException(java.security.SignatureException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) GeneralSecurityException(java.security.GeneralSecurityException) SignatureException(java.security.SignatureException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 7 with NoSuchProviderException

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

the class AlgorithmParameterGenerator2Test method testGetInstance02.

/**
     * Test for <code>getInstance(String algorithm, String provider)</code>
     * method
     * Assertions:
     * throws NullPointerException must be thrown is null
     * throws NoSuchAlgorithmException must be thrown if algorithm is not available
     * throws IllegalArgumentException when provider is null;
     * throws NoSuchProviderException when provider is available;
     * returns AlgorithmParameterGenerator object
     */
public void testGetInstance02() throws NoSuchAlgorithmException, NoSuchProviderException, IllegalArgumentException, InvalidAlgorithmParameterException {
    try {
        AlgorithmParameterGenerator.getInstance(null, mProv.getName());
        fail("NullPointerException or NoSuchAlgorithmException should be thrown");
    } catch (NullPointerException e) {
    } catch (NoSuchAlgorithmException e) {
    }
    for (int i = 0; i < invalidValues.length; i++) {
        try {
            AlgorithmParameterGenerator.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 {
            AlgorithmParameterGenerator.getInstance(validValues[i], prov);
            fail("IllegalArgumentException must be thrown when provider is null (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 {
                AlgorithmParameterGenerator.getInstance(validValues[i], invalidValues[j]);
                fail("NoSuchProviderException must be thrown (algorithm: ".concat(invalidValues[i]).concat(" provider: ").concat(invalidValues[j]).concat(")"));
            } catch (NoSuchProviderException e) {
            }
        }
    }
    AlgorithmParameterGenerator apG;
    for (int i = 0; i < validValues.length; i++) {
        apG = AlgorithmParameterGenerator.getInstance(validValues[i], mProv.getName());
        assertEquals("Incorrect algorithm", apG.getAlgorithm(), validValues[i]);
        assertEquals("Incorrect provider", apG.getProvider().getName(), mProv.getName());
        checkResult(apG);
    }
}
Also used : AlgorithmParameterGenerator(java.security.AlgorithmParameterGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 8 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 9 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 10 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)

Aggregations

NoSuchProviderException (java.security.NoSuchProviderException)93 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)67 InvalidKeyException (java.security.InvalidKeyException)30 IOException (java.io.IOException)28 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)18 CertificateException (java.security.cert.CertificateException)18 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)14 Cipher (javax.crypto.Cipher)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 BadPaddingException (javax.crypto.BadPaddingException)12 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)12 KeyStoreException (java.security.KeyStoreException)11 SignatureException (java.security.SignatureException)10 X509Certificate (java.security.cert.X509Certificate)10 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