Search in sources :

Example 91 with Certificate

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

the class KeyStore2Test method test_getCreationDate.

/**
     * java.security.KeyStore#getCreationDate(String)
     */
public void test_getCreationDate() throws Exception {
    String type = "DSA";
    KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
    try {
        keyTest.getCreationDate("anAlias");
        fail();
    } catch (KeyStoreException expected) {
    }
    keyTest.load(null, pssWord);
    assertNull(keyTest.getCreationDate(""));
    try {
        keyTest.getCreationDate(null);
        fail();
    } catch (NullPointerException expected) {
    }
    Certificate[] chain = { new MyCertificate(type, testEncoding), new MyCertificate(type, testEncoding) };
    PrivateKey privateKey1 = KeyFactory.getInstance(type).generatePrivate(new DSAPrivateKeySpec(new BigInteger("0"), new BigInteger("0"), new BigInteger("0"), new BigInteger("0")));
    KeyStore.PasswordProtection pp = new KeyStore.PasswordProtection(pssWord);
    KeyStore.PrivateKeyEntry pke = new KeyStore.PrivateKeyEntry(getPrivateKey(), chain);
    KeyStore.PrivateKeyEntry pke1 = new KeyStore.PrivateKeyEntry(privateKey1, chain);
    keyTest.setEntry("alias1", pke, pp);
    keyTest.setEntry("alias2", pke1, pp);
    Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    int dayExpected = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    int monthExpected = Calendar.getInstance().get(Calendar.MONTH);
    int yearExpected = Calendar.getInstance().get(Calendar.YEAR);
    int hourExpected = Calendar.getInstance().get(Calendar.HOUR);
    int minuteExpected = Calendar.getInstance().get(Calendar.MINUTE);
    Calendar.getInstance().setTimeInMillis(keyTest.getCreationDate("alias1").getTime());
    int dayActual1 = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    int monthActual1 = Calendar.getInstance().get(Calendar.MONTH);
    int yearActual1 = Calendar.getInstance().get(Calendar.YEAR);
    int hourActual1 = Calendar.getInstance().get(Calendar.HOUR);
    int minuteActual1 = Calendar.getInstance().get(Calendar.MINUTE);
    assertEquals(dayExpected, dayActual1);
    assertEquals(monthExpected, monthActual1);
    assertEquals(yearExpected, yearActual1);
    assertEquals(hourExpected, hourActual1);
    assertEquals(minuteExpected, minuteActual1);
    Calendar.getInstance().setTimeInMillis(keyTest.getCreationDate("alias2").getTime());
    int dayActual2 = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    int monthActual2 = Calendar.getInstance().get(Calendar.MONTH);
    int yearActual2 = Calendar.getInstance().get(Calendar.YEAR);
    int hourActual2 = Calendar.getInstance().get(Calendar.HOUR);
    int minuteActual2 = Calendar.getInstance().get(Calendar.MINUTE);
    assertEquals(dayExpected, dayActual2);
    assertEquals(monthExpected, monthActual2);
    assertEquals(yearExpected, yearActual2);
    assertEquals(hourExpected, hourActual2);
    assertEquals(minuteExpected, minuteActual2);
    try {
        keyTest.getCreationDate(null);
        fail();
    } catch (NullPointerException expected) {
    }
}
Also used : DSAPrivateKey(java.security.interfaces.DSAPrivateKey) PrivateKey(java.security.PrivateKey) KeyStoreException(java.security.KeyStoreException) KeyStore(java.security.KeyStore) DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) BigInteger(java.math.BigInteger) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 92 with Certificate

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

the class myHostnameVerifier method test_getServerCertificates.

/**
     * javax.net.ssl.HttpsURLConnection#getServerCertificates()
     */
public final void test_getServerCertificates() throws Exception {
    URL url = new URL("https://localhost:55555");
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
    try {
        connection.getServerCertificates();
        fail("IllegalStateException wasn't thrown");
    } catch (IllegalStateException expected) {
    }
    HttpsURLConnection con = new MyHttpsURLConnection(new URL("https://www.fortify.net/"), "X.508");
    try {
        con.getServerCertificates();
        fail("SSLPeerUnverifiedException wasn't thrown");
    } catch (SSLPeerUnverifiedException expected) {
    }
    con = new MyHttpsURLConnection(new URL("https://www.fortify.net/"), "X.509");
    Certificate[] cert = con.getServerCertificates();
    assertNotNull(cert);
    assertEquals(1, cert.length);
}
Also used : SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Certificate(java.security.cert.Certificate)

Example 93 with Certificate

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

the class myHostnameVerifier method getServerCertificates.

/*
     * @see javax.net.ssl.HttpsURLConnection#getServerCertificates()
     */
public Certificate[] getServerCertificates() throws SSLPeerUnverifiedException {
    try {
        CertificateFactory cf = CertificateFactory.getInstance(typeDone);
        byte[] barr = TestUtils.getX509Certificate_v3();
        ByteArrayInputStream bis = new ByteArrayInputStream(barr);
        Certificate cert = cf.generateCertificate(bis);
        return new Certificate[] { cert };
    } catch (CertificateException se) {
        throw new SSLPeerUnverifiedException("No server's end-entity certificate");
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) CertificateException(java.security.cert.CertificateException) CertificateFactory(java.security.cert.CertificateFactory) Certificate(java.security.cert.Certificate)

Example 94 with Certificate

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

the class SSLSessionTest method test_getLocalCertificates.

/**
     * javax.net.ssl.SSLSession#getLocalCertificates()
     */
public void test_getLocalCertificates() throws Exception {
    KeyStore store = client.getStore();
    Certificate cert = store.getCertificate("mykey");
    Certificate[] certs = clientSession.getLocalCertificates();
    assertEquals(cert, certs[0]);
}
Also used : KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 95 with Certificate

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

the class CertificateFactory3Test method testGenerateCertPath01.

/**
     * Test for <code>generateCertPath(List certificates)</code> method
     * Assertion: returns CertPath with 1 Certificate
     */
public void testGenerateCertPath01() throws Exception {
    CertificateFactory[] certFs = initCertFs();
    assertNotNull("CertificateFactory objects were not created", certFs);
    // create list of certificates with one certificate
    Certificate cert = certFs[0].generateCertificate(new ByteArrayInputStream(TestUtils.getEncodedX509Certificate()));
    List<Certificate> list = new Vector<Certificate>();
    list.add(cert);
    for (int i = 0; i < certFs.length; i++) {
        CertPath certPath = null;
        certPath = certFs[i].generateCertPath(list);
        assertEquals(cert.getType(), certPath.getType());
        List<? extends Certificate> list1 = certPath.getCertificates();
        assertFalse("Result list is empty", list1.isEmpty());
        Iterator<? extends Certificate> it = list1.iterator();
        assertEquals("Incorrect Certificate in CertPath", cert, it.next());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CertPath(java.security.cert.CertPath) CertificateFactory(java.security.cert.CertificateFactory) Vector(java.util.Vector) Certificate(java.security.cert.Certificate)

Aggregations

Certificate (java.security.cert.Certificate)723 X509Certificate (java.security.cert.X509Certificate)469 CertificateFactory (java.security.cert.CertificateFactory)272 ByteArrayInputStream (java.io.ByteArrayInputStream)237 KeyStore (java.security.KeyStore)133 PrivateKey (java.security.PrivateKey)132 IOException (java.io.IOException)106 CertificateException (java.security.cert.CertificateException)102 KeyFactory (java.security.KeyFactory)89 KeyStoreException (java.security.KeyStoreException)88 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)72 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)69 PrivateKeyEntry (java.security.KeyStore.PrivateKeyEntry)63 ArrayList (java.util.ArrayList)63 TrustedCertificateEntry (java.security.KeyStore.TrustedCertificateEntry)56 Entry (java.security.KeyStore.Entry)53 PublicKey (java.security.PublicKey)48 InputStream (java.io.InputStream)40 FileInputStream (java.io.FileInputStream)39 Key (java.security.Key)36