Search in sources :

Example 41 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project robovm by robovm.

the class APSpecSpi method testKeyGeneratorSpi01.

/**
     * Test for <code>KeyGeneratorSpi</code> constructor Assertion: constructs
     * KeyGeneratorSpi
     */
public void testKeyGeneratorSpi01() throws InvalidAlgorithmParameterException {
    Mock_KeyGeneratorSpi kgSpi = new Mock_KeyGeneratorSpi();
    assertNull("Not null result", kgSpi.engineGenerateKey());
    try {
        kgSpi.engineInit(77, new SecureRandom());
        fail("IllegalArgumentException must be thrown");
    } catch (IllegalArgumentException e) {
    }
    try {
        kgSpi.engineInit(new SecureRandom());
        fail("IllegalArgumentException must be thrown");
    } catch (IllegalArgumentException e) {
    }
    AlgorithmParameterSpec aps = null;
    try {
        kgSpi.engineInit(aps, new SecureRandom());
        fail("InvalidAlgorithmParameterException must be thrown when parameter is null");
    } catch (InvalidAlgorithmParameterException e) {
    }
    aps = new APSpecSpi();
    kgSpi.engineInit(aps, new SecureRandom());
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecureRandom(java.security.SecureRandom) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 42 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project robovm by robovm.

the class myKeyGenerator method testKeyGenerator.

/**
     * Test for <code>KeyGenerator</code> constructor Assertion: returns
     * KeyGenerator object
     */
public void testKeyGenerator() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    KeyGeneratorSpi spi = new MyKeyGeneratorSpi();
    KeyGenerator keyG = new myKeyGenerator(spi, defaultProvider, defaultAlgorithm);
    assertEquals("Incorrect algorithm", keyG.getAlgorithm(), defaultAlgorithm);
    assertEquals("Incorrect provider", keyG.getProvider(), defaultProvider);
    AlgorithmParameterSpec params = null;
    int keysize = 0;
    try {
        keyG.init(params, null);
        fail("InvalidAlgorithmParameterException must be thrown");
    } catch (InvalidAlgorithmParameterException e) {
    }
    try {
        keyG.init(keysize, null);
        fail("IllegalArgumentException must be thrown");
    } catch (IllegalArgumentException e) {
    }
    keyG = new myKeyGenerator(null, null, null);
    assertNull("Algorithm must be null", keyG.getAlgorithm());
    assertNull("Provider must be null", keyG.getProvider());
    try {
        keyG.init(params, null);
        fail("NullPointerException must be thrown");
    } catch (NullPointerException e) {
    }
    try {
        keyG.init(keysize, null);
        fail("NullPointerException or InvalidParameterException must be thrown");
    } catch (InvalidParameterException e) {
    } catch (NullPointerException e) {
    }
}
Also used : InvalidParameterException(java.security.InvalidParameterException) MyKeyGeneratorSpi(org.apache.harmony.crypto.tests.support.MyKeyGeneratorSpi) KeyGeneratorSpi(javax.crypto.KeyGeneratorSpi) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) MyKeyGeneratorSpi(org.apache.harmony.crypto.tests.support.MyKeyGeneratorSpi) KeyGenerator(javax.crypto.KeyGenerator) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 43 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project robovm by robovm.

the class myKeyGenerator method testInitParams.

/*
     * Test for <code>init(AlgorithmParameterSpec params)</code> and
     * <code>init(AlgorithmParameterSpec params, SecureRandom random)</code> methods
     * Assertion: throws InvalidAlgorithmParameterException when params is null
     */
public void testInitParams() throws Exception {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    KeyGenerator[] kgs = createKGs();
    AlgorithmParameterSpec aps = null;
    for (int i = 0; i < kgs.length; i++) {
        try {
            kgs[i].init(aps);
            fail("InvalidAlgorithmParameterException must be thrown");
        } catch (InvalidAlgorithmParameterException e) {
        }
        try {
            kgs[i].init(aps, new SecureRandom());
            fail("InvalidAlgorithmParameterException must be thrown");
        } catch (InvalidAlgorithmParameterException e) {
        }
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecureRandom(java.security.SecureRandom) KeyGenerator(javax.crypto.KeyGenerator) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 44 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project robovm by robovm.

the class CipherTest method test_updateLjava_nio_ByteBufferLjava_nio_ByteBuffer.

public void test_updateLjava_nio_ByteBufferLjava_nio_ByteBuffer() throws Exception {
    byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
    ByteBuffer bInput = ByteBuffer.allocate(256);
    ByteBuffer bOutput = ByteBuffer.allocate(256);
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES);
    bInput.put(b, 0, 10);
    bInput.rewind();
    bOutput.rewind();
    c.update(bInput, bOutput);
    c = Cipher.getInstance("DES/CBC/NoPadding");
    try {
        c.update(bInput, bOutput);
        fail();
    } catch (IllegalStateException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES);
    bInput = ByteBuffer.allocate(16);
    bInput.put(b, 0, 16);
    bInput.rewind();
    bOutput.rewind();
    c.update(bInput, bOutput);
    AlgorithmParameterSpec ap = new IvParameterSpec(IV);
    c = Cipher.getInstance("DES/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
    bInput = ByteBuffer.allocate(64);
    c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES);
    bInput.put(b, 0, 16);
    bInput.rewind();
    try {
        c.update(bInput, bInput);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES);
    bInput.put(b, 0, 16);
    bInput.rewind();
    bOutput.rewind();
    try {
        c.update(bInput, bOutput.asReadOnlyBuffer());
        fail();
    } catch (ReadOnlyBufferException expected) {
    }
    bInput.rewind();
    bInput.put(b, 0, 16);
    bInput.rewind();
    bOutput = ByteBuffer.allocate(8);
    c = Cipher.getInstance("DESede");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES);
    try {
        c.update(bInput, bOutput);
        fail();
    } catch (ShortBufferException expected) {
    }
}
Also used : ReadOnlyBufferException(java.nio.ReadOnlyBufferException) ShortBufferException(javax.crypto.ShortBufferException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) ByteBuffer(java.nio.ByteBuffer) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 45 with AlgorithmParameterSpec

use of java.security.spec.AlgorithmParameterSpec in project jdk8u_jdk by JetBrains.

the class PKCS12KeyStore method encryptPrivateKey.

/*
     * Encrypt private key using Password-based encryption (PBE)
     * as defined in PKCS#5.
     *
     * NOTE: By default, pbeWithSHAAnd3-KeyTripleDES-CBC algorithmID is
     *       used to derive the key and IV.
     *
     * @return encrypted private key encoded as EncryptedPrivateKeyInfo
     */
private byte[] encryptPrivateKey(byte[] data, KeyStore.PasswordProtection passwordProtection) throws IOException, NoSuchAlgorithmException, UnrecoverableKeyException {
    byte[] key = null;
    try {
        String algorithm;
        AlgorithmParameters algParams;
        AlgorithmId algid;
        // Initialize PBE algorithm and parameters
        algorithm = passwordProtection.getProtectionAlgorithm();
        if (algorithm != null) {
            AlgorithmParameterSpec algParamSpec = passwordProtection.getProtectionParameters();
            if (algParamSpec != null) {
                algParams = AlgorithmParameters.getInstance(algorithm);
                algParams.init(algParamSpec);
            } else {
                algParams = getAlgorithmParameters(algorithm);
            }
        } else {
            // Check default key protection algorithm for PKCS12 keystores
            algorithm = AccessController.doPrivileged(new PrivilegedAction<String>() {

                public String run() {
                    String prop = Security.getProperty(KEY_PROTECTION_ALGORITHM[0]);
                    if (prop == null) {
                        prop = Security.getProperty(KEY_PROTECTION_ALGORITHM[1]);
                    }
                    return prop;
                }
            });
            if (algorithm == null || algorithm.isEmpty()) {
                algorithm = "PBEWithSHA1AndDESede";
            }
            algParams = getAlgorithmParameters(algorithm);
        }
        ObjectIdentifier pbeOID = mapPBEAlgorithmToOID(algorithm);
        if (pbeOID == null) {
            throw new IOException("PBE algorithm '" + algorithm + " 'is not supported for key entry protection");
        }
        // Use JCE
        SecretKey skey = getPBEKey(passwordProtection.getPassword());
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, skey, algParams);
        byte[] encryptedKey = cipher.doFinal(data);
        algid = new AlgorithmId(pbeOID, cipher.getParameters());
        if (debug != null) {
            debug.println("  (Cipher algorithm: " + cipher.getAlgorithm() + ")");
        }
        // wrap encrypted private key in EncryptedPrivateKeyInfo
        // as defined in PKCS#8
        EncryptedPrivateKeyInfo encrInfo = new EncryptedPrivateKeyInfo(algid, encryptedKey);
        key = encrInfo.getEncoded();
    } catch (Exception e) {
        UnrecoverableKeyException uke = new UnrecoverableKeyException("Encrypt Private Key failed: " + e.getMessage());
        uke.initCause(e);
        throw uke;
    }
    return key;
}
Also used : KeyStoreException(java.security.KeyStoreException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) DestroyFailedException(javax.security.auth.DestroyFailedException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SecretKey(javax.crypto.SecretKey) UnrecoverableKeyException(java.security.UnrecoverableKeyException) AlgorithmId(sun.security.x509.AlgorithmId) PrivilegedAction(java.security.PrivilegedAction) EncryptedPrivateKeyInfo(sun.security.pkcs.EncryptedPrivateKeyInfo) Cipher(javax.crypto.Cipher) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) AlgorithmParameters(java.security.AlgorithmParameters) ObjectIdentifier(sun.security.util.ObjectIdentifier)

Aggregations

AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)186 IvParameterSpec (javax.crypto.spec.IvParameterSpec)59 Cipher (javax.crypto.Cipher)55 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)51 InvalidKeyException (java.security.InvalidKeyException)42 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)37 SecureRandom (java.security.SecureRandom)27 SecretKey (javax.crypto.SecretKey)27 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)24 BigInteger (java.math.BigInteger)21 BadPaddingException (javax.crypto.BadPaddingException)21 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)20 RSAKeyGenParameterSpec (java.security.spec.RSAKeyGenParameterSpec)19 ShortBufferException (javax.crypto.ShortBufferException)19 Key (java.security.Key)18 SecretKeySpec (javax.crypto.spec.SecretKeySpec)18 AlgorithmParameters (java.security.AlgorithmParameters)17 KeyGenerator (javax.crypto.KeyGenerator)17 OAEPParameterSpec (javax.crypto.spec.OAEPParameterSpec)15 IOException (java.io.IOException)14