Search in sources :

Example 86 with Certificate

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

the class KSPrivateKeyEntryTest method testGetCertificate.

/**
     * Test for <code>getCertificate()</code> method
     * Assertion: returns end Certificate (with 0 index in chain)
     */
public void testGetCertificate() {
    createParams(false, false);
    KeyStore.PrivateKeyEntry ksPKE = new KeyStore.PrivateKeyEntry(testPrivateKey, testChain);
    Certificate res = ksPKE.getCertificate();
    assertEquals("Incorrect end certificate (number 0)", testChain[0], res);
}
Also used : KeyStore(java.security.KeyStore) Certificate(java.security.cert.Certificate) MyCertificate(org.apache.harmony.security.tests.support.cert.MyCertificate)

Example 87 with Certificate

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

the class OpenSSLX509CertificateFactory method engineGenerateCertPath.

@Override
public CertPath engineGenerateCertPath(List<? extends Certificate> certificates) throws CertificateException {
    final List<X509Certificate> filtered = new ArrayList<X509Certificate>(certificates.size());
    for (int i = 0; i < certificates.size(); i++) {
        final Certificate c = certificates.get(i);
        if (!(c instanceof X509Certificate)) {
            throw new CertificateException("Certificate not X.509 type at index " + i);
        }
        filtered.add((X509Certificate) c);
    }
    return new OpenSSLX509CertPath(filtered);
}
Also used : ArrayList(java.util.ArrayList) CertificateException(java.security.cert.CertificateException) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 88 with Certificate

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

the class KeyStorePrivateKeyEntryTest method testGetCertificateChain.

public void testGetCertificateChain() throws Exception {
    String certificateData = "-----BEGIN CERTIFICATE-----\n" + "MIICZTCCAdICBQL3AAC2MA0GCSqGSIb3DQEBAgUAMF8xCzAJBgNVBAYTAlVTMSAw\n" + "HgYDVQQKExdSU0EgRGF0YSBTZWN1cml0eSwgSW5jLjEuMCwGA1UECxMlU2VjdXJl\n" + "IFNlcnZlciBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw05NzAyMjAwMDAwMDBa\n" + "Fw05ODAyMjAyMzU5NTlaMIGWMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZv\n" + "cm5pYTESMBAGA1UEBxMJUGFsbyBBbHRvMR8wHQYDVQQKExZTdW4gTWljcm9zeXN0\n" + "ZW1zLCBJbmMuMSEwHwYDVQQLExhUZXN0IGFuZCBFdmFsdWF0aW9uIE9ubHkxGjAY\n" + "BgNVBAMTEWFyZ29uLmVuZy5zdW4uY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB\n" + "iQKBgQCofmdY+PiUWN01FOzEewf+GaG+lFf132UpzATmYJkA4AEA/juW7jSi+LJk\n" + "wJKi5GO4RyZoyimAL/5yIWDV6l1KlvxyKslr0REhMBaD/3Z3EsLTTEf5gVrQS6sT\n" + "WMoSZAyzB39kFfsB6oUXNtV8+UKKxSxKbxvhQn267PeCz5VX2QIDAQABMA0GCSqG\n" + "SIb3DQEBAgUAA34AXl3at6luiV/7I9MN5CXYoPJYI8Bcdc1hBagJvTMcmlqL2uOZ\n" + "H9T5hNMEL9Tk6aI7yZPXcw/xI2K6pOR/FrMp0UwJmdxX7ljV6ZtUZf7pY492UqwC\n" + "1777XQ9UEZyrKJvF5ntleeO0ayBqLGVKCWzWZX9YsXCpv47FNLZbupE=\n" + "-----END CERTIFICATE-----\n";
    ByteArrayInputStream certArray;
    {
        try {
            certArray = new ByteArrayInputStream(certificateData.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e.getMessage());
        }
    }
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    Certificate certificate = cf.generateCertificate(certArray);
    assertTrue(certificate instanceof X509Certificate);
    String algorithm = certificate.getPublicKey().getAlgorithm();
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);
    KeyPair keyPair = keyPairGenerator.generateKeyPair();
    PrivateKey privateKey = keyPair.getPrivate();
    // If all the certificate in the chain is X509Certificate,
    // KeyStore.PrivateKeyEntry will return a X509Certificate array.
    KeyStore.PrivateKeyEntry privateKeyEntry = new KeyStore.PrivateKeyEntry(privateKey, new Certificate[] { certificate });
    Certificate[] chain = privateKeyEntry.getCertificateChain();
    assertTrue(chain instanceof X509Certificate[]);
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) UnsupportedEncodingException(java.io.UnsupportedEncodingException) KeyPairGenerator(java.security.KeyPairGenerator) CertificateFactory(java.security.cert.CertificateFactory) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) ByteArrayInputStream(java.io.ByteArrayInputStream) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 89 with Certificate

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

the class KeyStore2Test method test_getCertificateLjava_lang_String.

/**
     * java.security.KeyStore#getCertificate(java.lang.String)
     */
public void test_getCertificateLjava_lang_String() throws Exception {
    // Test for method java.security.cert.Certificate
    // java.security.KeyStore.getCertificate(java.lang.String)
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509Certificate[] cert = new X509Certificate[2];
    cert[0] = (X509Certificate) cf.generateCertificate(certArray);
    cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
    KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
    try {
        keyTest.getCertificate("anAlias");
        fail();
    } catch (KeyStoreException expected) {
    }
    keyTest.load(null, null);
    // alias 1
    PublicKey pub = cert[0].getPublicKey();
    keyTest.setCertificateEntry("alias1", cert[0]);
    Certificate certRes = keyTest.getCertificate("alias1");
    assertEquals("the public key of the certificate from getCertificate() " + "did not equal the original certificate", pub, certRes.getPublicKey());
    // alias 2
    keyTest.setCertificateEntry("alias2", cert[0]);
    // testing for a certificate chain
    Certificate cert2 = keyTest.getCertificate("alias2");
    assertEquals("the certificate for alias2 is supposed to exist", cert2, cert[0]);
}
Also used : PublicKey(java.security.PublicKey) KeyStoreException(java.security.KeyStoreException) CertificateFactory(java.security.cert.CertificateFactory) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 90 with Certificate

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

the class KeyStore2Test method test_deleteEntry.

/**
     * java.security.KeyStore#deleteEntry(String)
     */
public void test_deleteEntry() throws Exception {
    try {
        KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
        keyTest.load(null, null);
        keyTest.deleteEntry(null);
        fail();
    } catch (NullPointerException expected) {
    }
    KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
    keyTest.load(null, "password".toCharArray());
    KeyStore.PasswordProtection pp = new KeyStore.PasswordProtection(pssWord);
    Certificate[] chain = { new MyCertificate("DSA", testEncoding), new MyCertificate("DSA", testEncoding) };
    KeyStore.PrivateKeyEntry pkEntry = new KeyStore.PrivateKeyEntry(getPrivateKey(), chain);
    keyTest.setEntry("symKey", pkEntry, pp);
    keyTest.deleteEntry("symKey");
}
Also used : KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) 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