Search in sources :

Example 86 with KeyStore

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

the class TrustManagerFactory2Test method checkResult.

private void checkResult(TrustManagerFactory tmf) throws Exception {
    KeyStore kStore = null;
    ManagerFactoryParameters mfp = null;
    try {
        tmf.init(kStore);
        fail("KeyStoreException must be thrown");
    } catch (KeyStoreException e) {
    }
    try {
        tmf.init(mfp);
        fail("InvalidAlgorithmParameterException must be thrown");
    } catch (InvalidAlgorithmParameterException e) {
    }
    assertNull("getTrustManagers() should return null object", tmf.getTrustManagers());
    try {
        kStore = KeyStore.getInstance(KeyStore.getDefaultType());
        kStore.load(null, null);
    } catch (KeyStoreException e) {
        fail("default keystore is not supported");
        return;
    }
    tmf.init(kStore);
    mfp = (ManagerFactoryParameters) new MyTrustManagerFactorySpi.Parameters(null);
    try {
        tmf.init(mfp);
        fail("RuntimeException must be thrown");
    } catch (RuntimeException e) {
        assertTrue("Incorrect exception", e.getCause() instanceof KeyStoreException);
    }
    mfp = (ManagerFactoryParameters) new MyTrustManagerFactorySpi.Parameters(kStore);
    tmf.init(mfp);
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) ManagerFactoryParameters(javax.net.ssl.ManagerFactoryParameters) KeyStoreException(java.security.KeyStoreException) KeyStore(java.security.KeyStore) ManagerFactoryParameters(javax.net.ssl.ManagerFactoryParameters)

Example 87 with KeyStore

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

the class SSLSessionTest method test_getPeerPrincipal.

/**
     * javax.net.ssl.SSLSession#getPeerPrincipal()
     */
public void test_getPeerPrincipal() throws Exception {
    Principal p1 = clientSession.getPeerPrincipal();
    KeyStore store = server.getStore();
    X509Certificate cert = (X509Certificate) store.getCertificate("mykey");
    Principal p2 = cert.getSubjectX500Principal();
    assertEquals(p1, p2);
}
Also used : KeyStore(java.security.KeyStore) Principal(java.security.Principal) X509Certificate(java.security.cert.X509Certificate)

Example 88 with KeyStore

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

the class PKIXBuilderParametersTest method testPKIXBuilderParametersKeyStoreCertSelector03.

/**
     * Test #3 for <code>PKIXBuilderParameters(KeyStore, CertSelector)</code>
     * constructor<br>
     * Assertion: <code>InvalidAlgorithmParameterException</code> - if the
     * <code>keystore</code> does not contain at least one trusted certificate
     * entry
     */
public final void testPKIXBuilderParametersKeyStoreCertSelector03() throws Exception {
    KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
    keyTest.load(null, null);
    try {
        new PKIXBuilderParameters(keyTest, new X509CertSelector());
        fail("InvalidAlgorithmParameterException expected");
    } catch (InvalidAlgorithmParameterException e) {
    // expected
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) PKIXBuilderParameters(java.security.cert.PKIXBuilderParameters) X509CertSelector(java.security.cert.X509CertSelector) KeyStore(java.security.KeyStore)

Example 89 with KeyStore

use of java.security.KeyStore 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 90 with KeyStore

use of java.security.KeyStore 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)

Aggregations

KeyStore (java.security.KeyStore)738 IOException (java.io.IOException)190 X509Certificate (java.security.cert.X509Certificate)189 FileInputStream (java.io.FileInputStream)163 KeyStoreException (java.security.KeyStoreException)151 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)138 InputStream (java.io.InputStream)125 Certificate (java.security.cert.Certificate)124 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)119 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)114 SSLContext (javax.net.ssl.SSLContext)112 PrivateKey (java.security.PrivateKey)94 CertificateException (java.security.cert.CertificateException)94 File (java.io.File)82 ByteArrayInputStream (java.io.ByteArrayInputStream)75 CertificateFactory (java.security.cert.CertificateFactory)75 Key (java.security.Key)61 UnrecoverableKeyException (java.security.UnrecoverableKeyException)55 TrustManager (javax.net.ssl.TrustManager)47 KeyManagementException (java.security.KeyManagementException)40