Search in sources :

Example 51 with CertificateFactory

use of java.security.cert.CertificateFactory in project robovm by robovm.

the class X509KeyManagerTest method init.

void init(String name) {
    keyType = name;
    try {
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        KeyFactory kf = KeyFactory.getInstance("RSA");
        keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
        keyTest.load(null, "1234".toCharArray());
        if (keyType.equals(CLIENT)) {
            keys = new PrivateKey[3];
            keys[0] = kf.generatePrivate(new PKCS8EncodedKeySpec(keyBytes));
            keys[1] = kf.generatePrivate(new PKCS8EncodedKeySpec(key2Bytes));
            keys[2] = kf.generatePrivate(new PKCS8EncodedKeySpec(key3Bytes));
            cert = new X509Certificate[3];
            cert[0] = (X509Certificate) cf.generateCertificate(certArray);
            cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
            cert[2] = (X509Certificate) cf.generateCertificate(certArray3);
            keyTest.setKeyEntry("clientKey_01", keys[0], PASSWORD, new X509Certificate[] { cert[0] });
            keyTest.setKeyEntry("clientKey_02", keys[1], PASSWORD, new X509Certificate[] { cert[0], cert[1] });
            keyTest.setKeyEntry("clientKey_03", keys[2], PASSWORD, new X509Certificate[] { cert[0], cert[2] });
            keyTest.setCertificateEntry("clientAlias_01", cert[0]);
            keyTest.setCertificateEntry("clientAlias_02", cert[0]);
            keyTest.setCertificateEntry("clientAlias_03", cert[1]);
        } else if (keyType.equals(SERVER)) {
            keys = new PrivateKey[1];
            keys[0] = kf.generatePrivate(new PKCS8EncodedKeySpec(keyBytes));
            cert = new X509Certificate[1];
            cert[0] = (X509Certificate) cf.generateCertificate(certArray3);
            keyTest.setKeyEntry("serverKey_00", keys[0], PASSWORD, new X509Certificate[] { cert[0] });
            keyTest.setCertificateEntry("serverAlias_00", cert[0]);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new IllegalArgumentException(ex.getMessage());
    }
    try {
        factory.init(keyTest, "1234".toCharArray());
    } catch (Exception e) {
        fail("Could't init the KeyManagerFactory");
    }
    manager = (X509KeyManager) factory.getKeyManagers()[0];
}
Also used : PrivateKey(java.security.PrivateKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) CertificateFactory(java.security.cert.CertificateFactory) KeyFactory(java.security.KeyFactory) X509Certificate(java.security.cert.X509Certificate) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 52 with CertificateFactory

use of java.security.cert.CertificateFactory in project robovm by robovm.

the class X509Certificate2Test method test_toString.

/**
     * Test for X.509 Certificate provider
     */
public void test_toString() throws Exception {
    // Regression for HARMONY-3384
    CertificateFactory certFact = CertificateFactory.getInstance("X509");
    X509Certificate pemCert = (X509Certificate) certFact.generateCertificate(new ByteArrayInputStream(TestUtils.getX509Certificate_v3()));
    // extension value is empty sequence
    byte[] extnValue = pemCert.getExtensionValue("2.5.29.35");
    assertEquals(Arrays.toString(new byte[] { 0x04, 0x02, 0x30, 0x00 }), Arrays.toString(extnValue));
    assertNotNull(pemCert.toString());
// End regression for HARMONY-3384
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate)

Example 53 with CertificateFactory

use of java.security.cert.CertificateFactory in project robovm by robovm.

the class PKIXBuilderParametersTest method testToString.

/**
     * Test for <code>toString()</code>
     */
public final void testToString() throws Exception {
    KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
    keyTest.load(null, null);
    ByteArrayInputStream certArray = new ByteArrayInputStream(certificate.getBytes());
    ByteArrayInputStream certArray2 = new ByteArrayInputStream(certificate2.getBytes());
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509Certificate[] cert = new X509Certificate[2];
    cert[0] = (X509Certificate) cf.generateCertificate(certArray);
    cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
    keyTest.setCertificateEntry("alias1", cert[0]);
    keyTest.setCertificateEntry("alias2", cert[0]);
    keyTest.setCertificateEntry("alias3", cert[1]);
    PKIXBuilderParameters p = new PKIXBuilderParameters(keyTest, new X509CertSelector());
    assertNotNull(p.toString());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PKIXBuilderParameters(java.security.cert.PKIXBuilderParameters) X509CertSelector(java.security.cert.X509CertSelector) KeyStore(java.security.KeyStore) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate)

Example 54 with CertificateFactory

use of java.security.cert.CertificateFactory in project robovm by robovm.

the class PKIXBuilderParametersTest method testPKIXBuilderParametersKeyStoreCertSelector04.

/**
     * Test #4 for <code>PKIXBuilderParameters(KeyStore, CertSelector)</code>
     * constructor<br>
     * Assertion: <code>NullPointerException</code> -
     * if the <code>keystore</code> is <code>null</code>
     */
public final void testPKIXBuilderParametersKeyStoreCertSelector04() throws Exception {
    KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
    keyTest.load(null, null);
    ByteArrayInputStream certArray = new ByteArrayInputStream(certificate.getBytes());
    ByteArrayInputStream certArray2 = new ByteArrayInputStream(certificate2.getBytes());
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509Certificate[] cert = new X509Certificate[2];
    cert[0] = (X509Certificate) cf.generateCertificate(certArray);
    cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
    keyTest.setCertificateEntry("alias1", cert[0]);
    keyTest.setCertificateEntry("alias2", cert[0]);
    keyTest.setCertificateEntry("alias3", cert[1]);
    try {
        PKIXBuilderParameters p = new PKIXBuilderParameters(keyTest, new X509CertSelector());
        assertEquals(3, p.getTrustAnchors().size());
        assertEquals(5, p.getMaxPathLength());
    } catch (Exception e) {
        fail("Unexpected exception " + e.getMessage());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PKIXBuilderParameters(java.security.cert.PKIXBuilderParameters) X509CertSelector(java.security.cert.X509CertSelector) KeyStore(java.security.KeyStore) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) KeyStoreException(java.security.KeyStoreException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InvalidParameterException(java.security.InvalidParameterException)

Example 55 with CertificateFactory

use of java.security.cert.CertificateFactory in project robovm by robovm.

the class PKIXBuilderParametersTest method testGetMaxPathLength.

/**
     * Test for <code>getMaxPathLength()</code>
     */
public final void testGetMaxPathLength() throws Exception {
    KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
    keyTest.load(null, null);
    ByteArrayInputStream certArray = new ByteArrayInputStream(certificate.getBytes());
    ByteArrayInputStream certArray2 = new ByteArrayInputStream(certificate2.getBytes());
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509Certificate[] cert = new X509Certificate[2];
    cert[0] = (X509Certificate) cf.generateCertificate(certArray);
    cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
    keyTest.setCertificateEntry("alias1", cert[0]);
    keyTest.setCertificateEntry("alias2", cert[0]);
    keyTest.setCertificateEntry("alias3", cert[1]);
    PKIXBuilderParameters p = new PKIXBuilderParameters(keyTest, new X509CertSelector());
    assertEquals(5, p.getMaxPathLength());
    p.setMaxPathLength(10);
    assertEquals(10, p.getMaxPathLength());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PKIXBuilderParameters(java.security.cert.PKIXBuilderParameters) X509CertSelector(java.security.cert.X509CertSelector) KeyStore(java.security.KeyStore) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate)

Aggregations

CertificateFactory (java.security.cert.CertificateFactory)550 X509Certificate (java.security.cert.X509Certificate)409 ByteArrayInputStream (java.io.ByteArrayInputStream)372 Certificate (java.security.cert.Certificate)272 CertificateException (java.security.cert.CertificateException)120 KeyFactory (java.security.KeyFactory)103 PrivateKey (java.security.PrivateKey)93 InputStream (java.io.InputStream)92 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)86 IOException (java.io.IOException)80 KeyStore (java.security.KeyStore)77 PrivateKeyEntry (java.security.KeyStore.PrivateKeyEntry)67 Entry (java.security.KeyStore.Entry)59 TrustedCertificateEntry (java.security.KeyStore.TrustedCertificateEntry)59 KeyStoreException (java.security.KeyStoreException)49 ArrayList (java.util.ArrayList)49 FileInputStream (java.io.FileInputStream)47 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)35 File (java.io.File)23 HashSet (java.util.HashSet)21