Search in sources :

Example 71 with PKCS8EncodedKeySpec

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

the class KeyFactory method engineGeneratePrivate.

protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException {
    if (keySpec instanceof PKCS8EncodedKeySpec) {
        try {
            PrivateKeyInfo info = PrivateKeyInfo.getInstance(((PKCS8EncodedKeySpec) keySpec).getEncoded());
            PrivateKey key = BouncyCastleProvider.getPrivateKey(info);
            if (key != null) {
                return key;
            }
            throw new InvalidKeySpecException("no factory found for OID: " + info.getPrivateKeyAlgorithm().getAlgorithm());
        } catch (Exception e) {
            throw new InvalidKeySpecException(e.toString());
        }
    }
    throw new InvalidKeySpecException("Unknown KeySpec type: " + keySpec.getClass().getName());
}
Also used : PrivateKey(java.security.PrivateKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) PrivateKeyInfo(org.bouncycastle.asn1.pkcs.PrivateKeyInfo) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidKeyException(java.security.InvalidKeyException)

Example 72 with PKCS8EncodedKeySpec

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

the class EncryptedPrivateKeyInfoTest method test_ROUNDTRIP_GetKeySpecKeyProvider01.

/**
     * Encrypted data contains valid PKCS8 key info encoding
     */
public final void test_ROUNDTRIP_GetKeySpecKeyProvider01() {
    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], privateKeyInfo, 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 {
                    PKCS8EncodedKeySpec eks = epki.getKeySpec(g.pubK() == null ? g.k() : g.pubK(), provider[l]);
                    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 (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)

Example 73 with PKCS8EncodedKeySpec

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

the class PKCS8EncodedKeySpecTest method testGetEncoded.

/**
     * Test for <code>getEncoded()</code> method<br>
     * Assertion: returns encoded key
     */
public final void testGetEncoded() {
    byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
    PKCS8EncodedKeySpec meks = new PKCS8EncodedKeySpec(encodedKey);
    byte[] ek = meks.getEncoded();
    assertTrue(Arrays.equals(encodedKey, ek));
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec)

Example 74 with PKCS8EncodedKeySpec

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

the class PKCS8EncodedKeySpecTest method testPKCS8EncodedKeySpec.

//
// Tests
//
/**
     * Test for <code>PKCS8EncodedKeySpec</code> constructor<br>
     * Assertion: constructs new <code>PKCS8EncodedKeySpec</code>
     * object using valid parameter
     */
public final void testPKCS8EncodedKeySpec() {
    byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
    EncodedKeySpec eks = new PKCS8EncodedKeySpec(encodedKey);
    assertTrue(eks instanceof PKCS8EncodedKeySpec);
    try {
        eks = new PKCS8EncodedKeySpec(null);
        fail("expected NullPointerException");
    } catch (NullPointerException e) {
    // ok
    }
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) EncodedKeySpec(java.security.spec.EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec)

Example 75 with PKCS8EncodedKeySpec

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

the class PKCS8EncodedKeySpecTest method testIsStatePreserved1.

/**
     * Tests that internal state of the object
     * can not be changed by modifying initial
     * array value
     */
public final void testIsStatePreserved1() {
    // Reference array
    byte[] encodedKey = new byte[] { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
    // Reference array's copy will be used for test
    byte[] encodedKeyCopy = encodedKey.clone();
    PKCS8EncodedKeySpec meks = new PKCS8EncodedKeySpec(encodedKeyCopy);
    // Modify initial array's value
    encodedKeyCopy[3] = (byte) 5;
    // Get encoded key
    byte[] ek = meks.getEncoded();
    // Check  using reference array that
    // byte value has not been changed
    assertTrue(Arrays.equals(encodedKey, ek));
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec)

Aggregations

PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)227 KeyFactory (java.security.KeyFactory)179 PrivateKey (java.security.PrivateKey)148 CertificateFactory (java.security.cert.CertificateFactory)86 ByteArrayInputStream (java.io.ByteArrayInputStream)85 Certificate (java.security.cert.Certificate)72 X509Certificate (java.security.cert.X509Certificate)71 PrivateKeyEntry (java.security.KeyStore.PrivateKeyEntry)59 Entry (java.security.KeyStore.Entry)53 TrustedCertificateEntry (java.security.KeyStore.TrustedCertificateEntry)53 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)50 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)47 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)40 PublicKey (java.security.PublicKey)39 IOException (java.io.IOException)30 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)30 SecretKey (javax.crypto.SecretKey)28 InvalidKeyException (java.security.InvalidKeyException)26 Key (java.security.Key)24 KeyStoreException (java.security.KeyStoreException)15