Search in sources :

Example 61 with SecureRandom

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

the class BaseKeyGenerator method engineInit.

protected void engineInit(int keySize, SecureRandom random) {
    try {
        if (random == null) {
            random = new SecureRandom();
        }
        engine.init(new KeyGenerationParameters(random, keySize));
        uninitialised = false;
    } catch (IllegalArgumentException e) {
        throw new InvalidParameterException(e.getMessage());
    }
}
Also used : InvalidParameterException(java.security.InvalidParameterException) SecureRandom(java.security.SecureRandom) KeyGenerationParameters(org.bouncycastle.crypto.KeyGenerationParameters)

Example 62 with SecureRandom

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

the class BaseKeyGenerator method engineGenerateKey.

protected SecretKey engineGenerateKey() {
    if (uninitialised) {
        engine.init(new KeyGenerationParameters(new SecureRandom(), defaultKeySize));
        uninitialised = false;
    }
    return new SecretKeySpec(engine.generateKey(), algName);
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecureRandom(java.security.SecureRandom) KeyGenerationParameters(org.bouncycastle.crypto.KeyGenerationParameters)

Example 63 with SecureRandom

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

the class OpenSSLCipher method engineInitInternal.

private void engineInitInternal(int opmode, Key key, byte[] iv, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    if (opmode == Cipher.ENCRYPT_MODE || opmode == Cipher.WRAP_MODE) {
        encrypting = true;
    } else if (opmode == Cipher.DECRYPT_MODE || opmode == Cipher.UNWRAP_MODE) {
        encrypting = false;
    } else {
        throw new InvalidParameterException("Unsupported opmode " + opmode);
    }
    if (!(key instanceof SecretKey)) {
        throw new InvalidKeyException("Only SecretKey is supported");
    }
    final byte[] encodedKey = key.getEncoded();
    if (encodedKey == null) {
        throw new InvalidKeyException("key.getEncoded() == null");
    }
    checkSupportedKeySize(encodedKey.length);
    final long cipherType = NativeCrypto.EVP_get_cipherbyname(getCipherName(encodedKey.length, mode));
    if (cipherType == 0) {
        throw new InvalidAlgorithmParameterException("Cannot find name for key length = " + (encodedKey.length * 8) + " and mode = " + mode);
    }
    final int ivLength = NativeCrypto.EVP_CIPHER_iv_length(cipherType);
    if (iv == null && ivLength != 0) {
        iv = new byte[ivLength];
        if (encrypting) {
            if (random == null) {
                random = new SecureRandom();
            }
            random.nextBytes(iv);
        }
    } else if (iv != null && iv.length != ivLength) {
        throw new InvalidAlgorithmParameterException("expected IV length of " + ivLength);
    }
    this.iv = iv;
    if (supportsVariableSizeKey()) {
        NativeCrypto.EVP_CipherInit_ex(cipherCtx.getContext(), cipherType, null, null, encrypting);
        NativeCrypto.EVP_CIPHER_CTX_set_key_length(cipherCtx.getContext(), encodedKey.length);
        NativeCrypto.EVP_CipherInit_ex(cipherCtx.getContext(), 0, encodedKey, iv, encrypting);
    } else {
        NativeCrypto.EVP_CipherInit_ex(cipherCtx.getContext(), cipherType, encodedKey, iv, encrypting);
    }
    // OpenSSL only supports PKCS5 Padding.
    NativeCrypto.EVP_CIPHER_CTX_set_padding(cipherCtx.getContext(), padding == Padding.PKCS5PADDING);
    modeBlockSize = NativeCrypto.EVP_CIPHER_CTX_block_size(cipherCtx.getContext());
    calledUpdate = false;
}
Also used : InvalidParameterException(java.security.InvalidParameterException) SecretKey(javax.crypto.SecretKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecureRandom(java.security.SecureRandom) InvalidKeyException(java.security.InvalidKeyException)

Example 64 with SecureRandom

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

Example 65 with SecureRandom

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

Aggregations

SecureRandom (java.security.SecureRandom)720 SSLContext (javax.net.ssl.SSLContext)106 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)97 IOException (java.io.IOException)87 Test (org.junit.Test)76 SecretKey (javax.crypto.SecretKey)62 X509Certificate (java.security.cert.X509Certificate)61 KeyGenerator (javax.crypto.KeyGenerator)57 TrustManager (javax.net.ssl.TrustManager)56 X509TrustManager (javax.net.ssl.X509TrustManager)47 Cipher (javax.crypto.Cipher)46 KeyPairGenerator (java.security.KeyPairGenerator)44 BigInteger (java.math.BigInteger)42 CertificateException (java.security.cert.CertificateException)40 InvalidKeyException (java.security.InvalidKeyException)35 KeyPair (java.security.KeyPair)34 KeyStore (java.security.KeyStore)34 SecretKeySpec (javax.crypto.spec.SecretKeySpec)30 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)28 KeyManagementException (java.security.KeyManagementException)28