Search in sources :

Example 1 with EncryptedPrivateKeyInfo

use of javax.crypto.EncryptedPrivateKeyInfo in project netty by netty.

the class SslContext method generateKeySpec.

/**
     * Generates a key specification for an (encrypted) private key.
     *
     * @param password characters, if {@code null} an unencrypted key is assumed
     * @param key bytes of the DER encoded private key
     *
     * @return a key specification
     *
     * @throws IOException if parsing {@code key} fails
     * @throws NoSuchAlgorithmException if the algorithm used to encrypt {@code key} is unkown
     * @throws NoSuchPaddingException if the padding scheme specified in the decryption algorithm is unkown
     * @throws InvalidKeySpecException if the decryption key based on {@code password} cannot be generated
     * @throws InvalidKeyException if the decryption key based on {@code password} cannot be used to decrypt
     *                             {@code key}
     * @throws InvalidAlgorithmParameterException if decryption algorithm parameters are somehow faulty
     */
protected static PKCS8EncodedKeySpec generateKeySpec(char[] password, byte[] key) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException, InvalidKeyException, InvalidAlgorithmParameterException {
    if (password == null) {
        return new PKCS8EncodedKeySpec(key);
    }
    EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = new EncryptedPrivateKeyInfo(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(encryptedPrivateKeyInfo.getAlgName());
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec);
    Cipher cipher = Cipher.getInstance(encryptedPrivateKeyInfo.getAlgName());
    cipher.init(Cipher.DECRYPT_MODE, pbeKey, encryptedPrivateKeyInfo.getAlgParameters());
    return encryptedPrivateKeyInfo.getKeySpec(cipher);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 2 with EncryptedPrivateKeyInfo

use of javax.crypto.EncryptedPrivateKeyInfo in project robovm by robovm.

the class EncryptedPrivateKeyInfoTest method testGetKeySpecKey01.

public final void testGetKeySpecKey01() {
    boolean performed = false;
    for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
        try {
            EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(EncryptedPrivateKeyInfoData.algName0[i][0], EncryptedPrivateKeyInfoData.encryptedData);
            try {
                // check that method under test throws NPE
                epki.getKeySpec((Key) null);
                fail(getName() + "NullPointerException has not been thrown");
            } catch (NullPointerException ok) {
            } catch (InvalidKeyException e) {
                fail(getName() + "Unexpected exception: " + e);
            }
            performed = true;
        } catch (NoSuchAlgorithmException allowedFailure) {
        }
    }
    assertTrue("Test not performed", performed);
}
Also used : EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 3 with EncryptedPrivateKeyInfo

use of javax.crypto.EncryptedPrivateKeyInfo in project robovm by robovm.

the class EncryptedPrivateKeyInfoTest method test_ROUNDTRIP_GetKeySpecKeyString02.

/**
     * Encrypted data contains invalid PKCS8 key info encoding
     */
public final void test_ROUNDTRIP_GetKeySpecKeyString02() throws Exception {
    boolean performed = false;
    for (int i = 0; i < algName.length; i++) {
        for (int l = 0; l < provider.length; l++) {
            if (provider[l] == null) {
                continue;
            }
            TestDataGenerator g;
            try {
                // generate test data
                g = new TestDataGenerator(algName[i][0], algName[i][1], privateKeyInfoDamaged, provider[l]);
            } catch (TestDataGenerator.AllowedFailure allowedFailure) {
                continue;
            }
            try {
                // create test object
                EncryptedPrivateKeyInfo epki;
                if (g.ap() == null) {
                    epki = new EncryptedPrivateKeyInfo(algName[i][0], g.ct());
                } else {
                    epki = new EncryptedPrivateKeyInfo(g.ap(), g.ct());
                }
                try {
                    epki.getKeySpec(g.pubK() == null ? g.k() : g.pubK(), provider[l].getName());
                    fail(algName[i][0] + ", " + algName[i][1]);
                } catch (InvalidKeyException e) {
                }
                performed = true;
            } catch (NoSuchAlgorithmException allowedFailure) {
            }
        }
    }
    assertTrue("Test not performed", performed);
}
Also used : EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 4 with EncryptedPrivateKeyInfo

use of javax.crypto.EncryptedPrivateKeyInfo in project robovm by robovm.

the class EncryptedPrivateKeyInfoTest method testGetEncoded01.

/**
     * Test #1 for <code>getEncoded()</code> method <br>
     * Assertion: returns the ASN.1 encoding of this object <br>
     * Test preconditions: test object created using ctor which takes encoded
     * form as the only parameter <br>
     * Expected: equivalent encoded form must be returned
     *
     * @throws IOException
     */
public final void testGetEncoded01() throws IOException {
    boolean performed = false;
    for (int i = 0; i < EncryptedPrivateKeyInfoData.algName0.length; i++) {
        try {
            byte[] enc = EncryptedPrivateKeyInfoData.getValidEncryptedPrivateKeyInfoEncoding(EncryptedPrivateKeyInfoData.algName0[i][0]);
            EncryptedPrivateKeyInfo epki = new EncryptedPrivateKeyInfo(enc);
            // check that method under test returns
            // valid encoded form
            assertTrue(Arrays.equals(enc, epki.getEncoded()));
            performed = true;
        } catch (NoSuchAlgorithmException allowedFailure) {
        }
    }
    assertTrue("Test not performed", performed);
}
Also used : EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 5 with EncryptedPrivateKeyInfo

use of javax.crypto.EncryptedPrivateKeyInfo in project robovm by robovm.

the class EncryptedPrivateKeyInfoTest method test_ROUNDTRIP_GetKeySpecKey01.

/**
     * Encrypted data contains valid PKCS8 key info encoding
     */
public final void test_ROUNDTRIP_GetKeySpecKey01() {
    boolean performed = false;
    for (int i = 0; i < algName.length; i++) {
        try {
            // generate test data
            TestDataGenerator g = new TestDataGenerator(algName[i][0], algName[i][1], privateKeyInfo, null);
            // create test object
            EncryptedPrivateKeyInfo epki;
            if (g.ap() == null) {
                epki = new EncryptedPrivateKeyInfo(algName[i][0], g.ct());
            } else {
                epki = new EncryptedPrivateKeyInfo(g.ap(), g.ct());
            }
            try {
                PKCS8EncodedKeySpec eks = epki.getKeySpec(g.pubK() == null ? g.k() : g.pubK());
                if (!Arrays.equals(privateKeyInfo, eks.getEncoded())) {
                    fail(algName[i][0] + " != " + algName[i][1]);
                }
            } catch (InvalidKeyException e) {
                fail(algName[i][0] + ", " + algName[i][1] + ": " + e);
            }
            performed = true;
        } catch (TestDataGenerator.AllowedFailure allowedFailure) {
        } catch (NoSuchAlgorithmException allowedFailure) {
        }
    }
    assertTrue("Test not performed", performed);
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

EncryptedPrivateKeyInfo (javax.crypto.EncryptedPrivateKeyInfo)40 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)26 AlgorithmParameters (java.security.AlgorithmParameters)10 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)10 InvalidKeyException (java.security.InvalidKeyException)7 SecretKey (javax.crypto.SecretKey)7 PBEKeySpec (javax.crypto.spec.PBEKeySpec)7 SecretKeyFactory (javax.crypto.SecretKeyFactory)6 Cipher (javax.crypto.Cipher)5 Key (java.security.Key)4 KeyFactory (java.security.KeyFactory)3 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 KeyStore (java.security.KeyStore)2 PrivateKey (java.security.PrivateKey)2 CertificateFactory (java.security.cert.CertificateFactory)2 X509Certificate (java.security.cert.X509Certificate)2 File (java.io.File)1