Search in sources :

Example 96 with SecureRandom

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

the class KeyGeneratorTest method test_KeyGenerator.

private void test_KeyGenerator(KeyGenerator kg) throws Exception {
    if (isUnsupported(kg)) {
        return;
    }
    kg.init((SecureRandom) null);
    test_SecretKey(kg, kg.generateKey());
    kg.init(new SecureRandom());
    test_SecretKey(kg, kg.generateKey());
    String algorithm = kg.getAlgorithm();
    List<Integer> keySizes = getKeySizes(algorithm);
    for (int keySize : keySizes) {
        kg.init(keySize);
        test_SecretKey(kg, kg.generateKey());
        kg.init(keySize, (SecureRandom) null);
        test_SecretKey(kg, kg.generateKey());
        kg.init(keySize, new SecureRandom());
        test_SecretKey(kg, kg.generateKey());
    }
}
Also used : SecureRandom(java.security.SecureRandom)

Example 97 with SecureRandom

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

the class CipherTest method getEncryptAlgorithmParameterSpec.

private static AlgorithmParameterSpec getEncryptAlgorithmParameterSpec(String algorithm) {
    if (isPBE(algorithm)) {
        final byte[] salt = new byte[8];
        new SecureRandom().nextBytes(salt);
        return new PBEParameterSpec(salt, 1024);
    }
    if (algorithm.equals("AES/CBC/NOPADDING") || algorithm.equals("AES/CBC/PKCS5PADDING") || algorithm.equals("AES/CFB/NOPADDING") || algorithm.equals("AES/CTR/NOPADDING") || algorithm.equals("AES/CTS/NOPADDING") || algorithm.equals("AES/OFB/NOPADDING")) {
        final byte[] iv = new byte[16];
        new SecureRandom().nextBytes(iv);
        return new IvParameterSpec(iv);
    }
    return null;
}
Also used : SecureRandom(java.security.SecureRandom) IvParameterSpec(javax.crypto.spec.IvParameterSpec) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 98 with SecureRandom

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

the class KeyFactory2Test method test_getKeySpecLjava_security_KeyLjava_lang_Class.

public void test_getKeySpecLjava_security_KeyLjava_lang_Class() throws Exception {
    // java.lang.Class)
    for (int i = 0; i < keyfactAlgs.length; i++) {
        KeyFactory fact = KeyFactory.getInstance(keyfactAlgs[i], providerName);
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(keyfactAlgs[i]);
        // We don't use getInstance
        SecureRandom random = new SecureRandom();
        keyGen.initialize(StandardNames.getMinimumKeySize(keyfactAlgs[i]), random);
        KeepAlive keepalive = createKeepAlive(keyfactAlgs[i]);
        KeyPair keys = keyGen.generateKeyPair();
        if (keepalive != null) {
            keepalive.interrupt();
        }
        KeySpec privateKeySpec = fact.getKeySpec(keys.getPrivate(), StandardNames.getPrivateKeySpecClass(keyfactAlgs[i]));
        KeySpec publicKeySpec = fact.getKeySpec(keys.getPublic(), StandardNames.getPublicKeySpecClass(keyfactAlgs[i]));
        PrivateKey privateKey = fact.generatePrivate(privateKeySpec);
        PublicKey publicKey = fact.generatePublic(publicKeySpec);
        assertEquals("generatePrivate generated different key for algorithm " + keyfactAlgs[i] + " (provider=" + fact.getProvider().getName() + ")", Arrays.toString(keys.getPrivate().getEncoded()), Arrays.toString(privateKey.getEncoded()));
        assertEquals("generatePublic generated different key for algorithm " + keyfactAlgs[i] + " (provider=" + fact.getProvider().getName() + ")", Arrays.toString(keys.getPublic().getEncoded()), Arrays.toString(publicKey.getEncoded()));
        KeySpec encodedSpec = fact.getKeySpec(keys.getPublic(), X509EncodedKeySpec.class);
        assertTrue("improper key spec for encoded public key", encodedSpec.getClass().equals(X509EncodedKeySpec.class));
        encodedSpec = fact.getKeySpec(keys.getPrivate(), PKCS8EncodedKeySpec.class);
        assertTrue("improper key spec for encoded private key", encodedSpec.getClass().equals(PKCS8EncodedKeySpec.class));
    }
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeySpec(java.security.spec.KeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) SecureRandom(java.security.SecureRandom) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyPairGenerator(java.security.KeyPairGenerator) KeyFactory(java.security.KeyFactory)

Example 99 with SecureRandom

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

the class KeyPairGenerator1Test method testKeyPairGenerator13.

/**
     * Test for methods: <code>initialize(int keysize)</code>
     * <code>initialize(int keysize, SecureRandom random)</code>
     * <code>initialize(AlgorithmParameterSpec param)</code>
     * <code>initialize(AlgorithmParameterSpec param, SecureRandom random)</code>
     * <code>generateKeyPair()</code>
     * <code>genKeyPair()</code>
     * Assertion: initialize(int ...) throws InvalidParameterException when
     * keysize in incorrect Assertion: initialize(AlgorithmParameterSpec
     * ...)throws UnsupportedOperationException Assertion: generateKeyPair() and
     * genKeyPair() return not null KeyPair Additional class MyKeyPairGenerator2
     * is used
     */
public void testKeyPairGenerator13() {
    int[] keys = { -1, -250, 1, 63, -512, -1024 };
    SecureRandom random = new SecureRandom();
    KeyPairGenerator mKPG = new MyKeyPairGenerator2(null);
    assertEquals("Algorithm must be null", mKPG.getAlgorithm(), MyKeyPairGenerator2.getResAlgorithm());
    assertNull("genKeyPair() must return null", mKPG.genKeyPair());
    assertNull("generateKeyPair() mut return null", mKPG.generateKeyPair());
    for (int i = 0; i < keys.length; i++) {
        try {
            mKPG.initialize(keys[i]);
            fail("InvalidParameterException must be thrown (key: " + Integer.toString(keys[i]) + ")");
        } catch (InvalidParameterException e) {
        }
        try {
            mKPG.initialize(keys[i], random);
            fail("InvalidParameterException must be thrown (key: " + Integer.toString(keys[i]) + ")");
        } catch (InvalidParameterException e) {
        }
    }
    try {
        mKPG.initialize(64);
    } catch (InvalidParameterException e) {
        fail("Unexpected InvalidParameterException was thrown");
    }
    try {
        mKPG.initialize(64, null);
    } catch (InvalidParameterException e) {
        fail("Unexpected InvalidParameterException was thrown");
    }
    try {
        mKPG.initialize(null, random);
    } catch (UnsupportedOperationException e) {
    // on j2se1.4 this exception is not thrown
    } catch (InvalidAlgorithmParameterException e) {
        fail("Unexpected InvalidAlgorithmParameterException was thrown");
    }
}
Also used : MyKeyPairGenerator2(org.apache.harmony.security.tests.support.MyKeyPairGenerator2) InvalidParameterException(java.security.InvalidParameterException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecureRandom(java.security.SecureRandom) KeyPairGenerator(java.security.KeyPairGenerator)

Example 100 with SecureRandom

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

the class KeyPairGenerator1Test method testKeyPairGenerator11.

/**
     * Test for methods:
     * <code>initialize(int keysize)</code>
     * <code>initialize(int keysize, SecureRandom random)</code>
     * <code>initialize(AlgorithmParameterSpec param)</code>
     * <code>initialize(AlgorithmParameterSpec param, SecureRandom random)</code>
     * Assertion: throws InvalidParameterException or
     * InvalidAlgorithmParameterException when parameters keysize or param are
     * incorrect
     */
public void testKeyPairGenerator11() throws NoSuchAlgorithmException, NoSuchProviderException {
    if (!DSASupported) {
        fail(NotSupportMsg);
        return;
    }
    int[] keys = { -10000, -1024, -1, 0, 10000 };
    KeyPairGenerator[] kpg = createKPGen();
    assertNotNull("KeyPairGenerator objects were not created", kpg);
    SecureRandom random = new SecureRandom();
    AlgorithmParameterSpec aps = null;
    for (int i = 0; i < kpg.length; i++) {
        for (int j = 0; j < keys.length; j++) {
            try {
                kpg[i].initialize(keys[j]);
                kpg[i].initialize(keys[j], random);
            } catch (InvalidParameterException e) {
            }
        }
        try {
            kpg[i].initialize(aps);
            kpg[i].initialize(aps, random);
        } catch (InvalidAlgorithmParameterException e) {
        }
    }
}
Also used : InvalidParameterException(java.security.InvalidParameterException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecureRandom(java.security.SecureRandom) KeyPairGenerator(java.security.KeyPairGenerator) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Aggregations

SecureRandom (java.security.SecureRandom)639 SSLContext (javax.net.ssl.SSLContext)94 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)87 IOException (java.io.IOException)68 Test (org.junit.Test)62 SecretKey (javax.crypto.SecretKey)59 KeyGenerator (javax.crypto.KeyGenerator)53 X509Certificate (java.security.cert.X509Certificate)50 TrustManager (javax.net.ssl.TrustManager)50 Cipher (javax.crypto.Cipher)43 X509TrustManager (javax.net.ssl.X509TrustManager)40 KeyPairGenerator (java.security.KeyPairGenerator)38 BigInteger (java.math.BigInteger)35 InvalidKeyException (java.security.InvalidKeyException)34 CertificateException (java.security.cert.CertificateException)33 KeyPair (java.security.KeyPair)30 KeyStore (java.security.KeyStore)29 Random (java.util.Random)28 SecretKeySpec (javax.crypto.spec.SecretKeySpec)28 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)27